posix
Class CPtr

java.lang.Object
  extended by posix.CPtr
Direct Known Subclasses:
Malloc

public class CPtr
extends Object

Read and write memory reachable through a C ptr. Memory access is bounds checked to keep it within the size of region addressed by the C ptr. Only classes within the posix package can create CPtr objects, and the use of this class is completely safe at present. This is because we know the size of, for instance, shared memory segments or memory allocated with C malloc. At some future date, we may need the ability to dereference arbitrary C pointers - those dereferences will be unsafe.

We have not yet implemented the floating and 64-bit types for get/set due to a lack of need.


Nested Class Summary
static class CPtr.Struct
          Compute the offsets of a C struct one member at a time.
 
Field Summary
static int CBYTE_TYPE
          Type codes for alignOf() and sizeOf().
static int CDBL_TYPE
          Type codes for alignOf() and sizeOf().
static int CFLT_TYPE
          Type codes for alignOf() and sizeOf().
static int CINT_TYPE
          Type codes for alignOf() and sizeOf().
static int CLONG_TYPE
          Type codes for alignOf() and sizeOf().
static int CPTR_TYPE
          Type codes for alignOf() and sizeOf().
static int CSHORT_TYPE
          Type codes for alignOf() and sizeOf().
 
Method Summary
static int alignOf(int type)
          Get the alignment of a C type.
 void copyIn(int off, byte[] ba, int pos, int cnt)
          Copy a Java byte array into C memory.
 void copyOut(int off, byte[] ba, int pos, int cnt)
          Copy bytes out of C memory into a Java byte array.
 byte getByte(int off)
           
 int getCInt(int off)
           
 int getCInt(int off, int idx)
           
 short getCShort(int off)
           
 short getCShort(int off, int idx)
           
 int getInt(int off)
           
 short getShort(int off)
           
 void setByte(int off, byte val)
           
 void setCInt(int off, int val)
           
 void setCInt(int off, int idx, int val)
           
 void setCShort(int off, int idx, short val)
           
 void setCShort(int off, short val)
           
 void setInt(int off, int val)
           
 void setShort(int off, short val)
           
static int sizeOf(int type)
          Get the size of a C type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CBYTE_TYPE

public static final int CBYTE_TYPE
Type codes for alignOf() and sizeOf().

See Also:
Constant Field Values

CSHORT_TYPE

public static final int CSHORT_TYPE
Type codes for alignOf() and sizeOf().

See Also:
Constant Field Values

CINT_TYPE

public static final int CINT_TYPE
Type codes for alignOf() and sizeOf().

See Also:
Constant Field Values

CLONG_TYPE

public static final int CLONG_TYPE
Type codes for alignOf() and sizeOf().

See Also:
Constant Field Values

CFLT_TYPE

public static final int CFLT_TYPE
Type codes for alignOf() and sizeOf().

See Also:
Constant Field Values

CDBL_TYPE

public static final int CDBL_TYPE
Type codes for alignOf() and sizeOf().

See Also:
Constant Field Values

CPTR_TYPE

public static final int CPTR_TYPE
Type codes for alignOf() and sizeOf().

See Also:
Constant Field Values
Method Detail

alignOf

public static int alignOf(int type)
Get the alignment of a C type. Can be used to compute C struct offsets in a mostly system independent manner.


sizeOf

public static int sizeOf(int type)
Get the size of a C type. Can be used to compute C struct offsets in a mostly system independent manner.


copyOut

public void copyOut(int off,
                    byte[] ba,
                    int pos,
                    int cnt)
Copy bytes out of C memory into a Java byte array.


copyIn

public void copyIn(int off,
                   byte[] ba,
                   int pos,
                   int cnt)
Copy a Java byte array into C memory.


getByte

public byte getByte(int off)

setByte

public void setByte(int off,
                    byte val)

getShort

public short getShort(int off)

setShort

public void setShort(int off,
                     short val)

getInt

public int getInt(int off)

setInt

public void setInt(int off,
                   int val)

getCShort

public short getCShort(int off,
                       int idx)

setCShort

public void setCShort(int off,
                      int idx,
                      short val)

getCInt

public int getCInt(int off,
                   int idx)

setCInt

public void setCInt(int off,
                    int idx,
                    int val)

getCShort

public short getCShort(int off)

setCShort

public void setCShort(int off,
                      short val)

getCInt

public int getCInt(int off)

setCInt

public void setCInt(int off,
                    int val)