patch to String.intern

Per Bothner
Sun Mar 25 17:28:00 GMT 2001

I'd like to check in the below patch soon.  However, Tom said he wanted to
understand why the gc was failing.  I'll be be mostly offline until
Wednesday evening, but if someone want to try to find the bug - or
just try building Kawa as a test application, do the following:

Grab Kawa from cvs:
cvs -d login
     (password is ``anoncvs'')
cvs -d co kawa

Make sure gcj is in your path, and LD_LIBRARY_PATH set as needed.

./configure  --enable-kawa-frontend --enable-xml --prefix /tmp/kawa --with-java-collections --without-awt --without-swing --enable-gcj-compiled


In my experience (on Red Hat Linux 7.0) the build get quite far,
but with the patch below, it fails while Kawa is compiling some
Scheme files.  Applying the patch avoids the problem.  I don't know
making all interned Strings be single-Object fixes the bug, or if
it just makes it less likely, or is just accidental.

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