use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addIntAndIntsFromStruct.
@Test
public void test_addIntAndIntsFromStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
VarHandle intHandle2 = structLayout.varHandle(int.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("addIntAndIntsFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
intHandle1.set(structSegmt, 1122334);
intHandle2.set(structSegmt, 1234567);
int result = (int) mh.invokeExact(2244668, structSegmt);
Assert.assertEquals(result, 4601569);
structSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName.
@Test
public void test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName() throws Throwable {
SequenceLayout byteArray = MemoryLayout.ofSequence(2, C_CHAR);
GroupLayout structLayout = MemoryLayout.ofStruct(byteArray, C_CHAR);
MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedByteArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 11);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 12);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 13);
byte result = (byte) mh.invokeExact((byte) 14, structSegmt);
Assert.assertEquals(result, 50);
structSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName.
@Test
public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName() throws Throwable {
GroupLayout intStruct = MemoryLayout.ofStruct(C_INT, C_INT);
SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_INT);
MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedStructArray").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();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder.
@Test
public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder() throws Throwable {
SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT);
GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), intArray.withName("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("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 0);
MemoryAccess.setIntAtOffset(structSegmt, 4, 1);
MemoryAccess.setIntAtOffset(structSegmt, 8, 0);
int result = (int) mh.invokeExact(0, structSegmt);
Assert.assertEquals(result, 1);
structSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addBoolFromPointerAndBoolsFromStructWithXor.
@Test
public void test_addBoolFromPointerAndBoolsFromStructWithXor() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
VarHandle boolHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
VarHandle boolHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(boolean.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("addBoolFromPointerAndBoolsFromStructWithXor").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment booleanSegmt = MemorySegment.allocateNative(C_INT);
MemoryAccess.setInt(booleanSegmt, 1);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
boolHandle1.set(structSegmt, 0);
boolHandle2.set(structSegmt, 1);
boolean result = (boolean) mh.invokeExact(booleanSegmt.address(), structSegmt);
Assert.assertEquals(result, false);
booleanSegmt.close();
structSegmt.close();
}
Aggregations