Search in sources :

Example 46 with MemoryAddress

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

the class StructTests2 method test_add2IntStructs_returnStructPointer_2.

@Test
public void test_add2IntStructs_returnStructPointer_2() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), C_INT.withName("elem2"));
    VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle intHandle2 = 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);
    Addressable functionSymbol = nativeLibLookup.lookup("add2IntStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        intHandle1.set(structSegmt1, 11223344);
        intHandle2.set(structSegmt1, 55667788);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        intHandle1.set(structSegmt2, 99001122);
        intHandle2.set(structSegmt2, 33445566);
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, structSegmt1.address(), structSegmt2);
        MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
        Assert.assertEquals(intHandle1.get(resultSegmt), 110224466);
        Assert.assertEquals(intHandle2.get(resultSegmt), 89113354);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) 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 47 with MemoryAddress

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

the class StructTests1 method test_addCharFromPointerAndCharsFromStruct_returnCharPointer_1.

@Test
public void test_addCharFromPointerAndCharsFromStruct_returnCharPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
    VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addCharFromPointerAndCharsFromStruct_returnCharPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment charSegmt = allocator.allocate(C_SHORT);
        MemoryAccess.setChar(charSegmt, 'D');
        MemorySegment structSegmt = allocator.allocate(structLayout);
        charHandle1.set(structSegmt, 'E');
        charHandle2.set(structSegmt, 'F');
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(charSegmt.address(), structSegmt);
        MemorySegment resultSegmt = resultAddr.asSegment(C_SHORT.byteSize(), scope);
        VarHandle charHandle = MemoryHandles.varHandle(char.class, ByteOrder.nativeOrder());
        char result = (char) charHandle.get(resultSegmt, 0);
        Assert.assertEquals(result, 'M');
        Assert.assertEquals(resultSegmt.address().toRawLongValue(), charSegmt.address().toRawLongValue());
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) 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 48 with MemoryAddress

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

the class PrimitiveTypeTests1 method test_addTwoShorts_fromMemAddr_1.

@Test
public void test_addTwoShorts_fromMemAddr_1() throws Throwable {
    MethodType mt = MethodType.methodType(short.class, short.class, short.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_SHORT);
    Addressable functionSymbol = nativeLibLookup.lookup("add2Shorts").get();
    MemoryAddress memAddr = functionSymbol.address();
    MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
    short result = (short) mh.invokeExact((short) 24, (short) 32);
    Assert.assertEquals(result, (short) 56);
}
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 49 with MemoryAddress

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

the class PrimitiveTypeTests1 method test_printfFromDefaultLibWithMemAddr_fromMemAddr_1.

@Test
public void test_printfFromDefaultLibWithMemAddr_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 functionSymbol = defaultLibLookup.lookup("printf").get();
        MemoryAddress memAddr = functionSymbol.address();
        MethodType mt = MethodType.methodType(int.class, MemoryAddress.class, int.class, int.class, int.class);
        FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_POINTER, C_INT, C_INT, C_INT);
        MethodHandle mh = clinker.downcallHandle(memAddr, mt, fd);
        MemorySegment formatMemSegment = CLinker.toCString("\n%d + %d = %d\n", resourceScope);
        mh.invoke(formatMemSegment.address(), 15, 27, 42);
    }
}
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 50 with MemoryAddress

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

the class PrimitiveTypeTests1 method test_addTwoIntsReturnVoid_fromMemAddr_1.

@Test
public void test_addTwoIntsReturnVoid_fromMemAddr_1() 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, 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)

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