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