Search in sources :

Example 21 with GroupLayout

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

the class StructTests method test_addByteAndBytesFromNestedStruct.

@Test
public void test_addByteAndBytesFromNestedStruct() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.ofStruct(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);
    Symbol functionSymbol = nativeLib.lookup("addByteAndBytesFromNestedStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(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);
    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 22 with GroupLayout

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

the class StructTests method test_add2ByteStructs_returnStruct.

@Test
public void test_add2ByteStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2ByteStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt1, (byte) 25);
    byteHandle2.set(structSegmt1, (byte) 11);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    byteHandle1.set(structSegmt2, (byte) 24);
    byteHandle2.set(structSegmt2, (byte) 13);
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals((byte) byteHandle1.get(resultSegmt), (byte) 49);
    Assert.assertEquals((byte) byteHandle2.get(resultSegmt), (byte) 24);
    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 23 with GroupLayout

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

the class StructTests method test_addBoolAndBoolsFromStructWithNestedStructArray.

@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray() throws Throwable {
    GroupLayout intStruct = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.ofSequence(2, intStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), C_INT.withName("elem2"));
    MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromStructWithNestedStructArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setIntAtOffset(structSegmt, 0, 0);
    MemoryAccess.setIntAtOffset(structSegmt, 4, 1);
    MemoryAccess.setIntAtOffset(structSegmt, 8, 0);
    MemoryAccess.setIntAtOffset(structSegmt, 12, 1);
    MemoryAccess.setIntAtOffset(structSegmt, 16, 0);
    int result = (int) mh.invokeExact(1, structSegmt);
    Assert.assertEquals(result, 1);
    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 24 with GroupLayout

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

the class StructTests method test_add2BoolStructsWithXor_returnStruct.

@Test
public void test_add2BoolStructsWithXor_returnStruct() 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(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2BoolStructsWithXor_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    boolHandle1.set(structSegmt1, 1);
    boolHandle2.set(structSegmt1, 0);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    boolHandle1.set(structSegmt2, 1);
    boolHandle2.set(structSegmt2, 1);
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals(boolHandle1.get(resultSegmt), 0);
    Assert.assertEquals(boolHandle2.get(resultSegmt), 1);
    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 25 with GroupLayout

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

the class StructTests method test_add2BoolStructsWithXor_returnStructPointer.

@Test
public void test_add2BoolStructsWithXor_returnStructPointer() 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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2BoolStructsWithXor_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    boolHandle1.set(structSegmt1, 1);
    boolHandle2.set(structSegmt1, 0);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    boolHandle1.set(structSegmt2, 1);
    boolHandle2.set(structSegmt2, 1);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
    Assert.assertEquals(boolHandle1.get(resultSegmt), 0);
    Assert.assertEquals(boolHandle2.get(resultSegmt), 1);
    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) MemoryAddress(jdk.incubator.foreign.MemoryAddress) 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