use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer_2.
@Test
public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer_2() 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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment floatSegmt = allocator.allocate(C_FLOAT);
MemoryAccess.setFloat(floatSegmt, 12.12F);
MemorySegment structSegmt = allocator.allocate(structLayout);
floatHandle1.set(structSegmt, 18.23F);
floatHandle2.set(structSegmt, 19.34F);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, floatSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(C_FLOAT.byteSize(), scope);
VarHandle floatHandle = MemoryHandles.varHandle(float.class, ByteOrder.nativeOrder());
float result = (float) floatHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 49.69F, 0.01F);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), floatSegmt.address().toRawLongValue());
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_2.
@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_2() 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(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(functionSymbol, false, structSegmt);
Assert.assertEquals(result, true);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_add2IntStructs_returnStructPointer_2.
@Test
public void test_add2IntStructs_returnStructPointer_2() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), C_INT.withName("elem2"));
VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
VarHandle intHandle2 = 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);
Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
intHandle1.set(structSegmt1, 11223344);
intHandle2.set(structSegmt1, 55667788);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
intHandle1.set(structSegmt2, 99001122);
intHandle2.set(structSegmt2, 33445566);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
Assert.assertEquals(intHandle1.get(resultSegmt), 110224466);
Assert.assertEquals(intHandle2.get(resultSegmt), 89113354);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addCharFromPointerAndCharsFromStruct_returnCharPointer_1.
@Test
public void test_addCharFromPointerAndCharsFromStruct_returnCharPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
VarHandle charHandle2 = structLayout.varHandle(char.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("addCharFromPointerAndCharsFromStruct_returnCharPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment charSegmt = allocator.allocate(C_SHORT);
MemoryAccess.setChar(charSegmt, 'D');
MemorySegment structSegmt = allocator.allocate(structLayout);
charHandle1.set(structSegmt, 'E');
charHandle2.set(structSegmt, 'F');
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(charSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(C_SHORT.byteSize(), scope);
VarHandle charHandle = MemoryHandles.varHandle(char.class, ByteOrder.nativeOrder());
char result = (char) charHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 'M');
Assert.assertEquals(resultSegmt.address().toRawLongValue(), charSegmt.address().toRawLongValue());
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests1 method test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_1.
@Test
public void test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_1() throws Throwable {
GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_CHAR, C_CHAR);
GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, 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("addBoolAndBoolsFromNestedStructWithXor").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);
boolean result = (boolean) mh.invokeExact(true, structSegmt);
Assert.assertEquals(result, true);
}
}
Aggregations