patch to String.intern

Per Bothner per@bothner.com
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  <per@bothner.com>

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

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

Index: natString.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/lang/natString.cc,v
retrieving revision 1.16.4.1
diff -u -r1.16.4.1 natString.cc
--- natString.cc	2001/03/18 00:52:31	1.16.4.1
+++ natString.cc	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);
     }
-  SET_STRING_IS_INTERNED(this);
+  jstring str = this->data == this ? this
+    : _Jv_NewString(JvGetStringChars(this), this->length());
+  SET_STRING_IS_INTERNED(str);
   strhash_count++;
-  *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. */
-void
-java::lang::String::unintern (jobject obj)
+static void
+unintern (jobject obj)
 {
   JvSynchronize sync (&StringClass);
   jstring str = reinterpret_cast<jstring> (obj);
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/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
per@bothner.com   http://www.bothner.com/~per/



More information about the Java-patches mailing list