Search in sources :

Example 21 with SequenceLayout

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

the class StructTests method test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName.

@Test
public void test_addCharAndCharsFromStructWithNestedCharArray_withoutLayoutName() throws Throwable {
    SequenceLayout charArray = MemoryLayout.ofSequence(2, C_SHORT);
    GroupLayout structLayout = MemoryLayout.ofStruct(charArray, C_SHORT, MemoryLayout.ofPaddingBits(C_SHORT.bitSize()));
    MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addCharAndCharsFromStructWithNestedCharArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(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');
    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 22 with SequenceLayout

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

the class StructTests method test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder.

@Test
public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder() throws Throwable {
    GroupLayout intStruct = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), structArray.withName("struct_array_elem2"));
    MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructWithNestedStructArray_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111);
    MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222);
    MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333);
    MemoryAccess.setIntAtOffset(structSegmt, 12, 4444444);
    MemoryAccess.setIntAtOffset(structSegmt, 16, 5555555);
    int result = (int) mh.invokeExact(6666666, structSegmt);
    Assert.assertEquals(result, 23333331);
    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 23 with SequenceLayout

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

the class StructTests method test_addLongAndLongsFromStructWithNestedStructArray.

@Test
public void test_addLongAndLongsFromStructWithNestedStructArray() throws Throwable {
    GroupLayout longStruct = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.ofSequence(2, longStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), longLayout.withName("elem2"));
    MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructWithNestedStructArray").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);
    MemoryAccess.setLongAtOffset(structSegmt, 24, 444444444L);
    MemoryAccess.setLongAtOffset(structSegmt, 32, 555555555L);
    long result = (long) mh.invokeExact(666666666L, structSegmt);
    Assert.assertEquals(result, 2333333331L);
    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 24 with SequenceLayout

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

the class StructTests method test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName.

@Test
public void test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName() throws Throwable {
    GroupLayout doubleStruct = MemoryLayout.ofStruct(C_DOUBLE, C_DOUBLE);
    SequenceLayout structArray = MemoryLayout.ofSequence(2, doubleStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_DOUBLE);
    MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setDoubleAtOffset(structSegmt, 0, 111.111D);
    MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D);
    MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D);
    MemoryAccess.setDoubleAtOffset(structSegmt, 24, 444.444D);
    MemoryAccess.setDoubleAtOffset(structSegmt, 32, 555.555D);
    double result = (double) mh.invokeExact(666.666D, structSegmt);
    Assert.assertEquals(result, 2333.331D, 0.001D);
    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 25 with SequenceLayout

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

the class StructTests method test_addFloatAndFloatsFromStructWithNestedFloatArray.

@Test
public void test_addFloatAndFloatsFromStructWithNestedFloatArray() throws Throwable {
    SequenceLayout floatArray = MemoryLayout.ofSequence(2, C_FLOAT);
    GroupLayout structLayout = MemoryLayout.ofStruct(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);
    Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").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);
    float result = (float) mh.invokeExact(444.44F, structSegmt);
    Assert.assertEquals(result, 1111.1F, 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)

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