- // Set up state for marking and allocation of Java objects.
- obj_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
- * sizeof (ptr_t),
- PTRFREE);
- memset (obj_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
-
- proc = GC_n_mark_procs++;
- GC_mark_procs[proc] = (mark_proc) _Jv_MarkObj;
-
- obj_kind_x = GC_n_kinds++;
- GC_obj_kinds[obj_kind_x].ok_freelist = obj_free_list;
- GC_obj_kinds[obj_kind_x].ok_reclaim_list = 0;
- GC_obj_kinds[obj_kind_x].ok_descriptor = MAKE_PROC (proc, 0);
- GC_obj_kinds[obj_kind_x].ok_relocate_descr = FALSE;
- GC_obj_kinds[obj_kind_x].ok_init = TRUE;
-
- // Set up state for marking and allocation of arrays of Java
- // objects.
- array_free_list = (ptr_t *) GC_generic_malloc_inner ((MAXOBJSZ + 1)
- * sizeof (ptr_t),
- PTRFREE);
- memset (array_free_list, 0, (MAXOBJSZ + 1) * sizeof (ptr_t));
-
- proc = GC_n_mark_procs++;
- GC_mark_procs[proc] = (mark_proc) _Jv_MarkArray;
-
- array_kind_x = GC_n_kinds++;
- GC_obj_kinds[array_kind_x].ok_freelist = array_free_list;
- GC_obj_kinds[array_kind_x].ok_reclaim_list = 0;
- GC_obj_kinds[array_kind_x].ok_descriptor = MAKE_PROC (proc, 0);
- GC_obj_kinds[array_kind_x].ok_relocate_descr = FALSE;
- GC_obj_kinds[array_kind_x].ok_init = TRUE;
-
- UNLOCK ();
- ENABLE_SIGNALS ();
+ // We use a different mark procedure for object arrays. This code
+ // configures a different object `kind' for object array allocation and
+ // marking.
+ array_free_list = GC_new_free_list();
+ 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.
+ 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);
+}
+
+#ifdef JV_HASH_SYNCHRONIZATION
+// Allocate an object with a fake vtable pointer, which causes only
+// the first field (beyond the fake vtable pointer) to be traced.
+// Eventually this should probably be generalized.
+
+static _Jv_VTable trace_one_vtable = {
+ 0, // class pointer
+ (void *)(2 * sizeof(void *)),
+ // descriptor; scan 2 words incl. vtable ptr.
+ // Least significant bits must be zero to
+ // identify this as a length descriptor
+ {0} // First method
+};
+
+void *
+_Jv_AllocTraceOne (jsize size /* includes vtable slot */)
+{
+ return GC_GCJ_MALLOC (size, &trace_one_vtable);
+}
+
+// Ditto for two words.
+// the first field (beyond the fake vtable pointer) to be traced.
+// Eventually this should probably be generalized.
+
+static _Jv_VTable trace_two_vtable =
+{
+ 0, // class pointer
+ (void *)(3 * sizeof(void *)),
+ // descriptor; scan 3 words incl. vtable ptr.
+ {0} // First method
+};
+
+void *
+_Jv_AllocTraceTwo (jsize size /* includes vtable slot */)
+{
+ return GC_GCJ_MALLOC (size, &trace_two_vtable);
+}
+
+#endif /* JV_HASH_SYNCHRONIZATION */
+
+void
+_Jv_GCInitializeFinalizers (void (*notifier) (void))
+{
+ GC_finalize_on_demand = 1;
+ GC_finalizer_notifier = notifier;
+}
+
+void
+_Jv_GCRegisterDisappearingLink (jobject *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
+_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