use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_1.
@Test
public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN);
FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope);
boolSegmt.set(JAVA_BOOLEAN, 0, false);
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
structSegmt.set(JAVA_BOOLEAN, 0, false);
structSegmt.set(JAVA_BOOLEAN, 1, true);
MemoryAddress resultAddr = (MemoryAddress) mh.invoke(boolSegmt, structSegmt);
Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true);
Assert.assertEquals(resultAddr.toRawLongValue(), boolSegmt.address().toRawLongValue());
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_add2ByteStructs_returnStructPointer_1.
@Test
public void test_add2ByteStructs_returnStructPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"));
VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
byteHandle1.set(structSegmt1, (byte) 25);
byteHandle2.set(structSegmt1, (byte) 11);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
byteHandle1.set(structSegmt2, (byte) 24);
byteHandle2.set(structSegmt2, (byte) 13);
MemoryAddress resultAddr = (MemoryAddress) mh.invoke(structSegmt1, structSegmt2);
Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49);
Assert.assertEquals(resultAddr.get(JAVA_BYTE, 1), 24);
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests1 method test_memoryAllocFreeFromDefaultLib_fromMemAddr_1.
@Test
public void test_memoryAllocFreeFromDefaultLib_fromMemAddr_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) {
Addressable allocSymbol = defaultLibLookup.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);
MemorySegment memSeg = allocMemAddr.asSegment(10L, resourceScope);
MemoryAccess.setIntAtOffset(memSeg, 0, 15);
Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, 0), 15);
Addressable freeSymbol = defaultLibLookup.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 PrimitiveTypeTests1 method test_addTwoBytes_fromMemAddr_1.
@Test
public void test_addTwoBytes_fromMemAddr_1() throws Throwable {
MethodType mt = MethodType.methodType(byte.class, byte.class, byte.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
Addressable functionSymbol = nativeLibLookup.lookup("add2Bytes").get();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
byte result = (byte) mh.invokeExact((byte) 6, (byte) 3);
Assert.assertEquals(result, (byte) 9);
}
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) {
Addressable allocSymbol = defaultLibLookup.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);
MemorySegment memSeg = allocMemAddr.asSegment(10L, resourceScope);
MemoryAccess.setIntAtOffset(memSeg, 0, 15);
Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, 0), 15);
Addressable freeSymbol = defaultLibLookup.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);
}
}
Aggregations