Search in sources :

Example 96 with NativeSymbol

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

the class StructTests2 method test_addLongAndLongsFromNestedStruct_reverseOrder_2.

@Test
public void test_addLongAndLongsFromNestedStruct_reverseOrder_2() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct_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_LONG, 0, 135791357913L);
        structSegmt.set(JAVA_LONG, 8, 246802468024L);
        structSegmt.set(JAVA_LONG, 16, 112233445566L);
        long result = (long) mh.invokeExact(functionSymbol, 778899001122L, structSegmt);
        Assert.assertEquals(result, 1273726272625L);
    }
}
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 97 with NativeSymbol

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

the class StructTests2 method test_addIntAndIntsFromNestedStruct_withoutLayoutName_2.

@Test
public void test_addIntAndIntsFromNestedStruct_withoutLayoutName_2() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT, JAVA_INT);
    GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_INT);
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromNestedStruct").get();
    MethodHandle mh = clinker.downcallHandle(fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_INT, 0, 21222324);
        structSegmt.set(JAVA_INT, 4, 25262728);
        structSegmt.set(JAVA_INT, 8, 29303132);
        int result = (int) mh.invokeExact(functionSymbol, 33343536, structSegmt);
        Assert.assertEquals(result, 109131720);
    }
}
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 98 with NativeSymbol

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

the class StructTests2 method test_addIntFromPointerAndIntsFromStruct_returnIntPointer_2.

@Test
public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"));
    VarHandle intHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle intHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get();
    MethodHandle mh = clinker.downcallHandle(fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment intSegmt = allocator.allocate(JAVA_INT, 1122333);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        intHandle1.set(structSegmt, 4455666);
        intHandle2.set(structSegmt, 7788999);
        MemoryAddress resultAddr = (MemoryAddress) mh.invoke(functionSymbol, intSegmt, structSegmt);
        Assert.assertEquals(resultAddr.get(JAVA_INT, 0), 13366998);
        Assert.assertEquals(resultAddr.toRawLongValue(), intSegmt.address().toRawLongValue());
    }
}
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) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 99 with NativeSymbol

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

the class StructTests2 method test_addFloatAndFloatsFromNestedStruct_reverseOrder_2.

@Test
public void test_addFloatAndFloatsFromNestedStruct_reverseOrder_2() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct_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_FLOAT, 0, 31.22F);
        structSegmt.set(JAVA_FLOAT, 4, 33.44F);
        structSegmt.set(JAVA_FLOAT, 8, 35.66F);
        float result = (float) mh.invokeExact(functionSymbol, 37.88F, structSegmt);
        Assert.assertEquals(result, 138.2F, 0.01F);
    }
}
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 100 with NativeSymbol

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

the class StructTests2 method test_addDoubleAndDoubleFloatFromStruct_2.

@Test
public void test_addDoubleAndDoubleFloatFromStruct_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2"));
    VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleFloatFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        elemHandle1.set(structSegmt, 218.555D);
        elemHandle2.set(structSegmt, 19.22F);
        double result = (double) mh.invokeExact(functionSymbol, 216.666D, structSegmt);
        Assert.assertEquals(result, 454.441D, 0.001D);
    }
}
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