Add GC benchmarks.
[bpt/guile.git] / libguile / gc.c
CommitLineData
bfb64eb4 1/* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006, 2008 Free Software Foundation, Inc.
a00c95d9 2 *
73be1d9e
MV
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.
a00c95d9 7 *
73be1d9e 8 * This library is distributed in the hope that it will be useful,
0f2d19dd 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
a00c95d9 12 *
73be1d9e
MV
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
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
37ddcaf6
MD
18/* #define DEBUGINFO */
19
dbb605f5 20#ifdef HAVE_CONFIG_H
aa54a9b0
RB
21# include <config.h>
22#endif
56495472 23
e7bca227
LC
24#include "libguile/gen-scmconfig.h"
25
0f2d19dd 26#include <stdio.h>
e6e2e95a 27#include <errno.h>
783e7774 28#include <string.h>
c8a1bdc4 29#include <assert.h>
e6e2e95a 30
3ec17f28
LC
31#ifdef __ia64__
32#include <ucontext.h>
33extern unsigned long * __libc_ia64_register_backing_store_base;
34#endif
35
a0599745 36#include "libguile/_scm.h"
0a7a7445 37#include "libguile/eval.h"
a0599745
MD
38#include "libguile/stime.h"
39#include "libguile/stackchk.h"
40#include "libguile/struct.h"
a0599745
MD
41#include "libguile/smob.h"
42#include "libguile/unif.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"
801cb5e7 48#include "libguile/weaks.h"
686765af 49#include "libguile/hashtab.h"
ecf470a2 50#include "libguile/tags.h"
a0599745 51
c8a1bdc4 52#include "libguile/private-gc.h"
a0599745 53#include "libguile/validate.h"
1be6b49c 54#include "libguile/deprecation.h"
a0599745 55#include "libguile/gc.h"
9de87eea 56#include "libguile/dynwind.h"
fce59c93 57
e7bca227 58#include "libguile/boehm-gc.h"
a82e7953 59
bc9d9bb2 60#ifdef GUILE_DEBUG_MALLOC
a0599745 61#include "libguile/debug-malloc.h"
bc9d9bb2
MD
62#endif
63
0f2d19dd 64#ifdef HAVE_MALLOC_H
95b88819 65#include <malloc.h>
0f2d19dd
JB
66#endif
67
68#ifdef HAVE_UNISTD_H
95b88819 69#include <unistd.h>
0f2d19dd
JB
70#endif
71
fb50ef08
MD
72/* Lock this mutex before doing lazy sweeping.
73 */
b17e0ac3 74scm_i_pthread_mutex_t scm_i_sweep_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
fb50ef08 75
eae33935 76/* Set this to != 0 if every cell that is accessed shall be checked:
61045190 77 */
eab1b259
HWN
78int scm_debug_cell_accesses_p = 0;
79int scm_expensive_debug_cell_accesses_p = 0;
406c7d90 80
e81d98ec
DH
81/* Set this to 0 if no additional gc's shall be performed, otherwise set it to
82 * the number of cell accesses after which a gc shall be called.
83 */
eab1b259 84int scm_debug_cells_gc_interval = 0;
e81d98ec 85
eab1b259
HWN
86/*
87 Global variable, so you can switch it off at runtime by setting
88 scm_i_cell_validation_already_running.
406c7d90 89 */
eab1b259
HWN
90int scm_i_cell_validation_already_running ;
91
92#if (SCM_DEBUG_CELL_ACCESSES == 1)
93
94
95/*
96
97 Assert that the given object is a valid reference to a valid cell. This
98 test involves to determine whether the object is a cell pointer, whether
99 this pointer actually points into a heap segment and whether the cell
100 pointed to is not a free cell. Further, additional garbage collections may
101 get executed after a user defined number of cell accesses. This helps to
102 find places in the C code where references are dropped for extremely short
103 periods.
104
105*/
406c7d90 106void
eab1b259 107scm_i_expensive_validation_check (SCM cell)
406c7d90 108{
eab1b259
HWN
109 if (!scm_in_heap_p (cell))
110 {
111 fprintf (stderr, "scm_assert_cell_valid: this object does not live in the heap: %lux\n",
112 (unsigned long) SCM_UNPACK (cell));
113 abort ();
114 }
115
116 /* If desired, perform additional garbage collections after a user
117 * defined number of cell accesses.
118 */
119 if (scm_debug_cells_gc_interval)
120 {
121 static unsigned int counter = 0;
61045190 122
eab1b259
HWN
123 if (counter != 0)
124 {
125 --counter;
126 }
127 else
128 {
129 counter = scm_debug_cells_gc_interval;
b17e0ac3 130 scm_gc ();
eab1b259
HWN
131 }
132 }
133}
134
135void
136scm_assert_cell_valid (SCM cell)
137{
138 if (!scm_i_cell_validation_already_running && scm_debug_cell_accesses_p)
406c7d90 139 {
eab1b259 140 scm_i_cell_validation_already_running = 1; /* set to avoid recursion */
406c7d90 141
c8a1bdc4 142 /*
eab1b259
HWN
143 During GC, no user-code should be run, and the guile core
144 should use non-protected accessors.
145 */
c8a1bdc4 146 if (scm_gc_running_p)
eab1b259 147 return;
c8a1bdc4
HWN
148
149 /*
eab1b259
HWN
150 Only scm_in_heap_p and rescanning the heap is wildly
151 expensive.
152 */
153 if (scm_expensive_debug_cell_accesses_p)
154 scm_i_expensive_validation_check (cell);
7ddb9baf 155#if (SCM_DEBUG_MARKING_API == 0)
c8a1bdc4 156 if (!SCM_GC_MARK_P (cell))
406c7d90 157 {
c8a1bdc4
HWN
158 fprintf (stderr,
159 "scm_assert_cell_valid: this object is unmarked. \n"
160 "It has been garbage-collected in the last GC run: "
161 "%lux\n",
1be6b49c 162 (unsigned long) SCM_UNPACK (cell));
406c7d90
DH
163 abort ();
164 }
7ddb9baf
HWN
165#endif /* SCM_DEBUG_MARKING_API */
166
eab1b259 167 scm_i_cell_validation_already_running = 0; /* re-enable */
406c7d90
DH
168 }
169}
170
171
eab1b259 172
406c7d90
DH
173SCM_DEFINE (scm_set_debug_cell_accesses_x, "set-debug-cell-accesses!", 1, 0, 0,
174 (SCM flag),
1e6808ea 175 "If @var{flag} is @code{#f}, cell access checking is disabled.\n"
eab1b259 176 "If @var{flag} is @code{#t}, cheap cell access checking is enabled,\n"
e81d98ec 177 "but no additional calls to garbage collection are issued.\n"
eab1b259 178 "If @var{flag} is a number, strict cell access checking is enabled,\n"
e81d98ec
DH
179 "with an additional garbage collection after the given\n"
180 "number of cell accesses.\n"
1e6808ea
MG
181 "This procedure only exists when the compile-time flag\n"
182 "@code{SCM_DEBUG_CELL_ACCESSES} was set to 1.")
406c7d90
DH
183#define FUNC_NAME s_scm_set_debug_cell_accesses_x
184{
7888309b 185 if (scm_is_false (flag))
eab1b259
HWN
186 {
187 scm_debug_cell_accesses_p = 0;
188 }
bc36d050 189 else if (scm_is_eq (flag, SCM_BOOL_T))
eab1b259
HWN
190 {
191 scm_debug_cells_gc_interval = 0;
192 scm_debug_cell_accesses_p = 1;
193 scm_expensive_debug_cell_accesses_p = 0;
194 }
e11e83f3 195 else
eab1b259 196 {
e11e83f3 197 scm_debug_cells_gc_interval = scm_to_signed_integer (flag, 0, INT_MAX);
eab1b259
HWN
198 scm_debug_cell_accesses_p = 1;
199 scm_expensive_debug_cell_accesses_p = 1;
200 }
406c7d90
DH
201 return SCM_UNSPECIFIED;
202}
203#undef FUNC_NAME
0f2d19dd 204
ecf470a2 205
c8a1bdc4 206#endif /* SCM_DEBUG_CELL_ACCESSES == 1 */
0f2d19dd
JB
207
208\f
26224b3f
LC
209/* Hooks. */
210scm_t_c_hook scm_before_gc_c_hook;
211scm_t_c_hook scm_before_mark_c_hook;
212scm_t_c_hook scm_before_sweep_c_hook;
213scm_t_c_hook scm_after_sweep_c_hook;
214scm_t_c_hook scm_after_gc_c_hook;
945fec60 215
0f2d19dd
JB
216
217/* scm_mtrigger
539b08a4 218 * is the number of bytes of malloc allocation needed to trigger gc.
0f2d19dd 219 */
c014a02e 220unsigned long scm_mtrigger;
0f2d19dd 221
0f2d19dd
JB
222/* GC Statistics Keeping
223 */
c014a02e 224unsigned long scm_mallocated = 0;
b74e86cf
LC
225unsigned long scm_gc_ports_collected = 0;
226
915b3f9f
LC
227
228static unsigned long protected_obj_count = 0;
c2cbcc57 229
0f2d19dd
JB
230
231SCM_SYMBOL (sym_cells_allocated, "cells-allocated");
915b3f9f
LC
232SCM_SYMBOL (sym_heap_size, "heap-size");
233SCM_SYMBOL (sym_heap_free_size, "heap-free-size");
234SCM_SYMBOL (sym_heap_total_allocated, "heap-total-allocated");
0f2d19dd
JB
235SCM_SYMBOL (sym_mallocated, "bytes-malloced");
236SCM_SYMBOL (sym_mtrigger, "gc-malloc-threshold");
237SCM_SYMBOL (sym_heap_segments, "cell-heap-segments");
238SCM_SYMBOL (sym_gc_time_taken, "gc-time-taken");
c9b0d4b0 239SCM_SYMBOL (sym_gc_mark_time_taken, "gc-mark-time-taken");
c9b0d4b0
ML
240SCM_SYMBOL (sym_times, "gc-times");
241SCM_SYMBOL (sym_cells_marked, "cells-marked");
40945e5e 242SCM_SYMBOL (sym_cells_marked_conservatively, "cells-marked-conservatively");
c9b0d4b0 243SCM_SYMBOL (sym_cells_swept, "cells-swept");
c2cbcc57
HWN
244SCM_SYMBOL (sym_malloc_yield, "malloc-yield");
245SCM_SYMBOL (sym_cell_yield, "cell-yield");
7eec4c37 246SCM_SYMBOL (sym_protected_objects, "protected-objects");
93632e3c 247SCM_SYMBOL (sym_total_cells_allocated, "total-cells-allocated");
cf2d30f6 248
d3dd80ab 249
cf2d30f6 250/* Number of calls to SCM_NEWCELL since startup. */
c8a1bdc4
HWN
251unsigned scm_newcell_count;
252unsigned scm_newcell2_count;
b37fe1c5 253
b37fe1c5 254
0f2d19dd
JB
255/* {Scheme Interface to GC}
256 */
1367aa5e
HWN
257static SCM
258tag_table_to_type_alist (void *closure, SCM key, SCM val, SCM acc)
259{
8fecbb19 260 if (scm_is_integer (key))
8a00ba71 261 {
3e2073bd 262 int c_tag = scm_to_int (key);
8fecbb19
HWN
263
264 char const * name = scm_i_tag_name (c_tag);
265 if (name != NULL)
266 {
267 key = scm_from_locale_string (name);
268 }
269 else
270 {
271 char s[100];
272 sprintf (s, "tag %d", c_tag);
273 key = scm_from_locale_string (s);
274 }
8a00ba71 275 }
8fecbb19 276
1367aa5e
HWN
277 return scm_cons (scm_cons (key, val), acc);
278}
279
280SCM_DEFINE (scm_gc_live_object_stats, "gc-live-object-stats", 0, 0, 0,
281 (),
282 "Return an alist of statistics of the current live objects. ")
283#define FUNC_NAME s_scm_gc_live_object_stats
284{
285 SCM tab = scm_make_hash_table (scm_from_int (57));
b01532af
NJ
286 SCM alist;
287
b01532af 288 alist
1367aa5e
HWN
289 = scm_internal_hash_fold (&tag_table_to_type_alist, NULL, SCM_EOL, tab);
290
291 return alist;
292}
293#undef FUNC_NAME
294
c2cbcc57 295extern int scm_gc_malloc_yield_percentage;
a00c95d9 296SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
1bbd0b84 297 (),
1e6808ea 298 "Return an association list of statistics about Guile's current\n"
c8a1bdc4 299 "use of storage.\n")
1bbd0b84 300#define FUNC_NAME s_scm_gc_stats
0f2d19dd 301{
0f2d19dd 302 SCM answer;
915b3f9f
LC
303 size_t heap_size, free_bytes, bytes_since_gc, total_bytes;
304 size_t gc_times;
4c9419ac 305
915b3f9f
LC
306 heap_size = GC_get_heap_size ();
307 free_bytes = GC_get_free_bytes ();
308 bytes_since_gc = GC_get_bytes_since_gc ();
309 total_bytes = GC_get_total_bytes ();
310 gc_times = GC_gc_no;
fca43887 311
33b320ae
NJ
312 /* njrev: can any of these scm_cons's or scm_list_n signal a memory
313 error? If so we need a frame here. */
b9bd8526 314 answer =
915b3f9f
LC
315 scm_list_n (scm_cons (sym_gc_time_taken, SCM_INUM0),
316#if 0
b9bd8526
MV
317 scm_cons (sym_cells_allocated,
318 scm_from_ulong (local_scm_cells_allocated)),
b9bd8526
MV
319 scm_cons (sym_mallocated,
320 scm_from_ulong (local_scm_mallocated)),
321 scm_cons (sym_mtrigger,
322 scm_from_ulong (local_scm_mtrigger)),
b9bd8526
MV
323 scm_cons (sym_gc_mark_time_taken,
324 scm_from_ulong (local_scm_gc_mark_time_taken)),
325 scm_cons (sym_cells_marked,
326 scm_from_double (local_scm_gc_cells_marked)),
327 scm_cons (sym_cells_swept,
328 scm_from_double (local_scm_gc_cells_swept)),
329 scm_cons (sym_malloc_yield,
1f584400 330 scm_from_long (local_scm_gc_malloc_yield_percentage)),
b9bd8526
MV
331 scm_cons (sym_cell_yield,
332 scm_from_long (local_scm_gc_cell_yield_percentage)),
b9bd8526 333 scm_cons (sym_heap_segments, heap_segs),
915b3f9f
LC
334#endif
335 scm_cons (sym_heap_size, scm_from_size_t (heap_size)),
336 scm_cons (sym_heap_free_size, scm_from_size_t (free_bytes)),
337 scm_cons (sym_heap_total_allocated,
338 scm_from_size_t (total_bytes)),
339 scm_cons (sym_protected_objects,
340 scm_from_ulong (protected_obj_count)),
341 scm_cons (sym_times, scm_from_size_t (gc_times)),
b9bd8526 342 SCM_UNDEFINED);
fca43887 343
c8a1bdc4 344 return answer;
0f2d19dd 345}
c8a1bdc4 346#undef FUNC_NAME
0f2d19dd 347
539b08a4 348
acf4331f 349
c8a1bdc4
HWN
350SCM_DEFINE (scm_object_address, "object-address", 1, 0, 0,
351 (SCM obj),
352 "Return an integer that for the lifetime of @var{obj} is uniquely\n"
353 "returned by this function for @var{obj}")
354#define FUNC_NAME s_scm_object_address
c68296f8 355{
b9bd8526 356 return scm_from_ulong (SCM_UNPACK (obj));
c68296f8 357}
c8a1bdc4 358#undef FUNC_NAME
c68296f8 359
1be6b49c 360
915b3f9f
LC
361SCM_DEFINE (scm_gc_disable, "gc-disable", 0, 0, 0,
362 (),
363 "Disables the garbage collector. Nested calls are permitted. "
364 "GC is re-enabled once @code{gc-enable} has been called the "
365 "same number of times @code{gc-disable} was called.")
366#define FUNC_NAME s_scm_gc_disable
367{
368 GC_disable ();
369 return SCM_UNSPECIFIED;
370}
371#undef FUNC_NAME
372
373SCM_DEFINE (scm_gc_enable, "gc-enable", 0, 0, 0,
374 (),
375 "Enables the garbage collector.")
376#define FUNC_NAME s_scm_gc_enable
377{
378 GC_enable ();
379 return SCM_UNSPECIFIED;
380}
381#undef FUNC_NAME
382
383
c8a1bdc4
HWN
384SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
385 (),
386 "Scans all of SCM objects and reclaims for further use those that are\n"
387 "no longer accessible.")
388#define FUNC_NAME s_scm_gc
389{
b17e0ac3 390 scm_i_scm_pthread_mutex_lock (&scm_i_sweep_mutex);
b17e0ac3 391 scm_i_gc ("call");
33b320ae
NJ
392 /* njrev: It looks as though other places, e.g. scm_realloc,
393 can call scm_i_gc without acquiring the sweep mutex. Does this
394 matter? Also scm_i_gc (or its descendants) touch the
395 scm_sys_protects, which are protected in some cases
396 (e.g. scm_permobjs above in scm_gc_stats) by a critical section,
397 not by the sweep mutex. Shouldn't all the GC-relevant objects be
398 protected in the same way? */
b17e0ac3
MV
399 scm_i_pthread_mutex_unlock (&scm_i_sweep_mutex);
400 scm_c_hook_run (&scm_after_gc_c_hook, 0);
c8a1bdc4 401 return SCM_UNSPECIFIED;
9d47a1e6 402}
c8a1bdc4 403#undef FUNC_NAME
9d47a1e6 404
c8a1bdc4 405void
b17e0ac3 406scm_i_gc (const char *what)
c8a1bdc4 407{
26224b3f 408 GC_gcollect ();
eab1b259 409}
0f2d19dd 410
4c7016dc 411
0f2d19dd
JB
412\f
413/* {GC Protection Helper Functions}
414 */
415
416
5d2b97cd
DH
417/*
418 * If within a function you need to protect one or more scheme objects from
419 * garbage collection, pass them as parameters to one of the
420 * scm_remember_upto_here* functions below. These functions don't do
421 * anything, but since the compiler does not know that they are actually
422 * no-ops, it will generate code that calls these functions with the given
423 * parameters. Therefore, you can be sure that the compiler will keep those
424 * scheme values alive (on the stack or in a register) up to the point where
425 * scm_remember_upto_here* is called. In other words, place the call to
592996c9 426 * scm_remember_upto_here* _behind_ the last code in your function, that
5d2b97cd
DH
427 * depends on the scheme object to exist.
428 *
8c494e99
DH
429 * Example: We want to make sure that the string object str does not get
430 * garbage collected during the execution of 'some_function' in the code
431 * below, because otherwise the characters belonging to str would be freed and
5d2b97cd
DH
432 * 'some_function' might access freed memory. To make sure that the compiler
433 * keeps str alive on the stack or in a register such that it is visible to
434 * the conservative gc we add the call to scm_remember_upto_here_1 _after_ the
435 * call to 'some_function'. Note that this would not be necessary if str was
436 * used anyway after the call to 'some_function'.
eb01cb64 437 * char *chars = scm_i_string_chars (str);
5d2b97cd
DH
438 * some_function (chars);
439 * scm_remember_upto_here_1 (str); // str will be alive up to this point.
440 */
441
9e1569bd
KR
442/* Remove any macro versions of these while defining the functions.
443 Functions are always included in the library, for upward binary
444 compatibility and in case combinations of GCC and non-GCC are used. */
445#undef scm_remember_upto_here_1
446#undef scm_remember_upto_here_2
447
5d2b97cd 448void
e81d98ec 449scm_remember_upto_here_1 (SCM obj SCM_UNUSED)
5d2b97cd
DH
450{
451 /* Empty. Protects a single object from garbage collection. */
452}
453
454void
e81d98ec 455scm_remember_upto_here_2 (SCM obj1 SCM_UNUSED, SCM obj2 SCM_UNUSED)
5d2b97cd
DH
456{
457 /* Empty. Protects two objects from garbage collection. */
458}
459
460void
e81d98ec 461scm_remember_upto_here (SCM obj SCM_UNUSED, ...)
5d2b97cd
DH
462{
463 /* Empty. Protects any number of objects from garbage collection. */
464}
465
c209c88e 466/*
41b0806d
GB
467 These crazy functions prevent garbage collection
468 of arguments after the first argument by
469 ensuring they remain live throughout the
470 function because they are used in the last
471 line of the code block.
472 It'd be better to have a nice compiler hint to
473 aid the conservative stack-scanning GC. --03/09/00 gjb */
0f2d19dd
JB
474SCM
475scm_return_first (SCM elt, ...)
0f2d19dd
JB
476{
477 return elt;
478}
479
41b0806d
GB
480int
481scm_return_first_int (int i, ...)
482{
483 return i;
484}
485
0f2d19dd 486
0f2d19dd 487SCM
6e8d25a6 488scm_permanent_object (SCM obj)
0f2d19dd 489{
8e7b3e98 490 return (scm_gc_protect_object (obj));
0f2d19dd
JB
491}
492
493
7bd4fbe2
MD
494/* Protect OBJ from the garbage collector. OBJ will not be freed, even if all
495 other references are dropped, until the object is unprotected by calling
6b1b030e 496 scm_gc_unprotect_object (OBJ). Calls to scm_gc_protect/unprotect_object nest,
7bd4fbe2
MD
497 i. e. it is possible to protect the same object several times, but it is
498 necessary to unprotect the object the same number of times to actually get
499 the object unprotected. It is an error to unprotect an object more often
500 than it has been protected before. The function scm_protect_object returns
501 OBJ.
502*/
503
504/* Implementation note: For every object X, there is a counter which
1f584400 505 scm_gc_protect_object (X) increments and scm_gc_unprotect_object (X) decrements.
7bd4fbe2 506*/
686765af 507
7eec4c37
HWN
508
509
ef290276 510SCM
6b1b030e 511scm_gc_protect_object (SCM obj)
ef290276 512{
686765af 513 SCM handle;
9d47a1e6 514
686765af 515 /* This critical section barrier will be replaced by a mutex. */
33b320ae
NJ
516 /* njrev: Indeed; if my comment above is correct, there is the same
517 critsec/mutex inconsistency here. */
9de87eea 518 SCM_CRITICAL_SECTION_START;
9d47a1e6 519
e11e83f3
MV
520 handle = scm_hashq_create_handle_x (scm_protects, obj, scm_from_int (0));
521 SCM_SETCDR (handle, scm_sum (SCM_CDR (handle), scm_from_int (1)));
9d47a1e6 522
7eec4c37
HWN
523 protected_obj_count ++;
524
9de87eea 525 SCM_CRITICAL_SECTION_END;
9d47a1e6 526
ef290276
JB
527 return obj;
528}
529
530
531/* Remove any protection for OBJ established by a prior call to
dab7f566 532 scm_protect_object. This function returns OBJ.
ef290276 533
dab7f566 534 See scm_protect_object for more information. */
ef290276 535SCM
6b1b030e 536scm_gc_unprotect_object (SCM obj)
ef290276 537{
686765af 538 SCM handle;
9d47a1e6 539
686765af 540 /* This critical section barrier will be replaced by a mutex. */
33b320ae 541 /* njrev: and again. */
9de87eea 542 SCM_CRITICAL_SECTION_START;
9d47a1e6 543
0ff7e3ff
HWN
544 if (scm_gc_running_p)
545 {
546 fprintf (stderr, "scm_unprotect_object called during GC.\n");
547 abort ();
548 }
b17e0ac3 549
686765af 550 handle = scm_hashq_get_handle (scm_protects, obj);
9d47a1e6 551
7888309b 552 if (scm_is_false (handle))
686765af 553 {
0f0f0899
MD
554 fprintf (stderr, "scm_unprotect_object called on unprotected object\n");
555 abort ();
686765af 556 }
6a199940
DH
557 else
558 {
e11e83f3 559 SCM count = scm_difference (SCM_CDR (handle), scm_from_int (1));
bc36d050 560 if (scm_is_eq (count, scm_from_int (0)))
6a199940
DH
561 scm_hashq_remove_x (scm_protects, obj);
562 else
1be6b49c 563 SCM_SETCDR (handle, count);
6a199940 564 }
7eec4c37 565 protected_obj_count --;
686765af 566
9de87eea 567 SCM_CRITICAL_SECTION_END;
ef290276
JB
568
569 return obj;
570}
571
6b1b030e
ML
572void
573scm_gc_register_root (SCM *p)
574{
8e7b3e98 575 /* Nothing. */
6b1b030e
ML
576}
577
578void
579scm_gc_unregister_root (SCM *p)
580{
8e7b3e98 581 /* Nothing. */
6b1b030e
ML
582}
583
584void
585scm_gc_register_roots (SCM *b, unsigned long n)
586{
587 SCM *p = b;
588 for (; p < b + n; ++p)
589 scm_gc_register_root (p);
590}
591
592void
593scm_gc_unregister_roots (SCM *b, unsigned long n)
594{
595 SCM *p = b;
596 for (; p < b + n; ++p)
597 scm_gc_unregister_root (p);
598}
599
04a98cff 600int scm_i_terminating;
c45acc34 601
0f2d19dd 602\f
a00c95d9 603
4c48ba06 604
c8a1bdc4
HWN
605/*
606 MOVE THIS FUNCTION. IT DOES NOT HAVE ANYTHING TODO WITH GC.
607 */
85db4a2c
DH
608
609/* Get an integer from an environment variable. */
c8a1bdc4
HWN
610int
611scm_getenv_int (const char *var, int def)
85db4a2c 612{
c8a1bdc4
HWN
613 char *end = 0;
614 char *val = getenv (var);
615 long res = def;
85db4a2c
DH
616 if (!val)
617 return def;
618 res = strtol (val, &end, 10);
619 if (end == val)
620 return def;
621 return res;
622}
623
c35738c1
MD
624void
625scm_storage_prehistory ()
626{
184327a6 627 GC_all_interior_pointers = 0;
21c097e0 628 GC_set_free_space_divisor (scm_getenv_int ("GC_FREE_SPACE_DIVISOR", 3));
184327a6 629
a82e7953 630 GC_INIT ();
e7bca227 631
11d2fc06
LC
632#if (! ((defined GC_VERSION_MAJOR) && (GC_VERSION_MAJOR >= 7))) \
633 && (defined SCM_I_GSC_USE_PTHREAD_THREADS)
e7bca227
LC
634 /* When using GC 6.8, this call is required to initialize thread-local
635 freelists (shouldn't be necessary with GC 7.0). */
636 GC_init ();
637#endif
638
fdab75a1 639 GC_expand_hp (SCM_DEFAULT_INIT_HEAP_SIZE_2);
915b3f9f 640
184327a6
LC
641 /* We only need to register a displacement for those types for which the
642 higher bits of the type tag are used to store a pointer (that is, a
643 pointer to an 8-octet aligned region). For `scm_tc3_struct', this is
644 handled in `scm_alloc_struct ()'. */
645 GC_REGISTER_DISPLACEMENT (scm_tc3_cons);
646 GC_REGISTER_DISPLACEMENT (scm_tc3_closure);
647
915b3f9f
LC
648 /* Sanity check. */
649 if (!GC_is_visible (scm_sys_protects))
650 abort ();
a82e7953 651
c35738c1
MD
652 scm_c_hook_init (&scm_before_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
653 scm_c_hook_init (&scm_before_mark_c_hook, 0, SCM_C_HOOK_NORMAL);
654 scm_c_hook_init (&scm_before_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
655 scm_c_hook_init (&scm_after_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
656 scm_c_hook_init (&scm_after_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
657}
85db4a2c 658
9de87eea 659scm_i_pthread_mutex_t scm_i_gc_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
eb01cb64 660
4a4c9785 661int
85db4a2c 662scm_init_storage ()
0f2d19dd 663{
1be6b49c 664 size_t j;
0f2d19dd
JB
665
666 j = SCM_NUM_PROTECTS;
667 while (j)
668 scm_sys_protects[--j] = SCM_BOOL_F;
4a4c9785 669
0f2d19dd 670 j = SCM_HEAP_SEG_SIZE;
d6884e63 671
9de87eea
MV
672#if 0
673 /* We can't have a cleanup handler since we have no thread to run it
674 in. */
675
a18bcd0e 676#ifdef HAVE_ATEXIT
c45acc34 677 atexit (cleanup);
e52ceaac
MD
678#else
679#ifdef HAVE_ON_EXIT
680 on_exit (cleanup, 0);
681#endif
9de87eea
MV
682#endif
683
a18bcd0e 684#endif
0f2d19dd 685
e4da0740 686 scm_stand_in_procs = scm_make_weak_key_hash_table (scm_from_int (257));
00ffa0e7 687 scm_protects = scm_c_make_hash_table (31);
d6884e63 688
0f2d19dd
JB
689 return 0;
690}
939794ce 691
0f2d19dd
JB
692\f
693
939794ce
DH
694SCM scm_after_gc_hook;
695
939794ce
DH
696static SCM gc_async;
697
939794ce
DH
698/* The function gc_async_thunk causes the execution of the after-gc-hook. It
699 * is run after the gc, as soon as the asynchronous events are handled by the
700 * evaluator.
701 */
702static SCM
703gc_async_thunk (void)
704{
705 scm_c_run_hook (scm_after_gc_hook, SCM_EOL);
939794ce
DH
706 return SCM_UNSPECIFIED;
707}
708
709
710/* The function mark_gc_async is run by the scm_after_gc_c_hook at the end of
711 * the garbage collection. The only purpose of this function is to mark the
712 * gc_async (which will eventually lead to the execution of the
713 * gc_async_thunk).
714 */
715static void *
e81d98ec 716mark_gc_async (void * hook_data SCM_UNUSED,
5c004b6d 717 void *fn_data SCM_UNUSED,
e81d98ec
DH
718 void *data SCM_UNUSED)
719{
720 /* If cell access debugging is enabled, the user may choose to perform
721 * additional garbage collections after an arbitrary number of cell
722 * accesses. We don't want the scheme level after-gc-hook to be performed
723 * for each of these garbage collections for the following reason: The
724 * execution of the after-gc-hook causes cell accesses itself. Thus, if the
725 * after-gc-hook was performed with every gc, and if the gc was performed
726 * after a very small number of cell accesses, then the number of cell
727 * accesses during the execution of the after-gc-hook will suffice to cause
728 * the execution of the next gc. Then, guile would keep executing the
729 * after-gc-hook over and over again, and would never come to do other
730 * things.
eae33935 731 *
e81d98ec
DH
732 * To overcome this problem, if cell access debugging with additional
733 * garbage collections is enabled, the after-gc-hook is never run by the
734 * garbage collecter. When running guile with cell access debugging and the
735 * execution of the after-gc-hook is desired, then it is necessary to run
736 * the hook explicitly from the user code. This has the effect, that from
737 * the scheme level point of view it seems that garbage collection is
738 * performed with a much lower frequency than it actually is. Obviously,
739 * this will not work for code that depends on a fixed one to one
740 * relationship between the execution counts of the C level garbage
741 * collection hooks and the execution count of the scheme level
742 * after-gc-hook.
743 */
9de87eea 744
e81d98ec 745#if (SCM_DEBUG_CELL_ACCESSES == 1)
eab1b259 746 if (scm_debug_cells_gc_interval == 0)
e81d98ec
DH
747 scm_system_async_mark (gc_async);
748#else
939794ce 749 scm_system_async_mark (gc_async);
e81d98ec
DH
750#endif
751
939794ce
DH
752 return NULL;
753}
754
26224b3f
LC
755char const *
756scm_i_tag_name (scm_t_bits tag)
757{
758 if (tag >= 255)
759 {
760 if (tag == scm_tc_free_cell)
761 return "free cell";
762
763 {
764 int k = 0xff & (tag >> 8);
765 return (scm_smobs[k].name);
766 }
767 }
768
769 switch (tag) /* 7 bits */
770 {
771 case scm_tcs_struct:
772 return "struct";
773 case scm_tcs_cons_imcar:
774 return "cons (immediate car)";
775 case scm_tcs_cons_nimcar:
776 return "cons (non-immediate car)";
777 case scm_tcs_closures:
778 return "closures";
779 case scm_tc7_pws:
780 return "pws";
781 case scm_tc7_wvect:
782 return "weak vector";
783 case scm_tc7_vector:
784 return "vector";
785#ifdef CCLO
786 case scm_tc7_cclo:
787 return "compiled closure";
788#endif
789 case scm_tc7_number:
790 switch (tag)
791 {
792 case scm_tc16_real:
793 return "real";
794 break;
795 case scm_tc16_big:
796 return "bignum";
797 break;
798 case scm_tc16_complex:
799 return "complex number";
800 break;
801 case scm_tc16_fraction:
802 return "fraction";
803 break;
804 }
805 break;
806 case scm_tc7_string:
807 return "string";
808 break;
809 case scm_tc7_stringbuf:
810 return "string buffer";
811 break;
812 case scm_tc7_symbol:
813 return "symbol";
814 break;
815 case scm_tc7_variable:
816 return "variable";
817 break;
818 case scm_tcs_subrs:
819 return "subrs";
820 break;
821 case scm_tc7_port:
822 return "port";
823 break;
824 case scm_tc7_smob:
825 return "smob"; /* should not occur. */
826 break;
827 }
828
829 return NULL;
830}
831
832
26224b3f
LC
833
834\f
0f2d19dd
JB
835void
836scm_init_gc ()
0f2d19dd 837{
a82e7953 838 /* `GC_INIT ()' was invoked in `scm_storage_prehistory ()'. */
d678e25c 839
fde50407
ML
840 scm_after_gc_hook = scm_permanent_object (scm_make_hook (SCM_INUM0));
841 scm_c_define ("after-gc-hook", scm_after_gc_hook);
939794ce 842
2592c4c7
MV
843 gc_async = scm_c_make_subr ("%gc-thunk", scm_tc7_subr_0,
844 gc_async_thunk);
939794ce
DH
845
846 scm_c_hook_add (&scm_after_gc_c_hook, mark_gc_async, NULL, 0);
847
a0599745 848#include "libguile/gc.x"
0f2d19dd 849}
89e00824 850
c8a1bdc4
HWN
851
852void
853scm_gc_sweep (void)
854#define FUNC_NAME "scm_gc_sweep"
855{
26224b3f
LC
856 /* FIXME */
857 fprintf (stderr, "%s: doing nothing\n", __FUNCTION__);
c8a1bdc4
HWN
858}
859
860#undef FUNC_NAME
861
862
56495472 863
89e00824
ML
864/*
865 Local Variables:
866 c-file-style: "gnu"
867 End:
868*/