Skip to content

Pointer and Reference

squid233 edited this page Nov 14, 2022 · 7 revisions

The pointer type of method parameters are all converted to Addressable, or MemoryAddress for return type.

Access and Assign

No-const pointer types can be assigned from native functions. We overload it to array type.

// C: void load(int* result)
void load(Addressable result);
void load(SegmentAllocator allocator, int[] result) {
    MemorySegment seg = allocator.allocate(JAVA_INT);
    load(seg);
    result[0] = seg.get(JAVA_INT, 0);
}

This also applied to array in C.

// C: void get(int* results);
void get(Addressable results);
void get(SegmentAllocator allocator, int[] results) {
    MemorySegment seg = allocator.allocateArray(JAVA_INT, results/* .length */);
    get(results);
    RuntimeHelper.toArray(seg, results);
}

For const array type, simply allocate an array.

// C: void set(int* const values);
void set(Addressable values);
void set(SegmentAllocator allocator, int[] values) {
    set(allocator.allocateArray(JAVA_INT, values));
}
Clone this wiki locally