Search in sources :

Example 26 with SequenceLayout

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

the class StructTests method test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder.

@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder() throws Throwable {
    GroupLayout floatStruct = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.ofSequence(2, floatStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2"));
    MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F);
    MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F);
    MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F);
    MemoryAccess.setFloatAtOffset(structSegmt, 12, 444.44F);
    MemoryAccess.setFloatAtOffset(structSegmt, 16, 555.55F);
    float result = (float) mh.invokeExact(666.66F, structSegmt);
    Assert.assertEquals(result, 2333.31F, 0.01F);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) 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 27 with SequenceLayout

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

the class StructTests method test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder.

@Test
public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder() throws Throwable {
    SequenceLayout shortArray = MemoryLayout.ofSequence(2, C_SHORT);
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), shortArray.withName("array_elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize()));
    MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedShortArray_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(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);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) 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 28 with SequenceLayout

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

the class StructTests method test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName.

@Test
public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName() throws Throwable {
    SequenceLayout longArray = MemoryLayout.ofSequence(2, longLayout);
    GroupLayout structLayout = MemoryLayout.ofStruct(longArray, longLayout);
    MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructWithNestedLongArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setLongAtOffset(structSegmt, 0, 111111111L);
    MemoryAccess.setLongAtOffset(structSegmt, 8, 222222222L);
    MemoryAccess.setLongAtOffset(structSegmt, 16, 333333333L);
    long result = (long) mh.invokeExact(444444444L, structSegmt);
    Assert.assertEquals(result, 1111111110L);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) 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 29 with SequenceLayout

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

the class StructTests method test_addFloatAndFloatsFromStructWithNestedStructArray.

@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray() throws Throwable {
    GroupLayout floatStruct = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.ofSequence(2, floatStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_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);
    Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedStructArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F);
    MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F);
    MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F);
    MemoryAccess.setFloatAtOffset(structSegmt, 12, 444.44F);
    MemoryAccess.setFloatAtOffset(structSegmt, 16, 555.55F);
    float result = (float) mh.invokeExact(666.66F, structSegmt);
    Assert.assertEquals(result, 2333.31F, 0.01F);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) 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 30 with SequenceLayout

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

the class StructTests1 method test_addByteAndBytesFromStructWithNestedByteArray_1.

@Test
public void test_addByteAndBytesFromStructWithNestedByteArray_1() throws Throwable {
    SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, C_CHAR);
    GroupLayout structLayout = MemoryLayout.structLayout(byteArray.withName("array_elem1"), C_CHAR.withName("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("addByteAndBytesFromStructWithNestedByteArray").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) 11);
        MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 12);
        MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 13);
        byte result = (byte) mh.invokeExact((byte) 14, structSegmt);
        Assert.assertEquals(result, 50);
    }
}
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

GroupLayout (jdk.incubator.foreign.GroupLayout)241 SequenceLayout (jdk.incubator.foreign.SequenceLayout)241 MethodHandle (java.lang.invoke.MethodHandle)240 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)240 MemorySegment (jdk.incubator.foreign.MemorySegment)240 Test (org.testng.annotations.Test)240 ResourceScope (jdk.incubator.foreign.ResourceScope)192 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)192 MethodType (java.lang.invoke.MethodType)144 Addressable (jdk.incubator.foreign.Addressable)96 NativeSymbol (jdk.incubator.foreign.NativeSymbol)96 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)48 MemoryLayout (jdk.incubator.foreign.MemoryLayout)1 ValueLayout (jdk.incubator.foreign.ValueLayout)1