Search in sources :

Example 11 with Addressable

use of jdk.incubator.foreign.Addressable 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 12 with Addressable

use of jdk.incubator.foreign.Addressable 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 13 with Addressable

use of jdk.incubator.foreign.Addressable 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)

Example 14 with Addressable

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

the class PrimitiveTypeTests2 method test_addTwoFloats_2.

@Test
public void test_addTwoFloats_2() throws Throwable {
    MethodType mt = MethodType.methodType(float.class, float.class, float.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, C_FLOAT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Floats").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
    float result = (float) mh.invokeExact(5.74f, 6.79f);
    Assert.assertEquals(result, 12.53f, 0.01f);
}
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)

Example 15 with Addressable

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

the class PrimitiveTypeTests2 method test_generateNewChar_fromMemAddr_2.

@Test
public void test_generateNewChar_fromMemAddr_2() throws Throwable {
    MethodType mt = MethodType.methodType(char.class, char.class, char.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT);
    Addressable functionSymbol = nativeLibLookup.lookup("createNewCharFrom2Chars").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, allocator, mt, fd);
    char result = (char) mh.invokeExact('B', 'D');
    Assert.assertEquals(result, 'C');
}
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)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)432 MethodType (java.lang.invoke.MethodType)432 Addressable (jdk.incubator.foreign.Addressable)432 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)432 Test (org.testng.annotations.Test)432 MemorySegment (jdk.incubator.foreign.MemorySegment)314 ResourceScope (jdk.incubator.foreign.ResourceScope)275 GroupLayout (jdk.incubator.foreign.GroupLayout)270 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)270 VarHandle (java.lang.invoke.VarHandle)128 SequenceLayout (jdk.incubator.foreign.SequenceLayout)96 MemoryAddress (jdk.incubator.foreign.MemoryAddress)63