1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 * 2004, 2006, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33 extern unsigned long * __libc_ia64_register_backing_store_base
;
36 #include "libguile/_scm.h"
37 #include "libguile/eval.h"
38 #include "libguile/stime.h"
39 #include "libguile/stackchk.h"
40 #include "libguile/struct.h"
41 #include "libguile/smob.h"
42 #include "libguile/arrays.h"
43 #include "libguile/async.h"
44 #include "libguile/ports.h"
45 #include "libguile/root.h"
46 #include "libguile/strings.h"
47 #include "libguile/vectors.h"
48 #include "libguile/weaks.h"
49 #include "libguile/hashtab.h"
50 #include "libguile/tags.h"
52 #include "libguile/validate.h"
53 #include "libguile/deprecation.h"
54 #include "libguile/gc.h"
56 #include "libguile/private-gc.h"
58 #ifdef GUILE_DEBUG_MALLOC
59 #include "libguile/debug-malloc.h"
67 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
70 After startup (at the guile> prompt), we have approximately 100k of
71 alloced memory, which won't go away on GC. Let's set the init such
72 that we get a nice yield on the next allocation:
74 #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
75 #define SCM_DEFAULT_MALLOC_MINYIELD 40
77 /* #define DEBUGINFO */
83 do_realloc (void *from
, size_t new_size
)
85 scm_gc_register_allocation (new_size
);
86 return realloc (from
, new_size
);
90 do_calloc (size_t n
, size_t size
)
92 scm_gc_register_allocation (size
);
93 return calloc (n
, size
);
97 do_gc_malloc (size_t size
, const char *what
)
99 /* Ensure nonzero size to be compatible with always-nonzero return of
101 return GC_MALLOC (size
? size
: sizeof (void *));
105 do_gc_malloc_atomic (size_t size
, const char *what
)
107 return GC_MALLOC_ATOMIC (size
? size
: sizeof (void *));
111 do_gc_realloc (void *from
, size_t size
, const char *what
)
113 return GC_REALLOC (from
, size
? size
: sizeof (void *));
117 do_gc_free (void *ptr
)
124 /* Function for non-cell memory management.
128 scm_realloc (void *mem
, size_t size
)
132 ptr
= do_realloc (mem
, size
);
134 if (ptr
|| size
== 0)
137 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
138 #ifdef HAVE_GC_GCOLLECT_AND_UNMAP
139 GC_gcollect_and_unmap ();
144 ptr
= do_realloc (mem
, size
);
148 scm_memory_error ("realloc");
152 scm_malloc (size_t sz
)
154 return scm_realloc (NULL
, sz
);
158 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
162 scm_calloc (size_t sz
)
167 By default, try to use calloc, as it is likely more efficient than
168 calling memset by hand.
170 ptr
= do_calloc (sz
, 1);
174 ptr
= scm_realloc (NULL
, sz
);
175 memset (ptr
, 0x0, sz
);
181 scm_strndup (const char *str
, size_t n
)
183 char *dst
= scm_malloc (n
+ 1);
184 memcpy (dst
, str
, n
);
190 scm_strdup (const char *str
)
192 return scm_strndup (str
, strlen (str
));
198 scm_gc_register_collectable_memory (void *mem
, size_t size
, const char *what
)
200 scm_gc_register_allocation (size
);
202 #ifdef GUILE_DEBUG_MALLOC
204 scm_malloc_register (mem
, what
);
210 scm_gc_unregister_collectable_memory (void *mem
, size_t size
, const char *what
)
213 #ifdef GUILE_DEBUG_MALLOC
215 scm_malloc_unregister (mem
);
219 /* Allocate SIZE bytes of memory whose contents should not be scanned
220 for pointers (useful, e.g., for strings). Note though that this
221 memory is *not* cleared; be sure to initialize it to prevent
222 information leaks. */
224 scm_gc_malloc_pointerless (size_t size
, const char *what
)
226 return do_gc_malloc_atomic (size
, what
);
230 scm_gc_malloc (size_t size
, const char *what
)
232 return do_gc_malloc (size
, what
);
236 scm_gc_calloc (size_t size
, const char *what
)
238 /* `GC_MALLOC ()' always returns a zeroed buffer. */
239 return do_gc_malloc (size
, what
);
243 scm_gc_realloc (void *mem
, size_t old_size
, size_t new_size
, const char *what
)
245 return do_gc_realloc (mem
, new_size
, what
);
249 scm_gc_free (void *mem
, size_t size
, const char *what
)
255 scm_gc_strndup (const char *str
, size_t n
, const char *what
)
257 char *dst
= do_gc_malloc_atomic (n
+ 1, what
);
258 memcpy (dst
, str
, n
);
264 scm_gc_strdup (const char *str
, const char *what
)
266 return scm_gc_strndup (str
, strlen (str
), what
);
269 #if SCM_ENABLE_DEPRECATED == 1
271 /* {Deprecated front end to malloc}
273 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
276 * These functions provide services comparable to malloc, realloc, and
279 * There has been a fair amount of confusion around the use of these functions;
280 * see "Memory Blocks" in the manual. They are totally unnecessary in 2.0 given
285 scm_must_malloc (size_t size
, const char *what
)
287 scm_c_issue_deprecation_warning
288 ("scm_must_malloc is deprecated. "
289 "Use scm_gc_malloc and scm_gc_free instead.");
291 return scm_gc_malloc (size
, what
);
295 scm_must_realloc (void *where
,
300 scm_c_issue_deprecation_warning
301 ("scm_must_realloc is deprecated. "
302 "Use scm_gc_realloc and scm_gc_free instead.");
304 return scm_gc_realloc (where
, old_size
, size
, what
);
308 scm_must_strndup (const char *str
, size_t length
)
310 scm_c_issue_deprecation_warning
311 ("scm_must_strndup is deprecated. "
312 "Use scm_gc_strndup and scm_gc_free instead.");
314 return scm_gc_strndup (str
, length
, "string");
318 scm_must_strdup (const char *str
)
320 scm_c_issue_deprecation_warning
321 ("scm_must_strdup is deprecated. "
322 "Use scm_gc_strdup and scm_gc_free instead.");
324 return scm_gc_strdup (str
, "string");
328 scm_must_free (void *obj
)
329 #define FUNC_NAME "scm_must_free"
331 scm_c_issue_deprecation_warning
332 ("scm_must_free is deprecated. "
333 "Use scm_gc_malloc and scm_gc_free instead.");
341 scm_done_malloc (long size
)
343 scm_c_issue_deprecation_warning
344 ("scm_done_malloc is deprecated. "
345 "Use scm_gc_register_collectable_memory instead.");
348 scm_gc_register_collectable_memory (NULL
, size
, "foreign mallocs");
350 scm_gc_unregister_collectable_memory (NULL
, -size
, "foreign mallocs");
354 scm_done_free (long size
)
356 scm_c_issue_deprecation_warning
357 ("scm_done_free is deprecated. "
358 "Use scm_gc_unregister_collectable_memory instead.");
361 scm_gc_unregister_collectable_memory (NULL
, size
, "foreign mallocs");
363 scm_gc_register_collectable_memory (NULL
, -size
, "foreign mallocs");
366 #endif /* SCM_ENABLE_DEPRECATED == 1 */