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