Search in sources :

Example 61 with SegmentAllocator

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

the class StructTests1 method test_addBoolAndBoolsFromStructWithNestedStructArray_1.

@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray_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(structArray.withName("struct_array_elem1"), C_CHAR.withName("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").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)

Example 62 with SegmentAllocator

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

the class StructTests1 method test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_1.

@Test
public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName_1() throws Throwable {
    SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, C_SHORT);
    GroupLayout structLayout = MemoryLayout.structLayout(shortArray, C_SHORT, MemoryLayout.paddingLayout(C_SHORT.bitSize()));
    MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setShortAtOffset(structSegmt, 0, (short) 111);
        MemoryAccess.setShortAtOffset(structSegmt, 2, (short) 222);
        MemoryAccess.setShortAtOffset(structSegmt, 4, (short) 333);
        short result = (short) mh.invokeExact((short) 444, structSegmt);
        Assert.assertEquals(result, 1110);
    }
}
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 63 with SegmentAllocator

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

the class StructTests1 method test_addFloatAndFloatsFromStructWithNestedFloatArray_1.

@Test
public void test_addFloatAndFloatsFromStructWithNestedFloatArray_1() throws Throwable {
    SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, C_FLOAT);
    GroupLayout structLayout = MemoryLayout.structLayout(floatArray.withName("array_elem1"), C_FLOAT.withName("elem2"));
    MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F);
        MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F);
        MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F);
        float result = (float) mh.invokeExact(444.44F, structSegmt);
        Assert.assertEquals(result, 1111.1F, 0.01F);
    }
}
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 64 with SegmentAllocator

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

the class StructTests1 method test_add3ShortStructs_returnStruct_1.

@Test
public void test_add3ShortStructs_returnStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"), MemoryLayout.paddingLayout(C_SHORT.bitSize()));
    VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
    VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
    VarHandle shortHandle3 = structLayout.varHandle(short.class, PathElement.groupElement("elem3"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("add3ShortStructs_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) 25);
        shortHandle2.set(structSegmt1, (short) 26);
        shortHandle3.set(structSegmt1, (short) 27);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        shortHandle1.set(structSegmt2, (short) 34);
        shortHandle2.set(structSegmt2, (short) 35);
        shortHandle3.set(structSegmt2, (short) 36);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
        Assert.assertEquals((short) shortHandle1.get(resultSegmt), (short) 59);
        Assert.assertEquals((short) shortHandle2.get(resultSegmt), (short) 61);
        Assert.assertEquals((short) shortHandle3.get(resultSegmt), (short) 63);
    }
}
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 65 with SegmentAllocator

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

the class StructTests1 method test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder_1.

@Test
public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder_1() throws Throwable {
    SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, C_DOUBLE);
    GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2"));
    MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray_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, 111.111D);
        MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D);
        MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D);
        double result = (double) mh.invokeExact(444.444D, structSegmt);
        Assert.assertEquals(result, 1111.11D, 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) 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