52 * The layout for the {@code int} C type
53 */
54 public static final ValueLayout.OfInt C_INT = ValueLayout.JAVA_INT;
55
56 /**
57 * The layout for the {@code long long} C type.
58 */
59 public static final ValueLayout.OfLong C_LONG_LONG = ValueLayout.JAVA_LONG;
60 /**
61 * The layout for the {@code float} C type
62 */
63 public static final ValueLayout.OfFloat C_FLOAT = ValueLayout.JAVA_FLOAT;
64 /**
65 * The layout for the {@code double} C type
66 */
67 public static final ValueLayout.OfDouble C_DOUBLE = ValueLayout.JAVA_DOUBLE;
68 /**
69 * The {@code T*} native type.
70 */
71 public static final AddressLayout C_POINTER = ValueLayout.ADDRESS
72 .withTargetLayout(MemoryLayout.sequenceLayout(C_CHAR));
73
74 private static Linker LINKER = Linker.nativeLinker();
75
76 private static final MethodHandle FREE = LINKER.downcallHandle(
77 LINKER.defaultLookup().find("free").get(), FunctionDescriptor.ofVoid(C_POINTER));
78
79 private static final MethodHandle MALLOC = LINKER.downcallHandle(
80 LINKER.defaultLookup().find("malloc").get(), FunctionDescriptor.of(C_POINTER, ValueLayout.JAVA_LONG));
81
82 public static void freeMemory(MemorySegment address) {
83 try {
84 FREE.invokeExact(address);
85 } catch (Throwable ex) {
86 throw new IllegalStateException(ex);
87 }
88 }
89
90 public static MemorySegment allocateMemory(long size) {
91 try {
92 return (MemorySegment)MALLOC.invokeExact(size);
|
52 * The layout for the {@code int} C type
53 */
54 public static final ValueLayout.OfInt C_INT = ValueLayout.JAVA_INT;
55
56 /**
57 * The layout for the {@code long long} C type.
58 */
59 public static final ValueLayout.OfLong C_LONG_LONG = ValueLayout.JAVA_LONG;
60 /**
61 * The layout for the {@code float} C type
62 */
63 public static final ValueLayout.OfFloat C_FLOAT = ValueLayout.JAVA_FLOAT;
64 /**
65 * The layout for the {@code double} C type
66 */
67 public static final ValueLayout.OfDouble C_DOUBLE = ValueLayout.JAVA_DOUBLE;
68 /**
69 * The {@code T*} native type.
70 */
71 public static final AddressLayout C_POINTER = ValueLayout.ADDRESS
72 .withTargetLayout(MemoryLayout.sequenceLayout(Long.MAX_VALUE, C_CHAR));
73
74 private static Linker LINKER = Linker.nativeLinker();
75
76 private static final MethodHandle FREE = LINKER.downcallHandle(
77 LINKER.defaultLookup().find("free").get(), FunctionDescriptor.ofVoid(C_POINTER));
78
79 private static final MethodHandle MALLOC = LINKER.downcallHandle(
80 LINKER.defaultLookup().find("malloc").get(), FunctionDescriptor.of(C_POINTER, ValueLayout.JAVA_LONG));
81
82 public static void freeMemory(MemorySegment address) {
83 try {
84 FREE.invokeExact(address);
85 } catch (Throwable ex) {
86 throw new IllegalStateException(ex);
87 }
88 }
89
90 public static MemorySegment allocateMemory(long size) {
91 try {
92 return (MemorySegment)MALLOC.invokeExact(size);
|