Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / gc-malloc.c
index b909e94..19d6878 100644 (file)
@@ -1,23 +1,24 @@
-/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2008 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
  */
 
 
 \f
-#if HAVE_CONFIG_H
+#ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
@@ -84,8 +85,8 @@ scm_gc_init_malloc (void)
 {
   scm_mtrigger = scm_getenv_int ("GUILE_INIT_MALLOC_LIMIT",
                                 SCM_DEFAULT_INIT_MALLOC_LIMIT);
-  scm_i_minyield_malloc =  scm_getenv_int ("GUILE_MIN_YIELD_MALLOC",
-                                          SCM_DEFAULT_MALLOC_MINYIELD);
+  scm_i_minyield_malloc = scm_getenv_int ("GUILE_MIN_YIELD_MALLOC",
+                                         SCM_DEFAULT_MALLOC_MINYIELD);
 
   if (scm_i_minyield_malloc >= 100)
     scm_i_minyield_malloc = 99;
@@ -110,22 +111,9 @@ scm_realloc (void *mem, size_t size)
   if (ptr)
     return ptr;
 
-  scm_rec_mutex_lock (&scm_i_sweep_mutex);
-  
-  scm_i_sweep_all_segments ("realloc");
-  
-  SCM_SYSCALL (ptr = realloc (mem, size));
-  if (ptr)
-    { 
-      scm_rec_mutex_unlock (&scm_i_sweep_mutex);
-      return ptr;
-    }
+  /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again.  */
+  GC_gcollect ();
 
-  scm_igc ("realloc");
-  scm_i_sweep_all_segments ("realloc");
-  
-  scm_rec_mutex_unlock (&scm_i_sweep_mutex);
-  
   SCM_SYSCALL (ptr = realloc (mem, size));
   if (ptr)
     return ptr;
@@ -155,7 +143,7 @@ scm_calloc (size_t sz)
   SCM_SYSCALL (ptr = calloc (sz, 1));
   if (ptr)
     return ptr;
-  
+
   ptr = scm_realloc (NULL, sz);
   memset (ptr, 0x0, sz);
   return ptr;
@@ -177,105 +165,15 @@ scm_strdup (const char *str)
   return scm_strndup (str, strlen (str));
 }
 
-static void
-decrease_mtrigger (size_t size, const char * what)
-{
-  scm_i_plugin_mutex_lock (&scm_i_gc_admin_mutex);
-  scm_mallocated -= size;
-  scm_gc_malloc_collected += size;
-  scm_i_plugin_mutex_unlock (&scm_i_gc_admin_mutex);
-}
 
-static void
-increase_mtrigger (size_t size, const char *what)
-{
-  size_t mallocated = 0;
-  int overflow = 0, triggered = 0;
-
-  scm_i_plugin_mutex_lock (&scm_i_gc_admin_mutex);
-  if (ULONG_MAX - size < scm_mallocated)
-    overflow = 1;
-  else
-    {
-      scm_mallocated += size;
-      mallocated = scm_mallocated;
-      if (scm_mallocated > scm_mtrigger)
-       triggered = 1;
-    }
-  scm_i_plugin_mutex_unlock (&scm_i_gc_admin_mutex);
-
-  if (overflow)
-    {
-      scm_memory_error ("Overflow of scm_mallocated: too much memory in use.");
-    }
-
-  /*
-    A program that uses a lot of malloced collectable memory (vectors,
-    strings), will use a lot of memory off the cell-heap; it needs to
-    do GC more often (before cells are exhausted), otherwise swapping
-    and malloc management will tie it down.
-   */
-  if (triggered)
-    {
-      unsigned long prev_alloced;
-      float yield;
-      
-      scm_rec_mutex_lock (&scm_i_sweep_mutex);
-      
-      prev_alloced  = mallocated;
-      scm_igc (what);
-      scm_i_sweep_all_segments ("mtrigger");
-
-      yield = (((float) prev_alloced - (float) scm_mallocated)
-              / (float) prev_alloced);
-      
-      scm_gc_malloc_yield_percentage = (int) (100  * yield);
-
-#ifdef DEBUGINFO
-      fprintf (stderr,  "prev %lud , now %lud, yield %4.2lf, want %d",
-              prev_alloced,
-              scm_mallocated,
-              100.0 * yield,
-              scm_i_minyield_malloc);
-#endif
-      
-      if (yield < scm_i_minyield_malloc /  100.0)
-       {
-         /*
-           We make the trigger a little larger, even; If you have a
-           program that builds up a lot of data in strings, then the
-           desired yield will never be satisfied.
-
-           Instead of getting bogged down, we let the mtrigger grow
-           strongly with it.
-          */
-         float no_overflow_trigger = scm_mallocated * 110.0;
-
-         no_overflow_trigger /= (float)  (100.0 - scm_i_minyield_malloc);
-
-         
-         if (no_overflow_trigger >= (float) ULONG_MAX)
-           scm_mtrigger = ULONG_MAX;
-         else
-           scm_mtrigger =  (unsigned long) no_overflow_trigger;
-         
-#ifdef DEBUGINFO
-         fprintf (stderr, "Mtrigger sweep: ineffective. New trigger %d\n",
-                  scm_mtrigger);
-#endif
-       }
-      
-      scm_rec_mutex_unlock (&scm_i_sweep_mutex);
-    }
-}
 
 void
 scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
 {
-  increase_mtrigger (size, what); 
+  /* Nothing to do.  */
 #ifdef GUILE_DEBUG_MALLOC
   if (mem)
-    scm_malloc_register (mem, what);
+    scm_malloc_register (mem);
 #endif
 }
 
