patch to String.intern

Per Bothner
Tue Mar 20 23:04:00 GMT 2001

I got a crash in (I think) _Jv_StringFindSlot involving a string that
had a data field pointing to a separate char[].  I think the latter
had been zero'd, presumably by some gc bug.  I didn't really track down
the problem, but it seems to be it makes sense that intern'd strings
should all be single-object strings (i.e. one whose data field is this).
(This came up in the discussion of saving a String's hashCode.)  I
implemented this patch, and the problem seems to have gone away.

I also changed unintern from a method to a static function.  That
seems much cleaner, and we don't get into the confusing issues
of C++ pointers-to-member.

Ok to apply?

2001-03-20  Per Bothner  <>

	* java/lang/ (intern):  If string's data does not point to
	this String, make a fresh String that does.

	* java/lang/ (unintern):  Replace method by static function.
	* java/lang/ (unintern):  Remove method.

RCS file: /cvs/gcc/gcc/libjava/java/lang/,v
retrieving revision
diff -u -r1.16.4.1
---	2001/03/18 00:52:31
+++	2001/03/21 06:52:06
@@ -29,6 +29,7 @@
 #include <gnu/gcj/convert/BytesToUnicode.h>
 #include <jvm.h>
+static void unintern (jobject);
 static jstring* strhash = NULL;
 static int strhash_count = 0;  /* Number of slots used in strhash. */
 static int strhash_size = 0;  /* Number of slots available in strhash.
@@ -174,17 +175,19 @@
       *ptr = (jstring) MASK_PTR (*ptr);
       return (jstring) UNMASK_PTR (*ptr);
+  jstring str = this->data == this ? this
+    : _Jv_NewString(JvGetStringChars(this), this->length());
-  *ptr = this;
+  *ptr = str;
   // When string is GC'd, clear the slot in the hash table.
-  _Jv_RegisterFinalizer ((void *) this, unintern);
-  return this;
+  _Jv_RegisterFinalizer ((void *) str, unintern);
+  return str;
 /* Called by String fake finalizer. */
-java::lang::String::unintern (jobject obj)
+static void
+unintern (jobject obj)
   JvSynchronize sync (&StringClass);
   jstring str = reinterpret_cast<jstring> (obj);
RCS file: /cvs/gcc/gcc/libjava/java/lang/,v
retrieving revision 1.12
diff -u -r1.12
---	2000/11/18 02:29:13	1.12
+++	2001/03/21 06:52:06
@@ -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

More information about the Java-patches mailing list