Search in sources :

Example 26 with SegmentAllocator

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

the class StructTests1 method test_addIntAndIntLongFromStruct_1.

@Test
public void test_addIntAndIntLongFromStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), MemoryLayout.paddingLayout(C_INT.bitSize()), longLayout.withName("elem2"));
    VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(long.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_INT, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntLongFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        elemHandle1.set(structSegmt, 11223344);
        elemHandle2.set(structSegmt, 667788990011L);
        long result = (long) mh.invokeExact(22446688, structSegmt);
        Assert.assertEquals(result, 667822660043L);
    }
}
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 27 with SegmentAllocator

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

the class StructTests1 method test_addIntAndIntsFromStructPointer_1.

@Test
public void test_addIntAndIntsFromStructPointer_1() 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, int.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_POINTER);
    Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        intHandle1.set(structSegmt, 11121314);
        intHandle2.set(structSegmt, 15161718);
        int result = (int) mh.invokeExact(19202122, structSegmt.address());
        Assert.assertEquals(result, 45485154);
    }
}
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 28 with SegmentAllocator

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

the class StructTests1 method test_add2FloatStructs_returnStructPointer_1.

@Test
public void test_add2FloatStructs_returnStructPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
    VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(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);
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
        MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
        Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
        Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
    }
}
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) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 29 with SegmentAllocator

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

the class StructTests1 method test_addDoubleAndIntDoubleFromStruct_1.

@Test
public void test_addDoubleAndIntDoubleFromStruct_1() throws Throwable {
    GroupLayout structLayout = null;
    MemorySegment structSegmt = null;
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        /* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default
			 * while the same struct is 16 bytes with padding on other platforms.
			 */
        if (isAixOS) {
            structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), C_DOUBLE.withName("elem2"));
            structSegmt = allocator.allocate(structLayout);
            MemoryAccess.setIntAtOffset(structSegmt, 0, 18);
            MemoryAccess.setDoubleAtOffset(structSegmt, 4, 619.777D);
        } else {
            structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), MemoryLayout.paddingLayout(C_INT.bitSize()), C_DOUBLE.withName("elem2"));
            VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
            VarHandle elemHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2"));
            structSegmt = allocator.allocate(structLayout);
            elemHandle1.set(structSegmt, 18);
            elemHandle2.set(structSegmt, 619.777D);
        }
        MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
        FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
        Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFromStruct").get();
        MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
        double result = (double) mh.invokeExact(113.567D, structSegmt);
        Assert.assertEquals(result, 751.344D, 0.001D);
    }
}
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 30 with SegmentAllocator

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

the class StructTests1 method test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_1.

@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_1() 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(functionSymbol, 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(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)

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