Search in sources :

Example 46 with NativeSymbol

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

the class StructTests1 method test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_1.

@Test
public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_1() throws Throwable {
    SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN);
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize()));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder").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_BOOLEAN, 0, false);
        structSegmt.set(JAVA_BOOLEAN, 1, true);
        structSegmt.set(JAVA_BOOLEAN, 2, false);
        boolean result = (boolean) mh.invokeExact(false, structSegmt);
        Assert.assertEquals(result, true);
    }
}
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 47 with NativeSymbol

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

the class StructTests1 method test_addDoubleAndDoublesFromNestedStruct_reverseOrder_1.

@Test
public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder_1() 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(functionSymbol, 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(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 48 with NativeSymbol

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

the class StructTests1 method test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1.

@Test
public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1() 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(functionSymbol, 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(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 49 with NativeSymbol

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

the class StructTests1 method test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName_1.

@Test
public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName_1() throws Throwable {
    SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG);
    GroupLayout structLayout = MemoryLayout.structLayout(longArray, JAVA_LONG);
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray").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_LONG, 0, 111111111L);
        structSegmt.set(JAVA_LONG, 8, 222222222L);
        structSegmt.set(JAVA_LONG, 16, 333333333L);
        long result = (long) mh.invokeExact(444444444L, structSegmt);
        Assert.assertEquals(result, 1111111110L);
    }
}
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 50 with NativeSymbol

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

the class StructTests1 method test_add2BoolStructsWithXor_returnStruct_1.

@Test
public void test_add2BoolStructsWithXor_returnStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"));
    VarHandle boolHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle boolHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        boolHandle1.set(structSegmt1, true);
        boolHandle2.set(structSegmt1, false);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        boolHandle1.set(structSegmt2, true);
        boolHandle2.set(structSegmt2, true);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(allocator, structSegmt1, structSegmt2);
        Assert.assertEquals(boolHandle1.get(resultSegmt), false);
        Assert.assertEquals(boolHandle2.get(resultSegmt), true);
    }
}
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