Search in sources :

Example 91 with NativeSymbol

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

the class StructTests2 method test_addDoubleAndDoublesFromNestedStruct_reverseOrder_2.

@Test
public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder_2() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_DOUBLE, 0, 31.789D);
        structSegmt.set(JAVA_DOUBLE, 8, 33.456D);
        structSegmt.set(JAVA_DOUBLE, 16, 35.123D);
        double result = (double) mh.invokeExact(functionSymbol, 37.864D, structSegmt);
        Assert.assertEquals(result, 138.232D, 0.001D);
    }
}
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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 92 with NativeSymbol

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

the class StructTests2 method test_addShortAndShortsFromNestedStruct_reverseOrder_2.

@Test
public void test_addShortAndShortsFromNestedStruct_reverseOrder_2() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_SHORT, 0, (short) 31);
        structSegmt.set(JAVA_SHORT, 2, (short) 33);
        structSegmt.set(JAVA_SHORT, 4, (short) 35);
        short result = (short) mh.invokeExact(functionSymbol, (short) 37, structSegmt);
        Assert.assertEquals(result, 136);
    }
}
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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 93 with NativeSymbol

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

the class StructTests2 method test_addIntAndIntLongFromStruct_2.

@Test
public void test_addIntAndIntLongFromStruct_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_LONG.withName("elem2"));
    VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntLongFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        elemHandle1.set(structSegmt, 11223344);
        elemHandle2.set(structSegmt, 667788990011L);
        long result = (long) mh.invokeExact(functionSymbol, 22446688, structSegmt);
        Assert.assertEquals(result, 667822660043L);
    }
}
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 94 with NativeSymbol

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

the class StructTests2 method test_addDoubleAndDoublesFromStructWithNestedStructArray_2.

@Test
public void test_addDoubleAndDoublesFromStructWithNestedStructArray_2() throws Throwable {
    GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get();
    MethodHandle mh = clinker.downcallHandle(fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_DOUBLE, 0, 111.111D);
        structSegmt.set(JAVA_DOUBLE, 8, 222.222D);
        structSegmt.set(JAVA_DOUBLE, 16, 333.333D);
        structSegmt.set(JAVA_DOUBLE, 24, 444.444D);
        structSegmt.set(JAVA_DOUBLE, 32, 555.555D);
        double result = (double) mh.invokeExact(functionSymbol, 666.666D, structSegmt);
        Assert.assertEquals(result, 2333.331D, 0.001D);
    }
}
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 95 with NativeSymbol

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

the class StructTests2 method test_addShortAndShortsFromStructPointer_2.

@Test
public void test_addShortAndShortsFromStructPointer_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"));
    VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        shortHandle1.set(structSegmt, (short) 22);
        shortHandle2.set(structSegmt, (short) 44);
        short result = (short) mh.invoke(functionSymbol, (short) 66, structSegmt);
        Assert.assertEquals(result, 132);
    }
}
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)

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