Search in sources :

Example 66 with SequenceLayout

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

the class StructTests2 method test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_2.

@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable {
    GroupLayout boolStruct = MemoryLayout.structLayout(C_CHAR, C_CHAR);
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray, C_CHAR);
    MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get();
    MethodHandle mh = clinker.downcallHandle(mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 1);
        MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 0);
        MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 1);
        MemoryAccess.setByteAtOffset(structSegmt, 3, (byte) 0);
        MemoryAccess.setByteAtOffset(structSegmt, 4, (byte) 1);
        boolean result = (boolean) mh.invokeExact(functionSymbol, false, structSegmt);
        Assert.assertEquals(result, true);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) 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 67 with SequenceLayout

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

the class StructTests1 method test_addShortAndShortsFromStructWithNestedStructArray_1.

@Test
public void test_addShortAndShortsFromStructWithNestedStructArray_1() throws Throwable {
    GroupLayout shortStruct = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, shortStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struc_array_elem1"), JAVA_SHORT.withName("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedStructArray").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) 111);
        structSegmt.set(JAVA_SHORT, 2, (short) 222);
        structSegmt.set(JAVA_SHORT, 4, (short) 333);
        structSegmt.set(JAVA_SHORT, 6, (short) 444);
        structSegmt.set(JAVA_SHORT, 8, (short) 555);
        short result = (short) mh.invokeExact((short) 666, structSegmt);
        Assert.assertEquals(result, 2331);
    }
}
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 68 with SequenceLayout

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

the class StructTests1 method test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_1.

@Test
public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_1() throws Throwable {
    SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT);
    GroupLayout structLayout = MemoryLayout.structLayout(shortArray, JAVA_SHORT, MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").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) 111);
        structSegmt.set(JAVA_SHORT, 2, (short) 222);
        structSegmt.set(JAVA_SHORT, 4, (short) 333);
        short result = (short) mh.invokeExact((short) 444, structSegmt);
        Assert.assertEquals(result, 1110);
    }
}
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 69 with SequenceLayout

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

the class StructTests1 method test_addCharAndCharsFromStructWithNestedStructArray_1.

@Test
public void test_addCharAndCharsFromStructWithNestedStructArray_1() throws Throwable {
    GroupLayout charStruct = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, charStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_CHAR.withName("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedStructArray").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, 'E');
        structSegmt.set(JAVA_CHAR, 2, 'F');
        structSegmt.set(JAVA_CHAR, 4, 'G');
        structSegmt.set(JAVA_CHAR, 6, 'H');
        structSegmt.set(JAVA_CHAR, 8, 'I');
        char result = (char) mh.invokeExact('J', structSegmt);
        Assert.assertEquals(result, 'h');
    }
}
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 70 with SequenceLayout

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

the class StructTests1 method test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName_1.

@Test
public void test_addIntAndIntsFromStructWithNestedIntArray_withoutLayoutName_1() throws Throwable {
    SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT);
    GroupLayout structLayout = MemoryLayout.structLayout(intArray, JAVA_INT);
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").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_INT, 0, 1111111);
        structSegmt.set(JAVA_INT, 4, 2222222);
        structSegmt.set(JAVA_INT, 8, 3333333);
        int result = (int) mh.invokeExact(4444444, structSegmt);
        Assert.assertEquals(result, 11111110);
    }
}
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)

Aggregations

GroupLayout (jdk.incubator.foreign.GroupLayout)241 SequenceLayout (jdk.incubator.foreign.SequenceLayout)241 MethodHandle (java.lang.invoke.MethodHandle)240 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)240 MemorySegment (jdk.incubator.foreign.MemorySegment)240 Test (org.testng.annotations.Test)240 ResourceScope (jdk.incubator.foreign.ResourceScope)192 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)192 MethodType (java.lang.invoke.MethodType)144 Addressable (jdk.incubator.foreign.Addressable)96 NativeSymbol (jdk.incubator.foreign.NativeSymbol)96 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)48 MemoryLayout (jdk.incubator.foreign.MemoryLayout)1 ValueLayout (jdk.incubator.foreign.ValueLayout)1