jdk/src/share/back/commonRef.c
author jjh
Tue, 01 Jul 2008 09:23:00 -0700
changeset 828 ad3f54bd6ae8
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
2157677: ClassPrepareRequest.addSourceNameFilter() does not behave as documented Summary: Add proper handling of JVMTI errors. Reviewed-by: tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include "util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "commonRef.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#define ALL_REFS -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Each object sent to the front end is tracked with the RefNode struct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * (see util.h).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * External to this module, objects are identified by a jlong id which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * simply the sequence number. A weak reference is usually used so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the presence of a debugger-tracked object will not prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * its collection. Once an object is collected, its RefNode may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * deleted and the weak ref inside may be reused (these may happen in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * either order). Using the sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * as the object id prevents ambiguity in the object id when the weak ref
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * is reused. The RefNode* is stored with the object as it's JVMTI Tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The ref member is changed from weak to strong when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * gc of the object is to be prevented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Whether or not it is strong, it is never exported from this module.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * A reference count of each jobject is also maintained here. It tracks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the number times an object has been referenced through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * commonRef_refToID. A RefNode is freed once the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * count is decremented to 0 (with commonRef_release*), even if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * correspoding object has not been collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * One hash table is maintained. The mapping of ID to jobject (or RefNode*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * is handled with one hash table that will re-size itself as the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * of RefNode's grow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
/* Initial hash table size (must be power of 2) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#define HASH_INIT_SIZE 512
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/* If element count exceeds HASH_EXPAND_SCALE*hash_size we expand & re-hash */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#define HASH_EXPAND_SCALE 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
/* Maximum hash table size (must be power of 2) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#define HASH_MAX_SIZE  (1024*HASH_INIT_SIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
/* Map a key (ID) to a hash bucket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
static jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
hashBucket(jlong key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /* Size should always be a power of 2, use mask instead of mod operator */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    return ((jint)key) & (gdata->objectsByIDsize-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
/* Generate a new ID */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
static jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
newSeqNum(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    return gdata->nextSeqNum++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
/* Create a fresh RefNode structure, create a weak ref and tag the object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
static RefNode *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
createNode(JNIEnv *env, jobject ref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    RefNode   *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    jobject    weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /* Could allocate RefNode's in blocks, not sure it would help much */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    node = (RefNode*)jvmtiAllocate((int)sizeof(RefNode));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    if (node == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /* Create weak reference to make sure we have a reference */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    weakRef = JNI_FUNC_PTR(env,NewWeakGlobalRef)(env, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    if (weakRef == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        jvmtiDeallocate(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /* Set tag on weakRef */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    error = JVMTI_FUNC_PTR(gdata->jvmti, SetTag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                          (gdata->jvmti, weakRef, ptr_to_jlong(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    if ( error != JVMTI_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, weakRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        jvmtiDeallocate(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /* Fill in RefNode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    node->ref      = weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    node->isStrong = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    node->count    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    node->seqNum   = newSeqNum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /* Count RefNode's created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    gdata->objectsByIDcount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
/* Delete a RefNode allocation, delete weak/global ref and clear tag */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
deleteNode(JNIEnv *env, RefNode *node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    LOG_MISC(("Freeing %d (%x)\n", (int)node->seqNum, node->ref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    if ( node->ref != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        /* Clear tag */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        (void)JVMTI_FUNC_PTR(gdata->jvmti,SetTag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                            (gdata->jvmti, node->ref, NULL_OBJECT_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (node->isStrong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            JNI_FUNC_PTR(env,DeleteGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    gdata->objectsByIDcount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    jvmtiDeallocate(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
/* Change a RefNode to have a strong reference */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
static jobject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
strengthenNode(JNIEnv *env, RefNode *node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    if (!node->isStrong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        jobject strongRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        strongRef = JNI_FUNC_PTR(env,NewGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         * NewGlobalRef on a weak ref will return NULL if the weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * reference has been collected or if out of memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         * We need to distinguish those two occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if ((strongRef == NULL) && !isSameObject(env, node->ref, NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"NewGlobalRef");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (strongRef != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            node->ref      = strongRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            node->isStrong = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return strongRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return node->ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
/* Change a RefNode to have a weak reference */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
static jweak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
weakenNode(JNIEnv *env, RefNode *node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    if (node->isStrong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        jweak weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        weakRef = JNI_FUNC_PTR(env,NewWeakGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (weakRef != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            JNI_FUNC_PTR(env,DeleteGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            node->ref      = weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            node->isStrong = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return node->ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * Returns the node which contains the common reference for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * given object. The passed reference should not be a weak reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * managed in the object hash table (i.e. returned by commonRef_idToRef)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * because no sequence number checking is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
static RefNode *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
findNodeByRef(JNIEnv *env, jobject ref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    jlong      tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    tag   = NULL_OBJECT_ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    error = JVMTI_FUNC_PTR(gdata->jvmti,GetTag)(gdata->jvmti, ref, &tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    if ( error == JVMTI_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        RefNode   *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        node = (RefNode*)jlong_to_ptr(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
/* Locate and delete a node based on ID */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
deleteNodeByID(JNIEnv *env, jlong id, jint refCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    jint     slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    RefNode *prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    slot = hashBucket(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    node = gdata->objectsByID[slot];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    prev = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    while (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (id == node->seqNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (refCount != ALL_REFS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                node->count -= refCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                node->count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (node->count <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                if ( node->count < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    EXIT_ERROR(AGENT_ERROR_INTERNAL,"RefNode count < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                /* Detach from id hash table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                if (prev == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    gdata->objectsByID[slot] = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    prev->next = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                deleteNode(env, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        prev = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        node = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * Returns the node stored in the object hash table for the given object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * id. The id should be a value previously returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * commonRef_refToID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *  NOTE: It is possible that a match is found here, but that the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *        is garbage collected by the time the caller inspects node->ref.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *        Callers should take care using the node->ref object returned here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
static RefNode *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
findNodeByID(JNIEnv *env, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    jint     slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    RefNode *prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    slot = hashBucket(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    node = gdata->objectsByID[slot];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    prev = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    while (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if ( id == node->seqNum ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if ( prev != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                /* Re-order hash list so this one is up front */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                prev->next = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                node->next = gdata->objectsByID[slot];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                gdata->objectsByID[slot] = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        node = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
/* Initialize the hash table stored in gdata area */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
initializeObjectsByID(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /* Size should always be a power of 2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    if ( size > HASH_MAX_SIZE ) size = HASH_MAX_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    gdata->objectsByIDsize  = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    gdata->objectsByIDcount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    gdata->objectsByID      = (RefNode**)jvmtiAllocate((int)sizeof(RefNode*)*size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    (void)memset(gdata->objectsByID, 0, (int)sizeof(RefNode*)*size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
/* hash in a RefNode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
hashIn(RefNode *node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    jint     slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /* Add to id hashtable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    slot                     = hashBucket(node->seqNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    node->next               = gdata->objectsByID[slot];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    gdata->objectsByID[slot] = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
/* Allocate and add RefNode to hash table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
static RefNode *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
newCommonRef(JNIEnv *env, jobject ref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /* Allocate the node and set it up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    node = createNode(env, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    if ( node == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /* See if hash table needs expansion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    if ( gdata->objectsByIDcount > gdata->objectsByIDsize*HASH_EXPAND_SCALE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         gdata->objectsByIDsize < HASH_MAX_SIZE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        RefNode **old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        int       oldsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        int       newsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        int       i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        /* Save old information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        old     = gdata->objectsByID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        oldsize = gdata->objectsByIDsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        /* Allocate new hash table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        gdata->objectsByID = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        newsize = oldsize*HASH_EXPAND_SCALE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if ( newsize > HASH_MAX_SIZE ) newsize = HASH_MAX_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        initializeObjectsByID(newsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        /* Walk over old one and hash in all the RefNodes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        for ( i = 0 ; i < oldsize ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            RefNode *onode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            onode = old[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            while (onode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                RefNode *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                next = onode->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                hashIn(onode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                onode = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        jvmtiDeallocate(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /* Add to id hashtable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    hashIn(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
/* Initialize the commonRefs usage */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
commonRef_initialize(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    gdata->refLock = debugMonitorCreate("JDWP Reference Table Monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    gdata->nextSeqNum       = 1; /* 0 used for error indication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    initializeObjectsByID(HASH_INIT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
/* Reset the commonRefs usage */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
commonRef_reset(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        for (i = 0; i < gdata->objectsByIDsize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            node = gdata->objectsByID[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            while (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                RefNode *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                next = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                deleteNode(env, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                node = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            gdata->objectsByID[i] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        /* Toss entire hash table and re-create a new one */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        jvmtiDeallocate(gdata->objectsByID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        gdata->objectsByID      = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        gdata->nextSeqNum       = 1; /* 0 used for error indication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        initializeObjectsByID(HASH_INIT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
 * Given a reference obtained from JNI or JVMTI, return an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
 * id suitable for sending to the debugger front end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
commonRef_refToID(JNIEnv *env, jobject ref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    jlong id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    if (ref == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return NULL_OBJECT_ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    id = NULL_OBJECT_ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        node = findNodeByRef(env, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (node == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            node = newCommonRef(env, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if ( node != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                id = node->seqNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            id = node->seqNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            node->count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
 * Given an object ID obtained from the debugger front end, return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 * strong, global reference to that object (or NULL if the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
 * has been collected). The reference can then be used for JNI and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 * JVMTI calls. Caller is resposible for deleting the returned reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
jobject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
commonRef_idToRef(JNIEnv *env, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    jobject ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    ref = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        node = findNodeByID(env, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (node->isStrong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                saveGlobalRef(env, node->ref, &ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                jobject lref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                lref = JNI_FUNC_PTR(env,NewLocalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                if ( lref == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    /* Object was GC'd shortly after we found the node */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    deleteNodeByID(env, node->seqNum, ALL_REFS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    saveGlobalRef(env, node->ref, &ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    JNI_FUNC_PTR(env,DeleteLocalRef)(env, lref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    return ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
/* Deletes the global reference that commonRef_idToRef() created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
commonRef_idToRef_delete(JNIEnv *env, jobject ref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    if ( ref==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    tossGlobalRef(env, &ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
/* Prevent garbage collection of an object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
jvmtiError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
commonRef_pin(jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    error = JVMTI_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    if (id == NULL_OBJECT_ID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        JNIEnv  *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        env  = getEnv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        node = findNodeByID(env, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (node == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            error = AGENT_ERROR_INVALID_OBJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            jobject strongRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            strongRef = strengthenNode(env, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            if (strongRef == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                 * Referent has been collected, clean up now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                error = AGENT_ERROR_INVALID_OBJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                deleteNodeByID(env, id, ALL_REFS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
/* Permit garbage collection of an object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
jvmtiError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
commonRef_unpin(jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    error = JVMTI_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        JNIEnv  *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        env  = getEnv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        node = findNodeByID(env, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            jweak weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            weakRef = weakenNode(env, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            if (weakRef == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                error = AGENT_ERROR_OUT_OF_MEMORY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
/* Release tracking of an object by ID */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
commonRef_release(JNIEnv *env, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        deleteNodeByID(env, id, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
commonRef_releaseMultiple(JNIEnv *env, jlong id, jint refCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        deleteNodeByID(env, id, refCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
/* Get rid of RefNodes for objects that no longer exist */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
commonRef_compact(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    JNIEnv  *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    RefNode *prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    int      i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    env = getEnv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if ( gdata->objectsByIDsize > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
             * Walk through the id-based hash table. Detach any nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
             * for which the ref has been collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            for (i = 0; i < gdata->objectsByIDsize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                node = gdata->objectsByID[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                prev = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                while (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    /* Has the object been collected? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    if ( (!node->isStrong) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                          isSameObject(env, node->ref, NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        RefNode *freed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        /* Detach from the ID list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        if (prev == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                            gdata->objectsByID[i] = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                            prev->next = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        freed = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        node = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        deleteNode(env, freed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        prev = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        node = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
/* Lock the commonRef tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
commonRef_lock(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    debugMonitorEnter(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
/* Unlock the commonRef tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
commonRef_unlock(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
}