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