Search in sources :

Example 31 with FunctionDescriptor

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

the class StructTests method test_addFloatFromPointerAndFloatsFromStruct.

@Test
public void test_addFloatFromPointerAndFloatsFromStruct() 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(float.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addFloatFromPointerAndFloatsFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment floatSegmt = MemorySegment.allocateNative(C_FLOAT);
    MemoryAccess.setFloat(floatSegmt, 12.12F);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    floatHandle1.set(structSegmt, 18.23F);
    floatHandle2.set(structSegmt, 19.34F);
    float result = (float) mh.invokeExact(floatSegmt.address(), structSegmt);
    Assert.assertEquals(result, 49.69F, 0.01F);
    structSegmt.close();
    floatSegmt.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 32 with FunctionDescriptor

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

the class StructTests method test_addDoubleAndDoublesFromStructWithNestedDoubleArray.

@Test
public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray() throws Throwable {
    SequenceLayout doubleArray = MemoryLayout.ofSequence(2, C_DOUBLE);
    GroupLayout structLayout = MemoryLayout.ofStruct(doubleArray.withName("array_elem1"), C_DOUBLE.withName("elem2"));
    MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructWithNestedDoubleArray").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);
    double result = (double) mh.invokeExact(444.444D, structSegmt);
    Assert.assertEquals(result, 1111.11D, 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 33 with FunctionDescriptor

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

the class StructTests method test_add2DoubleStructs_returnStructPointer.

@Test
public void test_add2DoubleStructs_returnStructPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"));
    VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1"));
    VarHandle doubleHandle2 = structLayout.varHandle(double.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("add2DoubleStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    doubleHandle1.set(structSegmt1, 11.222D);
    doubleHandle2.set(structSegmt1, 22.333D);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    doubleHandle1.set(structSegmt2, 33.444D);
    doubleHandle2.set(structSegmt2, 44.555D);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
    Assert.assertEquals((double) doubleHandle1.get(resultSegmt), 44.666D, 0.001D);
    Assert.assertEquals((double) doubleHandle2.get(resultSegmt), 66.888D, 0.001D);
    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)

Example 34 with FunctionDescriptor

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

the class StructTests method test_addIntAndIntsFromStructPointer.

@Test
public void test_addIntAndIntsFromStructPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
    VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(int.class, int.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_POINTER);
    Symbol functionSymbol = nativeLib.lookup("addIntAndIntsFromStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    intHandle1.set(structSegmt, 11121314);
    intHandle2.set(structSegmt, 15161718);
    int result = (int) mh.invokeExact(19202122, structSegmt.address());
    Assert.assertEquals(result, 45485154);
    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 35 with FunctionDescriptor

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

the class StructTests method test_addBoolAndBoolsFromStructWithNestedBoolArray.

@Test
public void test_addBoolAndBoolsFromStructWithNestedBoolArray() throws Throwable {
    SequenceLayout intArray = MemoryLayout.ofSequence(2, C_INT);
    GroupLayout structLayout = MemoryLayout.ofStruct(intArray.withName("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("addBoolAndBoolsFromStructWithNestedBoolArray").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);
    int result = (int) mh.invokeExact(0, 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)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)939 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)939 Test (org.testng.annotations.Test)939 MemorySegment (jdk.incubator.foreign.MemorySegment)754 GroupLayout (jdk.incubator.foreign.GroupLayout)675 MethodType (java.lang.invoke.MethodType)605 ResourceScope (jdk.incubator.foreign.ResourceScope)549 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)541 Addressable (jdk.incubator.foreign.Addressable)432 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 VarHandle (java.lang.invoke.VarHandle)318 SequenceLayout (jdk.incubator.foreign.SequenceLayout)240 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)173 MemoryAddress (jdk.incubator.foreign.MemoryAddress)127 VaList (jdk.incubator.foreign.VaList)4