Search in sources :

Example 6 with GroupLayout

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

the class StructTests method test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder.

@Test
public void test_addDoubleAndDoublesFromStructWithNestedDoubleArray_reverseOrder() throws Throwable {
    SequenceLayout doubleArray = MemoryLayout.ofSequence(2, C_DOUBLE);
    GroupLayout structLayout = MemoryLayout.ofStruct(C_DOUBLE.withName("elem1"), doubleArray.withName("array_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_reverseOrder").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 7 with GroupLayout

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

the class StructTests method test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName.

@Test
public void test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName() throws Throwable {
    SequenceLayout floatArray = MemoryLayout.ofSequence(2, C_FLOAT);
    GroupLayout structLayout = MemoryLayout.ofStruct(floatArray, C_FLOAT);
    MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F);
    MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F);
    MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F);
    float result = (float) mh.invokeExact(444.44F, structSegmt);
    Assert.assertEquals(result, 1111.1F, 0.01F);
    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 8 with GroupLayout

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

the class StructTests method test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer.

@Test
public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer() 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("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").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);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(floatSegmt.address(), structSegmt);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(C_FLOAT.byteSize());
    VarHandle floatHandle = MemoryHandles.varHandle(float.class, ByteOrder.nativeOrder());
    float result = (float) floatHandle.get(resultSegmt, 0);
    Assert.assertEquals(result, 49.69F, 0.01F);
    Assert.assertEquals(resultSegmt.address().toRawLongValue(), floatSegmt.address().toRawLongValue());
    floatSegmt.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 9 with GroupLayout

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

the class StructTests method test_addBoolAndBoolsFromNestedStructWithXor_reverseOrder.

@Test
public void test_addBoolAndBoolsFromNestedStructWithXor_reverseOrder() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2"));
    MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromNestedStructWithXor_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setIntAtOffset(structSegmt, 0, 1);
    MemoryAccess.setIntAtOffset(structSegmt, 4, 0);
    MemoryAccess.setIntAtOffset(structSegmt, 8, 1);
    boolean result = (boolean) mh.invokeExact(true, structSegmt);
    Assert.assertEquals(result, true);
    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)

Example 10 with GroupLayout

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

the class StructTests method test_add3IntStructs_returnStruct.

@Test
public void test_add3IntStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"), C_INT.withName("elem3"));
    VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    VarHandle intHandle3 = structLayout.varHandle(int.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("add3IntStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    intHandle1.set(structSegmt1, 11223344);
    intHandle2.set(structSegmt1, 55667788);
    intHandle3.set(structSegmt1, 99001122);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    intHandle1.set(structSegmt2, 33445566);
    intHandle2.set(structSegmt2, 77889900);
    intHandle3.set(structSegmt2, 44332211);
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals(intHandle1.get(resultSegmt), 44668910);
    Assert.assertEquals(intHandle2.get(resultSegmt), 133557688);
    Assert.assertEquals(intHandle3.get(resultSegmt), 143333333);
    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)

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