make-string et al nulls memory if not given an initializer
[bpt/guile.git] / libguile / gc-malloc.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2008, 2009, 2010 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 * 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28
29 #ifdef __ia64__
30 #include <ucontext.h>
31 extern unsigned long * __libc_ia64_register_backing_store_base;
32 #endif
33
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"
49
50 #include "libguile/validate.h"
51 #include "libguile/deprecation.h"
52 #include "libguile/gc.h"
53
54 #include "libguile/private-gc.h"
55
56 #ifdef GUILE_DEBUG_MALLOC
57 #include "libguile/debug-malloc.h"
58 #endif
59
60 #ifdef HAVE_MALLOC_H
61 #include <malloc.h>
62 #endif
63
64 #ifdef HAVE_UNISTD_H
65 #include <unistd.h>
66 #endif
67
68 /*
69 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
70 trigger a GC.
71
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:
75 */
76 #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
77 #define SCM_DEFAULT_MALLOC_MINYIELD 40
78
79 /* #define DEBUGINFO */
80
81
82 \f
83 /* Function for non-cell memory management.
84 */
85
86 void *
87 scm_realloc (void *mem, size_t size)
88 {
89 void *ptr;
90
91 SCM_SYSCALL (ptr = realloc (mem, size));
92 if (ptr)
93 return ptr;
94
95 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
96 GC_gcollect ();
97
98 SCM_SYSCALL (ptr = realloc (mem, size));
99 if (ptr)
100 return ptr;
101
102 scm_memory_error ("realloc");
103 }
104
105 void *
106 scm_malloc (size_t sz)
107 {
108 return scm_realloc (NULL, sz);
109 }
110
111 /*
112 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
113 SIZEOF_ELT)? --hwn
114 */
115 void *
116 scm_calloc (size_t sz)
117 {
118 void * ptr;
119
120 /*
121 By default, try to use calloc, as it is likely more efficient than
122 calling memset by hand.
123 */
124 SCM_SYSCALL (ptr = calloc (sz, 1));
125 if (ptr)
126 return ptr;
127
128 ptr = scm_realloc (NULL, sz);
129 memset (ptr, 0x0, sz);
130 return ptr;
131 }
132
133
134 char *
135 scm_strndup (const char *str, size_t n)
136 {
137 char *dst = scm_malloc (n + 1);
138 memcpy (dst, str, n);
139 dst[n] = 0;
140 return dst;
141 }
142
143 char *
144 scm_strdup (const char *str)
145 {
146 return scm_strndup (str, strlen (str));
147 }
148
149
150
151 void
152 scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
153 {
154 /* Nothing to do. */
155 #ifdef GUILE_DEBUG_MALLOC
156 if (mem)
157 scm_malloc_register (mem);
158 #endif
159 }
160
161
162 void
163 scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
164 {
165 /* Nothing to do. */
166 #ifdef GUILE_DEBUG_MALLOC
167 if (mem)
168 scm_malloc_unregister (mem);
169 #endif
170 }
171
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. */
176 void *
177 scm_gc_malloc_pointerless (size_t size, const char *what)
178 {
179 return GC_MALLOC_ATOMIC (size);
180 }
181
182 void *
183 scm_gc_malloc (size_t size, const char *what)
184 {
185 void *ptr;
186
187 if (size == 0)
188 /* `GC_MALLOC ()' doesn't handle zero. */
189 size = sizeof (void *);
190
191 ptr = GC_MALLOC (size);
192
193 return ptr;
194 }
195
196 void *
197 scm_gc_calloc (size_t size, const char *what)
198 {
199 /* `GC_MALLOC ()' always returns a zeroed buffer. */
200 return scm_gc_malloc (size, what);
201 }
202
203
204 void *
205 scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
206 {
207 void *ptr;
208
209 ptr = GC_REALLOC (mem, new_size);
210
211 #ifdef GUILE_DEBUG_MALLOC
212 if (mem)
213 scm_malloc_reregister (mem, ptr, what);
214 #endif
215
216 return ptr;
217 }
218
219 void
220 scm_gc_free (void *mem, size_t size, const char *what)
221 {
222 scm_gc_unregister_collectable_memory (mem, size, what);
223 GC_FREE (mem);
224 }
225
226 char *
227 scm_gc_strndup (const char *str, size_t n, const char *what)
228 {
229 char *dst = GC_MALLOC_ATOMIC (n + 1);
230 memcpy (dst, str, n);
231 dst[n] = 0;
232 return dst;
233 }
234
235 char *
236 scm_gc_strdup (const char *str, const char *what)
237 {
238 return scm_gc_strndup (str, strlen (str), what);
239 }
240
241 #if SCM_ENABLE_DEPRECATED == 1
242
243 /* {Deprecated front end to malloc}
244 *
245 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
246 * scm_done_free
247 *
248 * These functions provide services comparable to malloc, realloc, and
249 * free.
250 *
251 * There has been a fair amount of confusion around the use of these functions;
252 * see "Memory Blocks" in the manual. They are totally unnecessary in 2.0 given
253 * the Boehm GC.
254 */
255
256 void *
257 scm_must_malloc (size_t size, const char *what)
258 {
259 scm_c_issue_deprecation_warning
260 ("scm_must_malloc is deprecated. "
261 "Use scm_gc_malloc and scm_gc_free instead.");
262
263 return scm_gc_malloc (size, what);
264 }
265
266 void *
267 scm_must_realloc (void *where,
268 size_t old_size,
269 size_t size,
270 const char *what)
271 {
272 scm_c_issue_deprecation_warning
273 ("scm_must_realloc is deprecated. "
274 "Use scm_gc_realloc and scm_gc_free instead.");
275
276 return scm_gc_realloc (where, old_size, size, what);
277 }
278
279 char *
280 scm_must_strndup (const char *str, size_t length)
281 {
282 scm_c_issue_deprecation_warning
283 ("scm_must_strndup is deprecated. "
284 "Use scm_gc_strndup and scm_gc_free instead.");
285
286 return scm_gc_strndup (str, length, "string");
287 }
288
289 char *
290 scm_must_strdup (const char *str)
291 {
292 scm_c_issue_deprecation_warning
293 ("scm_must_strdup is deprecated. "
294 "Use scm_gc_strdup and scm_gc_free instead.");
295
296 return scm_gc_strdup (str, "string");
297 }
298
299 void
300 scm_must_free (void *obj)
301 #define FUNC_NAME "scm_must_free"
302 {
303 scm_c_issue_deprecation_warning
304 ("scm_must_free is deprecated. "
305 "Use scm_gc_malloc and scm_gc_free instead.");
306
307 #ifdef GUILE_DEBUG_MALLOC
308 scm_malloc_unregister (obj);
309 #endif
310
311 GC_FREE (obj);
312 }
313 #undef FUNC_NAME
314
315
316 void
317 scm_done_malloc (long size)
318 {
319 scm_c_issue_deprecation_warning
320 ("scm_done_malloc is deprecated. "
321 "Use scm_gc_register_collectable_memory instead.");
322
323 if (size >= 0)
324 scm_gc_register_collectable_memory (NULL, size, "foreign mallocs");
325 else
326 scm_gc_unregister_collectable_memory (NULL, -size, "foreign mallocs");
327 }
328
329 void
330 scm_done_free (long size)
331 {
332 scm_c_issue_deprecation_warning
333 ("scm_done_free is deprecated. "
334 "Use scm_gc_unregister_collectable_memory instead.");
335
336 if (size >= 0)
337 scm_gc_unregister_collectable_memory (NULL, size, "foreign mallocs");
338 else
339 scm_gc_register_collectable_memory (NULL, -size, "foreign mallocs");
340 }
341
342 #endif /* SCM_ENABLE_DEPRECATED == 1 */