Search in sources :

Example 36 with GroupLayout

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

the class StructTests method test_addBoolAndBoolsFromStructWithNestedBoolArray.

@Test
public void test_addBoolAndBoolsFromStructWithNestedBoolArray() throws Throwable {
    SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT);
    GroupLayout structLayout = MemoryLayout.ofStruct(intArray.withName("array_elem1"), C_INT.withName("elem2"));
    MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedBoolArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setIntAtOffset(structSegmt, 0, 0);
    MemoryAccess.setIntAtOffset(structSegmt, 4, 1);
    MemoryAccess.setIntAtOffset(structSegmt, 8, 0);
    int result = (int) mh.invokeExact(0, structSegmt);
    Assert.assertEquals(result, 1);
    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 37 with GroupLayout

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

the class StructTests method test_addCharFromPointerAndCharsFromStruct_returnCharPointer.

@Test
public void test_addCharFromPointerAndCharsFromStruct_returnCharPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
    VarHandle charHandle2 = structLayout.varHandle(char.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("addCharFromPointerAndCharsFromStruct_returnCharPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment charSegmt = MemorySegment.allocateNative(C_SHORT);
    MemoryAccess.setChar(charSegmt, 'D');
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt, 'E');
    charHandle2.set(structSegmt, 'F');
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(charSegmt.address(), structSegmt);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_SHORT.byteSize());
    VarHandle charHandle = MemoryHandles.varHandle(char.class, ByteOrder.nativeOrder());
    char result = (char) charHandle.get(resultSegmt, 0);
    Assert.assertEquals(result, 'M');
    Assert.assertEquals(resultSegmt.address().toRawLongValue(), charSegmt.address().toRawLongValue());
    charSegmt.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 38 with GroupLayout

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

the class StructTests method test_addShortFromPointerAndShortsFromStruct_returnShortPointer.

@Test
public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer() 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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointer").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);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(shortSegmt.address(), structSegmt);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_SHORT.byteSize());
    VarHandle shortHandle = MemoryHandles.varHandle(short.class, ByteOrder.nativeOrder());
    short result = (short) shortHandle.get(resultSegmt, 0);
    Assert.assertEquals(result, 49);
    Assert.assertEquals(resultSegmt.address().toRawLongValue(), shortSegmt.address().toRawLongValue());
    shortSegmt.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 39 with GroupLayout

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

the class StructTests method test_addIntFromPointerAndIntsFromStruct_returnIntPointer.

@Test
public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(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);
    Symbol functionSymbol = nativeLib.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment intSegmt = MemorySegment.allocateNative(C_INT);
    MemoryAccess.setInt(intSegmt, 1122333);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    intHandle1.set(structSegmt, 4455666);
    intHandle2.set(structSegmt, 7788999);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(intSegmt.address(), structSegmt);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_INT.byteSize());
    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());
    intSegmt.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 40 with GroupLayout

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

the class StructTests method test_addLongAndLongsFromStruct.

@Test
public void test_addLongAndLongsFromStruct() 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(long.class, long.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    longHandle1.set(structSegmt, 1234567890L);
    longHandle2.set(structSegmt, 9876543210L);
    long result = (long) mh.invokeExact(2468024680L, structSegmt);
    Assert.assertEquals(result, 13579135780L);
    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

GroupLayout (jdk.incubator.foreign.GroupLayout)677 MethodHandle (java.lang.invoke.MethodHandle)675 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)675 MemorySegment (jdk.incubator.foreign.MemorySegment)675 Test (org.testng.annotations.Test)675 ResourceScope (jdk.incubator.foreign.ResourceScope)540 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)540 MethodType (java.lang.invoke.MethodType)405 VarHandle (java.lang.invoke.VarHandle)318 Addressable (jdk.incubator.foreign.Addressable)270 NativeSymbol (jdk.incubator.foreign.NativeSymbol)270 SequenceLayout (jdk.incubator.foreign.SequenceLayout)241 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)135 MemoryAddress (jdk.incubator.foreign.MemoryAddress)80 MemoryLayout (jdk.incubator.foreign.MemoryLayout)2 ValueLayout (jdk.incubator.foreign.ValueLayout)1