use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests2 method test_addTwoBoolsWithOr_fromMemAddr_2.
@Test
public void test_addTwoBoolsWithOr_fromMemAddr_2() throws Throwable {
MethodType mt = MethodType.methodType(boolean.class, boolean.class, boolean.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
Addressable functionSymbol = nativeLibLookup.lookup("add2BoolsWithOr").get();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
boolean result = (boolean) mh.invokeExact(true, false);
Assert.assertEquals(result, true);
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests2 method test_addTwoLongs_fromMemAddr_2.
@Test
public void test_addTwoLongs_fromMemAddr_2() throws Throwable {
MethodType mt = MethodType.methodType(long.class, long.class, long.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout);
Addressable functionSymbol = nativeLibLookup.lookup("add2Longs").get();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
long result = (long) mh.invokeExact(57424L, 698235L);
Assert.assertEquals(result, 755659L);
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests method test_addIntFromPointerAndIntsFromStruct_returnIntPointer.
@Test
public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer() 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("addIntFromPointerAndIntsFromStruct_returnIntPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment intSegmt = MemorySegment.allocateNative(C_INT);
MemoryAccess.setInt(intSegmt, 1122333);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
intHandle1.set(structSegmt, 4455666);
intHandle2.set(structSegmt, 7788999);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(intSegmt.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, 13366998);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), intSegmt.address().toRawLongValue());
intSegmt.close();
structSegmt.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests method test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer.
@Test
public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(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);
Symbol functionSymbol = nativeLib.lookup("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment doubleSegmt = MemorySegment.allocateNative(C_DOUBLE);
MemoryAccess.setDouble(doubleSegmt, 212.123D);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
doubleHandle1.set(structSegmt, 218.456D);
doubleHandle2.set(structSegmt, 219.789D);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(doubleSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_DOUBLE.byteSize());
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());
doubleSegmt.close();
structSegmt.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests method test_addLongFromPointerAndLongsFromStruct_returnLongPointer.
@Test
public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer() 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("addLongFromPointerAndLongsFromStruct_returnLongPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment longSegmt = MemorySegment.allocateNative(longLayout);
MemoryAccess.setLong(longSegmt, 1122334455L);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
longHandle1.set(structSegmt, 6677889900L);
longHandle2.set(structSegmt, 1234567890L);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(longSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(longLayout.byteSize());
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());
longSegmt.close();
structSegmt.close();
resultSegmt.close();
}
Aggregations