Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / gc-malloc.c
index 669f789..179558f 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2008, 2009 Free Software Foundation, Inc.
+/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
+ *   2004, 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
@@ -25,6 +26,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
+#include <stdlib.h>
 
 #ifdef __ia64__
 #include <ucontext.h>
@@ -43,7 +45,6 @@ extern unsigned long * __libc_ia64_register_backing_store_base;
 #include "libguile/root.h"
 #include "libguile/strings.h"
 #include "libguile/vectors.h"
-#include "libguile/weaks.h"
 #include "libguile/hashtab.h"
 #include "libguile/tags.h"
 
@@ -57,10 +58,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
@@ -80,6 +77,49 @@ extern unsigned long * __libc_ia64_register_backing_store_base;
 
 
 \f
+
+static void*
+do_realloc (void *from, size_t new_size)
+{
+  scm_gc_register_allocation (new_size);
+  return realloc (from, new_size);
+}
+
+static void*
+do_calloc (size_t n, size_t size)
+{
+  scm_gc_register_allocation (size);
+  return calloc (n, size);
+}
+
+static void*
+do_gc_malloc (size_t size, const char *what)
+{
+  /* Ensure nonzero size to be compatible with always-nonzero return of
+     glibc malloc.  */
+  return GC_MALLOC (size ? size : sizeof (void *));
+}
+
+static void*
+do_gc_malloc_atomic (size_t size, const char *what)
+{
+  return GC_MALLOC_ATOMIC (size ? size : sizeof (void *));
+}
+
+static void*
+do_gc_realloc (void *from, size_t size, const char *what)
+{
+  return GC_REALLOC (from, size ? size : sizeof (void *));
+}
+
+static void
+do_gc_free (void *ptr)
+{
+  GC_FREE (ptr);
+}
+
+
+\f
 /* Function for non-cell memory management.
  */
 
@@ -88,14 +128,19 @@ scm_realloc (void *mem, size_t size)
 {
   void *ptr;
 
-  SCM_SYSCALL (ptr = realloc (mem, size));
-  if (ptr)
+  ptr = do_realloc (mem, size);
+
+  if (ptr || size == 0)
     return ptr;
 
   /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again.  */
+#ifdef HAVE_GC_GCOLLECT_AND_UNMAP
+  GC_gcollect_and_unmap ();
+#else
   GC_gcollect ();
+#endif
 
-  SCM_SYSCALL (ptr = realloc (mem, size));
+  ptr = do_realloc (mem, size);
   if (ptr)
     return ptr;
 
@@ -121,8 +166,8 @@ scm_calloc (size_t sz)
     By default, try to use calloc, as it is likely more efficient than
     calling memset by hand.
    */
-  SCM_SYSCALL (ptr = calloc (sz, 1));
-  if (ptr)
+  ptr = do_calloc (sz, 1);
+  if (ptr || sz == 0)
     return ptr;
 
   ptr = scm_realloc (NULL, sz);
@@ -151,10 +196,11 @@ scm_strdup (const char *str)
 void
 scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
 {
-  /* Nothing to do.  */
+  scm_gc_register_allocation (size);
+
 #ifdef GUILE_DEBUG_MALLOC
   if (mem)
-    scm_malloc_register (mem);
+    scm_malloc_register (mem, what);
 #endif
 }
 
@@ -169,73 +215,45 @@ scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
 #endif
 }
 
-/* Allocate SIZE bytes of memory whose contents should not be scanned for
-   pointers (useful, e.g., for strings).  */
+/* Allocate SIZE bytes of memory whose contents should not be scanned
+   for pointers (useful, e.g., for strings).  Note though that this
+   memory is *not* cleared; be sure to initialize it to prevent
+   information leaks.  */
 void *
 scm_gc_malloc_pointerless (size_t size, const char *what)
 {
-  return GC_MALLOC_ATOMIC (size);
+  return do_gc_malloc_atomic (size, what);
 }
 
 void *
 scm_gc_malloc (size_t size, const char *what)
 {
-  /*
-    The straightforward implementation below has the problem
-     that it might call the GC twice, once in scm_malloc and then
-     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
-     returns NULL. Usually, memory is overcommitted, and when you try
-     to write it the program is killed with signal 11. --hwn
-  */
-
-  void *ptr;
-
-  if (size == 0)
-    /* `GC_MALLOC ()' doesn't handle zero.  */
-    size = sizeof (void *);
-
-  ptr = GC_MALLOC (size);
-
-  return ptr;
+  return do_gc_malloc (size, what);
 }
 
 void *
 scm_gc_calloc (size_t size, const char *what)
 {
   /* `GC_MALLOC ()' always returns a zeroed buffer.  */
-  return scm_gc_malloc (size, what);
+  return do_gc_malloc (size, what);
 }
 
