use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_add3ByteStructs_returnStruct_1.
@Test
public void test_add3ByteStructs_returnStruct_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"), C_CHAR.withName("elem3"), MemoryLayout.paddingLayout(C_CHAR.bitSize()));
VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
VarHandle byteHandle3 = structLayout.varHandle(byte.class, PathElement.groupElement("elem3"));
MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("add3ByteStructs_returnStruct").get();
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
byteHandle1.set(structSegmt1, (byte) 25);
byteHandle2.set(structSegmt1, (byte) 11);
byteHandle3.set(structSegmt1, (byte) 12);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
byteHandle1.set(structSegmt2, (byte) 24);
byteHandle2.set(structSegmt2, (byte) 13);
byteHandle3.set(structSegmt2, (byte) 16);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals((byte) byteHandle1.get(resultSegmt), (byte) 49);
Assert.assertEquals((byte) byteHandle2.get(resultSegmt), (byte) 24);
Assert.assertEquals((byte) byteHandle3.get(resultSegmt), (byte) 28);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addShortAndShortsFromNestedStruct_withoutLayoutName_1.
@Test
public void test_addShortAndShortsFromNestedStruct_withoutLayoutName_1() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_SHORT, C_SHORT);
GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, C_SHORT);
MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setShortAtOffset(structSegmt, 0, (short) 31);
MemoryAccess.setShortAtOffset(structSegmt, 2, (short) 33);
MemoryAccess.setShortAtOffset(structSegmt, 4, (short) 35);
short result = (short) mh.invokeExact((short) 37, structSegmt);
Assert.assertEquals(result, 136);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_add2ShortStructs_returnStructPointer_2.
@Test
public void test_add2ShortStructs_returnStructPointer_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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
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);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
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_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_2.
@Test
public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_2() throws Throwable {
SequenceLayout charArray = MemoryLayout.sequenceLayout(2, C_SHORT);
GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), charArray.withName("array_elem2"), MemoryLayout.paddingLayout(C_SHORT.bitSize()));
MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setCharAtOffset(structSegmt, 0, 'A');
MemoryAccess.setCharAtOffset(structSegmt, 2, 'B');
MemoryAccess.setCharAtOffset(structSegmt, 4, 'C');
char result = (char) mh.invokeExact(functionSymbol, 'D', structSegmt);
Assert.assertEquals(result, 'G');
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName_2.
@Test
public void test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable {
GroupLayout doubleStruct = MemoryLayout.structLayout(C_DOUBLE, C_DOUBLE);
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct);
GroupLayout structLayout = MemoryLayout.structLayout(structArray, C_DOUBLE);
MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setDoubleAtOffset(structSegmt, 0, 111.111D);
MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D);
MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D);
MemoryAccess.setDoubleAtOffset(structSegmt, 24, 444.444D);
MemoryAccess.setDoubleAtOffset(structSegmt, 32, 555.555D);
double result = (double) mh.invokeExact(functionSymbol, 666.666D, structSegmt);
Assert.assertEquals(result, 2333.331D, 0.001D);
}
}
Aggregations