PointerBase,
i.e., it is a word type. There is never a Java class that implements the interface.
Field accesses are done via interface methods that are annotated with CField,
CFieldAddress, or CFieldOffset. All calls of the interface methods are replaced
with the appropriate memory or address arithmetic operations. Here is an example to define a
complex number structure:
@CStruct
interface ComplexValue extends PointerBase {
@CField("re")
double realPart();
@CField("re")
void realPart(double re);
@CField("im")
double imagineryPart();
@CField("im")
void imagineryPart(double im);
}
The annotated interface, or an outer class that contains the interface, must be annotated with
CContext. Allocate an instances of the struct either by
org.graalvm.nativeimage.StackValue#get(java.lang.Class) or by
org.graalvm.nativeimage.UnmanagedMemory#malloc(org.graalvm.word.UnsignedWord) .
To access an array of structs one can define a special addressOf method:
@CStruct("int_double")
interface IntOrDouble extends PointerBase {
// allows access to individual structs in an array
IntOrDouble addressOf(int index);
@CField
int i();
@CField
void i(int i);
@CField
double d();
@CField
void d(double d);
}
Implementation of such method then allows one to do array arithmetics - e.g. obtain
pointer to the first element of the array and then access the others:
private static double acceptIntIntDouble(IntOrDouble arr) {
IntOrDouble firstInt = arr.addressOf(0);
IntOrDouble secondInt = arr.addressOf(1);
IntOrDouble thirdDouble = arr.addressOf(2);
return firstInt.i() + secondInt.i() + thirdDouble.d();
}
org.graalvm.nativeimage.StackValue , org.graalvm.nativeimage.UnmanagedMemory
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public boolean | |
| public String |
| addStructKeyword | back to summary |
|---|---|
| public boolean addStructKeyword() Add the C "struct" keyword to the name specified in
| |
| isIncomplete | back to summary |
|---|---|
| public boolean isIncomplete() If marked as incomplete, we will not try to determine the size of the struct.
| |
| value | back to summary |
|---|---|
| public String value() Specifies the name of the imported C struct type. If no name is provided, the type name is used as the struct name.
| |