jdk/src/jdk.jdwp.agent/share/native/libjdwp/commonRef.c
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 29272 3363c00f4a4e
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 1998, 2013, 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
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
    55
 * corresponding object has not been collected.
2
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);
29272
3363c00f4a4e 8030708: warnings from b119 for jdk/src/share/back: JNI exception pending
dsamersoff
parents: 25859
diff changeset
   101
    // NewWeakGlobalRef can throw OOM, clear exception here.
3363c00f4a4e 8030708: warnings from b119 for jdk/src/share/back: JNI exception pending
dsamersoff
parents: 25859
diff changeset
   102
    if ((*env)->ExceptionCheck(env)) {
3363c00f4a4e 8030708: warnings from b119 for jdk/src/share/back: JNI exception pending
dsamersoff
parents: 25859
diff changeset
   103
        (*env)->ExceptionClear(env);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        jvmtiDeallocate(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /* Set tag on weakRef */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    error = JVMTI_FUNC_PTR(gdata->jvmti, SetTag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                          (gdata->jvmti, weakRef, ptr_to_jlong(node));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    if ( error != JVMTI_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, weakRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        jvmtiDeallocate(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /* Fill in RefNode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    node->ref      = weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    node->isStrong = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    node->count    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    node->seqNum   = newSeqNum();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /* Count RefNode's created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    gdata->objectsByIDcount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
/* Delete a RefNode allocation, delete weak/global ref and clear tag */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
deleteNode(JNIEnv *env, RefNode *node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    LOG_MISC(("Freeing %d (%x)\n", (int)node->seqNum, node->ref));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    if ( node->ref != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        /* Clear tag */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        (void)JVMTI_FUNC_PTR(gdata->jvmti,SetTag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                            (gdata->jvmti, node->ref, NULL_OBJECT_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (node->isStrong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            JNI_FUNC_PTR(env,DeleteGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    gdata->objectsByIDcount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    jvmtiDeallocate(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
/* Change a RefNode to have a strong reference */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
static jobject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
strengthenNode(JNIEnv *env, RefNode *node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    if (!node->isStrong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        jobject strongRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        strongRef = JNI_FUNC_PTR(env,NewGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         * NewGlobalRef on a weak ref will return NULL if the weak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         * reference has been collected or if out of memory.
29272
3363c00f4a4e 8030708: warnings from b119 for jdk/src/share/back: JNI exception pending
dsamersoff
parents: 25859
diff changeset
   159
         * It never throws OOM.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * We need to distinguish those two occurrences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if ((strongRef == NULL) && !isSameObject(env, node->ref, NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"NewGlobalRef");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (strongRef != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            node->ref      = strongRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            node->isStrong = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return strongRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return node->ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
/* Change a RefNode to have a weak reference */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
static jweak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
weakenNode(JNIEnv *env, RefNode *node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    if (node->isStrong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        jweak weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        weakRef = JNI_FUNC_PTR(env,NewWeakGlobalRef)(env, node->ref);
29272
3363c00f4a4e 8030708: warnings from b119 for jdk/src/share/back: JNI exception pending
dsamersoff
parents: 25859
diff changeset
   184
        // NewWeakGlobalRef can throw OOM, clear exception here.
3363c00f4a4e 8030708: warnings from b119 for jdk/src/share/back: JNI exception pending
dsamersoff
parents: 25859
diff changeset
   185
        if ((*env)->ExceptionCheck(env)) {
3363c00f4a4e 8030708: warnings from b119 for jdk/src/share/back: JNI exception pending
dsamersoff
parents: 25859
diff changeset
   186
            (*env)->ExceptionClear(env);
3363c00f4a4e 8030708: warnings from b119 for jdk/src/share/back: JNI exception pending
dsamersoff
parents: 25859
diff changeset
   187
        }
3363c00f4a4e 8030708: warnings from b119 for jdk/src/share/back: JNI exception pending
dsamersoff
parents: 25859
diff changeset
   188
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (weakRef != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            JNI_FUNC_PTR(env,DeleteGlobalRef)(env, node->ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            node->ref      = weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            node->isStrong = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return node->ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * Returns the node which contains the common reference for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * given object. The passed reference should not be a weak reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * managed in the object hash table (i.e. returned by commonRef_idToRef)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * because no sequence number checking is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
static RefNode *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
findNodeByRef(JNIEnv *env, jobject ref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    jlong      tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    tag   = NULL_OBJECT_ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    error = JVMTI_FUNC_PTR(gdata->jvmti,GetTag)(gdata->jvmti, ref, &tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    if ( error == JVMTI_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        RefNode   *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        node = (RefNode*)jlong_to_ptr(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
/* Locate and delete a node based on ID */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
deleteNodeByID(JNIEnv *env, jlong id, jint refCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    jint     slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    RefNode *prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    slot = hashBucket(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    node = gdata->objectsByID[slot];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    prev = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    while (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (id == node->seqNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (refCount != ALL_REFS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                node->count -= refCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                node->count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (node->count <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                if ( node->count < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    EXIT_ERROR(AGENT_ERROR_INTERNAL,"RefNode count < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                /* Detach from id hash table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                if (prev == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    gdata->objectsByID[slot] = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    prev->next = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                deleteNode(env, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        prev = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        node = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 * Returns the node stored in the object hash table for the given object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * id. The id should be a value previously returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * commonRef_refToID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 *  NOTE: It is possible that a match is found here, but that the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 *        is garbage collected by the time the caller inspects node->ref.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 *        Callers should take care using the node->ref object returned here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
static RefNode *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
findNodeByID(JNIEnv *env, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    jint     slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    RefNode *prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    slot = hashBucket(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    node = gdata->objectsByID[slot];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    prev = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    while (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if ( id == node->seqNum ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if ( prev != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                /* Re-order hash list so this one is up front */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                prev->next = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                node->next = gdata->objectsByID[slot];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                gdata->objectsByID[slot] = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        node = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
/* Initialize the hash table stored in gdata area */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
initializeObjectsByID(int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /* Size should always be a power of 2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    if ( size > HASH_MAX_SIZE ) size = HASH_MAX_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    gdata->objectsByIDsize  = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    gdata->objectsByIDcount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    gdata->objectsByID      = (RefNode**)jvmtiAllocate((int)sizeof(RefNode*)*size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    (void)memset(gdata->objectsByID, 0, (int)sizeof(RefNode*)*size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
/* hash in a RefNode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
hashIn(RefNode *node)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    jint     slot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /* Add to id hashtable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    slot                     = hashBucket(node->seqNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    node->next               = gdata->objectsByID[slot];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    gdata->objectsByID[slot] = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
/* Allocate and add RefNode to hash table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
static RefNode *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
newCommonRef(JNIEnv *env, jobject ref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /* Allocate the node and set it up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    node = createNode(env, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    if ( node == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /* See if hash table needs expansion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    if ( gdata->objectsByIDcount > gdata->objectsByIDsize*HASH_EXPAND_SCALE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         gdata->objectsByIDsize < HASH_MAX_SIZE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        RefNode **old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        int       oldsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        int       newsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        int       i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        /* Save old information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        old     = gdata->objectsByID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        oldsize = gdata->objectsByIDsize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        /* Allocate new hash table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        gdata->objectsByID = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        newsize = oldsize*HASH_EXPAND_SCALE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if ( newsize > HASH_MAX_SIZE ) newsize = HASH_MAX_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        initializeObjectsByID(newsize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        /* Walk over old one and hash in all the RefNodes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        for ( i = 0 ; i < oldsize ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            RefNode *onode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            onode = old[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            while (onode != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                RefNode *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                next = onode->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                hashIn(onode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                onode = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        jvmtiDeallocate(old);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /* Add to id hashtable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    hashIn(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
/* Initialize the commonRefs usage */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
commonRef_initialize(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    gdata->refLock = debugMonitorCreate("JDWP Reference Table Monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    gdata->nextSeqNum       = 1; /* 0 used for error indication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    initializeObjectsByID(HASH_INIT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
/* Reset the commonRefs usage */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
commonRef_reset(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        for (i = 0; i < gdata->objectsByIDsize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            node = gdata->objectsByID[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            while (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                RefNode *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                next = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                deleteNode(env, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                node = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            gdata->objectsByID[i] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        /* Toss entire hash table and re-create a new one */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        jvmtiDeallocate(gdata->objectsByID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        gdata->objectsByID      = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        gdata->nextSeqNum       = 1; /* 0 used for error indication */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        initializeObjectsByID(HASH_INIT_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
 * Given a reference obtained from JNI or JVMTI, return an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
 * id suitable for sending to the debugger front end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
commonRef_refToID(JNIEnv *env, jobject ref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    jlong id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    if (ref == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return NULL_OBJECT_ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    id = NULL_OBJECT_ID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        node = findNodeByRef(env, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (node == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            node = newCommonRef(env, ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            if ( node != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                id = node->seqNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            id = node->seqNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            node->count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
 * Given an object ID obtained from the debugger front end, return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 * strong, global reference to that object (or NULL if the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 * has been collected). The reference can then be used for JNI and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
 * JVMTI calls. Caller is resposible for deleting the returned reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
jobject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
commonRef_idToRef(JNIEnv *env, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    jobject ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    ref = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        node = findNodeByID(env, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (node->isStrong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                saveGlobalRef(env, node->ref, &ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                jobject lref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                lref = JNI_FUNC_PTR(env,NewLocalRef)(env, node->ref);
29272
3363c00f4a4e 8030708: warnings from b119 for jdk/src/share/back: JNI exception pending
dsamersoff
parents: 25859
diff changeset
   463
                // NewLocalRef never throws OOM.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                if ( lref == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    /* Object was GC'd shortly after we found the node */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    deleteNodeByID(env, node->seqNum, ALL_REFS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    saveGlobalRef(env, node->ref, &ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    JNI_FUNC_PTR(env,DeleteLocalRef)(env, lref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    return ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
/* Deletes the global reference that commonRef_idToRef() created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
commonRef_idToRef_delete(JNIEnv *env, jobject ref)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    if ( ref==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    tossGlobalRef(env, &ref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
/* Prevent garbage collection of an object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
jvmtiError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
commonRef_pin(jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    error = JVMTI_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    if (id == NULL_OBJECT_ID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        JNIEnv  *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        env  = getEnv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        node = findNodeByID(env, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        if (node == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            error = AGENT_ERROR_INVALID_OBJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            jobject strongRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            strongRef = strengthenNode(env, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (strongRef == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                 * Referent has been collected, clean up now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                error = AGENT_ERROR_INVALID_OBJECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                deleteNodeByID(env, id, ALL_REFS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
/* Permit garbage collection of an object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
jvmtiError
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
commonRef_unpin(jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    error = JVMTI_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        JNIEnv  *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        env  = getEnv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        node = findNodeByID(env, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            jweak weakRef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            weakRef = weakenNode(env, node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (weakRef == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                error = AGENT_ERROR_OUT_OF_MEMORY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
/* Release tracking of an object by ID */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
commonRef_release(JNIEnv *env, jlong id)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        deleteNodeByID(env, id, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
commonRef_releaseMultiple(JNIEnv *env, jlong id, jint refCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        deleteNodeByID(env, id, refCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
/* Get rid of RefNodes for objects that no longer exist */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
commonRef_compact(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    JNIEnv  *env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    RefNode *node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    RefNode *prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    int      i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    env = getEnv();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    debugMonitorEnter(gdata->refLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if ( gdata->objectsByIDsize > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
             * Walk through the id-based hash table. Detach any nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
             * for which the ref has been collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            for (i = 0; i < gdata->objectsByIDsize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                node = gdata->objectsByID[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                prev = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                while (node != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    /* Has the object been collected? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    if ( (!node->isStrong) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                          isSameObject(env, node->ref, NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        RefNode *freed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        /* Detach from the ID list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        if (prev == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                            gdata->objectsByID[i] = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                            prev->next = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                        freed = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                        node = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                        deleteNode(env, freed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                        prev = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        node = node->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    } debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
/* Lock the commonRef tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
commonRef_lock(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    debugMonitorEnter(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
/* Unlock the commonRef tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
commonRef_unlock(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    debugMonitorExit(gdata->refLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
}