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(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
VarHandle shortHandle2 = structLayout.varHandle(short.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("add2ShortStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(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.invokeExact(structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
Assert.assertEquals((short) shortHandle1.get(resultSegmt), (short) 134);
Assert.assertEquals((short) shortHandle2.get(resultSegmt), (short) 112);
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_add2LongStructs_returnStructPointer_1.
@Test
public void test_add2LongStructs_returnStructPointer_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("add2LongStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, 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(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 PrimitiveTypeTests2 method test_memoryAllocFreeFromDefaultLib_2.
@Test
public void test_memoryAllocFreeFromDefaultLib_2() 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 allocSymbol = defaultLibLookup.lookup("malloc").get();
MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
MethodHandle allocHandle = clinker.downcallHandle(allocSymbol, allocator, allocMethodType, allocFuncDesc);
MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
MemorySegment memSeg = allocMemAddr.asSegment(10L, resourceScope);
MemoryAccess.setIntAtOffset(memSeg, 0, 15);
Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, 0), 15);
Addressable freeSymbol = defaultLibLookup.lookup("free").get();
MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
MethodHandle freeHandle = clinker.downcallHandle(freeSymbol, allocator, freeMethodType, freeFuncDesc);
freeHandle.invokeExact(allocMemAddr);
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_1.
@Test
public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR, C_CHAR);
MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment boolSegmt = allocator.allocate(C_CHAR);
MemoryAccess.setByte(boolSegmt, (byte) 0);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 0);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 1);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(boolSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(C_CHAR.byteSize(), scope);
VarHandle boolHandle = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder());
byte result = (byte) boolHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 1);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), boolSegmt.address().toRawLongValue());
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_add2ByteStructs_returnStructPointer_1.
@Test
public void test_add2ByteStructs_returnStructPointer_1() 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(functionSymbol, 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(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);
}
}
Aggregations