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