patch to String.intern

Per Bothner per@bothner.com
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 :pserver:anoncvs@anoncvs.cygnus.com:/cvs/kawa login
     (password is ``anoncvs'')
cvs -d :pserver:anoncvs@anoncvs.cygnus.com:/cvs/kawa 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

make

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  <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