hash tables have a tc7
[bpt/guile.git] / libguile / gc.c
index 6d65f9c..4bedb05 100644 (file)
@@ -1,25 +1,24 @@
-/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006, 2008, 2009 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This library 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
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
  */
 
-#define _GNU_SOURCE
-
 /* #define DEBUGINFO */
 
-#if HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
@@ -41,7 +40,7 @@ extern unsigned long * __libc_ia64_register_backing_store_base;
 #include "libguile/stackchk.h"
 #include "libguile/struct.h"
 #include "libguile/smob.h"
-#include "libguile/unif.h"
+#include "libguile/arrays.h"
 #include "libguile/async.h"
 #include "libguile/ports.h"
 #include "libguile/root.h"
@@ -57,7 +56,7 @@ extern unsigned long * __libc_ia64_register_backing_store_base;
 #include "libguile/gc.h"
 #include "libguile/dynwind.h"
 
-#include "libguile/boehm-gc.h"
+#include "libguile/bdw-gc.h"
 
 #ifdef GUILE_DEBUG_MALLOC
 #include "libguile/debug-malloc.h"
@@ -108,13 +107,6 @@ int scm_i_cell_validation_already_running ;
 void
 scm_i_expensive_validation_check (SCM cell)
 {
-  if (!scm_in_heap_p (cell))
-    {
-      fprintf (stderr, "scm_assert_cell_valid: this object does not live in the heap: %lux\n",
-              (unsigned long) SCM_UNPACK (cell));
-      abort ();
-    }
-
   /* If desired, perform additional garbage collections after a user
    * defined number of cell accesses.
    */
@@ -154,16 +146,6 @@ scm_assert_cell_valid (SCM cell)
       */
       if (scm_expensive_debug_cell_accesses_p)
        scm_i_expensive_validation_check (cell);
-      
-      if (!SCM_GC_MARK_P (cell))
-       {
-         fprintf (stderr,
-                  "scm_assert_cell_valid: this object is unmarked. \n"
-                  "It has been garbage-collected in the last GC run: "
-                  "%lux\n",
-                   (unsigned long) SCM_UNPACK (cell));
-         abort ();
-       }
 
       scm_i_cell_validation_already_running = 0;  /* re-enable */
     }
@@ -215,17 +197,10 @@ scm_t_c_hook scm_after_sweep_c_hook;
 scm_t_c_hook scm_after_gc_c_hook;
 
 
-/* scm_mtrigger
- * is the number of bytes of malloc allocation needed to trigger gc.
- */
-unsigned long scm_mtrigger;
-
 /* GC Statistics Keeping
  */
-unsigned long scm_mallocated = 0;
 unsigned long scm_gc_ports_collected = 0;
 
-
 static unsigned long protected_obj_count = 0;
 
 
@@ -240,6 +215,7 @@ SCM_SYMBOL (sym_gc_time_taken, "gc-time-taken");
 SCM_SYMBOL (sym_gc_mark_time_taken, "gc-mark-time-taken");
 SCM_SYMBOL (sym_times, "gc-times");
 SCM_SYMBOL (sym_cells_marked, "cells-marked");
+SCM_SYMBOL (sym_cells_marked_conservatively, "cells-marked-conservatively");
 SCM_SYMBOL (sym_cells_swept, "cells-swept");
 SCM_SYMBOL (sym_malloc_yield, "malloc-yield");
 SCM_SYMBOL (sym_cell_yield, "cell-yield");
@@ -327,7 +303,7 @@ SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
                scm_cons (sym_cells_swept,
                          scm_from_double (local_scm_gc_cells_swept)),
                scm_cons (sym_malloc_yield,
-                         scm_from_long(local_scm_gc_malloc_yield_percentage)),
+                         scm_from_long (local_scm_gc_malloc_yield_percentage)),
                scm_cons (sym_cell_yield,
                          scm_from_long (local_scm_gc_cell_yield_percentage)),
                scm_cons (sym_heap_segments, heap_segs),
@@ -346,6 +322,17 @@ SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
 #undef FUNC_NAME
 
 
