Search in sources :

Example 91 with SegmentAllocator

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

the class StructTests2 method test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_2.

@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_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(C_CHAR.withName("elem1"), structArray.withName("struct_array_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_reverseOrder").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 92 with SegmentAllocator

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

the class StructTests2 method test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName_2.

@Test
public void test_addIntAndIntsFromStructWithNestedStructArray_withoutLayoutName_2() throws Throwable {
    GroupLayout intStruct = MemoryLayout.structLayout(C_INT, C_INT);
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray, C_INT);
    MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray").get();
    MethodHandle mh = clinker.downcallHandle(mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111);
        MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222);
        MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333);
        MemoryAccess.setIntAtOffset(structSegmt, 12, 4444444);
        MemoryAccess.setIntAtOffset(structSegmt, 16, 5555555);
        int result = (int) mh.invokeExact(functionSymbol, 6666666, structSegmt);
        Assert.assertEquals(result, 23333331);
    }
}
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 SegmentAllocator

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

the class StructTests2 method test_add2BoolStructsWithXor_returnStruct_2.

@Test
public void test_add2BoolStructsWithXor_returnStruct_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    VarHandle boolHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle boolHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStruct").get();
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MethodHandle mh = clinker.downcallHandle(mt, fd);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        boolHandle1.set(structSegmt1, (byte) 1);
        boolHandle2.set(structSegmt1, (byte) 0);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        boolHandle1.set(structSegmt2, (byte) 1);
        boolHandle2.set(structSegmt2, (byte) 1);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2);
        Assert.assertEquals((byte) boolHandle1.get(resultSegmt), (byte) 0);
        Assert.assertEquals((byte) boolHandle2.get(resultSegmt), (byte) 1);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) 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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 94 with SegmentAllocator

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

the class StructTests2 method test_addIntFromPointerAndIntsFromStruct_2.

@Test
public void test_addIntFromPointerAndIntsFromStruct_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), C_INT.withName("elem2"));
    VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment intSegmt = allocator.allocate(C_INT);
        MemoryAccess.setInt(intSegmt, 7654321);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        intHandle1.set(structSegmt, 1234567);
        intHandle2.set(structSegmt, 2468024);
        int result = (int) mh.invokeExact(functionSymbol, intSegmt.address(), structSegmt);
        Assert.assertEquals(result, 11356912);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) 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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 95 with SegmentAllocator

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

the class StructTests2 method test_addByteAndBytesFromStructPointer_2.

@Test
public void test_addByteAndBytesFromStructPointer_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_POINTER);
    Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        byteHandle1.set(structSegmt, (byte) 11);
        byteHandle2.set(structSegmt, (byte) 12);
        byte result = (byte) mh.invokeExact(functionSymbol, (byte) 13, structSegmt.address());
        Assert.assertEquals(result, 36);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) 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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)541 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)541 MemorySegment (jdk.incubator.foreign.MemorySegment)541 ResourceScope (jdk.incubator.foreign.ResourceScope)541 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)541 Test (org.testng.annotations.Test)541 GroupLayout (jdk.incubator.foreign.GroupLayout)540 NativeSymbol (jdk.incubator.foreign.NativeSymbol)271 MethodType (java.lang.invoke.MethodType)270 Addressable (jdk.incubator.foreign.Addressable)270 VarHandle (java.lang.invoke.VarHandle)254 SequenceLayout (jdk.incubator.foreign.SequenceLayout)192 MemoryAddress (jdk.incubator.foreign.MemoryAddress)64 VaList (jdk.incubator.foreign.VaList)1