use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_addFloatAndFloatsFromStructWithNestedStructArray.
@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray() throws Throwable {
GroupLayout floatStruct = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
SequenceLayout structArray = MemoryLayout.ofSequence(2, floatStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), C_FLOAT.withName("elem2"));
MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F);
MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F);
MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F);
MemoryAccess.setFloatAtOffset(structSegmt, 12, 444.44F);
MemoryAccess.setFloatAtOffset(structSegmt, 16, 555.55F);
float result = (float) mh.invokeExact(666.66F, structSegmt);
Assert.assertEquals(result, 2333.31F, 0.01F);
structSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_add3ByteStructs_returnStruct.
@Test
public void test_add3ByteStructs_returnStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"), C_CHAR.withName("elem3"), MemoryLayout.ofPaddingBits(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);
Symbol functionSymbol = nativeLib.lookup("add3ByteStructs_returnStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
byteHandle1.set(structSegmt1, (byte) 25);
byteHandle2.set(structSegmt1, (byte) 11);
byteHandle3.set(structSegmt1, (byte) 12);
MemorySegment structSegmt2 = MemorySegment.allocateNative(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);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests method test_add3CharStructs_returnStruct.
@Test
public void test_add3CharStructs_returnStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"));
VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2"));
VarHandle charHandle3 = structLayout.varHandle(char.class, PathElement.groupElement("elem3"));
MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Symbol functionSymbol = nativeLib.lookup("add3CharStructs_returnStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
charHandle1.set(structSegmt1, 'A');
charHandle2.set(structSegmt1, 'B');
charHandle3.set(structSegmt1, 'C');
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
charHandle1.set(structSegmt2, 'B');
charHandle2.set(structSegmt2, 'C');
charHandle3.set(structSegmt2, 'D');
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals(charHandle1.get(resultSegmt), 'B');
Assert.assertEquals(charHandle2.get(resultSegmt), 'D');
Assert.assertEquals(charHandle3.get(resultSegmt), 'F');
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests1 method test_addByteAndBytesFromNestedStruct_1.
@Test
public void test_addByteAndBytesFromNestedStruct_1() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
GroupLayout structLayout = MemoryLayout.structLayout(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);
Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(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);
}
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests1 method test_addByteAndBytesFromStructWithNestedByteArray_1.
@Test
public void test_addByteAndBytesFromStructWithNestedByteArray_1() throws Throwable {
SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, C_CHAR);
GroupLayout structLayout = MemoryLayout.structLayout(byteArray.withName("array_elem1"), C_CHAR.withName("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("addByteAndBytesFromStructWithNestedByteArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 11);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 12);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 13);
byte result = (byte) mh.invokeExact((byte) 14, structSegmt);
Assert.assertEquals(result, 50);
}
}
Aggregations