Search in sources :

Example 66 with FunctionDescriptor

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

the class StructTests method test_addBoolFromPointerAndBoolsFromStructWithXor.

@Test
public void test_addBoolFromPointerAndBoolsFromStructWithXor() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
    VarHandle boolHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle boolHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(boolean.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addBoolFromPointerAndBoolsFromStructWithXor").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment booleanSegmt = MemorySegment.allocateNative(C_INT);
    MemoryAccess.setInt(booleanSegmt, 1);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    boolHandle1.set(structSegmt, 0);
    boolHandle2.set(structSegmt, 1);
    boolean result = (boolean) mh.invokeExact(booleanSegmt.address(), structSegmt);
    Assert.assertEquals(result, false);
    booleanSegmt.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 67 with FunctionDescriptor

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

the class StructTests method test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer.

@Test
public void test_addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer() 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("addDoubleFromPointerAndDoublesFromStruct_returnDoublePointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment doubleSegmt = MemorySegment.allocateNative(C_DOUBLE);
    MemoryAccess.setDouble(doubleSegmt, 212.123D);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    doubleHandle1.set(structSegmt, 218.456D);
    doubleHandle2.set(structSegmt, 219.789D);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(doubleSegmt.address(), structSegmt);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_DOUBLE.byteSize());
    VarHandle doubleHandle = MemoryHandles.varHandle(double.class, ByteOrder.nativeOrder());
    double result = (double) doubleHandle.get(resultSegmt, 0);
    Assert.assertEquals(result, 650.368D, 0.001D);
    Assert.assertEquals(resultSegmt.address().toRawLongValue(), doubleSegmt.address().toRawLongValue());
    doubleSegmt.close();
    structSegmt.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 68 with FunctionDescriptor

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

the class StructTests method test_addFloatAndFloatsFromStruct.

@Test
public void test_addFloatAndFloatsFromStruct() 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, float.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    floatHandle1.set(structSegmt, 8.12F);
    floatHandle2.set(structSegmt, 9.24F);
    float result = (float) mh.invokeExact(6.56F, structSegmt);
    Assert.assertEquals(result, 23.92F, 0.01F);
    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 69 with FunctionDescriptor

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

the class StructTests method test_addDoubleAndDoubleIntFromStruct.

@Test
public void test_addDoubleAndDoubleIntFromStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), C_INT.withName("elem2"));
    VarHandle elemHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoubleIntFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    elemHandle1.set(structSegmt, 218.555D);
    elemHandle2.set(structSegmt, 19);
    double result = (double) mh.invokeExact(216.666D, structSegmt);
    Assert.assertEquals(result, 454.221D, 0.001D);
    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 70 with FunctionDescriptor

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

the class StructTests method test_addShortAndShortsFromNestedStruct_reverseOrder.

@Test
public void test_addShortAndShortsFromNestedStruct_reverseOrder() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), nestedStructLayout.withName("struct_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("addShortAndShortsFromNestedStruct_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setShortAtOffset(structSegmt, 0, (short) 31);
    MemoryAccess.setShortAtOffset(structSegmt, 2, (short) 33);
    MemoryAccess.setShortAtOffset(structSegmt, 4, (short) 35);
    short result = (short) mh.invokeExact((short) 37, structSegmt);
    Assert.assertEquals(result, 136);
    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) 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