2008-02-15 23:13:59 +00:00
|
|
|
|
/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
|
2002-08-04 00:18:33 +00:00
|
|
|
|
*
|
2003-04-05 19:15:35 +00:00
|
|
|
|
* 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.
|
2002-08-04 00:18:33 +00:00
|
|
|
|
*
|
2003-04-05 19:15:35 +00:00
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2002-08-04 00:18:33 +00:00
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2003-04-05 19:15:35 +00:00
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
|
* Lesser General Public License for more details.
|
2002-08-04 00:18:33 +00:00
|
|
|
|
*
|
2003-04-05 19:15:35 +00:00
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2005-05-23 19:57:22 +00:00
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-04-05 19:15:35 +00:00
|
|
|
|
*/
|
2002-08-04 00:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-09-13 15:35:27 +02:00
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2003-03-25 23:55:20 +00:00
|
|
|
|
# include <config.h>
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2002-08-04 00:18:33 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __ia64__
|
|
|
|
|
|
#include <ucontext.h>
|
|
|
|
|
|
extern unsigned long * __libc_ia64_register_backing_store_base;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#include "libguile/_scm.h"
|
|
|
|
|
|
#include "libguile/eval.h"
|
|
|
|
|
|
#include "libguile/stime.h"
|
|
|
|
|
|
#include "libguile/stackchk.h"
|
|
|
|
|
|
#include "libguile/struct.h"
|
|
|
|
|
|
#include "libguile/smob.h"
|
|
|
|
|
|
#include "libguile/unif.h"
|
|
|
|
|
|
#include "libguile/async.h"
|
|
|
|
|
|
#include "libguile/ports.h"
|
|
|
|
|
|
#include "libguile/root.h"
|
|
|
|
|
|
#include "libguile/strings.h"
|
|
|
|
|
|
#include "libguile/vectors.h"
|
|
|
|
|
|
#include "libguile/weaks.h"
|
|
|
|
|
|
#include "libguile/hashtab.h"
|
|
|
|
|
|
#include "libguile/tags.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "libguile/validate.h"
|
|
|
|
|
|
#include "libguile/deprecation.h"
|
|
|
|
|
|
#include "libguile/gc.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "libguile/private-gc.h"
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef GUILE_DEBUG_MALLOC
|
|
|
|
|
|
#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
|
|
|
|
|
|
trigger a GC.
|
|
|
|
|
|
|
|
|
|
|
|
After startup (at the guile> prompt), we have approximately 100k of
|
|
|
|
|
|
alloced memory, which won't go away on GC. Let's set the init such
|
|
|
|
|
|
that we get a nice yield on the next allocation:
|
|
|
|
|
|
*/
|
2002-08-04 14:09:14 +00:00
|
|
|
|
#define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
|
2002-08-04 00:18:33 +00:00
|
|
|
|
#define SCM_DEFAULT_MALLOC_MINYIELD 40
|
|
|
|
|
|
|
2002-09-05 20:43:43 +00:00
|
|
|
|
/* #define DEBUGINFO */
|
2002-08-04 00:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
static int scm_i_minyield_malloc;
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
scm_gc_init_malloc (void)
|
|
|
|
|
|
{
|
|
|
|
|
|
scm_mtrigger = scm_getenv_int ("GUILE_INIT_MALLOC_LIMIT",
|
|
|
|
|
|
SCM_DEFAULT_INIT_MALLOC_LIMIT);
|
2008-08-16 13:57:23 -03:00
|
|
|
|
scm_i_minyield_malloc = scm_getenv_int ("GUILE_MIN_YIELD_MALLOC",
|
|
|
|
|
|
SCM_DEFAULT_MALLOC_MINYIELD);
|
2002-09-05 21:55:33 +00:00
|
|
|
|
|
|
|
|
|
|
if (scm_i_minyield_malloc >= 100)
|
|
|
|
|
|
scm_i_minyield_malloc = 99;
|
|
|
|
|
|
if (scm_i_minyield_malloc < 1)
|
|
|
|
|
|
scm_i_minyield_malloc = 1;
|
|
|
|
|
|
|
|
|
|
|
|
if (scm_mtrigger < 0)
|
|
|
|
|
|
scm_mtrigger = SCM_DEFAULT_INIT_MALLOC_LIMIT;
|
2002-08-04 00:18:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Function for non-cell memory management.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
void *
|
|
|
|
|
|
scm_realloc (void *mem, size_t size)
|
|
|
|
|
|
{
|
|
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
|
|
|
|
SCM_SYSCALL (ptr = realloc (mem, size));
|
|
|
|
|
|
if (ptr)
|
|
|
|
|
|
return ptr;
|
|
|
|
|
|
|
2005-03-02 20:42:01 +00:00
|
|
|
|
scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
|
2005-03-10 18:39:53 +00:00
|
|
|
|
scm_gc_running_p = 1;
|
|
|
|
|
|
|
|
|
|
|
|
scm_i_gc ("realloc");
|
2008-08-16 15:20:55 -03:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
We don't want these sweep statistics to influence results for
|
|
|
|
|
|
cell GC, so we don't collect statistics.
|
|
|
|
|
|
|
2008-08-21 23:16:20 -03:00
|
|
|
|
realloc () failed, so we're really desparate to free memory. Run a
|
2008-08-16 15:20:55 -03:00
|
|
|
|
full sweep.
|
|
|
|
|
|
*/
|
2008-08-16 13:57:23 -03:00
|
|
|
|
scm_i_sweep_all_segments ("realloc", NULL);
|
2008-08-16 15:20:55 -03:00
|
|
|
|
|
2005-03-10 18:39:53 +00:00
|
|
|
|
scm_gc_running_p = 0;
|
2005-03-02 20:42:01 +00:00
|
|
|
|
scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
|
* __scm.h (SCM_ALLOW_INTS_ONLY): Removed.
(SCM_NONREC_CRITICAL_SECTION_START,
SCM_NONREC_CRITICAL_SECTION_END, SCM_REC_CRITICAL_SECTION_START,
SCM_REC_CRITICAL_SECTION_END): New macros.
(SCM_CRITICAL_SECTION_START/END): Defined here.
* eval.c: Insert SOURCE_SECTION_START / SOURCE_SECTION_END around
the three calls to scm_m_expand_body.
* gc.h: #include "libguile/pthread-threads.h";
(SCM_FREELIST_CREATE, SCM_FREELIST_LOC): New macros.
* gc.c (scm_i_freelist, scm_i_freelist2): Defined to be of type
scm_t_key;
* gc.c, gc-freelist.c, inline.h: Use SCM_FREELIST_LOC for freelist
access.
* gc-freelist.c (scm_gc_init_freelist): Create freelist keys.
* gc-freelist.c, threads.c (really_launch): Use
SCM_FREELIST_CREATE.
* gc-malloc.c (scm_realloc, scm_gc_register_collectable_memory):
* gc.c (scm_i_expensive_validation_check, scm_gc,
scm_gc_for_newcell): Put threads to sleep before doing GC-related
heap administration so that those pieces of code are executed
single-threaded. We might consider rewriting these code sections
in terms of a "call_gc_code_singly_threaded" construct instead of
calling the pair of scm_i_thread_put_to_sleep () and
scm_i_thread_wake_up (). Also, we would want to have as many of
these sections eleminated.
* init.c (scm_init_guile_1): Call scm_threads_prehistory.
* inline.h: #include "libguile/threads.h"
* pthread-threads.h: Macros now conform more closely to the
pthreads interface. Some of them now take a second argument.
* threads.c, threads.h: Many changes.
* configure.in: Temporarily replaced "copt" threads option with new
option "pthreads".
(USE_PTHREAD_THREADS): Define if pthreads configured.
2002-12-09 13:42:58 +00:00
|
|
|
|
|
2002-08-04 00:18:33 +00:00
|
|
|
|
SCM_SYSCALL (ptr = realloc (mem, size));
|
|
|
|
|
|
if (ptr)
|
|
|
|
|
|
return ptr;
|
|
|
|
|
|
|
|
|
|
|
|
scm_memory_error ("realloc");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-04 23:33:28 +00:00
|
|
|
|
void *
|
|
|
|
|
|
scm_malloc (size_t sz)
|
|
|
|
|
|
{
|
|
|
|
|
|
return scm_realloc (NULL, sz);
|
|
|
|
|
|
}
|
2002-08-05 23:04:44 +00:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
|
|
|
|
|
|
SIZEOF_ELT)? --hwn
|
|
|
|
|
|
*/
|
|
|
|
|
|
void *
|
|
|
|
|
|
scm_calloc (size_t sz)
|
|
|
|
|
|
{
|
2002-08-28 23:13:30 +00:00
|
|
|
|
void * ptr;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
By default, try to use calloc, as it is likely more efficient than
|
|
|
|
|
|
calling memset by hand.
|
|
|
|
|
|
*/
|
2002-12-21 18:59:47 +00:00
|
|
|
|
SCM_SYSCALL (ptr = calloc (sz, 1));
|
2002-08-28 23:13:30 +00:00
|
|
|
|
if (ptr)
|
|
|
|
|
|
return ptr;
|
|
|
|
|
|
|
|
|
|
|
|
ptr = scm_realloc (NULL, sz);
|
2002-08-05 23:04:44 +00:00
|
|
|
|
memset (ptr, 0x0, sz);
|
|
|
|
|
|
return ptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-04 23:33:28 +00:00
|
|
|
|
|
2002-08-04 00:18:33 +00:00
|
|
|
|
char *
|
|
|
|
|
|
scm_strndup (const char *str, size_t n)
|
|
|
|
|
|
{
|
2002-12-21 18:59:47 +00:00
|
|
|
|
char *dst = scm_malloc (n + 1);
|
2002-08-04 00:18:33 +00:00
|
|
|
|
memcpy (dst, str, n);
|
|
|
|
|
|
dst[n] = 0;
|
|
|
|
|
|
return dst;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
|
scm_strdup (const char *str)
|
|
|
|
|
|
{
|
|
|
|
|
|
return scm_strndup (str, strlen (str));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-07-06 14:54:07 +00:00
|
|
|
|
static void
|
|
|
|
|
|
decrease_mtrigger (size_t size, const char * what)
|
|
|
|
|
|
{
|
2005-03-02 20:42:01 +00:00
|
|
|
|
scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex);
|
2006-04-17 00:05:42 +00:00
|
|
|
|
|
|
|
|
|
|
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 ();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-07-06 14:54:07 +00:00
|
|
|
|
scm_mallocated -= size;
|
|
|
|
|
|
scm_gc_malloc_collected += size;
|
2005-03-02 20:42:01 +00:00
|
|
|
|
scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex);
|
2003-07-06 14:54:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
|
increase_mtrigger (size_t size, const char *what)
|
2002-08-04 00:18:33 +00:00
|
|
|
|
{
|
2004-08-19 16:48:38 +00:00
|
|
|
|
size_t mallocated = 0;
|
|
|
|
|
|
int overflow = 0, triggered = 0;
|
|
|
|
|
|
|
2005-03-02 20:42:01 +00:00
|
|
|
|
scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex);
|
2003-05-14 11:01:23 +00:00
|
|
|
|
if (ULONG_MAX - size < scm_mallocated)
|
2004-08-19 16:48:38 +00:00
|
|
|
|
overflow = 1;
|
|
|
|
|
|
else
|
2003-05-14 11:01:23 +00:00
|
|
|
|
{
|
2004-08-19 16:48:38 +00:00
|
|
|
|
scm_mallocated += size;
|
|
|
|
|
|
mallocated = scm_mallocated;
|
|
|
|
|
|
if (scm_mallocated > scm_mtrigger)
|
|
|
|
|
|
triggered = 1;
|
2003-05-14 11:01:23 +00:00
|
|
|
|
}
|
2005-03-02 20:42:01 +00:00
|
|
|
|
scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex);
|
2003-05-14 11:01:23 +00:00
|
|
|
|
|
2004-08-19 16:48:38 +00:00
|
|
|
|
if (overflow)
|
2005-03-02 20:42:01 +00:00
|
|
|
|
scm_memory_error ("Overflow of scm_mallocated: too much memory in use.");
|
2002-08-04 00:18:33 +00:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
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.
|
|
|
|
|
|
*/
|
2004-08-19 16:48:38 +00:00
|
|
|
|
if (triggered)
|
2002-08-04 00:18:33 +00:00
|
|
|
|
{
|
* __scm.h (SCM_ALLOW_INTS_ONLY): Removed.
(SCM_NONREC_CRITICAL_SECTION_START,
SCM_NONREC_CRITICAL_SECTION_END, SCM_REC_CRITICAL_SECTION_START,
SCM_REC_CRITICAL_SECTION_END): New macros.
(SCM_CRITICAL_SECTION_START/END): Defined here.
* eval.c: Insert SOURCE_SECTION_START / SOURCE_SECTION_END around
the three calls to scm_m_expand_body.
* gc.h: #include "libguile/pthread-threads.h";
(SCM_FREELIST_CREATE, SCM_FREELIST_LOC): New macros.
* gc.c (scm_i_freelist, scm_i_freelist2): Defined to be of type
scm_t_key;
* gc.c, gc-freelist.c, inline.h: Use SCM_FREELIST_LOC for freelist
access.
* gc-freelist.c (scm_gc_init_freelist): Create freelist keys.
* gc-freelist.c, threads.c (really_launch): Use
SCM_FREELIST_CREATE.
* gc-malloc.c (scm_realloc, scm_gc_register_collectable_memory):
* gc.c (scm_i_expensive_validation_check, scm_gc,
scm_gc_for_newcell): Put threads to sleep before doing GC-related
heap administration so that those pieces of code are executed
single-threaded. We might consider rewriting these code sections
in terms of a "call_gc_code_singly_threaded" construct instead of
calling the pair of scm_i_thread_put_to_sleep () and
scm_i_thread_wake_up (). Also, we would want to have as many of
these sections eleminated.
* init.c (scm_init_guile_1): Call scm_threads_prehistory.
* inline.h: #include "libguile/threads.h"
* pthread-threads.h: Macros now conform more closely to the
pthreads interface. Some of them now take a second argument.
* threads.c, threads.h: Many changes.
* configure.in: Temporarily replaced "copt" threads option with new
option "pthreads".
(USE_PTHREAD_THREADS): Define if pthreads configured.
2002-12-09 13:42:58 +00:00
|
|
|
|
unsigned long prev_alloced;
|
2002-08-04 00:18:33 +00:00
|
|
|
|
float yield;
|
2006-02-14 11:38:30 +00:00
|
|
|
|
|
2005-03-02 20:42:01 +00:00
|
|
|
|
scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
|
2005-03-10 18:39:53 +00:00
|
|
|
|
scm_gc_running_p = 1;
|
* __scm.h (SCM_ALLOW_INTS_ONLY): Removed.
(SCM_NONREC_CRITICAL_SECTION_START,
SCM_NONREC_CRITICAL_SECTION_END, SCM_REC_CRITICAL_SECTION_START,
SCM_REC_CRITICAL_SECTION_END): New macros.
(SCM_CRITICAL_SECTION_START/END): Defined here.
* eval.c: Insert SOURCE_SECTION_START / SOURCE_SECTION_END around
the three calls to scm_m_expand_body.
* gc.h: #include "libguile/pthread-threads.h";
(SCM_FREELIST_CREATE, SCM_FREELIST_LOC): New macros.
* gc.c (scm_i_freelist, scm_i_freelist2): Defined to be of type
scm_t_key;
* gc.c, gc-freelist.c, inline.h: Use SCM_FREELIST_LOC for freelist
access.
* gc-freelist.c (scm_gc_init_freelist): Create freelist keys.
* gc-freelist.c, threads.c (really_launch): Use
SCM_FREELIST_CREATE.
* gc-malloc.c (scm_realloc, scm_gc_register_collectable_memory):
* gc.c (scm_i_expensive_validation_check, scm_gc,
scm_gc_for_newcell): Put threads to sleep before doing GC-related
heap administration so that those pieces of code are executed
single-threaded. We might consider rewriting these code sections
in terms of a "call_gc_code_singly_threaded" construct instead of
calling the pair of scm_i_thread_put_to_sleep () and
scm_i_thread_wake_up (). Also, we would want to have as many of
these sections eleminated.
* init.c (scm_init_guile_1): Call scm_threads_prehistory.
* inline.h: #include "libguile/threads.h"
* pthread-threads.h: Macros now conform more closely to the
pthreads interface. Some of them now take a second argument.
* threads.c, threads.h: Many changes.
* configure.in: Temporarily replaced "copt" threads option with new
option "pthreads".
(USE_PTHREAD_THREADS): Define if pthreads configured.
2002-12-09 13:42:58 +00:00
|
|
|
|
|
2008-08-16 13:57:23 -03:00
|
|
|
|
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.
|
|
|
|
|
|
*/
|
2005-03-10 18:39:53 +00:00
|
|
|
|
scm_i_gc (what);
|
2002-08-04 00:18:33 +00:00
|
|
|
|
|
2002-12-21 18:59:47 +00:00
|
|
|
|
yield = (((float) prev_alloced - (float) scm_mallocated)
|
|
|
|
|
|
/ (float) prev_alloced);
|
2002-09-05 21:12:21 +00:00
|
|
|
|
|
2008-08-16 13:57:23 -03:00
|
|
|
|
scm_gc_malloc_yield_percentage = (int) (100 * yield);
|
2002-09-05 20:43:43 +00:00
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUGINFO
|
2002-08-04 00:18:33 +00:00
|
|
|
|
fprintf (stderr, "prev %lud , now %lud, yield %4.2lf, want %d",
|
2002-12-21 18:59:47 +00:00
|
|
|
|
prev_alloced,
|
|
|
|
|
|
scm_mallocated,
|
|
|
|
|
|
100.0 * yield,
|
|
|
|
|
|
scm_i_minyield_malloc);
|
2002-09-05 20:43:43 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2002-08-04 00:18:33 +00:00
|
|
|
|
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.
|
|
|
|
|
|
*/
|
2002-09-09 14:09:35 +00:00
|
|
|
|
float no_overflow_trigger = scm_mallocated * 110.0;
|
2002-09-05 21:55:33 +00:00
|
|
|
|
|
2002-09-09 14:09:35 +00:00
|
|
|
|
no_overflow_trigger /= (float) (100.0 - scm_i_minyield_malloc);
|
2003-05-14 11:01:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (no_overflow_trigger >= (float) ULONG_MAX)
|
|
|
|
|
|
scm_mtrigger = ULONG_MAX;
|
|
|
|
|
|
else
|
2008-08-16 13:57:23 -03:00
|
|
|
|
scm_mtrigger = (unsigned long) no_overflow_trigger;
|
2002-08-04 00:18:33 +00:00
|
|
|
|
|
2002-09-05 20:43:43 +00:00
|
|
|
|
#ifdef DEBUGINFO
|
2002-12-21 18:59:47 +00:00
|
|
|
|
fprintf (stderr, "Mtrigger sweep: ineffective. New trigger %d\n",
|
|
|
|
|
|
scm_mtrigger);
|
2002-09-05 20:43:43 +00:00
|
|
|
|
#endif
|
2002-08-04 00:18:33 +00:00
|
|
|
|
}
|
2005-03-10 18:39:53 +00:00
|
|
|
|
|
|
|
|
|
|
scm_gc_running_p = 0;
|
2005-03-02 20:42:01 +00:00
|
|
|
|
scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
|
2002-08-04 00:18:33 +00:00
|
|
|
|
}
|
2003-07-06 14:54:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
|
|
|
|
|
|
{
|
|
|
|
|
|
increase_mtrigger (size, what);
|
2002-08-04 00:18:33 +00:00
|
|
|
|
#ifdef GUILE_DEBUG_MALLOC
|
|
|
|
|
|
if (mem)
|
|
|
|
|
|
scm_malloc_register (mem, what);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-07-06 14:54:07 +00:00
|
|
|
|
|
2002-08-04 00:18:33 +00:00
|
|
|
|
void
|
|
|
|
|
|
scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
|
|
|
|
|
|
{
|
2003-07-06 14:54:07 +00:00
|
|
|
|
decrease_mtrigger (size, what);
|
2002-08-04 00:18:33 +00:00
|
|
|
|
#ifdef GUILE_DEBUG_MALLOC
|
|
|
|
|
|
if (mem)
|
|
|
|
|
|
scm_malloc_unregister (mem);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
2008-08-21 23:16:20 -03:00
|
|
|
|
Note: this is a theoretical peeve. In reality, malloc () never
|
2002-08-04 00:18:33 +00:00
|
|
|
|
returns NULL. Usually, memory is overcommitted, and when you try
|
|
|
|
|
|
to write it the program is killed with signal 11. --hwn
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2008-02-06 22:22:14 +00:00
|
|
|
|
void *ptr = size ? scm_malloc (size) : NULL;
|
2002-08-04 00:18:33 +00:00
|
|
|
|
scm_gc_register_collectable_memory (ptr, size, what);
|
|
|
|
|
|
return ptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2002-08-04 23:33:28 +00:00
|
|
|
|
void *
|
|
|
|
|
|
scm_gc_calloc (size_t size, const char *what)
|
|
|
|
|
|
{
|
|
|
|
|
|
void *ptr = scm_gc_malloc (size, what);
|
|
|
|
|
|
memset (ptr, 0x0, size);
|
|
|
|
|
|
return ptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-08-04 00:18:33 +00:00
|
|
|
|
void *
|
|
|
|
|
|
scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
|
|
|
|
|
|
{
|
2003-07-09 22:02:20 +00:00
|
|
|
|
void *ptr;
|
|
|
|
|
|
|
2002-08-04 00:18:33 +00:00
|
|
|
|
/* XXX - see scm_gc_malloc. */
|
|
|
|
|
|
|
2003-07-06 14:54:07 +00:00
|
|
|
|
|
|
|
|
|
|
/*
|
2008-08-21 23:16:20 -03:00
|
|
|
|
scm_realloc () may invalidate the block pointed to by WHERE, eg. by
|
2003-07-06 14:54:07 +00:00
|
|
|
|
unmapping it from memory or altering the contents. Since
|
2008-08-21 23:16:20 -03:00
|
|
|
|
increase_mtrigger () might trigger a GC that would scan
|
|
|
|
|
|
MEM, it is crucial that this call precedes realloc ().
|
2003-07-06 14:54:07 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
decrease_mtrigger (old_size, what);
|
|
|
|
|
|
increase_mtrigger (new_size, what);
|
|
|
|
|
|
|
2003-07-09 22:02:20 +00:00
|
|
|
|
ptr = scm_realloc (mem, new_size);
|
2003-07-06 14:54:07 +00:00
|
|
|
|
|
|
|
|
|
|
#ifdef GUILE_DEBUG_MALLOC
|
|
|
|
|
|
if (mem)
|
|
|
|
|
|
scm_malloc_reregister (mem, ptr, what);
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2002-08-04 00:18:33 +00:00
|
|
|
|
return ptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
scm_gc_free (void *mem, size_t size, const char *what)
|
|
|
|
|
|
{
|
|
|
|
|
|
scm_gc_unregister_collectable_memory (mem, size, what);
|
2008-02-06 22:22:14 +00:00
|
|
|
|
if (mem)
|
|
|
|
|
|
free (mem);
|
2002-08-04 00:18:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
|
scm_gc_strndup (const char *str, size_t n, const char *what)
|
|
|
|
|
|
{
|
|
|
|
|
|
char *dst = scm_gc_malloc (n+1, what);
|
|
|
|
|
|
memcpy (dst, str, n);
|
|
|
|
|
|
dst[n] = 0;
|
|
|
|
|
|
return dst;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *
|
|
|
|
|
|
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
|
2002-08-08 23:18:23 +00:00
|
|
|
|
{
|
|
|
|
|
|
fprintf (stderr,"freeing NULL pointer");
|
|
|
|
|
|
abort ();
|
|
|
|
|
|
}
|
2002-08-04 00:18:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
#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.");
|
|
|
|
|
|
|
2004-02-16 00:45:55 +00:00
|
|
|
|
if (size >= 0)
|
|
|
|
|
|
scm_gc_register_collectable_memory (NULL, size, "foreign mallocs");
|
|
|
|
|
|
else
|
|
|
|
|
|
scm_gc_unregister_collectable_memory (NULL, -size, "foreign mallocs");
|
2002-08-04 00:18:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
scm_done_free (long size)
|
|
|
|
|
|
{
|
|
|
|
|
|
scm_c_issue_deprecation_warning
|
|
|
|
|
|
("scm_done_free is deprecated. "
|
|
|
|
|
|
"Use scm_gc_unregister_collectable_memory instead.");
|
|
|
|
|
|
|
2004-02-16 00:45:55 +00:00
|
|
|
|
if (size >= 0)
|
|
|
|
|
|
scm_gc_unregister_collectable_memory (NULL, size, "foreign mallocs");
|
|
|
|
|
|
else
|
|
|
|
|
|
scm_gc_register_collectable_memory (NULL, -size, "foreign mallocs");
|
2002-08-04 00:18:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* SCM_ENABLE_DEPRECATED == 1 */
|