Search in sources :

Example 91 with MemoryAddress

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

the class StructTests1 method test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_1.

@Test
public void test_addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN, JAVA_BOOLEAN);
    FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addBoolFromPointerAndBoolsFromStructWithXor_returnBoolPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        MemorySegment boolSegmt = MemorySegment.allocateNative(JAVA_BOOLEAN, scope);
        boolSegmt.set(JAVA_BOOLEAN, 0, false);
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_BOOLEAN, 0, false);
        structSegmt.set(JAVA_BOOLEAN, 1, true);
        MemoryAddress resultAddr = (MemoryAddress) mh.invoke(boolSegmt, structSegmt);
        Assert.assertEquals(resultAddr.get(JAVA_BOOLEAN, 0), true);
        Assert.assertEquals(resultAddr.toRawLongValue(), boolSegmt.address().toRawLongValue());
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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)

Example 92 with MemoryAddress

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

the class StructTests1 method test_add2ByteStructs_returnStructPointer_1.

@Test
public void test_add2ByteStructs_returnStructPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"));
    VarHandle byteHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle byteHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2ByteStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        byteHandle1.set(structSegmt1, (byte) 25);
        byteHandle2.set(structSegmt1, (byte) 11);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        byteHandle1.set(structSegmt2, (byte) 24);
        byteHandle2.set(structSegmt2, (byte) 13);
        MemoryAddress resultAddr = (MemoryAddress) mh.invoke(structSegmt1, structSegmt2);
        Assert.assertEquals(resultAddr.get(JAVA_BYTE, 0), 49);
        Assert.assertEquals(resultAddr.get(JAVA_BYTE, 1), 24);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) 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)

Example 93 with MemoryAddress

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

the class PrimitiveTypeTests1 method test_memoryAllocFreeFromDefaultLib_fromMemAddr_1.

@Test
public void test_memoryAllocFreeFromDefaultLib_fromMemAddr_1() 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, 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, 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 94 with MemoryAddress

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

the class PrimitiveTypeTests1 method test_addTwoBytes_fromMemAddr_1.

@Test
public void test_addTwoBytes_fromMemAddr_1() throws Throwable {
    MethodType mt = MethodType.methodType(byte.class, byte.class, byte.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, C_CHAR);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Bytes").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
    byte result = (byte) mh.invokeExact((byte) 6, (byte) 3);
    Assert.assertEquals(result, (byte) 9);
}
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 95 with MemoryAddress

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

the class PrimitiveTypeTests1 method test_memoryAllocFreeFromDefaultLib_1.

@Test
public void test_memoryAllocFreeFromDefaultLib_1() 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();
        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);
        MemorySegment memSeg = allocMemAddr.asSegment(10L, resourceScope);
        MemoryAccess.setIntAtOffset(memSeg, 0, 15);
        Assert.assertEquals(MemoryAccess.getIntAtOffset(memSeg, 0), 15);
        Addressable freeSymbol = defaultLibLookup.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) 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)

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