Search in sources :

Example 21 with Address

use of org.vmmagic.unboxed.Address in project JikesRVM by JikesRVM.

the class JNIFunctions method GetStringCritical.

/**
 * GetStringCritical:
 * Like GetStringChars and ReleaseStringChars, but in some VM environments
 * the VM may be able to avoid making a copy.   Native code must not issue
 * arbitrary JNI calls and must not cause the current thread to block.<p>
 *
 * NOTE: Our interpretation of the JNI specification is that callers cannot
 * expect that changes in the array for the String are propagated back. Our
 * implementation assumes that the String will not be changed.
 *
 * @param env A JREF index for the JNI environment object
 * @param strJREF a JREF index for the string in Java
 * @param isCopyAddress address of isCopy jboolean (an int)
 * @return The address of the backing array; address zero (null) on error, and the jboolean pointed to by isCopyAddress is set to false, indicating that this is not a copy.
 */
private static Address GetStringCritical(JNIEnvironment env, int strJREF, Address isCopyAddress) {
    if (traceJNI)
        VM.sysWriteln("JNI called: GetStringCritical");
    RuntimeEntrypoints.checkJNICountDownToGC();
    String str = (String) env.getJNIRef(strJREF);
    char[] strChars = java.lang.JikesRVMSupport.getBackingCharArray(str);
    int strOffset = java.lang.JikesRVMSupport.getStringOffset(str);
    /* Set caller's isCopy boolean to false, if we got a valid (non-null)
       address */
    JNIGenericHelpers.setBoolStar(isCopyAddress, false);
    VM.disableGC(true);
    Address strBase = Magic.objectAsAddress(strChars);
    return strBase.plus(strOffset * 2);
}
Also used : Address(org.vmmagic.unboxed.Address)

Example 22 with Address

use of org.vmmagic.unboxed.Address in project JikesRVM by JikesRVM.

the class JNIFunctions method GetStringChars.

/**
 * GetStringChars:  return address of buffer containing contents of a String
 * @param env A JREF index for the JNI environment object
 * @param strJREF a JREF index for the String object
 * @param isCopyAddress address of isCopy jboolean (an int)
 * @return address of a copy of the String unicode characters
 *         and *isCopy is set to 1 (TRUE)
 * @throws OutOfMemoryError if the system runs out of memory
 */
private static Address GetStringChars(JNIEnvironment env, int strJREF, Address isCopyAddress) {
    if (traceJNI)
        VM.sysWriteln("JNI called: GetStringChars");
    RuntimeEntrypoints.checkJNICountDownToGC();
    String str = (String) env.getJNIRef(strJREF);
    char[] strChars = java.lang.JikesRVMSupport.getBackingCharArray(str);
    int strOffset = java.lang.JikesRVMSupport.getStringOffset(str);
    int len = java.lang.JikesRVMSupport.getStringLength(str);
    // alloc non moving buffer in C heap for a copy of string contents
    Address copyBuffer = sysCall.sysMalloc(len * 2);
    if (copyBuffer.isZero()) {
        env.recordException(new OutOfMemoryError());
        return Address.zero();
    }
    try {
        Address strBase = Magic.objectAsAddress(strChars);
        Address srcBase = strBase.plus(strOffset * 2);
        Memory.memcopy(copyBuffer, srcBase, len * 2);
        /* Set caller's isCopy boolean to true, if we got a valid (non-null)
         address */
        JNIGenericHelpers.setBoolStar(isCopyAddress, true);
        return copyBuffer;
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
        sysCall.sysFree(copyBuffer);
        return Address.zero();
    }
}
Also used : Address(org.vmmagic.unboxed.Address)

Example 23 with Address

use of org.vmmagic.unboxed.Address in project JikesRVM by JikesRVM.

the class JNIFunctions method GetCharArrayElements.

/**
 * GetCharArrayElements: get all the elements of a char array
 * @param env A JREF index for the JNI environment object
 * @param arrayJREF a JREF index for the source array
 * @param isCopyAddress address of a flag to indicate whether the returned array is a copy or a direct pointer
 * @return A pointer to the char array and the isCopy flag is set to true if it's a copy
 *         or false if it's a direct pointer
 * @throws OutOfMemoryError if the system runs out of memory
 */
