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