Merge commit '5e69ceb7a667377a61cb0c31d7ac20e245b3fafd'
[bpt/guile.git] / libguile / gc-malloc.c
1 /* Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2 * 2004, 2006, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
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.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
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
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 */
19
20
21 \f
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #ifdef __ia64__
32 #include <ucontext.h>
33 extern 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"
42 #include "libguile/arrays.h"
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/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 #ifdef GUILE_DEBUG_MALLOC
56 #include "libguile/debug-malloc.h"
57 #endif
58
59 #ifdef HAVE_UNISTD_H
60 #include <unistd.h>
61 #endif
62
63 /*
64 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
65 trigger a GC.
66
67 After startup (at the guile> prompt), we have approximately 100k of
68 alloced memory, which won't go away on GC. Let's set the init such
69 that we get a nice yield on the next allocation:
70 */
71 #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
72 #define SCM_DEFAULT_MALLOC_MINYIELD 40
73
74 /* #define DEBUGINFO */
75
76
77 \f
78
79 static void*
80 do_realloc (void *from, size_t new_size)
81 {
82 scm_gc_register_allocation (new_size);
83 return realloc (from, new_size);
84 }
85
86 static void*
87 do_calloc (size_t n, size_t size)
88 {
89 scm_gc_register_allocation (size);
90 return calloc (n, size);
91 }
92
93 static void*
94 do_gc_malloc (size_t size, const char *what)
95 {
96 /* Ensure nonzero size to be compatible with always-nonzero return of
97 glibc malloc. */
98 return GC_MALLOC (size ? size : sizeof (void *));
99 }
100
101 static void*
102 do_gc_malloc_atomic (size_t size, const char *what)
103 {
104 return GC_MALLOC_ATOMIC (size ? size : sizeof (void *));
105 }
106
107 static void*
108 do_gc_realloc (void *from, size_t size, const char *what)
109 {
110 return GC_REALLOC (from, size ? size : sizeof (void *));
111 }
112
113 static void
114 do_gc_free (void *ptr)
115 {
116 GC_FREE (ptr);
117 }
118
119
120 \f
121 /* Function for non-cell memory management.
122 */
123
124 void *
125 scm_realloc (void *mem, size_t size)
126 {
127 void *ptr;
128
129 ptr = do_realloc (mem, size);
130
131 if (ptr || size == 0)
132 return ptr;
133
134 /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */
135 GC_gcollect_and_unmap ();
136
137 ptr = do_realloc (mem, size);
138 if (ptr)
139 return ptr;
140
141 scm_memory_error ("realloc");
142 }
143
144 void *
145 scm_malloc (size_t sz)
146 {
147 return scm_realloc (NULL, sz);
148 }
149
150 /*
151 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
152 SIZEOF_ELT)? --hwn
153 */
154 void *
155 scm_calloc (size_t sz)
156 {
157 void * ptr;
158
159 /*
160 By default, try to use calloc, as it is likely more efficient than
161 calling memset by hand.
162 */
163 ptr = do_calloc (sz, 1);
164 if (ptr || sz == 0)
165 return ptr;
166
167 ptr = scm_realloc (NULL, sz);
168 memset (ptr, 0x0, sz);
169 return ptr;
170 }
171
172
173 char *
174 scm_strndup (const char *str, size_t n)
175 {
176 char *dst = scm_malloc (n + 1);
177 memcpy (dst, str, n);
178 dst[n] = 0;
179 return dst;
180 }
181
182 char *
183 scm_strdup (const char *str)
184 {
185 return scm_strndup (str, strlen (str));
186 }
187
188
189
190 void
191 scm_gc_register_collectable_memory (void *mem, size_t size, const char *what)
192 {
193 scm_gc_register_allocation (size);
194
195 #ifdef GUILE_DEBUG_MALLOC
196 if (mem)
197 scm_malloc_register (mem, what);
198 #endif
199 }
200
201
202 void
203 scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what)
204 {
205 /* Nothing to do. */
206 #ifdef GUILE_DEBUG_MALLOC
207 if (mem)
208 scm_malloc_unregister (mem);
209 #endif
210 }
211
212 /* Allocate SIZE bytes of memory whose contents should not be scanned
213 for pointers (useful, e.g., for strings). Note though that this
214 memory is *not* cleared; be sure to initialize it to prevent
215 information leaks. */
216 void *
217 scm_gc_malloc_pointerless (size_t size, const char *what)
218 {
219 return do_gc_malloc_atomic (size, what);
220 }
221
222 void *
223 scm_gc_malloc (size_t size, const char *what)
224 {
225 return do_gc_malloc (size, what);
226 }
227
228 void *
229 scm_gc_calloc (size_t size, const char *what)
230 {
231 /* `GC_MALLOC ()' always returns a zeroed buffer. */
232 return do_gc_malloc (size, what);
233 }
234
235 void *
236 scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what)
237 {
238 return do_gc_realloc (mem, new_size, what);
239 }
240
241 void
242 scm_gc_free (void *mem, size_t size, const char *what)
243 {
244 do_gc_free (mem);
245 }
246
247 char *
248 scm_gc_strndup (const char *str, size_t n, const char *what)
249 {
250 char *dst = do_gc_malloc_atomic (n + 1, what);
251 memcpy (dst, str, n);
252 dst[n] = 0;
253 return dst;
254 }
255
256 char *
257 scm_gc_strdup (const char *str, const char *what)
258 {
259 return scm_gc_strndup (str, strlen (str), what);
260 }