Search in sources :

Example 81 with NativeSymbol

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

the class StructTests1 method test_addByteAndBytesFromStructPointer_1.

@Test
public void test_addByteAndBytesFromStructPointer_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(JAVA_BYTE, JAVA_BYTE, ADDRESS);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        byteHandle1.set(structSegmt, (byte) 11);
        byteHandle2.set(structSegmt, (byte) 12);
        byte result = (byte) mh.invoke((byte) 13, structSegmt);
        Assert.assertEquals(result, 36);
    }
}
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 82 with NativeSymbol

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

the class StructTests1 method test_addShortAndShortsFromNestedStruct_withoutLayoutName_1.

@Test
public void test_addShortAndShortsFromNestedStruct_withoutLayoutName_1() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT, JAVA_SHORT);
    GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, JAVA_SHORT);
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromNestedStruct").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) 31);
        structSegmt.set(JAVA_SHORT, 2, (short) 33);
        structSegmt.set(JAVA_SHORT, 4, (short) 35);
        short result = (short) mh.invokeExact((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 83 with NativeSymbol

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

the class StructTests2 method test_add2FloatStructs_returnStruct_2.

@Test
public void test_add2FloatStructs_returnStruct_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"));
    VarHandle floatHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle floatHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        floatHandle1.set(structSegmt1, 25.12F);
        floatHandle2.set(structSegmt1, 11.23F);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        floatHandle1.set(structSegmt2, 24.34F);
        floatHandle2.set(structSegmt2, 13.45F);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2);
        Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
        Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
    }
}
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 84 with NativeSymbol

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

the class StructTests2 method test_addLongAndLongsFromStructPointer_2.

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

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

the class StructTests2 method test_addCharAndCharsFromNestedStruct_2.

@Test
public void test_addCharAndCharsFromNestedStruct_2() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_CHAR.withName("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromNestedStruct").get();
    MethodHandle mh = clinker.downcallHandle(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');
        char result = (char) mh.invokeExact(functionSymbol, 'H', structSegmt);
        Assert.assertEquals(result, 'W');
    }
}
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)

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