Search in sources :

Example 96 with GroupLayout

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

the class StructTests1 method test_addDoubleAndFloatDoubleFromStruct_1.

@Test
public void test_addDoubleAndFloatDoubleFromStruct_1() throws Throwable {
    GroupLayout structLayout = null;
    MemorySegment structSegmt = null;
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        /* The size of [float, 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_FLOAT.withName("elem1"), C_DOUBLE.withName("elem2"));
            structSegmt = allocator.allocate(structLayout);
            MemoryAccess.setFloatAtOffset(structSegmt, 0, 18.444F);
            MemoryAccess.setDoubleAtOffset(structSegmt, 4, 619.777D);
        } else {
            structLayout = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), MemoryLayout.paddingLayout(C_FLOAT.bitSize()), C_DOUBLE.withName("elem2"));
            VarHandle elemHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
            VarHandle elemHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2"));
            structSegmt = allocator.allocate(structLayout);
            elemHandle1.set(structSegmt, 18.444F);
            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("addDoubleAndFloatDoubleFromStruct").get();
        MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
        double result = (double) mh.invokeExact(113.567D, structSegmt);
        Assert.assertEquals(result, 751.788D, 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 97 with GroupLayout

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

the class StructTests1 method test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_1.

@Test
public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_1() throws Throwable {
    SequenceLayout charArray = MemoryLayout.sequenceLayout(2, C_SHORT);
    GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), charArray.withName("array_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_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.setCharAtOffset(structSegmt, 0, 'A');
        MemoryAccess.setCharAtOffset(structSegmt, 2, 'B');
        MemoryAccess.setCharAtOffset(structSegmt, 4, 'C');
        char result = (char) mh.invokeExact('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 98 with GroupLayout

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

the class StructTests1 method test_add2FloatStructs_returnStruct_1.

@Test
public void test_add2FloatStructs_returnStruct_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(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStruct").get();
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
        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);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
        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) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 99 with GroupLayout

use of jdk.incubator.foreign.GroupLayout 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 100 with GroupLayout

use of jdk.incubator.foreign.GroupLayout 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)

Aggregations

GroupLayout (jdk.incubator.foreign.GroupLayout)677 MethodHandle (java.lang.invoke.MethodHandle)675 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)675 MemorySegment (jdk.incubator.foreign.MemorySegment)675 Test (org.testng.annotations.Test)675 ResourceScope (jdk.incubator.foreign.ResourceScope)540 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)540 MethodType (java.lang.invoke.MethodType)405 VarHandle (java.lang.invoke.VarHandle)318 Addressable (jdk.incubator.foreign.Addressable)270 NativeSymbol (jdk.incubator.foreign.NativeSymbol)270 SequenceLayout (jdk.incubator.foreign.SequenceLayout)241 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)135 MemoryAddress (jdk.incubator.foreign.MemoryAddress)80 MemoryLayout (jdk.incubator.foreign.MemoryLayout)2 ValueLayout (jdk.incubator.foreign.ValueLayout)1