JNI global refs

Tom Tromey tromey@redhat.com
Thu Jul 26 10:11:00 GMT 2001


>>>>> "Jeff" == Jeff Sturm <jsturm@one-point.com> writes:

[ JNI problem ]

Jeff> I suspect the problem is with using a Hashtable.

I wrote a patch for this problem.

Marcus, could you test this?  I haven't reproduced the problem you
were seeing.  With this patch I would expect the problem to go away.

This patch isn't heavily tested.  I ran it through our JNI test suite,
but that barely tests anything.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>

	* jni.cc: Include IdentityHashMap.h, not Hashtable.h.
	(local_ref_table, global_ref_table): Now IdentityHashMap.
	(_Jv_JNI_Init): Updated for new types.
	(mark_for_gc): Likewise.
	(unmark_for_gc): Likewise.

	* gcj/javaprims.h: Rebuilt class list.
	* Makefile.in: Rebuilt.
	* Makefile.am (core_java_source_files): Added new file.
	* java/util/IdentityHashMap.java: New file.

Index: Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.159
diff -u -r1.159 Makefile.am
--- Makefile.am 2001/07/25 16:40:04 1.159
+++ Makefile.am 2001/07/26 16:55:09
@@ -947,6 +947,7 @@
 java/util/HashMap.java \
 java/util/HashSet.java \
 java/util/Hashtable.java \
+java/util/IdentityHashMap.java \
 java/util/Iterator.java	\
 java/util/LinkedList.java \
 java/util/List.java \
Index: Makefile.in
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.in,v
retrieving revision 1.170
diff -u -r1.170 Makefile.in
--- Makefile.in 2001/07/25 16:40:04 1.170
+++ Makefile.in 2001/07/26 16:55:11
@@ -717,6 +717,7 @@
 java/util/HashMap.java \
 java/util/HashSet.java \
 java/util/Hashtable.java \
+java/util/IdentityHashMap.java \
 java/util/Iterator.java	\
 java/util/LinkedList.java \
 java/util/List.java \
@@ -1859,10 +1860,11 @@
 .deps/java/util/EventListener.P .deps/java/util/EventObject.P \
 .deps/java/util/GregorianCalendar.P .deps/java/util/HashMap.P \
 .deps/java/util/HashSet.P .deps/java/util/Hashtable.P \
-.deps/java/util/Iterator.P .deps/java/util/LinkedList.P \
-.deps/java/util/List.P .deps/java/util/ListIterator.P \
-.deps/java/util/ListResourceBundle.P .deps/java/util/Locale.P \
-.deps/java/util/Map.P .deps/java/util/MissingResourceException.P \
+.deps/java/util/IdentityHashMap.P .deps/java/util/Iterator.P \
+.deps/java/util/LinkedList.P .deps/java/util/List.P \
+.deps/java/util/ListIterator.P .deps/java/util/ListResourceBundle.P \
+.deps/java/util/Locale.P .deps/java/util/Map.P \
+.deps/java/util/MissingResourceException.P \
 .deps/java/util/NoSuchElementException.P .deps/java/util/Observable.P \
 .deps/java/util/Observer.P .deps/java/util/Properties.P \
 .deps/java/util/PropertyPermission.P \
Index: jni.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/jni.cc,v
retrieving revision 1.47
diff -u -r1.47 jni.cc
--- jni.cc 2001/06/15 23:44:45 1.47
+++ jni.cc 2001/07/26 16:55:13
@@ -37,7 +37,7 @@
 #include <java/lang/reflect/Method.h>
 #include <java/lang/reflect/Modifier.h>
 #include <java/lang/OutOfMemoryError.h>
-#include <java/util/Hashtable.h>
+#include <java/util/IdentityHashMap.h>
 #include <java/lang/Integer.h>
 #include <java/lang/ThreadGroup.h>
 #include <java/lang/Thread.h>
