Search in sources :

Example 11 with SequenceLayout

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

the class StructTests method test_addCharAndCharsFromStructWithNestedCharArray.

@Test
public void test_addCharAndCharsFromStructWithNestedCharArray() throws Throwable {
    SequenceLayout charArray = MemoryLayout.ofSequence(2, C_SHORT);
    GroupLayout structLayout = MemoryLayout.ofStruct(charArray.withName("array_elem1"), C_SHORT.withName("elem2"), 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 12 with SequenceLayout

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

the class StructTests method test_addShortAndShortsFromStructWithNestedStructArray.

@Test
public void test_addShortAndShortsFromStructWithNestedStructArray() throws Throwable {
    GroupLayout shortStruct = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.ofSequence(2, shortStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struc_array_elem1"), C_SHORT.withName("elem2"));
    MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedStructArray").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);
    MemoryAccess.setShortAtOffset(structSegmt, 6, (short) 444);
    MemoryAccess.setShortAtOffset(structSegmt, 8, (short) 555);
    short result = (short) mh.invokeExact((short) 666, structSegmt);
    Assert.assertEquals(result, 2331);
    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 13 with SequenceLayout

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

the class StructTests method test_addBoolAndBoolsFromStructWithNestedStructArray.

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

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

the class StructTests method test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName.

@Test
public void test_addShortAndShortsFromStructWithNestedShortArray_withoutLayoutName() throws Throwable {
    SequenceLayout shortArray = MemoryLayout.ofSequence(2, C_SHORT);
    GroupLayout structLayout = MemoryLayout.ofStruct(shortArray, C_SHORT, 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").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 15 with SequenceLayout

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

the class StructTests method test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder.

@Test
public void test_addByteAndBytesFromStructWithNestedStructArray_reverseOrder() throws Throwable {
    GroupLayout byteStruct = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.ofSequence(2, byteStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), structArray.withName("struct_array_elem2"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize() * 3));
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromStructWithNestedStructArray_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 12);
    MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 14);
    MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 16);
    MemoryAccess.setByteAtOffset(structSegmt, 3, (byte) 18);
    MemoryAccess.setByteAtOffset(structSegmt, 4, (byte) 20);
    byte result = (byte) mh.invokeExact((byte) 22, structSegmt);
    Assert.assertEquals(result, 102);
    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