use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addIntAndIntShortFromStruct.
@Test
public void test_addIntAndIntShortFromStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_SHORT.withName("elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize()));
VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
VarHandle elemHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addIntAndIntShortFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
elemHandle1.set(structSegmt, 11223344);
elemHandle2.set(structSegmt, (short) 32766);
int result = (int) mh.invokeExact(22334455, structSegmt);
Assert.assertEquals(result, 33590565);
structSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addByteFromPointerAndBytesFromStruct.
@Test
public void test_addByteFromPointerAndBytesFromStruct() 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, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("addByteFromPointerAndBytesFromStruct").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) 14);
byteHandle2.set(structSegmt, (byte) 16);
byte result = (byte) mh.invokeExact(byteSegmt.address(), structSegmt);
Assert.assertEquals(result, 42);
byteSegmt.close();
structSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName.
@Test
public void test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName() throws Throwable {
SequenceLayout charArray = MemoryLayout.ofSequence(2, C_SHORT);
GroupLayout structLayout = MemoryLayout.ofStruct(charArray, C_SHORT, MemoryLayout.ofPaddingBits(C_SHORT.bitSize()));
MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromStructWithNestedCharArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setCharAtOffset(structSegmt, 0, 'A');
MemoryAccess.setCharAtOffset(structSegmt, 2, 'B');
MemoryAccess.setCharAtOffset(structSegmt, 4, 'C');
char result = (char) mh.invokeExact('D', structSegmt);
Assert.assertEquals(result, 'G');
structSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_add2FloatStructs_returnStruct.
@Test
public void test_add2FloatStructs_returnStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Symbol functionSymbol = nativeLib.lookup("add2FloatStructs_returnStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
floatHandle1.set(structSegmt1, 25.12F);
floatHandle2.set(structSegmt1, 11.23F);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
floatHandle1.set(structSegmt2, 24.34F);
floatHandle2.set(structSegmt2, 13.45F);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder.
@Test
public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder() throws Throwable {
GroupLayout intStruct = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), structArray.withName("struct_array_elem2"));
MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedStructArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111);
MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222);
MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333);
MemoryAccess.setIntAtOffset(structSegmt, 12, 4444444);
MemoryAccess.setIntAtOffset(structSegmt, 16, 5555555);
int result = (int) mh.invokeExact(6666666, structSegmt);
Assert.assertEquals(result, 23333331);
structSegmt.close();
}
Aggregations