1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 extern unsigned long * __libc_ia64_register_backing_store_base
;
34 #include "libguile/_scm.h"
35 #include "libguile/eval.h"
36 #include "libguile/stime.h"
37 #include "libguile/stackchk.h"
38 #include "libguile/struct.h"
39 #include "libguile/smob.h"
40 #include "libguile/arrays.h"
41 #include "libguile/async.h"
42 #include "libguile/ports.h"
43 #include "libguile/root.h"
44 #include "libguile/strings.h"
45 #include "libguile/vectors.h"
46 #include "libguile/weaks.h"
47 #include "libguile/hashtab.h"
48 #include "libguile/tags.h"
50 #include "libguile/validate.h"
51 #include "libguile/deprecation.h"
52 #include "libguile/gc.h"
54 #include "libguile/private-gc.h"
56 #ifdef GUILE_DEBUG_MALLOC
57 #include "libguile/debug-malloc.h"
69 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
72 After startup (at the guile> prompt), we have approximately 100k of
73 alloced memory, which won't go away on GC. Let's set the init such
74 that we get a nice yield on the next allocation:
76 #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
77 #define SCM_DEFAULT_MALLOC_MINYIELD 40
79 /* #define DEBUGINFO */
83 /* Function for non-cell memory management.
87 scm_realloc (void *mem
, size_t size
)
91 SCM_SYSCALL (ptr
= realloc (mem
, size
));
95 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
98 SCM_SYSCALL (ptr
= realloc (mem
, size
));
102 scm_memory_error ("realloc");
106 scm_malloc (size_t sz
)
108 return scm_realloc (NULL
, sz
);
112 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
116 scm_calloc (size_t sz
)
121 By default, try to use calloc, as it is likely more efficient than
122 calling memset by hand.
124 SCM_SYSCALL (ptr
= calloc (sz
, 1));
128 ptr
= scm_realloc (NULL
, sz
);
129 memset (ptr
, 0x0, sz
);
135 scm_strndup (const char *str
, size_t n
)
137 char *dst
= scm_malloc (n
+ 1);
138 memcpy (dst
, str
, n
);
144 scm_strdup (const char *str
)
146 return scm_strndup (str
, strlen (str
));
152 scm_gc_register_collectable_memory (void *mem
, size_t size
, const char *what
)
155 #ifdef GUILE_DEBUG_MALLOC
157 scm_malloc_register (mem
);
163 scm_gc_unregister_collectable_memory (void *mem
, size_t size
, const char *what
)
166 #ifdef GUILE_DEBUG_MALLOC
168 scm_malloc_unregister (mem
);
172 /* Allocate SIZE bytes of memory whose contents should not be scanned
173 for pointers (useful, e.g., for strings). Note though that this
174 memory is *not* cleared; be sure to initialize it to prevent
175 information leaks. */
177 scm_gc_malloc_pointerless (size_t size
, const char *what
)
179 return GC_MALLOC_ATOMIC (size
);
183 scm_gc_malloc (size_t size
, const char *what
)
188 /* `GC_MALLOC ()' doesn't handle zero. */
189 size
= sizeof (void *);
191 ptr
= GC_MALLOC (size
);
197 scm_gc_calloc (size_t size
, const char *what
)
199 /* `GC_MALLOC ()' always returns a zeroed buffer. */
200 return scm_gc_malloc (size
, what
);
205 scm_gc_realloc (void *mem
, size_t old_size
, size_t new_size
, const char *what
)
209 ptr
= GC_REALLOC (mem
, new_size
);
211 #ifdef GUILE_DEBUG_MALLOC
213 scm_malloc_reregister (mem
, ptr
, what
);
220 scm_gc_free (void *mem
, size_t size
, const char *what
)
222 scm_gc_unregister_collectable_memory (mem
, size
, what
);
227 scm_gc_strndup (const char *str
, size_t n
, const char *what
)
229 char *dst
= GC_MALLOC_ATOMIC (n
+ 1);
230 memcpy (dst
, str
, n
);
236 scm_gc_strdup (const char *str
, const char *what
)
238 return scm_gc_strndup (str
, strlen (str
), what
);