Search in sources :

Example 76 with GroupLayout

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

the class StructTests1 method test_add2DoubleStructs_returnStructPointer_1.

@Test
public void test_add2DoubleStructs_returnStructPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"));
    VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1"));
    VarHandle doubleHandle2 = structLayout.varHandle(double.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("add2DoubleStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        doubleHandle1.set(structSegmt1, 11.222D);
        doubleHandle2.set(structSegmt1, 22.333D);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        doubleHandle1.set(structSegmt2, 33.444D);
        doubleHandle2.set(structSegmt2, 44.555D);
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
        MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
        Assert.assertEquals((double) doubleHandle1.get(resultSegmt), 44.666D, 0.001D);
        Assert.assertEquals((double) doubleHandle2.get(resultSegmt), 66.888D, 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) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 77 with GroupLayout

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

the class StructTests1 method test_add2ShortStructs_returnStruct_1.

@Test
public void test_add2ShortStructs_returnStruct_1() 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(functionSymbol, allocator, 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(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 78 with GroupLayout

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

the class StructTests1 method test_addIntFromPointerAndIntsFromStruct_returnIntPointer_1.

@Test
public void test_addIntFromPointerAndIntsFromStruct_returnIntPointer_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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addIntFromPointerAndIntsFromStruct_returnIntPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment intSegmt = allocator.allocate(C_INT);
        MemoryAccess.setInt(intSegmt, 1122333);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        intHandle1.set(structSegmt, 4455666);
        intHandle2.set(structSegmt, 7788999);
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(intSegmt.address(), structSegmt);
        MemorySegment resultSegmt = resultAddr.asSegment(C_INT.byteSize(), scope);
        VarHandle intHandle = MemoryHandles.varHandle(int.class, ByteOrder.nativeOrder());
        int result = (int) intHandle.get(resultSegmt, 0);
        Assert.assertEquals(result, 13366998);
        Assert.assertEquals(resultSegmt.address().toRawLongValue(), intSegmt.address().toRawLongValue());
    }
}
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 79 with GroupLayout

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

the class StructTests1 method test_addByteFromPointerAndBytesFromStruct_returnBytePointer_1.

@Test
public void test_addByteFromPointerAndBytesFromStruct_returnBytePointer_1() 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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct_returnBytePointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment byteSegmt = allocator.allocate(C_CHAR);
        MemoryAccess.setByte(byteSegmt, (byte) 12);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        byteHandle1.set(structSegmt, (byte) 18);
        byteHandle2.set(structSegmt, (byte) 19);
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(byteSegmt.address(), structSegmt);
        MemorySegment resultSegmt = resultAddr.asSegment(C_CHAR.byteSize(), scope);
        VarHandle byteHandle = MemoryHandles.varHandle(byte.class, ByteOrder.nativeOrder());
        byte result = (byte) byteHandle.get(resultSegmt, 0);
        Assert.assertEquals(result, 49);
        Assert.assertEquals(resultSegmt.address().toRawLongValue(), byteSegmt.address().toRawLongValue());
    }
}
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 80 with GroupLayout

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

the class StructTests1 method test_addDoubleAndDoublesFromNestedStruct_reverseOrder_1.

@Test
public void test_addDoubleAndDoublesFromNestedStruct_reverseOrder_1() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2"));
    MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromNestedStruct_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.setDoubleAtOffset(structSegmt, 0, 31.789D);
        MemoryAccess.setDoubleAtOffset(structSegmt, 8, 33.456D);
        MemoryAccess.setDoubleAtOffset(structSegmt, 16, 35.123D);
        double result = (double) mh.invokeExact(37.864D, structSegmt);
        Assert.assertEquals(result, 138.232D, 0.001D);
    }
}
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)

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