compile occam-channel
[bpt/guile.git] / libguile / gc-malloc.c
index b2c73a1..cdc985e 100644 (file)
@@ -1,46 +1,26 @@
-/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
  *
- * This program 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.
+ * 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.
  *
- * This program is distributed in the hope that it will be useful,
+ * 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 General Public License for more details.
+ * 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 General Public License
- * along with this software; 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, the Free Software Foundation gives permission
- * for additional uses of the text contained in its release of GUILE.
- *
- * The exception is that, if you link the GUILE library with other files
- * to produce an executable, this does not by itself cause the
- * resulting executable to be covered by the GNU General Public License.
- * Your use of that executable is in no way restricted on account of
- * linking the GUILE library code into it.
- *
- * This exception does not however invalidate any other reasons why
- * the executable file might be covered by the GNU General Public License.
- *
- * This exception applies only to the code released by the
- * Free Software Foundation under the name GUILE.  If you copy
- * code from other Free Software Foundation releases into a copy of
- * GUILE, as the General Public License permits, the exception does
- * not apply to the code that you add in this way.  To avoid misleading
- * anyone as to the status of such modified files, you must delete
- * this exception notice from them.
- *
- * If you write modifications of your own for GUILE, it is your choice
- * whether to permit this exception to apply to your modifications.
- * If you do not wish that, delete this exception notice.  */
+ * 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
+ */
 
 
 \f
+#ifdef HAVE_CONFIG_H
+#  include <config.h>
+#endif
+
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
@@ -99,21 +79,13 @@ extern unsigned long * __libc_ia64_register_backing_store_base;
 
 static int scm_i_minyield_malloc;
 
-static scm_t_mutex malloc_mutex;
-
-void
-scm_gc_malloc_prehistory ()
-{
-  scm_i_plugin_mutex_init (&malloc_mutex, 0);
-}
-
 void
 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;
@@ -134,33 +106,28 @@ scm_realloc (void *mem, size_t size)
 {
   void *ptr;
 
-  scm_i_plugin_mutex_lock (&malloc_mutex);
   SCM_SYSCALL (ptr = realloc (mem, size));
-  scm_i_plugin_mutex_unlock (&malloc_mutex);
   if (ptr)
     return ptr;
 
-  scm_i_thread_put_to_sleep ();
-  
-  scm_i_sweep_all_segments ("realloc");
-  
-  scm_i_plugin_mutex_lock (&malloc_mutex);
-  SCM_SYSCALL (ptr = realloc (mem, size));
-  scm_i_plugin_mutex_unlock (&malloc_mutex);
-  if (ptr)
-    { 
-      scm_i_thread_wake_up ();
-      return ptr;
-    }
+  scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
+  scm_gc_running_p = 1;
 
-  scm_igc ("realloc");
-  scm_i_sweep_all_segments ("realloc");
-  
-  scm_i_thread_wake_up ();
+  scm_i_gc ("realloc");
+
+  /*
+   We don't want these sweep statistics to influence results for
+   cell GC, so we don't collect statistics.
+   
+   realloc () failed, so we're really desparate to free memory. Run a
+   full sweep.
+  */
+  scm_i_sweep_all_segments ("realloc", NULL);
+
+  scm_gc_running_p = 0;
+  scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
   
-  scm_i_plugin_mutex_lock (&malloc_mutex);
   SCM_SYSCALL (ptr = realloc (mem, size));
-  scm_i_plugin_mutex_unlock (&malloc_mutex);
   if (ptr)
     return ptr;
 
@@ -186,9 +153,7 @@ scm_calloc (size_t sz)
     By default, try to use calloc, as it is likely more efficient than
     calling memset by hand.
    */
-  scm_i_plugin_mutex_lock (&malloc_mutex);
-  SCM_SYSCALL(ptr= calloc (sz, 1));
-  scm_i_plugin_mutex_unlock (&malloc_mutex);
+  SCM_SYSCALL (ptr = calloc (sz, 1));
   if (ptr)
     return ptr;
   
@@ -201,7 +166,7 @@ scm_calloc (size_t sz)
 char *
 scm_strndup (const char *str, size_t n)
 {
-  char *dst = scm_malloc (n+1);
+  char *dst = scm_malloc (n + 1);
   memcpy (dst, str, n);
   dst[n] = 0;
   return dst;
@@ -213,15 +178,45 @@ scm_strdup (const char *str)
   return scm_strndup (str, strlen (str));
 }
 
-void
-scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
+static void
+decrease_mtrigger (size_t size, const char * what)
 {
-  scm_mallocated += size;
+  scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex);
 
-  /*
-    we could finish the full sweep (without mark) here, but in
-    practice this turns out to be ineffective.
-   */
+  if (size > scm_mallocated)
+    {
+      fprintf (stderr, "`scm_mallocated' underflow.  This means that more "
+              "memory was unregistered\n"
+              "via `scm_gc_unregister_collectable_memory ()' than "
+              "registered.\n");
+      abort ();
+    }
+
+  scm_mallocated -= size;
+  scm_gc_malloc_collected += size;
+  scm_i_pthread_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_pthread_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_pthread_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,
@@ -229,25 +224,33 @@ scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
     do GC more often (before cells are exhausted), otherwise swapping
     and malloc management will tie it down.
    */
-  if (scm_mallocated > scm_mtrigger)
+  if (triggered)
     {
       unsigned long prev_alloced;
       float yield;
+
+      scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
+      scm_gc_running_p = 1;
       
-      scm_i_thread_put_to_sleep ();
-      
-      prev_alloced  = scm_mallocated;
-      scm_igc (what);
-      scm_i_sweep_all_segments ("mtrigger");
+      prev_alloced = mallocated;
+
+      /* The GC will finish the pending sweep. For that reason, we
+        don't execute a complete sweep after GC, although that might
+        free some more memory.
+      */
+      scm_i_gc (what);
 
-      yield = ((float)prev_alloced - (float) scm_mallocated)
-       / (float) prev_alloced;
+      yield = (((float) prev_alloced - (float) scm_mallocated)
+              / (float) prev_alloced);
       
-      scm_gc_malloc_yield_percentage = (int) (100  * yield);
+      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);
+              prev_alloced,
+              scm_mallocated,
+              100.0 * yield,
+              scm_i_minyield_malloc);
 #endif
       
       if (yield < scm_i_minyield_malloc /  100.0)
