use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests method test_addTwoLongs_fromMemAddr.
@Test
public void test_addTwoLongs_fromMemAddr() throws Throwable {
MethodType mt = MethodType.methodType(long.class, long.class, long.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, longLayout);
Symbol functionSymbol = nativeLib.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);
FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), longLayout.withName("long"), longLayout.withName("long"));
mh = clinker.downcallHandle(memAddr, mt, fd2);
result = (long) mh.invokeExact(111222L, 333444L);
Assert.assertEquals(result, 444666L);
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests method test_printfFromDefaultLibWithMemAddr_fromMemAddr.
@Test
public void test_printfFromDefaultLibWithMemAddr_fromMemAddr() throws Throwable {
Symbol functionSymbol = defaultLib.lookup("printf").get();
MemoryAddress memAddr = functionSymbol.address();
MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, int.class, int.class, int.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_INT, C_INT, C_INT);
MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
MemorySegment formatMemSegment = CLinker.toCString("\n%d + %d = %d\n");
mh.invoke(formatMemSegment.address(), 15, 27, 42);
FunctionDescriptor fd2 = FunctionDescriptor.of(C_INT.withName("int"), C_POINTER.withName("pointer"), C_INT.withName("int"), C_INT.withName("int"), C_INT.withName("int"));
mh = clinker.downcallHandle(memAddr, mt, fd2);
mh.invoke(formatMemSegment.address(), 115, 127, 242);
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests2 method test_add2LongStructs_returnStructPointer_2.
@Test
public void test_add2LongStructs_returnStructPointer_2() 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("add2LongStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
longHandle1.set(structSegmt1, 1122334455L);
longHandle2.set(structSegmt1, 5566778899L);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
longHandle1.set(structSegmt2, 9900112233L);
longHandle2.set(structSegmt2, 3344556677L);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
Assert.assertEquals(longHandle1.get(resultSegmt), 11022446688L);
Assert.assertEquals(longHandle2.get(resultSegmt), 8911335576L);
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests2 method test_add2ByteStructs_returnStructPointer_2.
@Test
public void test_add2ByteStructs_returnStructPointer_2() 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("add2ByteStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
byteHandle1.set(structSegmt1, (byte) 25);
byteHandle2.set(structSegmt1, (byte) 11);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
byteHandle1.set(structSegmt2, (byte) 24);
byteHandle2.set(structSegmt2, (byte) 13);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
Assert.assertEquals((byte) byteHandle1.get(resultSegmt), (byte) 49);
Assert.assertEquals((byte) byteHandle2.get(resultSegmt), (byte) 24);
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_add2ShortStructs_returnStructPointer_1.
@Test
public void test_add2ShortStructs_returnStructPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"));
VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ShortStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
shortHandle1.set(structSegmt1, (short) 56);
shortHandle2.set(structSegmt1, (short) 45);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
shortHandle1.set(structSegmt2, (short) 78);
shortHandle2.set(structSegmt2, (short) 67);
MemoryAddress resultAddr = (MemoryAddress) mh.invoke(structSegmt1, structSegmt2);
Assert.assertEquals(resultAddr.get(JAVA_SHORT, 0), 134);
Assert.assertEquals(resultAddr.get(JAVA_SHORT, 2), 112);
}
}
Aggregations