use of jdk.incubator.foreign.MemoryAddress 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.MemoryAddress 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.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests1 method test_addTwoShorts_fromMemAddr_1.
@Test
public void test_addTwoShorts_fromMemAddr_1() throws Throwable {
MethodType mt = MethodType.methodType(short.class, short.class, short.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT);
Addressable functionSymbol = nativeLibLookup.lookup("add2Shorts").get();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
short result = (short) mh.invokeExact((short) 24, (short) 32);
Assert.assertEquals(result, (short) 56);
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests1 method test_printfFromDefaultLibWithMemAddr_fromMemAddr_1.
@Test
public void test_printfFromDefaultLibWithMemAddr_fromMemAddr_1() throws Throwable {
/* Temporarily disable the default library loading on AIX till we figure out a way
* around to handle the case as the official implementation in OpenJDK17 doesn't
* help to load the static libray (libc.a).
*/
if (!isAixOS) {
Addressable functionSymbol = defaultLibLookup.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", resourceScope);
mh.invoke(formatMemSegment.address(), 15, 27, 42);
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests1 method test_addTwoIntsReturnVoid_fromMemAddr_1.
@Test
public void test_addTwoIntsReturnVoid_fromMemAddr_1() throws Throwable {
MethodType mt = MethodType.methodType(void.class, int.class, int.class);
FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT);
Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get();
MemoryAddress memAddr = functionSymbol.address();
MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
mh.invokeExact(454, 398);
}
Aggregations