Search in sources :

Example 91 with SequenceLayout

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

the class StructTests2 method test_addShortAndShortsFromStructWithNestedShortArray_2.

@Test
public void test_addShortAndShortsFromStructWithNestedShortArray_2() throws Throwable {
    SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, C_SHORT);
    GroupLayout structLayout = MemoryLayout.structLayout(shortArray.withName("array_elem1"), C_SHORT.withName("elem2"), MemoryLayout.paddingLayout(C_SHORT.bitSize()));
    MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get();
    MethodHandle mh = clinker.downcallHandle(mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setShortAtOffset(structSegmt, 0, (short) 111);
        MemoryAccess.setShortAtOffset(structSegmt, 2, (short) 222);
        MemoryAccess.setShortAtOffset(structSegmt, 4, (short) 333);
        short result = (short) mh.invokeExact(functionSymbol, (short) 444, structSegmt);
        Assert.assertEquals(result, 1110);
    }
}
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 92 with SequenceLayout

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

the class StructTests2 method test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName_2.

@Test
public void test_addByteAndBytesFromStructWithNestedByteArray_withoutLayoutName_2() throws Throwable {
    SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, C_CHAR);
    GroupLayout structLayout = MemoryLayout.structLayout(byteArray, C_CHAR);
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").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) 11);
        MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 12);
        MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 13);
        byte result = (byte) mh.invokeExact(functionSymbol, (byte) 14, structSegmt);
        Assert.assertEquals(result, 50);
    }
}
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 93 with SequenceLayout

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

the class StructTests2 method test_addBoolAndBoolsFromStructWithNestedStructArray_2.

@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray_2() throws Throwable {
    GroupLayout boolStruct = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), C_CHAR.withName("elem2"), MemoryLayout.paddingLayout(C_CHAR.bitSize() * 3));
    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) 0);
        MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 1);
        MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 0);
        MemoryAccess.setByteAtOffset(structSegmt, 3, (byte) 1);
        MemoryAccess.setByteAtOffset(structSegmt, 4, (byte) 0);
        boolean result = (boolean) mh.invokeExact(functionSymbol, true, 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 94 with SequenceLayout

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

the class StructTests2 method test_addCharAndCharsFromStructWithNestedCharArray_2.

@Test
public void test_addCharAndCharsFromStructWithNestedCharArray_2() throws Throwable {
    SequenceLayout charArray = MemoryLayout.sequenceLayout(2, C_SHORT);
    GroupLayout structLayout = MemoryLayout.structLayout(charArray.withName("array_elem1"), C_SHORT.withName("elem2"), MemoryLayout.paddingLayout(C_SHORT.bitSize()));
    MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray").get();
    MethodHandle mh = clinker.downcallHandle(mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setCharAtOffset(structSegmt, 0, 'A');
        MemoryAccess.setCharAtOffset(structSegmt, 2, 'B');
        MemoryAccess.setCharAtOffset(structSegmt, 4, 'C');
        char result = (char) mh.invokeExact(functionSymbol, 'D', structSegmt);
        Assert.assertEquals(result, 'G');
    }
}
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 95 with SequenceLayout

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

the class StructTests1 method test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder_1.

@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
    GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray_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_FLOAT, 0, 111.11F);
        structSegmt.set(JAVA_FLOAT, 4, 222.22F);
        structSegmt.set(JAVA_FLOAT, 8, 333.33F);
        structSegmt.set(JAVA_FLOAT, 12, 444.44F);
        structSegmt.set(JAVA_FLOAT, 16, 555.55F);
        float result = (float) mh.invokeExact(666.66F, structSegmt);
        Assert.assertEquals(result, 2333.31F, 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) 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