use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addByteAndBytesFromNestedStruct.
@Test
public void test_addByteAndBytesFromNestedStruct() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout.withName("struct_elem1"), C_CHAR.withName("elem2"));
MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromNestedStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 11);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 22);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 33);
byte result = (byte) mh.invokeExact((byte) 46, structSegmt);
Assert.assertEquals(result, 112);
structSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_add2ByteStructs_returnStruct.
@Test
public void test_add2ByteStructs_returnStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
VarHandle byteHandle2 = structLayout.varHandle(byte.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("add2ByteStructs_returnStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
byteHandle1.set(structSegmt1, (byte) 25);
byteHandle2.set(structSegmt1, (byte) 11);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
byteHandle1.set(structSegmt2, (byte) 24);
byteHandle2.set(structSegmt2, (byte) 13);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals((byte) byteHandle1.get(resultSegmt), (byte) 49);
Assert.assertEquals((byte) byteHandle2.get(resultSegmt), (byte) 24);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout 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.GroupLayout in project openj9 by eclipse.
the class StructTests method test_add2BoolStructsWithXor_returnStruct.
@Test
public void test_add2BoolStructsWithXor_returnStruct() 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(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Symbol functionSymbol = nativeLib.lookup("add2BoolStructsWithXor_returnStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
boolHandle1.set(structSegmt1, 1);
boolHandle2.set(structSegmt1, 0);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
boolHandle1.set(structSegmt2, 1);
boolHandle2.set(structSegmt2, 1);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals(boolHandle1.get(resultSegmt), 0);
Assert.assertEquals(boolHandle2.get(resultSegmt), 1);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_add2BoolStructsWithXor_returnStructPointer.
@Test
public void test_add2BoolStructsWithXor_returnStructPointer() 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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("add2BoolStructsWithXor_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
boolHandle1.set(structSegmt1, 1);
boolHandle2.set(structSegmt1, 0);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
boolHandle1.set(structSegmt2, 1);
boolHandle2.set(structSegmt2, 1);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
Assert.assertEquals(boolHandle1.get(resultSegmt), 0);
Assert.assertEquals(boolHandle2.get(resultSegmt), 1);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
Aggregations