Search in sources :

Example 41 with NativeSymbol

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

the class StructTests1 method test_addLongAndLongsFromNestedStruct_1.

@Test
public void test_addLongAndLongsFromNestedStruct_1() 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").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, 135791357913L);
        structSegmt.set(JAVA_LONG, 8, 246802468024L);
        structSegmt.set(JAVA_LONG, 16, 112233445566L);
        long result = (long) mh.invokeExact(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 42 with NativeSymbol

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

the class StructTests1 method test_addLongAndLongsFromStructPointer_1.

@Test
public void test_addLongAndLongsFromStructPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"));
    VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        longHandle1.set(structSegmt, 224466880022L);
        longHandle2.set(structSegmt, 446688002244L);
        long result = (long) mh.invoke(668800224466L, structSegmt);
        Assert.assertEquals(result, 1339955106732L);
    }
}
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 43 with NativeSymbol

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

the class StructTests1 method test_addLongAndLongsFromStructWithNestedStructArray_1.

@Test
public void test_addLongAndLongsFromStructWithNestedStructArray_1() throws Throwable {
    GroupLayout longStruct = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_LONG.withName("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray").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);
        structSegmt.set(JAVA_LONG, 24, 444444444L);
        structSegmt.set(JAVA_LONG, 32, 555555555L);
        long result = (long) mh.invokeExact(666666666L, structSegmt);
        Assert.assertEquals(result, 2333333331L);
    }
}
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 44 with NativeSymbol

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

the class StructTests1 method test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_1.

@Test
public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_1() throws Throwable {
    SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR);
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), charArray.withName("array_elem2"), MemoryLayout.paddingLayout(JAVA_CHAR.bitSize()));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray_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_CHAR, 0, 'A');
        structSegmt.set(JAVA_CHAR, 2, 'B');
        structSegmt.set(JAVA_CHAR, 4, 'C');
        char result = (char) mh.invokeExact('D', structSegmt);
        Assert.assertEquals(result, 'G');
    }
}
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 45 with NativeSymbol

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

the class StructTests1 method test_addDoubleAndFloatDoubleFromStruct_1.

@Test
public void test_addDoubleAndFloatDoubleFromStruct_1() throws Throwable {
    GroupLayout structLayout = null;
    MemorySegment structSegmt = null;
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        /* The size of [float, double] on AIX/PPC 64-bit is 12 bytes without padding by default
			 * while the same struct is 16 bytes with padding on other platforms.
			 */
        if (isAixOS) {
            structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_DOUBLE.withName("elem2"));
            structSegmt = allocator.allocate(structLayout);
            structSegmt.set(JAVA_FLOAT, 0, 18.444F);
            structSegmt.set(JAVA_DOUBLE, 4, 619.777D);
        } else {
            structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), MemoryLayout.paddingLayout(JAVA_FLOAT.bitSize()), JAVA_DOUBLE.withName("elem2"));
            VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
            VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
            structSegmt = allocator.allocate(structLayout);
            elemHandle1.set(structSegmt, 18.444F);
            elemHandle2.set(structSegmt, 619.777D);
        }
        FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout);
        NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndFloatDoubleFromStruct").get();
        MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
        double result = (double) mh.invokeExact(113.567D, structSegmt);
        Assert.assertEquals(result, 751.788D, 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