@@ -263,28 +266,39 @@ scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
          float no_overflow_trigger = scm_mallocated * 110.0;
 
          no_overflow_trigger /= (float)  (100.0 - scm_i_minyield_malloc);
-         scm_mtrigger =  (unsigned long) no_overflow_trigger;
+
+         
+         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);
+         fprintf (stderr, "Mtrigger sweep: ineffective. New trigger %d\n",
+                  scm_mtrigger);
 #endif
        }
-      
-      scm_i_thread_wake_up ();
+
+      scm_gc_running_p = 0;
+      scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
     }
-  
+}
+
+void
+scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
+{
+  increase_mtrigger (size, what); 
 #ifdef GUILE_DEBUG_MALLOC
   if (mem)
     scm_malloc_register (mem, what);
 #endif
 }
 
+
 void
 scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
 {
-  scm_mallocated -= size;
-  scm_gc_malloc_collected += size;
-  
+  decrease_mtrigger (size, what);
 #ifdef GUILE_DEBUG_MALLOC
   if (mem)
     scm_malloc_unregister (mem);
@@ -300,13 +314,12 @@ 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);
+  void *ptr = size ? scm_malloc (size) : NULL;
   scm_gc_register_collectable_memory (ptr, size, what);
   return ptr;
 }
@@ -323,11 +336,28 @@ scm_gc_calloc (size_t size, const char *what)
 void *
 scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
 {
+  void *ptr;
+
   /* XXX - see scm_gc_malloc. */
 
-  void *ptr = scm_realloc (mem, new_size);
-  scm_gc_unregister_collectable_memory (mem, old_size, what);
-  scm_gc_register_collectable_memory (ptr, new_size, what);
+
+  /*    
+  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);
+
+#ifdef GUILE_DEBUG_MALLOC
+  if (mem)
+    scm_malloc_reregister (mem, ptr, what);
+#endif
+  
   return ptr;
 }
 
@@ -335,7 +365,8 @@ void
 scm_gc_free (void *mem, size_t size, const char *what)
 {
   scm_gc_unregister_collectable_memory (mem, size, what);
-  free (mem);
+  if (mem)
+    free (mem);
 }
 
 char *
@@ -446,7 +477,10 @@ scm_done_malloc (long size)
     ("scm_done_malloc is deprecated.  "
      "Use scm_gc_register_collectable_memory instead.");
 
-  scm_gc_register_collectable_memory (NULL, size, "foreign mallocs");
+  if (size >= 0)
+    scm_gc_register_collectable_memory (NULL, size, "foreign mallocs");
+  else
+    scm_gc_unregister_collectable_memory (NULL, -size, "foreign mallocs");
 }
 
 void
@@ -456,7 +490,10 @@ scm_done_free (long size)
     ("scm_done_free is deprecated.  "
      "Use scm_gc_unregister_collectable_memory instead.");
 
-  scm_gc_unregister_collectable_memory (NULL, size, "foreign mallocs");
+  if (size >= 0)
+    scm_gc_unregister_collectable_memory (NULL, size, "foreign mallocs");
+  else
+    scm_gc_register_collectable_memory (NULL, -size, "foreign mallocs");
 }
 
 #endif /* SCM_ENABLE_DEPRECATED == 1 */