/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#if defined(_ALLBSD_SOURCE)
#include <stdint.h> /* for uintptr_t */
#endif
#include "util.h"
#include "commonRef.h"
/*
* Each object sent to the front end is tracked with the RefNode struct
* (see util.h).
* External to this module, objects are identified by a jlong id which is
* simply the sequence number. A weak reference is usually used so that
* the presence of a debugger-tracked object will not prevent
* its collection. Once an object is collected, its RefNode may be
* deleted and the weak ref inside may be reused (these may happen in
* either order). Using the sequence number
* as the object id prevents ambiguity in the object id when the weak ref
* is reused. The RefNode* is stored with the object as it's JVMTI Tag.
*
* The ref member is changed from weak to strong when
* gc of the object is to be prevented.
* Whether or not it is strong, it is never exported from this module.
*
* A reference count of each jobject is also maintained here. It tracks
* the number times an object has been referenced through
* commonRef_refToID. A RefNode is freed once the reference
* count is decremented to 0 (with commonRef_release*), even if the
* correspoding object has not been collected.
*
* One hash table is maintained. The mapping of ID to jobject (or RefNode*)
* is handled with one hash table that will re-size itself as the number
* of RefNode's grow.
*/
/* Initial hash table size (must be power of 2) */
/* If element count exceeds HASH_EXPAND_SCALE*hash_size we expand & re-hash */
/* Maximum hash table size (must be power of 2) */
/* Map a key (ID) to a hash bucket */
static jint
{
/* Size should always be a power of 2, use mask instead of mod operator */
/*LINTED*/
}
/* Generate a new ID */
static jlong
newSeqNum(void)
{
return gdata->nextSeqNum++;
}
/* Create a fresh RefNode structure, create a weak ref and tag the object */
static RefNode *
{
/* Could allocate RefNode's in blocks, not sure it would help much */
return NULL;
}
/* Create weak reference to make sure we have a reference */
return NULL;
}
/* Set tag on weakRef */
if ( error != JVMTI_ERROR_NONE ) {
return NULL;
}
/* Fill in RefNode */
/* Count RefNode's created */
return node;
}
static void
{
/* Clear tag */
} else {
}
}
}
/* Change a RefNode to have a strong reference */
static jobject
{
/*
* NewGlobalRef on a weak ref will return NULL if the weak
* reference has been collected or if out of memory.
* We need to distinguish those two occurrences.
*/
}
}
return strongRef;
} else {
}
}
/* Change a RefNode to have a weak reference */
static jweak
{
}
return weakRef;
} else {
}
}
/*
* Returns the node which contains the common reference for the
* given object. The passed reference should not be a weak reference
* managed in the object hash table (i.e. returned by commonRef_idToRef)
* because no sequence number checking is done.
*/
static RefNode *
{
if ( error == JVMTI_ERROR_NONE ) {
return node;
}
return NULL;
}
/* Locate and delete a node based on ID */
static void
{
} else {
}
}
/* Detach from id hash table */
} else {
}
}
break;
}
}
}
/*
* Returns the node stored in the object hash table for the given object
* id. The id should be a value previously returned by
* commonRef_refToID.
*
* NOTE: It is possible that a match is found here, but that the object
* is garbage collected by the time the caller inspects node->ref.
* Callers should take care using the node->ref object returned here.
*
*/
static RefNode *
{
/* Re-order hash list so this one is up front */
}
break;
}
}
return node;
}
/* Initialize the hash table stored in gdata area */
static void
{
/* Size should always be a power of 2 */
gdata->objectsByIDcount = 0;
}
/* hash in a RefNode */
static void
{
/* Add to id hashtable */
}
/* Allocate and add RefNode to hash table */
static RefNode *
{
/* Allocate the node and set it up */
return NULL;
}
/* See if hash table needs expansion */
int oldsize;
int newsize;
int i;
/* Save old information */
/* Allocate new hash table */
/* Walk over old one and hash in all the RefNodes */
for ( i = 0 ; i < oldsize ; i++ ) {
}
}
}
/* Add to id hashtable */
return node;
}
/* Initialize the commonRefs usage */
void
commonRef_initialize(void)
{
}
/* Reset the commonRefs usage */
void
{
int i;
for (i = 0; i < gdata->objectsByIDsize; i++) {
}
}
/* Toss entire hash table and re-create a new one */
}
/*
* Given a reference obtained from JNI or JVMTI, return an object
* id suitable for sending to the debugger front end.
*/
{
return NULL_OBJECT_ID;
}
id = NULL_OBJECT_ID;
}
} else {
}
return id;
}
/*
* Given an object ID obtained from the debugger front end, return a
* strong, global reference to that object (or NULL if the object
* has been collected). The reference can then be used for JNI and
* JVMTI calls. Caller is resposible for deleting the returned reference.
*/
{
} else {
/* Object was GC'd shortly after we found the node */
} else {
}
}
}
return ref;
}
/* Deletes the global reference that commonRef_idToRef() created */
void
{
return;
}
}
/* Prevent garbage collection of an object */
{
if (id == NULL_OBJECT_ID) {
return error;
}
} else {
/*
* Referent has been collected, clean up now.
*/
}
}
return error;
}
/* Permit garbage collection of an object */
{
}
}
return error;
}
/* Release tracking of an object by ID */
void
{
}
void
{
}
/* Get rid of RefNodes for objects that no longer exist */
void
commonRef_compact(void)
{
int i;
if ( gdata->objectsByIDsize > 0 ) {
/*
* Walk through the id-based hash table. Detach any nodes
* for which the ref has been collected.
*/
for (i = 0; i < gdata->objectsByIDsize; i++) {
/* Has the object been collected? */
/* Detach from the ID list */
} else {
}
} else {
}
}
}
}
}
/* Lock the commonRef tables */
void
commonRef_lock(void)
{
}
/* Unlock the commonRef tables */
void
commonRef_unlock(void)
{
}