use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests1 method test_memoryAllocFreeFromDefaultLib_1.
@Test
public void test_memoryAllocFreeFromDefaultLib_1() throws Throwable {
/* Temporarily disable the default library loading on AIX till we figure out a way
* around to handle the case as the official implementation in OpenJDK17 doesn't
* help to load the static libray (libc.a).
*/
if (!isAixOS) {
NativeSymbol allocSymbol = clinker.lookup("malloc").get();
FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(ADDRESS, JAVA_LONG);
MethodHandle allocHandle = clinker.downcallHandle(allocSymbol, allocFuncDesc);
MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
allocMemAddr.set(JAVA_INT, 0, 15);
Assert.assertEquals(allocMemAddr.get(JAVA_INT, 0), 15);
NativeSymbol freeSymbol = clinker.lookup("free").get();
FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(ADDRESS);
MethodHandle freeHandle = clinker.downcallHandle(freeSymbol, freeFuncDesc);
freeHandle.invoke(allocMemAddr);
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1.
@Test
public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"));
VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333);
MemorySegment structSegmt = allocator.allocate(structLayout);
intHandle1.set(structSegmt, 4455666);
intHandle2.set(structSegmt, 7788999);
MemoryAddress resultAddr = (MemoryAddress) mh.invoke(intSegmt, structSegmt);
Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 13366998);
Assert.assertEquals(resultAddr.toRawLongValue(), intSegmt.address().toRawLongValue());
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests2 method test_addLongFromPointerAndLongsFromStruct_returnLongPointer_2.
@Test
public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_2() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(longLayout.withName("elem1"), longLayout.withName("elem2"));
VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1"));
VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment longSegmt = allocator.allocate(longLayout);
MemoryAccess.setLong(longSegmt, 1122334455L);
MemorySegment structSegmt = allocator.allocate(structLayout);
longHandle1.set(structSegmt, 6677889900L);
longHandle2.set(structSegmt, 1234567890L);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, longSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(longLayout.byteSize(), scope);
VarHandle longHandle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
long result = (long) longHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 9034792245L);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), longSegmt.address().toRawLongValue());
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests2 method test_addShortFromPointerAndShortsFromStruct_returnShortPointer_2.
@Test
public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer_2() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointer").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment shortSegmt = allocator.allocate(C_SHORT);
MemoryAccess.setShort(shortSegmt, (short) 12);
MemorySegment structSegmt = allocator.allocate(structLayout);
shortHandle1.set(structSegmt, (short) 18);
shortHandle2.set(structSegmt, (short) 19);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, shortSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(C_SHORT.byteSize(), scope);
VarHandle shortHandle = MemoryHandles.varHandle(short.class, ByteOrder.nativeOrder());
short result = (short) shortHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 49);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), shortSegmt.address().toRawLongValue());
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_addLongFromPointerAndLongsFromStruct_returnLongPointer_1.
@Test
public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"));
VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L);
MemorySegment structSegmt = allocator.allocate(structLayout);
longHandle1.set(structSegmt, 6677889900L);
longHandle2.set(structSegmt, 1234567890L);
MemoryAddress resultAddr = (MemoryAddress) mh.invoke(longSegmt, structSegmt);
Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 9034792245L);
Assert.assertEquals(resultAddr.toRawLongValue(), longSegmt.address().toRawLongValue());
}
}
Aggregations