Search in sources :

Example 1 with AddressArray

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

the class JNIFunctions method RegisterNatives.

/**
 * RegisterNatives: registers implementation of native methods
 * @param env A JREF index for the JNI environment object
 * @param classJREF a JREF index for the class to register native methods in
 * @param methodsAddress the address of an array of native methods to be registered
 * @param nmethods the number of native methods in the array
 * @return 0 is successful -1 if failed
 * @throws NoSuchMethodError if a specified method cannot be found or is not native
 */
private static int RegisterNatives(JNIEnvironment env, int classJREF, Address methodsAddress, int nmethods) {
    if (traceJNI)
        VM.sysWriteln("JNI called: RegisterNatives");
    RuntimeEntrypoints.checkJNICountDownToGC();
    try {
        // get the target class
        Class<?> jcls = (Class<?>) env.getJNIRef(classJREF);
        RVMType type = java.lang.JikesRVMSupport.getTypeForClass(jcls);
        if (!type.isClassType()) {
            env.recordException(new NoSuchMethodError());
            return 0;
        }
        RVMClass klass = type.asClass();
        if (!klass.isInitialized()) {
            RuntimeEntrypoints.initializeClassForDynamicLink(klass);
        }
        // Create list of methods and verify them to avoid partial success
        NativeMethod[] methods = new NativeMethod[nmethods];
        AddressArray symbols = AddressArray.create(nmethods);
        Address curMethod = methodsAddress;
        for (int i = 0; i < nmethods; i++) {
            String methodString = JNIGenericHelpers.createStringFromC(curMethod.loadAddress());
            Atom methodName = Atom.findOrCreateAsciiAtom(methodString);
            String sigString = JNIGenericHelpers.createStringFromC(curMethod.loadAddress(Offset.fromIntSignExtend(BYTES_IN_ADDRESS)));
            Atom sigName = Atom.findOrCreateAsciiAtom(sigString);
            // Find the target method
            RVMMethod meth = klass.findDeclaredMethod(methodName, sigName);
            if (meth == null || !meth.isNative()) {
                env.recordException(new NoSuchMethodError(klass + ": " + methodName + " " + sigName));
                return -1;
            }
            methods[i] = (NativeMethod) meth;
            symbols.set(i, curMethod.loadAddress(Offset.fromIntSignExtend(BYTES_IN_ADDRESS * 2)));
            curMethod = curMethod.plus(3 * BYTES_IN_ADDRESS);
        }
        // Register methods
        for (int i = 0; i < nmethods; i++) {
            methods[i].registerNativeSymbol(symbols.get(i));
        }
        return 0;
    } catch (Throwable unexpected) {
        if (traceJNI)
            unexpected.printStackTrace(System.err);
        env.recordException(unexpected);
        return -1;
    }
}
Also used : AddressArray(org.vmmagic.unboxed.AddressArray) Address(org.vmmagic.unboxed.Address) RVMType(org.jikesrvm.classloader.RVMType) Atom(org.jikesrvm.classloader.Atom) RVMClass(org.jikesrvm.classloader.RVMClass) RVMMethod(org.jikesrvm.classloader.RVMMethod) NativeMethod(org.jikesrvm.classloader.NativeMethod) RVMClass(org.jikesrvm.classloader.RVMClass)

Example 2 with AddressArray

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

the class JNIEnvironment method boot.

/**
 * Initialization required during VM booting; only does something if
 * we are on a platform that needs linkage triplets.
 */
public static void boot() {
    if (VM.BuildForPower64ELF_ABI) {
        // fill in the TOC and IP entries for each linkage triplet
        for (int i = 0; i < JNIFunctions.length(); i++) {
            AddressArray triplet = linkageTriplets.get(i);
            triplet.set(1, Magic.getTocPointer());
            triplet.set(0, Magic.objectAsAddress(JNIFunctions.get(i)));
        }
    }
}
Also used : AddressArray(org.vmmagic.unboxed.AddressArray) Entrypoint(org.vmmagic.pragma.Entrypoint)

