use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class PrimitiveTypeTests1 method test_addTwoLongs_fromMemAddr_1.
@Test
public void test_addTwoLongs_fromMemAddr_1() 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, mt, fd);
long result = (long) mh.invokeExact(57424L, 698235L);
Assert.assertEquals(result, 755659L);
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class PrimitiveTypeTests1 method test_generateNewChar_fromMemAddr_1.
@Test
public void test_generateNewChar_fromMemAddr_1() throws Throwable {
MethodType mt = MethodType.methodType(char.class, char.class, char.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT);
Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFrom2Chars").get();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
char result = (char) mh.invokeExact('B', 'D');
Assert.assertEquals(result, 'C');
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class PrimitiveTypeTests1 method test_addByteAndByteFromPointer_1.
@Test
public void test_addByteAndByteFromPointer_1() throws Throwable {
MethodType mt = MethodType.methodType(byte.class, byte.class, MemoryAddress.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_POINTER);
Addressable functionSymbol = nativeLibLookup.lookup("addByteAndByteFromPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment charSegmt = MemorySegment.allocateNative(C_CHAR, resourceScope);
MemoryAccess.setByte(charSegmt, (byte) 3);
byte result = (byte) mh.invokeExact((byte) 6, charSegmt.address());
Assert.assertEquals(result, (byte) 9);
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class StructTests1 method test_add2BoolStructsWithXor_returnStructPointer_1.
@Test
public void test_add2BoolStructsWithXor_returnStructPointer_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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
boolHandle1.set(structSegmt1, (byte) 1);
boolHandle2.set(structSegmt1, (byte) 0);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
boolHandle1.set(structSegmt2, (byte) 1);
boolHandle2.set(structSegmt2, (byte) 1);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
Assert.assertEquals((byte) boolHandle1.get(resultSegmt), (byte) 0);
Assert.assertEquals((byte) boolHandle2.get(resultSegmt), (byte) 1);
}
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class StructTests1 method test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_1.
@Test
public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
GroupLayout doubleStruct = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"));
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct);
GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2"));
MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray_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, 111.111D);
MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D);
MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D);
MemoryAccess.setDoubleAtOffset(structSegmt, 24, 444.444D);
MemoryAccess.setDoubleAtOffset(structSegmt, 32, 555.555D);
double result = (double) mh.invokeExact(666.666D, structSegmt);
Assert.assertEquals(result, 2333.331D, 0.001D);
}
}
Aggregations