This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

patch for gc of interned String


I was having a String mysteriously turn into another String.
I finally noticed that the intern'd string table is allocated
using _Jv_AllocBytes, which means the GC does not search for
pointers in it.  That is ok - but what is not ok was that
when an interned String is collected, there is nothing to
remove it from the intern table - the code that does so is
commented out ...  Ooops.

Does this look like a reasonable fix?  I especially would
like Hans's opinion, of course.  Basically we want the
intern table to be a "weak set", and this seems like a simple
way to do so, but it depends on the cost of finalizers.

        * String.java (unintern):  Remove method.
        * natString.cc (unintern):  Make into a regular static function,
        instead of a method.
        (intern):  Register unintern as a finalizer.

Index: natString.cc
===================================================================
RCS file: /cvs/gcc/gcc/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/03/17 06:55:36
@@ -150,6 +150,19 @@
     }
 }
 
+/* Called by String fake finalizer. */
+static void
+::unintern (jobject obj)
+{
+  JvSynchronize sync (&StringClass);
+  jstring str = reinterpret_cast<jstring> (obj);
+  jstring* ptr = _Jv_StringGetSlot(str);
+  if (*ptr == NULL || *ptr == DELETED_STRING)
+    return;
+  *ptr = DELETED_STRING;
+  strhash_count--;
+}
+
 jstring
 java::lang::String::intern()
 {
@@ -163,21 +176,8 @@
   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;
-}
-
-/* Called by String fake finalizer. */
-void
-java::lang::String::unintern (jobject obj)
-{
-  JvSynchronize sync (&StringClass);
-  jstring str = reinterpret_cast<jstring> (obj);
-  jstring* ptr = _Jv_StringGetSlot(str);
-  if (*ptr == NULL || *ptr == DELETED_STRING)
-    return;
-  *ptr = DELETED_STRING;
-  strhash_count--;
 }
 
 jstring
Index: String.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/String.java,v
retrieving revision 1.12
diff -u -r1.12 String.java
--- String.java	2000/11/18 02:29:13	1.12
+++ String.java	2001/03/17 06:55:36
@@ -347,6 +347,5 @@
   private native void init (byte[] chars, int hibyte, int offset, int count);
   private native void init (byte[] chars, int offset, int count, String enc)
     throws UnsupportedEncodingException;
-  private static native void unintern (Object obj);
   private static native void rehash ();
 }

-- 
	--Per Bothner
per@bothner.com   http://www.bothner.com/~per/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]