-/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
+/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
+ * 2004, 2006, 2008, 2009, 2010, 2011, 2012,
+ * 2014 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
#include <stdio.h>
#include <errno.h>
#include <string.h>
+#include <stdlib.h>
#ifdef __ia64__
#include <ucontext.h>
#include "libguile/stackchk.h"
#include "libguile/struct.h"
#include "libguile/smob.h"
-#include "libguile/unif.h"
+#include "libguile/arrays.h"
#include "libguile/async.h"
#include "libguile/ports.h"
#include "libguile/root.h"
#include "libguile/debug-malloc.h"
#endif
-#ifdef HAVE_MALLOC_H
-#include <malloc.h>
-#endif
-
-#ifdef HAVE_UNISTD_H
#include <unistd.h>
-#endif
/*
INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
/* #define DEBUGINFO */
-static int scm_i_minyield_malloc;
-void
-scm_gc_init_malloc (void)
+\f
+
+static void*
+do_realloc (void *from, size_t new_size)
{
- int 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);
-
- if (scm_i_minyield_malloc >= 100)
- scm_i_minyield_malloc = 99;
- if (scm_i_minyield_malloc < 1)
- scm_i_minyield_malloc = 1;
-
- if (mtrigger < 0)
- scm_mtrigger = SCM_DEFAULT_INIT_MALLOC_LIMIT;
- else
- scm_mtrigger = mtrigger;
+ 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);
}
{
void *ptr;
- SCM_SYSCALL (ptr = realloc (mem, size));
- if (ptr)
- return ptr;
+ ptr = do_realloc (mem, size);
- scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
- scm_gc_running_p = 1;
+ if (ptr || size == 0)
+ return ptr;
- scm_i_gc ("realloc");
+ /* 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
- /*
- 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_SYSCALL (ptr = realloc (mem, size));
+ ptr = do_realloc (mem, size);
if (ptr)
return ptr;
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);
memset (ptr, 0x0, sz);
return ptr;
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);
-
- 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,
- 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_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
- scm_gc_running_p = 1;
-
- 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);
-
- 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_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);
+ scm_gc_register_allocation (size);
+
#ifdef GUILE_DEBUG_MALLOC
if (mem)
scm_malloc_register (mem, 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). 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 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 = size ? scm_malloc (size) : NULL;
- scm_gc_register_collectable_memory (ptr, size, what);
- return ptr;
+ return do_gc_malloc (size, what);
}
void *
scm_gc_calloc (size_t size, const char *what)
{
- void *ptr = scm_gc_malloc (size, what);
- memset (ptr, 0x0, size);
- return ptr;
+ /* `GC_MALLOC ()' always returns a zeroed buffer. */
+ 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;
-
- /* 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);
-
-#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);
- if (mem)
- free (mem);
+ do_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 = do_gc_malloc_atomic (n + 1, what);
memcpy (dst, str, n);
dst[n] = 0;
return dst;
* 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.
+ * free.
*
- * 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.
+ * There has been a fair amount of confusion around the use of these functions;
+ * see "Memory Blocks" in the manual. They are totally unnecessary in 2.0 given
+ * the Boehm GC.
*/
void *
("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 ();
- }
+ do_gc_free (obj);
}
#undef FUNC_NAME