Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / gc.c
index 71e7133..71efd03 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006, 2008, 2009, 2010, 2011, 2012 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
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <stdlib.h>
 #include <math.h>
 
 #ifdef __ia64__
@@ -64,10 +65,6 @@ extern unsigned long * __libc_ia64_register_backing_store_base;
 #include "libguile/debug-malloc.h"
 #endif
 
-#ifdef HAVE_MALLOC_H
-#include <malloc.h>
-#endif
-
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -200,7 +197,11 @@ GC_get_heap_usage_safe (GC_word *pheap_size, GC_word *pfree_bytes,
 {
   *pheap_size = GC_get_heap_size ();
   *pfree_bytes = GC_get_free_bytes ();
+#ifdef HAVE_GC_GET_UNMAPPED_BYTES
   *punmapped_bytes = GC_get_unmapped_bytes ();
+#else
+  *punmapped_bytes = 0;
+#endif
   *pbytes_since_gc = GC_get_bytes_since_gc ();
   *ptotal_bytes = GC_get_total_bytes ();
 }
@@ -226,6 +227,10 @@ scm_t_c_hook scm_after_gc_c_hook;
 static void
 run_before_gc_c_hook (void)
 {
+  if (!SCM_I_CURRENT_THREAD)
+    /* GC while a thread is spinning up; punt.  */
+    return;
+
   scm_c_hook_run (&scm_before_gc_c_hook, NULL);
 }
 
@@ -379,6 +384,9 @@ SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
 #define FUNC_NAME s_scm_gc
 {
   scm_i_gc ("call");
+  /* If you're calling scm_gc(), you probably want synchronous
+     finalization.  */
+  GC_invoke_finalizers ();
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
@@ -603,6 +611,14 @@ scm_getenv_int (const char *var, int def)
   return res;
 }
 
+#ifndef HAVE_GC_SET_FINALIZE_ON_DEMAND
+static void
+GC_set_finalize_on_demand (int foo)
+{
+  GC_finalize_on_demand = foo;
+}
+#endif
+
 void
 scm_storage_prehistory ()
 {
@@ -611,6 +627,7 @@ scm_storage_prehistory ()
   minimum_free_space_divisor = free_space_divisor;
   target_free_space_divisor = free_space_divisor;
   GC_set_free_space_divisor (free_space_divisor);
+  GC_set_finalize_on_demand (1);
 
   GC_INIT ();