This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: String.unintern
- To: Java Patch List <java-patches at sourceware dot cygnus dot com>
- Subject: Patch: String.unintern
- From: Tom Tromey <tromey at redhat dot com>
- Date: 23 Jan 2001 20:34:03 -0700
- Reply-To: tromey at redhat dot com
I've written a patch that, I think, makes uninterning of Strings work.
Last time we talked about this it turned out there were some ways to
make libgcj crash if uninterning was enabled. Does anybody have a
test program that used to do this? I can't make it happen, but that
doesn't mean this patch is correct...
I don't want to check it in until someone else looks at it.
The patch is a bit ugly. Any suggestions?
2001-01-23 Tom Tromey <tromey@redhat.com>
* java/lang/natString.cc (rehash): Don't bother with memset;
_Jv_AllocBytes returns zero'd memory.
(UNMASK_PTR): New macro.
(intern): Unmask pointer before returning it. Register finalizer
for the string.
(unintern): Handle case where
(MASK_PTR): New macro.
(PTR_MAKSED): Likewise.
Tom
Index: java/lang/natString.cc
===================================================================
RCS file: /cvs/gcc/egcs/libjava/java/lang/natString.cc,v
retrieving revision 1.16
diff -u -r1.16 natString.cc
--- natString.cc 2000/12/02 00:28:44 1.16
+++ natString.cc 2001/01/23 23:40:07
@@ -1,6 +1,6 @@
// natString.cc - Implementation of java.lang.String native methods.
-/* Copyright (C) 1998, 1999, 2000 Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation
This file is part of libgcj.
@@ -45,6 +45,10 @@
#define DELETED_STRING ((jstring)(~0))
#define SET_STRING_IS_INTERNED(STR) /* nothing */
+#define UNMASK_PTR(Ptr) (((unsigned long) (Ptr)) & ~0x01)
+#define MASK_PTR(Ptr) (((unsigned long) (Ptr)) | 0x01)
+#define PTR_MAKSED(Ptr) (((unsigned long) (Ptr)) & 0x01)
+
/* Find a slot where the string with elements DATA, length LEN,
and hash HASH should go in the strhash table of interned strings. */
jstring*
@@ -61,17 +65,18 @@
for (;;)
{
jstring* ptr = &strhash[index];
- if (*ptr == NULL)
+ jstring value = (jstring) UNMASK_PTR (*ptr);
+ if (value == NULL)
{
if (deleted_index >= 0)
return (&strhash[deleted_index]);
else
return ptr;
}
- else if (*ptr == DELETED_STRING)
+ else if (value == DELETED_STRING)
deleted_index = index;
- else if ((*ptr)->length() == len
- && memcmp(JvGetStringChars(*ptr), data, 2*len) == 0)
+ else if (value->length() == len
+ && memcmp(JvGetStringChars(value), data, 2*len) == 0)
return (ptr);
index = (index + step) & (strhash_size - 1);
JvAssert (index != start_index);
@@ -116,7 +121,6 @@
{
strhash_size = 1024;
strhash = (jstring *) _Jv_AllocBytes (strhash_size * sizeof (jstring));
- memset (strhash, 0, strhash_size * sizeof (jstring));
}
else
{
@@ -124,7 +128,6 @@
jstring* ptr = strhash + i;
strhash_size *= 2;
strhash = (jstring *) _Jv_AllocBytes (strhash_size * sizeof (jstring));
- memset (strhash, 0, strhash_size * sizeof (jstring));
while (--i >= 0)
{
@@ -158,12 +161,16 @@
rehash();
jstring* ptr = _Jv_StringGetSlot(this);
if (*ptr != NULL && *ptr != DELETED_STRING)
- return *ptr;
+ {
+ // See description in unintern() to understand this.
+ *ptr = (jstring) MASK_PTR (*ptr);
+ return (jstring) UNMASK_PTR (*ptr);
+ }
SET_STRING_IS_INTERNED(this);
strhash_count++;
*ptr = this;
// When string is GC'd, clear the slot in the hash table.
- // _Jv_RegisterFinalizer ((void *) this, unintern);
+ _Jv_RegisterFinalizer ((void *) this, unintern);
return this;
}
@@ -176,8 +183,33 @@
jstring* ptr = _Jv_StringGetSlot(str);
if (*ptr == NULL || *ptr == DELETED_STRING)
return;
- *ptr = DELETED_STRING;
- strhash_count--;
+
+ // We assume the lowest bit of the pointer is free for our nefarious
+ // manipulations. What we do is set it to `0' (implicitly) when
+ // interning the String. If we subsequently re-intern the same
+ // String, then we set the bit. When finalizing, if the bit is set
+ // then we clear it and re-register the finalizer. We know this is
+ // a safe approach because both the intern() and unintern() acquire
+ // the class lock; this bit can't be manipulated when the lock is
+ // not held. So if we are finalizing and the bit is clear then we
+ // know all references are gone and we can clear the entry in the
+ // hash table. The naive approach of simply clearing the pointer
+ // here fails in the case where a request to intern a new string
+ // with the same contents is made between the time the intern()d
+ // string is found to be unreachable and when the finalizer is
+ // actually run. In this case we could clear a pointer to a valid
+ // string, and future intern() calls for that particular value would
+ // spuriously fail.
+ if (PTR_MAKSED (*ptr))
+ {
+ *ptr = (jstring) UNMASK_PTR (*ptr);
+ _Jv_RegisterFinalizer ((void *) obj, unintern);
+ }
+ else
+ {
+ *ptr = DELETED_STRING;
+ strhash_count--;
+ }
}
jstring