Search in sources :

Example 6 with WordPointer

use of org.graalvm.nativeimage.c.type.WordPointer in project graal by oracle.

the class Util_jni method socketSetOption.

/* Do not re-format commented-out code: @formatter:off */
// 886 /*
// 887  * Class:     java_net_PlainSocketImpl
// 888  * Method:    socketSetOption
// 889  * Signature: (IZLjava/lang/Object;)V
// 890  */
// 891 JNIEXPORT void JNICALL
// 892 Java_java_net_PlainSocketImpl_socketSetOption(JNIEnv *env, jobject this,
// 893                                               jint cmd, jboolean on,
// 894                                               jobject value) {
@Substitute
void socketSetOption(int cmd, boolean on, Object value) throws SocketException {
    // 895     int fd;
    int fd;
    // 896     int level, optname, optlen;
    CIntPointer level_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
    CIntPointer optname_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
    int optlen;
    /* Translated as a WordPointer to the larger of the arms. */
    // 897     union {
    // 898         int i;
    // 899         struct linger ling;
    // 900     } optval;
    /* Guess which arm of the union is larger. Trust, but verify. */
    final int sizeof_optval = SizeOf.get(Socket.linger.class);
    VMError.guarantee(SizeOf.get(CIntPointer.class) <= sizeof_optval, "sizeof(int) <= sizeof(union optval)");
    VMError.guarantee(SizeOf.get(Socket.linger.class) <= sizeof_optval, "sizeof(struct linger) <= sizeof(union optval)");
    WordPointer optval_Pointer = StackValue.get(sizeof_optval);
    // 901
    // 902     /*
    // 903      * Check that socket hasn't been closed
    // 904      */
    // 905     fd = getFD(env, this);
    fd = Util_java_io_FileDescriptor.getFD(Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).fd);
    // 906     if (fd < 0) {
    if (fd < 0) {
        // 909         return;
        throw new SocketException("Socket closed");
    }
    // 915     if (cmd == java_net_SocketOptions_SO_TIMEOUT) {
    if (cmd == SocketOptions.SO_TIMEOUT) {
        // 916         return;
        return;
    }
    // 923     if (NET_MapSocketOption(cmd, &level, &optname)) {
    if (CTypeConversion.toBoolean(JavaNetNetUtilMD.NET_MapSocketOption(cmd, level_Pointer, optname_Pointer))) {
        // 925         return;
        throw new SocketException("Invalid option");
    }
    // 928     switch (cmd) {
    switch(cmd) {
        // 932         case java_net_SocketOptions_IP_TOS :
        case SocketOptions.SO_SNDBUF:
        case SocketOptions.SO_RCVBUF:
        case SocketOptions.SO_LINGER:
        case SocketOptions.IP_TOS:
            {
                /* Accessing values of java.lang.Integer the easy way. */
                Integer valueInteger = (Integer) value;
                // 942                 if (cmd == java_net_SocketOptions_SO_LINGER) {
                if (cmd == SocketOptions.SO_LINGER) {
                    // 943                     if (on) {
                    if (on) {
                        // 944                         optval.ling.l_onoff = 1;
                        ((Socket.linger) optval_Pointer).set_l_onoff(1);
                        // 945                         optval.ling.l_linger = (*env)->GetIntField(env, value, fid);
                        ((Socket.linger) optval_Pointer).set_l_linger(valueInteger.intValue());
                    } else {
                        // 947                         optval.ling.l_onoff = 0;
                        ((Socket.linger) optval_Pointer).set_l_onoff(0);
                        // 948                         optval.ling.l_linger = 0;
                        ((Socket.linger) optval_Pointer).set_l_linger(0);
                    }
                    // 950                     optlen = sizeof(optval.ling);
                    optlen = SizeOf.get(Socket.linger.class);
                    /* Copy to optval. */
                    LibC.memcpy(optval_Pointer, optval_Pointer, WordFactory.unsigned(optlen));
                } else {
                    // 952                     optval.i = (*env)->GetIntField(env, value, fid);
                    ((CIntPointer) optval_Pointer).write(valueInteger.intValue());
                    // 953                     optlen = sizeof(optval.i);
                    optlen = SizeOf.get(CIntPointer.class);
                    /* Copy to optval. */
                    LibC.memcpy(optval_Pointer, optval_Pointer, WordFactory.unsigned(optlen));
                }
                // 956                 break;
                break;
            }
        // 960         default :
        default:
            // 961             optval.i = (on ? 1 : 0);
            ((CIntPointer) optval_Pointer).write(on ? 1 : 0);
            // 962             optlen = sizeof(optval.i);
            optlen = SizeOf.get(CIntPointer.class);
            /* Copy to optval. */
            LibC.memcpy(optval_Pointer, optval_Pointer, WordFactory.unsigned(optlen));
    }
    // 966     if (NET_SetSockOpt(fd, level, optname, (const void *)&optval, optlen) < 0) {
    if (JavaNetNetUtilMD.NET_SetSockOpt(fd, level_Pointer.read(), optname_Pointer.read(), optval_Pointer, optlen) < 0) {
        // 968         if (errno == EINVAL) {
        if (Errno.errno() == Errno.EINVAL()) {
            // 974             return;
            throw new SocketException("Invalid option or socket reset by remote peer");
        }
        // 978                                       "Error setting socket option");
        throw new SocketException("Error setting socket option");
    }
}
Also used : SocketException(java.net.SocketException) CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) WordPointer(org.graalvm.nativeimage.c.type.WordPointer) ServerSocket(java.net.ServerSocket) Socket(com.oracle.svm.core.posix.headers.Socket) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 7 with WordPointer

