Search in sources :

Example 56 with GroupLayout

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

the class StructTests method test_addIntAndIntShortFromStruct.

@Test
public void test_addIntAndIntShortFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_SHORT.withName("elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize()));
    VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addIntAndIntShortFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    elemHandle1.set(structSegmt, 11223344);
    elemHandle2.set(structSegmt, (short) 32766);
    int result = (int) mh.invokeExact(22334455, structSegmt);
    Assert.assertEquals(result, 33590565);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 57 with GroupLayout

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

the class StructTests method test_addByteFromPointerAndBytesFromStruct.

@Test
public void test_addByteFromPointerAndBytesFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(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(byte.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addByteFromPointerAndBytesFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment byteSegmt = MemorySegment.allocateNative(C_CHAR);
    MemoryAccess.setByte(byteSegmt, (byte) 12);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt, (byte) 14);
    byteHandle2.set(structSegmt, (byte) 16);
    byte result = (byte) mh.invokeExact(byteSegmt.address(), structSegmt);
    Assert.assertEquals(result, 42);
    byteSegmt.close();
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 58 with GroupLayout

use of jdk.incubator.foreign.GroupLayout 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 59 with GroupLayout

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

the class StructTests method test_add2FloatStructs_returnStruct.

@Test
public void test_add2FloatStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
    VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2FloatStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    floatHandle1.set(structSegmt1, 25.12F);
    floatHandle2.set(structSegmt1, 11.23F);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    floatHandle1.set(structSegmt2, 24.34F);
    floatHandle2.set(structSegmt2, 13.45F);
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
    Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
    structSegmt1.close();
    structSegmt2.close();
    resultSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 60 with GroupLayout

use of jdk.incubator.foreign.GroupLayout 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)

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