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