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