use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests method test_memoryAllocFreeFromDefaultLib_fromMemAddr.
@Test
public void test_memoryAllocFreeFromDefaultLib_fromMemAddr() throws Throwable {
Symbol allocSymbol = defaultLib.lookup("malloc").get();
MemoryAddress allocMemAddrFromSymbol = allocSymbol.address();
MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
MethodHandle allocHandle = clinker.downcallHandle(allocMemAddrFromSymbol, allocMethodType, allocFuncDesc);
MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
long allocMemAddrValue = allocMemAddr.toRawLongValue();
MemorySegment memSeg = MemorySegment.ofNativeRestricted();
MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 15);
Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 15);
Symbol freeSymbol = defaultLib.lookup("free").get();
MemoryAddress freeMemAddr = freeSymbol.address();
MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
MethodHandle freeHandle = clinker.downcallHandle(freeMemAddr, freeMethodType, freeFuncDesc);
freeHandle.invokeExact(allocMemAddr);
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests method test_strlenFromDefaultLibWithMemAddr_fromMemAddr.
@Test
public void test_strlenFromDefaultLibWithMemAddr_fromMemAddr() throws Throwable {
Symbol strlenSymbol = defaultLib.lookup("strlen").get();
MemoryAddress memAddr = strlenSymbol.address();
MethodType mt = MethodType.methodType(long.class, MemoryAddress.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER);
MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
MemorySegment funcMemSegment = CLinker.toCString("JEP389 DOWNCALL TEST SUITES");
long strLength = (long) mh.invokeExact(funcMemSegment.address());
Assert.assertEquals(strLength, 27);
FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), C_POINTER.withName("pointer"));
mh = clinker.downcallHandle(memAddr, mt, fd2);
strLength = (long) mh.invokeExact(funcMemSegment.address());
Assert.assertEquals(strLength, 27);
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests method test_memoryAllocFreeFromDefaultLib.
@Test
public void test_memoryAllocFreeFromDefaultLib() throws Throwable {
Symbol allocSymbol = defaultLib.lookup("malloc").get();
MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
MethodHandle allocHandle = clinker.downcallHandle(allocSymbol, allocMethodType, allocFuncDesc);
MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
long allocMemAddrValue = allocMemAddr.toRawLongValue();
MemorySegment memSeg = MemorySegment.ofNativeRestricted();
MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 15);
Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 15);
Symbol freeSymbol = defaultLib.lookup("free").get();
MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
MethodHandle freeHandle = clinker.downcallHandle(freeSymbol, freeMethodType, freeFuncDesc);
freeHandle.invokeExact(allocMemAddr);
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests method test_addTwoIntsReturnVoid_fromMemAddr.
@Test
public void test_addTwoIntsReturnVoid_fromMemAddr() throws Throwable {
MethodType mt = MethodType.methodType(void.class, int.class, int.class);
FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT);
Symbol functionSymbol = nativeLib.lookup("add2IntsReturnVoid").get();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
mh.invokeExact(454, 398);
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests method test_memoryAllocFreeFromCLinkerMethod.
@Test
public void test_memoryAllocFreeFromCLinkerMethod() throws Throwable {
MemoryAddress allocMemAddr = CLinker.allocateMemoryRestricted(10L);
long allocMemAddrValue = allocMemAddr.toRawLongValue();
MemorySegment memSeg = MemorySegment.ofNativeRestricted();
MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 49);
Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 49);
CLinker.freeMemoryRestricted(allocMemAddr);
}
Aggregations