Commit | Line | Data |
---|---|---|
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> | |
32 | extern 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 |
87 | void * |
88 | scm_realloc (void *mem, size_t size) | |
89 | { | |
90 | void *ptr; | |
91 | ||
fd51e661 AW |
92 | scm_gc_register_allocation (size); |
93 | ||
c7743d02 HWN |
94 | SCM_SYSCALL (ptr = realloc (mem, size)); |
95 | if (ptr) | |
96 | return ptr; | |
97 | ||
26224b3f | 98 | /* Time is hard: trigger a full, ``stop-the-world'' GC, and try again. */ |
fd51e661 | 99 | GC_gcollect_and_unmap (); |
b17e0ac3 | 100 | |
c7743d02 HWN |
101 | SCM_SYSCALL (ptr = realloc (mem, size)); |
102 | if (ptr) | |
103 | return ptr; | |
104 | ||
105 | scm_memory_error ("realloc"); | |
106 | } | |
107 | ||
39e8f371 HWN |
108 | void * |
109 | scm_malloc (size_t sz) | |
110 | { | |
111 | return scm_realloc (NULL, sz); | |
112 | } | |
ba1b2226 HWN |
113 | |
114 | /* | |
115 | Hmm. Should we use the C convention for arguments (i.e. N_ELTS, | |
116 | SIZEOF_ELT)? --hwn | |
117 | */ | |
118 | void * | |
119 | scm_calloc (size_t sz) | |
120 | { | |
1383773b HWN |
121 | void * ptr; |
122 | ||
123 | /* | |
124 | By default, try to use calloc, as it is likely more efficient than | |
125 | calling memset by hand. | |
126 | */ | |
fb50ef08 | 127 | SCM_SYSCALL (ptr = calloc (sz, 1)); |
1383773b HWN |
128 | if (ptr) |
129 | return ptr; | |
26224b3f | 130 | |
1383773b | 131 | ptr = scm_realloc (NULL, sz); |
ba1b2226 HWN |
132 | memset (ptr, 0x0, sz); |
133 | return ptr; | |
134 | } | |
135 | ||
39e8f371 | 136 | |
c7743d02 HWN |
137 | char * |
138 | scm_strndup (const char *str, size_t n) | |
139 | { | |
fb50ef08 | 140 | char *dst = scm_malloc (n + 1); |
c7743d02 HWN |
141 | memcpy (dst, str, n); |
142 | dst[n] = 0; | |
143 | return dst; | |
144 | } | |
145 | ||
146 | char * | |
147 | scm_strdup (const char *str) | |
148 | { | |
149 | return scm_strndup (str, strlen (str)); | |
150 | } | |
151 | ||
b17e0ac3 | 152 | |
cbfe8e62 HWN |
153 | |
154 | void | |
155 | scm_gc_register_collectable_memory (void *mem, size_t size, const char *what) | |
156 | { | |
c5018a2b | 157 | /* Nothing to do. */ |
c7743d02 HWN |
158 | #ifdef GUILE_DEBUG_MALLOC |
159 | if (mem) | |
8fc5ef7d | 160 | scm_malloc_register (mem, what); |
c7743d02 HWN |
161 | #endif |
162 | } | |
163 | ||
cbfe8e62 | 164 | |
c7743d02 HWN |
165 | void |
166 | scm_gc_unregister_collectable_memory (void *mem, size_t size, const char *what) | |
167 | { | |
c5018a2b | 168 | /* Nothing to do. */ |
c7743d02 HWN |
169 | #ifdef GUILE_DEBUG_MALLOC |
170 | if (mem) | |
171 | scm_malloc_unregister (mem); | |
172 | #endif | |
173 | } | |
174 | ||
3ef6650d AW |
175 | /* Allocate SIZE bytes of memory whose contents should not be scanned |
176 | for pointers (useful, e.g., for strings). Note though that this | |
177 | memory is *not* cleared; be sure to initialize it to prevent | |
178 | information leaks. */ | |
c5018a2b LC |
179 | void * |
180 | scm_gc_malloc_pointerless (size_t size, const char *what) | |
181 | { | |
182 | return GC_MALLOC_ATOMIC (size); | |
183 | } | |
184 | ||
c7743d02 HWN |
185 | void * |
186 | scm_gc_malloc (size_t size, const char *what) | |
187 | { | |
ea4f8ea1 LC |
188 | void *ptr; |
189 | ||
190 | if (size == 0) | |
191 | /* `GC_MALLOC ()' doesn't handle zero. */ | |
192 | size = sizeof (void *); | |
193 | ||
194 | ptr = GC_MALLOC (size); | |
195 | ||
c7743d02 HWN |
196 | return ptr; |
197 | } | |
198 | ||
39e8f371 HWN |
199 | void * |
200 | scm_gc_calloc (size_t size, const char *what) | |
201 | { | |
b1f6293e LC |
202 | /* `GC_MALLOC ()' always returns a zeroed buffer. */ |
203 | return scm_gc_malloc (size, what); | |
39e8f371 HWN |
204 | } |
205 | ||
206 | ||
c7743d02 HWN |
207 | void * |
208 | scm_gc_realloc (void *mem, size_t old_size, size_t new_size, const char *what) | |
209 | { | |
53f18a0d KR |
210 | void *ptr; |
211 | ||
26224b3f | 212 | ptr = GC_REALLOC (mem, new_size); |
cbfe8e62 HWN |
213 | |
214 | #ifdef GUILE_DEBUG_MALLOC | |
215 | if (mem) | |
216 | scm_malloc_reregister (mem, ptr, what); | |
217 | #endif | |
26224b3f | 218 | |
c7743d02 HWN |
219 | return ptr; |
220 | } | |
221 | ||
222 | void | |
223 | scm_gc_free (void *mem, size_t size, const char *what) | |
224 | { | |
225 | scm_gc_unregister_collectable_memory (mem, size, what); | |
26224b3f | 226 | GC_FREE (mem); |
c7743d02 HWN |
227 | } |
228 | ||
229 | char * | |
230 | scm_gc_strndup (const char *str, size_t n, const char *what) | |
231 | { | |
e7acfa88 | 232 | char *dst = GC_MALLOC_ATOMIC (n + 1); |
c7743d02 HWN |
233 | memcpy (dst, str, n); |
234 | dst[n] = 0; | |
235 | return dst; | |
236 | } | |
237 | ||
238 | char * | |
239 | scm_gc_strdup (const char *str, const char *what) | |
240 | { | |
241 | return scm_gc_strndup (str, strlen (str), what); | |
242 | } | |
243 | ||
244 | #if SCM_ENABLE_DEPRECATED == 1 | |
245 | ||
246 | /* {Deprecated front end to malloc} | |
247 | * | |
248 | * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc, | |
249 | * scm_done_free | |
250 | * | |
251 | * These functions provide services comparable to malloc, realloc, and | |
0eedfa5c | 252 | * free. |
c7743d02 | 253 | * |
0eedfa5c AW |
254 | * There has been a fair amount of confusion around the use of these functions; |
255 | * see "Memory Blocks" in the manual. They are totally unnecessary in 2.0 given | |
256 | * the Boehm GC. | |
c7743d02 HWN |
257 | */ |
258 | ||
259 | void * | |
260 | scm_must_malloc (size_t size, const char *what) | |
261 | { | |
262 | scm_c_issue_deprecation_warning | |
263 | ("scm_must_malloc is deprecated. " | |
264 | "Use scm_gc_malloc and scm_gc_free instead."); | |
265 | ||
266 | return scm_gc_malloc (size, what); | |
267 | } | |
268 | ||
269 | void * | |
270 | scm_must_realloc (void *where, | |
271 | size_t old_size, | |
272 | size_t size, | |
273 | const char *what) | |
274 | { | |
275 | scm_c_issue_deprecation_warning | |
276 | ("scm_must_realloc is deprecated. " | |
277 | "Use scm_gc_realloc and scm_gc_free instead."); | |
278 | ||
279 | return scm_gc_realloc (where, old_size, size, what); | |
280 | } | |
281 | ||
282 | char * | |
283 | scm_must_strndup (const char *str, size_t length) | |
284 | { | |
285 | scm_c_issue_deprecation_warning | |
286 | ("scm_must_strndup is deprecated. " | |
287 | "Use scm_gc_strndup and scm_gc_free instead."); | |
288 | ||
289 | return scm_gc_strndup (str, length, "string"); | |
290 | } | |
291 | ||
292 | char * | |
293 | scm_must_strdup (const char *str) | |
294 | { | |
295 | scm_c_issue_deprecation_warning | |
296 | ("scm_must_strdup is deprecated. " | |
297 | "Use scm_gc_strdup and scm_gc_free instead."); | |
298 | ||
299 | return scm_gc_strdup (str, "string"); | |
300 | } | |
301 | ||
302 | void | |
303 | scm_must_free (void *obj) | |
304 | #define FUNC_NAME "scm_must_free" | |
305 | { | |
306 | scm_c_issue_deprecation_warning | |
307 | ("scm_must_free is deprecated. " | |
308 | "Use scm_gc_malloc and scm_gc_free instead."); | |
309 | ||
310 | #ifdef GUILE_DEBUG_MALLOC | |
311 | scm_malloc_unregister (obj); | |
312 | #endif | |
c291b588 | 313 | |
0eedfa5c | 314 | GC_FREE (obj); |
c7743d02 HWN |
315 | } |
316 | #undef FUNC_NAME | |
317 | ||
318 | ||
319 | void | |
320 | scm_done_malloc (long size) | |
321 | { | |
322 | scm_c_issue_deprecation_warning | |
323 | ("scm_done_malloc is deprecated. " | |
324 | "Use scm_gc_register_collectable_memory instead."); | |
325 | ||
4cd3853f KR |
326 | if (size >= 0) |
327 | scm_gc_register_collectable_memory (NULL, size, "foreign mallocs"); | |
328 | else | |
329 | scm_gc_unregister_collectable_memory (NULL, -size, "foreign mallocs"); | |
c7743d02 HWN |
330 | } |
331 | ||
332 | void | |
333 | scm_done_free (long size) | |
334 | { | |
335 | scm_c_issue_deprecation_warning | |
336 | ("scm_done_free is deprecated. " | |
337 | "Use scm_gc_unregister_collectable_memory instead."); | |
338 | ||
4cd3853f KR |
339 | if (size >= 0) |
340 | scm_gc_unregister_collectable_memory (NULL, size, "foreign mallocs"); | |
341 | else | |
342 | scm_gc_register_collectable_memory (NULL, -size, "foreign mallocs"); | |
c7743d02 HWN |
343 | } |
344 | ||
345 | #endif /* SCM_ENABLE_DEPRECATED == 1 */ |