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