Search in sources :

Example 71 with GroupLayout

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

the class StructTests method test_addFloatAndFloatsFromStructWithNestedStructArray.

@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray() throws Throwable {
    GroupLayout floatStruct = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.ofSequence(2, floatStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), C_FLOAT.withName("elem2"));
    MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedStructArray").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);
    MemoryAccess.setFloatAtOffset(structSegmt, 12, 444.44F);
    MemoryAccess.setFloatAtOffset(structSegmt, 16, 555.55F);
    float result = (float) mh.invokeExact(666.66F, structSegmt);
    Assert.assertEquals(result, 2333.31F, 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 72 with GroupLayout

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

the class StructTests method test_add3ByteStructs_returnStruct.

@Test
public void test_add3ByteStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"), C_CHAR.withName("elem3"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize()));
    VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    VarHandle byteHandle3 = structLayout.varHandle(byte.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("add3ByteStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt1, (byte) 25);
    byteHandle2.set(structSegmt1, (byte) 11);
    byteHandle3.set(structSegmt1, (byte) 12);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt2, (byte) 24);
    byteHandle2.set(structSegmt2, (byte) 13);
    byteHandle3.set(structSegmt2, (byte) 16);
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals((byte) byteHandle1.get(resultSegmt), (byte) 49);
    Assert.assertEquals((byte) byteHandle2.get(resultSegmt), (byte) 24);
    Assert.assertEquals((byte) byteHandle3.get(resultSegmt), (byte) 28);
    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)

Example 73 with GroupLayout

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

the class StructTests method test_add3CharStructs_returnStruct.

@Test
public void test_add3CharStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"));
    VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
    VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2"));
    VarHandle charHandle3 = structLayout.varHandle(char.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("add3CharStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt1, 'A');
    charHandle2.set(structSegmt1, 'B');
    charHandle3.set(structSegmt1, 'C');
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt2, 'B');
    charHandle2.set(structSegmt2, 'C');
    charHandle3.set(structSegmt2, 'D');
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals(charHandle1.get(resultSegmt), 'B');
    Assert.assertEquals(charHandle2.get(resultSegmt), 'D');
    Assert.assertEquals(charHandle3.get(resultSegmt), 'F');
    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)

Example 74 with GroupLayout

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

the class StructTests1 method test_addByteAndBytesFromNestedStruct_1.

@Test
public void test_addByteAndBytesFromNestedStruct_1() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_elem1"), C_CHAR.withName("elem2"));
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromNestedStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 11);
        MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 22);
        MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 33);
        byte result = (byte) mh.invokeExact((byte) 46, structSegmt);
        Assert.assertEquals(result, 112);
    }
}
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 75 with GroupLayout

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

the class StructTests1 method test_addByteAndBytesFromStructWithNestedByteArray_1.

@Test
public void test_addByteAndBytesFromStructWithNestedByteArray_1() throws Throwable {
    SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, C_CHAR);
    GroupLayout structLayout = MemoryLayout.structLayout(byteArray.withName("array_elem1"), C_CHAR.withName("elem2"), MemoryLayout.paddingLayout(C_CHAR.bitSize()));
    MethodType mt = MethodType.methodType(byte.class, byte.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addByteAndBytesFromStructWithNestedByteArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        MemoryAccess.setByteAtOffset(structSegmt, 0, (byte) 11);
        MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 12);
        MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 13);
        byte result = (byte) mh.invokeExact((byte) 14, structSegmt);
        Assert.assertEquals(result, 50);
    }
}
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)

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