OSDN Git Service

PR c++/35297
[pf3gnuchains/gcc-fork.git] / libjava / boehm.cc
index c2a93a5..855d23c 100644 (file)
@@ -1,6 +1,7 @@
 // boehm.cc - interface between libjava and Boehm GC.
 
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+   Free Software Foundation
 
    This file is part of libgcj.
 
@@ -10,6 +11,33 @@ details.  */
 
 #include <config.h>
 
+#include <stdio.h>
+#include <limits.h>
+
+#include <jvm.h>
+#include <gcj/cni.h>
+
+#include <java/lang/Class.h>
+#include <java/lang/reflect/Modifier.h>
+#include <java-interp.h>
+
+// More nastiness: the GC wants to define TRUE and FALSE.  We don't
+// need the Java definitions (themselves a hack), so we undefine them.
+#undef TRUE
+#undef FALSE
+
+// We include two autoconf headers. Avoid multiple definition warnings.
+#undef PACKAGE_NAME
+#undef PACKAGE_STRING
+#undef PACKAGE_TARNAME
+#undef PACKAGE_VERSION
+
+#ifdef HAVE_DLFCN_H
+#undef _GNU_SOURCE
+#define _GNU_SOURCE
+#include <dlfcn.h>
+#endif
+
 extern "C"
 {
 #include <gc_config.h>
@@ -27,26 +55,22 @@ extern "C"
 # define GC_REDIRECT_TO_LOCAL
 # include <gc_local_alloc.h>
 #endif
-};
-
-#include <stdio.h>
-#include <limits.h>
-
-#include <jvm.h>
-#include <gcj/cni.h>
 
-#include <java/lang/Class.h>
-#include <java/lang/reflect/Modifier.h>
-#include <java-interp.h>
+  // From boehm's misc.c 
+  void GC_enable();
+  void GC_disable();
+};
 
-#define MAYBE_MARK(Obj, Top, Limit, Source, Exit)  \
-       Top=GC_MARK_AND_PUSH((GC_PTR)Obj, Top, Limit, (GC_PTR *)Source)
+#define MAYBE_MARK(Obj, Top, Limit, Source)  \
+       Top=GC_MARK_AND_PUSH((GC_PTR) Obj, Top, Limit, (GC_PTR *) Source)
 
 // `kind' index used when allocating Java arrays.
 static int array_kind_x;
 
 // Freelist used for Java arrays.
-static void * *array_free_list;
+static void **array_free_list;
+
+static int _Jv_GC_has_static_roots (const char *filename, void *, size_t);
 
 \f
 
@@ -54,7 +78,7 @@ static void * *array_free_list;
 // object.  We use `void *' arguments and return, and not what the
 // Boehm GC wants, to avoid pollution in our headers.
 void *
-_Jv_MarkObj (void *addr, void *msp, void *msl, void * env)
+_Jv_MarkObj (void *addr, void *msp, void *msl, void *env)
 {
   struct GC_ms_entry *mark_stack_ptr = (struct GC_ms_entry *)msp;
   struct GC_ms_entry *mark_stack_limit = (struct GC_ms_entry *)msl;
@@ -75,14 +99,14 @@ _Jv_MarkObj (void *addr, void *msp, void *msl, void * env)
   jclass klass = dt->clas;
   GC_PTR p;
 
+  p = (GC_PTR) dt;
+  MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj);
+
 # ifndef JV_HASH_SYNCHRONIZATION
     // Every object has a sync_info pointer.
     p = (GC_PTR) obj->sync_info;
-    MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o1label);
+    MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj);
 # endif
-  // Mark the object's class.
-  p = (GC_PTR) klass;
-  MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj, o2label);
 
   if (__builtin_expect (klass == &java::lang::Class::class$, false))
     {
@@ -101,152 +125,56 @@ _Jv_MarkObj (void *addr, void *msp, void *msl, void * env)
       jclass c = (jclass) addr;
 
       p = (GC_PTR) c->name;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c3label);
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
       p = (GC_PTR) c->superclass;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c4label);