@@ -283,13 +181,21 @@ scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
 void
 scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
 {
-  decrease_mtrigger (size, what);
+  /* Nothing to do.  */
 #ifdef GUILE_DEBUG_MALLOC
   if (mem)
     scm_malloc_unregister (mem);
 #endif
 }
 
+/* Allocate SIZE bytes of memory whose contents should not be scanned for
+   pointers (useful, e.g., for strings).  */
+void *
+scm_gc_malloc_pointerless (size_t size, const char *what)
+{
+  return GC_MALLOC_ATOMIC (size);
+}
+
 void *
 scm_gc_malloc (size_t size, const char *what)
 {
@@ -299,13 +205,19 @@ scm_gc_malloc (size_t size, const char *what)
      again in scm_gc_register_collectable_memory.  We don't really
      want the second GC since it will not find new garbage.
 
-     Note: this is a theoretical peeve. In reality, malloc() never
+     Note: this is a theoretical peeve. In reality, malloc () never
      returns NULL. Usually, memory is overcommitted, and when you try
      to write it the program is killed with signal 11. --hwn
   */
 
-  void *ptr = scm_malloc (size);
-  scm_gc_register_collectable_memory (ptr, size, what);
+  void *ptr;
+
+  if (size == 0)
+    /* `GC_MALLOC ()' doesn't handle zero.  */
+    size = sizeof (void *);
+
+  ptr = GC_MALLOC (size);
+
   return ptr;
 }
 
@@ -323,26 +235,13 @@ scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
 {
   void *ptr;
 
-  /* XXX - see scm_gc_malloc. */
-
-
-  /*    
-  scm_realloc() may invalidate the block pointed to by WHERE, eg. by
-  unmapping it from memory or altering the contents.  Since
-  increase_mtrigger() might trigger a GC that would scan
-  MEM, it is crucial that this call precedes realloc().
-  */
-
-  decrease_mtrigger (old_size, what);
-  increase_mtrigger (new_size, what);
-
-  ptr = scm_realloc (mem, new_size);
+  ptr = GC_REALLOC (mem, new_size);
 
 #ifdef GUILE_DEBUG_MALLOC
   if (mem)
     scm_malloc_reregister (mem, ptr, what);
 #endif
-  
+
   return ptr;
 }
 
@@ -350,13 +249,13 @@ void
 scm_gc_free (void *mem, size_t size, const char *what)
 {
   scm_gc_unregister_collectable_memory (mem, size, what);
-  free (mem);
+  GC_FREE (mem);
 }
 
 char *
 scm_gc_strndup (const char *str, size_t n, const char *what)
 {
-  char *dst = scm_gc_malloc (n+1, what);
+  char *dst = GC_MALLOC (n+1);
   memcpy (dst, str, n);
   dst[n] = 0;
   return dst;