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