use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests method test_addByteFromPointerAndBytesFromStruct_returnBytePointer.
@Test
public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(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);
Symbol functionSymbol = nativeLib.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment byteSegmt = MemorySegment.allocateNative(C_CHAR);
MemoryAccess.setByte(byteSegmt, (byte) 12);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
byteHandle1.set(structSegmt, (byte) 18);
byteHandle2.set(structSegmt, (byte) 19);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(byteSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_CHAR.byteSize());
VarHandle byteHandle = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder());
byte result = (byte) byteHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 49);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), byteSegmt.address().toRawLongValue());
byteSegmt.close();
structSegmt.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests method test_add2FloatStructs_returnStructPointer.
@Test
public void test_add2FloatStructs_returnStructPointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Symbol functionSymbol = nativeLib.lookup("add2FloatStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
floatHandle1.set(structSegmt1, 25.12F);
floatHandle2.set(structSegmt1, 11.23F);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
floatHandle1.set(structSegmt2, 24.34F);
floatHandle2.set(structSegmt2, 13.45F);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_add2DoubleStructs_returnStructPointer_1.
@Test
public void test_add2DoubleStructs_returnStructPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"));
VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1"));
VarHandle doubleHandle2 = structLayout.varHandle(double.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("add2DoubleStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
doubleHandle1.set(structSegmt1, 11.222D);
doubleHandle2.set(structSegmt1, 22.333D);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
doubleHandle1.set(structSegmt2, 33.444D);
doubleHandle2.set(structSegmt2, 44.555D);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
Assert.assertEquals((double) doubleHandle1.get(resultSegmt), 44.666D, 0.001D);
Assert.assertEquals((double) doubleHandle2.get(resultSegmt), 66.888D, 0.001D);
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1.
@Test
public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1() 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("addIntFromPointerAndIntsFromStruct_returnIntPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment intSegmt = allocator.allocate(C_INT);
MemoryAccess.setInt(intSegmt, 1122333);
MemorySegment structSegmt = allocator.allocate(structLayout);
intHandle1.set(structSegmt, 4455666);
intHandle2.set(structSegmt, 7788999);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(intSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(C_INT.byteSize(), scope);
VarHandle intHandle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
int result = (int) intHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 13366998);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), intSegmt.address().toRawLongValue());
}
}
use of jdk.incubator.foreign.MemoryAddress in project openj9 by eclipse.
the class StructTests1 method test_addByteFromPointerAndBytesFromStruct_returnBytePointer_1.
@Test
public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer_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("addByteFromPointerAndBytesFromStruct_returnBytePointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment byteSegmt = allocator.allocate(C_CHAR);
MemoryAccess.setByte(byteSegmt, (byte) 12);
MemorySegment structSegmt = allocator.allocate(structLayout);
byteHandle1.set(structSegmt, (byte) 18);
byteHandle2.set(structSegmt, (byte) 19);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(byteSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(C_CHAR.byteSize(), scope);
VarHandle byteHandle = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder());
byte result = (byte) byteHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 49);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), byteSegmt.address().toRawLongValue());
}
}
Aggregations