use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_addByteAndBytesFromNestedStruct_reverseOrder_2.
@Test
public void test_addByteAndBytesFromNestedStruct_reverseOrder_2() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(C_CHAR.bitSize()));
MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 12);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 24);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 36);
byte result = (byte) mh.invokeExact(functionSymbol, (byte) 48, structSegmt);
Assert.assertEquals(result, 120);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_2.
@Test
public void test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_2() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_CHAR, C_CHAR);
GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, C_CHAR);
MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromNestedStructWithXor").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 1);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 0);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 1);
boolean result = (boolean) mh.invokeExact(functionSymbol, true, structSegmt);
Assert.assertEquals(result, true);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_add2ShortStructs_returnStruct_2.
@Test
public void test_add2ShortStructs_returnStruct_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(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStruct").get();
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MethodHandle mh = clinker.downcallHandle(mt, fd);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
shortHandle1.set(structSegmt1, (short) 56);
shortHandle2.set(structSegmt1, (short) 45);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
shortHandle1.set(structSegmt2, (short) 78);
shortHandle2.set(structSegmt2, (short) 67);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2);
Assert.assertEquals((short) shortHandle1.get(resultSegmt), (short) 134);
Assert.assertEquals((short) shortHandle2.get(resultSegmt), (short) 112);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_addIntAndIntsFromStructWithNestedIntArray_2.
@Test
public void test_addIntAndIntsFromStructWithNestedIntArray_2() throws Throwable {
SequenceLayout intArray = MemoryLayout.sequenceLayout(2, C_INT);
GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), C_INT.withName("elem2"));
MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111);
MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222);
MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333);
int result = (int) mh.invokeExact(functionSymbol, 4444444, structSegmt);
Assert.assertEquals(result, 11111110);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_2.
@Test
public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_2() throws Throwable {
SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, C_CHAR);
GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(C_CHAR.bitSize()));
MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 0);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 1);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 0);
boolean result = (boolean) mh.invokeExact(functionSymbol, false, structSegmt);
Assert.assertEquals(result, true);
}
}
Aggregations