-      for (int i = 0; i < c->constants.size; ++i)
-       {
-         /* FIXME: We could make this more precise by using the tags -KKT */
-         p = (GC_PTR) c->constants.data[i].p;
-         MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5label);
-       }
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
 
-#ifdef INTERPRETER
-      if (_Jv_IsInterpretedClass (c))
-       {
-         p = (GC_PTR) c->constants.tags;
-         MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5alabel);
-         p = (GC_PTR) c->constants.data;
-         MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5blabel);
-         p = (GC_PTR) c->vtable;
-         MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c5clabel);
-       }
-#endif
+      p = (GC_PTR) c->constants.tags;
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
+      p = (GC_PTR) c->constants.data;
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
 
       // If the class is an array, then the methods field holds a
       // pointer to the element class.  If the class is primitive,
       // then the methods field holds a pointer to the array class.
       p = (GC_PTR) c->methods;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c6label);
-
-      // The vtable might have been set, but the rest of the class
-      // could still be uninitialized.  If this is the case, then
-      // c.isArray will SEGV.  We check for this, and if it is the
-      // case we just return.
-      if (__builtin_expect (c->name == NULL, false))
-       return mark_stack_ptr;
-
-      if (! c->isArray() && ! c->isPrimitive())
-       {
-         // Scan each method in the cases where `methods' really
-         // points to a methods structure.
-         for (int i = 0; i < c->method_count; ++i)
-           {
-             p = (GC_PTR) c->methods[i].name;
-             MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
-                            cm1label);
-             p = (GC_PTR) c->methods[i].signature;
-             MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
-                            cm2label);
-           }
-       }
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
 
-      // Mark all the fields.
       p = (GC_PTR) c->fields;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8label);
-      for (int i = 0; i < c->field_count; ++i)
-       {
-         _Jv_Field* field = &c->fields[i];
-
-#ifndef COMPACT_FIELDS
-         p = (GC_PTR) field->name;
-         MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8alabel);
-#endif
-         p = (GC_PTR) field->type;
-         MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8blabel);
-
-         // For the interpreter, we also need to mark the memory
-         // containing static members
-         if ((field->flags & java::lang::reflect::Modifier::STATIC))
-           {
-             p = (GC_PTR) field->u.addr;
-             MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c8clabel);
-
-             // also, if the static member is a reference,
-             // mark also the value pointed to.  We check for isResolved
-             // since marking can happen before memory is allocated for
-             // static members.
-             if (JvFieldIsRef (field) && field->isResolved()) 
-               {
-                 jobject val = *(jobject*) field->u.addr;
-                 p = (GC_PTR) val;
-                 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
-                             c, c8elabel);
-               }
-           }
-       }
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
 
+      // The vtable might be allocated even for compiled code.
       p = (GC_PTR) c->vtable;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, c9label);
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
+
       p = (GC_PTR) c->interfaces;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cAlabel);
-      for (int i = 0; i < c->interface_count; ++i)
-       {
-         p = (GC_PTR) c->interfaces[i];
-         MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cClabel);
-       }
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
       p = (GC_PTR) c->loader;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cBlabel);
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
+
+      // The dispatch tables can be allocated at runtime.
+      p = (GC_PTR) c->ancestors;
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
+
+      p = (GC_PTR) c->idt;
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
+
       p = (GC_PTR) c->arrayclass;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cDlabel);
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
       p = (GC_PTR) c->protectionDomain;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cPlabel);
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
       p = (GC_PTR) c->hack_signers;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cSlabel);
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
       p = (GC_PTR) c->aux_info;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c, cTlabel);
