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