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