Search in sources :

Example 66 with GroupLayout

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

the class StructTests method test_add2CharStructs_returnStruct.

@Test
public void test_add2CharStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
    VarHandle charHandle2 = structLayout.varHandle(char.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("add2CharStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt1, 'A');
    charHandle2.set(structSegmt1, 'B');
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt2, 'C');
    charHandle2.set(structSegmt2, 'D');
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals(charHandle1.get(resultSegmt), 'C');
    Assert.assertEquals(charHandle2.get(resultSegmt), 'E');
    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 67 with GroupLayout

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

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

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

the class StructTests method test_add3ShortStructs_returnStruct.

@Test
public void test_add3ShortStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"), MemoryLayout.ofPaddingBits(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);
    Symbol functionSymbol = nativeLib.lookup("add3ShortStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    shortHandle1.set(structSegmt1, (short) 25);
    shortHandle2.set(structSegmt1, (short) 26);
    shortHandle3.set(structSegmt1, (short) 27);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(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);
    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 70 with GroupLayout

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

the class StructTests method test_add2FloatStructs_returnStructPointer.

@Test
public void test_add2FloatStructs_returnStructPointer() 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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2FloatStructs_returnStructPointer").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);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
    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) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) 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