use of org.graalvm.nativeimage.c.type.WordPointer in project graal by oracle.

the class NativeClosure method call.

private Object call(WordPointer argPointers, ByteBuffer retBuffer) {
    Target_com_oracle_truffle_nfi_impl_LibFFIType[] argTypes = signature.getArgTypes();
    int length = argTypes.length - skippedArgCount;
    if (retBuffer != null) {
        length++;
    }
    int argIdx = 0;
    Object[] args = new Object[length];
    for (int i = 0; i < argTypes.length; i++) {
        Object type = argTypes[i];
        if (Target_com_oracle_truffle_nfi_impl_LibFFIType_StringType.class.isInstance(type)) {
            CCharPointerPointer argPtr = argPointers.read(i);
            args[argIdx++] = TruffleNFISupport.utf8ToJavaString(argPtr.read());
        } else if (Target_com_oracle_truffle_nfi_impl_LibFFIType_ObjectType.class.isInstance(type)) {
            WordPointer argPtr = argPointers.read(i);
            args[argIdx++] = ImageSingletons.lookup(TruffleNFISupport.class).resolveHandle(argPtr.read());
        } else if (Target_com_oracle_truffle_nfi_impl_LibFFIType_EnvType.class.isInstance(type)) {
        // skip
        } else {
            WordPointer argPtr = argPointers.read(i);
            args[argIdx++] = SubstrateUtil.wrapAsByteBuffer(argPtr, argTypes[i].size);
        }
    }
    if (retBuffer != null) {
        args[argIdx] = retBuffer;
    }
    return callTarget.call(args);
}
Also used : WordPointer(org.graalvm.nativeimage.c.type.WordPointer) PinnedObject(org.graalvm.nativeimage.PinnedObject) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CCharPointerPointer(org.graalvm.nativeimage.c.type.CCharPointerPointer)

Example 8 with WordPointer

use of org.graalvm.nativeimage.c.type.WordPointer in project graal by oracle.

the class NativeClosure method prepareClosure.

static Target_com_oracle_truffle_nfi_impl_ClosureNativePointer prepareClosure(Target_com_oracle_truffle_nfi_impl_NFIContext ctx, Target_com_oracle_truffle_nfi_impl_LibFFISignature signature, CallTarget callTarget, ffi_closure_callback callback) {
    NativeClosure closure = new NativeClosure(callTarget, signature);
    NativeClosureHandle handle = ImageSingletons.lookup(TruffleNFISupport.class).createClosureHandle(closure);
    WordPointer codePtr = StackValue.get(SizeOf.get(WordPointer.class));
    ClosureData data = ffi_closure_alloc(SizeOf.unsigned(ClosureData.class), codePtr);
    data.setNativeClosureHandle(handle);
    data.setIsolate(CEntryPointContext.getCurrentIsolate());
    PointerBase code = codePtr.read();
    LibFFI.ffi_prep_closure_loc(data.ffiClosure(), WordFactory.pointer(signature.cif), callback, data, code);
    return ctx.createClosureNativePointer(data.rawValue(), code.rawValue(), callTarget, signature);
}
Also used : NativeClosureHandle(com.oracle.svm.truffle.nfi.LibFFI.NativeClosureHandle) WordPointer(org.graalvm.nativeimage.c.type.WordPointer) PointerBase(org.graalvm.word.PointerBase) ClosureData(com.oracle.svm.truffle.nfi.LibFFI.ClosureData)

Aggregations

WordPointer (org.graalvm.nativeimage.c.type.WordPointer)8 Substitute (com.oracle.svm.core.annotate.Substitute)2 Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)2 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)2 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)2 CEntryPointOptions (com.oracle.svm.core.c.function.CEntryPointOptions)1 Socket (com.oracle.svm.core.posix.headers.Socket)1 SubstrateForeignCallTarget (com.oracle.svm.core.snippets.SubstrateForeignCallTarget)1 Safepoint (com.oracle.svm.core.thread.Safepoint)1 ClosureData (com.oracle.svm.truffle.nfi.LibFFI.ClosureData)1 NativeClosureHandle (com.oracle.svm.truffle.nfi.LibFFI.NativeClosureHandle)1 NativeTruffleContext (com.oracle.svm.truffle.nfi.NativeAPI.NativeTruffleContext)1 LibFFI.ffi_cif (com.oracle.svm.truffle.nfi.libffi.LibFFI.ffi_cif)1 ServerSocket (java.net.ServerSocket)1 SocketException (java.net.SocketException)1 Word (org.graalvm.compiler.word.Word)1 PinnedObject (org.graalvm.nativeimage.PinnedObject)1 CCharPointerPointer (org.graalvm.nativeimage.c.type.CCharPointerPointer)1 CLongPointer (org.graalvm.nativeimage.c.type.CLongPointer)1 Pointer (org.graalvm.word.Pointer)1