Search in sources :

Example 1 with NativeSymbol

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

the class StructTests1 method test_addShortAndShortsFromStructWithNestedStructArray_1.

@Test
public void test_addShortAndShortsFromStructWithNestedStructArray_1() throws Throwable {
    GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_SHORT, 0, (short) 111);
        structSegmt.set(JAVA_SHORT, 2, (short) 222);
        structSegmt.set(JAVA_SHORT, 4, (short) 333);
        structSegmt.set(JAVA_SHORT, 6, (short) 444);
        structSegmt.set(JAVA_SHORT, 8, (short) 555);
        short result = (short) mh.invokeExact((short) 666, structSegmt);
        Assert.assertEquals(result, 2331);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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 2 with NativeSymbol

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

the class StructTests1 method test_add2ByteStructs_returnStruct_1.

@Test
public void test_add2ByteStructs_returnStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"));
    VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStruct").get();
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        byteHandle1.set(structSegmt1, (byte) 25);
        byteHandle2.set(structSegmt1, (byte) 11);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        byteHandle1.set(structSegmt2, (byte) 24);
        byteHandle2.set(structSegmt2, (byte) 13);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(allocator, structSegmt1, structSegmt2);
        Assert.assertEquals((byte) byteHandle1.get(resultSegmt), (byte) 49);
        Assert.assertEquals((byte) byteHandle2.get(resultSegmt), (byte) 24);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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 3 with NativeSymbol

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

the class StructTests1 method test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_1.

@Test
public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_1() throws Throwable {
    SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT);
    GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_SHORT, MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_SHORT, 0, (short) 111);
        structSegmt.set(JAVA_SHORT, 2, (short) 222);
        structSegmt.set(JAVA_SHORT, 4, (short) 333);
        short result = (short) mh.invokeExact((short) 444, structSegmt);
        Assert.assertEquals(result, 1110);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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 4 with NativeSymbol

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

the class StructTests1 method test_addCharAndCharsFromStructWithNestedStructArray_1.

@Test
public void test_addCharAndCharsFromStructWithNestedStructArray_1() throws Throwable {
    GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_CHAR, 0, 'E');
        structSegmt.set(JAVA_CHAR, 2, 'F');
        structSegmt.set(JAVA_CHAR, 4, 'G');
        structSegmt.set(JAVA_CHAR, 6, 'H');
        structSegmt.set(JAVA_CHAR, 8, 'I');
        char result = (char) mh.invokeExact('J', structSegmt);
        Assert.assertEquals(result, 'h');
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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 5 with NativeSymbol

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

the class StructTests1 method test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName_1.

@Test
public void test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName_1() throws Throwable {
    SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT);
    GroupLayout structLayout = MemoryLayout.structLayout(intArray, JAVA_INT);
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_INT, 0, 1111111);
        structSegmt.set(JAVA_INT, 4, 2222222);
        structSegmt.set(JAVA_INT, 8, 3333333);
        int result = (int) mh.invokeExact(4444444, structSegmt);
        Assert.assertEquals(result, 11111110);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)334 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)334 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 Test (org.testng.annotations.Test)334 MemorySegment (jdk.incubator.foreign.MemorySegment)291 ResourceScope (jdk.incubator.foreign.ResourceScope)274 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)271 GroupLayout (jdk.incubator.foreign.GroupLayout)270 VarHandle (java.lang.invoke.VarHandle)126 SequenceLayout (jdk.incubator.foreign.SequenceLayout)96 MemoryAddress (jdk.incubator.foreign.MemoryAddress)34 VaList (jdk.incubator.foreign.VaList)4