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