use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addFloatAndFloatsFromStructWithNestedStructArray_1.
@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray_1() throws Throwable {
GroupLayout floatStruct = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct);
GroupLayout structLayout = MemoryLayout.structLayout(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);
Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(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);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addBoolAndBoolsFromStructWithXor_1.
@Test
public void test_addBoolAndBoolsFromStructWithXor_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
VarHandle boolHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
VarHandle boolHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithXor").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
boolHandle1.set(structSegmt, (byte) 0);
boolHandle2.set(structSegmt, (byte) 1);
boolean result = (boolean) mh.invokeExact(false, structSegmt);
Assert.assertEquals(result, true);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName_1.
@Test
public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable {
GroupLayout intStruct = MemoryLayout.structLayout(C_INT, C_INT);
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct);
GroupLayout structLayout = MemoryLayout.structLayout(structArray, C_INT);
MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111);
MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222);
MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333);
MemoryAccess.setIntAtOffset(structSegmt, 12, 4444444);
MemoryAccess.setIntAtOffset(structSegmt, 16, 5555555);
int result = (int) mh.invokeExact(6666666, structSegmt);
Assert.assertEquals(result, 23333331);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addLongFromPointerAndLongsFromStruct_1.
@Test
public void test_addLongFromPointerAndLongsFromStruct_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(longLayout.withName("elem1"), longLayout.withName("elem2"));
VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1"));
VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(long.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment longSegmt = allocator.allocate(longLayout);
MemoryAccess.setLong(longSegmt, 1111111111L);
MemorySegment structSegmt = allocator.allocate(structLayout);
longHandle1.set(structSegmt, 3333333333L);
longHandle2.set(structSegmt, 5555555555L);
long result = (long) mh.invokeExact(longSegmt.address(), structSegmt);
Assert.assertEquals(result, 9999999999L);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer_1.
@Test
public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment doubleSegmt = allocator.allocate(C_DOUBLE);
MemoryAccess.setDouble(doubleSegmt, 212.123D);
MemorySegment structSegmt = allocator.allocate(structLayout);
doubleHandle1.set(structSegmt, 218.456D);
doubleHandle2.set(structSegmt, 219.789D);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(doubleSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(C_DOUBLE.byteSize(), scope);
VarHandle doubleHandle = MemoryHandles.varHandle(double.class, ByteOrder.nativeOrder());
double result = (double) doubleHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 650.368D, 0.001D);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), doubleSegmt.address().toRawLongValue());
}
}
Aggregations