Search in sources :

Example 31 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests method test_addDoubleAndDoubleIntFromStruct.

@Test
public void test_addDoubleAndDoubleIntFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_INT.withName("elem2"));
    VarHandle elemHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoubleIntFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    elemHandle1.set(structSegmt, 218.555D);
    elemHandle2.set(structSegmt, 19);
    double result = (double) mh.invokeExact(216.666D, structSegmt);
    Assert.assertEquals(result, 454.221D, 0.001D);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 32 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests method test_addLongFromPointerAndLongsFromStruct_returnLongPointer.

@Test
public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(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);
    Symbol functionSymbol = nativeLib.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment longSegmt = MemorySegment.allocateNative(longLayout);
    MemoryAccess.setLong(longSegmt, 1122334455L);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    longHandle1.set(structSegmt, 6677889900L);
    longHandle2.set(structSegmt, 1234567890L);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(longSegmt.address(), structSegmt);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(longLayout.byteSize());
    VarHandle longHandle = MemoryHandles.varHandle(long.class, ByteOrder.nativeOrder());
    long result = (long) longHandle.get(resultSegmt, 0);
    Assert.assertEquals(result, 9034792245L);
    Assert.assertEquals(resultSegmt.address().toRawLongValue(), longSegmt.address().toRawLongValue());
    longSegmt.close();
    structSegmt.close();
    resultSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 33 with VarHandle

use of java.lang.invoke.VarHandle 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();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 34 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests method test_addIntAndLongIntFromStruct.

@Test
public void test_addIntAndLongIntFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), C_INT.withName("elem2"), MemoryLayout.ofPaddingBits(C_INT.bitSize()));
    VarHandle elemHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(long.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addIntAndLongIntFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    elemHandle1.set(structSegmt, 667788990011L);
    elemHandle2.set(structSegmt, 11223344);
    long result = (long) mh.invokeExact(1234567, structSegmt);
    Assert.assertEquals(result, 667801447922L);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 35 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests method test_addByteAndBytesFromStruct.

@Test
public void test_addByteAndBytesFromStruct() 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(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt, (byte) 8);
    byteHandle2.set(structSegmt, (byte) 9);
    byte result = (byte) mh.invokeExact((byte) 6, structSegmt);
    Assert.assertEquals(result, 23);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

VarHandle (java.lang.invoke.VarHandle)400 Test (org.testng.annotations.Test)388 MethodHandle (java.lang.invoke.MethodHandle)319 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)318 GroupLayout (jdk.incubator.foreign.GroupLayout)318 MemorySegment (jdk.incubator.foreign.MemorySegment)318 ResourceScope (jdk.incubator.foreign.ResourceScope)254 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)254 MethodType (java.lang.invoke.MethodType)192 Addressable (jdk.incubator.foreign.Addressable)128 NativeSymbol (jdk.incubator.foreign.NativeSymbol)126 MemoryAddress (jdk.incubator.foreign.MemoryAddress)78 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)64 Test (org.junit.Test)8 VarHandleDesc (java.lang.invoke.VarHandle.VarHandleDesc)2 ClassDesc (java.lang.constant.ClassDesc)1 Field (java.lang.reflect.Field)1