posix
Class CPtr.Struct

java.lang.Object
  extended by posix.CPtr.Struct
Enclosing class:
CPtr

public static class CPtr.Struct
extends Object

Compute the offsets of a C struct one member at a time. This is supposed to reflect what a C compiler would do. I can't think of a better way to track C data structs with code that doesn't get recompiled. A config file could do it, but would be even more work. Some C compilers will do surprising things - like padding structs that contain only 'char' members. They do this to avoid cache misses at the beginning of the struct - or to make struct pointers uniform on word addressable machines (e.g. PDP20). You can work around this for now with the addMember method - provided you can figure out when to do so. Please report any problems you encounter - we can add additional native methods, e.g. 'structAlign' to return minimum struct alignment.


Constructor Summary
CPtr.Struct(int offset)
          Initialize with the offset (within a CPtr) of the C struct.
 
Method Summary
 int addMember(int size, int mask)
          Add a member by size and alignment mask.
 int offsetOf(CPtr.Struct s, int cnt)
          Return the offset of a nested struct.
 int offsetOf(int type)
          Return the offset of the next member.
 int offsetOf(int type, int len)
          Return the offset of the next array member.
 int size()
          Return total struct size including padding to reflect maximum alignment.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CPtr.Struct

public CPtr.Struct(int offset)
Initialize with the offset (within a CPtr) of the C struct.

Method Detail

offsetOf

public final int offsetOf(int type)
Return the offset of the next member.


offsetOf

public final int offsetOf(int type,
                          int len)
Return the offset of the next array member.


addMember

public final int addMember(int size,
                           int mask)
Add a member by size and alignment mask. Return the member offset.


offsetOf

public final int offsetOf(CPtr.Struct s,
                          int cnt)
Return the offset of a nested struct. The members must have already been added to the nested struct so that it will have the proper size and alignment.


size

public final int size()
Return total struct size including padding to reflect maximum alignment.