Search in sources :

Example 1 with CCharPointer

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

the class JavaUtilZipSubstitutions method update.

// CRC32.c-Java_java_util_zip_CRC32_update(JNIEnv *env, jclass cls, jint crc, jint b)
@Substitute
private static int update(int crc, int b) {
    // Only the low eight bits of the argument b should be used
    CCharPointer bytes = StackValue.get(SizeOf.get(CCharPointer.class));
    bytes.write((byte) b);
    return (int) ZLib.crc32(WordFactory.unsigned(crc), bytes, 1).rawValue();
}
Also used : CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 2 with CCharPointer

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

the class PosixExecutableName method realpath.

/**
 * A platform-independent method to canonicalize a path.
 */
protected String realpath(String path) {
    /*
         * Find the real path to the executable. realpath(3) mallocs a result buffer and returns a
         * pointer to it, so I have to free it.
         */
    try (CCharPointerHolder pathHolder = CTypeConversion.toCString(path)) {
        final CCharPointer realpathPointer = Stdlib.realpath(pathHolder.get(), WordFactory.nullPointer());
        if (realpathPointer.isNull()) {
            /* Failure to find a real path. */
            return null;
        } else {
            /* Success */
            final String result = CTypeConversion.toJavaString(realpathPointer);
            LibC.free(realpathPointer);
            return result;
        }
    }
}
Also used : CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 3 with CCharPointer

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

the class Target_com_sun_security_auth_module_UnixSystem method getUnixInfo.

@Substitute
void getUnixInfo() {
    int realUid = Unistd.getuid();
    Word pwsize = WordFactory.signed(Unistd.sysconf(Unistd._SC_GETPW_R_SIZE_MAX()));
    if (pwsize.lessThan(0)) {
        // indicates no definitive bound: try different sizes
        pwsize = WordFactory.signed(1024);
    }
    CCharPointer pwbuf = LibC.malloc(pwsize);
    try {
        passwd pwent = StackValue.get(SizeOf.get(passwd.class));
        passwdPointer p = StackValue.get(SizeOf.get(passwdPointer.class));
        int result;
        do {
            if (pwbuf.isNull()) {
                throw new OutOfMemoryError("Native heap");
            }
            p.write(WordFactory.nullPointer());
            result = Pwd.getpwuid_r(realUid, pwent, pwbuf, pwsize, p);
            if (result == Errno.ERANGE()) {
                pwsize = pwsize.add(pwsize);
                pwbuf = LibC.realloc(pwbuf, pwsize);
            } else if (result < 0 && result != Errno.EINTR()) {
                throw new RuntimeException("getpwuid_r error: " + Errno.errno());
            }
        } while (result != 0);
        this.uid = pwent.pw_uid();
        this.gid = pwent.pw_gid();
        this.username = CTypeConversion.toJavaString(pwent.pw_name());
    } finally {
        LibC.free(pwbuf);
    }
    int ngroups = Unistd.getgroups(0, WordFactory.nullPointer());
    int[] groupIds = new int[ngroups];
    try (PinnedObject pinnedGroupIds = PinnedObject.create(groupIds)) {
        ngroups = Unistd.getgroups(groupIds.length, pinnedGroupIds.addressOfArrayElement(0));
    }
    this.groups = new long[ngroups];
    for (int i = 0; i < this.groups.length; i++) {
        this.groups[i] = groupIds[i];
    }
}
Also used : Word(org.graalvm.compiler.word.Word) Pwd.passwd(com.oracle.svm.core.posix.headers.Pwd.passwd) PinnedObject(org.graalvm.nativeimage.PinnedObject) Pwd.passwdPointer(com.oracle.svm.core.posix.headers.Pwd.passwdPointer) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 4 with CCharPointer

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

the class PosixJavaIOSubstitutions method getSpace.

@Substitute
public long getSpace(File f, int t) {
    statvfs statvfs = StackValue.get(SizeOf.get(statvfs.class));
    LibC.memset(statvfs, WordFactory.zero(), WordFactory.unsigned(SizeOf.get(statvfs.class)));
    try (CCharPointerHolder pathPin = CTypeConversion.toCString(f.getPath())) {
        CCharPointer pathPtr = pathPin.get();
        if (statvfs(pathPtr, statvfs) == 0) {
            final long frsize = statvfs.f_frsize();
            if (t == Target_java_io_FileSystem.SPACE_TOTAL) {
                return frsize * statvfs.f_blocks();
            } else if (t == Target_java_io_FileSystem.SPACE_FREE) {
                return frsize * statvfs.f_bfree();
            } else if (t == Target_java_io_FileSystem.SPACE_USABLE) {
                return frsize * statvfs.f_bavail();
            } else {
                throw VMError.shouldNotReachHere("illegal space mode");
            }
        }
    }
    return 0L;
}
Also used : Statvfs.statvfs(com.oracle.svm.core.posix.headers.Statvfs.statvfs) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 5 with CCharPointer

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

the class PosixJavaIOSubstitutions method createFileExclusively.

@Substitute
public boolean createFileExclusively(String path) throws IOException {
    int fd;
    if (path.equals("/")) {
        return false;
    } else {
        try (CCharPointerHolder pathPin = CTypeConversion.toCString(PosixUtils.removeTrailingSlashes(path))) {
            CCharPointer pathPtr = pathPin.get();
            fd = open(pathPtr, O_RDWR() | O_CREAT(), 0666);
        }
    }
    if (fd < 0) {
        if (fd != EEXIST()) {
            throw new IOException(lastErrorString(path));
        }
    } else {
        close(fd);
        return true;
    }
    return false;
}
Also used : IOException(java.io.IOException) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

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