1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2008, 2009 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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * 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
31 extern unsigned long * __libc_ia64_register_backing_store_base
;
34 #include "libguile/_scm.h"
35 #include "libguile/eval.h"
36 #include "libguile/stime.h"
37 #include "libguile/stackchk.h"
38 #include "libguile/struct.h"
39 #include "libguile/smob.h"
40 #include "libguile/arrays.h"
41 #include "libguile/async.h"
42 #include "libguile/ports.h"
43 #include "libguile/root.h"
44 #include "libguile/strings.h"
45 #include "libguile/vectors.h"
46 #include "libguile/weaks.h"
47 #include "libguile/hashtab.h"
48 #include "libguile/tags.h"
50 #include "libguile/validate.h"
51 #include "libguile/deprecation.h"
52 #include "libguile/gc.h"
54 #include "libguile/private-gc.h"
56 #ifdef GUILE_DEBUG_MALLOC
57 #include "libguile/debug-malloc.h"
69 INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
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:
76 #define SCM_DEFAULT_INIT_MALLOC_LIMIT 200*1024
77 #define SCM_DEFAULT_MALLOC_MINYIELD 40
79 /* #define DEBUGINFO */
81 static int scm_i_minyield_malloc
;
84 scm_gc_init_malloc (void)
86 scm_mtrigger
= scm_getenv_int ("GUILE_INIT_MALLOC_LIMIT",
87 SCM_DEFAULT_INIT_MALLOC_LIMIT
);
88 scm_i_minyield_malloc
= scm_getenv_int ("GUILE_MIN_YIELD_MALLOC",
89 SCM_DEFAULT_MALLOC_MINYIELD
);
91 if (scm_i_minyield_malloc
>= 100)
92 scm_i_minyield_malloc
= 99;
93 if (scm_i_minyield_malloc
< 1)
94 scm_i_minyield_malloc
= 1;
97 scm_mtrigger
= SCM_DEFAULT_INIT_MALLOC_LIMIT
;
102 /* Function for non-cell memory management.
106 scm_realloc (void *mem
, size_t size
)
110 SCM_SYSCALL (ptr
= realloc (mem
, size
));
114 scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex
);
115 scm_gc_running_p
= 1;
117 scm_i_gc ("realloc");
120 We don't want these sweep statistics to influence results for
121 cell GC, so we don't collect statistics.
123 realloc () failed, so we're really desparate to free memory. Run a
126 scm_i_sweep_all_segments ("realloc", NULL
);
128 scm_gc_running_p
= 0;
129 scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex
);
131 SCM_SYSCALL (ptr
= realloc (mem
, size
));
135 scm_memory_error ("realloc");
139 scm_malloc (size_t sz
)
141 return scm_realloc (NULL
, sz
);
145 Hmm. Should we use the C convention for arguments (i.e. N_ELTS,
149 scm_calloc (size_t sz
)
154 By default, try to use calloc, as it is likely more efficient than
155 calling memset by hand.
157 SCM_SYSCALL (ptr
= calloc (sz
, 1));
161 ptr
= scm_realloc (NULL
, sz
);
162 memset (ptr
, 0x0, sz
);
168 scm_strndup (const char *str
, size_t n
)
170 char *dst
= scm_malloc (n
+ 1);
171 memcpy (dst
, str
, n
);
177 scm_strdup (const char *str
)
179 return scm_strndup (str
, strlen (str
));
183 decrease_mtrigger (size_t size
, const char * what
)
185 scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex
);
187 if (size
> scm_mallocated
)
189 fprintf (stderr
, "`scm_mallocated' underflow. This means that more "
190 "memory was unregistered\n"
191 "via `scm_gc_unregister_collectable_memory ()' than "
196 scm_mallocated
-= size
;
197 scm_gc_malloc_collected
+= size
;
198 scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex
);
202 increase_mtrigger (size_t size
, const char *what
)
204 size_t mallocated
= 0;
205 int overflow
= 0, triggered
= 0;
207 scm_i_pthread_mutex_lock (&scm_i_gc_admin_mutex
);
208 if (ULONG_MAX
- size
< scm_mallocated
)
212 scm_mallocated
+= size
;
213 mallocated
= scm_mallocated
;
214 if (scm_mallocated
> scm_mtrigger
)
217 scm_i_pthread_mutex_unlock (&scm_i_gc_admin_mutex
);
220 scm_memory_error ("Overflow of scm_mallocated: too much memory in use.");
223 A program that uses a lot of malloced collectable memory (vectors,
224 strings), will use a lot of memory off the cell-heap; it needs to
225 do GC more often (before cells are exhausted), otherwise swapping
226 and malloc management will tie it down.
230 unsigned long prev_alloced
;
233 scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex
);
234 scm_gc_running_p
= 1;
236 prev_alloced
= mallocated
;
238 /* The GC will finish the pending sweep. For that reason, we
239 don't execute a complete sweep after GC, although that might
240 free some more memory.
244 yield
= (((float) prev_alloced
- (float) scm_mallocated
)
245 / (float) prev_alloced
);
247 scm_gc_malloc_yield_percentage
= (int) (100 * yield
);
250 fprintf (stderr
, "prev %lud , now %lud, yield %4.2lf, want %d",
254 scm_i_minyield_malloc
);
257 if (yield
< scm_i_minyield_malloc
/ 100.0)
260 We make the trigger a little larger, even; If you have a
261 program that builds up a lot of data in strings, then the
262 desired yield will never be satisfied.
264 Instead of getting bogged down, we let the mtrigger grow
267 float no_overflow_trigger
= scm_mallocated
* 110.0;
269 no_overflow_trigger
/= (float) (100.0 - scm_i_minyield_malloc
);
272 if (no_overflow_trigger
>= (float) ULONG_MAX
)
273 scm_mtrigger
= ULONG_MAX
;
275 scm_mtrigger
= (unsigned long) no_overflow_trigger
;
278 fprintf (stderr
, "Mtrigger sweep: ineffective. New trigger %d\n",
283 scm_gc_running_p
= 0;
284 scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex
);
289 scm_gc_register_collectable_memory (void *mem
, size_t size
, const char *what
)
291 increase_mtrigger (size
, what
);
292 #ifdef GUILE_DEBUG_MALLOC
294 scm_malloc_register (mem
, what
);
300 scm_gc_unregister_collectable_memory (void *mem
, size_t size
, const char *what
)
302 decrease_mtrigger (size
, what
);
303 #ifdef GUILE_DEBUG_MALLOC
305 scm_malloc_unregister (mem
);
310 scm_gc_malloc (size_t size
, const char *what
)
313 The straightforward implementation below has the problem
314 that it might call the GC twice, once in scm_malloc and then
315 again in scm_gc_register_collectable_memory. We don't really
316 want the second GC since it will not find new garbage.
318 Note: this is a theoretical peeve. In reality, malloc () never
319 returns NULL. Usually, memory is overcommitted, and when you try
320 to write it the program is killed with signal 11. --hwn
323 void *ptr
= size
? scm_malloc (size
) : NULL
;
324 scm_gc_register_collectable_memory (ptr
, size
, what
);
329 scm_gc_calloc (size_t size
, const char *what
)
331 void *ptr
= scm_gc_malloc (size
, what
);
332 memset (ptr
, 0x0, size
);
338 scm_gc_realloc (void *mem
, size_t old_size
, size_t new_size
, const char *what
)
342 /* XXX - see scm_gc_malloc. */
346 scm_realloc () may invalidate the block pointed to by WHERE, eg. by
347 unmapping it from memory or altering the contents. Since
348 increase_mtrigger () might trigger a GC that would scan
349 MEM, it is crucial that this call precedes realloc ().
352 decrease_mtrigger (old_size
, what
);
353 increase_mtrigger (new_size
, what
);
355 ptr
= scm_realloc (mem
, new_size
);
357 #ifdef GUILE_DEBUG_MALLOC
359 scm_malloc_reregister (mem
, ptr
, what
);
366 scm_gc_free (void *mem
, size_t size
, const char *what
)
368 scm_gc_unregister_collectable_memory (mem
, size
, what
);
374 scm_gc_strndup (const char *str
, size_t n
, const char *what
)
376 char *dst
= scm_gc_malloc (n
+1, what
);
377 memcpy (dst
, str
, n
);
383 scm_gc_strdup (const char *str
, const char *what
)
385 return scm_gc_strndup (str
, strlen (str
), what
);
388 #if SCM_ENABLE_DEPRECATED == 1
390 /* {Deprecated front end to malloc}
392 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
395 * These functions provide services comparable to malloc, realloc, and
396 * free. They should be used when allocating memory that will be under
397 * control of the garbage collector, i.e., if the memory may be freed
398 * during garbage collection.
400 * They are deprecated because they weren't really used the way
401 * outlined above, and making sure to return the right amount from
402 * smob free routines was sometimes difficult when dealing with nested
403 * data structures. We basically want everybody to review their code
404 * and use the more symmetrical scm_gc_malloc/scm_gc_free functions
405 * instead. In some cases, where scm_must_malloc has been used
406 * incorrectly (i.e. for non-GC-able memory), use scm_malloc/free.
410 scm_must_malloc (size_t size
, const char *what
)
412 scm_c_issue_deprecation_warning
413 ("scm_must_malloc is deprecated. "
414 "Use scm_gc_malloc and scm_gc_free instead.");
416 return scm_gc_malloc (size
, what
);
420 scm_must_realloc (void *where
,
425 scm_c_issue_deprecation_warning
426 ("scm_must_realloc is deprecated. "
427 "Use scm_gc_realloc and scm_gc_free instead.");
429 return scm_gc_realloc (where
, old_size
, size
, what
);
433 scm_must_strndup (const char *str
, size_t length
)
435 scm_c_issue_deprecation_warning
436 ("scm_must_strndup is deprecated. "
437 "Use scm_gc_strndup and scm_gc_free instead.");
439 return scm_gc_strndup (str
, length
, "string");
443 scm_must_strdup (const char *str
)
445 scm_c_issue_deprecation_warning
446 ("scm_must_strdup is deprecated. "
447 "Use scm_gc_strdup and scm_gc_free instead.");
449 return scm_gc_strdup (str
, "string");
453 scm_must_free (void *obj
)
454 #define FUNC_NAME "scm_must_free"
456 scm_c_issue_deprecation_warning
457 ("scm_must_free is deprecated. "
458 "Use scm_gc_malloc and scm_gc_free instead.");
460 #ifdef GUILE_DEBUG_MALLOC
461 scm_malloc_unregister (obj
);
467 fprintf (stderr
,"freeing NULL pointer");
475 scm_done_malloc (long size
)
477 scm_c_issue_deprecation_warning
478 ("scm_done_malloc is deprecated. "
479 "Use scm_gc_register_collectable_memory instead.");
482 scm_gc_register_collectable_memory (NULL
, size
, "foreign mallocs");
484 scm_gc_unregister_collectable_memory (NULL
, -size
, "foreign mallocs");
488 scm_done_free (long size
)
490 scm_c_issue_deprecation_warning
491 ("scm_done_free is deprecated. "
492 "Use scm_gc_unregister_collectable_memory instead.");
495 scm_gc_unregister_collectable_memory (NULL
, size
, "foreign mallocs");
497 scm_gc_register_collectable_memory (NULL
, -size
, "foreign mallocs");
500 #endif /* SCM_ENABLE_DEPRECATED == 1 */