private static Address GetCharArrayElements(JNIEnvironment env, int arrayJREF, Address isCopyAddress) {
    if (traceJNI)
        VM.sysWriteln("JNI called: GetCharArrayElements");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        char[] sourceArray = (char[]) env.getJNIRef(arrayJREF);
        int size = sourceArray.length;
        if (MemoryManager.willNeverMove(sourceArray)) {
            JNIGenericHelpers.setBoolStar(isCopyAddress, false);
            return Magic.objectAsAddress(sourceArray);
        } else {
            // alloc non moving buffer in C heap for a copy of string contents
            Address copyBuffer = sysCall.sysMalloc(size * BYTES_IN_CHAR);
            if (copyBuffer.isZero()) {
                env.recordException(new OutOfMemoryError());
                return Address.zero();
            }
            Memory.memcopy(copyBuffer, Magic.objectAsAddress(sourceArray), size * BYTES_IN_CHAR);
            /* Set caller's isCopy boolean to true, if we got a valid (non-null)
         address */
            JNIGenericHelpers.setBoolStar(isCopyAddress, true);
            return copyBuffer;
        }
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
        return Address.zero();
    }
}
Also used : Address(org.vmmagic.unboxed.Address)

Example 24 with Address

use of org.vmmagic.unboxed.Address in project JikesRVM by JikesRVM.

the class JNIFunctions method GetJavaVM.

private static int GetJavaVM(JNIEnvironment env, Address StarStarJavaVM) {
    if (traceJNI)
        VM.sysWriteln("JNI called: GetJavaVM");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        if (traceJNI)
            VM.sysWriteln(StarStarJavaVM);
        Address JavaVM = BootRecord.the_boot_record.sysJavaVM;
        StarStarJavaVM.store(JavaVM);
        return 0;
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        return -1;
    }
}
Also used : Address(org.vmmagic.unboxed.Address)

Example 25 with Address

use of org.vmmagic.unboxed.Address in project JikesRVM by JikesRVM.

the class JNIFunctions method GetStringRegion.

/**
 * GetStringRegion:  Copy a region of Unicode characters from a string to
 *  the given buffer.
 *
 *  @param env A JREF index for the JNI environment object
 *  @param strJREF a JREF index for the String object
 *  @param start index to start reading characters from the string
 *  @param len how many characters to read
 *  @param buf the buffer to copy the region into
 *  @throws StringIndexOutOfBoundsException if asked for an out-of-range
 *        region of the string.
 */
private static void GetStringRegion(JNIEnvironment env, int strJREF, int start, int len, Address buf) {
    if (traceJNI)
        VM.sysWriteln("JNI called: GetStringRegion");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        String str = (String) env.getJNIRef(strJREF);
        char[] strChars = java.lang.JikesRVMSupport.getBackingCharArray(str);
        int strOffset = java.lang.JikesRVMSupport.getStringOffset(str);
        int strLen = java.lang.JikesRVMSupport.getStringLength(str);
        if (strLen < start + len) {
            env.recordException(new StringIndexOutOfBoundsException());
            return;
        }
        Address strBase = Magic.objectAsAddress(strChars);
        Address srcBase = strBase.plus(strOffset * 2).plus(start * 2);
        Memory.memcopy(buf, srcBase, len * 2);
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
    }
}
Also used : Address(org.vmmagic.unboxed.Address)

Aggregations

Address (org.vmmagic.unboxed.Address)281 Offset (org.vmmagic.unboxed.Offset)48 CompiledMethod (org.jikesrvm.compilers.common.CompiledMethod)30 NoInline (org.vmmagic.pragma.NoInline)30 Test (org.junit.Test)24 Entrypoint (org.vmmagic.pragma.Entrypoint)22 TypeReference (org.jikesrvm.classloader.TypeReference)21 OptCompiledMethod (org.jikesrvm.compilers.opt.runtimesupport.OptCompiledMethod)17 RVMType (org.jikesrvm.classloader.RVMType)16 Inline (org.vmmagic.pragma.Inline)15 Uninterruptible (org.vmmagic.pragma.Uninterruptible)14 Word (org.vmmagic.unboxed.Word)14 BaseMMTkTest (org.mmtk.harness.tests.BaseMMTkTest)13 Unpreemptible (org.vmmagic.pragma.Unpreemptible)12 ObjectReference (org.vmmagic.unboxed.ObjectReference)12 Interruptible (org.vmmagic.pragma.Interruptible)11 Extent (org.vmmagic.unboxed.Extent)11 RVMClass (org.jikesrvm.classloader.RVMClass)9 AddressConstantOperand (org.jikesrvm.compilers.opt.ir.operand.AddressConstantOperand)8 IntConstantOperand (org.jikesrvm.compilers.opt.ir.operand.IntConstantOperand)8