@@ -91,9 +91,9 @@
 };
 
 // This holds a reference count for all local references.
-static java::util::Hashtable *local_ref_table;
+static java::util::IdentityHashMap *local_ref_table;
 // This holds a reference count for all global references.
-static java::util::Hashtable *global_ref_table;
+static java::util::IdentityHashMap *global_ref_table;
 
 // The only VM.
 static JavaVM *the_vm;
@@ -148,8 +148,8 @@
 void
 _Jv_JNI_Init (void)
 {
-  local_ref_table = new java::util::Hashtable;
-  global_ref_table = new java::util::Hashtable;
+  local_ref_table = new java::util::IdentityHashMap;
+  global_ref_table = new java::util::IdentityHashMap;
 
 #ifdef ENABLE_JVMPI
   _Jv_JVMPI_Interface.version = 1;
@@ -163,7 +163,7 @@
 
 // Tell the GC that a certain pointer is live.
 static void
-mark_for_gc (jobject obj, java::util::Hashtable *ref_table)
+mark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
 {
   JvSynchronize sync (ref_table);
 
@@ -176,7 +176,7 @@
 
 // Unmark a pointer.
 static void
-unmark_for_gc (jobject obj, java::util::Hashtable *ref_table)
+unmark_for_gc (jobject obj, java::util::IdentityHashMap *ref_table)
 {
   JvSynchronize sync (ref_table);
 
Index: java/util/IdentityHashMap.java
===================================================================
RCS file: IdentityHashMap.java
diff -N IdentityHashMap.java
--- /dev/null	Tue May  5 13:32:27 1998
+++ java/util/IdentityHashMap.java Thu Jul 26 09:55:13 2001
@@ -0,0 +1,402 @@
+/* IdentityHashMap.java -- a class providing a hashtable data structure,
+   mapping Object --> Object, which uses object identity for hashing.
+   Copyright (C) 2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+As a special exception, if you link this library with other files to
+produce an executable, this library does not by itself cause the
+resulting executable to be covered by the GNU General Public License.
+This exception does not however invalidate any other reasons why the
+executable file might be covered by the GNU General Public License. */
+
+package java.util;
+
+import java.io.*;
+
+/**
+ * This class provides a hashtable-backed implementation of the
+ * Map interface.  Unlike HashMap, it uses object identity to
+ * do its hashing.  Also, it uses a linear-probe hash table.
+ *
+ * @author Tom Tromey <tromey@redhat.com>
+ */
+
+public class IdentityHashMap extends AbstractMap
+  implements Map, Serializable, Cloneable
+{
+  private static final int DEFAULT_CAPACITY = 21;
+
+  public IdentityHashMap ()
+  {
+    this (DEFAULT_CAPACITY);
+  }
+
+  public IdentityHashMap (int max)
+  {
+    if (max < 0)
+      throw new IllegalArgumentException ();
+    table = new Object[2 * max];
+    Arrays.fill (table, emptyslot);
+    size = 0;
+  }
+
+  public IdentityHashMap (Map m)
+  {
+    int len = 2 * Math.max (m.size (), DEFAULT_CAPACITY);
+    table = new Object[len];
+    Arrays.fill (table, emptyslot);
+    putAll (m);
+  }
+
+  public void clear ()
+  {
+    Arrays.fill (table, emptyslot);
+    size = 0;
+  }
+
+  public Object clone ()
+  {
+    IdentityHashMap copy = (IdentityHashMap) super.clone ();
+    copy.table = (Object[]) table.clone ();
+    return copy;
+  }
+
+  public boolean containsKey (Object key)
+  {
+    int h = Math.abs (2 * System.identityHashCode (key) % table.length);
+    int save = h;
+    while (true)
+      {
+	if (table[h] == key)
+	  return true;
+	if (table[h] == emptyslot)
+	  return false;
+	h += 2;
+	if (h > table.length)
+	  h = 0;
+	if (h == save)
+	  return false;
+      }
+  }
+
+  public boolean containsValue (Object value)
+  {
+    for (int i = 1; i < table.length; i += 2)
+      if (table[i] == value)
+	return true;
+    return false;
+  }
+
+  public Set entrySet ()
+  {
+    return new AbstractSet ()
+    {
+      public int size ()
+      {
+	return size;
+      }
+
+      public Iterator iterator ()
+      {
+	return new IdentityIterator (IdentityIterator.ENTRIES);
+      }
+
+      public void clear ()
+      {
+	IdentityHashMap.this.clear ();
+      }
+
+      public boolean contains (Object o)
+      {
+	if (! (o instanceof Map.Entry))
+	  return false;
+	Map.Entry m = (Map.Entry) o;
+	return (IdentityHashMap.this.containsKey (m.getKey ())
+		&& IdentityHashMap.this.get (m.getKey ()) == m.getValue ());
+      }
+
+      public boolean remove (Object o)
+      {
+	if (! (o instanceof Map.Entry))
+	  return false;
+	Map.Entry m = (Map.Entry) o;
+	if (IdentityHashMap.this.containsKey (m.getKey ())
+	    && IdentityHashMap.this.get (m.getKey ()) == m.getValue ())
+	  {
+	    int oldsize = size;
+	    IdentityHashMap.this.remove (m.getKey ());
+	    return oldsize != size;
+	  }
+	return false;
+      }
+    };
+  }
+
+  public Object get (Object key)
+  {
+    int h = Math.abs (2 * System.identityHashCode (key) % table.length);
+    int save = h;
+    while (true)
+      {
+	if (table[h] == key)
+	  return table[h + 1];
+	if (table[h] == emptyslot)
+	  return null;
+	h += 2;
+	if (h > table.length)
+	  h = 0;
+	if (h == save)
+	  return null;
+      }
+  }
+
+  public boolean isEmpty ()
+  {
+    return size == 0;
+  }
+
+  public Set keySet ()
+  {
+    return new AbstractSet ()
+    {
+      public int size ()
+      {
+	return size;
+      }
+
+      public Iterator iterator ()
+      {
+	return new IdentityIterator (IdentityIterator.KEYS);
+      }
+
+      public void clear ()
+      {
+	IdentityHashMap.this.clear ();
+      }
+
+      public boolean contains (Object o)
+      {
+	return IdentityHashMap.this.containsKey (o);
+      }
+
+      public boolean remove (Object o)
+      {
+	int oldsize = size;
+	IdentityHashMap.this.remove (o);
+	return oldsize != size;
+      }
+    };
+  }
+
+  public Object put (Object key, Object value)
+  {
+    // Rehash is the load factor is too high.
+    if (size * 3 / 2 > table.length)
+      {
+	Object[] old = table;
+	table = new Object[old.length * 2];
+	Arrays.fill (table, emptyslot);
+	size = 0;
+	for (int i = 0; i < old.length; ++i)
+	  {
+	    if (old[i] != tombstone && old[i] != emptyslot)
+	      {
+		// Just use put.  This isn't very efficient, but it is
+		// ok.
+		put (old[i], old[i + 1]);
+	      }
+	  }
+      }
+
+    int h = Math.abs (2 * System.identityHashCode (key) % table.length);
+    int save = h;
+    int del = -1;
+    while (true)
+      {
+	if (table[h] == key)
+	  {
+	    Object r = table[h + 1];
+	    table[h + 1] = value;
+	    return r;
+	  }
+	else if (table[h] == tombstone && del == -1)
+	  del = h;
+	else if (table[h] == emptyslot)
+	  {
+	    if (del == -1)
+	      del = h;
+	    break;
+	  }
+	h += 2;
+	if (h > table.length)
+	  h = 0;
+	if (h == save)
+	  break;
+      }
+
+    if (del != -1)
+      {
+	table[del] = key;
+	table[del + 1] = value;
+	++size;
+	return null;
+      }
+
+    // This is an error.
+    return null;
+  }
+
+  public Object remove (Object key)
+  {
+    int h = Math.abs (2 * System.identityHashCode (key) % table.length);
+    int save = h;
+    while (true)
+      {
+	if (table[h] == key)
+	  {
+	    Object r = table[h + 1];
+	    table[h] = tombstone;
+	    table[h + 1] = tombstone;
+	    --size;
+	    return r;
+	  }
+	h += 2;
+	if (h > table.length)
+	  h = 0;
+	if (h == save)
+	  break;
+      }
+
+    return null;
+  }
+
+  public int size ()
+  {
+    return size;
+  }
+
+  public Collection values ()
+  {
+    return new AbstractCollection ()
+    {
+      public int size ()
+      {
+	return size;
+      }
+
+      public Iterator iterator ()
+      {
+	return new IdentityIterator (IdentityIterator.VALUES);
+      }
+
+      public void clear ()
+      {
+	IdentityHashMap.this.clear ();
+      }
+    };
+  }
+
+  private class IdentityIterator implements Iterator
+  {
+    static final int KEYS = 0;
+    static final int VALUES = 1;
+    static final int ENTRIES = 2;
+
+    // Type of iterator.
+    int type;
+    // Location in the table.
+    int loc;
+    // How many items we've seen.
+    int seen;
+
+    IdentityIterator (int type)
+    {
+      this.type = type;
+      loc = 0;
+      seen = 0;
+    }
+
+    public boolean hasNext ()
+    {
+      return seen < size;
+    }
+
+    public Object next ()
+    {
+      while (true)
+	{
+	  loc += 2;
+	  if (loc >= table.length)
+	    throw new NoSuchElementException ();
+	  if (table[loc] != tombstone && table[loc] != emptyslot)
+	    {
+	      ++seen;
+	      return table[loc];
+	    }
+	}
+    }
+
+    public void remove ()
+    {
+      if (loc >= table.length
+	  || table[loc] == tombstone
+	  || table[loc] == emptyslot)
+	throw new IllegalStateException ();
+      table[loc] = tombstone;
+      table[loc + 1] = tombstone;
+      --size;
+    }
+  }
+
+  private void readObject (ObjectInputStream s)
+    throws IOException, ClassNotFoundException
+  {
+    int num = s.readInt ();
+    for (int i = 0; i < num; ++i)
+      {
+	Object key = s.readObject ();
+	Object value = s.readObject ();
+	put (key, value);
+      }
+  }
+
+  private void writeObject (ObjectOutputStream s)
+    throws IOException
+  {
+    s.writeInt (size);
+    Iterator it = entrySet ().iterator ();
+    while (it.hasNext ())
+      {
+	Map.Entry entry = (Map.Entry) it.next ();
+	s.writeObject (entry.getKey ());
+	s.writeObject (entry.getValue ());
+      }
+  }
+
+  // Number of items in hash table.
+  private int size;
+  // The table itself.
+  private Object[] table;
+
+  // This object is used to mark deleted items.
+  private Object tombstone = new Object ();
+  // This object is used to mark empty slots.  We need this because
+  // using null is ambiguous.
+  private Object emptyslot = new Object ();
+}
Index: gcj/javaprims.h
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gcj/javaprims.h,v
retrieving revision 1.23
diff -u -r1.23 javaprims.h
--- gcj/javaprims.h 2001/07/23 20:01:29 1.23
+++ gcj/javaprims.h 2001/07/26 16:55:13
@@ -280,6 +280,8 @@
       class Hashtable$Entry;
       class Hashtable$Enumerator;
       class Hashtable$HashIterator;
+      class IdentityHashMap;
+      class IdentityHashMap$IdentityIterator;
       class Iterator;
       class LinkedList;
       class LinkedList$Entry;



More information about the Java-patches mailing list