Search in sources :

Example 1 with JByteArray

use of org.graalvm.jniutils.JNI.JByteArray in project graal by oracle.

the class TruffleToLibGraalEntryPoints method getVersionProperties.

@TruffleToLibGraal(GetVersionProperties)
@CEntryPoint(name = "Java_org_graalvm_compiler_truffle_runtime_hotspot_libgraal_TruffleToLibGraalCalls_getVersionProperties")
@SuppressWarnings({ "unused", "try" })
public static JByteArray getVersionProperties(JNIEnv env, JClass hsClazz, @CEntryPoint.IsolateThreadContext long isolateThreadId) {
    JNIMethodScope scope = LibGraalUtil.openScope(TruffleToLibGraalEntryPoints.class, GetVersionProperties, env);
    try (JNIMethodScope s = scope) {
        Map<String, Object> versionProperties = new HashMap<>();
        for (Map.Entry<Object, Object> e : DebugContext.addVersionProperties(null).entrySet()) {
            Object key = e.getKey();
            Object value = e.getValue();
            assert key instanceof String;
            assert value instanceof String;
            versionProperties.put((String) key, value);
        }
        byte[] serializedProperties = OptionsEncoder.encode(versionProperties);
        JByteArray hsSerializedOptions = NewByteArray(env, serializedProperties.length);
        CCharPointer cdata = GetByteArrayElements(env, hsSerializedOptions, WordFactory.nullPointer());
        CTypeConversion.asByteBuffer(cdata, serializedProperties.length).put(serializedProperties);
        ReleaseByteArrayElements(env, hsSerializedOptions, cdata, JArray.MODE_WRITE_RELEASE);
        scope.setObjectResult(hsSerializedOptions);
    } catch (Throwable t) {
        JNIExceptionWrapper.throwInHotSpot(env, t);
        scope.setObjectResult(WordFactory.nullPointer());
    }
    return scope.getObjectResult();
}
Also used : JByteArray(org.graalvm.jniutils.JNI.JByteArray) HashMap(java.util.HashMap) JNIMethodScope(org.graalvm.jniutils.JNIMethodScope) HSObject(org.graalvm.jniutils.HSObject) JObject(org.graalvm.jniutils.JNI.JObject) JNIUtil.createString(org.graalvm.jniutils.JNIUtil.createString) JString(org.graalvm.jniutils.JNI.JString) GetSuppliedString(org.graalvm.compiler.truffle.common.hotspot.libgraal.TruffleToLibGraal.Id.GetSuppliedString) JNIUtil.createHSString(org.graalvm.jniutils.JNIUtil.createHSString) Map(java.util.Map) HashMap(java.util.HashMap) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer) CEntryPoint(org.graalvm.nativeimage.c.function.CEntryPoint) TruffleToLibGraal(org.graalvm.compiler.truffle.common.hotspot.libgraal.TruffleToLibGraal)

Example 2 with JByteArray

use of org.graalvm.jniutils.JNI.JByteArray in project graal by oracle.

the class JNINativeMarshallerAdapter method unmarshall.

@Override
public T unmarshall(JNIEnv env, JObject jObject) {
    if (jObject.isNull()) {
        return null;
    }
    int bufSize = BUFFER_SIZE;
    CCharPointer staticBuffer = StackValue.get(bufSize);
    int len = JNIUtil.GetArrayLength(env, (JByteArray) jObject);
    CCharPointer useBuffer;
    if (len < bufSize) {
        useBuffer = staticBuffer;
    } else {
        useBuffer = UnmanagedMemory.malloc(len);
    }
    try {
        JNIUtil.GetByteArrayRegion(env, ((JByteArray) jObject), 0, len, useBuffer);
        try (BinaryInput in = BinaryInput.create(useBuffer, len)) {
            return binaryMarshaller.read(in);
        } catch (IOException e) {
            throw new AssertionError(e.getMessage(), e);
        }
    } finally {
        if (useBuffer != staticBuffer) {
            UnmanagedMemory.free(useBuffer);
        }
    }
}
Also used : JByteArray(org.graalvm.jniutils.JNI.JByteArray) IOException(java.io.IOException) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Example 3 with JByteArray

use of org.graalvm.jniutils.JNI.JByteArray in project graal by oracle.

the class JNIUtil method createHSArray.

public static JByteArray createHSArray(JNIEnv jniEnv, byte[] a) {
    if (a == null) {
        return WordFactory.nullPointer();
    }
    JByteArray array = NewByteArray(jniEnv, a.length);
    arrayCopy(jniEnv, a, 0, array, 0, a.length);
    return array;
}
Also used : JByteArray(org.graalvm.jniutils.JNI.JByteArray)

Example 4 with JByteArray

use of org.graalvm.jniutils.JNI.JByteArray in project graal by oracle.

the class JNINativeMarshallerAdapter method marshall.

@Override
public JObject marshall(JNIEnv env, T object) {
    if (object == null) {
        return WordFactory.nullPointer();
    }
    int bufSize = BUFFER_SIZE;
    CCharPointer buffer = StackValue.get(bufSize);
    try (CCharPointerBinaryOutput out = BinaryOutput.create(buffer, bufSize, false)) {
        binaryMarshaller.write(out, object);
        int len = out.getPosition();
        JByteArray binaryData = JNIUtil.NewByteArray(env, len);
        JNIUtil.SetByteArrayRegion(env, binaryData, 0, len, out.getAddress());
        return binaryData;
    } catch (IOException e) {
        throw new AssertionError(e.getMessage(), e);
    }
}
Also used : JByteArray(org.graalvm.jniutils.JNI.JByteArray) CCharPointerBinaryOutput(org.graalvm.nativebridge.BinaryOutput.CCharPointerBinaryOutput) IOException(java.io.IOException) CCharPointer(org.graalvm.nativeimage.c.type.CCharPointer)

Aggregations

JByteArray (org.graalvm.jniutils.JNI.JByteArray)4 CCharPointer (org.graalvm.nativeimage.c.type.CCharPointer)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TruffleToLibGraal (org.graalvm.compiler.truffle.common.hotspot.libgraal.TruffleToLibGraal)1 GetSuppliedString (org.graalvm.compiler.truffle.common.hotspot.libgraal.TruffleToLibGraal.Id.GetSuppliedString)1 HSObject (org.graalvm.jniutils.HSObject)1 JObject (org.graalvm.jniutils.JNI.JObject)1 JString (org.graalvm.jniutils.JNI.JString)1 JNIMethodScope (org.graalvm.jniutils.JNIMethodScope)1 JNIUtil.createHSString (org.graalvm.jniutils.JNIUtil.createHSString)1 JNIUtil.createString (org.graalvm.jniutils.JNIUtil.createString)1 CCharPointerBinaryOutput (org.graalvm.nativebridge.BinaryOutput.CCharPointerBinaryOutput)1 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)1