use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests3 method test_memoryAllocFreeFromDefaultLib_3.
@Test
public void test_memoryAllocFreeFromDefaultLib_3() 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(allocMethodType, allocFuncDesc);
MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(allocSymbol, 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(freeMethodType, freeFuncDesc);
freeHandle.invokeExact(freeSymbol, allocMemAddr);
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class PrimitiveTypeTests3 method test_memoryAllocFreeFromDefaultLib_fromMemAddr_3.
@Test
public void test_memoryAllocFreeFromDefaultLib_fromMemAddr_3() 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();
MemoryAddress allocMemAddrFromSymbol = allocSymbol.address();
MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
MethodHandle allocHandle = clinker.downcallHandle(allocMethodType, allocFuncDesc);
MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(allocSymbol, 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();
MemoryAddress freeMemAddr = freeSymbol.address();
MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
MethodHandle freeHandle = clinker.downcallHandle(freeMethodType, freeFuncDesc);
freeHandle.invokeExact(freeSymbol, allocMemAddr);
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class VaListTests method test_vprintfFromDefaultLibWithVaList_fromMemAddr.
@Test
public void test_vprintfFromDefaultLibWithVaList_fromMemAddr() throws Throwable {
/* Disable the test on Windows given a misaligned access exception coming from
* java.base/java.lang.invoke.MemoryAccessVarHandleBase triggered by CLinker.toCString()
* is also captured on OpenJDK/Hotspot.
*/
if (!isWinOS) {
Addressable functionSymbol = defaultLibLookup.lookup("vprintf").get();
MemoryAddress memAddr = functionSymbol.address();
MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, VaList.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_VA_LIST);
MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
MemorySegment formatMemSegment = CLinker.toCString("%d * %d = %d\n", scope);
VaList vaList = CLinker.VaList.make(vaListBuilder -> vaListBuilder.vargFromInt(C_INT, 7).vargFromInt(C_INT, 8).vargFromInt(C_INT, 56), scope);
mh.invoke(formatMemSegment.address(), vaList);
}
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_add2FloatStructs_returnStructPointer_1.
@Test
public void test_add2FloatStructs_returnStructPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"));
VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
floatHandle1.set(structSegmt1, 25.12F);
floatHandle2.set(structSegmt1, 11.23F);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
floatHandle1.set(structSegmt2, 24.34F);
floatHandle2.set(structSegmt2, 13.45F);
MemoryAddress resultAddr = (MemoryAddress) mh.invoke(structSegmt1, structSegmt2);
Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 0), 49.46F, 0.01F);
Assert.assertEquals(resultAddr.get(JAVA_FLOAT, 4), 24.68F, 0.01F);
}
}
use of jdk.incubator.foreign.MemoryAddress 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(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"));
VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
boolHandle1.set(structSegmt1, true);
boolHandle2.set(structSegmt1, false);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
boolHandle1.set(structSegmt2, true);
boolHandle2.set(structSegmt2, true);
MemoryAddress resultAddr = (MemoryAddress) mh.invoke(structSegmt1, structSegmt2);
Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), false);
Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 1), true);
}
}
Aggregations