+SCM_DEFINE (scm_gc_dump, "gc-dump", 0, 0, 0,
+           (void),
+           "Dump information about the garbage collector's internal data "
+           "structures and memory usage to the standard output.")
+#define FUNC_NAME s_scm_gc_dump
+{
+  GC_dump ();
+
+  return SCM_UNSPECIFIED;
+}
+#undef FUNC_NAME
 
 
 SCM_DEFINE (scm_object_address, "object-address", 1, 0, 0,
@@ -389,7 +376,6 @@ SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
 #define FUNC_NAME s_scm_gc
 {
   scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
-  scm_gc_running_p = 1;
   scm_i_gc ("call");
   /* njrev: It looks as though other places, e.g. scm_realloc,
      can call scm_i_gc without acquiring the sweep mutex.  Does this
@@ -398,7 +384,6 @@ SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
      (e.g. scm_permobjs above in scm_gc_stats) by a critical section,
      not by the sweep mutex.  Shouldn't all the GC-relevant objects be
      protected in the same way? */
-  scm_gc_running_p = 0;
   scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
   scm_c_hook_run (&scm_after_gc_c_hook, 0);
   return SCM_UNSPECIFIED;
@@ -505,7 +490,7 @@ scm_permanent_object (SCM obj)
 */
 
 /* Implementation note:  For every object X, there is a counter which
-   scm_gc_protect_object(X) increments and scm_gc_unprotect_object(X) decrements.
+   scm_gc_protect_object (X) increments and scm_gc_unprotect_object (X) decrements.
 */
 
 
@@ -628,10 +613,12 @@ void
 scm_storage_prehistory ()
 {
   GC_all_interior_pointers = 0;
+  GC_set_free_space_divisor (scm_getenv_int ("GC_FREE_SPACE_DIVISOR", 3));
 
   GC_INIT ();
 
-#ifdef SCM_I_GSC_USE_PTHREAD_THREADS
+#if (! ((defined GC_VERSION_MAJOR) && (GC_VERSION_MAJOR >= 7))) \
+    && (defined SCM_I_GSC_USE_PTHREAD_THREADS)
   /* When using GC 6.8, this call is required to initialize thread-local
      freelists (shouldn't be necessary with GC 7.0).  */
   GC_init ();
@@ -644,7 +631,7 @@ scm_storage_prehistory ()
      pointer to an 8-octet aligned region).  For `scm_tc3_struct', this is
      handled in `scm_alloc_struct ()'.  */
   GC_REGISTER_DISPLACEMENT (scm_tc3_cons);
-  GC_REGISTER_DISPLACEMENT (scm_tc3_closure);
+  /* GC_REGISTER_DISPLACEMENT (scm_tc3_unused); */
 
   /* Sanity check.  */
   if (!GC_is_visible (scm_sys_protects))
@@ -668,8 +655,6 @@ scm_init_storage ()
   while (j)
     scm_sys_protects[--j] = SCM_BOOL_F;
 
-  j = SCM_HEAP_SEG_SIZE;
-
 #if 0
   /* We can't have a cleanup handler since we have no thread to run it
      in. */
@@ -684,7 +669,6 @@ scm_init_storage ()
 
 #endif
 
-  scm_stand_in_procs = scm_make_weak_key_hash_table (scm_from_int (257));
   scm_protects = scm_c_make_hash_table (31);
 
   return 0;
@@ -715,7 +699,7 @@ gc_async_thunk (void)
  */
 static void *
 mark_gc_async (void * hook_data SCM_UNUSED,
-              void *func_data SCM_UNUSED,
+              void *fn_data SCM_UNUSED,
               void *data SCM_UNUSED)
 {
   /* If cell access debugging is enabled, the user may choose to perform
@@ -758,15 +742,10 @@ scm_i_tag_name (scm_t_bits tag)
 {
   if (tag >= 255)
     {
-      if (tag == scm_tc_free_cell)
-       return "free cell";
-
-      {
-       int k = 0xff & (tag >> 8);
-       return (scm_smobs[k].name);
-      }
+      int k = 0xff & (tag >> 8);
+      return (scm_smobs[k].name);
     }
-  
+
   switch (tag) /* 7 bits */
     {
     case scm_tcs_struct:
@@ -775,18 +754,14 @@ scm_i_tag_name (scm_t_bits tag)
       return "cons (immediate car)";
     case scm_tcs_cons_nimcar:
       return "cons (non-immediate car)";
-    case scm_tcs_closures:
-      return "closures";
     case scm_tc7_pws:
       return "pws";
+    case scm_tc7_hashtable:
+      return "hashtable";
     case scm_tc7_wvect:
       return "weak vector";
     case scm_tc7_vector:
       return "vector";
-#ifdef CCLO
-    case scm_tc7_cclo:
-      return "compiled closure";
-#endif
     case scm_tc7_number:
       switch (tag)
        {
@@ -816,8 +791,8 @@ scm_i_tag_name (scm_t_bits tag)
     case scm_tc7_variable:
       return "variable";
       break;
-    case scm_tcs_subrs:
-      return "subrs";
+    case scm_tc7_gsubr:
+      return "gsubr";
       break;
     case scm_tc7_port:
       return "port";
@@ -841,8 +816,7 @@ scm_init_gc ()
   scm_after_gc_hook = scm_permanent_object (scm_make_hook (SCM_INUM0));
   scm_c_define ("after-gc-hook", scm_after_gc_hook);
 
-  gc_async = scm_c_make_subr ("%gc-thunk", scm_tc7_subr_0,
-                             gc_async_thunk);
+  gc_async = scm_c_make_gsubr ("%gc-thunk", 0, 0, 0, gc_async_thunk);
 
   scm_c_hook_add (&scm_after_gc_c_hook, mark_gc_async, NULL, 0);