Search in sources :

Example 46 with FunctionDescriptor

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

the class PrimitiveTypeTests2 method test_addTwoIntsReturnVoid_fromMemAddr_2.

@Test
public void test_addTwoIntsReturnVoid_fromMemAddr_2() throws Throwable {
    MethodType mt = MethodType.methodType(void.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.ofVoid(C_INT, C_INT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2IntsReturnVoid").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
    mh.invokeExact(454, 398);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 47 with FunctionDescriptor

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

the class PrimitiveTypeTests2 method test_generateNewCharFromPointer_2.

@Test
public void test_generateNewCharFromPointer_2() throws Throwable {
    MethodType mt = MethodType.methodType(char.class, MemoryAddress.class, char.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_POINTER, C_SHORT);
    Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFromCharAndCharFromPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    MemorySegment shortSegmt = MemorySegment.allocateNative(C_SHORT, resourceScope);
    MemoryAccess.setChar(shortSegmt, 'B');
    char result = (char) mh.invokeExact(shortSegmt.address(), 'D');
    Assert.assertEquals(result, 'C');
}
Also used : MethodType(java.lang.invoke.MethodType) 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 48 with FunctionDescriptor

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

the class PrimitiveTypeTests2 method test_memoryAllocFreeFromDefaultLib_fromMemAddr_2.

@Test
public void test_memoryAllocFreeFromDefaultLib_fromMemAddr_2() throws Throwable {
    /* Temporarily disable the default library loading on AIX till we figure out a way
		 * around to handle the case as the official implementation in OpenJDK17 doesn't
		 * help to load the static libray (libc.a).
		 */
    if (!isAixOS) {
        Addressable allocSymbol = defaultLibLookup.lookup("malloc").get();
        MemoryAddress allocMemAddrFromSymbol = allocSymbol.address();
        MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
        FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
        MethodHandle allocHandle = clinker.downcallHandle(allocMemAddrFromSymbol, allocator, allocMethodType, allocFuncDesc);
        MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
        MemorySegment memSeg = allocMemAddr.asSegment(10L, resourceScope);
        MemoryAccess.setIntAtOffset(memSeg, 0, 15);
        Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, 0), 15);
        Addressable freeSymbol = defaultLibLookup.lookup("free").get();
        MemoryAddress freeMemAddr = freeSymbol.address();
        MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
        FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
        MethodHandle freeHandle = clinker.downcallHandle(freeMemAddr, allocator, freeMethodType, freeFuncDesc);
        freeHandle.invokeExact(allocMemAddr);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) 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 49 with FunctionDescriptor

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

the class PrimitiveTypeTests2 method test_addTwoInts_fromMemAddr_2.

@Test
public void test_addTwoInts_fromMemAddr_2() throws Throwable {
    MethodType mt = MethodType.methodType(int.class, int.class, int.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_INT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Ints").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
    int result = (int) mh.invokeExact(112, 123);
    Assert.assertEquals(result, 235);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 50 with FunctionDescriptor

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

the class PrimitiveTypeTests2 method test_addTwoDoubles_2.

@Test
public void test_addTwoDoubles_2() throws Throwable {
    MethodType mt = MethodType.methodType(double.class, double.class, double.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, C_DOUBLE);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Doubles").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    double result = (double) mh.invokeExact(159.748d, 262.795d);
    Assert.assertEquals(result, 422.543d, 0.001d);
}
Also used : MethodType(java.lang.invoke.MethodType) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)939 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)939 Test (org.testng.annotations.Test)939 MemorySegment (jdk.incubator.foreign.MemorySegment)754 GroupLayout (jdk.incubator.foreign.GroupLayout)675 MethodType (java.lang.invoke.MethodType)605 ResourceScope (jdk.incubator.foreign.ResourceScope)549 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)541 Addressable (jdk.incubator.foreign.Addressable)432 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 VarHandle (java.lang.invoke.VarHandle)318 SequenceLayout (jdk.incubator.foreign.SequenceLayout)240 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)173 MemoryAddress (jdk.incubator.foreign.MemoryAddress)127 VaList (jdk.incubator.foreign.VaList)4