1 // Generated by jextract 2 3 import java.lang.invoke.VarHandle; 4 import jdk.incubator.foreign.MemoryAccess; 5 import jdk.incubator.foreign.MemoryAddress; 6 import jdk.incubator.foreign.MemoryCopy; 7 import jdk.incubator.foreign.MemoryLayout; 8 import jdk.incubator.foreign.MemorySegment; 9 import jdk.incubator.foreign.SegmentAllocator; 10 import static jdk.incubator.foreign.CLinker.*; 11 12 public class C-X { 13 C-X() {} 14 15 public static MemorySegment allocate(SegmentAllocator allocator) { 16 return allocator.allocate(LAYOUT); 17 } 18 19 public static MemorySegment allocate(${CARRIER} val, SegmentAllocator allocator) { 20 MemorySegment segment = allocator.allocate(LAYOUT); 21 MemoryAccess.set${CAPITALIZED_CARRIER}AtOffset(segment, 0, val); 22 return segment; 23 } 24 25 public static MemorySegment allocateArray(long size, SegmentAllocator allocator) { 26 return allocator.allocateArray(LAYOUT, size); 27 } 28 29 public static MemorySegment allocateArray(${CARRIER}[] values, SegmentAllocator allocator) { 30 MemorySegment segment = allocator.allocateArray(LAYOUT, values.length); 31 MemoryCopy.copyFromArray(values, 0, values.length, segment, 0); 32 return segment; 33 } 34 35 public static ${CARRIER} get(MemorySegment segment, long offset) { 36 return MemoryAccess.get${CAPITALIZED_CARRIER}AtOffset(segment, offset); 37 } 38 39 public static void set(MemorySegment segment, long offset, ${CARRIER} value) { 40 MemoryAccess.set${CAPITALIZED_CARRIER}AtOffset(segment, offset, value); 41 } 42 43 public static long sizeof() { 44 return LAYOUT.byteSize(); 45 } 46 47 public static ${CARRIER}[] toJavaArray(MemorySegment seg) { 48 var segSize = seg.byteSize(); 49 var elemSize = sizeof(); 50 if (segSize % elemSize != 0) { 51 throw new UnsupportedOperationException("segment cannot contain integral number of elements"); 52 } 53 ${CARRIER}[] array = new ${CARRIER}[(int) (segSize / elemSize)]; 54 MemoryCopy.copyToArray(seg, 0, array, 0, array.length); 55 return array; 56 } 57 58 public static final MemoryLayout LAYOUT = ${LAYOUT}; 59 }