Skip to content

Pointer and Reference

squid233 edited this page Jan 3, 2025 · 6 revisions

The pointer type of method parameters are all converted to MemorySegment.

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(MemorySegment result);
void load(SegmentAllocator allocator, @Out int[] result) {
    MemorySegment seg = allocator.allocate(JAVA_INT);
    load(seg);
    result[0] = seg.get(JAVA_INT, 0);
}

This also applies to array in C.

// C: void get(int* results);
void get(MemorySegment results);
void get(SegmentAllocator allocator, @Out int[] results) {
    MemorySegment seg = Marshal.marshal(allocator, results);
    get(seg);
    Unmarshal.copy(seg, results);
}

For const array type, simply allocate an array.

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