1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004 Free Software Foundation, Inc.
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.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
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
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 extern unsigned long * __libc_ia64_register_backing_store_base
;
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"
49 #include "libguile/validate.h"
50 #include "libguile/deprecation.h"
51 #include "libguile/gc.h"
53 #include "libguile/private-gc.h"
55 #ifdef GUILE_DEBUG_MALLOC
56 #include "libguile/debug-malloc.h"
68 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
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:
75 #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
76 #define SCM_DEFAULT_MALLOC_MINYIELD 40
78 /* #define DEBUGINFO */
80 static int scm_i_minyield_malloc
;
83 scm_gc_init_malloc (void)
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
);
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;
96 scm_mtrigger
= SCM_DEFAULT_INIT_MALLOC_LIMIT
;
101 /* Function for non-cell memory management.
105 scm_realloc (void *mem
, size_t size
)
109 SCM_SYSCALL (ptr
= realloc (mem
, size
));
113 scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex
);
114 scm_gc_running_p
= 1;
116 scm_i_sweep_all_segments ("realloc");
118 SCM_SYSCALL (ptr
= realloc (mem
, size
));
121 scm_gc_running_p
= 0;
122 scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex
);
126 scm_i_gc ("realloc");
127 scm_i_sweep_all_segments ("realloc");
129 scm_gc_running_p
= 0;
130 scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex
);
132 SCM_SYSCALL (ptr
= realloc (mem
, size
));
136 scm_memory_error ("realloc");
140 scm_malloc (size_t sz
)
142 return scm_realloc (NULL
, sz
);
146 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
150 scm_calloc (size_t sz
)
155 By default, try to use calloc, as it is likely more efficient than
156 calling memset by hand.
158 SCM_SYSCALL (ptr
= calloc (sz
, 1));
162 ptr
= scm_realloc (NULL
, sz
);
163 memset (ptr
, 0x0, sz
);
169 scm_strndup (const char *str
, size_t n
)
171 char *dst
= scm_malloc (n
+ 1);
172 memcpy (dst
, str
, n
);
178 scm_strdup (const char *str
)
180 return scm_strndup (str
, strlen (str
));
184 decrease_mtrigger (size_t size
, const char * what
)
186 scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex
);
187 scm_mallocated
-= size
;
188 scm_gc_malloc_collected
+= size
;
189 scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex
);
193 increase_mtrigger (size_t size
, const char *what
)
195 size_t mallocated
= 0;
196 int overflow
= 0, triggered
= 0;
198 scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex
);
199 if (ULONG_MAX
- size
< scm_mallocated
)
203 scm_mallocated
+= size
;
204 mallocated
= scm_mallocated
;
205 if (scm_mallocated
> scm_mtrigger
)
208 scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex
);
211 scm_memory_error ("Overflow of scm_mallocated: too much memory in use.");
214 A program that uses a lot of malloced collectable memory (vectors,
215 strings), will use a lot of memory off the cell-heap; it needs to
216 do GC more often (before cells are exhausted), otherwise swapping
217 and malloc management will tie it down.
221 unsigned long prev_alloced
;
224 scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex
);
225 scm_gc_running_p
= 1;
227 prev_alloced
= mallocated
;
229 scm_i_sweep_all_segments ("mtrigger");
231 yield
= (((float) prev_alloced
- (float) scm_mallocated
)
232 / (float) prev_alloced
);
234 scm_gc_malloc_yield_percentage
= (int) (100 * yield
);
237 fprintf (stderr
, "prev %lud , now %lud, yield %4.2lf, want %d",
241 scm_i_minyield_malloc
);
244 if (yield
< scm_i_minyield_malloc
/ 100.0)
247 We make the trigger a little larger, even; If you have a
248 program that builds up a lot of data in strings, then the
249 desired yield will never be satisfied.
251 Instead of getting bogged down, we let the mtrigger grow
254 float no_overflow_trigger
= scm_mallocated
* 110.0;
256 no_overflow_trigger
/= (float) (100.0 - scm_i_minyield_malloc
);
259 if (no_overflow_trigger
>= (float) ULONG_MAX
)
260 scm_mtrigger
= ULONG_MAX
;
262 scm_mtrigger
= (unsigned long) no_overflow_trigger
;
265 fprintf (stderr
, "Mtrigger sweep: ineffective. New trigger %d\n",
270 scm_gc_running_p
= 0;
271 scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex
);
276 scm_gc_register_collectable_memory (void *mem
, size_t size
, const char *what
)
278 increase_mtrigger (size
, what
);
279 #ifdef GUILE_DEBUG_MALLOC
281 scm_malloc_register (mem
, what
);
287 scm_gc_unregister_collectable_memory (void *mem
, size_t size
, const char *what
)
289 decrease_mtrigger (size
, what
);
290 #ifdef GUILE_DEBUG_MALLOC
292 scm_malloc_unregister (mem
);
297 scm_gc_malloc (size_t size
, const char *what
)
300 The straightforward implementation below has the problem
301 that it might call the GC twice, once in scm_malloc and then
302 again in scm_gc_register_collectable_memory. We don't really
303 want the second GC since it will not find new garbage.
305 Note: this is a theoretical peeve. In reality, malloc() never
306 returns NULL. Usually, memory is overcommitted, and when you try
307 to write it the program is killed with signal 11. --hwn
310 void *ptr
= scm_malloc (size
);
311 scm_gc_register_collectable_memory (ptr
, size
, what
);
316 scm_gc_calloc (size_t size
, const char *what
)
318 void *ptr
= scm_gc_malloc (size
, what
);
319 memset (ptr
, 0x0, size
);
325 scm_gc_realloc (void *mem
, size_t old_size
, size_t new_size
, const char *what
)
329 /* XXX - see scm_gc_malloc. */
333 scm_realloc() may invalidate the block pointed to by WHERE, eg. by
334 unmapping it from memory or altering the contents. Since
335 increase_mtrigger() might trigger a GC that would scan
336 MEM, it is crucial that this call precedes realloc().
339 decrease_mtrigger (old_size
, what
);
340 increase_mtrigger (new_size
, what
);
342 ptr
= scm_realloc (mem
, new_size
);
344 #ifdef GUILE_DEBUG_MALLOC
346 scm_malloc_reregister (mem
, ptr
, what
);
353 scm_gc_free (void *mem
, size_t size
, const char *what
)
355 scm_gc_unregister_collectable_memory (mem
, size
, what
);
360 scm_gc_strndup (const char *str
, size_t n
, const char *what
)
362 char *dst
= scm_gc_malloc (n
+1, what
);
363 memcpy (dst
, str
, n
);
369 scm_gc_strdup (const char *str
, const char *what
)
371 return scm_gc_strndup (str
, strlen (str
), what
);
374 #if SCM_ENABLE_DEPRECATED == 1
376 /* {Deprecated front end to malloc}
378 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
381 * These functions provide services comparable to malloc, realloc, and
382 * free. They should be used when allocating memory that will be under
383 * control of the garbage collector, i.e., if the memory may be freed
384 * during garbage collection.
386 * They are deprecated because they weren't really used the way
387 * outlined above, and making sure to return the right amount from
388 * smob free routines was sometimes difficult when dealing with nested
389 * data structures. We basically want everybody to review their code
390 * and use the more symmetrical scm_gc_malloc/scm_gc_free functions
391 * instead. In some cases, where scm_must_malloc has been used
392 * incorrectly (i.e. for non-GC-able memory), use scm_malloc/free.
396 scm_must_malloc (size_t size
, const char *what
)
398 scm_c_issue_deprecation_warning
399 ("scm_must_malloc is deprecated. "
400 "Use scm_gc_malloc and scm_gc_free instead.");
402 return scm_gc_malloc (size
, what
);
406 scm_must_realloc (void *where
,
411 scm_c_issue_deprecation_warning
412 ("scm_must_realloc is deprecated. "
413 "Use scm_gc_realloc and scm_gc_free instead.");
415 return scm_gc_realloc (where
, old_size
, size
, what
);
419 scm_must_strndup (const char *str
, size_t length
)
421 scm_c_issue_deprecation_warning
422 ("scm_must_strndup is deprecated. "
423 "Use scm_gc_strndup and scm_gc_free instead.");
425 return scm_gc_strndup (str
, length
, "string");
429 scm_must_strdup (const char *str
)
431 scm_c_issue_deprecation_warning
432 ("scm_must_strdup is deprecated. "
433 "Use scm_gc_strdup and scm_gc_free instead.");
435 return scm_gc_strdup (str
, "string");
439 scm_must_free (void *obj
)
440 #define FUNC_NAME "scm_must_free"
442 scm_c_issue_deprecation_warning
443 ("scm_must_free is deprecated. "
444 "Use scm_gc_malloc and scm_gc_free instead.");
446 #ifdef GUILE_DEBUG_MALLOC
447 scm_malloc_unregister (obj
);
453 fprintf (stderr
,"freeing NULL pointer");
461 scm_done_malloc (long size
)
463 scm_c_issue_deprecation_warning
464 ("scm_done_malloc is deprecated. "
465 "Use scm_gc_register_collectable_memory instead.");
468 scm_gc_register_collectable_memory (NULL
, size
, "foreign mallocs");
470 scm_gc_unregister_collectable_memory (NULL
, -size
, "foreign mallocs");
474 scm_done_free (long size
)
476 scm_c_issue_deprecation_warning
477 ("scm_done_free is deprecated. "
478 "Use scm_gc_unregister_collectable_memory instead.");
481 scm_gc_unregister_collectable_memory (NULL
, size
, "foreign mallocs");
483 scm_gc_register_collectable_memory (NULL
, -size
, "foreign mallocs");
486 #endif /* SCM_ENABLE_DEPRECATED == 1 */