use of jdk.incubator.foreign.SequenceLayout 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.SequenceLayout 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.SequenceLayout in project openj9 by eclipse.
the class StructTests method test_addBoolAndBoolsFromStructWithNestedStructArray.
@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray() throws Throwable {
GroupLayout intStruct = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_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);
Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedStructArray").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);
MemoryAccess.setIntAtOffset(structSegmt, 12, 1);
MemoryAccess.setIntAtOffset(structSegmt, 16, 0);
int result = (int) mh.invokeExact(1, structSegmt);
Assert.assertEquals(result, 1);
structSegmt.close();
}
use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests method test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName.
@Test
public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName() throws Throwable {
SequenceLayout shortArray = MemoryLayout.ofSequence(2, C_SHORT);
GroupLayout structLayout = MemoryLayout.ofStruct(shortArray, C_SHORT, MemoryLayout.ofPaddingBits(C_SHORT.bitSize()));
MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedShortArray").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);
short result = (short) mh.invokeExact((short) 444, structSegmt);
Assert.assertEquals(result, 1110);
structSegmt.close();
}
use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests method test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder.
@Test
public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder() throws Throwable {
GroupLayout byteStruct = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
SequenceLayout structArray = MemoryLayout.ofSequence(2, byteStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), structArray.withName("struct_array_elem2"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize() * 3));
MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 12);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 14);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 16);
MemoryAccess.setByteAtOffset(structSegmt, 3, (byte) 18);
MemoryAccess.setByteAtOffset(structSegmt, 4, (byte) 20);
byte result = (byte) mh.invokeExact((byte) 22, structSegmt);
Assert.assertEquals(result, 102);
structSegmt.close();
}
Aggregations