Example 3 with AddressArray

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

the class FinalizableProcessor method add.

/**
 * Allocate an entry in the table. This should be called from an unpreemptible
 * context so that the entry can be filled. This method is responsible for growing
 * the table if necessary.
 *
 * @param object the object to add to the table of candidates
 */
@NoInline
@UnpreemptibleNoWarn("Non-preemptible but yield when table needs to be grown")
public void add(Object object) {
    lock.acquire();
    while (maxIndex >= table.length() || maxIndex >= freeReady()) {
        int newTableSize = -1;
        int newReadyForFinalizeSize = -1;
        AddressArray newTable = null;
        Object[] newReadyForFinalize = null;
        if (maxIndex >= table.length()) {
            newTableSize = STRESS ? table.length() + 1 : (int) (table.length() * GROWTH_FACTOR);
        }
        if (maxIndex >= freeReady()) {
            newReadyForFinalizeSize = table.length() + countReady();
            if (newReadyForFinalizeSize <= readyForFinalize.length) {
                newReadyForFinalizeSize = -1;
            }
        }
        {
            lock.release();
            if (newTableSize >= 0) {
                newTable = AddressArray.create(newTableSize);
            }
            if (newReadyForFinalizeSize >= 0) {
                newReadyForFinalize = new Object[newReadyForFinalizeSize];
            }
            lock.acquire();
        }
        if (maxIndex >= table.length() && newTable != null) {
            for (int i = 0; i < table.length(); i++) {
                newTable.set(i, table.get(i));
            }
            table = newTable;
        }
        if (maxIndex >= freeReady() && newReadyForFinalize != null) {
            int j = 0;
            for (int i = nextReadyIndex; i < lastReadyIndex && i < readyForFinalize.length; i++) {
                newReadyForFinalize[j++] = readyForFinalize[i];
            }
            if (lastReadyIndex < nextReadyIndex) {
                for (int i = 0; i < lastReadyIndex; i++) {
                    newReadyForFinalize[j++] = readyForFinalize[i];
                }
            }
            lastReadyIndex = j;
            nextReadyIndex = 0;
            readyForFinalize = newReadyForFinalize;
        }
    }
    table.set(maxIndex++, Magic.objectAsAddress(object));
    lock.release();
}
Also used : AddressArray(org.vmmagic.unboxed.AddressArray) NoInline(org.vmmagic.pragma.NoInline) UnpreemptibleNoWarn(org.vmmagic.pragma.UnpreemptibleNoWarn)

Example 4 with AddressArray

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

the class JNIGlobalRefTable method newGlobalRef.

static int newGlobalRef(Object referent) {
    if (VM.VerifyAssertions)
        VM._assert(MemoryManager.validRef(ObjectReference.fromObject(referent)));
    if (free >= JNIGlobalRefs.length()) {
        AddressArray newGlobalRefs = AddressArray.create(JNIGlobalRefs.length() * 2);
        copyAndReplaceGlobalRefs(newGlobalRefs);
    }
    JNIGlobalRefs.set(free, Magic.objectAsAddress(referent));
    return -free++;
}
Also used : AddressArray(org.vmmagic.unboxed.AddressArray)

Aggregations

AddressArray (org.vmmagic.unboxed.AddressArray)4 Atom (org.jikesrvm.classloader.Atom)1 NativeMethod (org.jikesrvm.classloader.NativeMethod)1 RVMClass (org.jikesrvm.classloader.RVMClass)1 RVMMethod (org.jikesrvm.classloader.RVMMethod)1 RVMType (org.jikesrvm.classloader.RVMType)1 Entrypoint (org.vmmagic.pragma.Entrypoint)1 NoInline (org.vmmagic.pragma.NoInline)1 UnpreemptibleNoWarn (org.vmmagic.pragma.UnpreemptibleNoWarn)1 Address (org.vmmagic.unboxed.Address)1