Update Gnulib to v0.0-6827-g39c3009; use the `dirfd' module.
[bpt/guile.git] / libguile / gc-malloc.c
CommitLineData
8fc5ef7d
BT
1/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 * 2004, 2006, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
c7743d02 3 *
73be1d9e 4 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
c7743d02 8 *
53befeb7
NJ
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
c7743d02 13 *
73be1d9e
MV
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
53befeb7
NJ
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
73be1d9e 18 */
c7743d02
HWN
19
20
21\f
dbb605f5 22#ifdef HAVE_CONFIG_H
132fa21f
RB
23# include <config.h>
24#endif
25
c7743d02
HWN
26#include <stdio.h>
27#include <errno.h>
28#include <string.h>
34cf38c3 29#include <stdlib.h>
c7743d02
HWN
30
31#ifdef __ia64__
32#include <ucontext.h>
33extern unsigned long * __libc_ia64_register_backing_store_base;
34#endif
35
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"
2fa901a5 42#include "libguile/arrays.h"
c7743d02
HWN
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"
51
52#include "libguile/validate.h"
53#include "libguile/deprecation.h"
54#include "libguile/gc.h"
55
56#include "libguile/private-gc.h"
57
58#ifdef GUILE_DEBUG_MALLOC
59#include "libguile/debug-malloc.h"
60#endif
61
c7743d02
HWN
62#ifdef HAVE_UNISTD_H
63#include <unistd.h>
64#endif
65
66/*
67 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
68 trigger a GC.
69
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:
73*/
c2cbcc57 74#define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
c7743d02
HWN
75#define SCM_DEFAULT_MALLOC_MINYIELD 40
76
61ef9c1f 77/* #define DEBUGINFO */
c7743d02 78
c7743d02
HWN
79
80\f
81/* Function for non-cell memory management.
82 */
83
c7743d02
HWN
84void *
85scm_realloc (void *mem, size_t size)
86{
87 void *ptr;
88
fd51e661
AW
89 scm_gc_register_allocation (size);
90
c7743d02
HWN
91 SCM_SYSCALL (ptr = realloc (mem, size));
92 if (ptr)
93 return ptr;
94
26224b3f 95 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
4eb28612 96#ifdef HAVE_GC_GCOLLECT_AND_UNMAP
fd51e661 97 GC_gcollect_and_unmap ();
4eb28612
CJY
98#else
99 GC_gcollect ();
100#endif
b17e0ac3 101
c7743d02
HWN
102 SCM_SYSCALL (ptr = realloc (mem, size));
103 if (ptr)
104 return ptr;
105
106 scm_memory_error ("realloc");
107}
108
39e8f371
HWN
109void *
110scm_malloc (size_t sz)
111{
112 return scm_realloc (NULL, sz);
113}
ba1b2226
HWN
114
115/*
116 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
117 SIZEOF_ELT)? --hwn
118 */
119void *
120scm_calloc (size_t sz)
121{
1383773b
HWN
122 void * ptr;
123
124 /*
125 By default, try to use calloc, as it is likely more efficient than
126 calling memset by hand.
127 */
fb50ef08 128 SCM_SYSCALL (ptr = calloc (sz, 1));
1383773b
HWN
129 if (ptr)
130 return ptr;
26224b3f 131
1383773b 132 ptr = scm_realloc (NULL, sz);
ba1b2226
HWN
133 memset (ptr, 0x0, sz);
134 return ptr;
135}
136
39e8f371 137
c7743d02
HWN
138char *
139scm_strndup (const char *str, size_t n)
140{
fb50ef08 141 char *dst = scm_malloc (n + 1);
c7743d02
HWN
142 memcpy (dst, str, n);
143 dst[n] = 0;
144 return dst;
145}
146
147char *
148scm_strdup (const char *str)
149{
150 return scm_strndup (str, strlen (str));
151}
152
b17e0ac3 153
cbfe8e62
HWN
154
155void
156scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
157{
76f3ee77
AW
158 scm_gc_register_allocation (size);
159
c7743d02
HWN
160#ifdef GUILE_DEBUG_MALLOC
161 if (mem)
8fc5ef7d 162 scm_malloc_register (mem, what);
c7743d02
HWN
163#endif
164}
165
cbfe8e62 166
c7743d02
HWN
167void
168scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
169{
c5018a2b 170 /* Nothing to do. */
c7743d02
HWN
171#ifdef GUILE_DEBUG_MALLOC
172 if (mem)
173 scm_malloc_unregister (mem);
174#endif
175}
176
3ef6650d
AW
177/* Allocate SIZE bytes of memory whose contents should not be scanned
178 for pointers (useful, e.g., for strings). Note though that this
179 memory is *not* cleared; be sure to initialize it to prevent
180 information leaks. */
c5018a2b
LC
181void *
182scm_gc_malloc_pointerless (size_t size, const char *what)
183{
184 return GC_MALLOC_ATOMIC (size);
185}
186
c7743d02
HWN
187void *
188scm_gc_malloc (size_t size, const char *what)
189{
ea4f8ea1
LC
190 void *ptr;
191
192 if (size == 0)
193 /* `GC_MALLOC ()' doesn't handle zero. */
194 size = sizeof (void *);
195
196 ptr = GC_MALLOC (size);
197
c7743d02
HWN
198 return ptr;
199}
200
39e8f371
HWN
201void *
202scm_gc_calloc (size_t size, const char *what)
203{
b1f6293e
LC
204 /* `GC_MALLOC ()' always returns a zeroed buffer. */
205 return scm_gc_malloc (size, what);
39e8f371
HWN
206}
207
208
c7743d02
HWN
209void *
210scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
211{
53f18a0d
KR
212 void *ptr;
213
26224b3f 214 ptr = GC_REALLOC (mem, new_size);
cbfe8e62
HWN
215
216#ifdef GUILE_DEBUG_MALLOC
217 if (mem)
218 scm_malloc_reregister (mem, ptr, what);
219#endif
26224b3f 220
c7743d02
HWN
221 return ptr;
222}
223
224void
225scm_gc_free (void *mem, size_t size, const char *what)
226{
227 scm_gc_unregister_collectable_memory (mem, size, what);
26224b3f 228 GC_FREE (mem);
c7743d02
HWN
229}
230
231char *
232scm_gc_strndup (const char *str, size_t n, const char *what)
233{
e7acfa88 234 char *dst = GC_MALLOC_ATOMIC (n + 1);
c7743d02
HWN
235 memcpy (dst, str, n);
236 dst[n] = 0;
237 return dst;
238}
239
240char *
241scm_gc_strdup (const char *str, const char *what)
242{
243 return scm_gc_strndup (str, strlen (str), what);
244}
245
246#if SCM_ENABLE_DEPRECATED == 1
247
248/* {Deprecated front end to malloc}
249 *
250 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
251 * scm_done_free
252 *
253 * These functions provide services comparable to malloc, realloc, and
0eedfa5c 254 * free.
c7743d02 255 *
0eedfa5c
AW
256 * There has been a fair amount of confusion around the use of these functions;
257 * see "Memory Blocks" in the manual. They are totally unnecessary in 2.0 given
258 * the Boehm GC.
c7743d02
HWN
259 */
260
261void *
262scm_must_malloc (size_t size, const char *what)
263{
264 scm_c_issue_deprecation_warning
265 ("scm_must_malloc is deprecated. "
266 "Use scm_gc_malloc and scm_gc_free instead.");
267
268 return scm_gc_malloc (size, what);
269}
270
271void *
272scm_must_realloc (void *where,
273 size_t old_size,
274 size_t size,
275 const char *what)
276{
277 scm_c_issue_deprecation_warning
278 ("scm_must_realloc is deprecated. "
279 "Use scm_gc_realloc and scm_gc_free instead.");
280
281 return scm_gc_realloc (where, old_size, size, what);
282}
283
284char *
285scm_must_strndup (const char *str, size_t length)
286{
287 scm_c_issue_deprecation_warning
288 ("scm_must_strndup is deprecated. "
289 "Use scm_gc_strndup and scm_gc_free instead.");
290
291 return scm_gc_strndup (str, length, "string");
292}
293
294char *
295scm_must_strdup (const char *str)
296{
297 scm_c_issue_deprecation_warning
298 ("scm_must_strdup is deprecated. "
299 "Use scm_gc_strdup and scm_gc_free instead.");
300
301 return scm_gc_strdup (str, "string");
302}
303
304void
305scm_must_free (void *obj)
306#define FUNC_NAME "scm_must_free"
307{
308 scm_c_issue_deprecation_warning
309 ("scm_must_free is deprecated. "
310 "Use scm_gc_malloc and scm_gc_free instead.");
311
312#ifdef GUILE_DEBUG_MALLOC
313 scm_malloc_unregister (obj);
314#endif
c291b588 315
0eedfa5c 316 GC_FREE (obj);
c7743d02
HWN
317}
318#undef FUNC_NAME
319
320
321void
322scm_done_malloc (long size)
323{
324 scm_c_issue_deprecation_warning
325 ("scm_done_malloc is deprecated. "
326 "Use scm_gc_register_collectable_memory instead.");
327
4cd3853f
KR
328 if (size >= 0)
329 scm_gc_register_collectable_memory (NULL, size, "foreign mallocs");
330 else
331 scm_gc_unregister_collectable_memory (NULL, -size, "foreign mallocs");
c7743d02
HWN
332}
333
334void
335scm_done_free (long size)
336{
337 scm_c_issue_deprecation_warning
338 ("scm_done_free is deprecated. "
339 "Use scm_gc_unregister_collectable_memory instead.");
340
4cd3853f
KR
341 if (size >= 0)
342 scm_gc_unregister_collectable_memory (NULL, size, "foreign mallocs");
343 else
344 scm_gc_register_collectable_memory (NULL, -size, "foreign mallocs");
c7743d02
HWN
345}
346
347#endif /* SCM_ENABLE_DEPRECATED == 1 */