/* Initial hash table size (must be power of 2) */ #define HASH_INIT_SIZE 512 /* If element count exceeds HASH_EXPAND_SCALE*hash_size we expand & re-hash */ #define HASH_EXPAND_SCALE 8 /* Maximum hash table size (must be power of 2) */ #define HASH_MAX_SIZE (1024*HASH_INIT_SIZE)
/* Map a key (ID) to a hash bucket */ static jint
hashBucket(jlong key)
{ /* Size should always be a power of 2, use mask instead of mod operator */ /*LINTED*/ return ((jint)key) & (gdata->objectsByIDsize-1);
}
/* Generate a new ID */ static jlong
newSeqNum(void)
{ return gdata->nextSeqNum++;
}
/* Returns true if this is a strong reference, meaning that either or both of
isPinAll and isCommonPin are true. */ static jboolean
isStrong(RefNode* node)
{ return node->isPinAll || node->isCommonPin;
}
/* Create a fresh RefNode structure, create a weak ref and tag the object */ static RefNode *
createNode(JNIEnv *env, jobject ref)
{
RefNode *node;
jobject strongOrWeakRef;
jvmtiError error;
jboolean pinAll = gdata->pinAllCount != 0;
/* Could allocate RefNode's in blocks, not sure it would help much */
node = (RefNode*)jvmtiAllocate((int)sizeof(RefNode)); if (node == NULL) { return NULL;
}
if (pinAll) { /* Create strong reference to make sure we have a reference */
strongOrWeakRef = JNI_FUNC_PTR(env,NewGlobalRef)(env, ref);
} else { /* Create weak reference to make sure we have a reference */
strongOrWeakRef = JNI_FUNC_PTR(env,NewWeakGlobalRef)(env, ref);
// NewWeakGlobalRef can throw OOM, clear exception here. if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionClear(env);
jvmtiDeallocate(node); return NULL;
}
}
/* Set tag on strongOrWeakRef */
error = JVMTI_FUNC_PTR(gdata->jvmti, SetTag)
(gdata->jvmti, strongOrWeakRef, ptr_to_jlong(node)); if ( error != JVMTI_ERROR_NONE ) { if (pinAll) {
JNI_FUNC_PTR(env,DeleteGlobalRef)(env, strongOrWeakRef);
} else {
JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, strongOrWeakRef);
}
jvmtiDeallocate(node); return NULL;
}
/* Fill in RefNode */
node->ref = strongOrWeakRef;
node->count = 1;
node->isPinAll = pinAll;
node->isCommonPin = JNI_FALSE;
node->seqNum = newSeqNum();
/* Count RefNode's created */
gdata->objectsByIDcount++; return node;
}
/* Delete a RefNode allocation, delete weak/global ref and clear tag */ staticvoid
deleteNode(JNIEnv *env, RefNode *node)
{
LOG_MISC(("Freeing %d (%x)\n", (int)node->seqNum, node->ref));
if ( node->ref != NULL ) { /* Clear tag */
(void)JVMTI_FUNC_PTR(gdata->jvmti,SetTag)
(gdata->jvmti, node->ref, NULL_OBJECT_ID); if (isStrong(node)) {
JNI_FUNC_PTR(env,DeleteGlobalRef)(env, node->ref);
} else {
JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, node->ref);
}
}
gdata->objectsByIDcount--;
jvmtiDeallocate(node);
}
/* Change a RefNode to have a strong reference */ static jobject
strengthenNode(JNIEnv *env, RefNode *node, jboolean isPinAll)
{ if (!isStrong(node)) {
jobject strongRef;
/* Change a RefNode to have a weak reference */ static jweak
weakenNode(JNIEnv *env, RefNode *node, jboolean isUnpinAll)
{
jboolean willStillBeStrong = (node->isPinAll && !isUnpinAll) || (node->isCommonPin && isUnpinAll);
// If the node is strong, but the reason(s) for it being strong // will no longer exist, then weaken it. if (isStrong(node) && !willStillBeStrong) {
jweak weakRef;
weakRef = JNI_FUNC_PTR(env,NewWeakGlobalRef)(env, node->ref); // NewWeakGlobalRef can throw OOM, clear exception here. if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionClear(env);
}
while (node != NULL) { if ( id == node->seqNum ) { if ( prev != NULL ) { /* Re-order hash list so this one is up front */
prev->next = node->next;
node->next = gdata->objectsByID[slot];
gdata->objectsByID[slot] = node;
} break;
}
node = node->next;
} return node;
}
/* Initialize the hash table stored in gdata area */ staticvoid
initializeObjectsByID(int size)
{ /* Size should always be a power of 2 */ if ( size > HASH_MAX_SIZE ) size = HASH_MAX_SIZE;
gdata->objectsByIDsize = size;
gdata->objectsByIDcount = 0;
gdata->objectsByID = (RefNode**)jvmtiAllocate((int)sizeof(RefNode*)*size);
(void)memset(gdata->objectsByID, 0, (int)sizeof(RefNode*)*size);
}
/* hash in a RefNode */ staticvoid
hashIn(RefNode *node)
{
jint slot;
/* Add to id hashtable */
slot = hashBucket(node->seqNum);
node->next = gdata->objectsByID[slot];
gdata->objectsByID[slot] = node;
}
/* Allocate and add RefNode to hash table */ static RefNode *
newCommonRef(JNIEnv *env, jobject ref)
{
RefNode *node;
/* Allocate the node and set it up */
node = createNode(env, ref); if ( node == NULL ) { return NULL;
}
/* See if hash table needs expansion */ if ( gdata->objectsByIDcount > gdata->objectsByIDsize*HASH_EXPAND_SCALE &&
gdata->objectsByIDsize < HASH_MAX_SIZE ) {
RefNode **old; int oldsize; int newsize; int i;
/* Save old information */
old = gdata->objectsByID;
oldsize = gdata->objectsByIDsize; /* Allocate new hash table */
gdata->objectsByID = NULL;
newsize = oldsize*HASH_EXPAND_SCALE; if ( newsize > HASH_MAX_SIZE ) newsize = HASH_MAX_SIZE;
initializeObjectsByID(newsize); /* Walk over old one and hash in all the RefNodes */ for ( i = 0 ; i < oldsize ; i++ ) {
RefNode *onode;
onode = old[i]; while (onode != NULL) {
RefNode *next;
/* Toss entire hash table and re-create a new one */
jvmtiDeallocate(gdata->objectsByID);
gdata->objectsByID = NULL;
gdata->nextSeqNum = 1; /* 0 used for error indication */
initializeObjectsByID(HASH_INIT_SIZE);
node = findNodeByID(env, id); if (node != NULL) { if (isStrong(node)) {
saveGlobalRef(env, node->ref, &ref);
} else {
jobject lref;
lref = JNI_FUNC_PTR(env,NewLocalRef)(env, node->ref); // NewLocalRef never throws OOM. if ( lref == NULL ) { /* Object was GC'd shortly after we found the node */
deleteNodeByID(env, node->seqNum, ALL_REFS);
} else {
saveGlobalRef(env, node->ref, &ref);
JNI_FUNC_PTR(env,DeleteLocalRef)(env, lref);
}
}
}
} debugMonitorExit(gdata->refLock); return ref;
}
/* Deletes the global reference that commonRef_idToRef() created */ void
commonRef_idToRef_delete(JNIEnv *env, jobject ref)
{ if ( ref==NULL ) { return;
}
tossGlobalRef(env, &ref);
}
/* Prevent garbage collection of an object */
jvmtiError
commonRef_pin(jlong id)
{
jvmtiError error;
error = JVMTI_ERROR_NONE; if (id == NULL_OBJECT_ID) { return error;
}
debugMonitorEnter(gdata->refLock); {
JNIEnv *env;
RefNode *node;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.