(scm_num_eq_p): On 64-bit systems, be careful about
[bpt/guile.git] / libguile / gc-malloc.c
index 02032ad..a58ed13 100644 (file)
@@ -1,4 +1,4 @@
-/* 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 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
@@ -110,21 +110,24 @@ scm_realloc (void *mem, size_t size)
   if (ptr)
     return ptr;
 
-  scm_rec_mutex_lock (&scm_i_sweep_mutex);
-  
+  scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
+  scm_gc_running_p = 1;
+
   scm_i_sweep_all_segments ("realloc");
   
   SCM_SYSCALL (ptr = realloc (mem, size));
   if (ptr)
     { 
-      scm_rec_mutex_unlock (&scm_i_sweep_mutex);
+      scm_gc_running_p = 0;
+      scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
       return ptr;
     }
 
-  scm_igc ("realloc");
+  scm_i_gc ("realloc");
   scm_i_sweep_all_segments ("realloc");
   
-  scm_rec_mutex_unlock (&scm_i_sweep_mutex);
+  scm_gc_running_p = 0;
+  scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
   
   SCM_SYSCALL (ptr = realloc (mem, size));
   if (ptr)
@@ -177,23 +180,35 @@ scm_strdup (const char *str)
   return scm_strndup (str, strlen (str));
 }
 
-
 static void
 decrease_mtrigger (size_t size, const char * what)
 {
+  scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex);
   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_memory_error ("Overflow of scm_mallocated: too much memory in use.");
+      scm_mallocated += size;
+      mallocated = scm_mallocated;
+      if (scm_mallocated > scm_mtrigger)
+       triggered = 1;
     }
+  scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex);
 
-  scm_mallocated += size;
+  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,
@@ -201,15 +216,16 @@ increase_mtrigger (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_rec_mutex_lock (&scm_i_sweep_mutex);
+      scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
+      scm_gc_running_p = 1;
       
-      prev_alloced  = scm_mallocated;
-      scm_igc (what);
+      prev_alloced  = mallocated;
+      scm_i_gc (what);
       scm_i_sweep_all_segments ("mtrigger");
 
       yield = (((float) prev_alloced - (float) scm_mallocated)
@@ -250,8 +266,9 @@ increase_mtrigger (size_t size, const char *what)
                   scm_mtrigger);
 #endif
        }
-      
-      scm_rec_mutex_unlock (&scm_i_sweep_mutex);
+
+      scm_gc_running_p = 0;
+      scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
     }
 }
 
@@ -307,6 +324,8 @@ 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. */
 
 
@@ -320,7 +339,7 @@ scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
   decrease_mtrigger (old_size, what);
   increase_mtrigger (new_size, what);
 
-  void *ptr = scm_realloc (mem, new_size);
+  ptr = scm_realloc (mem, new_size);
 
 #ifdef GUILE_DEBUG_MALLOC
   if (mem)
@@ -445,7 +464,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
@@ -455,7 +477,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 */