use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests method test_add2LongStructs_returnStructPointer.
@Test
public void test_add2LongStructs_returnStructPointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("add2LongStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
longHandle1.set(structSegmt1, 1122334455L);
longHandle2.set(structSegmt1, 5566778899L);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
longHandle1.set(structSegmt2, 9900112233L);
longHandle2.set(structSegmt2, 3344556677L);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
Assert.assertEquals(longHandle1.get(resultSegmt), 11022446688L);
Assert.assertEquals(longHandle2.get(resultSegmt), 8911335576L);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests method test_add2IntStructs_returnStructPointer.
@Test
public void test_add2IntStructs_returnStructPointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(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);
Symbol functionSymbol = nativeLib.lookup("add2IntStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
intHandle1.set(structSegmt1, 11223344);
intHandle2.set(structSegmt1, 55667788);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
intHandle1.set(structSegmt2, 99001122);
intHandle2.set(structSegmt2, 33445566);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
Assert.assertEquals(intHandle1.get(resultSegmt), 110224466);
Assert.assertEquals(intHandle2.get(resultSegmt), 89113354);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests method test_add2CharStructs_returnStructPointer.
@Test
public void test_add2CharStructs_returnStructPointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(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);
Symbol functionSymbol = nativeLib.lookup("add2CharStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
charHandle1.set(structSegmt1, 'A');
charHandle2.set(structSegmt1, 'B');
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
charHandle1.set(structSegmt2, 'C');
charHandle2.set(structSegmt2, 'D');
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
Assert.assertEquals(charHandle1.get(resultSegmt), 'C');
Assert.assertEquals(charHandle2.get(resultSegmt), 'E');
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests method test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer.
@Test
public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_INT, C_INT);
MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment booleanSegmt = MemorySegment.allocateNative(C_INT);
MemoryAccess.setInt(booleanSegmt, 0);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 0);
MemoryAccess.setIntAtOffset(structSegmt, 4, 1);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(booleanSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_INT.byteSize());
VarHandle intHandle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
int result = (int) intHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 1);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), booleanSegmt.address().toRawLongValue());
booleanSegmt.close();
structSegmt.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_addLongFromPointerAndLongsFromStruct_returnLongPointer_1.
@Test
public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").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, 1122334455L);
MemorySegment structSegmt = allocator.allocate(structLayout);
longHandle1.set(structSegmt, 6677889900L);
longHandle2.set(structSegmt, 1234567890L);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(longSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(longLayout.byteSize(), scope);
VarHandle longHandle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
long result = (long) longHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 9034792245L);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), longSegmt.address().toRawLongValue());
}
}
Aggregations