Search in sources :

Example 26 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class StructTests method test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder.

@Test
public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder() throws Throwable {
    GroupLayout byteStruct = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.ofSequence(2, byteStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), structArray.withName("struct_array_elem2"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize() * 3));
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 12);
    MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 14);
    MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 16);
    MemoryAccess.setByteAtOffset(structSegmt, 3, (byte) 18);
    MemoryAccess.setByteAtOffset(structSegmt, 4, (byte) 20);
    byte result = (byte) mh.invokeExact((byte) 22, structSegmt);
    Assert.assertEquals(result, 102);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) SequenceLayout(jdk.incubator.foreign.SequenceLayout) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 27 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class StructTests method test_addIntAndShortIntFromStruct.

@Test
public void test_addIntAndShortIntFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize()), C_INT.withName("elem2"));
    VarHandle elemHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addIntAndShortIntFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    elemHandle1.set(structSegmt, (short) 32766);
    elemHandle2.set(structSegmt, 22446688);
    int result = (int) mh.invokeExact(11335577, structSegmt);
    Assert.assertEquals(result, 33815031);
    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 28 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class StructTests method test_addShortFromPointerAndShortsFromStruct.

@Test
public void test_addShortFromPointerAndShortsFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(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(short.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addShortFromPointerAndShortsFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment shortSegmt = MemorySegment.allocateNative(C_SHORT);
    MemoryAccess.setShort(shortSegmt, (short) 12);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    shortHandle1.set(structSegmt, (short) 18);
    shortHandle2.set(structSegmt, (short) 19);
    short result = (short) mh.invokeExact(shortSegmt.address(), structSegmt);
    Assert.assertEquals(result, 49);
    shortSegmt.close();
    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 29 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class StructTests method test_add2ByteStructs_returnStructPointer.

@Test
public void test_add2ByteStructs_returnStructPointer() 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("add2ByteStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt1, (byte) 25);
    byteHandle2.set(structSegmt1, (byte) 11);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt2, (byte) 24);
    byteHandle2.set(structSegmt2, (byte) 13);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
    Assert.assertEquals((byte) byteHandle1.get(resultSegmt), (byte) 49);
    Assert.assertEquals((byte) byteHandle2.get(resultSegmt), (byte) 24);
    structSegmt1.close();
    structSegmt2.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 30 with FunctionDescriptor

use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.

the class StructTests method test_addLongAndLongsFromNestedStruct_withoutLayoutName.

@Test
public void test_addLongAndLongsFromNestedStruct_withoutLayoutName() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.ofStruct(longLayout, longLayout);
    GroupLayout structLayout = MemoryLayout.ofStruct(longLayout, nestedStructLayout);
    MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromNestedStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setLongAtOffset(structSegmt, 0, 135791357913L);
    MemoryAccess.setLongAtOffset(structSegmt, 8, 246802468024L);
    MemoryAccess.setLongAtOffset(structSegmt, 16, 112233445566L);
    long result = (long) mh.invokeExact(778899001122L, structSegmt);
    Assert.assertEquals(result, 1273726272625L);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) 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

MethodHandle (java.lang.invoke.MethodHandle)939 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)939 Test (org.testng.annotations.Test)939 MemorySegment (jdk.incubator.foreign.MemorySegment)754 GroupLayout (jdk.incubator.foreign.GroupLayout)675 MethodType (java.lang.invoke.MethodType)605 ResourceScope (jdk.incubator.foreign.ResourceScope)549 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)541 Addressable (jdk.incubator.foreign.Addressable)432 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 VarHandle (java.lang.invoke.VarHandle)318 SequenceLayout (jdk.incubator.foreign.SequenceLayout)240 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)173 MemoryAddress (jdk.incubator.foreign.MemoryAddress)127 VaList (jdk.incubator.foreign.VaList)4