Merge remote-tracking branch 'origin/stable-2.0'
[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"
c7743d02
HWN
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*/
c2cbcc57 76#define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
c7743d02
HWN
77#define SCM_DEFAULT_MALLOC_MINYIELD 40
78
61ef9c1f 79/* #define DEBUGINFO */
c7743d02 80
c7743d02
HWN
81
82\f
83/* Function for non-cell memory management.
84 */
85
c7743d02
HWN
86void *
87scm_realloc (void *mem, size_t size)
88{
89 void *ptr;
90
fd51e661
AW
91 scm_gc_register_allocation (size);
92
c7743d02
HWN
93 SCM_SYSCALL (ptr = realloc (mem, size));
94 if (ptr)
95 return ptr;
96
26224b3f 97 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
4eb28612 98#ifdef HAVE_GC_GCOLLECT_AND_UNMAP
fd51e661 99 GC_gcollect_and_unmap ();
4eb28612
CJY
100#else
101 GC_gcollect ();
102#endif
b17e0ac3 103
c7743d02
HWN
104 SCM_SYSCALL (ptr = realloc (mem, size));
105 if (ptr)
106 return ptr;
107
108 scm_memory_error ("realloc");
109}
110
39e8f371
HWN
111void *
112scm_malloc (size_t sz)
113{
114 return scm_realloc (NULL, sz);
115}
ba1b2226
HWN
116
117/*
118 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
119 SIZEOF_ELT)? --hwn
120 */
121void *
122scm_calloc (size_t sz)
123{
1383773b
HWN
124 void * ptr;
125
126 /*
127 By default, try to use calloc, as it is likely more efficient than
128 calling memset by hand.
129 */
fb50ef08 130 SCM_SYSCALL (ptr = calloc (sz, 1));
1383773b
HWN
131 if (ptr)
132 return ptr;
26224b3f 133
1383773b 134 ptr = scm_realloc (NULL, sz);
ba1b2226
HWN
135 memset (ptr, 0x0, sz);
136 return ptr;
137}
138
39e8f371 139
c7743d02
HWN
140char *
141scm_strndup (const char *str, size_t n)
142{
fb50ef08 143 char *dst = scm_malloc (n + 1);
c7743d02
HWN
144 memcpy (dst, str, n);
145 dst[n] = 0;
146 return dst;
147}
148
149char *
150scm_strdup (const char *str)
151{
152 return scm_strndup (str, strlen (str));
153}
154
b17e0ac3 155
cbfe8e62
HWN
156
157void
158scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
159{
76f3ee77
AW
160 scm_gc_register_allocation (size);
161
c7743d02
HWN
162#ifdef GUILE_DEBUG_MALLOC
163 if (mem)
8fc5ef7d 164 scm_malloc_register (mem, what);
c7743d02
HWN
165#endif
166}
167
cbfe8e62 168
c7743d02
HWN
169void
170scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
171{
c5018a2b 172 /* Nothing to do. */
c7743d02
HWN
173#ifdef GUILE_DEBUG_MALLOC
174 if (mem)
175 scm_malloc_unregister (mem);
176#endif
177}
178
3ef6650d
AW
179/* Allocate SIZE bytes of memory whose contents should not be scanned
180 for pointers (useful, e.g., for strings). Note though that this
181 memory is *not* cleared; be sure to initialize it to prevent
182 information leaks. */
c5018a2b
LC
183void *
184scm_gc_malloc_pointerless (size_t size, const char *what)
185{
186 return GC_MALLOC_ATOMIC (size);
187}
188
c7743d02
HWN
189void *
190scm_gc_malloc (size_t size, const char *what)
191{
ea4f8ea1
LC
192 void *ptr;
193
194 if (size == 0)
195 /* `GC_MALLOC ()' doesn't handle zero. */
196 size = sizeof (void *);
197
198 ptr = GC_MALLOC (size);
199
c7743d02
HWN
200 return ptr;
201}
202
39e8f371
HWN
203void *
204scm_gc_calloc (size_t size, const char *what)
205{
b1f6293e
LC
206 /* `GC_MALLOC ()' always returns a zeroed buffer. */
207 return scm_gc_malloc (size, what);
39e8f371
HWN
208}
209
210
c7743d02
HWN
211void *
212scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
213{
53f18a0d
KR
214 void *ptr;
215
26224b3f 216 ptr = GC_REALLOC (mem, new_size);
cbfe8e62
HWN
217
218#ifdef GUILE_DEBUG_MALLOC
219 if (mem)
220 scm_malloc_reregister (mem, ptr, what);
221#endif
26224b3f 222
c7743d02
HWN
223 return ptr;
224}
225
226void
227scm_gc_free (void *mem, size_t size, const char *what)
228{
229 scm_gc_unregister_collectable_memory (mem, size, what);
26224b3f 230 GC_FREE (mem);
c7743d02
HWN
231}
232
233char *
234scm_gc_strndup (const char *str, size_t n, const char *what)
235{
e7acfa88 236 char *dst = GC_MALLOC_ATOMIC (n + 1);
c7743d02
HWN
237 memcpy (dst, str, n);
238 dst[n] = 0;
239 return dst;
240}
241
242char *
243scm_gc_strdup (const char *str, const char *what)
244{
245 return scm_gc_strndup (str, strlen (str), what);
246}