Search in sources :

Example 26 with MemoryAddress

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

the class PrimitiveTypeTests method test_memoryAllocFreeFromDefaultLib_fromMemAddr.

@Test
public void test_memoryAllocFreeFromDefaultLib_fromMemAddr() throws Throwable {
    Symbol allocSymbol = defaultLib.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, allocMethodType, allocFuncDesc);
    MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
    long allocMemAddrValue = allocMemAddr.toRawLongValue();
    MemorySegment memSeg = MemorySegment.ofNativeRestricted();
    MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 15);
    Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 15);
    Symbol freeSymbol = defaultLib.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, freeMethodType, freeFuncDesc);
    freeHandle.invokeExact(allocMemAddr);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 27 with MemoryAddress

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

the class PrimitiveTypeTests method test_strlenFromDefaultLibWithMemAddr_fromMemAddr.

@Test
public void test_strlenFromDefaultLibWithMemAddr_fromMemAddr() throws Throwable {
    Symbol strlenSymbol = defaultLib.lookup("strlen").get();
    MemoryAddress memAddr = strlenSymbol.address();
    MethodType mt = MethodType.methodType(long.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_POINTER);
    MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
    MemorySegment funcMemSegment = CLinker.toCString("JEP389 DOWNCALL TEST SUITES");
    long strLength = (long) mh.invokeExact(funcMemSegment.address());
    Assert.assertEquals(strLength, 27);
    FunctionDescriptor fd2 = FunctionDescriptor.of(longLayout.withName("long"), C_POINTER.withName("pointer"));
    mh = clinker.downcallHandle(memAddr, mt, fd2);
    strLength = (long) mh.invokeExact(funcMemSegment.address());
    Assert.assertEquals(strLength, 27);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 28 with MemoryAddress

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

the class PrimitiveTypeTests method test_memoryAllocFreeFromDefaultLib.

@Test
public void test_memoryAllocFreeFromDefaultLib() throws Throwable {
    Symbol allocSymbol = defaultLib.lookup("malloc").get();
    MethodType allocMethodType = MethodType.methodType(MemoryAddress.class, long.class);
    FunctionDescriptor allocFuncDesc = FunctionDescriptor.of(C_POINTER, longLayout);
    MethodHandle allocHandle = clinker.downcallHandle(allocSymbol, allocMethodType, allocFuncDesc);
    MemoryAddress allocMemAddr = (MemoryAddress) allocHandle.invokeExact(10L);
    long allocMemAddrValue = allocMemAddr.toRawLongValue();
    MemorySegment memSeg = MemorySegment.ofNativeRestricted();
    MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 15);
    Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 15);
    Symbol freeSymbol = defaultLib.lookup("free").get();
    MethodType freeMethodType = MethodType.methodType(void.class, MemoryAddress.class);
    FunctionDescriptor freeFuncDesc = FunctionDescriptor.ofVoid(C_POINTER);
    MethodHandle freeHandle = clinker.downcallHandle(freeSymbol, freeMethodType, freeFuncDesc);
    freeHandle.invokeExact(allocMemAddr);
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 29 with MemoryAddress

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

the class PrimitiveTypeTests method test_addTwoIntsReturnVoid_fromMemAddr.

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

Example 30 with MemoryAddress

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

the class PrimitiveTypeTests method test_memoryAllocFreeFromCLinkerMethod.

@Test
public void test_memoryAllocFreeFromCLinkerMethod() throws Throwable {
    MemoryAddress allocMemAddr = CLinker.allocateMemoryRestricted(10L);
    long allocMemAddrValue = allocMemAddr.toRawLongValue();
    MemorySegment memSeg = MemorySegment.ofNativeRestricted();
    MemoryAccess.setIntAtOffset(memSeg, allocMemAddrValue, 49);
    Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, allocMemAddrValue), 49);
    CLinker.freeMemoryRestricted(allocMemAddr);
}
Also used : MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) Test(org.testng.annotations.Test)

Aggregations

MemoryAddress (jdk.incubator.foreign.MemoryAddress)136 Test (org.testng.annotations.Test)131 MethodHandle (java.lang.invoke.MethodHandle)127 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)127 MemorySegment (jdk.incubator.foreign.MemorySegment)102 MethodType (java.lang.invoke.MethodType)93 GroupLayout (jdk.incubator.foreign.GroupLayout)80 VarHandle (java.lang.invoke.VarHandle)78 ResourceScope (jdk.incubator.foreign.ResourceScope)65 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)64 Addressable (jdk.incubator.foreign.Addressable)63 NativeSymbol (jdk.incubator.foreign.NativeSymbol)34 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)30 AbstractEndpoint (org.apache.tomcat.util.net.AbstractEndpoint)4 X509Certificate (java.security.cert.X509Certificate)3 CertificateException (java.security.cert.CertificateException)2 ArrayList (java.util.ArrayList)2 SSLException (javax.net.ssl.SSLException)2 PrivateKey (java.security.PrivateKey)1 X509KeyManager (javax.net.ssl.X509KeyManager)1