Search in sources :

Example 41 with SegmentAllocator

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

the class StructTests2 method test_addByteAndBytesFromNestedStruct_reverseOrder_2.

@Test
public void test_addByteAndBytesFromNestedStruct_reverseOrder_2() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2"), MemoryLayout.paddingLayout(C_CHAR.bitSize()));
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct_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) 12);
        MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 24);
        MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 36);
        byte result = (byte) mh.invokeExact(functionSymbol, (byte) 48, structSegmt);
        Assert.assertEquals(result, 120);
    }
}
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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 42 with SegmentAllocator

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

the class StructTests2 method test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_2.

@Test
public void test_addBoolAndBoolsFromNestedStructWithXor_withoutLayoutName_2() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_CHAR, C_CHAR);
    GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout, 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("addBoolAndBoolsFromNestedStructWithXor").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);
        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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 43 with SegmentAllocator

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

the class StructTests2 method test_add2ShortStructs_returnStruct_2.

@Test
public void test_add2ShortStructs_returnStruct_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
    VarHandle shortHandle2 = structLayout.varHandle(short.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("add2ShortStructs_returnStruct").get();
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MethodHandle mh = clinker.downcallHandle(mt, fd);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        shortHandle1.set(structSegmt1, (short) 56);
        shortHandle2.set(structSegmt1, (short) 45);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        shortHandle1.set(structSegmt2, (short) 78);
        shortHandle2.set(structSegmt2, (short) 67);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2);
        Assert.assertEquals((short) shortHandle1.get(resultSegmt), (short) 134);
        Assert.assertEquals((short) shortHandle2.get(resultSegmt), (short) 112);
    }
}
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 44 with SegmentAllocator

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

the class StructTests2 method test_addIntAndIntsFromStructWithNestedIntArray_2.

@Test
public void test_addIntAndIntsFromStructWithNestedIntArray_2() throws Throwable {
    SequenceLayout intArray = MemoryLayout.sequenceLayout(2, C_INT);
    GroupLayout structLayout = MemoryLayout.structLayout(intArray.withName("array_elem1"), C_INT.withName("elem2"));
    MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedIntArray").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);
        int result = (int) mh.invokeExact(functionSymbol, 4444444, structSegmt);
        Assert.assertEquals(result, 11111110);
    }
}
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 45 with SegmentAllocator

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

the class StructTests2 method test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_2.

@Test
public void test_addBoolAndBoolsFromStructWithNestedBoolArray_reverseOrder_2() throws Throwable {
    SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, C_CHAR);
    GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), boolArray.withName("array_elem2"), MemoryLayout.paddingLayout(C_CHAR.bitSize()));
    MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedBoolArray_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);
        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)

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