Search in sources :

Example 76 with NativeSymbol

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

the class StructTests1 method test_addByteAndBytesFromStruct_1.

@Test
public void test_addByteAndBytesFromStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"));
    VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        byteHandle1.set(structSegmt, (byte) 8);
        byteHandle2.set(structSegmt, (byte) 9);
        byte result = (byte) mh.invokeExact((byte) 6, structSegmt);
        Assert.assertEquals(result, 23);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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 77 with NativeSymbol

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

the class StructTests1 method test_addByteFromPointerAndBytesFromStruct_1.

@Test
public void test_addByteFromPointerAndBytesFromStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"));
    VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, ADDRESS, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addByteFromPointerAndBytesFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment byteSegmt = allocator.allocate(JAVA_BYTE, (byte) 12);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        byteHandle1.set(structSegmt, (byte) 14);
        byteHandle2.set(structSegmt, (byte) 16);
        byte result = (byte) mh.invoke(byteSegmt, structSegmt);
        Assert.assertEquals(result, 42);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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 78 with NativeSymbol

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

the class StructTests1 method test_addLongFromPointerAndLongsFromStruct_returnLongPointer_1.

@Test
public void test_addLongFromPointerAndLongsFromStruct_returnLongPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"));
    VarHandle longHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle longHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addLongFromPointerAndLongsFromStruct_returnLongPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment longSegmt = allocator.allocate(JAVA_LONG, 1122334455L);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        longHandle1.set(structSegmt, 6677889900L);
        longHandle2.set(structSegmt, 1234567890L);
        MemoryAddress resultAddr = (MemoryAddress) mh.invoke(longSegmt, structSegmt);
        Assert.assertEquals(resultAddr.get(JAVA_LONG, 0), 9034792245L);
        Assert.assertEquals(resultAddr.toRawLongValue(), longSegmt.address().toRawLongValue());
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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 79 with NativeSymbol

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

the class StructTests1 method test_addFloatAndFloatsFromNestedStruct_1.

@Test
public void test_addFloatAndFloatsFromNestedStruct_1() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), JAVA_FLOAT.withName("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromNestedStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_FLOAT, 0, 31.22F);
        structSegmt.set(JAVA_FLOAT, 4, 33.44F);
        structSegmt.set(JAVA_FLOAT, 8, 35.66F);
        float result = (float) mh.invokeExact(37.88F, structSegmt);
        Assert.assertEquals(result, 138.2F, 0.01F);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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 80 with NativeSymbol

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

the class StructTests1 method test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_1.

@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
    GroupLayout boolStruct = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(JAVA_BOOLEAN.bitSize() * 3));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_BOOLEAN, 0, false);
        structSegmt.set(JAVA_BOOLEAN, 1, true);
        structSegmt.set(JAVA_BOOLEAN, 2, false);
        structSegmt.set(JAVA_BOOLEAN, 3, true);
        structSegmt.set(JAVA_BOOLEAN, 4, false);
        boolean result = (boolean) mh.invokeExact(true, structSegmt);
        Assert.assertEquals(result, true);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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)334 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)334 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 Test (org.testng.annotations.Test)334 MemorySegment (jdk.incubator.foreign.MemorySegment)291 ResourceScope (jdk.incubator.foreign.ResourceScope)274 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)271 GroupLayout (jdk.incubator.foreign.GroupLayout)270 VarHandle (java.lang.invoke.VarHandle)126 SequenceLayout (jdk.incubator.foreign.SequenceLayout)96 MemoryAddress (jdk.incubator.foreign.MemoryAddress)34 VaList (jdk.incubator.foreign.VaList)4