-
-#ifdef INTERPRETER
-      if (_Jv_IsInterpretedClass (c))
-       {
-         _Jv_InterpClass* ic = (_Jv_InterpClass*) c->aux_info;
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
 
-         p = (GC_PTR) ic->interpreted_methods;
-         MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cElabel);
-
-         for (int i = 0; i < c->method_count; i++)
-           {
-             p = (GC_PTR) ic->interpreted_methods[i];
-             MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
-                         cFlabel);
-
-             // Mark the direct-threaded code.
-             if ((c->methods[i].accflags
-                  & java::lang::reflect::Modifier::NATIVE) == 0)
-               {
-                 _Jv_InterpMethod *im
-                   = (_Jv_InterpMethod *) ic->interpreted_methods[i];
-                 if (im)
-                   {
-                     p = (GC_PTR) im->prepared;
-                     MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, \
-                                 cFlabel);
-                   }
-               }
-
-             // The interpreter installs a heap-allocated trampoline
-             // here, so we'll mark it.
-             p = (GC_PTR) c->methods[i].ncode;
-             MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c,
-                         cm3label);
-           }
-
-         p = (GC_PTR) ic->field_initializers;
-         MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, ic, cGlabel);
-         
-       }
-#endif
+      p = (GC_PTR) c->reflection_data;
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
 
+      // The class chain must be marked for runtime-allocated Classes
+      // loaded by the bootstrap ClassLoader.
+      p = (GC_PTR) c->next_or_version;
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, c);
     }
   else
     {
@@ -269,8 +197,7 @@ _Jv_MarkObj (void *addr, void *msp, void *msl, void * env)
                {
                  jobject val = JvGetObjectField (obj, field);
                  p = (GC_PTR) val;
-                 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit,
-                             obj, elabel);
+                 MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, obj);
                }
              field = field->getNextField ();
            }
@@ -285,7 +212,7 @@ _Jv_MarkObj (void *addr, void *msp, void *msl, void * env)
 // array (of objects).  We use `void *' arguments and return, and not
 // what the Boehm GC wants, to avoid pollution in our headers.
 void *
-_Jv_MarkArray (void *addr, void *msp, void *msl, void * env)
+_Jv_MarkArray (void *addr, void *msp, void *msl, void *env)
 {
   struct GC_ms_entry *mark_stack_ptr = (struct GC_ms_entry *)msp;
   struct GC_ms_entry *mark_stack_limit = (struct GC_ms_entry *)msl;
@@ -300,23 +227,22 @@ _Jv_MarkArray (void *addr, void *msp, void *msl, void * env)
   // we may need to round up the size.
   if (__builtin_expect (! dt || !(dt -> get_finalizer()), false))
     return mark_stack_ptr;
-  jclass klass = dt->clas;
   GC_PTR p;
 
+  p = (GC_PTR) dt;
+  MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array);
+
 # ifndef JV_HASH_SYNCHRONIZATION
     // Every object has a sync_info pointer.
     p = (GC_PTR) array->sync_info;
-    MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e1label);
+    MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array);
 # endif
-  // Mark the object's class.
-  p = (GC_PTR) klass;
-  MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, &(dt -> clas), o2label);
 
   for (int i = 0; i < JvGetArrayLength (array); ++i)
     {
       jobject obj = elements (array)[i];
       p = (GC_PTR) obj;
-      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array, e2label);
+      MAYBE_MARK (p, mark_stack_ptr, mark_stack_limit, array);
     }
 
   return mark_stack_ptr;
@@ -364,7 +290,7 @@ _Jv_BuildGCDescr(jclass self)
              // If we find a field outside the range of our bitmap,
              // fall back to procedure marker. The bottom 2 bits are
              // reserved.
-             if (off >= (unsigned)bits_per_word - 2)
+             if (off >= (unsigned) bits_per_word - 2)
                return (void *) (GCJ_DEFAULT_DESCR);
              desc |= 1ULL << (bits_per_word - off - 1);
            }
@@ -451,9 +377,34 @@ _Jv_AllocArray (jsize size, jclass klass)
 void *
 _Jv_AllocRawObj (jsize size)
 {
-  return (void *) GC_MALLOC (size);
+  return (void *) GC_MALLOC (size ? size : 1);
 }
 
