Merge from lcourtes@laas.fr--2005-mobile
[bpt/guile.git] / libguile / gc-malloc.c
CommitLineData
2b829bbb 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
c7743d02 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
c7743d02 7 *
73be1d9e 8 * This library is distributed in the hope that it will be useful,
c7743d02 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
c7743d02 12 *
73be1d9e
MV
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
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
c7743d02
HWN
17
18
19\f
132fa21f
RB
20#if HAVE_CONFIG_H
21# include <config.h>
22#endif
23
c7743d02
HWN
24#include <stdio.h>
25#include <errno.h>
26#include <string.h>
27
28#ifdef __ia64__
29#include <ucontext.h>
30extern unsigned long * __libc_ia64_register_backing_store_base;
31#endif
32
33#include "libguile/_scm.h"
34#include "libguile/eval.h"
35#include "libguile/stime.h"
36#include "libguile/stackchk.h"
37#include "libguile/struct.h"
38#include "libguile/smob.h"
39#include "libguile/unif.h"
40#include "libguile/async.h"
41#include "libguile/ports.h"
42#include "libguile/root.h"
43#include "libguile/strings.h"
44#include "libguile/vectors.h"
45#include "libguile/weaks.h"
46#include "libguile/hashtab.h"
47#include "libguile/tags.h"
48
49#include "libguile/validate.h"
50#include "libguile/deprecation.h"
51#include "libguile/gc.h"
52
53#include "libguile/private-gc.h"
54
55#ifdef GUILE_DEBUG_MALLOC
56#include "libguile/debug-malloc.h"
57#endif
58
59#ifdef HAVE_MALLOC_H
60#include <malloc.h>
61#endif
62
63#ifdef HAVE_UNISTD_H
64#include <unistd.h>
65#endif
66
67/*
68 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
69 trigger a GC.
70
71 After startup (at the guile> prompt), we have approximately 100k of
72 alloced memory, which won't go away on GC. Let's set the init such
73 that we get a nice yield on the next allocation:
74*/
c2cbcc57 75#define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
c7743d02
HWN
76#define SCM_DEFAULT_MALLOC_MINYIELD 40
77
61ef9c1f 78/* #define DEBUGINFO */
c7743d02
HWN
79
80static int scm_i_minyield_malloc;
81
82void
83scm_gc_init_malloc (void)
84{
85 scm_mtrigger = scm_getenv_int ("GUILE_INIT_MALLOC_LIMIT",
86 SCM_DEFAULT_INIT_MALLOC_LIMIT);
87 scm_i_minyield_malloc = scm_getenv_int ("GUILE_MIN_YIELD_MALLOC",
88 SCM_DEFAULT_MALLOC_MINYIELD);
dac04e9f
HWN
89
90 if (scm_i_minyield_malloc >= 100)
91 scm_i_minyield_malloc = 99;
92 if (scm_i_minyield_malloc < 1)
93 scm_i_minyield_malloc = 1;
94
95 if (scm_mtrigger < 0)
96 scm_mtrigger = SCM_DEFAULT_INIT_MALLOC_LIMIT;
c7743d02
HWN
97}
98
99
100\f
101/* Function for non-cell memory management.
102 */
103
c7743d02
HWN
104void *
105scm_realloc (void *mem, size_t size)
106{
107 void *ptr;
108
109 SCM_SYSCALL (ptr = realloc (mem, size));
110 if (ptr)
111 return ptr;
112
26224b3f
LC
113 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
114 GC_gcollect ();
b17e0ac3 115
c7743d02
HWN
116 SCM_SYSCALL (ptr = realloc (mem, size));
117 if (ptr)
118 return ptr;
119
120 scm_memory_error ("realloc");
121}
122
39e8f371
HWN
123void *
124scm_malloc (size_t sz)
125{
126 return scm_realloc (NULL, sz);
127}
ba1b2226
HWN
128
129/*
130 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
131 SIZEOF_ELT)? --hwn
132 */
133void *
134scm_calloc (size_t sz)
135{
1383773b
HWN
136 void * ptr;
137
138 /*
139 By default, try to use calloc, as it is likely more efficient than
140 calling memset by hand.
141 */
fb50ef08 142 SCM_SYSCALL (ptr = calloc (sz, 1));
1383773b
HWN
143 if (ptr)
144 return ptr;
26224b3f 145
1383773b 146 ptr = scm_realloc (NULL, sz);
ba1b2226
HWN
147 memset (ptr, 0x0, sz);
148 return ptr;
149}
150
39e8f371 151
c7743d02
HWN
152char *
153scm_strndup (const char *str, size_t n)
154{
fb50ef08 155 char *dst = scm_malloc (n + 1);
c7743d02
HWN
156 memcpy (dst, str, n);
157 dst[n] = 0;
158 return dst;
159}
160
161char *
162scm_strdup (const char *str)
163{
164 return scm_strndup (str, strlen (str));
165}
166
b17e0ac3 167
cbfe8e62
HWN
168
169void
170scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
171{
c7743d02
HWN
172#ifdef GUILE_DEBUG_MALLOC
173 if (mem)
26224b3f 174 scm_malloc_register (mem);
c7743d02 175#endif
26224b3f 176 fprintf (stderr, "%s: nothing done\n", __FUNCTION__); /* FIXME: What to do? */
c7743d02
HWN
177}
178
cbfe8e62 179
c7743d02
HWN
180void
181scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
182{
c7743d02
HWN
183#ifdef GUILE_DEBUG_MALLOC
184 if (mem)
185 scm_malloc_unregister (mem);
186#endif
187}
188
189void *
190scm_gc_malloc (size_t size, const char *what)
191{
192 /*
193 The straightforward implementation below has the problem
194 that it might call the GC twice, once in scm_malloc and then
195 again in scm_gc_register_collectable_memory. We don't really
196 want the second GC since it will not find new garbage.
197
c7743d02
HWN
198 Note: this is a theoretical peeve. In reality, malloc() never
199 returns NULL. Usually, memory is overcommitted, and when you try
200 to write it the program is killed with signal 11. --hwn
201 */
202
26224b3f 203 void *ptr = GC_MALLOC (size);
c7743d02
HWN
204 return ptr;
205}
206
39e8f371
HWN
207void *
208scm_gc_calloc (size_t size, const char *what)
209{
210 void *ptr = scm_gc_malloc (size, what);
211 memset (ptr, 0x0, size);
212 return ptr;
213}
214
215
c7743d02
HWN
216void *
217scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
218{
53f18a0d
KR
219 void *ptr;
220
26224b3f 221 ptr = GC_REALLOC (mem, new_size);
cbfe8e62
HWN
222
223#ifdef GUILE_DEBUG_MALLOC
224 if (mem)
225 scm_malloc_reregister (mem, ptr, what);
226#endif
26224b3f 227
c7743d02
HWN
228 return ptr;
229}
230
231void
232scm_gc_free (void *mem, size_t size, const char *what)
233{
234 scm_gc_unregister_collectable_memory (mem, size, what);
26224b3f 235 GC_FREE (mem);
c7743d02
HWN
236}
237
238char *
239scm_gc_strndup (const char *str, size_t n, const char *what)
240{
26224b3f 241 char *dst = GC_MALLOC (n+1);
c7743d02
HWN
242 memcpy (dst, str, n);
243 dst[n] = 0;
244 return dst;
245}
246
247char *
248scm_gc_strdup (const char *str, const char *what)
249{
250 return scm_gc_strndup (str, strlen (str), what);
251}
252
253#if SCM_ENABLE_DEPRECATED == 1
254
255/* {Deprecated front end to malloc}
256 *
257 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
258 * scm_done_free
259 *
260 * These functions provide services comparable to malloc, realloc, and
261 * free. They should be used when allocating memory that will be under
262 * control of the garbage collector, i.e., if the memory may be freed
263 * during garbage collection.
264 *
265 * They are deprecated because they weren't really used the way
266 * outlined above, and making sure to return the right amount from
267 * smob free routines was sometimes difficult when dealing with nested
268 * data structures. We basically want everybody to review their code
269 * and use the more symmetrical scm_gc_malloc/scm_gc_free functions
270 * instead. In some cases, where scm_must_malloc has been used
271 * incorrectly (i.e. for non-GC-able memory), use scm_malloc/free.
272 */
273
274void *
275scm_must_malloc (size_t size, const char *what)
276{
277 scm_c_issue_deprecation_warning
278 ("scm_must_malloc is deprecated. "
279 "Use scm_gc_malloc and scm_gc_free instead.");
280
281 return scm_gc_malloc (size, what);
282}
283
284void *
285scm_must_realloc (void *where,
286 size_t old_size,
287 size_t size,
288 const char *what)
289{
290 scm_c_issue_deprecation_warning
291 ("scm_must_realloc is deprecated. "
292 "Use scm_gc_realloc and scm_gc_free instead.");
293
294 return scm_gc_realloc (where, old_size, size, what);
295}
296
297char *
298scm_must_strndup (const char *str, size_t length)
299{
300 scm_c_issue_deprecation_warning
301 ("scm_must_strndup is deprecated. "
302 "Use scm_gc_strndup and scm_gc_free instead.");
303
304 return scm_gc_strndup (str, length, "string");
305}
306
307char *
308scm_must_strdup (const char *str)
309{
310 scm_c_issue_deprecation_warning
311 ("scm_must_strdup is deprecated. "
312 "Use scm_gc_strdup and scm_gc_free instead.");
313
314 return scm_gc_strdup (str, "string");
315}
316
317void
318scm_must_free (void *obj)
319#define FUNC_NAME "scm_must_free"
320{
321 scm_c_issue_deprecation_warning
322 ("scm_must_free is deprecated. "
323 "Use scm_gc_malloc and scm_gc_free instead.");
324
325#ifdef GUILE_DEBUG_MALLOC
326 scm_malloc_unregister (obj);
327#endif
328 if (obj)
329 free (obj);
330 else
be3ff021
HWN
331 {
332 fprintf (stderr,"freeing NULL pointer");
333 abort ();
334 }
c7743d02
HWN
335}
336#undef FUNC_NAME
337
338
339void
340scm_done_malloc (long size)
341{
342 scm_c_issue_deprecation_warning
343 ("scm_done_malloc is deprecated. "
344 "Use scm_gc_register_collectable_memory instead.");
345
4cd3853f
KR
346 if (size >= 0)
347 scm_gc_register_collectable_memory (NULL, size, "foreign mallocs");
348 else
349 scm_gc_unregister_collectable_memory (NULL, -size, "foreign mallocs");
c7743d02
HWN
350}
351
352void
353scm_done_free (long size)
354{
355 scm_c_issue_deprecation_warning
356 ("scm_done_free is deprecated. "
357 "Use scm_gc_unregister_collectable_memory instead.");
358
4cd3853f
KR
359 if (size >= 0)
360 scm_gc_unregister_collectable_memory (NULL, size, "foreign mallocs");
361 else
362 scm_gc_register_collectable_memory (NULL, -size, "foreign mallocs");
c7743d02
HWN
363}
364
365#endif /* SCM_ENABLE_DEPRECATED == 1 */