1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 * 2004, 2006, 2008, 2009, 2010, 2011, 2012,
3 * 2014 Free Software Foundation, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 3 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 extern unsigned long * __libc_ia64_register_backing_store_base
;
37 #include "libguile/_scm.h"
38 #include "libguile/eval.h"
39 #include "libguile/stime.h"
40 #include "libguile/stackchk.h"
41 #include "libguile/struct.h"
42 #include "libguile/smob.h"
43 #include "libguile/arrays.h"
44 #include "libguile/async.h"
45 #include "libguile/ports.h"
46 #include "libguile/root.h"
47 #include "libguile/strings.h"
48 #include "libguile/vectors.h"
49 #include "libguile/weaks.h"
50 #include "libguile/hashtab.h"
51 #include "libguile/tags.h"
53 #include "libguile/validate.h"
54 #include "libguile/deprecation.h"
55 #include "libguile/gc.h"
57 #include "libguile/private-gc.h"
59 #ifdef GUILE_DEBUG_MALLOC
60 #include "libguile/debug-malloc.h"
66 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
69 After startup (at the guile> prompt), we have approximately 100k of
70 alloced memory, which won't go away on GC. Let's set the init such
71 that we get a nice yield on the next allocation:
73 #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
74 #define SCM_DEFAULT_MALLOC_MINYIELD 40
76 /* #define DEBUGINFO */
82 do_realloc (void *from
, size_t new_size
)
84 scm_gc_register_allocation (new_size
);
85 return realloc (from
, new_size
);
89 do_calloc (size_t n
, size_t size
)
91 scm_gc_register_allocation (size
);
92 return calloc (n
, size
);
96 do_gc_malloc (size_t size
, const char *what
)
98 /* Ensure nonzero size to be compatible with always-nonzero return of
100 return GC_MALLOC (size
? size
: sizeof (void *));
104 do_gc_malloc_atomic (size_t size
, const char *what
)
106 return GC_MALLOC_ATOMIC (size
? size
: sizeof (void *));
110 do_gc_realloc (void *from
, size_t size
, const char *what
)
112 return GC_REALLOC (from
, size
? size
: sizeof (void *));
116 do_gc_free (void *ptr
)
123 /* Function for non-cell memory management.
127 scm_realloc (void *mem
, size_t size
)
131 ptr
= do_realloc (mem
, size
);
133 if (ptr
|| size
== 0)
136 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
137 #ifdef HAVE_GC_GCOLLECT_AND_UNMAP
138 GC_gcollect_and_unmap ();
143 ptr
= do_realloc (mem
, size
);
147 scm_memory_error ("realloc");
151 scm_malloc (size_t sz
)
153 return scm_realloc (NULL
, sz
);
157 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
161 scm_calloc (size_t sz
)
166 By default, try to use calloc, as it is likely more efficient than
167 calling memset by hand.
169 ptr
= do_calloc (sz
, 1);
173 ptr
= scm_realloc (NULL
, sz
);
174 memset (ptr
, 0x0, sz
);
180 scm_strndup (const char *str
, size_t n
)
182 char *dst
= scm_malloc (n
+ 1);
183 memcpy (dst
, str
, n
);
189 scm_strdup (const char *str
)
191 return scm_strndup (str
, strlen (str
));
197 scm_gc_register_collectable_memory (void *mem
, size_t size
, const char *what
)
199 scm_gc_register_allocation (size
);
201 #ifdef GUILE_DEBUG_MALLOC
203 scm_malloc_register (mem
, what
);
209 scm_gc_unregister_collectable_memory (void *mem
, size_t size
, const char *what
)
212 #ifdef GUILE_DEBUG_MALLOC
214 scm_malloc_unregister (mem
);
218 /* Allocate SIZE bytes of memory whose contents should not be scanned
219 for pointers (useful, e.g., for strings). Note though that this
220 memory is *not* cleared; be sure to initialize it to prevent
221 information leaks. */
223 scm_gc_malloc_pointerless (size_t size
, const char *what
)
225 return do_gc_malloc_atomic (size
, what
);
229 scm_gc_malloc (size_t size
, const char *what
)
231 return do_gc_malloc (size
, what
);
235 scm_gc_calloc (size_t size
, const char *what
)
237 /* `GC_MALLOC ()' always returns a zeroed buffer. */
238 return do_gc_malloc (size
, what
);
242 scm_gc_realloc (void *mem
, size_t old_size
, size_t new_size
, const char *what
)
244 return do_gc_realloc (mem
, new_size
, what
);
248 scm_gc_free (void *mem
, size_t size
, const char *what
)
254 scm_gc_strndup (const char *str
, size_t n
, const char *what
)
256 char *dst
= do_gc_malloc_atomic (n
+ 1, what
);
257 memcpy (dst
, str
, n
);
263 scm_gc_strdup (const char *str
, const char *what
)
265 return scm_gc_strndup (str
, strlen (str
), what
);
268 #if SCM_ENABLE_DEPRECATED == 1
270 /* {Deprecated front end to malloc}
272 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
275 * These functions provide services comparable to malloc, realloc, and
278 * There has been a fair amount of confusion around the use of these functions;
279 * see "Memory Blocks" in the manual. They are totally unnecessary in 2.0 given
284 scm_must_malloc (size_t size
, const char *what
)
286 scm_c_issue_deprecation_warning
287 ("scm_must_malloc is deprecated. "
288 "Use scm_gc_malloc and scm_gc_free instead.");
290 return scm_gc_malloc (size
, what
);
294 scm_must_realloc (void *where
,
299 scm_c_issue_deprecation_warning
300 ("scm_must_realloc is deprecated. "
301 "Use scm_gc_realloc and scm_gc_free instead.");
303 return scm_gc_realloc (where
, old_size
, size
, what
);
307 scm_must_strndup (const char *str
, size_t length
)
309 scm_c_issue_deprecation_warning
310 ("scm_must_strndup is deprecated. "
311 "Use scm_gc_strndup and scm_gc_free instead.");
313 return scm_gc_strndup (str
, length
, "string");
317 scm_must_strdup (const char *str
)
319 scm_c_issue_deprecation_warning
320 ("scm_must_strdup is deprecated. "
321 "Use scm_gc_strdup and scm_gc_free instead.");
323 return scm_gc_strdup (str
, "string");
327 scm_must_free (void *obj
)
328 #define FUNC_NAME "scm_must_free"
330 scm_c_issue_deprecation_warning
331 ("scm_must_free is deprecated. "
332 "Use scm_gc_malloc and scm_gc_free instead.");
340 scm_done_malloc (long size
)
342 scm_c_issue_deprecation_warning
343 ("scm_done_malloc is deprecated. "
344 "Use scm_gc_register_collectable_memory instead.");
347 scm_gc_register_collectable_memory (NULL
, size
, "foreign mallocs");
349 scm_gc_unregister_collectable_memory (NULL
, -size
, "foreign mallocs");
353 scm_done_free (long size
)
355 scm_c_issue_deprecation_warning
356 ("scm_done_free is deprecated. "
357 "Use scm_gc_unregister_collectable_memory instead.");
360 scm_gc_unregister_collectable_memory (NULL
, size
, "foreign mallocs");
362 scm_gc_register_collectable_memory (NULL
, -size
, "foreign mallocs");
365 #endif /* SCM_ENABLE_DEPRECATED == 1 */