Search in sources :

Example 16 with CCharPointer

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

the class JavaUtilZipSubstitutions method updateByteBuffer.

// Adler32.c-Java_java_util_zip_Adler32_updateByteBuffer(JNIEnv *env, jclass cls, jint adler,
@Substitute
private static int updateByteBuffer(int adler, long addr, int off, int len) {
    CCharPointer bytes = WordFactory.pointer(addr);
    CCharPointer bytesAtOffset = bytes.addressOf(off);
    return (int) ZLib.adler32(WordFactory.unsigned(adler), bytesAtOffset, len).rawValue();
}
Also used : CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) Substitute(com.oracle.svm.core.annotate.Substitute)

Example 17 with CCharPointer

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

the class RealLog method rawString.

private void rawString(char[] value) {
    int length = value.length;
    /*
         * Stack allocation needs an allocation size that is a compile time constant, so we split
         * the string up in multiple chunks and write them separately.
         */
    final int chunkSize = 256;
    final CCharPointer bytes = StackValue.get(chunkSize);
    int chunkOffset = 0;
    while (length > 0) {
        int chunkLength = Math.min(length, chunkSize);
        for (int i = 0; i < chunkLength; i++) {
            char c = value[chunkOffset + i];
            bytes.write(i, (byte) c);
        }
        rawBytes(bytes, WordFactory.unsigned(chunkLength));
        chunkOffset += chunkLength;
        length -= chunkLength;
    }
}
Also used : CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 18 with CCharPointer

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

the class CInterfaceTutorial method javaEntryPoint2.

@CEntryPoint(name = "java_entry_point2")
protected static void javaEntryPoint2(@SuppressWarnings("unused") IsolateThread thread, Substruct1 s1, Substruct2 s2) {
    System.out.println("*** In Java, demonstrating inheritance with @CStruct class");
    CCharPointer tp1 = s1.typePtr();
    CCharPointer tp2 = s2.header().typePtr();
    System.out.println("tp1 = 0x" + Long.toHexString(tp1.rawValue()) + " tp2 = 0x" + Long.toHexString(tp2.rawValue()));
    System.out.println("&s1.header = 0x" + Long.toHexString(s1.header().rawValue()) + " &s2.header = 0x" + Long.toHexString(s2.header().rawValue()));
    System.out.println("s1.f1 = " + s1.f1() + "  s2.f2 = " + s2.f1());
    System.out.println("&s1.f1 = 0x" + Long.toHexString(s1.addressOff1().rawValue()));
    System.out.println("*&s1.f1 = " + s1.addressOff1().read());
    System.out.println("offset_of(s1.f1) = " + s1.offsetOff1());
    System.out.println("s1.header.type = " + s1.type() + "  ((Header) s2).type = " + s2.header().type());
    System.out.println("*** In Java, demonstrating @CFieldOffset");
    System.out.println("offset_of(s1.header) = " + Substruct1.offsetOfHeader());
    System.out.println("offset_of(s2.header) = " + s2.offsetOfHeader());
    System.out.println("offset_of(s2.f1) = " + s2.offsetOff1().rawValue());
    /*
         * Accessing elements of an array of struct. Say that s1 is the first element of subdata_t
         * s[10].
         */
    // ps1 = &s[0]
    Substruct1 ps1 = s1.addressOf(0);
    // ps2 = &s[1]
    Substruct1 ps2 = s1.addressOf(1);
    long s = ps2.rawValue() - ps1.rawValue();
    System.out.print("sizeof(s1) =" + SizeOf.get(Substruct1.class));
    System.out.print(" s1 = 0x" + Long.toHexString(s1.rawValue()));
    System.out.print("  ps1 = 0x" + Long.toHexString(ps1.rawValue()));
    System.out.println("  ps2 = 0x" + Long.toHexString(ps2.rawValue()));
    System.out.println(" ps2 - ps1 " + (s == SizeOf.get(Substruct1.class) ? "=" : "!=") + " sizeof(substruct1)");
}
Also used : CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 19 with CCharPointer

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

the class PolyglotNativeAPI method polyglot_available_languages.

@CEntryPoint(name = "polyglot_available_languages")
public static PolyglotStatus polyglot_available_languages(IsolateThread isolate_thread, PolyglotEnginePointer engine_handle, CCharPointerPointer lang_array) {
    return withHandledErrors(() -> {
        String[] langs = sortedLangs(ObjectHandles.getGlobal().get(engine_handle));
        for (int i = 0; i < langs.length; i++) {
            CCharPointer name = lang_array.read(i);
            CTypeConversion.toCString(langs[i], UTF8_CHARSET, name, WordFactory.unsigned(langs[i].getBytes(UTF8_CHARSET).length));
        }
    });
}
Also used : CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint)

Example 20 with CCharPointer

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

the class PolyglotNativeAPI method handleThrowable.

private static PolyglotStatus handleThrowable(Throwable t) {
    PolyglotStatus errorCode = t instanceof PolyglotNativeAPIError ? ((PolyglotNativeAPIError) t).getCode() : polyglot_generic_failure;
    String message = t.getMessage();
    PolyglotExtendedErrorInfo unmanagedErrorInfo = UnmanagedMemory.malloc(SizeOf.get(PolyglotExtendedErrorInfo.class));
    unmanagedErrorInfo.setErrorCode(errorCode.getCValue());
    CCharPointerHolder holder = CTypeConversion.toCString(message);
    CCharPointer value = holder.get();
    unmanagedErrorInfo.setErrorMessage(value);
    errorInfo.set(new ErrorInfoHolder(unmanagedErrorInfo, holder));
    return errorCode;
}
Also used : PolyglotExtendedErrorInfo(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotExtendedErrorInfo) PolyglotStatus(org.graalvm.polyglot.nativeapi.PolyglotNativeAPITypes.PolyglotStatus) CCharPointerHolder(org.graalvm.nativeimage.c.type.CTypeConversion.CCharPointerHolder) 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