use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addDoubleFromPointerAndDoublesFromStruct.
@Test
public void test_addDoubleFromPointerAndDoublesFromStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"));
VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1"));
VarHandle doubleHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(double.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("addDoubleFromPointerAndDoublesFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment doubleSegmt = MemorySegment.allocateNative(C_DOUBLE);
MemoryAccess.setDouble(doubleSegmt, 112.123D);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
doubleHandle1.set(structSegmt, 118.456D);
doubleHandle2.set(structSegmt, 119.789D);
double result = (double) mh.invokeExact(doubleSegmt.address(), structSegmt);
Assert.assertEquals(result, 350.368D, 0.001D);
doubleSegmt.close();
structSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addIntFromPointerAndIntsFromStruct.
@Test
public void test_addIntFromPointerAndIntsFromStruct() 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, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("addIntFromPointerAndIntsFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment intSegmt = MemorySegment.allocateNative(C_INT);
MemoryAccess.setInt(intSegmt, 7654321);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
intHandle1.set(structSegmt, 1234567);
intHandle2.set(structSegmt, 2468024);
int result = (int) mh.invokeExact(intSegmt.address(), structSegmt);
Assert.assertEquals(result, 11356912);
structSegmt.close();
intSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addCharAndCharsFromStructWithNestedCharArray.
@Test
public void test_addCharAndCharsFromStructWithNestedCharArray() throws Throwable {
SequenceLayout charArray = MemoryLayout.ofSequence(2, C_SHORT);
GroupLayout structLayout = MemoryLayout.ofStruct(charArray.withName("array_elem1"), C_SHORT.withName("elem2"), 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_addShortAndShortsFromStructWithNestedStructArray.
@Test
public void test_addShortAndShortsFromStructWithNestedStructArray() throws Throwable {
GroupLayout shortStruct = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
SequenceLayout structArray = MemoryLayout.ofSequence(2, shortStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struc_array_elem1"), C_SHORT.withName("elem2"));
MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedStructArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setShortAtOffset(structSegmt, 0, (short) 111);
MemoryAccess.setShortAtOffset(structSegmt, 2, (short) 222);
MemoryAccess.setShortAtOffset(structSegmt, 4, (short) 333);
MemoryAccess.setShortAtOffset(structSegmt, 6, (short) 444);
MemoryAccess.setShortAtOffset(structSegmt, 8, (short) 555);
short result = (short) mh.invokeExact((short) 666, structSegmt);
Assert.assertEquals(result, 2331);
structSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_add2IntStructs_returnStruct.
@Test
public void test_add2IntStructs_returnStruct() 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(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Symbol functionSymbol = nativeLib.lookup("add2IntStructs_returnStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
intHandle1.set(structSegmt1, 11223344);
intHandle2.set(structSegmt1, 55667788);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
intHandle1.set(structSegmt2, 99001122);
intHandle2.set(structSegmt2, 33445566);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals(intHandle1.get(resultSegmt), 110224466);
Assert.assertEquals(intHandle2.get(resultSegmt), 89113354);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
Aggregations