use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addByteFromPointerAndBytesFromStruct_returnBytePointer_1.
@Test
public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment byteSegmt = allocator.allocate(C_CHAR);
MemoryAccess.setByte(byteSegmt, (byte) 12);
MemorySegment structSegmt = allocator.allocate(structLayout);
byteHandle1.set(structSegmt, (byte) 18);
byteHandle2.set(structSegmt, (byte) 19);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(byteSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(C_CHAR.byteSize(), scope);
VarHandle byteHandle = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder());
byte result = (byte) byteHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 49);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), byteSegmt.address().toRawLongValue());
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addDoubleAndDoublesFromNestedStruct_reverseOrder_1.
@Test
public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder_1() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"));
GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2"));
MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setDoubleAtOffset(structSegmt, 0, 31.789D);
MemoryAccess.setDoubleAtOffset(structSegmt, 8, 33.456D);
MemoryAccess.setDoubleAtOffset(structSegmt, 16, 35.123D);
double result = (double) mh.invokeExact(37.864D, structSegmt);
Assert.assertEquals(result, 138.232D, 0.001D);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_1.
@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable {
GroupLayout boolStruct = MemoryLayout.structLayout(C_CHAR, C_CHAR);
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct);
GroupLayout structLayout = MemoryLayout.structLayout(structArray, C_CHAR);
MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").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) 1);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 0);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 1);
MemoryAccess.setByteAtOffset(structSegmt, 3, (byte) 0);
MemoryAccess.setByteAtOffset(structSegmt, 4, (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_addFloatAndFloatsFromStructPointer_1.
@Test
public void test_addFloatAndFloatsFromStructPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(float.class, float.class, MemoryAddress.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_POINTER);
Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
floatHandle1.set(structSegmt, 35.11F);
floatHandle2.set(structSegmt, 46.22F);
float result = (float) mh.invokeExact(79.33F, structSegmt.address());
Assert.assertEquals(result, 160.66F, 0.01F);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_1.
@Test
public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_1() throws Throwable {
SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, C_CHAR);
GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(C_CHAR.bitSize()));
MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").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) 0);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 1);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 0);
boolean result = (boolean) mh.invokeExact(false, structSegmt);
Assert.assertEquals(result, true);
}
}
Aggregations