Search in sources :

Example 26 with CCharPointer

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

the class DarwinExecutableName method apply.

/**
 * This implementation of {@link CompilerCommandPlugin#apply(Object[])} does not use the
 * arguments, and returns a String, possibly null.
 */
@Override
public Object apply(Object[] args) {
    /* Find out how long the executable path is. */
    final CIntPointer sizePointer = StackValue.get(SizeOf.get(CIntPointer.class));
    sizePointer.write(0);
    if (DarwinDyld._NSGetExecutablePath(WordFactory.nullPointer(), sizePointer) != -1) {
        VMError.shouldNotReachHere("DarwinExecutableName.getExecutableName: Executable path length is 0?");
    }
    /* Allocate a correctly-sized buffer and ask again. */
    final byte[] byteBuffer = new byte[sizePointer.read()];
    try (PinnedObject pinnedBuffer = PinnedObject.create(byteBuffer)) {
        final CCharPointer bufferPointer = pinnedBuffer.addressOfArrayElement(0);
        if (DarwinDyld._NSGetExecutablePath(bufferPointer, sizePointer) == -1) {
            /* Failure to find executable path. */
            return null;
        }
        final String executableString = CTypeConversion.toJavaString(bufferPointer);
        final String result = realpath(executableString);
        return result;
    }
}
Also used : CIntPointer(org.graalvm.nativeimage.c.type.CIntPointer) PinnedObject(org.graalvm.nativeimage.PinnedObject) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 27 with CCharPointer

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

the class DarwinSystemPropertiesFeature method tmpdirValue.

@Override
protected String tmpdirValue() {
    /* Darwin has a per-user temp dir */
    int buflen = Limits.PATH_MAX();
    CCharPointer tmpPath = StackValue.get(buflen);
    UnsignedWord pathSize = Unistd.confstr(Unistd._CS_DARWIN_USER_TEMP_DIR(), tmpPath, WordFactory.unsigned(buflen));
    if (pathSize.aboveThan(0) && pathSize.belowOrEqual(buflen)) {
        return CTypeConversion.toJavaString(tmpPath);
    } else {
        /*
             * Default as defined in JDK source/jdk/src/solaris/native/java/lang/java_props_md.c
             * line 135.
             */
        return "/var/tmp";
    }
}
Also used : UnsignedWord(org.graalvm.word.UnsignedWord) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 28 with CCharPointer

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

the class JavaMainWrapper method setCRuntimeArgument0.

public static boolean setCRuntimeArgument0(String arg0) {
    boolean arg0truncation = false;
    try (CCharPointerHolder arg0Pin = CTypeConversion.toCString(arg0)) {
        CCharPointer arg0Pointer = arg0Pin.get();
        UnsignedWord arg0Length = SubstrateUtil.strlen(arg0Pointer);
        UnsignedWord origLength = WordFactory.unsigned(getCRuntimeArgumentBlockLength());
        UnsignedWord newArgLength = origLength;
        if (arg0Length.add(1).belowThan(origLength)) {
            newArgLength = arg0Length.add(1);
        }
        arg0truncation = arg0Length.aboveThan(origLength);
        CCharPointer firstArgPos = argv.read(0);
        // Copy the new arg0 to the original argv[0] position
        MemoryUtil.copyConjointMemoryAtomic(WordFactory.pointer(arg0Pointer.rawValue()), WordFactory.pointer(firstArgPos.rawValue()), newArgLength);
        // Zero-out the remaining space
        MemoryUtil.fillToMemoryAtomic((Pointer) WordFactory.unsigned(firstArgPos.rawValue()).add(newArgLength), origLength.subtract(newArgLength), (byte) 0);
    }
    // Let caller know if truncation happened
    return arg0truncation;
}
Also used : UnsignedWord(org.graalvm.word.UnsignedWord) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 29 with CCharPointer

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

the class RealLog method rawBytes.

/**
 * Write a raw java array by copying it first to a stack allocated temporary buffer. Caller must
 * ensure that the offset and length are within bounds.
 */
private void rawBytes(byte[] value, int offset, int length) {
    /*
         * Stack allocation needs an allocation size that is a compile time constant, so we split
         * the byte array up in multiple chunks and write them separately.
         */
    final int chunkSize = 256;
    final CCharPointer bytes = StackValue.get(chunkSize);
    int chunkOffset = offset;
    int inputLength = length;
    while (inputLength > 0) {
        int chunkLength = Math.min(inputLength, chunkSize);
        for (int i = 0; i < chunkLength; i++) {
            byte b = value[chunkOffset + i];
            bytes.write(i, b);
        }
        rawBytes(bytes, WordFactory.unsigned(chunkLength));
        chunkOffset += chunkLength;
        inputLength -= chunkLength;
    }
}
Also used : CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 30 with CCharPointer

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

the class RealLog method number.

private Log number(long value, int radix, boolean signed, int fill, int align) {
    if (radix < 2 || radix > 36) {
        /* Ignore bogus parameter value. */
        return this;
    }
    /* Enough space for 64 digits in binary format, and the '-' for a negative value. */
    final int chunkSize = Long.SIZE + 1;
    CCharPointer bytes = StackValue.get(chunkSize, SizeOf.get(CCharPointer.class));
    int charPos = chunkSize;
    boolean negative = signed && value < 0;
    long curValue;
    if (negative) {
        /*
             * We do not have to worry about the overflow of Long.MIN_VALUE here, since we treat
             * curValue as an unsigned value.
             */
        curValue = -value;
    } else {
        curValue = value;
    }
    while (UnsignedMath.aboveOrEqual(curValue, radix)) {
        charPos--;
        bytes.write(charPos, digit(Long.remainderUnsigned(curValue, radix)));
        curValue = Long.divideUnsigned(curValue, radix);
    }
    charPos--;
    bytes.write(charPos, digit(curValue));
    if (negative) {
        charPos--;
        bytes.write(charPos, (byte) '-');
    }
    int length = chunkSize - charPos;
    if (align == RIGHT_ALIGN) {
        int spaces = fill - length;
        spaces(spaces);
    }
    rawBytes(bytes.addressOf(charPos), WordFactory.unsigned(length));
    if (align == LEFT_ALIGN) {
        int spaces = fill - length;
        spaces(spaces);
    }
    return this;
}
Also used : CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Aggregations

CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)47 Substitute (com.oracle.svm.core.annotate.Substitute)18 CCharPointerHolder (org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder)15 PinnedObject (org.graalvm.nativeimage.PinnedObject)8 IOException (java.io.IOException)7 NetinetIn (com.oracle.svm.core.posix.headers.NetinetIn)6 CIntPointer (org.graalvm.nativeimage.c.type.CIntPointer)6 SocketException (java.net.SocketException)5 SignedWord (org.graalvm.word.SignedWord)5 Inet6Address (java.net.Inet6Address)4 InetAddress (java.net.InetAddress)4 UnsignedWord (org.graalvm.word.UnsignedWord)4 Uninterruptible (com.oracle.svm.core.annotate.Uninterruptible)3 Socket (com.oracle.svm.core.posix.headers.Socket)3 InterruptedIOException (java.io.InterruptedIOException)3 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)3 CCharPointerPointer (org.graalvm.nativeimage.c.type.CCharPointerPointer)3 PosixOSInterface.lastErrorString (com.oracle.svm.core.posix.PosixOSInterface.lastErrorString)2 DIR (com.oracle.svm.core.posix.headers.Dirent.DIR)2 Dirent.dirent (com.oracle.svm.core.posix.headers.Dirent.dirent)2