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