Search in sources :

Example 31 with SegmentAllocator

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

the class StructTests1 method test_addLongAndLongsFromNestedStruct_withoutLayoutName_1.

@Test
public void test_addLongAndLongsFromNestedStruct_withoutLayoutName_1() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(longLayout, longLayout);
    GroupLayout structLayout = MemoryLayout.structLayout(longLayout, nestedStructLayout);
    MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromNestedStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setLongAtOffset(structSegmt, 0, 135791357913L);
        MemoryAccess.setLongAtOffset(structSegmt, 8, 246802468024L);
        MemoryAccess.setLongAtOffset(structSegmt, 16, 112233445566L);
        long result = (long) mh.invokeExact(778899001122L, structSegmt);
        Assert.assertEquals(result, 1273726272625L);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 32 with SegmentAllocator

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

the class StructTests1 method test_addShortFromPointerAndShortsFromStruct_returnShortPointer_1.

@Test
public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
    VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment shortSegmt = allocator.allocate(C_SHORT);
        MemoryAccess.setShort(shortSegmt, (short) 12);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        shortHandle1.set(structSegmt, (short) 18);
        shortHandle2.set(structSegmt, (short) 19);
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(shortSegmt.address(), structSegmt);
        MemorySegment resultSegmt = resultAddr.asSegment(C_SHORT.byteSize(), scope);
        VarHandle shortHandle = MemoryHandles.varHandle(short.class, ByteOrder.nativeOrder());
        short result = (short) shortHandle.get(resultSegmt, 0);
        Assert.assertEquals(result, 49);
        Assert.assertEquals(resultSegmt.address().toRawLongValue(), shortSegmt.address().toRawLongValue());
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) 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 33 with SegmentAllocator

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

the class StructTests1 method test_addFloatFromPointerAndFloatsFromStruct_1.

@Test
public void test_addFloatFromPointerAndFloatsFromStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(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);
    Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment floatSegmt = allocator.allocate(C_FLOAT);
        MemoryAccess.setFloat(floatSegmt, 12.12F);
        MemorySegment structSegmt = allocator.allocate(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);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 34 with SegmentAllocator

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

the class StructTests1 method test_addShortAndShortsFromStructWithNestedShortArray_1.

@Test
public void test_addShortAndShortsFromStructWithNestedShortArray_1() throws Throwable {
    SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, C_SHORT);
    GroupLayout structLayout = MemoryLayout.structLayout(shortArray.withName("array_elem1"), C_SHORT.withName("elem2"), MemoryLayout.paddingLayout(C_SHORT.bitSize()));
    MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(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);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) 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 35 with SegmentAllocator

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

the class StructTests1 method test_addDoubleAndDoubleIntFromStruct_1.

@Test
public void test_addDoubleAndDoubleIntFromStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(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);
    Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleIntFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(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);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) 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)541 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)541 MemorySegment (jdk.incubator.foreign.MemorySegment)541 ResourceScope (jdk.incubator.foreign.ResourceScope)541 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)541 Test (org.testng.annotations.Test)541 GroupLayout (jdk.incubator.foreign.GroupLayout)540 NativeSymbol (jdk.incubator.foreign.NativeSymbol)271 MethodType (java.lang.invoke.MethodType)270 Addressable (jdk.incubator.foreign.Addressable)270 VarHandle (java.lang.invoke.VarHandle)254 SequenceLayout (jdk.incubator.foreign.SequenceLayout)192 MemoryAddress (jdk.incubator.foreign.MemoryAddress)64 VaList (jdk.incubator.foreign.VaList)1