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