+#ifdef INTERPRETER
+typedef _Jv_ClosureList *closure_list_pointer;
+
+/* Release closures in a _Jv_ClosureList.  */
+static void
+finalize_closure_list (GC_PTR obj, GC_PTR)
+{
+  _Jv_ClosureList **clpp = (_Jv_ClosureList **)obj;
+  _Jv_ClosureList::releaseClosures (clpp);
+}
+
+/* Allocate a double-indirect pointer to a _Jv_ClosureList that will
+   get garbage-collected after this double-indirect pointer becomes
+   unreachable by any other objects, including finalizable ones.  */
+_Jv_ClosureList **
+_Jv_ClosureListFinalizer ()
+{
+  _Jv_ClosureList **clpp;
+  clpp = (_Jv_ClosureList **)_Jv_AllocBytes (sizeof (*clpp));
+  GC_REGISTER_FINALIZER_UNREACHABLE (clpp, finalize_closure_list,
+                                    NULL, NULL, NULL);
+  return clpp;
+}
+#endif // INTERPRETER
+
 static void
 call_finalizer (GC_PTR obj, GC_PTR client_data)
 {
@@ -514,9 +465,11 @@ _Jv_GCSetMaximumHeapSize (size_t size)
   GC_set_max_heap_size ((GC_word) size);
 }
 
-// From boehm's misc.c 
-extern "C" void GC_enable();
-extern "C" void GC_disable();
+int
+_Jv_SetGCFreeSpaceDivisor (int div)
+{
+  return (int)GC_set_free_space_divisor ((GC_word)div);
+}
 
 void
 _Jv_DisableGC (void)
@@ -562,10 +515,21 @@ void
 _Jv_InitGC (void)
 {
   int proc;
+  static bool gc_initialized;
+
+  if (gc_initialized)
+    return;
+
+  gc_initialized = 1;
 
   // Ignore pointers that do not point to the start of an object.
   GC_all_interior_pointers = 0;
 
+#if defined (HAVE_DLFCN_H) && defined (HAVE_DLADDR)
+  // Tell the collector to ask us before scanning DSOs.
+  GC_register_has_static_roots_callback (_Jv_GC_has_static_roots);
+#endif
+
   // Configure the collector to use the bitmap marking descriptors that we
   // stash in the class vtable.
   // We always use mark proc descriptor 0, since the compiler knows
@@ -585,7 +549,7 @@ _Jv_InitGC (void)
   proc = GC_new_proc((GC_mark_proc)_Jv_MarkArray);
   array_kind_x = GC_new_kind(array_free_list, GC_MAKE_PROC (proc, 0), 0, 1);
 
-  /* Arrange to have the GC print Java class names in backtraces, etc.         */
+  // Arrange to have the GC print Java class names in backtraces, etc.
   GC_register_describe_type_fn(GC_gcj_kind, gcj_describe_type_fn);
   GC_register_describe_type_fn(GC_gcj_debug_kind, gcj_describe_type_fn);
 }
@@ -640,7 +604,11 @@ _Jv_GCInitializeFinalizers (void (*notifier) (void))
 void
 _Jv_GCRegisterDisappearingLink (jobject *objp)
 {
-  GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp);
+  // This test helps to ensure that we meet a precondition of
+  // GC_general_register_disappearing_link, viz. "Obj must be a
+  // pointer to the first word of an object we allocated."
+  if (GC_base(*objp))
+    GC_general_register_disappearing_link ((GC_PTR *) objp, (GC_PTR) *objp);
 }
 
 jboolean
@@ -649,3 +617,145 @@ _Jv_GCCanReclaimSoftReference (jobject)
   // For now, always reclaim soft references.  FIXME.
   return true;
 }
