1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 * 2004, 2006, 2008, 2009, 2010, 2011 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
32 extern unsigned long * __libc_ia64_register_backing_store_base
;
35 #include "libguile/_scm.h"
36 #include "libguile/eval.h"
37 #include "libguile/stime.h"
38 #include "libguile/stackchk.h"
39 #include "libguile/struct.h"
40 #include "libguile/smob.h"
41 #include "libguile/arrays.h"
42 #include "libguile/async.h"
43 #include "libguile/ports.h"
44 #include "libguile/root.h"
45 #include "libguile/strings.h"
46 #include "libguile/vectors.h"
47 #include "libguile/weaks.h"
48 #include "libguile/hashtab.h"
49 #include "libguile/tags.h"
51 #include "libguile/validate.h"
52 #include "libguile/deprecation.h"
53 #include "libguile/gc.h"
55 #include "libguile/private-gc.h"
57 #ifdef GUILE_DEBUG_MALLOC
58 #include "libguile/debug-malloc.h"
70 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
73 After startup (at the guile> prompt), we have approximately 100k of
74 alloced memory, which won't go away on GC. Let's set the init such
75 that we get a nice yield on the next allocation:
77 #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
78 #define SCM_DEFAULT_MALLOC_MINYIELD 40
80 /* #define DEBUGINFO */
84 /* Function for non-cell memory management.
88 scm_realloc (void *mem
, size_t size
)
92 SCM_SYSCALL (ptr
= realloc (mem
, size
));
96 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
99 SCM_SYSCALL (ptr
= realloc (mem
, size
));
103 scm_memory_error ("realloc");
107 scm_malloc (size_t sz
)
109 return scm_realloc (NULL
, sz
);
113 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
117 scm_calloc (size_t sz
)
122 By default, try to use calloc, as it is likely more efficient than
123 calling memset by hand.
125 SCM_SYSCALL (ptr
= calloc (sz
, 1));
129 ptr
= scm_realloc (NULL
, sz
);
130 memset (ptr
, 0x0, sz
);
136 scm_strndup (const char *str
, size_t n
)
138 char *dst
= scm_malloc (n
+ 1);
139 memcpy (dst
, str
, n
);
145 scm_strdup (const char *str
)
147 return scm_strndup (str
, strlen (str
));
153 scm_gc_register_collectable_memory (void *mem
, size_t size
, const char *what
)
156 #ifdef GUILE_DEBUG_MALLOC
158 scm_malloc_register (mem
, what
);
164 scm_gc_unregister_collectable_memory (void *mem
, size_t size
, const char *what
)
167 #ifdef GUILE_DEBUG_MALLOC
169 scm_malloc_unregister (mem
);
173 /* Allocate SIZE bytes of memory whose contents should not be scanned
174 for pointers (useful, e.g., for strings). Note though that this
175 memory is *not* cleared; be sure to initialize it to prevent
176 information leaks. */
178 scm_gc_malloc_pointerless (size_t size
, const char *what
)
180 return GC_MALLOC_ATOMIC (size
);
184 scm_gc_malloc (size_t size
, const char *what
)
189 /* `GC_MALLOC ()' doesn't handle zero. */
190 size
= sizeof (void *);
192 ptr
= GC_MALLOC (size
);
198 scm_gc_calloc (size_t size
, const char *what
)
200 /* `GC_MALLOC ()' always returns a zeroed buffer. */
201 return scm_gc_malloc (size
, what
);
206 scm_gc_realloc (void *mem
, size_t old_size
, size_t new_size
, const char *what
)
210 ptr
= GC_REALLOC (mem
, new_size
);
212 #ifdef GUILE_DEBUG_MALLOC
214 scm_malloc_reregister (mem
, ptr
, what
);
221 scm_gc_free (void *mem
, size_t size
, const char *what
)
223 scm_gc_unregister_collectable_memory (mem
, size
, what
);
228 scm_gc_strndup (const char *str
, size_t n
, const char *what
)
230 char *dst
= GC_MALLOC_ATOMIC (n
+ 1);
231 memcpy (dst
, str
, n
);
237 scm_gc_strdup (const char *str
, const char *what
)
239 return scm_gc_strndup (str
, strlen (str
), what
);
242 #if SCM_ENABLE_DEPRECATED == 1
244 /* {Deprecated front end to malloc}
246 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
249 * These functions provide services comparable to malloc, realloc, and
252 * There has been a fair amount of confusion around the use of these functions;
253 * see "Memory Blocks" in the manual. They are totally unnecessary in 2.0 given
258 scm_must_malloc (size_t size
, const char *what
)
260 scm_c_issue_deprecation_warning
261 ("scm_must_malloc is deprecated. "
262 "Use scm_gc_malloc and scm_gc_free instead.");
264 return scm_gc_malloc (size
, what
);
268 scm_must_realloc (void *where
,
273 scm_c_issue_deprecation_warning
274 ("scm_must_realloc is deprecated. "
275 "Use scm_gc_realloc and scm_gc_free instead.");
277 return scm_gc_realloc (where
, old_size
, size
, what
);
281 scm_must_strndup (const char *str
, size_t length
)
283 scm_c_issue_deprecation_warning
284 ("scm_must_strndup is deprecated. "
285 "Use scm_gc_strndup and scm_gc_free instead.");
287 return scm_gc_strndup (str
, length
, "string");
291 scm_must_strdup (const char *str
)
293 scm_c_issue_deprecation_warning
294 ("scm_must_strdup is deprecated. "
295 "Use scm_gc_strdup and scm_gc_free instead.");
297 return scm_gc_strdup (str
, "string");
301 scm_must_free (void *obj
)
302 #define FUNC_NAME "scm_must_free"
304 scm_c_issue_deprecation_warning
305 ("scm_must_free is deprecated. "
306 "Use scm_gc_malloc and scm_gc_free instead.");
308 #ifdef GUILE_DEBUG_MALLOC
309 scm_malloc_unregister (obj
);
318 scm_done_malloc (long size
)
320 scm_c_issue_deprecation_warning
321 ("scm_done_malloc is deprecated. "
322 "Use scm_gc_register_collectable_memory instead.");
325 scm_gc_register_collectable_memory (NULL
, size
, "foreign mallocs");
327 scm_gc_unregister_collectable_memory (NULL
, -size
, "foreign mallocs");
331 scm_done_free (long size
)
333 scm_c_issue_deprecation_warning
334 ("scm_done_free is deprecated. "
335 "Use scm_gc_unregister_collectable_memory instead.");
338 scm_gc_unregister_collectable_memory (NULL
, size
, "foreign mallocs");
340 scm_gc_register_collectable_memory (NULL
, -size
, "foreign mallocs");
343 #endif /* SCM_ENABLE_DEPRECATED == 1 */