-
 void *
 scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
 {
-  void *ptr;
-
-  ptr = GC_REALLOC (mem, new_size);
-
-#ifdef GUILE_DEBUG_MALLOC
-  if (mem)
-    scm_malloc_reregister (mem, ptr, what);
-#endif
-
-  return ptr;
+  return do_gc_realloc (mem, new_size, what);
 }
 
 void
 scm_gc_free (void *mem, size_t size, const char *what)
 {
-  scm_gc_unregister_collectable_memory (mem, size, what);
-  GC_FREE (mem);
+  do_gc_free (mem);
 }
 
 char *
 scm_gc_strndup (const char *str, size_t n, const char *what)
 {
-  char *dst = GC_MALLOC_ATOMIC (n + 1);
+  char *dst = do_gc_malloc_atomic (n + 1, what);
   memcpy (dst, str, n);
   dst[n] = 0;
   return dst;
@@ -246,117 +264,3 @@ scm_gc_strdup (const char *str, const char *what)
 {
   return scm_gc_strndup (str, strlen (str), what);
 }
-
-#if SCM_ENABLE_DEPRECATED == 1
-
-/* {Deprecated front end to malloc}
- *
- * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
- * scm_done_free
- *
- * These functions provide services comparable to malloc, realloc, and
- * free.  They should be used when allocating memory that will be under
- * control of the garbage collector, i.e., if the memory may be freed
- * during garbage collection.
- *
- * They are deprecated because they weren't really used the way
- * outlined above, and making sure to return the right amount from
- * smob free routines was sometimes difficult when dealing with nested
- * data structures.  We basically want everybody to review their code
- * and use the more symmetrical scm_gc_malloc/scm_gc_free functions
- * instead.  In some cases, where scm_must_malloc has been used
- * incorrectly (i.e. for non-GC-able memory), use scm_malloc/free.
- */
-
-void *
-scm_must_malloc (size_t size, const char *what)
-{
-  scm_c_issue_deprecation_warning
-    ("scm_must_malloc is deprecated.  "
-     "Use scm_gc_malloc and scm_gc_free instead.");
-
-  return scm_gc_malloc (size, what);
-}
-
-void *
-scm_must_realloc (void *where,
-                 size_t old_size,
-                 size_t size,
-                 const char *what)
-{
-  scm_c_issue_deprecation_warning
-    ("scm_must_realloc is deprecated.  "
-     "Use scm_gc_realloc and scm_gc_free instead.");
-
-  return scm_gc_realloc (where, old_size, size, what);
-}
-
-char *
-scm_must_strndup (const char *str, size_t length)
-{
-  scm_c_issue_deprecation_warning
-    ("scm_must_strndup is deprecated.  "
-     "Use scm_gc_strndup and scm_gc_free instead.");
-
-  return scm_gc_strndup (str, length, "string");
-}
-
-char *
-scm_must_strdup (const char *str)
-{
-  scm_c_issue_deprecation_warning
-    ("scm_must_strdup is deprecated.  "
-     "Use scm_gc_strdup and scm_gc_free instead.");
-
-  return scm_gc_strdup (str, "string");
-}
-
-void
-scm_must_free (void *obj)
-#define FUNC_NAME "scm_must_free"
-{
-  scm_c_issue_deprecation_warning
-    ("scm_must_free is deprecated.  "
-     "Use scm_gc_malloc and scm_gc_free instead.");
-
-#ifdef GUILE_DEBUG_MALLOC
-  scm_malloc_unregister (obj);
-#endif
-  if (obj)
-    free (obj);
-  else
-    {
-      fprintf (stderr,"freeing NULL pointer");
-      abort ();
-    }
-}
-#undef FUNC_NAME
-
-
-void
-scm_done_malloc (long size)
-{
-  scm_c_issue_deprecation_warning
-    ("scm_done_malloc is deprecated.  "
-     "Use scm_gc_register_collectable_memory instead.");
-
-  if (size >= 0)
-    scm_gc_register_collectable_memory (NULL, size, "foreign mallocs");
-  else
-    scm_gc_unregister_collectable_memory (NULL, -size, "foreign mallocs");
-}
-
-void
-scm_done_free (long size)
-{
-  scm_c_issue_deprecation_warning
-    ("scm_done_free is deprecated.  "
-     "Use scm_gc_unregister_collectable_memory instead.");
-
-  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 */