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