+
+\f
+
+#if defined (HAVE_DLFCN_H) && defined (HAVE_DLADDR)
+
+// We keep a store of the filenames of DSOs that need to be
+// conservatively scanned by the garbage collector.  During collection
+// the gc calls _Jv_GC_has_static_roots() to see if the data segment
+// of a DSO should be scanned.
+typedef struct filename_node
+{
+  char *name;
+  struct filename_node *link;
+} filename_node;
+
+#define FILENAME_STORE_SIZE 17
+static filename_node *filename_store[FILENAME_STORE_SIZE];
+
+// Find a filename in filename_store.
+static filename_node **
+find_file (const char *filename)
+{
+  int index = strlen (filename) % FILENAME_STORE_SIZE;
+  filename_node **node = &filename_store[index];
+  
+  while (*node)
+    {
+      if (strcmp ((*node)->name, filename) == 0)
+       return node;
+      node = &(*node)->link;
+    }
+
+  return node;
+}  
+
+// Print the store of filenames of DSOs that need collection.
+void
+_Jv_print_gc_store (void)
+{
+  for (int i = 0; i < FILENAME_STORE_SIZE; i++)
+    {
+      filename_node *node = filename_store[i];
+      while (node)
+       {
+         fprintf (stderr, "%s\n", node->name);
+         node = node->link;
+       }
+    }
+}
+
+// Create a new node in the store of libraries to collect.
+static filename_node *
+new_node (const char *filename)
+{
+  filename_node *node = (filename_node*)_Jv_Malloc (sizeof (filename_node));
+  node->name = (char *)_Jv_Malloc (strlen (filename) + 1);
+  node->link = NULL;
+  strcpy (node->name, filename);
+  
+  return node;
+}
+
+// Nonzero if the gc should scan this lib.
+static int 
+_Jv_GC_has_static_roots (const char *filename, void *, size_t)
+{
+  if (filename == NULL || strlen (filename) == 0)
+    // No filename; better safe than sorry.
+    return 1;
+
+  filename_node **node = find_file (filename);
+  if (*node)
+    return 1;
+
+  return 0;
+}
+
+#endif
+
+// Register the DSO that contains p for collection.
+void
+_Jv_RegisterLibForGc (const void *p __attribute__ ((__unused__)))
+{
+#if defined (HAVE_DLFCN_H) && defined (HAVE_DLADDR)
+  Dl_info info;
+
+  if (dladdr (const_cast<void *>(p), &info) != 0)
+    {
+      filename_node **node = find_file (info.dli_fname);
+      if (! *node)
+       *node = new_node (info.dli_fname);
+    }
+#endif
+}
+
+void
+_Jv_SuspendThread (_Jv_Thread_t *thread)
+{
+#if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
+     && !defined(GC_WIN32_THREADS) && !defined(GC_DARWIN_THREADS)
+  GC_suspend_thread (_Jv_GetPlatformThreadID (thread));
+#endif
+}
+
+void
+_Jv_ResumeThread (_Jv_Thread_t *thread)
+{
+#if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
+     && !defined(GC_WIN32_THREADS) && !defined(GC_DARWIN_THREADS)
+  GC_resume_thread (_Jv_GetPlatformThreadID (thread));
+#endif
+}
+
+int
+_Jv_IsThreadSuspended (_Jv_Thread_t *thread)
+{
+#if defined(GC_PTHREADS) && !defined(GC_SOLARIS_THREADS) \
+     && !defined(GC_WIN32_THREADS) && !defined(GC_DARWIN_THREADS)
+  return GC_is_thread_suspended (_Jv_GetPlatformThreadID (thread));
+#else
+  return 0;
+#endif
+}
+
+void
+_Jv_GCAttachThread ()
+{
+  // The registration interface is only defined on posixy systems and
+  // only actually works if pthread_getattr_np is defined.
+  // FIXME: until gc7 it is simpler to disable this on solaris.
+#if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(GC_SOLARIS_THREADS)
+  GC_register_my_thread ();
+#endif
+}
+
+void
+_Jv_GCDetachThread ()
+{
+#if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(GC_SOLARIS_THREADS)
+  GC_unregister_my_thread ();
+#endif
+}