* gc.h, gc.c (scm_must_strdup, scm_must_strndup): New.
[bpt/guile.git] / libguile / gc.c
1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 /* #define DEBUGINFO */
46
47 /* SECTION: This code is compiled once.
48 */
49
50 #ifndef MARK_DEPENDENCIES
51
52 \f
53 #include <stdio.h>
54 #include <errno.h>
55 #include <string.h>
56
57 #include "libguile/_scm.h"
58 #include "libguile/eval.h"
59 #include "libguile/stime.h"
60 #include "libguile/stackchk.h"
61 #include "libguile/struct.h"
62 #include "libguile/smob.h"
63 #include "libguile/unif.h"
64 #include "libguile/async.h"
65 #include "libguile/ports.h"
66 #include "libguile/root.h"
67 #include "libguile/strings.h"
68 #include "libguile/vectors.h"
69 #include "libguile/weaks.h"
70 #include "libguile/hashtab.h"
71 #include "libguile/tags.h"
72
73 #include "libguile/validate.h"
74 #include "libguile/gc.h"
75
76 #ifdef GUILE_DEBUG_MALLOC
77 #include "libguile/debug-malloc.h"
78 #endif
79
80 #ifdef HAVE_MALLOC_H
81 #include <malloc.h>
82 #endif
83
84 #ifdef HAVE_UNISTD_H
85 #include <unistd.h>
86 #endif
87
88 #ifdef __STDC__
89 #include <stdarg.h>
90 #define var_start(x, y) va_start(x, y)
91 #else
92 #include <varargs.h>
93 #define var_start(x, y) va_start(x)
94 #endif
95
96 \f
97
98 unsigned int scm_gc_running_p = 0;
99
100 \f
101
102 #if (SCM_DEBUG_CELL_ACCESSES == 1)
103
104 scm_bits_t scm_tc16_allocated;
105
106 /* Set this to != 0 if every cell that is accessed shall be checked:
107 */
108 unsigned int scm_debug_cell_accesses_p = 1;
109
110
111 /* Assert that the given object is a valid reference to a valid cell. This
112 * test involves to determine whether the object is a cell pointer, whether
113 * this pointer actually points into a heap segment and whether the cell
114 * pointed to is not a free cell.
115 */
116 void
117 scm_assert_cell_valid (SCM cell)
118 {
119 static unsigned int already_running = 0;
120
121 if (scm_debug_cell_accesses_p && !already_running)
122 {
123 already_running = 1; /* set to avoid recursion */
124
125 if (!scm_cellp (cell))
126 {
127 fprintf (stderr, "scm_assert_cell_valid: Not a cell object: %lx\n", SCM_UNPACK (cell));
128 abort ();
129 }
130 else if (!scm_gc_running_p)
131 {
132 /* Dirk::FIXME:: During garbage collection there occur references to
133 free cells. This is allright during conservative marking, but
134 should not happen otherwise (I think). The case of free cells
135 accessed during conservative marking is handled in function
136 scm_mark_locations. However, there still occur accesses to free
137 cells during gc. I don't understand why this happens. If it is
138 a bug and gets fixed, the following test should also work while
139 gc is running.
140 */
141 if (SCM_FREE_CELL_P (cell))
142 {
143 fprintf (stderr, "scm_assert_cell_valid: Accessing free cell: %lx\n", SCM_UNPACK (cell));
144 abort ();
145 }
146 }
147 already_running = 0; /* re-enable */
148 }
149 }
150
151
152 SCM_DEFINE (scm_set_debug_cell_accesses_x, "set-debug-cell-accesses!", 1, 0, 0,
153 (SCM flag),
154 "If @var{flag} is @code{#f}, cell access checking is disabled.\n"
155 "If @var{flag} is @code{#t}, cell access checking is enabled.\n"
156 "This procedure only exists when the compile-time flag\n"
157 "@code{SCM_DEBUG_CELL_ACCESSES} was set to 1.")
158 #define FUNC_NAME s_scm_set_debug_cell_accesses_x
159 {
160 if (SCM_FALSEP (flag)) {
161 scm_debug_cell_accesses_p = 0;
162 } else if (SCM_EQ_P (flag, SCM_BOOL_T)) {
163 scm_debug_cell_accesses_p = 1;
164 } else {
165 SCM_WRONG_TYPE_ARG (1, flag);
166 }
167 return SCM_UNSPECIFIED;
168 }
169 #undef FUNC_NAME
170
171 #endif /* SCM_DEBUG_CELL_ACCESSES == 1 */
172
173 \f
174
175 /* {heap tuning parameters}
176 *
177 * These are parameters for controlling memory allocation. The heap
178 * is the area out of which scm_cons, and object headers are allocated.
179 *
180 * Each heap cell is 8 bytes on a 32 bit machine and 16 bytes on a
181 * 64 bit machine. The units of the _SIZE parameters are bytes.
182 * Cons pairs and object headers occupy one heap cell.
183 *
184 * SCM_INIT_HEAP_SIZE is the initial size of heap. If this much heap is
185 * allocated initially the heap will grow by half its current size
186 * each subsequent time more heap is needed.
187 *
188 * If SCM_INIT_HEAP_SIZE heap cannot be allocated initially, SCM_HEAP_SEG_SIZE
189 * will be used, and the heap will grow by SCM_HEAP_SEG_SIZE when more
190 * heap is needed. SCM_HEAP_SEG_SIZE must fit into type scm_sizet. This code
191 * is in scm_init_storage() and alloc_some_heap() in sys.c
192 *
193 * If SCM_INIT_HEAP_SIZE can be allocated initially, the heap will grow by
194 * SCM_EXPHEAP(scm_heap_size) when more heap is needed.
195 *
196 * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap
197 * is needed.
198 *
199 * INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
200 * trigger a GC.
201 *
202 * SCM_MTRIGGER_HYSTERESIS is the amount of malloc storage that must be
203 * reclaimed by a GC triggered by must_malloc. If less than this is
204 * reclaimed, the trigger threshold is raised. [I don't know what a
205 * good value is. I arbitrarily chose 1/10 of the INIT_MALLOC_LIMIT to
206 * work around a oscillation that caused almost constant GC.]
207 */
208
209 /*
210 * Heap size 45000 and 40% min yield gives quick startup and no extra
211 * heap allocation. Having higher values on min yield may lead to
212 * large heaps, especially if code behaviour is varying its
213 * maximum consumption between different freelists.
214 */
215
216 #define SCM_DATA_CELLS2CARDS(n) (((n) + SCM_GC_CARD_N_DATA_CELLS - 1) / SCM_GC_CARD_N_DATA_CELLS)
217 #define SCM_CARDS_PER_CLUSTER SCM_DATA_CELLS2CARDS (2000L)
218 #define SCM_CLUSTER_SIZE_1 (SCM_CARDS_PER_CLUSTER * SCM_GC_CARD_N_DATA_CELLS)
219 int scm_default_init_heap_size_1 = (((SCM_DATA_CELLS2CARDS (45000L) + SCM_CARDS_PER_CLUSTER - 1)
220 / SCM_CARDS_PER_CLUSTER) * SCM_GC_CARD_SIZE);
221 int scm_default_min_yield_1 = 40;
222
223 #define SCM_CLUSTER_SIZE_2 (SCM_CARDS_PER_CLUSTER * (SCM_GC_CARD_N_DATA_CELLS / 2))
224 int scm_default_init_heap_size_2 = (((SCM_DATA_CELLS2CARDS (2500L * 2) + SCM_CARDS_PER_CLUSTER - 1)
225 / SCM_CARDS_PER_CLUSTER) * SCM_GC_CARD_SIZE);
226 /* The following value may seem large, but note that if we get to GC at
227 * all, this means that we have a numerically intensive application
228 */
229 int scm_default_min_yield_2 = 40;
230
231 int scm_default_max_segment_size = 2097000L;/* a little less (adm) than 2 Mb */
232
233 #define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_CARD_SIZE)
234 #ifdef _QC
235 # define SCM_HEAP_SEG_SIZE 32768L
236 #else
237 # ifdef sequent
238 # define SCM_HEAP_SEG_SIZE (7000L * sizeof (scm_cell))
239 # else
240 # define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_cell))
241 # endif
242 #endif
243 /* Make heap grow with factor 1.5 */
244 #define SCM_EXPHEAP(scm_heap_size) (scm_heap_size / 2)
245 #define SCM_INIT_MALLOC_LIMIT 100000
246 #define SCM_MTRIGGER_HYSTERESIS (SCM_INIT_MALLOC_LIMIT/10)
247
248 /* CELL_UP and CELL_DN are used by scm_init_heap_seg to find (scm_cell * span)
249 aligned inner bounds for allocated storage */
250
251 #ifdef PROT386
252 /*in 386 protected mode we must only adjust the offset */
253 # define CELL_UP(p, span) MK_FP(FP_SEG(p), ~(8*(span)-1)&(FP_OFF(p)+8*(span)-1))
254 # define CELL_DN(p, span) MK_FP(FP_SEG(p), ~(8*(span)-1)&FP_OFF(p))
255 #else
256 # ifdef _UNICOS
257 # define CELL_UP(p, span) (SCM_CELLPTR)(~(span) & ((long)(p)+(span)))
258 # define CELL_DN(p, span) (SCM_CELLPTR)(~(span) & (long)(p))
259 # else
260 # define CELL_UP(p, span) (SCM_CELLPTR)(~(sizeof(scm_cell)*(span)-1L) & ((long)(p)+sizeof(scm_cell)*(span)-1L))
261 # define CELL_DN(p, span) (SCM_CELLPTR)(~(sizeof(scm_cell)*(span)-1L) & (long)(p))
262 # endif /* UNICOS */
263 #endif /* PROT386 */
264
265 #define DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_cell) - 1) & SCM_UNPACK (x)) == 0)
266
267 #define ALIGNMENT_SLACK(freelist) (SCM_GC_CARD_SIZE - 1)
268 #define CLUSTER_SIZE_IN_BYTES(freelist) \
269 (((freelist)->cluster_size / (SCM_GC_CARD_N_DATA_CELLS / (freelist)->span)) * SCM_GC_CARD_SIZE)
270
271 \f
272 /* scm_freelists
273 */
274
275 typedef struct scm_freelist_t {
276 /* collected cells */
277 SCM cells;
278 /* number of cells left to collect before cluster is full */
279 unsigned int left_to_collect;
280 /* number of clusters which have been allocated */
281 unsigned int clusters_allocated;
282 /* a list of freelists, each of size cluster_size,
283 * except the last one which may be shorter
284 */
285 SCM clusters;
286 SCM *clustertail;
287 /* this is the number of objects in each cluster, including the spine cell */
288 int cluster_size;
289 /* indicates that we should grow heap instead of GC:ing
290 */
291 int grow_heap_p;
292 /* minimum yield on this list in order not to grow the heap
293 */
294 long min_yield;
295 /* defines min_yield as percent of total heap size
296 */
297 int min_yield_fraction;
298 /* number of cells per object on this list */
299 int span;
300 /* number of collected cells during last GC */
301 long collected;
302 /* number of collected cells during penultimate GC */
303 long collected_1;
304 /* total number of cells in heap segments
305 * belonging to this list.
306 */
307 long heap_size;
308 } scm_freelist_t;
309
310 SCM scm_freelist = SCM_EOL;
311 scm_freelist_t scm_master_freelist = {
312 SCM_EOL, 0, 0, SCM_EOL, 0, SCM_CLUSTER_SIZE_1, 0, 0, 0, 1, 0, 0
313 };
314 SCM scm_freelist2 = SCM_EOL;
315 scm_freelist_t scm_master_freelist2 = {
316 SCM_EOL, 0, 0, SCM_EOL, 0, SCM_CLUSTER_SIZE_2, 0, 0, 0, 2, 0, 0
317 };
318
319 /* scm_mtrigger
320 * is the number of bytes of must_malloc allocation needed to trigger gc.
321 */
322 unsigned long scm_mtrigger;
323
324 /* scm_gc_heap_lock
325 * If set, don't expand the heap. Set only during gc, during which no allocation
326 * is supposed to take place anyway.
327 */
328 int scm_gc_heap_lock = 0;
329
330 /* GC Blocking
331 * Don't pause for collection if this is set -- just
332 * expand the heap.
333 */
334 int scm_block_gc = 1;
335
336 /* During collection, this accumulates objects holding
337 * weak references.
338 */
339 SCM scm_weak_vectors;
340
341 /* During collection, this accumulates structures which are to be freed.
342 */
343 SCM scm_structs_to_free;
344
345 /* GC Statistics Keeping
346 */
347 unsigned long scm_cells_allocated = 0;
348 long scm_mallocated = 0;
349 unsigned long scm_gc_cells_collected;
350 unsigned long scm_gc_yield;
351 static unsigned long scm_gc_yield_1 = 0; /* previous GC yield */
352 unsigned long scm_gc_malloc_collected;
353 unsigned long scm_gc_ports_collected;
354 unsigned long scm_gc_time_taken = 0;
355 static unsigned long t_before_gc;
356 static unsigned long t_before_sweep;
357 unsigned long scm_gc_mark_time_taken = 0;
358 unsigned long scm_gc_sweep_time_taken = 0;
359 unsigned long scm_gc_times = 0;
360 unsigned long scm_gc_cells_swept = 0;
361 double scm_gc_cells_marked_acc = 0.;
362 double scm_gc_cells_swept_acc = 0.;
363
364 SCM_SYMBOL (sym_cells_allocated, "cells-allocated");
365 SCM_SYMBOL (sym_heap_size, "cell-heap-size");
366 SCM_SYMBOL (sym_mallocated, "bytes-malloced");
367 SCM_SYMBOL (sym_mtrigger, "gc-malloc-threshold");
368 SCM_SYMBOL (sym_heap_segments, "cell-heap-segments");
369 SCM_SYMBOL (sym_gc_time_taken, "gc-time-taken");
370 SCM_SYMBOL (sym_gc_mark_time_taken, "gc-mark-time-taken");
371 SCM_SYMBOL (sym_gc_sweep_time_taken, "gc-sweep-time-taken");
372 SCM_SYMBOL (sym_times, "gc-times");
373 SCM_SYMBOL (sym_cells_marked, "cells-marked");
374 SCM_SYMBOL (sym_cells_swept, "cells-swept");
375
376 typedef struct scm_heap_seg_data_t
377 {
378 /* lower and upper bounds of the segment */
379 SCM_CELLPTR bounds[2];
380
381 /* address of the head-of-freelist pointer for this segment's cells.
382 All segments usually point to the same one, scm_freelist. */
383 scm_freelist_t *freelist;
384
385 /* number of cells per object in this segment */
386 int span;
387 } scm_heap_seg_data_t;
388
389
390
391 static scm_sizet init_heap_seg (SCM_CELLPTR, scm_sizet, scm_freelist_t *);
392
393 typedef enum { return_on_error, abort_on_error } policy_on_error;
394 static void alloc_some_heap (scm_freelist_t *, policy_on_error);
395
396
397 #define SCM_HEAP_SIZE \
398 (scm_master_freelist.heap_size + scm_master_freelist2.heap_size)
399 #define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
400
401 #define BVEC_GROW_SIZE 256
402 #define BVEC_GROW_SIZE_IN_LIMBS (SCM_GC_CARD_BVEC_SIZE_IN_LIMBS * BVEC_GROW_SIZE)
403 #define BVEC_GROW_SIZE_IN_BYTES (BVEC_GROW_SIZE_IN_LIMBS * sizeof (scm_c_bvec_limb_t))
404
405 /* mark space allocation */
406
407 typedef struct scm_mark_space_t
408 {
409 scm_c_bvec_limb_t *bvec_space;
410 struct scm_mark_space_t *next;
411 } scm_mark_space_t;
412
413 static scm_mark_space_t *current_mark_space;
414 static scm_mark_space_t **mark_space_ptr;
415 static int current_mark_space_offset;
416 static scm_mark_space_t *mark_space_head;
417
418 static scm_c_bvec_limb_t *
419 get_bvec ()
420 #define FUNC_NAME "get_bvec"
421 {
422 scm_c_bvec_limb_t *res;
423
424 if (!current_mark_space)
425 {
426 SCM_SYSCALL (current_mark_space = (scm_mark_space_t *) malloc (sizeof (scm_mark_space_t)));
427 if (!current_mark_space)
428 SCM_MISC_ERROR ("could not grow heap", SCM_EOL);
429
430 current_mark_space->bvec_space = NULL;
431 current_mark_space->next = NULL;
432
433 *mark_space_ptr = current_mark_space;
434 mark_space_ptr = &(current_mark_space->next);
435
436 return get_bvec ();
437 }
438
439 if (!(current_mark_space->bvec_space))
440 {
441 SCM_SYSCALL (current_mark_space->bvec_space =
442 (scm_c_bvec_limb_t *) calloc (BVEC_GROW_SIZE_IN_BYTES, 1));
443 if (!(current_mark_space->bvec_space))
444 SCM_MISC_ERROR ("could not grow heap", SCM_EOL);
445
446 current_mark_space_offset = 0;
447
448 return get_bvec ();
449 }
450
451 if (current_mark_space_offset == BVEC_GROW_SIZE_IN_LIMBS)
452 {
453 current_mark_space = NULL;
454
455 return get_bvec ();
456 }
457
458 res = current_mark_space->bvec_space + current_mark_space_offset;
459 current_mark_space_offset += SCM_GC_CARD_BVEC_SIZE_IN_LIMBS;
460
461 return res;
462 }
463 #undef FUNC_NAME
464
465
466 static void
467 clear_mark_space ()
468 {
469 scm_mark_space_t *ms;
470
471 for (ms = mark_space_head; ms; ms = ms->next)
472 memset (ms->bvec_space, 0, BVEC_GROW_SIZE_IN_BYTES);
473 }
474
475
476 \f
477 /* Debugging functions. */
478
479 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
480
481 /* Return the number of the heap segment containing CELL. */
482 static int
483 which_seg (SCM cell)
484 {
485 int i;
486
487 for (i = 0; i < scm_n_heap_segs; i++)
488 if (SCM_PTR_LE (scm_heap_table[i].bounds[0], SCM2PTR (cell))
489 && SCM_PTR_GT (scm_heap_table[i].bounds[1], SCM2PTR (cell)))
490 return i;
491 fprintf (stderr, "which_seg: can't find segment containing cell %lx\n",
492 SCM_UNPACK (cell));
493 abort ();
494 }
495
496
497 static void
498 map_free_list (scm_freelist_t *master, SCM freelist)
499 {
500 int last_seg = -1, count = 0;
501 SCM f;
502
503 for (f = freelist; !SCM_NULLP (f); f = SCM_FREE_CELL_CDR (f))
504 {
505 int this_seg = which_seg (f);
506
507 if (this_seg != last_seg)
508 {
509 if (last_seg != -1)
510 fprintf (stderr, " %5d %d-cells in segment %d\n",
511 count, master->span, last_seg);
512 last_seg = this_seg;
513 count = 0;
514 }
515 count++;
516 }
517 if (last_seg != -1)
518 fprintf (stderr, " %5d %d-cells in segment %d\n",
519 count, master->span, last_seg);
520 }
521
522 SCM_DEFINE (scm_map_free_list, "map-free-list", 0, 0, 0,
523 (),
524 "Print debugging information about the free-list.\n"
525 "@code{map-free-list} is only included in\n"
526 "@code{--enable-guile-debug} builds of Guile.")
527 #define FUNC_NAME s_scm_map_free_list
528 {
529 int i;
530 fprintf (stderr, "%d segments total (%d:%d",
531 scm_n_heap_segs,
532 scm_heap_table[0].span,
533 scm_heap_table[0].bounds[1] - scm_heap_table[0].bounds[0]);
534 for (i = 1; i < scm_n_heap_segs; i++)
535 fprintf (stderr, ", %d:%d",
536 scm_heap_table[i].span,
537 scm_heap_table[i].bounds[1] - scm_heap_table[i].bounds[0]);
538 fprintf (stderr, ")\n");
539 map_free_list (&scm_master_freelist, scm_freelist);
540 map_free_list (&scm_master_freelist2, scm_freelist2);
541 fflush (stderr);
542
543 return SCM_UNSPECIFIED;
544 }
545 #undef FUNC_NAME
546
547 static int last_cluster;
548 static int last_size;
549
550 static int
551 free_list_length (char *title, int i, SCM freelist)
552 {
553 SCM ls;
554 int n = 0;
555 for (ls = freelist; !SCM_NULLP (ls); ls = SCM_FREE_CELL_CDR (ls))
556 if (SCM_FREE_CELL_P (ls))
557 ++n;
558 else
559 {
560 fprintf (stderr, "bad cell in %s at position %d\n", title, n);
561 abort ();
562 }
563 if (n != last_size)
564 {
565 if (i > 0)
566 {
567 if (last_cluster == i - 1)
568 fprintf (stderr, "\t%d\n", last_size);
569 else
570 fprintf (stderr, "-%d\t%d\n", i - 1, last_size);
571 }
572 if (i >= 0)
573 fprintf (stderr, "%s %d", title, i);
574 else
575 fprintf (stderr, "%s\t%d\n", title, n);
576 last_cluster = i;
577 last_size = n;
578 }
579 return n;
580 }
581
582 static void
583 free_list_lengths (char *title, scm_freelist_t *master, SCM freelist)
584 {
585 SCM clusters;
586 int i = 0, len, n = 0;
587 fprintf (stderr, "%s\n\n", title);
588 n += free_list_length ("free list", -1, freelist);
589 for (clusters = master->clusters;
590 SCM_NNULLP (clusters);
591 clusters = SCM_CDR (clusters))
592 {
593 len = free_list_length ("cluster", i++, SCM_CAR (clusters));
594 n += len;
595 }
596 if (last_cluster == i - 1)
597 fprintf (stderr, "\t%d\n", last_size);
598 else
599 fprintf (stderr, "-%d\t%d\n", i - 1, last_size);
600 fprintf (stderr, "\ntotal %d objects\n\n", n);
601 }
602
603 SCM_DEFINE (scm_free_list_length, "free-list-length", 0, 0, 0,
604 (),
605 "Print debugging information about the free-list.\n"
606 "@code{free-list-length} is only included in\n"
607 "@code{--enable-guile-debug} builds of Guile.")
608 #define FUNC_NAME s_scm_free_list_length
609 {
610 free_list_lengths ("1-cells", &scm_master_freelist, scm_freelist);
611 free_list_lengths ("2-cells", &scm_master_freelist2, scm_freelist2);
612 return SCM_UNSPECIFIED;
613 }
614 #undef FUNC_NAME
615
616 #endif
617
618 #ifdef GUILE_DEBUG_FREELIST
619
620 /* Non-zero if freelist debugging is in effect. Set this via
621 `gc-set-debug-check-freelist!'. */
622 static int scm_debug_check_freelist = 0;
623
624 /* Number of calls to SCM_NEWCELL since startup. */
625 static unsigned long scm_newcell_count;
626 static unsigned long scm_newcell2_count;
627
628 /* Search freelist for anything that isn't marked as a free cell.
629 Abort if we find something. */
630 static void
631 scm_check_freelist (SCM freelist)
632 {
633 SCM f;
634 int i = 0;
635
636 for (f = freelist; !SCM_NULLP (f); f = SCM_FREE_CELL_CDR (f), i++)
637 if (!SCM_FREE_CELL_P (f))
638 {
639 fprintf (stderr, "Bad cell in freelist on newcell %lu: %d'th elt\n",
640 scm_newcell_count, i);
641 abort ();
642 }
643 }
644
645 SCM_DEFINE (scm_gc_set_debug_check_freelist_x, "gc-set-debug-check-freelist!", 1, 0, 0,
646 (SCM flag),
647 "If @var{flag} is @code{#t}, check the freelist for consistency\n"
648 "on each cell allocation. This procedure only exists when the\n"
649 "@code{GUILE_DEBUG_FREELIST} compile-time flag was selected.")
650 #define FUNC_NAME s_scm_gc_set_debug_check_freelist_x
651 {
652 /* [cmm] I did a double-take when I read this code the first time.
653 well, FWIW. */
654 SCM_VALIDATE_BOOL_COPY (1, flag, scm_debug_check_freelist);
655 return SCM_UNSPECIFIED;
656 }
657 #undef FUNC_NAME
658
659
660 SCM
661 scm_debug_newcell (void)
662 {
663 SCM new;
664
665 scm_newcell_count++;
666 if (scm_debug_check_freelist)
667 {
668 scm_check_freelist (scm_freelist);
669 scm_gc();
670 }
671
672 /* The rest of this is supposed to be identical to the SCM_NEWCELL
673 macro. */
674 if (SCM_NULLP (scm_freelist))
675 {
676 new = scm_gc_for_newcell (&scm_master_freelist, &scm_freelist);
677 SCM_GC_SET_ALLOCATED (new);
678 }
679 else
680 {
681 new = scm_freelist;
682 scm_freelist = SCM_FREE_CELL_CDR (scm_freelist);
683 SCM_GC_SET_ALLOCATED (new);
684 }
685
686 return new;
687 }
688
689 SCM
690 scm_debug_newcell2 (void)
691 {
692 SCM new;
693
694 scm_newcell2_count++;
695 if (scm_debug_check_freelist)
696 {
697 scm_check_freelist (scm_freelist2);
698 scm_gc ();
699 }
700
701 /* The rest of this is supposed to be identical to the SCM_NEWCELL
702 macro. */
703 if (SCM_NULLP (scm_freelist2))
704 {
705 new = scm_gc_for_newcell (&scm_master_freelist2, &scm_freelist2);
706 SCM_GC_SET_ALLOCATED (new);
707 }
708 else
709 {
710 new = scm_freelist2;
711 scm_freelist2 = SCM_FREE_CELL_CDR (scm_freelist2);
712 SCM_GC_SET_ALLOCATED (new);
713 }
714
715 return new;
716 }
717
718 #endif /* GUILE_DEBUG_FREELIST */
719
720 \f
721
722 static unsigned long
723 master_cells_allocated (scm_freelist_t *master)
724 {
725 /* the '- 1' below is to ignore the cluster spine cells. */
726 int objects = master->clusters_allocated * (master->cluster_size - 1);
727 if (SCM_NULLP (master->clusters))
728 objects -= master->left_to_collect;
729 return master->span * objects;
730 }
731
732 static unsigned long
733 freelist_length (SCM freelist)
734 {
735 int n;
736 for (n = 0; !SCM_NULLP (freelist); freelist = SCM_FREE_CELL_CDR (freelist))
737 ++n;
738 return n;
739 }
740
741 static unsigned long
742 compute_cells_allocated ()
743 {
744 return (scm_cells_allocated
745 + master_cells_allocated (&scm_master_freelist)
746 + master_cells_allocated (&scm_master_freelist2)
747 - scm_master_freelist.span * freelist_length (scm_freelist)
748 - scm_master_freelist2.span * freelist_length (scm_freelist2));
749 }
750
751 /* {Scheme Interface to GC}
752 */
753
754 SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0,
755 (),
756 "Return an association list of statistics about Guile's current\n"
757 "use of storage.")
758 #define FUNC_NAME s_scm_gc_stats
759 {
760 int i;
761 int n;
762 SCM heap_segs;
763 long int local_scm_mtrigger;
764 long int local_scm_mallocated;
765 long int local_scm_heap_size;
766 long int local_scm_cells_allocated;
767 long int local_scm_gc_time_taken;
768 long int local_scm_gc_times;
769 long int local_scm_gc_mark_time_taken;
770 long int local_scm_gc_sweep_time_taken;
771 double local_scm_gc_cells_swept;
772 double local_scm_gc_cells_marked;
773 SCM answer;
774
775 SCM_DEFER_INTS;
776
777 ++scm_block_gc;
778
779 retry:
780 heap_segs = SCM_EOL;
781 n = scm_n_heap_segs;
782 for (i = scm_n_heap_segs; i--; )
783 heap_segs = scm_cons (scm_cons (scm_ulong2num ((unsigned long)scm_heap_table[i].bounds[1]),
784 scm_ulong2num ((unsigned long)scm_heap_table[i].bounds[0])),
785 heap_segs);
786 if (scm_n_heap_segs != n)
787 goto retry;
788
789 --scm_block_gc;
790
791 /* Below, we cons to produce the resulting list. We want a snapshot of
792 * the heap situation before consing.
793 */
794 local_scm_mtrigger = scm_mtrigger;
795 local_scm_mallocated = scm_mallocated;
796 local_scm_heap_size = SCM_HEAP_SIZE;
797 local_scm_cells_allocated = compute_cells_allocated ();
798 local_scm_gc_time_taken = scm_gc_time_taken;
799 local_scm_gc_mark_time_taken = scm_gc_mark_time_taken;
800 local_scm_gc_sweep_time_taken = scm_gc_sweep_time_taken;
801 local_scm_gc_times = scm_gc_times;
802 local_scm_gc_cells_swept = scm_gc_cells_swept_acc;
803 local_scm_gc_cells_marked = scm_gc_cells_marked_acc;
804
805 answer = scm_listify (scm_cons (sym_gc_time_taken, scm_ulong2num (local_scm_gc_time_taken)),
806 scm_cons (sym_cells_allocated, scm_ulong2num (local_scm_cells_allocated)),
807 scm_cons (sym_heap_size, scm_ulong2num (local_scm_heap_size)),
808 scm_cons (sym_mallocated, scm_ulong2num (local_scm_mallocated)),
809 scm_cons (sym_mtrigger, scm_ulong2num (local_scm_mtrigger)),
810 scm_cons (sym_times, scm_ulong2num (local_scm_gc_times)),
811 scm_cons (sym_gc_mark_time_taken, scm_ulong2num (local_scm_gc_mark_time_taken)),
812 scm_cons (sym_gc_sweep_time_taken, scm_ulong2num (local_scm_gc_sweep_time_taken)),
813 scm_cons (sym_cells_marked, scm_dbl2big (local_scm_gc_cells_marked)),
814 scm_cons (sym_cells_swept, scm_dbl2big (local_scm_gc_cells_swept)),
815 scm_cons (sym_heap_segments, heap_segs),
816 SCM_UNDEFINED);
817 SCM_ALLOW_INTS;
818 return answer;
819 }
820 #undef FUNC_NAME
821
822
823 static void
824 gc_start_stats (const char *what)
825 {
826 t_before_gc = scm_c_get_internal_run_time ();
827 scm_gc_cells_swept = 0;
828 scm_gc_cells_collected = 0;
829 scm_gc_yield_1 = scm_gc_yield;
830 scm_gc_yield = (scm_cells_allocated
831 + master_cells_allocated (&scm_master_freelist)
832 + master_cells_allocated (&scm_master_freelist2));
833 scm_gc_malloc_collected = 0;
834 scm_gc_ports_collected = 0;
835 }
836
837
838 static void
839 gc_end_stats ()
840 {
841 unsigned long t = scm_c_get_internal_run_time ();
842 scm_gc_time_taken += (t - t_before_gc);
843 scm_gc_sweep_time_taken += (t - t_before_sweep);
844 ++scm_gc_times;
845
846 scm_gc_cells_marked_acc += scm_gc_cells_swept - scm_gc_cells_collected;
847 scm_gc_cells_swept_acc += scm_gc_cells_swept;
848 }
849
850
851 SCM_DEFINE (scm_object_address, "object-address", 1, 0, 0,
852 (SCM obj),
853 "Return an integer that for the lifetime of @var{obj} is uniquely\n"
854 "returned by this function for @var{obj}")
855 #define FUNC_NAME s_scm_object_address
856 {
857 return scm_ulong2num ((unsigned long) SCM_UNPACK (obj));
858 }
859 #undef FUNC_NAME
860
861
862 SCM_DEFINE (scm_gc, "gc", 0, 0, 0,
863 (),
864 "Scans all of SCM objects and reclaims for further use those that are\n"
865 "no longer accessible.")
866 #define FUNC_NAME s_scm_gc
867 {
868 SCM_DEFER_INTS;
869 scm_igc ("call");
870 SCM_ALLOW_INTS;
871 return SCM_UNSPECIFIED;
872 }
873 #undef FUNC_NAME
874
875
876 \f
877 /* {C Interface For When GC is Triggered}
878 */
879
880 static void
881 adjust_min_yield (scm_freelist_t *freelist)
882 {
883 /* min yield is adjusted upwards so that next predicted total yield
884 * (allocated cells actually freed by GC) becomes
885 * `min_yield_fraction' of total heap size. Note, however, that
886 * the absolute value of min_yield will correspond to `collected'
887 * on one master (the one which currently is triggering GC).
888 *
889 * The reason why we look at total yield instead of cells collected
890 * on one list is that we want to take other freelists into account.
891 * On this freelist, we know that (local) yield = collected cells,
892 * but that's probably not the case on the other lists.
893 *
894 * (We might consider computing a better prediction, for example
895 * by computing an average over multiple GC:s.)
896 */
897 if (freelist->min_yield_fraction)
898 {
899 /* Pick largest of last two yields. */
900 int delta = ((SCM_HEAP_SIZE * freelist->min_yield_fraction / 100)
901 - (long) SCM_MAX (scm_gc_yield_1, scm_gc_yield));
902 #ifdef DEBUGINFO
903 fprintf (stderr, " after GC = %d, delta = %d\n",
904 scm_cells_allocated,
905 delta);
906 #endif
907 if (delta > 0)
908 freelist->min_yield += delta;
909 }
910 }
911
912
913 /* When we get POSIX threads support, the master will be global and
914 * common while the freelist will be individual for each thread.
915 */
916
917 SCM
918 scm_gc_for_newcell (scm_freelist_t *master, SCM *freelist)
919 {
920 SCM cell;
921 ++scm_ints_disabled;
922 do
923 {
924 if (SCM_NULLP (master->clusters))
925 {
926 if (master->grow_heap_p || scm_block_gc)
927 {
928 /* In order to reduce gc frequency, try to allocate a new heap
929 * segment first, even if gc might find some free cells. If we
930 * can't obtain a new heap segment, we will try gc later.
931 */
932 master->grow_heap_p = 0;
933 alloc_some_heap (master, return_on_error);
934 }
935 if (SCM_NULLP (master->clusters))
936 {
937 /* The heap was not grown, either because it wasn't scheduled to
938 * grow, or because there was not enough memory available. In
939 * both cases we have to try gc to get some free cells.
940 */
941 #ifdef DEBUGINFO
942 fprintf (stderr, "allocated = %d, ",
943 scm_cells_allocated
944 + master_cells_allocated (&scm_master_freelist)
945 + master_cells_allocated (&scm_master_freelist2));
946 #endif
947 scm_igc ("cells");
948 adjust_min_yield (master);
949 if (SCM_NULLP (master->clusters))
950 {
951 /* gc could not free any cells. Now, we _must_ allocate a
952 * new heap segment, because there is no other possibility
953 * to provide a new cell for the caller.
954 */
955 alloc_some_heap (master, abort_on_error);
956 }
957 }
958 }
959 cell = SCM_CAR (master->clusters);
960 master->clusters = SCM_CDR (master->clusters);
961 ++master->clusters_allocated;
962 }
963 while (SCM_NULLP (cell));
964
965 #ifdef GUILE_DEBUG_FREELIST
966 scm_check_freelist (cell);
967 #endif
968
969 --scm_ints_disabled;
970 *freelist = SCM_FREE_CELL_CDR (cell);
971 return cell;
972 }
973
974
975 #if 0
976 /* This is a support routine which can be used to reserve a cluster
977 * for some special use, such as debugging. It won't be useful until
978 * free cells are preserved between garbage collections.
979 */
980
981 void
982 scm_alloc_cluster (scm_freelist_t *master)
983 {
984 SCM freelist, cell;
985 cell = scm_gc_for_newcell (master, &freelist);
986 SCM_SETCDR (cell, freelist);
987 return cell;
988 }
989 #endif
990
991
992 scm_c_hook_t scm_before_gc_c_hook;
993 scm_c_hook_t scm_before_mark_c_hook;
994 scm_c_hook_t scm_before_sweep_c_hook;
995 scm_c_hook_t scm_after_sweep_c_hook;
996 scm_c_hook_t scm_after_gc_c_hook;
997
998
999 void
1000 scm_igc (const char *what)
1001 {
1002 int j;
1003
1004 ++scm_gc_running_p;
1005 scm_c_hook_run (&scm_before_gc_c_hook, 0);
1006 #ifdef DEBUGINFO
1007 fprintf (stderr,
1008 SCM_NULLP (scm_freelist)
1009 ? "*"
1010 : (SCM_NULLP (scm_freelist2) ? "o" : "m"));
1011 #endif
1012 /* During the critical section, only the current thread may run. */
1013 SCM_CRITICAL_SECTION_START;
1014
1015 /* fprintf (stderr, "gc: %s\n", what); */
1016
1017 if (!scm_stack_base || scm_block_gc)
1018 {
1019 --scm_gc_running_p;
1020 return;
1021 }
1022
1023 gc_start_stats (what);
1024
1025 if (scm_mallocated < 0)
1026 /* The byte count of allocated objects has underflowed. This is
1027 probably because you forgot to report the sizes of objects you
1028 have allocated, by calling scm_done_malloc or some such. When
1029 the GC freed them, it subtracted their size from
1030 scm_mallocated, which underflowed. */
1031 abort ();
1032
1033 if (scm_gc_heap_lock)
1034 /* We've invoked the collector while a GC is already in progress.
1035 That should never happen. */
1036 abort ();
1037
1038 ++scm_gc_heap_lock;
1039
1040 /* flush dead entries from the continuation stack */
1041 {
1042 int x;
1043 int bound;
1044 SCM * elts;
1045 elts = SCM_VELTS (scm_continuation_stack);
1046 bound = SCM_VECTOR_LENGTH (scm_continuation_stack);
1047 x = SCM_INUM (scm_continuation_stack_ptr);
1048 while (x < bound)
1049 {
1050 elts[x] = SCM_BOOL_F;
1051 ++x;
1052 }
1053 }
1054
1055 scm_c_hook_run (&scm_before_mark_c_hook, 0);
1056
1057 clear_mark_space ();
1058
1059 #ifndef USE_THREADS
1060
1061 /* Mark objects on the C stack. */
1062 SCM_FLUSH_REGISTER_WINDOWS;
1063 /* This assumes that all registers are saved into the jmp_buf */
1064 setjmp (scm_save_regs_gc_mark);
1065 scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark,
1066 ( (scm_sizet) (sizeof (SCM_STACKITEM) - 1 +
1067 sizeof scm_save_regs_gc_mark)
1068 / sizeof (SCM_STACKITEM)));
1069
1070 {
1071 scm_sizet stack_len = scm_stack_size (scm_stack_base);
1072 #ifdef SCM_STACK_GROWS_UP
1073 scm_mark_locations (scm_stack_base, stack_len);
1074 #else
1075 scm_mark_locations (scm_stack_base - stack_len, stack_len);
1076 #endif
1077 }
1078
1079 #else /* USE_THREADS */
1080
1081 /* Mark every thread's stack and registers */
1082 scm_threads_mark_stacks ();
1083
1084 #endif /* USE_THREADS */
1085
1086 j = SCM_NUM_PROTECTS;
1087 while (j--)
1088 scm_gc_mark (scm_sys_protects[j]);
1089
1090 /* FIXME: we should have a means to register C functions to be run
1091 * in different phases of GC
1092 */
1093 scm_mark_subr_table ();
1094
1095 #ifndef USE_THREADS
1096 scm_gc_mark (scm_root->handle);
1097 #endif
1098
1099 t_before_sweep = scm_c_get_internal_run_time ();
1100 scm_gc_mark_time_taken += (t_before_sweep - t_before_gc);
1101
1102 scm_c_hook_run (&scm_before_sweep_c_hook, 0);
1103
1104 scm_gc_sweep ();
1105
1106 scm_c_hook_run (&scm_after_sweep_c_hook, 0);
1107
1108 --scm_gc_heap_lock;
1109 gc_end_stats ();
1110
1111 SCM_CRITICAL_SECTION_END;
1112 scm_c_hook_run (&scm_after_gc_c_hook, 0);
1113 --scm_gc_running_p;
1114 }
1115
1116 \f
1117
1118 /* {Mark/Sweep}
1119 */
1120
1121 #define MARK scm_gc_mark
1122 #define FNAME "scm_gc_mark"
1123
1124 #endif /*!MARK_DEPENDENCIES*/
1125
1126 /* Mark an object precisely.
1127 */
1128 void
1129 MARK (SCM p)
1130 #define FUNC_NAME FNAME
1131 {
1132 register long i;
1133 register SCM ptr;
1134 scm_bits_t cell_type;
1135
1136 #ifndef MARK_DEPENDENCIES
1137 # define RECURSE scm_gc_mark
1138 #else
1139 /* go through the usual marking, but not for self-cycles. */
1140 # define RECURSE(x) do { if ((x) != p) scm_gc_mark (x); } while (0)
1141 #endif
1142 ptr = p;
1143
1144 #ifdef MARK_DEPENDENCIES
1145 goto gc_mark_loop_first_time;
1146 #endif
1147
1148 /* A simple hack for debugging. Chose the second branch to get a
1149 meaningful backtrace for crashes inside the GC.
1150 */
1151 #if 1
1152 #define goto_gc_mark_loop goto gc_mark_loop
1153 #define goto_gc_mark_nimp goto gc_mark_nimp
1154 #else
1155 #define goto_gc_mark_loop RECURSE(ptr); return
1156 #define goto_gc_mark_nimp RECURSE(ptr); return
1157 #endif
1158
1159 gc_mark_loop:
1160 if (SCM_IMP (ptr))
1161 return;
1162
1163 gc_mark_nimp:
1164
1165 #ifdef MARK_DEPENDENCIES
1166 if (SCM_EQ_P (ptr, p))
1167 return;
1168
1169 scm_gc_mark (ptr);
1170 return;
1171
1172 gc_mark_loop_first_time:
1173 #endif
1174
1175 #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST))
1176 /* We are in debug mode. Check the ptr exhaustively. */
1177 if (!scm_cellp (ptr))
1178 SCM_MISC_ERROR ("rogue pointer in heap", SCM_EOL);
1179 #else
1180 /* In non-debug mode, do at least some cheap testing. */
1181 if (!SCM_CELLP (ptr))
1182 SCM_MISC_ERROR ("rogue pointer in heap", SCM_EOL);
1183 #endif
1184
1185 #ifndef MARK_DEPENDENCIES
1186
1187 if (SCM_GCMARKP (ptr))
1188 return;
1189
1190 SCM_SETGCMARK (ptr);
1191
1192 #endif
1193
1194 cell_type = SCM_GC_CELL_TYPE (ptr);
1195 switch (SCM_ITAG7 (cell_type))
1196 {
1197 case scm_tcs_cons_nimcar:
1198 if (SCM_IMP (SCM_CDR (ptr)))
1199 {
1200 ptr = SCM_CAR (ptr);
1201 goto_gc_mark_nimp;
1202 }
1203 RECURSE (SCM_CAR (ptr));
1204 ptr = SCM_CDR (ptr);
1205 goto_gc_mark_nimp;
1206 case scm_tcs_cons_imcar:
1207 ptr = SCM_CDR (ptr);
1208 goto_gc_mark_loop;
1209 case scm_tc7_pws:
1210 RECURSE (SCM_SETTER (ptr));
1211 ptr = SCM_PROCEDURE (ptr);
1212 goto_gc_mark_loop;
1213 case scm_tcs_cons_gloc:
1214 {
1215 /* Dirk:FIXME:: The following code is super ugly: ptr may be a
1216 * struct or a gloc. If it is a gloc, the cell word #0 of ptr
1217 * is the address of a scm_tc16_variable smob. If it is a
1218 * struct, the cell word #0 of ptr is a pointer to a struct
1219 * vtable data region. (The fact that these are accessed in
1220 * the same way restricts the possibilites to change the data
1221 * layout of structs or heap cells.) To discriminate between
1222 * the two, it is guaranteed that the scm_vtable_index_vcell
1223 * element of the prospective vtable is always zero. For a
1224 * gloc, this location has the CDR of the variable smob, which
1225 * is guaranteed to be non-zero.
1226 */
1227 scm_bits_t word0 = SCM_CELL_WORD_0 (ptr) - scm_tc3_cons_gloc;
1228 scm_bits_t * vtable_data = (scm_bits_t *) word0; /* access as struct */
1229 if (vtable_data [scm_vtable_index_vcell] != 0)
1230 {
1231 /* ptr is a gloc */
1232 SCM gloc_car = SCM_PACK (word0);
1233 RECURSE (gloc_car);
1234 ptr = SCM_CDR (ptr);
1235 goto gc_mark_loop;
1236 }
1237 else
1238 {
1239 /* ptr is a struct */
1240 SCM layout = SCM_PACK (vtable_data [scm_vtable_index_layout]);
1241 int len = SCM_SYMBOL_LENGTH (layout);
1242 char * fields_desc = SCM_SYMBOL_CHARS (layout);
1243 scm_bits_t * struct_data = (scm_bits_t *) SCM_STRUCT_DATA (ptr);
1244
1245 if (vtable_data[scm_struct_i_flags] & SCM_STRUCTF_ENTITY)
1246 {
1247 RECURSE (SCM_PACK (struct_data[scm_struct_i_procedure]));
1248 RECURSE (SCM_PACK (struct_data[scm_struct_i_setter]));
1249 }
1250 if (len)
1251 {
1252 int x;
1253
1254 for (x = 0; x < len - 2; x += 2, ++struct_data)
1255 if (fields_desc[x] == 'p')
1256 RECURSE (SCM_PACK (*struct_data));
1257 if (fields_desc[x] == 'p')
1258 {
1259 if (SCM_LAYOUT_TAILP (fields_desc[x + 1]))
1260 for (x = *struct_data++; x; --x, ++struct_data)
1261 RECURSE (SCM_PACK (*struct_data));
1262 else
1263 RECURSE (SCM_PACK (*struct_data));
1264 }
1265 }
1266 /* mark vtable */
1267 ptr = SCM_PACK (vtable_data [scm_vtable_index_vtable]);
1268 goto_gc_mark_loop;
1269 }
1270 }
1271 break;
1272 case scm_tcs_closures:
1273 if (SCM_IMP (SCM_ENV (ptr)))
1274 {
1275 ptr = SCM_CLOSCAR (ptr);
1276 goto_gc_mark_nimp;
1277 }
1278 RECURSE (SCM_CLOSCAR (ptr));
1279 ptr = SCM_ENV (ptr);
1280 goto_gc_mark_nimp;
1281 case scm_tc7_vector:
1282 i = SCM_VECTOR_LENGTH (ptr);
1283 if (i == 0)
1284 break;
1285 while (--i > 0)
1286 if (SCM_NIMP (SCM_VELTS (ptr)[i]))
1287 RECURSE (SCM_VELTS (ptr)[i]);
1288 ptr = SCM_VELTS (ptr)[0];
1289 goto_gc_mark_loop;
1290 #ifdef CCLO
1291 case scm_tc7_cclo:
1292 {
1293 unsigned long int i = SCM_CCLO_LENGTH (ptr);
1294 unsigned long int j;
1295 for (j = 1; j != i; ++j)
1296 {
1297 SCM obj = SCM_CCLO_REF (ptr, j);
1298 if (!SCM_IMP (obj))
1299 RECURSE (obj);
1300 }
1301 ptr = SCM_CCLO_REF (ptr, 0);
1302 goto_gc_mark_loop;
1303 }
1304 #endif
1305 #ifdef HAVE_ARRAYS
1306 case scm_tc7_bvect:
1307 case scm_tc7_byvect:
1308 case scm_tc7_ivect:
1309 case scm_tc7_uvect:
1310 case scm_tc7_fvect:
1311 case scm_tc7_dvect:
1312 case scm_tc7_cvect:
1313 case scm_tc7_svect:
1314 #ifdef HAVE_LONG_LONGS
1315 case scm_tc7_llvect:
1316 #endif
1317 #endif
1318 case scm_tc7_string:
1319 break;
1320
1321 case scm_tc7_substring:
1322 ptr = SCM_CDR (ptr);
1323 goto_gc_mark_loop;
1324
1325 case scm_tc7_wvect:
1326 SCM_WVECT_GC_CHAIN (ptr) = scm_weak_vectors;
1327 scm_weak_vectors = ptr;
1328 if (SCM_IS_WHVEC_ANY (ptr))
1329 {
1330 int x;
1331 int len;
1332 int weak_keys;
1333 int weak_values;
1334
1335 len = SCM_VECTOR_LENGTH (ptr);
1336 weak_keys = SCM_IS_WHVEC (ptr) || SCM_IS_WHVEC_B (ptr);
1337 weak_values = SCM_IS_WHVEC_V (ptr) || SCM_IS_WHVEC_B (ptr);
1338
1339 for (x = 0; x < len; ++x)
1340 {
1341 SCM alist;
1342 alist = SCM_VELTS (ptr)[x];
1343
1344 /* mark everything on the alist except the keys or
1345 * values, according to weak_values and weak_keys. */
1346 while ( SCM_CONSP (alist)
1347 && !SCM_GCMARKP (alist)
1348 && SCM_CONSP (SCM_CAR (alist)))
1349 {
1350 SCM kvpair;
1351 SCM next_alist;
1352
1353 kvpair = SCM_CAR (alist);
1354 next_alist = SCM_CDR (alist);
1355 /*
1356 * Do not do this:
1357 * SCM_SETGCMARK (alist);
1358 * SCM_SETGCMARK (kvpair);
1359 *
1360 * It may be that either the key or value is protected by
1361 * an escaped reference to part of the spine of this alist.
1362 * If we mark the spine here, and only mark one or neither of the
1363 * key and value, they may never be properly marked.
1364 * This leads to a horrible situation in which an alist containing
1365 * freelist cells is exported.
1366 *
1367 * So only mark the spines of these arrays last of all marking.
1368 * If somebody confuses us by constructing a weak vector
1369 * with a circular alist then we are hosed, but at least we
1370 * won't prematurely drop table entries.
1371 */
1372 if (!weak_keys)
1373 RECURSE (SCM_CAR (kvpair));
1374 if (!weak_values)
1375 RECURSE (SCM_CDR (kvpair));
1376 alist = next_alist;
1377 }
1378 if (SCM_NIMP (alist))
1379 RECURSE (alist);
1380 }
1381 }
1382 break;
1383
1384 case scm_tc7_symbol:
1385 ptr = SCM_PROP_SLOTS (ptr);
1386 goto_gc_mark_loop;
1387 case scm_tcs_subrs:
1388 break;
1389 case scm_tc7_port:
1390 i = SCM_PTOBNUM (ptr);
1391 #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST))
1392 if (!(i < scm_numptob))
1393 SCM_MISC_ERROR ("undefined port type", SCM_EOL);
1394 #endif
1395 if (SCM_PTAB_ENTRY(ptr))
1396 RECURSE (SCM_FILENAME (ptr));
1397 if (scm_ptobs[i].mark)
1398 {
1399 ptr = (scm_ptobs[i].mark) (ptr);
1400 goto_gc_mark_loop;
1401 }
1402 else
1403 return;
1404 break;
1405 case scm_tc7_smob:
1406 switch (SCM_TYP16 (ptr))
1407 { /* should be faster than going through scm_smobs */
1408 case scm_tc_free_cell:
1409 /* printf("found free_cell %X ", ptr); fflush(stdout); */
1410 case scm_tc16_big:
1411 case scm_tc16_real:
1412 case scm_tc16_complex:
1413 break;
1414 default:
1415 i = SCM_SMOBNUM (ptr);
1416 #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST))
1417 if (!(i < scm_numsmob))
1418 SCM_MISC_ERROR ("undefined smob type", SCM_EOL);
1419 #endif
1420 if (scm_smobs[i].mark)
1421 {
1422 ptr = (scm_smobs[i].mark) (ptr);
1423 goto_gc_mark_loop;
1424 }
1425 else
1426 return;
1427 }
1428 break;
1429 default:
1430 SCM_MISC_ERROR ("unknown type", SCM_EOL);
1431 }
1432 #undef RECURSE
1433 }
1434 #undef FUNC_NAME
1435
1436 #ifndef MARK_DEPENDENCIES
1437
1438 #undef MARK
1439 #undef FNAME
1440
1441 /* And here we define `scm_gc_mark_dependencies', by including this
1442 * same file in itself.
1443 */
1444 #define MARK scm_gc_mark_dependencies
1445 #define FNAME "scm_gc_mark_dependencies"
1446 #define MARK_DEPENDENCIES
1447 #include "gc.c"
1448 #undef MARK_DEPENDENCIES
1449 #undef MARK
1450 #undef FNAME
1451
1452
1453 /* Mark a Region Conservatively
1454 */
1455
1456 void
1457 scm_mark_locations (SCM_STACKITEM x[], scm_sizet n)
1458 {
1459 unsigned long m;
1460
1461 for (m = 0; m < n; ++m)
1462 {
1463 SCM obj = * (SCM *) &x[m];
1464 if (SCM_CELLP (obj))
1465 {
1466 SCM_CELLPTR ptr = SCM2PTR (obj);
1467 int i = 0;
1468 int j = scm_n_heap_segs - 1;
1469 if (SCM_PTR_LE (scm_heap_table[i].bounds[0], ptr)
1470 && SCM_PTR_GT (scm_heap_table[j].bounds[1], ptr))
1471 {
1472 while (i <= j)
1473 {
1474 int seg_id;
1475 seg_id = -1;
1476 if ((i == j)
1477 || SCM_PTR_GT (scm_heap_table[i].bounds[1], ptr))
1478 seg_id = i;
1479 else if (SCM_PTR_LE (scm_heap_table[j].bounds[0], ptr))
1480 seg_id = j;
1481 else
1482 {
1483 int k;
1484 k = (i + j) / 2;
1485 if (k == i)
1486 break;
1487 if (SCM_PTR_GT (scm_heap_table[k].bounds[1], ptr))
1488 {
1489 j = k;
1490 ++i;
1491 if (SCM_PTR_LE (scm_heap_table[i].bounds[0], ptr))
1492 continue;
1493 else
1494 break;
1495 }
1496 else if (SCM_PTR_LE (scm_heap_table[k].bounds[0], ptr))
1497 {
1498 i = k;
1499 --j;
1500 if (SCM_PTR_GT (scm_heap_table[j].bounds[1], ptr))
1501 continue;
1502 else
1503 break;
1504 }
1505 }
1506
1507 if (SCM_GC_IN_CARD_HEADERP (ptr))
1508 break;
1509
1510 if (scm_heap_table[seg_id].span == 1
1511 || DOUBLECELL_ALIGNED_P (obj))
1512 scm_gc_mark (obj);
1513
1514 break;
1515 }
1516 }
1517 }
1518 }
1519 }
1520
1521
1522 /* The function scm_cellp determines whether an SCM value can be regarded as a
1523 * pointer to a cell on the heap. Binary search is used in order to determine
1524 * the heap segment that contains the cell.
1525 */
1526 int
1527 scm_cellp (SCM value)
1528 {
1529 if (SCM_CELLP (value)) {
1530 scm_cell * ptr = SCM2PTR (value);
1531 unsigned int i = 0;
1532 unsigned int j = scm_n_heap_segs - 1;
1533
1534 if (SCM_GC_IN_CARD_HEADERP (ptr))
1535 return 0;
1536
1537 while (i < j) {
1538 int k = (i + j) / 2;
1539 if (SCM_PTR_GT (scm_heap_table[k].bounds[1], ptr)) {
1540 j = k;
1541 } else if (SCM_PTR_LE (scm_heap_table[k].bounds[0], ptr)) {
1542 i = k + 1;
1543 }
1544 }
1545
1546 if (SCM_PTR_LE (scm_heap_table[i].bounds[0], ptr)
1547 && SCM_PTR_GT (scm_heap_table[i].bounds[1], ptr)
1548 && (scm_heap_table[i].span == 1 || DOUBLECELL_ALIGNED_P (value))
1549 && !SCM_GC_IN_CARD_HEADERP (ptr)
1550 )
1551 return 1;
1552 else
1553 return 0;
1554 } else
1555 return 0;
1556 }
1557
1558
1559 static void
1560 gc_sweep_freelist_start (scm_freelist_t *freelist)
1561 {
1562 freelist->cells = SCM_EOL;
1563 freelist->left_to_collect = freelist->cluster_size;
1564 freelist->clusters_allocated = 0;
1565 freelist->clusters = SCM_EOL;
1566 freelist->clustertail = &freelist->clusters;
1567 freelist->collected_1 = freelist->collected;
1568 freelist->collected = 0;
1569 }
1570
1571 static void
1572 gc_sweep_freelist_finish (scm_freelist_t *freelist)
1573 {
1574 int collected;
1575 *freelist->clustertail = freelist->cells;
1576 if (!SCM_NULLP (freelist->cells))
1577 {
1578 SCM c = freelist->cells;
1579 SCM_SET_CELL_WORD_0 (c, SCM_FREE_CELL_CDR (c));
1580 SCM_SET_CELL_WORD_1 (c, SCM_EOL);
1581 freelist->collected +=
1582 freelist->span * (freelist->cluster_size - freelist->left_to_collect);
1583 }
1584 scm_gc_cells_collected += freelist->collected;
1585
1586 /* Although freelist->min_yield is used to test freelist->collected
1587 * (which is the local GC yield for freelist), it is adjusted so
1588 * that *total* yield is freelist->min_yield_fraction of total heap
1589 * size. This means that a too low yield is compensated by more
1590 * heap on the list which is currently doing most work, which is
1591 * just what we want.
1592 */
1593 collected = SCM_MAX (freelist->collected_1, freelist->collected);
1594 freelist->grow_heap_p = (collected < freelist->min_yield);
1595 }
1596
1597 #define NEXT_DATA_CELL(ptr, span) \
1598 do { \
1599 scm_cell *nxt__ = CELL_UP ((char *) (ptr) + 1, (span)); \
1600 (ptr) = (SCM_GC_IN_CARD_HEADERP (nxt__) ? \
1601 CELL_UP (SCM_GC_CELL_CARD (nxt__) + SCM_GC_CARD_N_HEADER_CELLS, span) \
1602 : nxt__); \
1603 } while (0)
1604
1605 void
1606 scm_gc_sweep ()
1607 #define FUNC_NAME "scm_gc_sweep"
1608 {
1609 register SCM_CELLPTR ptr;
1610 register SCM nfreelist;
1611 register scm_freelist_t *freelist;
1612 register long m;
1613 register int span;
1614 long i;
1615 scm_sizet seg_size;
1616
1617 m = 0;
1618
1619 gc_sweep_freelist_start (&scm_master_freelist);
1620 gc_sweep_freelist_start (&scm_master_freelist2);
1621
1622 for (i = 0; i < scm_n_heap_segs; i++)
1623 {
1624 register unsigned int left_to_collect;
1625 register scm_sizet j;
1626
1627 /* Unmarked cells go onto the front of the freelist this heap
1628 segment points to. Rather than updating the real freelist
1629 pointer as we go along, we accumulate the new head in
1630 nfreelist. Then, if it turns out that the entire segment is
1631 free, we free (i.e., malloc's free) the whole segment, and
1632 simply don't assign nfreelist back into the real freelist. */
1633 freelist = scm_heap_table[i].freelist;
1634 nfreelist = freelist->cells;
1635 left_to_collect = freelist->left_to_collect;
1636 span = scm_heap_table[i].span;
1637
1638 ptr = CELL_UP (scm_heap_table[i].bounds[0], span);
1639 seg_size = CELL_DN (scm_heap_table[i].bounds[1], span) - ptr;
1640
1641 /* use only data cells in seg_size */
1642 seg_size = (seg_size / SCM_GC_CARD_N_CELLS) * (SCM_GC_CARD_N_DATA_CELLS / span) * span;
1643
1644 scm_gc_cells_swept += seg_size;
1645
1646 for (j = seg_size + span; j -= span; ptr += span)
1647 {
1648 SCM scmptr;
1649
1650 if (SCM_GC_IN_CARD_HEADERP (ptr))
1651 {
1652 SCM_CELLPTR nxt;
1653
1654 /* cheat here */
1655 nxt = ptr;
1656 NEXT_DATA_CELL (nxt, span);
1657 j += span;
1658
1659 ptr = nxt - span;
1660 continue;
1661 }
1662
1663 scmptr = PTR2SCM (ptr);
1664
1665 if (SCM_GCMARKP (scmptr))
1666 continue;
1667
1668 switch SCM_TYP7 (scmptr)
1669 {
1670 case scm_tcs_cons_gloc:
1671 {
1672 /* Dirk:FIXME:: Again, super ugly code: scmptr may be a
1673 * struct or a gloc. See the corresponding comment in
1674 * scm_gc_mark.
1675 */
1676 scm_bits_t word0 = (SCM_CELL_WORD_0 (scmptr)
1677 - scm_tc3_cons_gloc);
1678 /* access as struct */
1679 scm_bits_t * vtable_data = (scm_bits_t *) word0;
1680 if (vtable_data[scm_vtable_index_vcell] == 0)
1681 {
1682 /* Structs need to be freed in a special order.
1683 * This is handled by GC C hooks in struct.c.
1684 */
1685 SCM_SET_STRUCT_GC_CHAIN (scmptr, scm_structs_to_free);
1686 scm_structs_to_free = scmptr;
1687 continue;
1688 }
1689 /* fall through so that scmptr gets collected */
1690 }
1691 break;
1692 case scm_tcs_cons_imcar:
1693 case scm_tcs_cons_nimcar:
1694 case scm_tcs_closures:
1695 case scm_tc7_pws:
1696 break;
1697 case scm_tc7_wvect:
1698 m += (2 + SCM_VECTOR_LENGTH (scmptr)) * sizeof (SCM);
1699 scm_must_free (SCM_VECTOR_BASE (scmptr) - 2);
1700 break;
1701 case scm_tc7_vector:
1702 {
1703 unsigned long int length = SCM_VECTOR_LENGTH (scmptr);
1704 if (length > 0)
1705 {
1706 m += length * sizeof (scm_bits_t);
1707 scm_must_free (SCM_VECTOR_BASE (scmptr));
1708 }
1709 break;
1710 }
1711 #ifdef CCLO
1712 case scm_tc7_cclo:
1713 m += (SCM_CCLO_LENGTH (scmptr) * sizeof (SCM));
1714 scm_must_free (SCM_CCLO_BASE (scmptr));
1715 break;
1716 #endif
1717 #ifdef HAVE_ARRAYS
1718 case scm_tc7_bvect:
1719 {
1720 unsigned long int length = SCM_BITVECTOR_LENGTH (scmptr);
1721 if (length > 0)
1722 {
1723 m += sizeof (long) * ((length + SCM_LONG_BIT - 1) / SCM_LONG_BIT);
1724 scm_must_free (SCM_BITVECTOR_BASE (scmptr));
1725 }
1726 }
1727 break;
1728 case scm_tc7_byvect:
1729 case scm_tc7_ivect:
1730 case scm_tc7_uvect:
1731 case scm_tc7_svect:
1732 #ifdef HAVE_LONG_LONGS
1733 case scm_tc7_llvect:
1734 #endif
1735 case scm_tc7_fvect:
1736 case scm_tc7_dvect:
1737 case scm_tc7_cvect:
1738 m += SCM_UVECTOR_LENGTH (scmptr) * scm_uniform_element_size (scmptr);
1739 scm_must_free (SCM_UVECTOR_BASE (scmptr));
1740 break;
1741 #endif
1742 case scm_tc7_substring:
1743 break;
1744 case scm_tc7_string:
1745 m += SCM_STRING_LENGTH (scmptr) + 1;
1746 scm_must_free (SCM_STRING_CHARS (scmptr));
1747 break;
1748 case scm_tc7_symbol:
1749 m += SCM_SYMBOL_LENGTH (scmptr) + 1;
1750 scm_must_free (SCM_SYMBOL_CHARS (scmptr));
1751 break;
1752 case scm_tcs_subrs:
1753 /* the various "subrs" (primitives) are never freed */
1754 continue;
1755 case scm_tc7_port:
1756 if SCM_OPENP (scmptr)
1757 {
1758 int k = SCM_PTOBNUM (scmptr);
1759 #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST))
1760 if (!(k < scm_numptob))
1761 SCM_MISC_ERROR ("undefined port type", SCM_EOL);
1762 #endif
1763 /* Keep "revealed" ports alive. */
1764 if (scm_revealed_count (scmptr) > 0)
1765 continue;
1766 /* Yes, I really do mean scm_ptobs[k].free */
1767 /* rather than ftobs[k].close. .close */
1768 /* is for explicit CLOSE-PORT by user */
1769 m += (scm_ptobs[k].free) (scmptr);
1770 SCM_SETSTREAM (scmptr, 0);
1771 scm_remove_from_port_table (scmptr);
1772 scm_gc_ports_collected++;
1773 SCM_CLR_PORT_OPEN_FLAG (scmptr);
1774 }
1775 break;
1776 case scm_tc7_smob:
1777 switch SCM_TYP16 (scmptr)
1778 {
1779 case scm_tc_free_cell:
1780 case scm_tc16_real:
1781 break;
1782 #ifdef SCM_BIGDIG
1783 case scm_tc16_big:
1784 m += (SCM_NUMDIGS (scmptr) * SCM_BITSPERDIG / SCM_CHAR_BIT);
1785 scm_must_free (SCM_BDIGITS (scmptr));
1786 break;
1787 #endif /* def SCM_BIGDIG */
1788 case scm_tc16_complex:
1789 m += sizeof (scm_complex_t);
1790 scm_must_free (SCM_COMPLEX_MEM (scmptr));
1791 break;
1792 default:
1793 {
1794 int k;
1795 k = SCM_SMOBNUM (scmptr);
1796 #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST))
1797 if (!(k < scm_numsmob))
1798 SCM_MISC_ERROR ("undefined smob type", SCM_EOL);
1799 #endif
1800 if (scm_smobs[k].free)
1801 m += (scm_smobs[k].free) (scmptr);
1802 break;
1803 }
1804 }
1805 break;
1806 default:
1807 SCM_MISC_ERROR ("unknown type", SCM_EOL);
1808 }
1809
1810 if (!--left_to_collect)
1811 {
1812 SCM_SET_CELL_WORD_0 (scmptr, nfreelist);
1813 *freelist->clustertail = scmptr;
1814 freelist->clustertail = SCM_CDRLOC (scmptr);
1815
1816 nfreelist = SCM_EOL;
1817 freelist->collected += span * freelist->cluster_size;
1818 left_to_collect = freelist->cluster_size;
1819 }
1820 else
1821 {
1822 /* Stick the new cell on the front of nfreelist. It's
1823 critical that we mark this cell as freed; otherwise, the
1824 conservative collector might trace it as some other type
1825 of object. */
1826 SCM_SET_CELL_TYPE (scmptr, scm_tc_free_cell);
1827 SCM_SET_FREE_CELL_CDR (scmptr, nfreelist);
1828 nfreelist = scmptr;
1829 }
1830 }
1831
1832 #ifdef GC_FREE_SEGMENTS
1833 if (n == seg_size)
1834 {
1835 register long j;
1836
1837 freelist->heap_size -= seg_size;
1838 free ((char *) scm_heap_table[i].bounds[0]);
1839 scm_heap_table[i].bounds[0] = 0;
1840 for (j = i + 1; j < scm_n_heap_segs; j++)
1841 scm_heap_table[j - 1] = scm_heap_table[j];
1842 scm_n_heap_segs -= 1;
1843 i--; /* We need to scan the segment just moved. */
1844 }
1845 else
1846 #endif /* ifdef GC_FREE_SEGMENTS */
1847 {
1848 /* Update the real freelist pointer to point to the head of
1849 the list of free cells we've built for this segment. */
1850 freelist->cells = nfreelist;
1851 freelist->left_to_collect = left_to_collect;
1852 }
1853
1854 #ifdef GUILE_DEBUG_FREELIST
1855 scm_map_free_list ();
1856 #endif
1857 }
1858
1859 gc_sweep_freelist_finish (&scm_master_freelist);
1860 gc_sweep_freelist_finish (&scm_master_freelist2);
1861
1862 /* When we move to POSIX threads private freelists should probably
1863 be GC-protected instead. */
1864 scm_freelist = SCM_EOL;
1865 scm_freelist2 = SCM_EOL;
1866
1867 scm_cells_allocated = (SCM_HEAP_SIZE - scm_gc_cells_collected);
1868 scm_gc_yield -= scm_cells_allocated;
1869 scm_mallocated -= m;
1870 scm_gc_malloc_collected = m;
1871 }
1872 #undef FUNC_NAME
1873
1874
1875 \f
1876 /* {Front end to malloc}
1877 *
1878 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
1879 * scm_done_free
1880 *
1881 * These functions provide services comparable to malloc, realloc, and
1882 * free. They should be used when allocating memory that will be under
1883 * control of the garbage collector, i.e., if the memory may be freed
1884 * during garbage collection.
1885 */
1886
1887 /* scm_must_malloc
1888 * Return newly malloced storage or throw an error.
1889 *
1890 * The parameter WHAT is a string for error reporting.
1891 * If the threshold scm_mtrigger will be passed by this
1892 * allocation, or if the first call to malloc fails,
1893 * garbage collect -- on the presumption that some objects
1894 * using malloced storage may be collected.
1895 *
1896 * The limit scm_mtrigger may be raised by this allocation.
1897 */
1898 void *
1899 scm_must_malloc (scm_sizet size, const char *what)
1900 {
1901 void *ptr;
1902 unsigned long nm = scm_mallocated + size;
1903
1904 if (nm <= scm_mtrigger)
1905 {
1906 SCM_SYSCALL (ptr = malloc (size));
1907 if (NULL != ptr)
1908 {
1909 scm_mallocated = nm;
1910 #ifdef GUILE_DEBUG_MALLOC
1911 scm_malloc_register (ptr, what);
1912 #endif
1913 return ptr;
1914 }
1915 }
1916
1917 scm_igc (what);
1918
1919 nm = scm_mallocated + size;
1920 SCM_SYSCALL (ptr = malloc (size));
1921 if (NULL != ptr)
1922 {
1923 scm_mallocated = nm;
1924 if (nm > scm_mtrigger - SCM_MTRIGGER_HYSTERESIS) {
1925 if (nm > scm_mtrigger)
1926 scm_mtrigger = nm + nm / 2;
1927 else
1928 scm_mtrigger += scm_mtrigger / 2;
1929 }
1930 #ifdef GUILE_DEBUG_MALLOC
1931 scm_malloc_register (ptr, what);
1932 #endif
1933
1934 return ptr;
1935 }
1936
1937 scm_memory_error (what);
1938 }
1939
1940
1941 /* scm_must_realloc
1942 * is similar to scm_must_malloc.
1943 */
1944 void *
1945 scm_must_realloc (void *where,
1946 scm_sizet old_size,
1947 scm_sizet size,
1948 const char *what)
1949 {
1950 void *ptr;
1951 scm_sizet nm = scm_mallocated + size - old_size;
1952
1953 if (nm <= scm_mtrigger)
1954 {
1955 SCM_SYSCALL (ptr = realloc (where, size));
1956 if (NULL != ptr)
1957 {
1958 scm_mallocated = nm;
1959 #ifdef GUILE_DEBUG_MALLOC
1960 scm_malloc_reregister (where, ptr, what);
1961 #endif
1962 return ptr;
1963 }
1964 }
1965
1966 scm_igc (what);
1967
1968 nm = scm_mallocated + size - old_size;
1969 SCM_SYSCALL (ptr = realloc (where, size));
1970 if (NULL != ptr)
1971 {
1972 scm_mallocated = nm;
1973 if (nm > scm_mtrigger - SCM_MTRIGGER_HYSTERESIS) {
1974 if (nm > scm_mtrigger)
1975 scm_mtrigger = nm + nm / 2;
1976 else
1977 scm_mtrigger += scm_mtrigger / 2;
1978 }
1979 #ifdef GUILE_DEBUG_MALLOC
1980 scm_malloc_reregister (where, ptr, what);
1981 #endif
1982 return ptr;
1983 }
1984
1985 scm_memory_error (what);
1986 }
1987
1988 char *
1989 scm_must_strndup (const char *str, unsigned long length)
1990 {
1991 char * dst = scm_must_malloc (length + 1, "scm_must_strndup");
1992 memcpy (dst, str, length);
1993 dst[length] = 0;
1994 return dst;
1995 }
1996
1997 char *
1998 scm_must_strdup (const char *str)
1999 {
2000 return scm_must_strndup (str, strlen (str));
2001 }
2002
2003 void
2004 scm_must_free (void *obj)
2005 #define FUNC_NAME "scm_must_free"
2006 {
2007 #ifdef GUILE_DEBUG_MALLOC
2008 scm_malloc_unregister (obj);
2009 #endif
2010 if (obj)
2011 free (obj);
2012 else
2013 SCM_MISC_ERROR ("freeing NULL pointer", SCM_EOL);
2014 }
2015 #undef FUNC_NAME
2016
2017
2018 /* Announce that there has been some malloc done that will be freed
2019 * during gc. A typical use is for a smob that uses some malloced
2020 * memory but can not get it from scm_must_malloc (for whatever
2021 * reason). When a new object of this smob is created you call
2022 * scm_done_malloc with the size of the object. When your smob free
2023 * function is called, be sure to include this size in the return
2024 * value.
2025 *
2026 * If you can't actually free the memory in the smob free function,
2027 * for whatever reason (like reference counting), you still can (and
2028 * should) report the amount of memory freed when you actually free it.
2029 * Do it by calling scm_done_malloc with the _negated_ size. Clever,
2030 * eh? Or even better, call scm_done_free. */
2031
2032 void
2033 scm_done_malloc (long size)
2034 {
2035 scm_mallocated += size;
2036
2037 if (scm_mallocated > scm_mtrigger)
2038 {
2039 scm_igc ("foreign mallocs");
2040 if (scm_mallocated > scm_mtrigger - SCM_MTRIGGER_HYSTERESIS)
2041 {
2042 if (scm_mallocated > scm_mtrigger)
2043 scm_mtrigger = scm_mallocated + scm_mallocated / 2;
2044 else
2045 scm_mtrigger += scm_mtrigger / 2;
2046 }
2047 }
2048 }
2049
2050 void
2051 scm_done_free (long size)
2052 {
2053 scm_mallocated -= size;
2054 }
2055
2056
2057 \f
2058 /* {Heap Segments}
2059 *
2060 * Each heap segment is an array of objects of a particular size.
2061 * Every segment has an associated (possibly shared) freelist.
2062 * A table of segment records is kept that records the upper and
2063 * lower extents of the segment; this is used during the conservative
2064 * phase of gc to identify probably gc roots (because they point
2065 * into valid segments at reasonable offsets). */
2066
2067 /* scm_expmem
2068 * is true if the first segment was smaller than INIT_HEAP_SEG.
2069 * If scm_expmem is set to one, subsequent segment allocations will
2070 * allocate segments of size SCM_EXPHEAP(scm_heap_size).
2071 */
2072 int scm_expmem = 0;
2073
2074 scm_sizet scm_max_segment_size;
2075
2076 /* scm_heap_org
2077 * is the lowest base address of any heap segment.
2078 */
2079 SCM_CELLPTR scm_heap_org;
2080
2081 scm_heap_seg_data_t * scm_heap_table = 0;
2082 static unsigned int heap_segment_table_size = 0;
2083 int scm_n_heap_segs = 0;
2084
2085 /* init_heap_seg
2086 * initializes a new heap segment and returns the number of objects it contains.
2087 *
2088 * The segment origin and segment size in bytes are input parameters.
2089 * The freelist is both input and output.
2090 *
2091 * This function presumes that the scm_heap_table has already been expanded
2092 * to accomodate a new segment record and that the markbit space was reserved
2093 * for all the cards in this segment.
2094 */
2095
2096 #define INIT_CARD(card, span) \
2097 do { \
2098 SCM_GC_SET_CARD_BVEC (card, get_bvec ()); \
2099 if ((span) == 2) \
2100 SCM_GC_SET_CARD_DOUBLECELL (card); \
2101 } while (0)
2102
2103 static scm_sizet
2104 init_heap_seg (SCM_CELLPTR seg_org, scm_sizet size, scm_freelist_t *freelist)
2105 {
2106 register SCM_CELLPTR ptr;
2107 SCM_CELLPTR seg_end;
2108 int new_seg_index;
2109 int n_new_cells;
2110 int span = freelist->span;
2111
2112 if (seg_org == NULL)
2113 return 0;
2114
2115 /* Align the begin ptr up.
2116 */
2117 ptr = SCM_GC_CARD_UP (seg_org);
2118
2119 /* Compute the ceiling on valid object pointers w/in this segment.
2120 */
2121 seg_end = SCM_GC_CARD_DOWN ((char *)seg_org + size);
2122
2123 /* Find the right place and insert the segment record.
2124 *
2125 */
2126 for (new_seg_index = 0;
2127 ( (new_seg_index < scm_n_heap_segs)
2128 && SCM_PTR_LE (scm_heap_table[new_seg_index].bounds[0], seg_org));
2129 new_seg_index++)
2130 ;
2131
2132 {
2133 int i;
2134 for (i = scm_n_heap_segs; i > new_seg_index; --i)
2135 scm_heap_table[i] = scm_heap_table[i - 1];
2136 }
2137
2138 ++scm_n_heap_segs;
2139
2140 scm_heap_table[new_seg_index].span = span;
2141 scm_heap_table[new_seg_index].freelist = freelist;
2142 scm_heap_table[new_seg_index].bounds[0] = ptr;
2143 scm_heap_table[new_seg_index].bounds[1] = seg_end;
2144
2145 /*n_new_cells*/
2146 n_new_cells = seg_end - ptr;
2147
2148 freelist->heap_size += n_new_cells;
2149
2150 /* Partition objects in this segment into clusters */
2151 {
2152 SCM clusters;
2153 SCM *clusterp = &clusters;
2154
2155 NEXT_DATA_CELL (ptr, span);
2156 while (ptr < seg_end)
2157 {
2158 scm_cell *nxt = ptr;
2159 scm_cell *prv = NULL;
2160 scm_cell *last_card = NULL;
2161 int n_data_cells = (SCM_GC_CARD_N_DATA_CELLS / span) * SCM_CARDS_PER_CLUSTER - 1;
2162 NEXT_DATA_CELL(nxt, span);
2163
2164 /* Allocate cluster spine
2165 */
2166 *clusterp = PTR2SCM (ptr);
2167 SCM_SETCAR (*clusterp, PTR2SCM (nxt));
2168 clusterp = SCM_CDRLOC (*clusterp);
2169 ptr = nxt;
2170
2171 while (n_data_cells--)
2172 {
2173 scm_cell *card = SCM_GC_CELL_CARD (ptr);
2174 SCM scmptr = PTR2SCM (ptr);
2175 nxt = ptr;
2176 NEXT_DATA_CELL (nxt, span);
2177 prv = ptr;
2178
2179 if (card != last_card)
2180 {
2181 INIT_CARD (card, span);
2182 last_card = card;
2183 }
2184
2185 SCM_SET_CELL_TYPE (scmptr, scm_tc_free_cell);
2186 SCM_SET_FREE_CELL_CDR (scmptr, PTR2SCM (nxt));
2187
2188 ptr = nxt;
2189 }
2190
2191 SCM_SET_FREE_CELL_CDR (PTR2SCM (prv), SCM_EOL);
2192 }
2193
2194 /* sanity check */
2195 {
2196 scm_cell *ref = seg_end;
2197 NEXT_DATA_CELL (ref, span);
2198 if (ref != ptr)
2199 /* [cmm] looks like the segment size doesn't divide cleanly by
2200 cluster size. bad cmm! */
2201 abort();
2202 }
2203
2204 /* Patch up the last cluster pointer in the segment
2205 * to join it to the input freelist.
2206 */
2207 *clusterp = freelist->clusters;
2208 freelist->clusters = clusters;
2209 }
2210
2211 #ifdef DEBUGINFO
2212 fprintf (stderr, "H");
2213 #endif
2214 return size;
2215 }
2216
2217 static scm_sizet
2218 round_to_cluster_size (scm_freelist_t *freelist, scm_sizet len)
2219 {
2220 scm_sizet cluster_size_in_bytes = CLUSTER_SIZE_IN_BYTES (freelist);
2221
2222 return
2223 (len + cluster_size_in_bytes - 1) / cluster_size_in_bytes * cluster_size_in_bytes
2224 + ALIGNMENT_SLACK (freelist);
2225 }
2226
2227 static void
2228 alloc_some_heap (scm_freelist_t *freelist, policy_on_error error_policy)
2229 #define FUNC_NAME "alloc_some_heap"
2230 {
2231 SCM_CELLPTR ptr;
2232 long len;
2233
2234 if (scm_gc_heap_lock)
2235 {
2236 /* Critical code sections (such as the garbage collector) aren't
2237 * supposed to add heap segments.
2238 */
2239 fprintf (stderr, "alloc_some_heap: Can not extend locked heap.\n");
2240 abort ();
2241 }
2242
2243 if (scm_n_heap_segs == heap_segment_table_size)
2244 {
2245 /* We have to expand the heap segment table to have room for the new
2246 * segment. Do not yet increment scm_n_heap_segs -- that is done by
2247 * init_heap_seg only if the allocation of the segment itself succeeds.
2248 */
2249 unsigned int new_table_size = scm_n_heap_segs + 1;
2250 size_t size = new_table_size * sizeof (scm_heap_seg_data_t);
2251 scm_heap_seg_data_t * new_heap_table;
2252
2253 SCM_SYSCALL (new_heap_table = ((scm_heap_seg_data_t *)
2254 realloc ((char *)scm_heap_table, size)));
2255 if (!new_heap_table)
2256 {
2257 if (error_policy == abort_on_error)
2258 {
2259 fprintf (stderr, "alloc_some_heap: Could not grow heap segment table.\n");
2260 abort ();
2261 }
2262 else
2263 {
2264 return;
2265 }
2266 }
2267 else
2268 {
2269 scm_heap_table = new_heap_table;
2270 heap_segment_table_size = new_table_size;
2271 }
2272 }
2273
2274 /* Pick a size for the new heap segment.
2275 * The rule for picking the size of a segment is explained in
2276 * gc.h
2277 */
2278 {
2279 /* Assure that the new segment is predicted to be large enough.
2280 *
2281 * New yield should at least equal GC fraction of new heap size, i.e.
2282 *
2283 * y + dh > f * (h + dh)
2284 *
2285 * y : yield
2286 * f : min yield fraction
2287 * h : heap size
2288 * dh : size of new heap segment
2289 *
2290 * This gives dh > (f * h - y) / (1 - f)
2291 */
2292 int f = freelist->min_yield_fraction;
2293 long h = SCM_HEAP_SIZE;
2294 long min_cells = (f * h - 100 * (long) scm_gc_yield) / (99 - f);
2295 len = SCM_EXPHEAP (freelist->heap_size);
2296 #ifdef DEBUGINFO
2297 fprintf (stderr, "(%d < %d)", len, min_cells);
2298 #endif
2299 if (len < min_cells)
2300 len = min_cells + freelist->cluster_size;
2301 len *= sizeof (scm_cell);
2302 /* force new sampling */
2303 freelist->collected = LONG_MAX;
2304 }
2305
2306 if (len > scm_max_segment_size)
2307 len = scm_max_segment_size;
2308
2309 {
2310 scm_sizet smallest;
2311
2312 smallest = CLUSTER_SIZE_IN_BYTES (freelist);
2313
2314 if (len < smallest)
2315 len = smallest;
2316
2317 /* Allocate with decaying ambition. */
2318 while ((len >= SCM_MIN_HEAP_SEG_SIZE)
2319 && (len >= smallest))
2320 {
2321 scm_sizet rounded_len = round_to_cluster_size (freelist, len);
2322 SCM_SYSCALL (ptr = (SCM_CELLPTR) malloc (rounded_len));
2323 if (ptr)
2324 {
2325 init_heap_seg (ptr, rounded_len, freelist);
2326 return;
2327 }
2328 len /= 2;
2329 }
2330 }
2331
2332 if (error_policy == abort_on_error)
2333 {
2334 fprintf (stderr, "alloc_some_heap: Could not grow heap.\n");
2335 abort ();
2336 }
2337 }
2338 #undef FUNC_NAME
2339
2340 \f
2341 /* {GC Protection Helper Functions}
2342 */
2343
2344
2345 /*
2346 * If within a function you need to protect one or more scheme objects from
2347 * garbage collection, pass them as parameters to one of the
2348 * scm_remember_upto_here* functions below. These functions don't do
2349 * anything, but since the compiler does not know that they are actually
2350 * no-ops, it will generate code that calls these functions with the given
2351 * parameters. Therefore, you can be sure that the compiler will keep those
2352 * scheme values alive (on the stack or in a register) up to the point where
2353 * scm_remember_upto_here* is called. In other words, place the call to
2354 * scm_remember_upt_here* _behind_ the last code in your function, that
2355 * depends on the scheme object to exist.
2356 *
2357 * Example: We want to make sure, that the string object str does not get
2358 * garbage collected during the execution of 'some_function', because
2359 * otherwise the characters belonging to str would be freed and
2360 * 'some_function' might access freed memory. To make sure that the compiler
2361 * keeps str alive on the stack or in a register such that it is visible to
2362 * the conservative gc we add the call to scm_remember_upto_here_1 _after_ the
2363 * call to 'some_function'. Note that this would not be necessary if str was
2364 * used anyway after the call to 'some_function'.
2365 * char *chars = SCM_STRING_CHARS (str);
2366 * some_function (chars);
2367 * scm_remember_upto_here_1 (str); // str will be alive up to this point.
2368 */
2369
2370 void
2371 scm_remember_upto_here_1 (SCM obj)
2372 {
2373 /* Empty. Protects a single object from garbage collection. */
2374 }
2375
2376 void
2377 scm_remember_upto_here_2 (SCM obj1, SCM obj2)
2378 {
2379 /* Empty. Protects two objects from garbage collection. */
2380 }
2381
2382 void
2383 scm_remember_upto_here (SCM obj, ...)
2384 {
2385 /* Empty. Protects any number of objects from garbage collection. */
2386 }
2387
2388
2389 #if (SCM_DEBUG_DEPRECATED == 0)
2390
2391 void
2392 scm_remember (SCM *ptr)
2393 {
2394 /* empty */
2395 }
2396
2397 #endif /* SCM_DEBUG_DEPRECATED == 0 */
2398
2399 /*
2400 These crazy functions prevent garbage collection
2401 of arguments after the first argument by
2402 ensuring they remain live throughout the
2403 function because they are used in the last
2404 line of the code block.
2405 It'd be better to have a nice compiler hint to
2406 aid the conservative stack-scanning GC. --03/09/00 gjb */
2407 SCM
2408 scm_return_first (SCM elt, ...)
2409 {
2410 return elt;
2411 }
2412
2413 int
2414 scm_return_first_int (int i, ...)
2415 {
2416 return i;
2417 }
2418
2419
2420 SCM
2421 scm_permanent_object (SCM obj)
2422 {
2423 SCM_REDEFER_INTS;
2424 scm_permobjs = scm_cons (obj, scm_permobjs);
2425 SCM_REALLOW_INTS;
2426 return obj;
2427 }
2428
2429
2430 /* Protect OBJ from the garbage collector. OBJ will not be freed, even if all
2431 other references are dropped, until the object is unprotected by calling
2432 scm_unprotect_object (OBJ). Calls to scm_protect/unprotect_object nest,
2433 i. e. it is possible to protect the same object several times, but it is
2434 necessary to unprotect the object the same number of times to actually get
2435 the object unprotected. It is an error to unprotect an object more often
2436 than it has been protected before. The function scm_protect_object returns
2437 OBJ.
2438 */
2439
2440 /* Implementation note: For every object X, there is a counter which
2441 scm_protect_object(X) increments and scm_unprotect_object(X) decrements.
2442 */
2443
2444 SCM
2445 scm_protect_object (SCM obj)
2446 {
2447 SCM handle;
2448
2449 /* This critical section barrier will be replaced by a mutex. */
2450 SCM_REDEFER_INTS;
2451
2452 handle = scm_hashq_create_handle_x (scm_protects, obj, SCM_MAKINUM (0));
2453 SCM_SETCDR (handle, SCM_MAKINUM (SCM_INUM (SCM_CDR (handle)) + 1));
2454
2455 SCM_REALLOW_INTS;
2456
2457 return obj;
2458 }
2459
2460
2461 /* Remove any protection for OBJ established by a prior call to
2462 scm_protect_object. This function returns OBJ.
2463
2464 See scm_protect_object for more information. */
2465 SCM
2466 scm_unprotect_object (SCM obj)
2467 {
2468 SCM handle;
2469
2470 /* This critical section barrier will be replaced by a mutex. */
2471 SCM_REDEFER_INTS;
2472
2473 handle = scm_hashq_get_handle (scm_protects, obj);
2474
2475 if (SCM_FALSEP (handle))
2476 {
2477 fprintf (stderr, "scm_unprotect_object called on unprotected object\n");
2478 abort ();
2479 }
2480 else
2481 {
2482 unsigned long int count = SCM_INUM (SCM_CDR (handle)) - 1;
2483 if (count == 0)
2484 scm_hashq_remove_x (scm_protects, obj);
2485 else
2486 SCM_SETCDR (handle, SCM_MAKINUM (count));
2487 }
2488
2489 SCM_REALLOW_INTS;
2490
2491 return obj;
2492 }
2493
2494 int terminating;
2495
2496 /* called on process termination. */
2497 #ifdef HAVE_ATEXIT
2498 static void
2499 cleanup (void)
2500 #else
2501 #ifdef HAVE_ON_EXIT
2502 extern int on_exit (void (*procp) (), int arg);
2503
2504 static void
2505 cleanup (int status, void *arg)
2506 #else
2507 #error Dont know how to setup a cleanup handler on your system.
2508 #endif
2509 #endif
2510 {
2511 terminating = 1;
2512 scm_flush_all_ports ();
2513 }
2514
2515 \f
2516 static int
2517 make_initial_segment (scm_sizet init_heap_size, scm_freelist_t *freelist)
2518 {
2519 scm_sizet rounded_size = round_to_cluster_size (freelist, init_heap_size);
2520
2521 if (!init_heap_seg ((SCM_CELLPTR) malloc (rounded_size),
2522 rounded_size,
2523 freelist))
2524 {
2525 rounded_size = round_to_cluster_size (freelist, SCM_HEAP_SEG_SIZE);
2526 if (!init_heap_seg ((SCM_CELLPTR) malloc (rounded_size),
2527 rounded_size,
2528 freelist))
2529 return 1;
2530 }
2531 else
2532 scm_expmem = 1;
2533
2534 if (freelist->min_yield_fraction)
2535 freelist->min_yield = (freelist->heap_size * freelist->min_yield_fraction
2536 / 100);
2537 freelist->grow_heap_p = (freelist->heap_size < freelist->min_yield);
2538
2539 return 0;
2540 }
2541
2542 \f
2543 static void
2544 init_freelist (scm_freelist_t *freelist,
2545 int span,
2546 int cluster_size,
2547 int min_yield)
2548 {
2549 freelist->clusters = SCM_EOL;
2550 freelist->cluster_size = cluster_size + 1;
2551 freelist->left_to_collect = 0;
2552 freelist->clusters_allocated = 0;
2553 freelist->min_yield = 0;
2554 freelist->min_yield_fraction = min_yield;
2555 freelist->span = span;
2556 freelist->collected = 0;
2557 freelist->collected_1 = 0;
2558 freelist->heap_size = 0;
2559 }
2560
2561
2562 /* Get an integer from an environment variable. */
2563 static int
2564 scm_i_getenv_int (const char *var, int def)
2565 {
2566 char *end, *val = getenv (var);
2567 long res;
2568 if (!val)
2569 return def;
2570 res = strtol (val, &end, 10);
2571 if (end == val)
2572 return def;
2573 return res;
2574 }
2575
2576
2577 int
2578 scm_init_storage ()
2579 {
2580 scm_sizet gc_trigger_1;
2581 scm_sizet gc_trigger_2;
2582 scm_sizet init_heap_size_1;
2583 scm_sizet init_heap_size_2;
2584 scm_sizet j;
2585
2586 #if (SCM_DEBUG_CELL_ACCESSES == 1)
2587 scm_tc16_allocated = scm_make_smob_type ("allocated cell", 0);
2588 #endif /* SCM_DEBUG_CELL_ACCESSES == 1 */
2589
2590 j = SCM_NUM_PROTECTS;
2591 while (j)
2592 scm_sys_protects[--j] = SCM_BOOL_F;
2593 scm_block_gc = 1;
2594
2595 scm_freelist = SCM_EOL;
2596 scm_freelist2 = SCM_EOL;
2597 gc_trigger_1 = scm_i_getenv_int ("GUILE_MIN_YIELD_1", scm_default_min_yield_1);
2598 init_freelist (&scm_master_freelist, 1, SCM_CLUSTER_SIZE_1, gc_trigger_1);
2599 gc_trigger_2 = scm_i_getenv_int ("GUILE_MIN_YIELD_2", scm_default_min_yield_2);
2600 init_freelist (&scm_master_freelist2, 2, SCM_CLUSTER_SIZE_2, gc_trigger_2);
2601 scm_max_segment_size = scm_i_getenv_int ("GUILE_MAX_SEGMENT_SIZE", scm_default_max_segment_size);
2602
2603 scm_expmem = 0;
2604
2605 j = SCM_HEAP_SEG_SIZE;
2606 scm_mtrigger = SCM_INIT_MALLOC_LIMIT;
2607 scm_heap_table = ((scm_heap_seg_data_t *)
2608 scm_must_malloc (sizeof (scm_heap_seg_data_t) * 2, "hplims"));
2609 heap_segment_table_size = 2;
2610
2611 mark_space_ptr = &mark_space_head;
2612
2613 init_heap_size_1 = scm_i_getenv_int ("GUILE_INIT_SEGMENT_SIZE_1", scm_default_init_heap_size_1);
2614 init_heap_size_2 = scm_i_getenv_int ("GUILE_INIT_SEGMENT_SIZE_2", scm_default_init_heap_size_2);
2615 if (make_initial_segment (init_heap_size_1, &scm_master_freelist) ||
2616 make_initial_segment (init_heap_size_2, &scm_master_freelist2))
2617 return 1;
2618
2619 /* scm_hplims[0] can change. do not remove scm_heap_org */
2620 scm_heap_org = CELL_UP (scm_heap_table[0].bounds[0], 1);
2621
2622 scm_c_hook_init (&scm_before_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
2623 scm_c_hook_init (&scm_before_mark_c_hook, 0, SCM_C_HOOK_NORMAL);
2624 scm_c_hook_init (&scm_before_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
2625 scm_c_hook_init (&scm_after_sweep_c_hook, 0, SCM_C_HOOK_NORMAL);
2626 scm_c_hook_init (&scm_after_gc_c_hook, 0, SCM_C_HOOK_NORMAL);
2627
2628 /* Initialise the list of ports. */
2629 scm_port_table = (scm_port **)
2630 malloc (sizeof (scm_port *) * scm_port_table_room);
2631 if (!scm_port_table)
2632 return 1;
2633
2634 #ifdef HAVE_ATEXIT
2635 atexit (cleanup);
2636 #else
2637 #ifdef HAVE_ON_EXIT
2638 on_exit (cleanup, 0);
2639 #endif
2640 #endif
2641
2642 scm_stand_in_procs = SCM_EOL;
2643 scm_permobjs = SCM_EOL;
2644 scm_protects = scm_c_make_hash_table (31);
2645
2646 return 0;
2647 }
2648
2649 \f
2650
2651 SCM scm_after_gc_hook;
2652
2653 static SCM gc_async;
2654
2655 /* The function gc_async_thunk causes the execution of the after-gc-hook. It
2656 * is run after the gc, as soon as the asynchronous events are handled by the
2657 * evaluator.
2658 */
2659 static SCM
2660 gc_async_thunk (void)
2661 {
2662 scm_c_run_hook (scm_after_gc_hook, SCM_EOL);
2663 return SCM_UNSPECIFIED;
2664 }
2665
2666
2667 /* The function mark_gc_async is run by the scm_after_gc_c_hook at the end of
2668 * the garbage collection. The only purpose of this function is to mark the
2669 * gc_async (which will eventually lead to the execution of the
2670 * gc_async_thunk).
2671 */
2672 static void *
2673 mark_gc_async (void * hook_data, void *func_data, void *data)
2674 {
2675 scm_system_async_mark (gc_async);
2676 return NULL;
2677 }
2678
2679
2680 void
2681 scm_init_gc ()
2682 {
2683 SCM after_gc_thunk;
2684
2685 /* Dirk:FIXME:: scm_create_hook is strange. */
2686 scm_after_gc_hook = scm_create_hook ("after-gc-hook", 0);
2687
2688 after_gc_thunk = scm_c_make_subr ("%gc-thunk", scm_tc7_subr_0,
2689 gc_async_thunk);
2690 gc_async = scm_system_async (after_gc_thunk); /* protected via scm_asyncs */
2691
2692 scm_c_hook_add (&scm_after_gc_c_hook, mark_gc_async, NULL, 0);
2693
2694 #ifndef SCM_MAGIC_SNARFER
2695 #include "libguile/gc.x"
2696 #endif
2697 }
2698
2699 #endif /*MARK_DEPENDENCIES*/
2700
2701 /*
2702 Local Variables:
2703 c-file-style: "gnu"
2704 End:
2705 */