use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addLongFromPointerAndLongsFromStruct_returnLongPointer.
@Test
public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(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);
Symbol functionSymbol = nativeLib.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment longSegmt = MemorySegment.allocateNative(longLayout);
MemoryAccess.setLong(longSegmt, 1122334455L);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
longHandle1.set(structSegmt, 6677889900L);
longHandle2.set(structSegmt, 1234567890L);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(longSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(longLayout.byteSize());
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());
longSegmt.close();
structSegmt.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addByteFromPointerAndBytesFromStruct_returnBytePointer.
@Test
public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment byteSegmt = MemorySegment.allocateNative(C_CHAR);
MemoryAccess.setByte(byteSegmt, (byte) 12);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
byteHandle1.set(structSegmt, (byte) 18);
byteHandle2.set(structSegmt, (byte) 19);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(byteSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_CHAR.byteSize());
VarHandle byteHandle = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder());
byte result = (byte) byteHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 49);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), byteSegmt.address().toRawLongValue());
byteSegmt.close();
structSegmt.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addIntAndLongIntFromStruct.
@Test
public void test_addIntAndLongIntFromStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), C_INT.withName("elem2"), MemoryLayout.ofPaddingBits(C_INT.bitSize()));
VarHandle elemHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1"));
VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(long.class, int.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_INT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addIntAndLongIntFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
elemHandle1.set(structSegmt, 667788990011L);
elemHandle2.set(structSegmt, 11223344);
long result = (long) mh.invokeExact(1234567, structSegmt);
Assert.assertEquals(result, 667801447922L);
structSegmt.close();
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addByteAndBytesFromStruct.
@Test
public void test_addByteAndBytesFromStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
byteHandle1.set(structSegmt, (byte) 8);
byteHandle2.set(structSegmt, (byte) 9);
byte result = (byte) mh.invokeExact((byte) 6, structSegmt);
Assert.assertEquals(result, 23);
structSegmt.close();
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addByteAndBytesFromNestedStruct_reverseOrder.
@Test
public void test_addByteAndBytesFromNestedStruct_reverseOrder() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize()));
MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromNestedStruct_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 12);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 24);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 36);
byte result = (byte) mh.invokeExact((byte) 48, structSegmt);
Assert.assertEquals(result, 120);
structSegmt.close();
}
Aggregations