Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / libguile / gc-malloc.c
CommitLineData
8fc5ef7d 1/* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
817307cc 2 * 2004, 2006, 2008, 2009, 2010, 2011, 2012 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"
c7743d02
HWN
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
c7743d02
HWN
61#ifdef HAVE_UNISTD_H
62#include <unistd.h>
63#endif
64
65/*
66 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
67 trigger a GC.
68
69 After startup (at the guile> prompt), we have approximately 100k of
70 alloced memory, which won't go away on GC. Let's set the init such
71 that we get a nice yield on the next allocation:
72*/
c2cbcc57 73#define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
c7743d02
HWN
74#define SCM_DEFAULT_MALLOC_MINYIELD 40
75
61ef9c1f 76/* #define DEBUGINFO */
c7743d02 77
c7743d02
HWN
78
79\f
817307cc
AW
80
81static void*
82do_realloc (void *from, size_t new_size)
83{
84 scm_gc_register_allocation (new_size);
85 return realloc (from, new_size);
86}
87
88static void*
89do_calloc (size_t n, size_t size)
90{
91 scm_gc_register_allocation (size);
92 return calloc (n, size);
93}
94
95static void*
96do_gc_malloc (size_t size, const char *what)
97{
98 /* Ensure nonzero size to be compatible with always-nonzero return of
99 glibc malloc. */
100 return GC_MALLOC (size ? size : sizeof (void *));
101}
102
103static void*
104do_gc_malloc_atomic (size_t size, const char *what)
105{
106 return GC_MALLOC_ATOMIC (size ? size : sizeof (void *));
107}
108
109static void*
110do_gc_realloc (void *from, size_t size, const char *what)
111{
112 return GC_REALLOC (from, size ? size : sizeof (void *));
113}
114
115static void
116do_gc_free (void *ptr)
117{
118 GC_FREE (ptr);
119}
120
121
122\f
c7743d02
HWN
123/* Function for non-cell memory management.
124 */
125
c7743d02
HWN
126void *
127scm_realloc (void *mem, size_t size)
128{
129 void *ptr;
130
817307cc 131 ptr = do_realloc (mem, size);
fd51e661 132
817307cc 133 if (ptr || size == 0)
c7743d02
HWN
134 return ptr;
135
26224b3f 136 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
4eb28612 137#ifdef HAVE_GC_GCOLLECT_AND_UNMAP
fd51e661 138 GC_gcollect_and_unmap ();
4eb28612
CJY
139#else
140 GC_gcollect ();
141#endif
b17e0ac3 142
817307cc 143 ptr = do_realloc (mem, size);
c7743d02
HWN
144 if (ptr)
145 return ptr;
146
147 scm_memory_error ("realloc");
148}
149
39e8f371
HWN
150void *
151scm_malloc (size_t sz)
152{
153 return scm_realloc (NULL, sz);
154}
ba1b2226
HWN
155
156/*
157 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
158 SIZEOF_ELT)? --hwn
159 */
160void *
161scm_calloc (size_t sz)
162{
1383773b
HWN
163 void * ptr;
164
165 /*
166 By default, try to use calloc, as it is likely more efficient than
167 calling memset by hand.
168 */
817307cc
AW
169 ptr = do_calloc (sz, 1);
170 if (ptr || sz == 0)
1383773b 171 return ptr;
26224b3f 172
1383773b 173 ptr = scm_realloc (NULL, sz);
ba1b2226
HWN
174 memset (ptr, 0x0, sz);
175 return ptr;
176}
177
39e8f371 178
c7743d02
HWN
179char *
180scm_strndup (const char *str, size_t n)
181{
fb50ef08 182 char *dst = scm_malloc (n + 1);
c7743d02
HWN
183 memcpy (dst, str, n);
184 dst[n] = 0;
185 return dst;
186}
187
188char *
189scm_strdup (const char *str)
190{
191 return scm_strndup (str, strlen (str));
192}
193
b17e0ac3 194
cbfe8e62
HWN
195
196void
197scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
198{
76f3ee77
AW
199 scm_gc_register_allocation (size);
200
c7743d02
HWN
201#ifdef GUILE_DEBUG_MALLOC
202 if (mem)
8fc5ef7d 203 scm_malloc_register (mem, what);
c7743d02
HWN
204#endif
205}
206
cbfe8e62 207
c7743d02
HWN
208void
209scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
210{
c5018a2b 211 /* Nothing to do. */
c7743d02
HWN
212#ifdef GUILE_DEBUG_MALLOC
213 if (mem)
214 scm_malloc_unregister (mem);
215#endif
216}
217
3ef6650d
AW
218/* Allocate SIZE bytes of memory whose contents should not be scanned
219 for pointers (useful, e.g., for strings). Note though that this
220 memory is *not* cleared; be sure to initialize it to prevent
221 information leaks. */
c5018a2b
LC
222void *
223scm_gc_malloc_pointerless (size_t size, const char *what)
224{
817307cc 225 return do_gc_malloc_atomic (size, what);
c5018a2b
LC
226}
227
c7743d02
HWN
228void *
229scm_gc_malloc (size_t size, const char *what)
230{
817307cc 231 return do_gc_malloc (size, what);
c7743d02
HWN
232}
233
39e8f371
HWN
234void *
235scm_gc_calloc (size_t size, const char *what)
236{
b1f6293e 237 /* `GC_MALLOC ()' always returns a zeroed buffer. */
817307cc 238 return do_gc_malloc (size, what);
39e8f371
HWN
239}
240
c7743d02
HWN
241void *
242scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
243{
817307cc 244 return do_gc_realloc (mem, new_size, what);
c7743d02
HWN
245}
246
247void
248scm_gc_free (void *mem, size_t size, const char *what)
249{
817307cc 250 do_gc_free (mem);
c7743d02
HWN
251}
252
253char *
254scm_gc_strndup (const char *str, size_t n, const char *what)
255{
817307cc 256 char *dst = do_gc_malloc_atomic (n + 1, what);
c7743d02
HWN
257 memcpy (dst, str, n);
258 dst[n] = 0;
259 return dst;
260}
261
262char *
263scm_gc_strdup (const char *str, const char *what)
264{
265 return scm_gc_strndup (str, strlen (str), what);
266}