Commit | Line | Data |
---|---|---|
22a52da1 | 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc. |
a00c95d9 | 2 | * |
0f2d19dd JB |
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. | |
a00c95d9 | 7 | * |
0f2d19dd JB |
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. | |
a00c95d9 | 12 | * |
0f2d19dd JB |
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 | 41 | |
1bbd0b84 | 42 | |
37ddcaf6 MD |
43 | /* #define DEBUGINFO */ |
44 | ||
56495472 ML |
45 | /* SECTION: This code is compiled once. |
46 | */ | |
47 | ||
48 | #ifndef MARK_DEPENDENCIES | |
49 | ||
0f2d19dd JB |
50 | \f |
51 | #include <stdio.h> | |
e6e2e95a | 52 | #include <errno.h> |
783e7774 | 53 | #include <string.h> |
e6e2e95a | 54 | |
a0599745 | 55 | #include "libguile/_scm.h" |
0a7a7445 | 56 | #include "libguile/eval.h" |
a0599745 MD |
57 | #include "libguile/stime.h" |
58 | #include "libguile/stackchk.h" | |
59 | #include "libguile/struct.h" | |
a0599745 MD |
60 | #include "libguile/smob.h" |
61 | #include "libguile/unif.h" | |
62 | #include "libguile/async.h" | |
63 | #include "libguile/ports.h" | |
64 | #include "libguile/root.h" | |
65 | #include "libguile/strings.h" | |
66 | #include "libguile/vectors.h" | |
801cb5e7 | 67 | #include "libguile/weaks.h" |
686765af | 68 | #include "libguile/hashtab.h" |
ecf470a2 | 69 | #include "libguile/tags.h" |
a0599745 MD |
70 | |
71 | #include "libguile/validate.h" | |
1be6b49c | 72 | #include "libguile/deprecation.h" |
a0599745 | 73 | #include "libguile/gc.h" |
fce59c93 | 74 | |
bc9d9bb2 | 75 | #ifdef GUILE_DEBUG_MALLOC |
a0599745 | 76 | #include "libguile/debug-malloc.h" |
bc9d9bb2 MD |
77 | #endif |
78 | ||
0f2d19dd | 79 | #ifdef HAVE_MALLOC_H |
95b88819 | 80 | #include <malloc.h> |
0f2d19dd JB |
81 | #endif |
82 | ||
83 | #ifdef HAVE_UNISTD_H | |
95b88819 | 84 | #include <unistd.h> |
0f2d19dd JB |
85 | #endif |
86 | ||
1cc91f1b JB |
87 | #ifdef __STDC__ |
88 | #include <stdarg.h> | |
89 | #define var_start(x, y) va_start(x, y) | |
90 | #else | |
91 | #include <varargs.h> | |
92 | #define var_start(x, y) va_start(x) | |
93 | #endif | |
94 | ||
0f2d19dd | 95 | \f |
406c7d90 DH |
96 | |
97 | unsigned int scm_gc_running_p = 0; | |
98 | ||
99 | \f | |
100 | ||
101 | #if (SCM_DEBUG_CELL_ACCESSES == 1) | |
102 | ||
92c2555f | 103 | scm_t_bits scm_tc16_allocated; |
61045190 DH |
104 | |
105 | /* Set this to != 0 if every cell that is accessed shall be checked: | |
106 | */ | |
107 | unsigned int scm_debug_cell_accesses_p = 1; | |
406c7d90 | 108 | |
e81d98ec DH |
109 | /* Set this to 0 if no additional gc's shall be performed, otherwise set it to |
110 | * the number of cell accesses after which a gc shall be called. | |
111 | */ | |
112 | static unsigned int debug_cells_gc_interval = 0; | |
113 | ||
406c7d90 | 114 | |
592996c9 DH |
115 | /* If an allocated cell is detected during garbage collection, this means that |
116 | * some code has just obtained the object but was preempted before the | |
117 | * initialization of the object was completed. This meanst that some entries | |
118 | * of the allocated cell may already contain SCM objects. Therefore, | |
119 | * allocated cells are scanned conservatively. */ | |
120 | static SCM | |
121 | allocated_mark (SCM allocated) | |
122 | { | |
123 | scm_gc_mark_cell_conservatively (allocated); | |
124 | return SCM_BOOL_F; | |
125 | } | |
126 | ||
127 | ||
406c7d90 DH |
128 | /* Assert that the given object is a valid reference to a valid cell. This |
129 | * test involves to determine whether the object is a cell pointer, whether | |
130 | * this pointer actually points into a heap segment and whether the cell | |
e81d98ec DH |
131 | * pointed to is not a free cell. Further, additional garbage collections may |
132 | * get executed after a user defined number of cell accesses. This helps to | |
133 | * find places in the C code where references are dropped for extremely short | |
134 | * periods. | |
406c7d90 DH |
135 | */ |
136 | void | |
137 | scm_assert_cell_valid (SCM cell) | |
138 | { | |
61045190 DH |
139 | static unsigned int already_running = 0; |
140 | ||
141 | if (scm_debug_cell_accesses_p && !already_running) | |
406c7d90 | 142 | { |
61045190 | 143 | already_running = 1; /* set to avoid recursion */ |
406c7d90 | 144 | |
9d47a1e6 | 145 | if (!scm_cellp (cell)) |
406c7d90 | 146 | { |
1be6b49c ML |
147 | fprintf (stderr, "scm_assert_cell_valid: Not a cell object: %lux\n", |
148 | (unsigned long) SCM_UNPACK (cell)); | |
406c7d90 DH |
149 | abort (); |
150 | } | |
151 | else if (!scm_gc_running_p) | |
152 | { | |
153 | /* Dirk::FIXME:: During garbage collection there occur references to | |
154 | free cells. This is allright during conservative marking, but | |
155 | should not happen otherwise (I think). The case of free cells | |
156 | accessed during conservative marking is handled in function | |
157 | scm_mark_locations. However, there still occur accesses to free | |
158 | cells during gc. I don't understand why this happens. If it is | |
159 | a bug and gets fixed, the following test should also work while | |
160 | gc is running. | |
161 | */ | |
162 | if (SCM_FREE_CELL_P (cell)) | |
163 | { | |
1be6b49c ML |
164 | fprintf (stderr, "scm_assert_cell_valid: Accessing free cell: %lux\n", |
165 | (unsigned long) SCM_UNPACK (cell)); | |
406c7d90 DH |
166 | abort (); |
167 | } | |
e81d98ec DH |
168 | |
169 | /* If desired, perform additional garbage collections after a user | |
170 | * defined number of cell accesses. | |
171 | */ | |
172 | if (debug_cells_gc_interval) | |
173 | { | |
174 | static unsigned int counter = 0; | |
175 | ||
176 | if (counter != 0) | |
177 | { | |
178 | --counter; | |
179 | } | |
180 | else | |
181 | { | |
182 | counter = debug_cells_gc_interval; | |
183 | scm_igc ("scm_assert_cell_valid"); | |
184 | } | |
185 | } | |
406c7d90 | 186 | } |
61045190 | 187 | already_running = 0; /* re-enable */ |
406c7d90 DH |
188 | } |
189 | } | |
190 | ||
191 | ||
192 | SCM_DEFINE (scm_set_debug_cell_accesses_x, "set-debug-cell-accesses!", 1, 0, 0, | |
193 | (SCM flag), | |
1e6808ea | 194 | "If @var{flag} is @code{#f}, cell access checking is disabled.\n" |
e81d98ec DH |
195 | "If @var{flag} is @code{#t}, cell access checking is enabled,\n" |
196 | "but no additional calls to garbage collection are issued.\n" | |
197 | "If @var{flag} is a number, cell access checking is enabled,\n" | |
198 | "with an additional garbage collection after the given\n" | |
199 | "number of cell accesses.\n" | |
1e6808ea MG |
200 | "This procedure only exists when the compile-time flag\n" |
201 | "@code{SCM_DEBUG_CELL_ACCESSES} was set to 1.") | |
406c7d90 DH |
202 | #define FUNC_NAME s_scm_set_debug_cell_accesses_x |
203 | { | |
204 | if (SCM_FALSEP (flag)) { | |
205 | scm_debug_cell_accesses_p = 0; | |
206 | } else if (SCM_EQ_P (flag, SCM_BOOL_T)) { | |
e81d98ec DH |
207 | debug_cells_gc_interval = 0; |
208 | scm_debug_cell_accesses_p = 1; | |
209 | } else if (SCM_INUMP (flag)) { | |
210 | long int f = SCM_INUM (flag); | |
211 | if (f <= 0) SCM_OUT_OF_RANGE (1, flag); | |
212 | debug_cells_gc_interval = f; | |
406c7d90 DH |
213 | scm_debug_cell_accesses_p = 1; |
214 | } else { | |
215 | SCM_WRONG_TYPE_ARG (1, flag); | |
216 | } | |
217 | return SCM_UNSPECIFIED; | |
218 | } | |
219 | #undef FUNC_NAME | |
220 | ||
221 | #endif /* SCM_DEBUG_CELL_ACCESSES == 1 */ | |
222 | ||
223 | \f | |
224 | ||
0f2d19dd | 225 | /* {heap tuning parameters} |
a00c95d9 | 226 | * |
0f2d19dd JB |
227 | * These are parameters for controlling memory allocation. The heap |
228 | * is the area out of which scm_cons, and object headers are allocated. | |
229 | * | |
230 | * Each heap cell is 8 bytes on a 32 bit machine and 16 bytes on a | |
231 | * 64 bit machine. The units of the _SIZE parameters are bytes. | |
232 | * Cons pairs and object headers occupy one heap cell. | |
233 | * | |
234 | * SCM_INIT_HEAP_SIZE is the initial size of heap. If this much heap is | |
235 | * allocated initially the heap will grow by half its current size | |
236 | * each subsequent time more heap is needed. | |
237 | * | |
238 | * If SCM_INIT_HEAP_SIZE heap cannot be allocated initially, SCM_HEAP_SEG_SIZE | |
239 | * will be used, and the heap will grow by SCM_HEAP_SEG_SIZE when more | |
1be6b49c | 240 | * heap is needed. SCM_HEAP_SEG_SIZE must fit into type size_t. This code |
0f2d19dd | 241 | * is in scm_init_storage() and alloc_some_heap() in sys.c |
a00c95d9 | 242 | * |
0f2d19dd JB |
243 | * If SCM_INIT_HEAP_SIZE can be allocated initially, the heap will grow by |
244 | * SCM_EXPHEAP(scm_heap_size) when more heap is needed. | |
245 | * | |
246 | * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap | |
247 | * is needed. | |
248 | * | |
249 | * INIT_MALLOC_LIMIT is the initial amount of malloc usage which will | |
a00c95d9 | 250 | * trigger a GC. |
6064dcc6 MV |
251 | * |
252 | * SCM_MTRIGGER_HYSTERESIS is the amount of malloc storage that must be | |
253 | * reclaimed by a GC triggered by must_malloc. If less than this is | |
254 | * reclaimed, the trigger threshold is raised. [I don't know what a | |
255 | * good value is. I arbitrarily chose 1/10 of the INIT_MALLOC_LIMIT to | |
a00c95d9 | 256 | * work around a oscillation that caused almost constant GC.] |
0f2d19dd JB |
257 | */ |
258 | ||
8fef55a8 MD |
259 | /* |
260 | * Heap size 45000 and 40% min yield gives quick startup and no extra | |
261 | * heap allocation. Having higher values on min yield may lead to | |
262 | * large heaps, especially if code behaviour is varying its | |
263 | * maximum consumption between different freelists. | |
264 | */ | |
d6884e63 ML |
265 | |
266 | #define SCM_DATA_CELLS2CARDS(n) (((n) + SCM_GC_CARD_N_DATA_CELLS - 1) / SCM_GC_CARD_N_DATA_CELLS) | |
267 | #define SCM_CARDS_PER_CLUSTER SCM_DATA_CELLS2CARDS (2000L) | |
268 | #define SCM_CLUSTER_SIZE_1 (SCM_CARDS_PER_CLUSTER * SCM_GC_CARD_N_DATA_CELLS) | |
1be6b49c | 269 | size_t scm_default_init_heap_size_1 = (((SCM_DATA_CELLS2CARDS (45000L) + SCM_CARDS_PER_CLUSTER - 1) |
d6884e63 | 270 | / SCM_CARDS_PER_CLUSTER) * SCM_GC_CARD_SIZE); |
aeacfc8f | 271 | int scm_default_min_yield_1 = 40; |
4c48ba06 | 272 | |
d6884e63 | 273 | #define SCM_CLUSTER_SIZE_2 (SCM_CARDS_PER_CLUSTER * (SCM_GC_CARD_N_DATA_CELLS / 2)) |
1be6b49c | 274 | size_t scm_default_init_heap_size_2 = (((SCM_DATA_CELLS2CARDS (2500L * 2) + SCM_CARDS_PER_CLUSTER - 1) |
d6884e63 | 275 | / SCM_CARDS_PER_CLUSTER) * SCM_GC_CARD_SIZE); |
4c48ba06 MD |
276 | /* The following value may seem large, but note that if we get to GC at |
277 | * all, this means that we have a numerically intensive application | |
278 | */ | |
aeacfc8f | 279 | int scm_default_min_yield_2 = 40; |
4c48ba06 | 280 | |
1be6b49c | 281 | size_t scm_default_max_segment_size = 2097000L;/* a little less (adm) than 2 Mb */ |
4c48ba06 | 282 | |
d6884e63 | 283 | #define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_CARD_SIZE) |
0f2d19dd JB |
284 | #ifdef _QC |
285 | # define SCM_HEAP_SEG_SIZE 32768L | |
286 | #else | |
287 | # ifdef sequent | |
4c48ba06 | 288 | # define SCM_HEAP_SEG_SIZE (7000L * sizeof (scm_cell)) |
0f2d19dd | 289 | # else |
4c48ba06 | 290 | # define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_cell)) |
0f2d19dd JB |
291 | # endif |
292 | #endif | |
4c48ba06 | 293 | /* Make heap grow with factor 1.5 */ |
4a4c9785 | 294 | #define SCM_EXPHEAP(scm_heap_size) (scm_heap_size / 2) |
0f2d19dd | 295 | #define SCM_INIT_MALLOC_LIMIT 100000 |
6064dcc6 | 296 | #define SCM_MTRIGGER_HYSTERESIS (SCM_INIT_MALLOC_LIMIT/10) |
0f2d19dd | 297 | |
d6884e63 ML |
298 | /* CELL_UP and CELL_DN are used by scm_init_heap_seg to find (scm_cell * span) |
299 | aligned inner bounds for allocated storage */ | |
0f2d19dd JB |
300 | |
301 | #ifdef PROT386 | |
302 | /*in 386 protected mode we must only adjust the offset */ | |
a00c95d9 ML |
303 | # define CELL_UP(p, span) MK_FP(FP_SEG(p), ~(8*(span)-1)&(FP_OFF(p)+8*(span)-1)) |
304 | # define CELL_DN(p, span) MK_FP(FP_SEG(p), ~(8*(span)-1)&FP_OFF(p)) | |
0f2d19dd JB |
305 | #else |
306 | # ifdef _UNICOS | |
c014a02e ML |
307 | # define CELL_UP(p, span) (SCM_CELLPTR)(~(span) & ((long)(p)+(span))) |
308 | # define CELL_DN(p, span) (SCM_CELLPTR)(~(span) & (long)(p)) | |
0f2d19dd | 309 | # else |
c014a02e ML |
310 | # define CELL_UP(p, span) (SCM_CELLPTR)(~(sizeof(scm_cell)*(span)-1L) & ((long)(p)+sizeof(scm_cell)*(span)-1L)) |
311 | # define CELL_DN(p, span) (SCM_CELLPTR)(~(sizeof(scm_cell)*(span)-1L) & (long)(p)) | |
0f2d19dd JB |
312 | # endif /* UNICOS */ |
313 | #endif /* PROT386 */ | |
314 | ||
ecf470a2 ML |
315 | #define DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_cell) - 1) & SCM_UNPACK (x)) == 0) |
316 | ||
d6884e63 ML |
317 | #define ALIGNMENT_SLACK(freelist) (SCM_GC_CARD_SIZE - 1) |
318 | #define CLUSTER_SIZE_IN_BYTES(freelist) \ | |
319 | (((freelist)->cluster_size / (SCM_GC_CARD_N_DATA_CELLS / (freelist)->span)) * SCM_GC_CARD_SIZE) | |
0f2d19dd JB |
320 | |
321 | \f | |
945fec60 | 322 | /* scm_freelists |
0f2d19dd | 323 | */ |
945fec60 | 324 | |
92c2555f | 325 | typedef struct scm_t_freelist { |
a00c95d9 ML |
326 | /* collected cells */ |
327 | SCM cells; | |
a00c95d9 ML |
328 | /* number of cells left to collect before cluster is full */ |
329 | unsigned int left_to_collect; | |
b37fe1c5 MD |
330 | /* number of clusters which have been allocated */ |
331 | unsigned int clusters_allocated; | |
8fef55a8 MD |
332 | /* a list of freelists, each of size cluster_size, |
333 | * except the last one which may be shorter | |
334 | */ | |
a00c95d9 ML |
335 | SCM clusters; |
336 | SCM *clustertail; | |
b37fe1c5 | 337 | /* this is the number of objects in each cluster, including the spine cell */ |
1be6b49c | 338 | unsigned int cluster_size; |
8fef55a8 | 339 | /* indicates that we should grow heap instead of GC:ing |
a00c95d9 ML |
340 | */ |
341 | int grow_heap_p; | |
8fef55a8 | 342 | /* minimum yield on this list in order not to grow the heap |
a00c95d9 | 343 | */ |
8fef55a8 MD |
344 | long min_yield; |
345 | /* defines min_yield as percent of total heap size | |
a00c95d9 | 346 | */ |
8fef55a8 | 347 | int min_yield_fraction; |
a00c95d9 ML |
348 | /* number of cells per object on this list */ |
349 | int span; | |
350 | /* number of collected cells during last GC */ | |
c014a02e | 351 | unsigned long collected; |
1811ebce | 352 | /* number of collected cells during penultimate GC */ |
c014a02e | 353 | unsigned long collected_1; |
a00c95d9 ML |
354 | /* total number of cells in heap segments |
355 | * belonging to this list. | |
356 | */ | |
c014a02e | 357 | unsigned long heap_size; |
92c2555f | 358 | } scm_t_freelist; |
a00c95d9 | 359 | |
4a4c9785 | 360 | SCM scm_freelist = SCM_EOL; |
92c2555f | 361 | scm_t_freelist scm_master_freelist = { |
729dbac3 | 362 | SCM_EOL, 0, 0, SCM_EOL, 0, SCM_CLUSTER_SIZE_1, 0, 0, 0, 1, 0, 0, 0 |
4a4c9785 MD |
363 | }; |
364 | SCM scm_freelist2 = SCM_EOL; | |
92c2555f | 365 | scm_t_freelist scm_master_freelist2 = { |
729dbac3 | 366 | SCM_EOL, 0, 0, SCM_EOL, 0, SCM_CLUSTER_SIZE_2, 0, 0, 0, 2, 0, 0, 0 |
4a4c9785 | 367 | }; |
0f2d19dd JB |
368 | |
369 | /* scm_mtrigger | |
370 | * is the number of bytes of must_malloc allocation needed to trigger gc. | |
371 | */ | |
c014a02e | 372 | unsigned long scm_mtrigger; |
0f2d19dd | 373 | |
0f2d19dd JB |
374 | /* scm_gc_heap_lock |
375 | * If set, don't expand the heap. Set only during gc, during which no allocation | |
376 | * is supposed to take place anyway. | |
377 | */ | |
378 | int scm_gc_heap_lock = 0; | |
379 | ||
380 | /* GC Blocking | |
381 | * Don't pause for collection if this is set -- just | |
382 | * expand the heap. | |
383 | */ | |
0f2d19dd JB |
384 | int scm_block_gc = 1; |
385 | ||
0f2d19dd JB |
386 | /* During collection, this accumulates objects holding |
387 | * weak references. | |
388 | */ | |
ab4bef85 | 389 | SCM scm_weak_vectors; |
0f2d19dd | 390 | |
7445e0e8 MD |
391 | /* During collection, this accumulates structures which are to be freed. |
392 | */ | |
393 | SCM scm_structs_to_free; | |
394 | ||
0f2d19dd JB |
395 | /* GC Statistics Keeping |
396 | */ | |
c014a02e ML |
397 | unsigned long scm_cells_allocated = 0; |
398 | unsigned long scm_mallocated = 0; | |
399 | unsigned long scm_gc_cells_collected; | |
400 | unsigned long scm_gc_yield; | |
401 | static unsigned long scm_gc_yield_1 = 0; /* previous GC yield */ | |
402 | unsigned long scm_gc_malloc_collected; | |
403 | unsigned long scm_gc_ports_collected; | |
0f2d19dd | 404 | unsigned long scm_gc_time_taken = 0; |
c014a02e ML |
405 | static unsigned long t_before_gc; |
406 | static unsigned long t_before_sweep; | |
c9b0d4b0 ML |
407 | unsigned long scm_gc_mark_time_taken = 0; |
408 | unsigned long scm_gc_sweep_time_taken = 0; | |
c014a02e ML |
409 | unsigned long scm_gc_times = 0; |
410 | unsigned long scm_gc_cells_swept = 0; | |
c9b0d4b0 ML |
411 | double scm_gc_cells_marked_acc = 0.; |
412 | double scm_gc_cells_swept_acc = 0.; | |
0f2d19dd JB |
413 | |
414 | SCM_SYMBOL (sym_cells_allocated, "cells-allocated"); | |
415 | SCM_SYMBOL (sym_heap_size, "cell-heap-size"); | |
416 | SCM_SYMBOL (sym_mallocated, "bytes-malloced"); | |
417 | SCM_SYMBOL (sym_mtrigger, "gc-malloc-threshold"); | |
418 | SCM_SYMBOL (sym_heap_segments, "cell-heap-segments"); | |
419 | SCM_SYMBOL (sym_gc_time_taken, "gc-time-taken"); | |
c9b0d4b0 ML |
420 | SCM_SYMBOL (sym_gc_mark_time_taken, "gc-mark-time-taken"); |
421 | SCM_SYMBOL (sym_gc_sweep_time_taken, "gc-sweep-time-taken"); | |
422 | SCM_SYMBOL (sym_times, "gc-times"); | |
423 | SCM_SYMBOL (sym_cells_marked, "cells-marked"); | |
424 | SCM_SYMBOL (sym_cells_swept, "cells-swept"); | |
0f2d19dd | 425 | |
92c2555f | 426 | typedef struct scm_t_heap_seg_data |
0f2d19dd | 427 | { |
cf2d30f6 JB |
428 | /* lower and upper bounds of the segment */ |
429 | SCM_CELLPTR bounds[2]; | |
430 | ||
431 | /* address of the head-of-freelist pointer for this segment's cells. | |
432 | All segments usually point to the same one, scm_freelist. */ | |
92c2555f | 433 | scm_t_freelist *freelist; |
cf2d30f6 | 434 | |
fe517a7d | 435 | /* number of cells per object in this segment */ |
945fec60 | 436 | int span; |
92c2555f | 437 | } scm_t_heap_seg_data; |
0f2d19dd JB |
438 | |
439 | ||
440 | ||
92c2555f | 441 | static size_t init_heap_seg (SCM_CELLPTR, size_t, scm_t_freelist *); |
b6efc951 DH |
442 | |
443 | typedef enum { return_on_error, abort_on_error } policy_on_error; | |
92c2555f | 444 | static void alloc_some_heap (scm_t_freelist *, policy_on_error); |
0f2d19dd JB |
445 | |
446 | ||
d6884e63 ML |
447 | #define SCM_HEAP_SIZE \ |
448 | (scm_master_freelist.heap_size + scm_master_freelist2.heap_size) | |
449 | #define SCM_MAX(A, B) ((A) > (B) ? (A) : (B)) | |
450 | ||
451 | #define BVEC_GROW_SIZE 256 | |
452 | #define BVEC_GROW_SIZE_IN_LIMBS (SCM_GC_CARD_BVEC_SIZE_IN_LIMBS * BVEC_GROW_SIZE) | |
92c2555f | 453 | #define BVEC_GROW_SIZE_IN_BYTES (BVEC_GROW_SIZE_IN_LIMBS * sizeof (scm_t_c_bvec_limb)) |
d6884e63 ML |
454 | |
455 | /* mark space allocation */ | |
456 | ||
92c2555f | 457 | typedef struct scm_t_mark_space |
d6884e63 | 458 | { |
92c2555f MV |
459 | scm_t_c_bvec_limb *bvec_space; |
460 | struct scm_t_mark_space *next; | |
461 | } scm_t_mark_space; | |
d6884e63 | 462 | |
92c2555f MV |
463 | static scm_t_mark_space *current_mark_space; |
464 | static scm_t_mark_space **mark_space_ptr; | |
1be6b49c | 465 | static ptrdiff_t current_mark_space_offset; |
92c2555f | 466 | static scm_t_mark_space *mark_space_head; |
d6884e63 | 467 | |
92c2555f | 468 | static scm_t_c_bvec_limb * |
d6884e63 | 469 | get_bvec () |
db4b4ca6 | 470 | #define FUNC_NAME "get_bvec" |
d6884e63 | 471 | { |
92c2555f | 472 | scm_t_c_bvec_limb *res; |
d6884e63 ML |
473 | |
474 | if (!current_mark_space) | |
475 | { | |
92c2555f | 476 | SCM_SYSCALL (current_mark_space = (scm_t_mark_space *) malloc (sizeof (scm_t_mark_space))); |
d6884e63 | 477 | if (!current_mark_space) |
db4b4ca6 | 478 | SCM_MISC_ERROR ("could not grow heap", SCM_EOL); |
d6884e63 ML |
479 | |
480 | current_mark_space->bvec_space = NULL; | |
481 | current_mark_space->next = NULL; | |
482 | ||
483 | *mark_space_ptr = current_mark_space; | |
484 | mark_space_ptr = &(current_mark_space->next); | |
485 | ||
486 | return get_bvec (); | |
487 | } | |
488 | ||
489 | if (!(current_mark_space->bvec_space)) | |
490 | { | |
491 | SCM_SYSCALL (current_mark_space->bvec_space = | |
92c2555f | 492 | (scm_t_c_bvec_limb *) calloc (BVEC_GROW_SIZE_IN_BYTES, 1)); |
d6884e63 | 493 | if (!(current_mark_space->bvec_space)) |
db4b4ca6 | 494 | SCM_MISC_ERROR ("could not grow heap", SCM_EOL); |
d6884e63 ML |
495 | |
496 | current_mark_space_offset = 0; | |
497 | ||
498 | return get_bvec (); | |
499 | } | |
500 | ||
501 | if (current_mark_space_offset == BVEC_GROW_SIZE_IN_LIMBS) | |
502 | { | |
503 | current_mark_space = NULL; | |
504 | ||
505 | return get_bvec (); | |
506 | } | |
507 | ||
508 | res = current_mark_space->bvec_space + current_mark_space_offset; | |
509 | current_mark_space_offset += SCM_GC_CARD_BVEC_SIZE_IN_LIMBS; | |
510 | ||
511 | return res; | |
512 | } | |
db4b4ca6 DH |
513 | #undef FUNC_NAME |
514 | ||
d6884e63 ML |
515 | |
516 | static void | |
517 | clear_mark_space () | |
518 | { | |
92c2555f | 519 | scm_t_mark_space *ms; |
d6884e63 ML |
520 | |
521 | for (ms = mark_space_head; ms; ms = ms->next) | |
522 | memset (ms->bvec_space, 0, BVEC_GROW_SIZE_IN_BYTES); | |
523 | } | |
524 | ||
525 | ||
0f2d19dd | 526 | \f |
cf2d30f6 JB |
527 | /* Debugging functions. */ |
528 | ||
bb2c57fa | 529 | #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST) |
cf2d30f6 | 530 | |
8ded62a3 | 531 | static void |
92c2555f | 532 | map_free_list (scm_t_freelist *master, SCM freelist) |
8ded62a3 | 533 | { |
c014a02e | 534 | long last_seg = -1, count = 0; |
8ded62a3 | 535 | SCM f; |
a00c95d9 | 536 | |
3f5d82cd | 537 | for (f = freelist; !SCM_NULLP (f); f = SCM_FREE_CELL_CDR (f)) |
8ded62a3 | 538 | { |
592996c9 | 539 | long int this_seg = heap_segment (f); |
8ded62a3 | 540 | |
592996c9 DH |
541 | if (this_seg == -1) |
542 | { | |
543 | fprintf (stderr, | |
544 | "map_free_list: can't find segment containing cell %lux\n", | |
545 | (unsigned long int) SCM_UNPACK (cell)); | |
546 | abort (); | |
547 | } | |
548 | else if (this_seg != last_seg) | |
8ded62a3 MD |
549 | { |
550 | if (last_seg != -1) | |
1be6b49c ML |
551 | fprintf (stderr, " %5ld %d-cells in segment %ld\n", |
552 | (long) count, master->span, (long) last_seg); | |
8ded62a3 MD |
553 | last_seg = this_seg; |
554 | count = 0; | |
555 | } | |
556 | count++; | |
557 | } | |
558 | if (last_seg != -1) | |
1be6b49c ML |
559 | fprintf (stderr, " %5ld %d-cells in segment %ld\n", |
560 | (long) count, master->span, (long) last_seg); | |
8ded62a3 | 561 | } |
cf2d30f6 | 562 | |
a00c95d9 | 563 | SCM_DEFINE (scm_map_free_list, "map-free-list", 0, 0, 0, |
acb0a19c | 564 | (), |
5352393c MG |
565 | "Print debugging information about the free-list.\n" |
566 | "@code{map-free-list} is only included in\n" | |
567 | "@code{--enable-guile-debug} builds of Guile.") | |
acb0a19c MD |
568 | #define FUNC_NAME s_scm_map_free_list |
569 | { | |
592996c9 DH |
570 | size_t i; |
571 | ||
1be6b49c ML |
572 | fprintf (stderr, "%ld segments total (%d:%ld", |
573 | (long) scm_n_heap_segs, | |
4c48ba06 | 574 | scm_heap_table[0].span, |
1be6b49c | 575 | (long) (scm_heap_table[0].bounds[1] - scm_heap_table[0].bounds[0])); |
592996c9 DH |
576 | |
577 | for (i = 1; i != scm_n_heap_segs; i++) | |
1be6b49c | 578 | fprintf (stderr, ", %d:%ld", |
4c48ba06 | 579 | scm_heap_table[i].span, |
1be6b49c | 580 | (long) (scm_heap_table[i].bounds[1] - scm_heap_table[i].bounds[0])); |
4c48ba06 | 581 | fprintf (stderr, ")\n"); |
8ded62a3 MD |
582 | map_free_list (&scm_master_freelist, scm_freelist); |
583 | map_free_list (&scm_master_freelist2, scm_freelist2); | |
cf2d30f6 JB |
584 | fflush (stderr); |
585 | ||
586 | return SCM_UNSPECIFIED; | |
587 | } | |
1bbd0b84 | 588 | #undef FUNC_NAME |
cf2d30f6 | 589 | |
c014a02e ML |
590 | static long last_cluster; |
591 | static long last_size; | |
4c48ba06 | 592 | |
c014a02e ML |
593 | static long |
594 | free_list_length (char *title, long i, SCM freelist) | |
5384bc5b MD |
595 | { |
596 | SCM ls; | |
c014a02e | 597 | long n = 0; |
3f5d82cd DH |
598 | for (ls = freelist; !SCM_NULLP (ls); ls = SCM_FREE_CELL_CDR (ls)) |
599 | if (SCM_FREE_CELL_P (ls)) | |
5384bc5b MD |
600 | ++n; |
601 | else | |
602 | { | |
1be6b49c | 603 | fprintf (stderr, "bad cell in %s at position %ld\n", title, (long) n); |
5384bc5b MD |
604 | abort (); |
605 | } | |
4c48ba06 MD |
606 | if (n != last_size) |
607 | { | |
608 | if (i > 0) | |
609 | { | |
610 | if (last_cluster == i - 1) | |
1be6b49c | 611 | fprintf (stderr, "\t%ld\n", (long) last_size); |
4c48ba06 | 612 | else |
1be6b49c | 613 | fprintf (stderr, "-%ld\t%ld\n", (long) (i - 1), (long) last_size); |
4c48ba06 MD |
614 | } |
615 | if (i >= 0) | |
1be6b49c | 616 | fprintf (stderr, "%s %ld", title, (long) i); |
4c48ba06 | 617 | else |
1be6b49c | 618 | fprintf (stderr, "%s\t%ld\n", title, (long) n); |
4c48ba06 MD |
619 | last_cluster = i; |
620 | last_size = n; | |
621 | } | |
5384bc5b MD |
622 | return n; |
623 | } | |
624 | ||
625 | static void | |
92c2555f | 626 | free_list_lengths (char *title, scm_t_freelist *master, SCM freelist) |
5384bc5b MD |
627 | { |
628 | SCM clusters; | |
c014a02e | 629 | long i = 0, len, n = 0; |
5384bc5b MD |
630 | fprintf (stderr, "%s\n\n", title); |
631 | n += free_list_length ("free list", -1, freelist); | |
632 | for (clusters = master->clusters; | |
633 | SCM_NNULLP (clusters); | |
634 | clusters = SCM_CDR (clusters)) | |
4c48ba06 MD |
635 | { |
636 | len = free_list_length ("cluster", i++, SCM_CAR (clusters)); | |
637 | n += len; | |
638 | } | |
639 | if (last_cluster == i - 1) | |
1be6b49c | 640 | fprintf (stderr, "\t%ld\n", (long) last_size); |
4c48ba06 | 641 | else |
1be6b49c ML |
642 | fprintf (stderr, "-%ld\t%ld\n", (long) (i - 1), (long) last_size); |
643 | fprintf (stderr, "\ntotal %ld objects\n\n", (long) n); | |
5384bc5b MD |
644 | } |
645 | ||
a00c95d9 | 646 | SCM_DEFINE (scm_free_list_length, "free-list-length", 0, 0, 0, |
5384bc5b | 647 | (), |
5352393c MG |
648 | "Print debugging information about the free-list.\n" |
649 | "@code{free-list-length} is only included in\n" | |
650 | "@code{--enable-guile-debug} builds of Guile.") | |
5384bc5b MD |
651 | #define FUNC_NAME s_scm_free_list_length |
652 | { | |
b37fe1c5 MD |
653 | free_list_lengths ("1-cells", &scm_master_freelist, scm_freelist); |
654 | free_list_lengths ("2-cells", &scm_master_freelist2, scm_freelist2); | |
12e5fb3b | 655 | return SCM_UNSPECIFIED; |
5384bc5b MD |
656 | } |
657 | #undef FUNC_NAME | |
658 | ||
bb2c57fa MD |
659 | #endif |
660 | ||
661 | #ifdef GUILE_DEBUG_FREELIST | |
cf2d30f6 | 662 | |
d3dd80ab MG |
663 | /* Non-zero if freelist debugging is in effect. Set this via |
664 | `gc-set-debug-check-freelist!'. */ | |
665 | static int scm_debug_check_freelist = 0; | |
666 | ||
cf2d30f6 | 667 | /* Number of calls to SCM_NEWCELL since startup. */ |
c014a02e ML |
668 | static unsigned long scm_newcell_count; |
669 | static unsigned long scm_newcell2_count; | |
cf2d30f6 JB |
670 | |
671 | /* Search freelist for anything that isn't marked as a free cell. | |
672 | Abort if we find something. */ | |
8ded62a3 MD |
673 | static void |
674 | scm_check_freelist (SCM freelist) | |
675 | { | |
676 | SCM f; | |
c014a02e | 677 | long i = 0; |
8ded62a3 | 678 | |
3f5d82cd DH |
679 | for (f = freelist; !SCM_NULLP (f); f = SCM_FREE_CELL_CDR (f), i++) |
680 | if (!SCM_FREE_CELL_P (f)) | |
8ded62a3 | 681 | { |
1be6b49c ML |
682 | fprintf (stderr, "Bad cell in freelist on newcell %lu: %lu'th elt\n", |
683 | (long) scm_newcell_count, (long) i); | |
8ded62a3 MD |
684 | abort (); |
685 | } | |
686 | } | |
cf2d30f6 | 687 | |
a00c95d9 | 688 | SCM_DEFINE (scm_gc_set_debug_check_freelist_x, "gc-set-debug-check-freelist!", 1, 0, 0, |
1bbd0b84 | 689 | (SCM flag), |
1e6808ea MG |
690 | "If @var{flag} is @code{#t}, check the freelist for consistency\n" |
691 | "on each cell allocation. This procedure only exists when the\n" | |
692 | "@code{GUILE_DEBUG_FREELIST} compile-time flag was selected.") | |
1bbd0b84 | 693 | #define FUNC_NAME s_scm_gc_set_debug_check_freelist_x |
25748c78 | 694 | { |
d6884e63 ML |
695 | /* [cmm] I did a double-take when I read this code the first time. |
696 | well, FWIW. */ | |
945fec60 | 697 | SCM_VALIDATE_BOOL_COPY (1, flag, scm_debug_check_freelist); |
25748c78 GB |
698 | return SCM_UNSPECIFIED; |
699 | } | |
1bbd0b84 | 700 | #undef FUNC_NAME |
25748c78 GB |
701 | |
702 | ||
4a4c9785 MD |
703 | SCM |
704 | scm_debug_newcell (void) | |
705 | { | |
706 | SCM new; | |
707 | ||
708 | scm_newcell_count++; | |
709 | if (scm_debug_check_freelist) | |
710 | { | |
8ded62a3 | 711 | scm_check_freelist (scm_freelist); |
4a4c9785 MD |
712 | scm_gc(); |
713 | } | |
714 | ||
715 | /* The rest of this is supposed to be identical to the SCM_NEWCELL | |
716 | macro. */ | |
3f5d82cd | 717 | if (SCM_NULLP (scm_freelist)) |
7c33806a DH |
718 | { |
719 | new = scm_gc_for_newcell (&scm_master_freelist, &scm_freelist); | |
720 | SCM_GC_SET_ALLOCATED (new); | |
721 | } | |
4a4c9785 MD |
722 | else |
723 | { | |
724 | new = scm_freelist; | |
3f5d82cd | 725 | scm_freelist = SCM_FREE_CELL_CDR (scm_freelist); |
7c33806a | 726 | SCM_GC_SET_ALLOCATED (new); |
4a4c9785 MD |
727 | } |
728 | ||
729 | return new; | |
730 | } | |
731 | ||
732 | SCM | |
733 | scm_debug_newcell2 (void) | |
734 | { | |
735 | SCM new; | |
736 | ||
737 | scm_newcell2_count++; | |
738 | if (scm_debug_check_freelist) | |
739 | { | |
8ded62a3 | 740 | scm_check_freelist (scm_freelist2); |
4a4c9785 MD |
741 | scm_gc (); |
742 | } | |
743 | ||
744 | /* The rest of this is supposed to be identical to the SCM_NEWCELL | |
745 | macro. */ | |
3f5d82cd | 746 | if (SCM_NULLP (scm_freelist2)) |
7c33806a DH |
747 | { |
748 | new = scm_gc_for_newcell (&scm_master_freelist2, &scm_freelist2); | |
749 | SCM_GC_SET_ALLOCATED (new); | |
750 | } | |
4a4c9785 MD |
751 | else |
752 | { | |
753 | new = scm_freelist2; | |
3f5d82cd | 754 | scm_freelist2 = SCM_FREE_CELL_CDR (scm_freelist2); |
7c33806a | 755 | SCM_GC_SET_ALLOCATED (new); |
4a4c9785 MD |
756 | } |
757 | ||
758 | return new; | |
759 | } | |
760 | ||
fca7547b | 761 | #endif /* GUILE_DEBUG_FREELIST */ |
cf2d30f6 JB |
762 | |
763 | \f | |
0f2d19dd | 764 | |
c014a02e | 765 | static unsigned long |
92c2555f | 766 | master_cells_allocated (scm_t_freelist *master) |
b37fe1c5 | 767 | { |
d6884e63 | 768 | /* the '- 1' below is to ignore the cluster spine cells. */ |
c014a02e | 769 | long objects = master->clusters_allocated * (master->cluster_size - 1); |
b37fe1c5 MD |
770 | if (SCM_NULLP (master->clusters)) |
771 | objects -= master->left_to_collect; | |
772 | return master->span * objects; | |
773 | } | |
774 | ||
c014a02e | 775 | static unsigned long |
b37fe1c5 MD |
776 | freelist_length (SCM freelist) |
777 | { | |
c014a02e | 778 | long n; |
3f5d82cd | 779 | for (n = 0; !SCM_NULLP (freelist); freelist = SCM_FREE_CELL_CDR (freelist)) |
b37fe1c5 MD |
780 | ++n; |
781 | return n; | |
782 | } | |
783 | ||
c014a02e | 784 | static unsigned long |
b37fe1c5 MD |
785 | compute_cells_allocated () |
786 | { | |
787 | return (scm_cells_allocated | |
788 | + master_cells_allocated (&scm_master_freelist) | |
789 | + master_cells_allocated (&scm_master_freelist2) | |
790 | - scm_master_freelist.span * freelist_length (scm_freelist) | |
791 | - scm_master_freelist2.span * freelist_length (scm_freelist2)); | |
792 | } | |
b37fe1c5 | 793 | |
0f2d19dd JB |
794 | /* {Scheme Interface to GC} |
795 | */ | |
796 | ||
a00c95d9 | 797 | SCM_DEFINE (scm_gc_stats, "gc-stats", 0, 0, 0, |
1bbd0b84 | 798 | (), |
1e6808ea MG |
799 | "Return an association list of statistics about Guile's current\n" |
800 | "use of storage.") | |
1bbd0b84 | 801 | #define FUNC_NAME s_scm_gc_stats |
0f2d19dd | 802 | { |
c014a02e ML |
803 | long i; |
804 | long n; | |
0f2d19dd | 805 | SCM heap_segs; |
c014a02e ML |
806 | unsigned long int local_scm_mtrigger; |
807 | unsigned long int local_scm_mallocated; | |
808 | unsigned long int local_scm_heap_size; | |
809 | unsigned long int local_scm_cells_allocated; | |
810 | unsigned long int local_scm_gc_time_taken; | |
811 | unsigned long int local_scm_gc_times; | |
812 | unsigned long int local_scm_gc_mark_time_taken; | |
813 | unsigned long int local_scm_gc_sweep_time_taken; | |
c9b0d4b0 ML |
814 | double local_scm_gc_cells_swept; |
815 | double local_scm_gc_cells_marked; | |
0f2d19dd JB |
816 | SCM answer; |
817 | ||
818 | SCM_DEFER_INTS; | |
939794ce DH |
819 | |
820 | ++scm_block_gc; | |
821 | ||
0f2d19dd JB |
822 | retry: |
823 | heap_segs = SCM_EOL; | |
824 | n = scm_n_heap_segs; | |
825 | for (i = scm_n_heap_segs; i--; ) | |
c014a02e ML |
826 | heap_segs = scm_cons (scm_cons (scm_ulong2num ((unsigned long)scm_heap_table[i].bounds[1]), |
827 | scm_ulong2num ((unsigned long)scm_heap_table[i].bounds[0])), | |
0f2d19dd JB |
828 | heap_segs); |
829 | if (scm_n_heap_segs != n) | |
830 | goto retry; | |
939794ce DH |
831 | |
832 | --scm_block_gc; | |
0f2d19dd | 833 | |
7febb4a2 MD |
834 | /* Below, we cons to produce the resulting list. We want a snapshot of |
835 | * the heap situation before consing. | |
836 | */ | |
0f2d19dd JB |
837 | local_scm_mtrigger = scm_mtrigger; |
838 | local_scm_mallocated = scm_mallocated; | |
b37fe1c5 | 839 | local_scm_heap_size = SCM_HEAP_SIZE; |
b37fe1c5 | 840 | local_scm_cells_allocated = compute_cells_allocated (); |
0f2d19dd | 841 | local_scm_gc_time_taken = scm_gc_time_taken; |
c9b0d4b0 ML |
842 | local_scm_gc_mark_time_taken = scm_gc_mark_time_taken; |
843 | local_scm_gc_sweep_time_taken = scm_gc_sweep_time_taken; | |
844 | local_scm_gc_times = scm_gc_times; | |
845 | local_scm_gc_cells_swept = scm_gc_cells_swept_acc; | |
846 | local_scm_gc_cells_marked = scm_gc_cells_marked_acc; | |
0f2d19dd | 847 | |
1afff620 KN |
848 | answer = scm_list_n (scm_cons (sym_gc_time_taken, scm_ulong2num (local_scm_gc_time_taken)), |
849 | scm_cons (sym_cells_allocated, scm_ulong2num (local_scm_cells_allocated)), | |
850 | scm_cons (sym_heap_size, scm_ulong2num (local_scm_heap_size)), | |
851 | scm_cons (sym_mallocated, scm_ulong2num (local_scm_mallocated)), | |
852 | scm_cons (sym_mtrigger, scm_ulong2num (local_scm_mtrigger)), | |
853 | scm_cons (sym_times, scm_ulong2num (local_scm_gc_times)), | |
854 | scm_cons (sym_gc_mark_time_taken, scm_ulong2num (local_scm_gc_mark_time_taken)), | |
855 | scm_cons (sym_gc_sweep_time_taken, scm_ulong2num (local_scm_gc_sweep_time_taken)), | |
856 | scm_cons (sym_cells_marked, scm_i_dbl2big (local_scm_gc_cells_marked)), | |
857 | scm_cons (sym_cells_swept, scm_i_dbl2big (local_scm_gc_cells_swept)), | |
858 | scm_cons (sym_heap_segments, heap_segs), | |
859 | SCM_UNDEFINED); | |
0f2d19dd JB |
860 | SCM_ALLOW_INTS; |
861 | return answer; | |
862 | } | |
1bbd0b84 | 863 | #undef FUNC_NAME |
0f2d19dd JB |
864 | |
865 | ||
c9b0d4b0 | 866 | static void |
e81d98ec | 867 | gc_start_stats (const char *what SCM_UNUSED) |
0f2d19dd | 868 | { |
c9b0d4b0 ML |
869 | t_before_gc = scm_c_get_internal_run_time (); |
870 | scm_gc_cells_swept = 0; | |
b37fe1c5 | 871 | scm_gc_cells_collected = 0; |
37ddcaf6 | 872 | scm_gc_yield_1 = scm_gc_yield; |
8b0d194f MD |
873 | scm_gc_yield = (scm_cells_allocated |
874 | + master_cells_allocated (&scm_master_freelist) | |
875 | + master_cells_allocated (&scm_master_freelist2)); | |
0f2d19dd JB |
876 | scm_gc_malloc_collected = 0; |
877 | scm_gc_ports_collected = 0; | |
878 | } | |
879 | ||
939794ce | 880 | |
c9b0d4b0 ML |
881 | static void |
882 | gc_end_stats () | |
0f2d19dd | 883 | { |
c9b0d4b0 ML |
884 | unsigned long t = scm_c_get_internal_run_time (); |
885 | scm_gc_time_taken += (t - t_before_gc); | |
886 | scm_gc_sweep_time_taken += (t - t_before_sweep); | |
887 | ++scm_gc_times; | |
888 | ||
889 | scm_gc_cells_marked_acc += scm_gc_cells_swept - scm_gc_cells_collected; | |
890 | scm_gc_cells_swept_acc += scm_gc_cells_swept; | |
0f2d19dd JB |
891 | } |
892 | ||
893 | ||
a00c95d9 | 894 | SCM_DEFINE (scm_object_address, "object-address", 1, 0, 0, |
1bbd0b84 | 895 | (SCM obj), |
b380b885 MD |
896 | "Return an integer that for the lifetime of @var{obj} is uniquely\n" |
897 | "returned by this function for @var{obj}") | |
1bbd0b84 | 898 | #define FUNC_NAME s_scm_object_address |
0f2d19dd | 899 | { |
c014a02e | 900 | return scm_ulong2num ((unsigned long) SCM_UNPACK (obj)); |
0f2d19dd | 901 | } |
1bbd0b84 | 902 | #undef FUNC_NAME |
0f2d19dd JB |
903 | |
904 | ||
a00c95d9 | 905 | SCM_DEFINE (scm_gc, "gc", 0, 0, 0, |
1bbd0b84 | 906 | (), |
b380b885 MD |
907 | "Scans all of SCM objects and reclaims for further use those that are\n" |
908 | "no longer accessible.") | |
1bbd0b84 | 909 | #define FUNC_NAME s_scm_gc |
0f2d19dd JB |
910 | { |
911 | SCM_DEFER_INTS; | |
912 | scm_igc ("call"); | |
913 | SCM_ALLOW_INTS; | |
914 | return SCM_UNSPECIFIED; | |
915 | } | |
1bbd0b84 | 916 | #undef FUNC_NAME |
0f2d19dd JB |
917 | |
918 | ||
919 | \f | |
920 | /* {C Interface For When GC is Triggered} | |
921 | */ | |
922 | ||
b37fe1c5 | 923 | static void |
92c2555f | 924 | adjust_min_yield (scm_t_freelist *freelist) |
b37fe1c5 | 925 | { |
8fef55a8 | 926 | /* min yield is adjusted upwards so that next predicted total yield |
bda1446c | 927 | * (allocated cells actually freed by GC) becomes |
8fef55a8 MD |
928 | * `min_yield_fraction' of total heap size. Note, however, that |
929 | * the absolute value of min_yield will correspond to `collected' | |
bda1446c | 930 | * on one master (the one which currently is triggering GC). |
b37fe1c5 | 931 | * |
bda1446c MD |
932 | * The reason why we look at total yield instead of cells collected |
933 | * on one list is that we want to take other freelists into account. | |
934 | * On this freelist, we know that (local) yield = collected cells, | |
935 | * but that's probably not the case on the other lists. | |
b37fe1c5 MD |
936 | * |
937 | * (We might consider computing a better prediction, for example | |
938 | * by computing an average over multiple GC:s.) | |
939 | */ | |
8fef55a8 | 940 | if (freelist->min_yield_fraction) |
b37fe1c5 | 941 | { |
37ddcaf6 | 942 | /* Pick largest of last two yields. */ |
1be6b49c | 943 | long delta = ((SCM_HEAP_SIZE * freelist->min_yield_fraction / 100) |
8fef55a8 | 944 | - (long) SCM_MAX (scm_gc_yield_1, scm_gc_yield)); |
b37fe1c5 | 945 | #ifdef DEBUGINFO |
1be6b49c ML |
946 | fprintf (stderr, " after GC = %lu, delta = %ld\n", |
947 | (long) scm_cells_allocated, | |
948 | (long) delta); | |
b37fe1c5 MD |
949 | #endif |
950 | if (delta > 0) | |
8fef55a8 | 951 | freelist->min_yield += delta; |
b37fe1c5 MD |
952 | } |
953 | } | |
954 | ||
b6efc951 | 955 | |
4a4c9785 | 956 | /* When we get POSIX threads support, the master will be global and |
4c48ba06 MD |
957 | * common while the freelist will be individual for each thread. |
958 | */ | |
4a4c9785 MD |
959 | |
960 | SCM | |
92c2555f | 961 | scm_gc_for_newcell (scm_t_freelist *master, SCM *freelist) |
4a4c9785 MD |
962 | { |
963 | SCM cell; | |
964 | ++scm_ints_disabled; | |
4c48ba06 MD |
965 | do |
966 | { | |
c7387918 | 967 | if (SCM_NULLP (master->clusters)) |
4c48ba06 | 968 | { |
150c200b | 969 | if (master->grow_heap_p || scm_block_gc) |
4c48ba06 | 970 | { |
b6efc951 DH |
971 | /* In order to reduce gc frequency, try to allocate a new heap |
972 | * segment first, even if gc might find some free cells. If we | |
973 | * can't obtain a new heap segment, we will try gc later. | |
974 | */ | |
4c48ba06 | 975 | master->grow_heap_p = 0; |
b6efc951 | 976 | alloc_some_heap (master, return_on_error); |
4c48ba06 | 977 | } |
b6efc951 | 978 | if (SCM_NULLP (master->clusters)) |
b37fe1c5 | 979 | { |
b6efc951 DH |
980 | /* The heap was not grown, either because it wasn't scheduled to |
981 | * grow, or because there was not enough memory available. In | |
982 | * both cases we have to try gc to get some free cells. | |
983 | */ | |
37ddcaf6 | 984 | #ifdef DEBUGINFO |
1be6b49c ML |
985 | fprintf (stderr, "allocated = %lu, ", |
986 | (long) (scm_cells_allocated | |
37ddcaf6 | 987 | + master_cells_allocated (&scm_master_freelist) |
c014a02e | 988 | + master_cells_allocated (&scm_master_freelist2))); |
37ddcaf6 | 989 | #endif |
b37fe1c5 | 990 | scm_igc ("cells"); |
8fef55a8 | 991 | adjust_min_yield (master); |
c7387918 DH |
992 | if (SCM_NULLP (master->clusters)) |
993 | { | |
b6efc951 DH |
994 | /* gc could not free any cells. Now, we _must_ allocate a |
995 | * new heap segment, because there is no other possibility | |
996 | * to provide a new cell for the caller. | |
997 | */ | |
998 | alloc_some_heap (master, abort_on_error); | |
c7387918 | 999 | } |
b37fe1c5 | 1000 | } |
4c48ba06 MD |
1001 | } |
1002 | cell = SCM_CAR (master->clusters); | |
1003 | master->clusters = SCM_CDR (master->clusters); | |
b37fe1c5 | 1004 | ++master->clusters_allocated; |
4c48ba06 MD |
1005 | } |
1006 | while (SCM_NULLP (cell)); | |
d6884e63 ML |
1007 | |
1008 | #ifdef GUILE_DEBUG_FREELIST | |
1009 | scm_check_freelist (cell); | |
1010 | #endif | |
1011 | ||
4a4c9785 | 1012 | --scm_ints_disabled; |
3f5d82cd | 1013 | *freelist = SCM_FREE_CELL_CDR (cell); |
4a4c9785 MD |
1014 | return cell; |
1015 | } | |
1016 | ||
b6efc951 | 1017 | |
4c48ba06 MD |
1018 | #if 0 |
1019 | /* This is a support routine which can be used to reserve a cluster | |
1020 | * for some special use, such as debugging. It won't be useful until | |
1021 | * free cells are preserved between garbage collections. | |
1022 | */ | |
1023 | ||
1024 | void | |
92c2555f | 1025 | scm_alloc_cluster (scm_t_freelist *master) |
4c48ba06 MD |
1026 | { |
1027 | SCM freelist, cell; | |
1028 | cell = scm_gc_for_newcell (master, &freelist); | |
1029 | SCM_SETCDR (cell, freelist); | |
1030 | return cell; | |
1031 | } | |
1032 | #endif | |
1033 | ||
801cb5e7 | 1034 | |
92c2555f MV |
1035 | scm_t_c_hook scm_before_gc_c_hook; |
1036 | scm_t_c_hook scm_before_mark_c_hook; | |
1037 | scm_t_c_hook scm_before_sweep_c_hook; | |
1038 | scm_t_c_hook scm_after_sweep_c_hook; | |
1039 | scm_t_c_hook scm_after_gc_c_hook; | |
801cb5e7 | 1040 | |
b6efc951 | 1041 | |
0f2d19dd | 1042 | void |
1bbd0b84 | 1043 | scm_igc (const char *what) |
0f2d19dd | 1044 | { |
c014a02e | 1045 | long j; |
0f2d19dd | 1046 | |
406c7d90 | 1047 | ++scm_gc_running_p; |
801cb5e7 | 1048 | scm_c_hook_run (&scm_before_gc_c_hook, 0); |
4c48ba06 MD |
1049 | #ifdef DEBUGINFO |
1050 | fprintf (stderr, | |
1051 | SCM_NULLP (scm_freelist) | |
1052 | ? "*" | |
1053 | : (SCM_NULLP (scm_freelist2) ? "o" : "m")); | |
1054 | #endif | |
42db06f0 | 1055 | /* During the critical section, only the current thread may run. */ |
216eedfc | 1056 | SCM_CRITICAL_SECTION_START; |
42db06f0 | 1057 | |
e242dfd2 | 1058 | /* fprintf (stderr, "gc: %s\n", what); */ |
c68296f8 | 1059 | |
ab4bef85 JB |
1060 | if (!scm_stack_base || scm_block_gc) |
1061 | { | |
406c7d90 | 1062 | --scm_gc_running_p; |
ab4bef85 JB |
1063 | return; |
1064 | } | |
1065 | ||
c9b0d4b0 ML |
1066 | gc_start_stats (what); |
1067 | ||
ab4bef85 JB |
1068 | if (scm_gc_heap_lock) |
1069 | /* We've invoked the collector while a GC is already in progress. | |
1070 | That should never happen. */ | |
1071 | abort (); | |
0f2d19dd JB |
1072 | |
1073 | ++scm_gc_heap_lock; | |
ab4bef85 | 1074 | |
0f2d19dd JB |
1075 | /* flush dead entries from the continuation stack */ |
1076 | { | |
c014a02e ML |
1077 | long x; |
1078 | long bound; | |
0f2d19dd JB |
1079 | SCM * elts; |
1080 | elts = SCM_VELTS (scm_continuation_stack); | |
b5c2579a | 1081 | bound = SCM_VECTOR_LENGTH (scm_continuation_stack); |
0f2d19dd JB |
1082 | x = SCM_INUM (scm_continuation_stack_ptr); |
1083 | while (x < bound) | |
1084 | { | |
1085 | elts[x] = SCM_BOOL_F; | |
1086 | ++x; | |
1087 | } | |
1088 | } | |
1089 | ||
801cb5e7 MD |
1090 | scm_c_hook_run (&scm_before_mark_c_hook, 0); |
1091 | ||
d6884e63 ML |
1092 | clear_mark_space (); |
1093 | ||
42db06f0 | 1094 | #ifndef USE_THREADS |
a00c95d9 | 1095 | |
1b9be268 | 1096 | /* Mark objects on the C stack. */ |
0f2d19dd JB |
1097 | SCM_FLUSH_REGISTER_WINDOWS; |
1098 | /* This assumes that all registers are saved into the jmp_buf */ | |
1099 | setjmp (scm_save_regs_gc_mark); | |
1100 | scm_mark_locations ((SCM_STACKITEM *) scm_save_regs_gc_mark, | |
1be6b49c | 1101 | ( (size_t) (sizeof (SCM_STACKITEM) - 1 + |
ce4a361d JB |
1102 | sizeof scm_save_regs_gc_mark) |
1103 | / sizeof (SCM_STACKITEM))); | |
0f2d19dd JB |
1104 | |
1105 | { | |
6b1b030e | 1106 | unsigned long stack_len = scm_stack_size (scm_stack_base); |
0f2d19dd | 1107 | #ifdef SCM_STACK_GROWS_UP |
6ba93e5e | 1108 | scm_mark_locations (scm_stack_base, stack_len); |
0f2d19dd | 1109 | #else |
6ba93e5e | 1110 | scm_mark_locations (scm_stack_base - stack_len, stack_len); |
0f2d19dd JB |
1111 | #endif |
1112 | } | |
1113 | ||
42db06f0 MD |
1114 | #else /* USE_THREADS */ |
1115 | ||
1116 | /* Mark every thread's stack and registers */ | |
945fec60 | 1117 | scm_threads_mark_stacks (); |
42db06f0 MD |
1118 | |
1119 | #endif /* USE_THREADS */ | |
0f2d19dd | 1120 | |
0f2d19dd JB |
1121 | j = SCM_NUM_PROTECTS; |
1122 | while (j--) | |
1123 | scm_gc_mark (scm_sys_protects[j]); | |
1124 | ||
6b1b030e ML |
1125 | /* mark the registered roots */ |
1126 | { | |
592996c9 | 1127 | size_t i; |
6b1b030e ML |
1128 | for (i = 0; i < SCM_VECTOR_LENGTH (scm_gc_registered_roots); ++i) { |
1129 | SCM l = SCM_VELTS (scm_gc_registered_roots)[i]; | |
592996c9 | 1130 | for (; !SCM_NULLP (l); l = SCM_CDR (l)) { |
6b1b030e ML |
1131 | SCM *p = (SCM *) (scm_num2long (SCM_CAAR (l), 0, NULL)); |
1132 | scm_gc_mark (*p); | |
1133 | } | |
1134 | } | |
1135 | } | |
1136 | ||
9de33deb MD |
1137 | /* FIXME: we should have a means to register C functions to be run |
1138 | * in different phases of GC | |
a00c95d9 | 1139 | */ |
9de33deb | 1140 | scm_mark_subr_table (); |
a00c95d9 | 1141 | |
42db06f0 MD |
1142 | #ifndef USE_THREADS |
1143 | scm_gc_mark (scm_root->handle); | |
1144 | #endif | |
a00c95d9 | 1145 | |
c9b0d4b0 ML |
1146 | t_before_sweep = scm_c_get_internal_run_time (); |
1147 | scm_gc_mark_time_taken += (t_before_sweep - t_before_gc); | |
1148 | ||
801cb5e7 | 1149 | scm_c_hook_run (&scm_before_sweep_c_hook, 0); |
0493cd89 | 1150 | |
0f2d19dd JB |
1151 | scm_gc_sweep (); |
1152 | ||
801cb5e7 MD |
1153 | scm_c_hook_run (&scm_after_sweep_c_hook, 0); |
1154 | ||
0f2d19dd | 1155 | --scm_gc_heap_lock; |
c9b0d4b0 | 1156 | gc_end_stats (); |
42db06f0 | 1157 | |
216eedfc | 1158 | SCM_CRITICAL_SECTION_END; |
801cb5e7 | 1159 | scm_c_hook_run (&scm_after_gc_c_hook, 0); |
406c7d90 | 1160 | --scm_gc_running_p; |
0f2d19dd JB |
1161 | } |
1162 | ||
1163 | \f | |
939794ce | 1164 | |
a00c95d9 | 1165 | /* {Mark/Sweep} |
0f2d19dd JB |
1166 | */ |
1167 | ||
56495472 ML |
1168 | #define MARK scm_gc_mark |
1169 | #define FNAME "scm_gc_mark" | |
0f2d19dd | 1170 | |
56495472 | 1171 | #endif /*!MARK_DEPENDENCIES*/ |
0f2d19dd JB |
1172 | |
1173 | /* Mark an object precisely. | |
1174 | */ | |
a00c95d9 | 1175 | void |
56495472 ML |
1176 | MARK (SCM p) |
1177 | #define FUNC_NAME FNAME | |
0f2d19dd | 1178 | { |
c014a02e | 1179 | register long i; |
0f2d19dd | 1180 | register SCM ptr; |
92c2555f | 1181 | scm_t_bits cell_type; |
0f2d19dd | 1182 | |
56495472 ML |
1183 | #ifndef MARK_DEPENDENCIES |
1184 | # define RECURSE scm_gc_mark | |
1185 | #else | |
1186 | /* go through the usual marking, but not for self-cycles. */ | |
1187 | # define RECURSE(x) do { if ((x) != p) scm_gc_mark (x); } while (0) | |
1188 | #endif | |
0f2d19dd JB |
1189 | ptr = p; |
1190 | ||
56495472 ML |
1191 | #ifdef MARK_DEPENDENCIES |
1192 | goto gc_mark_loop_first_time; | |
1193 | #endif | |
1194 | ||
86d31dfe MV |
1195 | /* A simple hack for debugging. Chose the second branch to get a |
1196 | meaningful backtrace for crashes inside the GC. | |
1197 | */ | |
1198 | #if 1 | |
1199 | #define goto_gc_mark_loop goto gc_mark_loop | |
1200 | #define goto_gc_mark_nimp goto gc_mark_nimp | |
1201 | #else | |
1202 | #define goto_gc_mark_loop RECURSE(ptr); return | |
1203 | #define goto_gc_mark_nimp RECURSE(ptr); return | |
1204 | #endif | |
1205 | ||
0f2d19dd JB |
1206 | gc_mark_loop: |
1207 | if (SCM_IMP (ptr)) | |
1208 | return; | |
1209 | ||
1210 | gc_mark_nimp: | |
56495472 ML |
1211 | |
1212 | #ifdef MARK_DEPENDENCIES | |
0209177b | 1213 | if (SCM_EQ_P (ptr, p)) |
56495472 ML |
1214 | return; |
1215 | ||
1216 | scm_gc_mark (ptr); | |
0209177b | 1217 | return; |
56495472 ML |
1218 | |
1219 | gc_mark_loop_first_time: | |
1220 | #endif | |
9a6976cd | 1221 | |
61045190 | 1222 | #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST)) |
9a6976cd | 1223 | /* We are in debug mode. Check the ptr exhaustively. */ |
61045190 | 1224 | if (!scm_cellp (ptr)) |
db4b4ca6 | 1225 | SCM_MISC_ERROR ("rogue pointer in heap", SCM_EOL); |
9a6976cd DH |
1226 | #else |
1227 | /* In non-debug mode, do at least some cheap testing. */ | |
1228 | if (!SCM_CELLP (ptr)) | |
1229 | SCM_MISC_ERROR ("rogue pointer in heap", SCM_EOL); | |
d6884e63 ML |
1230 | #endif |
1231 | ||
56495472 ML |
1232 | #ifndef MARK_DEPENDENCIES |
1233 | ||
d6884e63 ML |
1234 | if (SCM_GCMARKP (ptr)) |
1235 | return; | |
56495472 | 1236 | |
d6884e63 ML |
1237 | SCM_SETGCMARK (ptr); |
1238 | ||
56495472 ML |
1239 | #endif |
1240 | ||
61045190 DH |
1241 | cell_type = SCM_GC_CELL_TYPE (ptr); |
1242 | switch (SCM_ITAG7 (cell_type)) | |
0f2d19dd JB |
1243 | { |
1244 | case scm_tcs_cons_nimcar: | |
d6884e63 | 1245 | if (SCM_IMP (SCM_CDR (ptr))) |
0f2d19dd JB |
1246 | { |
1247 | ptr = SCM_CAR (ptr); | |
86d31dfe | 1248 | goto_gc_mark_nimp; |
0f2d19dd | 1249 | } |
56495472 | 1250 | RECURSE (SCM_CAR (ptr)); |
d6884e63 | 1251 | ptr = SCM_CDR (ptr); |
86d31dfe | 1252 | goto_gc_mark_nimp; |
0f2d19dd | 1253 | case scm_tcs_cons_imcar: |
d6884e63 | 1254 | ptr = SCM_CDR (ptr); |
86d31dfe | 1255 | goto_gc_mark_loop; |
e641afaf | 1256 | case scm_tc7_pws: |
22a52da1 DH |
1257 | RECURSE (SCM_SETTER (ptr)); |
1258 | ptr = SCM_PROCEDURE (ptr); | |
86d31dfe | 1259 | goto_gc_mark_loop; |
904a077d | 1260 | case scm_tcs_struct: |
0f2d19dd | 1261 | { |
904a077d MV |
1262 | /* XXX - use less explicit code. */ |
1263 | scm_t_bits word0 = SCM_CELL_WORD_0 (ptr) - scm_tc3_struct; | |
1264 | scm_t_bits * vtable_data = (scm_t_bits *) word0; | |
1265 | SCM layout = SCM_PACK (vtable_data [scm_vtable_index_layout]); | |
1266 | long len = SCM_SYMBOL_LENGTH (layout); | |
1267 | char * fields_desc = SCM_SYMBOL_CHARS (layout); | |
1268 | scm_t_bits * struct_data = (scm_t_bits *) SCM_STRUCT_DATA (ptr); | |
1269 | ||
1270 | if (vtable_data[scm_struct_i_flags] & SCM_STRUCTF_ENTITY) | |
0f2d19dd | 1271 | { |
904a077d MV |
1272 | RECURSE (SCM_PACK (struct_data[scm_struct_i_procedure])); |
1273 | RECURSE (SCM_PACK (struct_data[scm_struct_i_setter])); | |
0f2d19dd | 1274 | } |
904a077d MV |
1275 | if (len) |
1276 | { | |
1277 | long x; | |
1278 | ||
1279 | for (x = 0; x < len - 2; x += 2, ++struct_data) | |
1280 | if (fields_desc[x] == 'p') | |
1281 | RECURSE (SCM_PACK (*struct_data)); | |
1282 | if (fields_desc[x] == 'p') | |
1283 | { | |
1284 | if (SCM_LAYOUT_TAILP (fields_desc[x + 1])) | |
1285 | for (x = *struct_data++; x; --x, ++struct_data) | |
1286 | RECURSE (SCM_PACK (*struct_data)); | |
1287 | else | |
1288 | RECURSE (SCM_PACK (*struct_data)); | |
1289 | } | |
1290 | } | |
1291 | /* mark vtable */ | |
1292 | ptr = SCM_PACK (vtable_data [scm_vtable_index_vtable]); | |
1293 | goto_gc_mark_loop; | |
0f2d19dd JB |
1294 | } |
1295 | break; | |
1296 | case scm_tcs_closures: | |
22a52da1 | 1297 | if (SCM_IMP (SCM_ENV (ptr))) |
0f2d19dd JB |
1298 | { |
1299 | ptr = SCM_CLOSCAR (ptr); | |
86d31dfe | 1300 | goto_gc_mark_nimp; |
0f2d19dd | 1301 | } |
56495472 | 1302 | RECURSE (SCM_CLOSCAR (ptr)); |
22a52da1 | 1303 | ptr = SCM_ENV (ptr); |
86d31dfe | 1304 | goto_gc_mark_nimp; |
0f2d19dd | 1305 | case scm_tc7_vector: |
b5c2579a DH |
1306 | i = SCM_VECTOR_LENGTH (ptr); |
1307 | if (i == 0) | |
1308 | break; | |
1309 | while (--i > 0) | |
1310 | if (SCM_NIMP (SCM_VELTS (ptr)[i])) | |
56495472 | 1311 | RECURSE (SCM_VELTS (ptr)[i]); |
b5c2579a | 1312 | ptr = SCM_VELTS (ptr)[0]; |
86d31dfe | 1313 | goto_gc_mark_loop; |
0f2d19dd JB |
1314 | #ifdef CCLO |
1315 | case scm_tc7_cclo: | |
362306b9 | 1316 | { |
1be6b49c ML |
1317 | size_t i = SCM_CCLO_LENGTH (ptr); |
1318 | size_t j; | |
362306b9 DH |
1319 | for (j = 1; j != i; ++j) |
1320 | { | |
1321 | SCM obj = SCM_CCLO_REF (ptr, j); | |
1322 | if (!SCM_IMP (obj)) | |
56495472 | 1323 | RECURSE (obj); |
362306b9 DH |
1324 | } |
1325 | ptr = SCM_CCLO_REF (ptr, 0); | |
86d31dfe | 1326 | goto_gc_mark_loop; |
362306b9 | 1327 | } |
b5c2579a | 1328 | #endif |
afe5177e | 1329 | #ifdef HAVE_ARRAYS |
0f2d19dd JB |
1330 | case scm_tc7_bvect: |
1331 | case scm_tc7_byvect: | |
1332 | case scm_tc7_ivect: | |
1333 | case scm_tc7_uvect: | |
1334 | case scm_tc7_fvect: | |
1335 | case scm_tc7_dvect: | |
1336 | case scm_tc7_cvect: | |
1337 | case scm_tc7_svect: | |
5c11cc9d | 1338 | #ifdef HAVE_LONG_LONGS |
0f2d19dd JB |
1339 | case scm_tc7_llvect: |
1340 | #endif | |
afe5177e | 1341 | #endif |
0f2d19dd | 1342 | case scm_tc7_string: |
0f2d19dd JB |
1343 | break; |
1344 | ||
1345 | case scm_tc7_substring: | |
0f2d19dd | 1346 | ptr = SCM_CDR (ptr); |
86d31dfe | 1347 | goto_gc_mark_loop; |
0f2d19dd JB |
1348 | |
1349 | case scm_tc7_wvect: | |
592996c9 | 1350 | SCM_SET_WVECT_GC_CHAIN (ptr, scm_weak_vectors); |
ab4bef85 | 1351 | scm_weak_vectors = ptr; |
0f2d19dd JB |
1352 | if (SCM_IS_WHVEC_ANY (ptr)) |
1353 | { | |
c014a02e ML |
1354 | long x; |
1355 | long len; | |
0f2d19dd JB |
1356 | int weak_keys; |
1357 | int weak_values; | |
1358 | ||
b5c2579a | 1359 | len = SCM_VECTOR_LENGTH (ptr); |
0f2d19dd JB |
1360 | weak_keys = SCM_IS_WHVEC (ptr) || SCM_IS_WHVEC_B (ptr); |
1361 | weak_values = SCM_IS_WHVEC_V (ptr) || SCM_IS_WHVEC_B (ptr); | |
a00c95d9 | 1362 | |
0f2d19dd JB |
1363 | for (x = 0; x < len; ++x) |
1364 | { | |
1365 | SCM alist; | |
1366 | alist = SCM_VELTS (ptr)[x]; | |
46408039 JB |
1367 | |
1368 | /* mark everything on the alist except the keys or | |
1369 | * values, according to weak_values and weak_keys. */ | |
0b5f3f34 | 1370 | while ( SCM_CONSP (alist) |
0f2d19dd | 1371 | && !SCM_GCMARKP (alist) |
0f2d19dd JB |
1372 | && SCM_CONSP (SCM_CAR (alist))) |
1373 | { | |
1374 | SCM kvpair; | |
1375 | SCM next_alist; | |
1376 | ||
1377 | kvpair = SCM_CAR (alist); | |
1378 | next_alist = SCM_CDR (alist); | |
a00c95d9 | 1379 | /* |
0f2d19dd JB |
1380 | * Do not do this: |
1381 | * SCM_SETGCMARK (alist); | |
1382 | * SCM_SETGCMARK (kvpair); | |
1383 | * | |
1384 | * It may be that either the key or value is protected by | |
1385 | * an escaped reference to part of the spine of this alist. | |
1386 | * If we mark the spine here, and only mark one or neither of the | |
1387 | * key and value, they may never be properly marked. | |
1388 | * This leads to a horrible situation in which an alist containing | |
1389 | * freelist cells is exported. | |
1390 | * | |
1391 | * So only mark the spines of these arrays last of all marking. | |
1392 | * If somebody confuses us by constructing a weak vector | |
1393 | * with a circular alist then we are hosed, but at least we | |
1394 | * won't prematurely drop table entries. | |
1395 | */ | |
1396 | if (!weak_keys) | |
56495472 | 1397 | RECURSE (SCM_CAR (kvpair)); |
0f2d19dd | 1398 | if (!weak_values) |
56495472 | 1399 | RECURSE (SCM_CDR (kvpair)); |
0f2d19dd JB |
1400 | alist = next_alist; |
1401 | } | |
1402 | if (SCM_NIMP (alist)) | |
56495472 | 1403 | RECURSE (alist); |
0f2d19dd JB |
1404 | } |
1405 | } | |
1406 | break; | |
1407 | ||
28b06554 DH |
1408 | case scm_tc7_symbol: |
1409 | ptr = SCM_PROP_SLOTS (ptr); | |
86d31dfe | 1410 | goto_gc_mark_loop; |
e5aca4b5 MV |
1411 | case scm_tc7_variable: |
1412 | ptr = SCM_CELL_OBJECT_1 (ptr); | |
1413 | goto_gc_mark_loop; | |
0f2d19dd | 1414 | case scm_tcs_subrs: |
9de33deb | 1415 | break; |
0f2d19dd JB |
1416 | case scm_tc7_port: |
1417 | i = SCM_PTOBNUM (ptr); | |
7a7f7c53 | 1418 | #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST)) |
0f2d19dd | 1419 | if (!(i < scm_numptob)) |
7a7f7c53 DH |
1420 | SCM_MISC_ERROR ("undefined port type", SCM_EOL); |
1421 | #endif | |
ebf7394e | 1422 | if (SCM_PTAB_ENTRY(ptr)) |
56495472 | 1423 | RECURSE (SCM_FILENAME (ptr)); |
dc53f026 JB |
1424 | if (scm_ptobs[i].mark) |
1425 | { | |
1426 | ptr = (scm_ptobs[i].mark) (ptr); | |
86d31dfe | 1427 | goto_gc_mark_loop; |
dc53f026 JB |
1428 | } |
1429 | else | |
1430 | return; | |
0f2d19dd JB |
1431 | break; |
1432 | case scm_tc7_smob: | |
d6884e63 | 1433 | switch (SCM_TYP16 (ptr)) |
0f2d19dd JB |
1434 | { /* should be faster than going through scm_smobs */ |
1435 | case scm_tc_free_cell: | |
592996c9 DH |
1436 | /* We have detected a free cell. This can happen if non-object data |
1437 | * on the C stack points into guile's heap and is scanned during | |
1438 | * conservative marking. */ | |
1439 | #if (SCM_DEBUG_CELL_ACCESSES == 0) | |
1440 | /* If cell debugging is disabled, there is a second situation in | |
1441 | * which a free cell can be encountered, namely if with preemptive | |
1442 | * threading one thread has just obtained a fresh cell and was | |
1443 | * preempted before the cell initialization was completed. In this | |
1444 | * case, some entries of the cell may already contain objects. | |
1445 | * Thus, if cell debugging is disabled, free cells are scanned | |
1446 | * conservatively. */ | |
1447 | scm_gc_mark_cell_conservatively (ptr); | |
1448 | #else /* SCM_DEBUG_CELL_ACCESSES == 1 */ | |
1449 | /* With cell debugging enabled, a freshly obtained but not fully | |
1450 | * initialized cell is guaranteed to be of type scm_tc16_allocated. | |
1451 | * Thus, no conservative scanning for free cells is necessary, but | |
1452 | * instead cells of type scm_tc16_allocated have to be scanned | |
1453 | * conservatively. This is done in the mark function of the | |
1454 | * scm_tc16_allocated smob type. */ | |
1455 | #endif | |
1456 | break; | |
acb0a19c MD |
1457 | case scm_tc16_big: |
1458 | case scm_tc16_real: | |
1459 | case scm_tc16_complex: | |
0f2d19dd JB |
1460 | break; |
1461 | default: | |
1462 | i = SCM_SMOBNUM (ptr); | |
7a7f7c53 | 1463 | #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST)) |
0f2d19dd | 1464 | if (!(i < scm_numsmob)) |
7a7f7c53 DH |
1465 | SCM_MISC_ERROR ("undefined smob type", SCM_EOL); |
1466 | #endif | |
dc53f026 JB |
1467 | if (scm_smobs[i].mark) |
1468 | { | |
1469 | ptr = (scm_smobs[i].mark) (ptr); | |
86d31dfe | 1470 | goto_gc_mark_loop; |
dc53f026 JB |
1471 | } |
1472 | else | |
1473 | return; | |
0f2d19dd JB |
1474 | } |
1475 | break; | |
1476 | default: | |
acf4331f | 1477 | SCM_MISC_ERROR ("unknown type", SCM_EOL); |
0f2d19dd | 1478 | } |
0209177b | 1479 | #undef RECURSE |
0f2d19dd | 1480 | } |
acf4331f | 1481 | #undef FUNC_NAME |
0f2d19dd | 1482 | |
56495472 ML |
1483 | #ifndef MARK_DEPENDENCIES |
1484 | ||
1485 | #undef MARK | |
56495472 ML |
1486 | #undef FNAME |
1487 | ||
1488 | /* And here we define `scm_gc_mark_dependencies', by including this | |
1489 | * same file in itself. | |
1490 | */ | |
1491 | #define MARK scm_gc_mark_dependencies | |
1492 | #define FNAME "scm_gc_mark_dependencies" | |
1493 | #define MARK_DEPENDENCIES | |
1494 | #include "gc.c" | |
1495 | #undef MARK_DEPENDENCIES | |
1496 | #undef MARK | |
56495472 ML |
1497 | #undef FNAME |
1498 | ||
0f2d19dd | 1499 | |
592996c9 DH |
1500 | /* Determine whether the given value does actually represent a cell in some |
1501 | * heap segment. If this is the case, the number of the heap segment is | |
1502 | * returned. Otherwise, -1 is returned. Binary search is used in order to | |
1503 | * determine the heap segment that contains the cell.*/ | |
1504 | /* FIXME: To be used within scm_gc_mark_cell_conservatively, | |
1505 | * scm_mark_locations and scm_cellp this function should be an inline | |
1506 | * function. */ | |
1507 | static long int | |
1508 | heap_segment (SCM obj) | |
0f2d19dd | 1509 | { |
592996c9 DH |
1510 | if (!SCM_CELLP (obj)) |
1511 | return -1; | |
1512 | else | |
c4da09e2 | 1513 | { |
592996c9 DH |
1514 | SCM_CELLPTR ptr = SCM2PTR (obj); |
1515 | unsigned long int i = 0; | |
1516 | unsigned long int j = scm_n_heap_segs - 1; | |
1517 | ||
1518 | if (SCM_PTR_LT (ptr, scm_heap_table[i].bounds[0])) | |
1519 | return -1; | |
1520 | else if (SCM_PTR_LE (scm_heap_table[j].bounds[1], ptr)) | |
1521 | return -1; | |
1522 | else | |
c4da09e2 | 1523 | { |
592996c9 | 1524 | while (i < j) |
c4da09e2 | 1525 | { |
592996c9 | 1526 | if (SCM_PTR_LT (ptr, scm_heap_table[i].bounds[1])) |
c4da09e2 | 1527 | { |
592996c9 DH |
1528 | break; |
1529 | } | |
1530 | else if (SCM_PTR_LE (scm_heap_table[j].bounds[0], ptr)) | |
1531 | { | |
1532 | i = j; | |
1533 | break; | |
1534 | } | |
1535 | else | |
1536 | { | |
1537 | unsigned long int k = (i + j) / 2; | |
1538 | ||
1539 | if (k == i) | |
1540 | return -1; | |
1541 | else if (SCM_PTR_LT (ptr, scm_heap_table[k].bounds[1])) | |
c4da09e2 | 1542 | { |
592996c9 DH |
1543 | j = k; |
1544 | ++i; | |
1545 | if (SCM_PTR_LT (ptr, scm_heap_table[i].bounds[0])) | |
1546 | return -1; | |
1547 | } | |
1548 | else if (SCM_PTR_LE (scm_heap_table[k].bounds[0], ptr)) | |
1549 | { | |
1550 | i = k; | |
1551 | --j; | |
1552 | if (SCM_PTR_LE (scm_heap_table[j].bounds[1], ptr)) | |
1553 | return -1; | |
c4da09e2 | 1554 | } |
c4da09e2 DH |
1555 | } |
1556 | } | |
592996c9 DH |
1557 | |
1558 | if (!DOUBLECELL_ALIGNED_P (obj) && scm_heap_table[i].span == 2) | |
1559 | return -1; | |
1560 | else if (SCM_GC_IN_CARD_HEADERP (ptr)) | |
1561 | return -1; | |
1562 | else | |
1563 | return i; | |
c4da09e2 DH |
1564 | } |
1565 | } | |
0f2d19dd JB |
1566 | } |
1567 | ||
1568 | ||
592996c9 DH |
1569 | /* Mark the entries of a cell conservatively. The given cell is known to be |
1570 | * on the heap. Still we have to determine its heap segment in order to | |
1571 | * figure out whether it is a single or a double cell. Then, each of the cell | |
1572 | * elements itself is checked and potentially marked. */ | |
1573 | void | |
1574 | scm_gc_mark_cell_conservatively (SCM cell) | |
2e11a577 | 1575 | { |
592996c9 DH |
1576 | unsigned long int cell_segment = heap_segment (cell); |
1577 | unsigned int span = scm_heap_table[cell_segment].span; | |
1578 | unsigned int i; | |
1a548472 | 1579 | |
592996c9 DH |
1580 | for (i = 1; i != span * 2; ++i) |
1581 | { | |
1582 | SCM obj = SCM_CELL_OBJECT (cell, i); | |
1583 | long int obj_segment = heap_segment (obj); | |
1584 | if (obj_segment >= 0) | |
1585 | scm_gc_mark (obj); | |
1586 | } | |
1587 | } | |
61045190 | 1588 | |
592996c9 DH |
1589 | |
1590 | /* Mark a region conservatively */ | |
1591 | void | |
1592 | scm_mark_locations (SCM_STACKITEM x[], unsigned long n) | |
1593 | { | |
1594 | unsigned long m; | |
1595 | ||
1596 | for (m = 0; m < n; ++m) | |
1597 | { | |
1598 | SCM obj = * (SCM *) &x[m]; | |
1599 | long int segment = heap_segment (obj); | |
1600 | if (segment >= 0) | |
1601 | scm_gc_mark (obj); | |
1a548472 | 1602 | } |
592996c9 | 1603 | } |
2e11a577 | 1604 | |
592996c9 DH |
1605 | |
1606 | /* The function scm_cellp determines whether an SCM value can be regarded as a | |
1607 | * pointer to a cell on the heap. | |
1608 | */ | |
1609 | int | |
1610 | scm_cellp (SCM value) | |
1611 | { | |
1612 | long int segment = heap_segment (value); | |
1613 | return (segment >= 0); | |
2e11a577 MD |
1614 | } |
1615 | ||
1616 | ||
4c48ba06 | 1617 | static void |
92c2555f | 1618 | gc_sweep_freelist_start (scm_t_freelist *freelist) |
4c48ba06 MD |
1619 | { |
1620 | freelist->cells = SCM_EOL; | |
1621 | freelist->left_to_collect = freelist->cluster_size; | |
b37fe1c5 | 1622 | freelist->clusters_allocated = 0; |
4c48ba06 MD |
1623 | freelist->clusters = SCM_EOL; |
1624 | freelist->clustertail = &freelist->clusters; | |
1811ebce | 1625 | freelist->collected_1 = freelist->collected; |
4c48ba06 MD |
1626 | freelist->collected = 0; |
1627 | } | |
1628 | ||
1629 | static void | |
92c2555f | 1630 | gc_sweep_freelist_finish (scm_t_freelist *freelist) |
4c48ba06 | 1631 | { |
c014a02e | 1632 | long collected; |
4c48ba06 | 1633 | *freelist->clustertail = freelist->cells; |
3f5d82cd | 1634 | if (!SCM_NULLP (freelist->cells)) |
4c48ba06 MD |
1635 | { |
1636 | SCM c = freelist->cells; | |
22a52da1 DH |
1637 | SCM_SET_CELL_WORD_0 (c, SCM_FREE_CELL_CDR (c)); |
1638 | SCM_SET_CELL_WORD_1 (c, SCM_EOL); | |
4c48ba06 MD |
1639 | freelist->collected += |
1640 | freelist->span * (freelist->cluster_size - freelist->left_to_collect); | |
1641 | } | |
b37fe1c5 | 1642 | scm_gc_cells_collected += freelist->collected; |
a00c95d9 | 1643 | |
8fef55a8 | 1644 | /* Although freelist->min_yield is used to test freelist->collected |
7dbff8b1 | 1645 | * (which is the local GC yield for freelist), it is adjusted so |
8fef55a8 | 1646 | * that *total* yield is freelist->min_yield_fraction of total heap |
7dbff8b1 MD |
1647 | * size. This means that a too low yield is compensated by more |
1648 | * heap on the list which is currently doing most work, which is | |
1649 | * just what we want. | |
1650 | */ | |
1811ebce | 1651 | collected = SCM_MAX (freelist->collected_1, freelist->collected); |
8fef55a8 | 1652 | freelist->grow_heap_p = (collected < freelist->min_yield); |
4c48ba06 | 1653 | } |
0f2d19dd | 1654 | |
d6884e63 ML |
1655 | #define NEXT_DATA_CELL(ptr, span) \ |
1656 | do { \ | |
1657 | scm_cell *nxt__ = CELL_UP ((char *) (ptr) + 1, (span)); \ | |
1658 | (ptr) = (SCM_GC_IN_CARD_HEADERP (nxt__) ? \ | |
1659 | CELL_UP (SCM_GC_CELL_CARD (nxt__) + SCM_GC_CARD_N_HEADER_CELLS, span) \ | |
1660 | : nxt__); \ | |
1661 | } while (0) | |
1662 | ||
a00c95d9 | 1663 | void |
0f2d19dd | 1664 | scm_gc_sweep () |
acf4331f | 1665 | #define FUNC_NAME "scm_gc_sweep" |
0f2d19dd JB |
1666 | { |
1667 | register SCM_CELLPTR ptr; | |
0f2d19dd | 1668 | register SCM nfreelist; |
92c2555f | 1669 | register scm_t_freelist *freelist; |
c014a02e | 1670 | register unsigned long m; |
0f2d19dd | 1671 | register int span; |
592996c9 | 1672 | size_t i; |
1be6b49c | 1673 | size_t seg_size; |
0f2d19dd | 1674 | |
0f2d19dd | 1675 | m = 0; |
0f2d19dd | 1676 | |
4c48ba06 MD |
1677 | gc_sweep_freelist_start (&scm_master_freelist); |
1678 | gc_sweep_freelist_start (&scm_master_freelist2); | |
a00c95d9 | 1679 | |
cf2d30f6 | 1680 | for (i = 0; i < scm_n_heap_segs; i++) |
0f2d19dd | 1681 | { |
c014a02e | 1682 | register long left_to_collect; |
1be6b49c | 1683 | register size_t j; |
15e9d186 | 1684 | |
cf2d30f6 JB |
1685 | /* Unmarked cells go onto the front of the freelist this heap |
1686 | segment points to. Rather than updating the real freelist | |
1687 | pointer as we go along, we accumulate the new head in | |
1688 | nfreelist. Then, if it turns out that the entire segment is | |
1689 | free, we free (i.e., malloc's free) the whole segment, and | |
1690 | simply don't assign nfreelist back into the real freelist. */ | |
4c48ba06 MD |
1691 | freelist = scm_heap_table[i].freelist; |
1692 | nfreelist = freelist->cells; | |
4c48ba06 | 1693 | left_to_collect = freelist->left_to_collect; |
945fec60 | 1694 | span = scm_heap_table[i].span; |
cf2d30f6 | 1695 | |
a00c95d9 ML |
1696 | ptr = CELL_UP (scm_heap_table[i].bounds[0], span); |
1697 | seg_size = CELL_DN (scm_heap_table[i].bounds[1], span) - ptr; | |
c9b0d4b0 | 1698 | |
d6884e63 ML |
1699 | /* use only data cells in seg_size */ |
1700 | seg_size = (seg_size / SCM_GC_CARD_N_CELLS) * (SCM_GC_CARD_N_DATA_CELLS / span) * span; | |
1701 | ||
c9b0d4b0 ML |
1702 | scm_gc_cells_swept += seg_size; |
1703 | ||
0f2d19dd JB |
1704 | for (j = seg_size + span; j -= span; ptr += span) |
1705 | { | |
d6884e63 | 1706 | SCM scmptr; |
96f6f4ae | 1707 | |
d6884e63 | 1708 | if (SCM_GC_IN_CARD_HEADERP (ptr)) |
0f2d19dd | 1709 | { |
d6884e63 ML |
1710 | SCM_CELLPTR nxt; |
1711 | ||
1712 | /* cheat here */ | |
1713 | nxt = ptr; | |
1714 | NEXT_DATA_CELL (nxt, span); | |
1715 | j += span; | |
1716 | ||
1717 | ptr = nxt - span; | |
1718 | continue; | |
1719 | } | |
1720 | ||
1721 | scmptr = PTR2SCM (ptr); | |
1722 | ||
1723 | if (SCM_GCMARKP (scmptr)) | |
1724 | continue; | |
7bb8eac7 | 1725 | |
d6884e63 ML |
1726 | switch SCM_TYP7 (scmptr) |
1727 | { | |
904a077d | 1728 | case scm_tcs_struct: |
0f2d19dd | 1729 | { |
904a077d MV |
1730 | /* Structs need to be freed in a special order. |
1731 | * This is handled by GC C hooks in struct.c. | |
c8045e8d | 1732 | */ |
904a077d MV |
1733 | SCM_SET_STRUCT_GC_CHAIN (scmptr, scm_structs_to_free); |
1734 | scm_structs_to_free = scmptr; | |
0f2d19dd | 1735 | } |
904a077d | 1736 | continue; |
0f2d19dd JB |
1737 | case scm_tcs_cons_imcar: |
1738 | case scm_tcs_cons_nimcar: | |
1739 | case scm_tcs_closures: | |
e641afaf | 1740 | case scm_tc7_pws: |
0f2d19dd JB |
1741 | break; |
1742 | case scm_tc7_wvect: | |
0f2d19dd | 1743 | case scm_tc7_vector: |
1b9be268 | 1744 | { |
c014a02e | 1745 | unsigned long int length = SCM_VECTOR_LENGTH (scmptr); |
1b9be268 DH |
1746 | if (length > 0) |
1747 | { | |
92c2555f | 1748 | m += length * sizeof (scm_t_bits); |
1b9be268 DH |
1749 | scm_must_free (SCM_VECTOR_BASE (scmptr)); |
1750 | } | |
1751 | break; | |
1752 | } | |
0f2d19dd JB |
1753 | #ifdef CCLO |
1754 | case scm_tc7_cclo: | |
b5c2579a | 1755 | m += (SCM_CCLO_LENGTH (scmptr) * sizeof (SCM)); |
06ee04b2 | 1756 | scm_must_free (SCM_CCLO_BASE (scmptr)); |
0f2d19dd | 1757 | break; |
06ee04b2 | 1758 | #endif |
afe5177e | 1759 | #ifdef HAVE_ARRAYS |
0f2d19dd | 1760 | case scm_tc7_bvect: |
93778877 | 1761 | { |
c014a02e | 1762 | unsigned long int length = SCM_BITVECTOR_LENGTH (scmptr); |
93778877 DH |
1763 | if (length > 0) |
1764 | { | |
c014a02e | 1765 | m += sizeof (long) * ((length + SCM_LONG_BIT - 1) / SCM_LONG_BIT); |
93778877 DH |
1766 | scm_must_free (SCM_BITVECTOR_BASE (scmptr)); |
1767 | } | |
1768 | } | |
06ee04b2 | 1769 | break; |
0f2d19dd | 1770 | case scm_tc7_byvect: |
0f2d19dd JB |
1771 | case scm_tc7_ivect: |
1772 | case scm_tc7_uvect: | |
0f2d19dd | 1773 | case scm_tc7_svect: |
5c11cc9d | 1774 | #ifdef HAVE_LONG_LONGS |
0f2d19dd | 1775 | case scm_tc7_llvect: |
0f2d19dd JB |
1776 | #endif |
1777 | case scm_tc7_fvect: | |
0f2d19dd | 1778 | case scm_tc7_dvect: |
0f2d19dd | 1779 | case scm_tc7_cvect: |
d1ca2c64 | 1780 | m += SCM_UVECTOR_LENGTH (scmptr) * scm_uniform_element_size (scmptr); |
06ee04b2 DH |
1781 | scm_must_free (SCM_UVECTOR_BASE (scmptr)); |
1782 | break; | |
afe5177e | 1783 | #endif |
0f2d19dd | 1784 | case scm_tc7_substring: |
0f2d19dd JB |
1785 | break; |
1786 | case scm_tc7_string: | |
b5c2579a | 1787 | m += SCM_STRING_LENGTH (scmptr) + 1; |
f151f912 DH |
1788 | scm_must_free (SCM_STRING_CHARS (scmptr)); |
1789 | break; | |
28b06554 | 1790 | case scm_tc7_symbol: |
b5c2579a | 1791 | m += SCM_SYMBOL_LENGTH (scmptr) + 1; |
f151f912 | 1792 | scm_must_free (SCM_SYMBOL_CHARS (scmptr)); |
0f2d19dd | 1793 | break; |
0f2d19dd | 1794 | case scm_tcs_subrs: |
d6884e63 | 1795 | /* the various "subrs" (primitives) are never freed */ |
0f2d19dd JB |
1796 | continue; |
1797 | case scm_tc7_port: | |
0f2d19dd JB |
1798 | if SCM_OPENP (scmptr) |
1799 | { | |
1800 | int k = SCM_PTOBNUM (scmptr); | |
7a7f7c53 | 1801 | #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST)) |
0f2d19dd | 1802 | if (!(k < scm_numptob)) |
7a7f7c53 DH |
1803 | SCM_MISC_ERROR ("undefined port type", SCM_EOL); |
1804 | #endif | |
0f2d19dd | 1805 | /* Keep "revealed" ports alive. */ |
945fec60 | 1806 | if (scm_revealed_count (scmptr) > 0) |
0f2d19dd JB |
1807 | continue; |
1808 | /* Yes, I really do mean scm_ptobs[k].free */ | |
1809 | /* rather than ftobs[k].close. .close */ | |
1810 | /* is for explicit CLOSE-PORT by user */ | |
84af0382 | 1811 | m += (scm_ptobs[k].free) (scmptr); |
0f2d19dd JB |
1812 | SCM_SETSTREAM (scmptr, 0); |
1813 | scm_remove_from_port_table (scmptr); | |
1814 | scm_gc_ports_collected++; | |
22a52da1 | 1815 | SCM_CLR_PORT_OPEN_FLAG (scmptr); |
0f2d19dd JB |
1816 | } |
1817 | break; | |
1818 | case scm_tc7_smob: | |
d6884e63 | 1819 | switch SCM_TYP16 (scmptr) |
0f2d19dd JB |
1820 | { |
1821 | case scm_tc_free_cell: | |
acb0a19c | 1822 | case scm_tc16_real: |
0f2d19dd JB |
1823 | break; |
1824 | #ifdef SCM_BIGDIG | |
acb0a19c | 1825 | case scm_tc16_big: |
0f2d19dd | 1826 | m += (SCM_NUMDIGS (scmptr) * SCM_BITSPERDIG / SCM_CHAR_BIT); |
06ee04b2 DH |
1827 | scm_must_free (SCM_BDIGITS (scmptr)); |
1828 | break; | |
0f2d19dd | 1829 | #endif /* def SCM_BIGDIG */ |
acb0a19c | 1830 | case scm_tc16_complex: |
92c2555f | 1831 | m += sizeof (scm_t_complex); |
405aaef9 | 1832 | scm_must_free (SCM_COMPLEX_MEM (scmptr)); |
06ee04b2 | 1833 | break; |
0f2d19dd | 1834 | default: |
0f2d19dd JB |
1835 | { |
1836 | int k; | |
1837 | k = SCM_SMOBNUM (scmptr); | |
7a7f7c53 | 1838 | #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST)) |
0f2d19dd | 1839 | if (!(k < scm_numsmob)) |
7a7f7c53 DH |
1840 | SCM_MISC_ERROR ("undefined smob type", SCM_EOL); |
1841 | #endif | |
1842 | if (scm_smobs[k].free) | |
1843 | m += (scm_smobs[k].free) (scmptr); | |
0f2d19dd JB |
1844 | break; |
1845 | } | |
1846 | } | |
1847 | break; | |
1848 | default: | |
acf4331f | 1849 | SCM_MISC_ERROR ("unknown type", SCM_EOL); |
0f2d19dd | 1850 | } |
7bb8eac7 | 1851 | |
4c48ba06 | 1852 | if (!--left_to_collect) |
4a4c9785 | 1853 | { |
22a52da1 | 1854 | SCM_SET_CELL_WORD_0 (scmptr, nfreelist); |
4c48ba06 MD |
1855 | *freelist->clustertail = scmptr; |
1856 | freelist->clustertail = SCM_CDRLOC (scmptr); | |
a00c95d9 | 1857 | |
4a4c9785 | 1858 | nfreelist = SCM_EOL; |
4c48ba06 MD |
1859 | freelist->collected += span * freelist->cluster_size; |
1860 | left_to_collect = freelist->cluster_size; | |
4a4c9785 MD |
1861 | } |
1862 | else | |
4a4c9785 MD |
1863 | { |
1864 | /* Stick the new cell on the front of nfreelist. It's | |
1865 | critical that we mark this cell as freed; otherwise, the | |
1866 | conservative collector might trace it as some other type | |
1867 | of object. */ | |
54778cd3 | 1868 | SCM_SET_CELL_TYPE (scmptr, scm_tc_free_cell); |
3f5d82cd | 1869 | SCM_SET_FREE_CELL_CDR (scmptr, nfreelist); |
4a4c9785 MD |
1870 | nfreelist = scmptr; |
1871 | } | |
0f2d19dd | 1872 | } |
d6884e63 | 1873 | |
0f2d19dd JB |
1874 | #ifdef GC_FREE_SEGMENTS |
1875 | if (n == seg_size) | |
1876 | { | |
c014a02e | 1877 | register long j; |
15e9d186 | 1878 | |
4c48ba06 | 1879 | freelist->heap_size -= seg_size; |
cf2d30f6 JB |
1880 | free ((char *) scm_heap_table[i].bounds[0]); |
1881 | scm_heap_table[i].bounds[0] = 0; | |
1882 | for (j = i + 1; j < scm_n_heap_segs; j++) | |
0f2d19dd JB |
1883 | scm_heap_table[j - 1] = scm_heap_table[j]; |
1884 | scm_n_heap_segs -= 1; | |
cf2d30f6 | 1885 | i--; /* We need to scan the segment just moved. */ |
0f2d19dd JB |
1886 | } |
1887 | else | |
1888 | #endif /* ifdef GC_FREE_SEGMENTS */ | |
4a4c9785 MD |
1889 | { |
1890 | /* Update the real freelist pointer to point to the head of | |
1891 | the list of free cells we've built for this segment. */ | |
4c48ba06 | 1892 | freelist->cells = nfreelist; |
4c48ba06 | 1893 | freelist->left_to_collect = left_to_collect; |
4a4c9785 MD |
1894 | } |
1895 | ||
fca7547b | 1896 | #ifdef GUILE_DEBUG_FREELIST |
cf2d30f6 JB |
1897 | scm_map_free_list (); |
1898 | #endif | |
4a4c9785 | 1899 | } |
a00c95d9 | 1900 | |
4c48ba06 MD |
1901 | gc_sweep_freelist_finish (&scm_master_freelist); |
1902 | gc_sweep_freelist_finish (&scm_master_freelist2); | |
a00c95d9 | 1903 | |
8ded62a3 MD |
1904 | /* When we move to POSIX threads private freelists should probably |
1905 | be GC-protected instead. */ | |
1906 | scm_freelist = SCM_EOL; | |
1907 | scm_freelist2 = SCM_EOL; | |
a00c95d9 | 1908 | |
b37fe1c5 | 1909 | scm_cells_allocated = (SCM_HEAP_SIZE - scm_gc_cells_collected); |
8b0d194f | 1910 | scm_gc_yield -= scm_cells_allocated; |
1be6b49c ML |
1911 | |
1912 | if (scm_mallocated < m) | |
1913 | /* The byte count of allocated objects has underflowed. This is | |
1914 | probably because you forgot to report the sizes of objects you | |
1915 | have allocated, by calling scm_done_malloc or some such. When | |
1916 | the GC freed them, it subtracted their size from | |
1917 | scm_mallocated, which underflowed. */ | |
1918 | abort (); | |
1919 | ||
0f2d19dd JB |
1920 | scm_mallocated -= m; |
1921 | scm_gc_malloc_collected = m; | |
1922 | } | |
acf4331f | 1923 | #undef FUNC_NAME |
0f2d19dd JB |
1924 | |
1925 | ||
1926 | \f | |
0f2d19dd JB |
1927 | /* {Front end to malloc} |
1928 | * | |
9d47a1e6 ML |
1929 | * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc, |
1930 | * scm_done_free | |
0f2d19dd | 1931 | * |
c6c79933 GH |
1932 | * These functions provide services comparable to malloc, realloc, and |
1933 | * free. They should be used when allocating memory that will be under | |
1934 | * control of the garbage collector, i.e., if the memory may be freed | |
1935 | * during garbage collection. | |
1936 | */ | |
bc9d9bb2 | 1937 | |
0f2d19dd JB |
1938 | /* scm_must_malloc |
1939 | * Return newly malloced storage or throw an error. | |
1940 | * | |
1941 | * The parameter WHAT is a string for error reporting. | |
a00c95d9 | 1942 | * If the threshold scm_mtrigger will be passed by this |
0f2d19dd JB |
1943 | * allocation, or if the first call to malloc fails, |
1944 | * garbage collect -- on the presumption that some objects | |
1945 | * using malloced storage may be collected. | |
1946 | * | |
1947 | * The limit scm_mtrigger may be raised by this allocation. | |
1948 | */ | |
07806695 | 1949 | void * |
1be6b49c | 1950 | scm_must_malloc (size_t size, const char *what) |
0f2d19dd | 1951 | { |
07806695 | 1952 | void *ptr; |
c014a02e | 1953 | unsigned long nm = scm_mallocated + size; |
1be6b49c ML |
1954 | |
1955 | if (nm < size) | |
1956 | /* The byte count of allocated objects has overflowed. This is | |
1957 | probably because you forgot to report the correct size of freed | |
1958 | memory in some of your smob free methods. */ | |
1959 | abort (); | |
e4ef2330 MD |
1960 | |
1961 | if (nm <= scm_mtrigger) | |
0f2d19dd | 1962 | { |
07806695 | 1963 | SCM_SYSCALL (ptr = malloc (size)); |
0f2d19dd JB |
1964 | if (NULL != ptr) |
1965 | { | |
1966 | scm_mallocated = nm; | |
bc9d9bb2 MD |
1967 | #ifdef GUILE_DEBUG_MALLOC |
1968 | scm_malloc_register (ptr, what); | |
1969 | #endif | |
0f2d19dd JB |
1970 | return ptr; |
1971 | } | |
1972 | } | |
6064dcc6 | 1973 | |
0f2d19dd | 1974 | scm_igc (what); |
e4ef2330 | 1975 | |
0f2d19dd | 1976 | nm = scm_mallocated + size; |
1be6b49c ML |
1977 | |
1978 | if (nm < size) | |
1979 | /* The byte count of allocated objects has overflowed. This is | |
1980 | probably because you forgot to report the correct size of freed | |
1981 | memory in some of your smob free methods. */ | |
1982 | abort (); | |
1983 | ||
07806695 | 1984 | SCM_SYSCALL (ptr = malloc (size)); |
0f2d19dd JB |
1985 | if (NULL != ptr) |
1986 | { | |
1987 | scm_mallocated = nm; | |
6064dcc6 MV |
1988 | if (nm > scm_mtrigger - SCM_MTRIGGER_HYSTERESIS) { |
1989 | if (nm > scm_mtrigger) | |
1990 | scm_mtrigger = nm + nm / 2; | |
1991 | else | |
1992 | scm_mtrigger += scm_mtrigger / 2; | |
1993 | } | |
bc9d9bb2 MD |
1994 | #ifdef GUILE_DEBUG_MALLOC |
1995 | scm_malloc_register (ptr, what); | |
1996 | #endif | |
1997 | ||
0f2d19dd JB |
1998 | return ptr; |
1999 | } | |
e4ef2330 | 2000 | |
acf4331f | 2001 | scm_memory_error (what); |
0f2d19dd JB |
2002 | } |
2003 | ||
2004 | ||
2005 | /* scm_must_realloc | |
2006 | * is similar to scm_must_malloc. | |
2007 | */ | |
07806695 JB |
2008 | void * |
2009 | scm_must_realloc (void *where, | |
1be6b49c ML |
2010 | size_t old_size, |
2011 | size_t size, | |
3eeba8d4 | 2012 | const char *what) |
0f2d19dd | 2013 | { |
07806695 | 2014 | void *ptr; |
c014a02e | 2015 | unsigned long nm; |
1be6b49c ML |
2016 | |
2017 | if (size <= old_size) | |
2018 | return where; | |
2019 | ||
2020 | nm = scm_mallocated + size - old_size; | |
2021 | ||
2022 | if (nm < (size - old_size)) | |
2023 | /* The byte count of allocated objects has overflowed. This is | |
2024 | probably because you forgot to report the correct size of freed | |
2025 | memory in some of your smob free methods. */ | |
2026 | abort (); | |
e4ef2330 MD |
2027 | |
2028 | if (nm <= scm_mtrigger) | |
0f2d19dd | 2029 | { |
07806695 | 2030 | SCM_SYSCALL (ptr = realloc (where, size)); |
0f2d19dd JB |
2031 | if (NULL != ptr) |
2032 | { | |
2033 | scm_mallocated = nm; | |
bc9d9bb2 MD |
2034 | #ifdef GUILE_DEBUG_MALLOC |
2035 | scm_malloc_reregister (where, ptr, what); | |
2036 | #endif | |
0f2d19dd JB |
2037 | return ptr; |
2038 | } | |
2039 | } | |
e4ef2330 | 2040 | |
0f2d19dd | 2041 | scm_igc (what); |
e4ef2330 MD |
2042 | |
2043 | nm = scm_mallocated + size - old_size; | |
1be6b49c ML |
2044 | |
2045 | if (nm < (size - old_size)) | |
2046 | /* The byte count of allocated objects has overflowed. This is | |
2047 | probably because you forgot to report the correct size of freed | |
2048 | memory in some of your smob free methods. */ | |
2049 | abort (); | |
2050 | ||
07806695 | 2051 | SCM_SYSCALL (ptr = realloc (where, size)); |
0f2d19dd JB |
2052 | if (NULL != ptr) |
2053 | { | |
2054 | scm_mallocated = nm; | |
6064dcc6 MV |
2055 | if (nm > scm_mtrigger - SCM_MTRIGGER_HYSTERESIS) { |
2056 | if (nm > scm_mtrigger) | |
2057 | scm_mtrigger = nm + nm / 2; | |
2058 | else | |
2059 | scm_mtrigger += scm_mtrigger / 2; | |
2060 | } | |
bc9d9bb2 MD |
2061 | #ifdef GUILE_DEBUG_MALLOC |
2062 | scm_malloc_reregister (where, ptr, what); | |
2063 | #endif | |
0f2d19dd JB |
2064 | return ptr; |
2065 | } | |
e4ef2330 | 2066 | |
acf4331f | 2067 | scm_memory_error (what); |
0f2d19dd JB |
2068 | } |
2069 | ||
e4a7824f | 2070 | char * |
1be6b49c | 2071 | scm_must_strndup (const char *str, size_t length) |
e4a7824f MV |
2072 | { |
2073 | char * dst = scm_must_malloc (length + 1, "scm_must_strndup"); | |
2074 | memcpy (dst, str, length); | |
2075 | dst[length] = 0; | |
2076 | return dst; | |
2077 | } | |
2078 | ||
2079 | char * | |
2080 | scm_must_strdup (const char *str) | |
2081 | { | |
2082 | return scm_must_strndup (str, strlen (str)); | |
2083 | } | |
acf4331f | 2084 | |
a00c95d9 | 2085 | void |
07806695 | 2086 | scm_must_free (void *obj) |
acf4331f | 2087 | #define FUNC_NAME "scm_must_free" |
0f2d19dd | 2088 | { |
bc9d9bb2 MD |
2089 | #ifdef GUILE_DEBUG_MALLOC |
2090 | scm_malloc_unregister (obj); | |
2091 | #endif | |
0f2d19dd JB |
2092 | if (obj) |
2093 | free (obj); | |
2094 | else | |
acf4331f | 2095 | SCM_MISC_ERROR ("freeing NULL pointer", SCM_EOL); |
0f2d19dd | 2096 | } |
acf4331f DH |
2097 | #undef FUNC_NAME |
2098 | ||
0f2d19dd | 2099 | |
c68296f8 MV |
2100 | /* Announce that there has been some malloc done that will be freed |
2101 | * during gc. A typical use is for a smob that uses some malloced | |
2102 | * memory but can not get it from scm_must_malloc (for whatever | |
2103 | * reason). When a new object of this smob is created you call | |
2104 | * scm_done_malloc with the size of the object. When your smob free | |
2105 | * function is called, be sure to include this size in the return | |
9d47a1e6 ML |
2106 | * value. |
2107 | * | |
2108 | * If you can't actually free the memory in the smob free function, | |
2109 | * for whatever reason (like reference counting), you still can (and | |
2110 | * should) report the amount of memory freed when you actually free it. | |
2111 | * Do it by calling scm_done_malloc with the _negated_ size. Clever, | |
2112 | * eh? Or even better, call scm_done_free. */ | |
0f2d19dd | 2113 | |
c68296f8 | 2114 | void |
c014a02e | 2115 | scm_done_malloc (long size) |
c68296f8 | 2116 | { |
1be6b49c ML |
2117 | if (size < 0) { |
2118 | if (scm_mallocated < size) | |
2119 | /* The byte count of allocated objects has underflowed. This is | |
2120 | probably because you forgot to report the sizes of objects you | |
2121 | have allocated, by calling scm_done_malloc or some such. When | |
2122 | the GC freed them, it subtracted their size from | |
2123 | scm_mallocated, which underflowed. */ | |
2124 | abort (); | |
2125 | } else { | |
c014a02e | 2126 | unsigned long nm = scm_mallocated + size; |
1be6b49c ML |
2127 | if (nm < size) |
2128 | /* The byte count of allocated objects has overflowed. This is | |
2129 | probably because you forgot to report the correct size of freed | |
2130 | memory in some of your smob free methods. */ | |
2131 | abort (); | |
2132 | } | |
2133 | ||
c68296f8 MV |
2134 | scm_mallocated += size; |
2135 | ||
2136 | if (scm_mallocated > scm_mtrigger) | |
2137 | { | |
2138 | scm_igc ("foreign mallocs"); | |
2139 | if (scm_mallocated > scm_mtrigger - SCM_MTRIGGER_HYSTERESIS) | |
2140 | { | |
2141 | if (scm_mallocated > scm_mtrigger) | |
2142 | scm_mtrigger = scm_mallocated + scm_mallocated / 2; | |
2143 | else | |
2144 | scm_mtrigger += scm_mtrigger / 2; | |
2145 | } | |
2146 | } | |
2147 | } | |
2148 | ||
9d47a1e6 | 2149 | void |
c014a02e | 2150 | scm_done_free (long size) |
9d47a1e6 | 2151 | { |
1be6b49c ML |
2152 | if (size >= 0) { |
2153 | if (scm_mallocated < size) | |
2154 | /* The byte count of allocated objects has underflowed. This is | |
2155 | probably because you forgot to report the sizes of objects you | |
2156 | have allocated, by calling scm_done_malloc or some such. When | |
2157 | the GC freed them, it subtracted their size from | |
2158 | scm_mallocated, which underflowed. */ | |
2159 | abort (); | |
2160 | } else { | |
c014a02e | 2161 | unsigned long nm = scm_mallocated + size; |
1be6b49c ML |
2162 | if (nm < size) |
2163 | /* The byte count of allocated objects has overflowed. This is | |
2164 | probably because you forgot to report the correct size of freed | |
2165 | memory in some of your smob free methods. */ | |
2166 | abort (); | |
2167 | } | |
2168 | ||
9d47a1e6 ML |
2169 | scm_mallocated -= size; |
2170 | } | |
2171 | ||
c68296f8 MV |
2172 | |
2173 | \f | |
0f2d19dd JB |
2174 | /* {Heap Segments} |
2175 | * | |
2176 | * Each heap segment is an array of objects of a particular size. | |
2177 | * Every segment has an associated (possibly shared) freelist. | |
2178 | * A table of segment records is kept that records the upper and | |
2179 | * lower extents of the segment; this is used during the conservative | |
2180 | * phase of gc to identify probably gc roots (because they point | |
c68296f8 | 2181 | * into valid segments at reasonable offsets). */ |
0f2d19dd JB |
2182 | |
2183 | /* scm_expmem | |
2184 | * is true if the first segment was smaller than INIT_HEAP_SEG. | |
2185 | * If scm_expmem is set to one, subsequent segment allocations will | |
2186 | * allocate segments of size SCM_EXPHEAP(scm_heap_size). | |
2187 | */ | |
2188 | int scm_expmem = 0; | |
2189 | ||
1be6b49c | 2190 | size_t scm_max_segment_size; |
4c48ba06 | 2191 | |
0f2d19dd JB |
2192 | /* scm_heap_org |
2193 | * is the lowest base address of any heap segment. | |
2194 | */ | |
2195 | SCM_CELLPTR scm_heap_org; | |
2196 | ||
92c2555f | 2197 | scm_t_heap_seg_data * scm_heap_table = 0; |
1be6b49c ML |
2198 | static size_t heap_segment_table_size = 0; |
2199 | size_t scm_n_heap_segs = 0; | |
0f2d19dd | 2200 | |
0f2d19dd | 2201 | /* init_heap_seg |
d6884e63 | 2202 | * initializes a new heap segment and returns the number of objects it contains. |
0f2d19dd | 2203 | * |
d6884e63 ML |
2204 | * The segment origin and segment size in bytes are input parameters. |
2205 | * The freelist is both input and output. | |
0f2d19dd | 2206 | * |
d6884e63 ML |
2207 | * This function presumes that the scm_heap_table has already been expanded |
2208 | * to accomodate a new segment record and that the markbit space was reserved | |
2209 | * for all the cards in this segment. | |
0f2d19dd JB |
2210 | */ |
2211 | ||
d6884e63 ML |
2212 | #define INIT_CARD(card, span) \ |
2213 | do { \ | |
322ec19d | 2214 | SCM_GC_SET_CARD_BVEC (card, get_bvec ()); \ |
d6884e63 ML |
2215 | if ((span) == 2) \ |
2216 | SCM_GC_SET_CARD_DOUBLECELL (card); \ | |
2217 | } while (0) | |
0f2d19dd | 2218 | |
1be6b49c | 2219 | static size_t |
92c2555f | 2220 | init_heap_seg (SCM_CELLPTR seg_org, size_t size, scm_t_freelist *freelist) |
0f2d19dd JB |
2221 | { |
2222 | register SCM_CELLPTR ptr; | |
0f2d19dd | 2223 | SCM_CELLPTR seg_end; |
592996c9 | 2224 | size_t new_seg_index; |
1be6b49c | 2225 | ptrdiff_t n_new_cells; |
4c48ba06 | 2226 | int span = freelist->span; |
a00c95d9 | 2227 | |
0f2d19dd JB |
2228 | if (seg_org == NULL) |
2229 | return 0; | |
2230 | ||
d6884e63 ML |
2231 | /* Align the begin ptr up. |
2232 | */ | |
2233 | ptr = SCM_GC_CARD_UP (seg_org); | |
acb0a19c | 2234 | |
a00c95d9 | 2235 | /* Compute the ceiling on valid object pointers w/in this segment. |
0f2d19dd | 2236 | */ |
d6884e63 | 2237 | seg_end = SCM_GC_CARD_DOWN ((char *)seg_org + size); |
0f2d19dd | 2238 | |
a00c95d9 | 2239 | /* Find the right place and insert the segment record. |
0f2d19dd | 2240 | */ |
592996c9 DH |
2241 | new_seg_index = 0; |
2242 | while (new_seg_index < scm_n_heap_segs | |
2243 | && SCM_PTR_LE (scm_heap_table[new_seg_index].bounds[0], seg_org)) | |
2244 | new_seg_index++; | |
0f2d19dd JB |
2245 | |
2246 | { | |
2247 | int i; | |
2248 | for (i = scm_n_heap_segs; i > new_seg_index; --i) | |
2249 | scm_heap_table[i] = scm_heap_table[i - 1]; | |
2250 | } | |
a00c95d9 | 2251 | |
0f2d19dd JB |
2252 | ++scm_n_heap_segs; |
2253 | ||
945fec60 | 2254 | scm_heap_table[new_seg_index].span = span; |
4c48ba06 | 2255 | scm_heap_table[new_seg_index].freelist = freelist; |
195e6201 DH |
2256 | scm_heap_table[new_seg_index].bounds[0] = ptr; |
2257 | scm_heap_table[new_seg_index].bounds[1] = seg_end; | |
0f2d19dd | 2258 | |
acb0a19c MD |
2259 | /*n_new_cells*/ |
2260 | n_new_cells = seg_end - ptr; | |
0f2d19dd | 2261 | |
4c48ba06 | 2262 | freelist->heap_size += n_new_cells; |
4a4c9785 | 2263 | |
a00c95d9 | 2264 | /* Partition objects in this segment into clusters */ |
4a4c9785 MD |
2265 | { |
2266 | SCM clusters; | |
2267 | SCM *clusterp = &clusters; | |
4a4c9785 | 2268 | |
d6884e63 ML |
2269 | NEXT_DATA_CELL (ptr, span); |
2270 | while (ptr < seg_end) | |
4a4c9785 | 2271 | { |
d6884e63 ML |
2272 | scm_cell *nxt = ptr; |
2273 | scm_cell *prv = NULL; | |
2274 | scm_cell *last_card = NULL; | |
2275 | int n_data_cells = (SCM_GC_CARD_N_DATA_CELLS / span) * SCM_CARDS_PER_CLUSTER - 1; | |
2276 | NEXT_DATA_CELL(nxt, span); | |
4a4c9785 | 2277 | |
4c48ba06 MD |
2278 | /* Allocate cluster spine |
2279 | */ | |
4a4c9785 | 2280 | *clusterp = PTR2SCM (ptr); |
d6884e63 | 2281 | SCM_SETCAR (*clusterp, PTR2SCM (nxt)); |
4a4c9785 | 2282 | clusterp = SCM_CDRLOC (*clusterp); |
d6884e63 | 2283 | ptr = nxt; |
a00c95d9 | 2284 | |
d6884e63 | 2285 | while (n_data_cells--) |
4a4c9785 | 2286 | { |
d6884e63 | 2287 | scm_cell *card = SCM_GC_CELL_CARD (ptr); |
96f6f4ae | 2288 | SCM scmptr = PTR2SCM (ptr); |
d6884e63 ML |
2289 | nxt = ptr; |
2290 | NEXT_DATA_CELL (nxt, span); | |
2291 | prv = ptr; | |
2292 | ||
2293 | if (card != last_card) | |
2294 | { | |
2295 | INIT_CARD (card, span); | |
2296 | last_card = card; | |
2297 | } | |
96f6f4ae | 2298 | |
54778cd3 | 2299 | SCM_SET_CELL_TYPE (scmptr, scm_tc_free_cell); |
22a52da1 | 2300 | SCM_SET_FREE_CELL_CDR (scmptr, PTR2SCM (nxt)); |
d6884e63 ML |
2301 | |
2302 | ptr = nxt; | |
4a4c9785 | 2303 | } |
4c48ba06 | 2304 | |
d6884e63 | 2305 | SCM_SET_FREE_CELL_CDR (PTR2SCM (prv), SCM_EOL); |
4a4c9785 | 2306 | } |
a00c95d9 | 2307 | |
d6884e63 ML |
2308 | /* sanity check */ |
2309 | { | |
2310 | scm_cell *ref = seg_end; | |
2311 | NEXT_DATA_CELL (ref, span); | |
2312 | if (ref != ptr) | |
2313 | /* [cmm] looks like the segment size doesn't divide cleanly by | |
2314 | cluster size. bad cmm! */ | |
2315 | abort(); | |
2316 | } | |
2317 | ||
4a4c9785 MD |
2318 | /* Patch up the last cluster pointer in the segment |
2319 | * to join it to the input freelist. | |
2320 | */ | |
4c48ba06 MD |
2321 | *clusterp = freelist->clusters; |
2322 | freelist->clusters = clusters; | |
4a4c9785 MD |
2323 | } |
2324 | ||
4c48ba06 MD |
2325 | #ifdef DEBUGINFO |
2326 | fprintf (stderr, "H"); | |
2327 | #endif | |
0f2d19dd | 2328 | return size; |
0f2d19dd JB |
2329 | } |
2330 | ||
1be6b49c | 2331 | static size_t |
92c2555f | 2332 | round_to_cluster_size (scm_t_freelist *freelist, size_t len) |
a00c95d9 | 2333 | { |
1be6b49c | 2334 | size_t cluster_size_in_bytes = CLUSTER_SIZE_IN_BYTES (freelist); |
a00c95d9 ML |
2335 | |
2336 | return | |
2337 | (len + cluster_size_in_bytes - 1) / cluster_size_in_bytes * cluster_size_in_bytes | |
2338 | + ALIGNMENT_SLACK (freelist); | |
2339 | } | |
2340 | ||
a00c95d9 | 2341 | static void |
92c2555f | 2342 | alloc_some_heap (scm_t_freelist *freelist, policy_on_error error_policy) |
acf4331f | 2343 | #define FUNC_NAME "alloc_some_heap" |
0f2d19dd | 2344 | { |
0f2d19dd | 2345 | SCM_CELLPTR ptr; |
1be6b49c | 2346 | size_t len; |
a00c95d9 | 2347 | |
9d47a1e6 | 2348 | if (scm_gc_heap_lock) |
b6efc951 DH |
2349 | { |
2350 | /* Critical code sections (such as the garbage collector) aren't | |
2351 | * supposed to add heap segments. | |
2352 | */ | |
2353 | fprintf (stderr, "alloc_some_heap: Can not extend locked heap.\n"); | |
2354 | abort (); | |
2355 | } | |
0f2d19dd | 2356 | |
9d47a1e6 | 2357 | if (scm_n_heap_segs == heap_segment_table_size) |
b6efc951 DH |
2358 | { |
2359 | /* We have to expand the heap segment table to have room for the new | |
2360 | * segment. Do not yet increment scm_n_heap_segs -- that is done by | |
2361 | * init_heap_seg only if the allocation of the segment itself succeeds. | |
2362 | */ | |
1be6b49c | 2363 | size_t new_table_size = scm_n_heap_segs + 1; |
92c2555f MV |
2364 | size_t size = new_table_size * sizeof (scm_t_heap_seg_data); |
2365 | scm_t_heap_seg_data *new_heap_table; | |
b6efc951 | 2366 | |
92c2555f | 2367 | SCM_SYSCALL (new_heap_table = ((scm_t_heap_seg_data *) |
b6efc951 DH |
2368 | realloc ((char *)scm_heap_table, size))); |
2369 | if (!new_heap_table) | |
2370 | { | |
2371 | if (error_policy == abort_on_error) | |
2372 | { | |
2373 | fprintf (stderr, "alloc_some_heap: Could not grow heap segment table.\n"); | |
2374 | abort (); | |
2375 | } | |
2376 | else | |
2377 | { | |
2378 | return; | |
2379 | } | |
2380 | } | |
2381 | else | |
2382 | { | |
2383 | scm_heap_table = new_heap_table; | |
2384 | heap_segment_table_size = new_table_size; | |
2385 | } | |
2386 | } | |
0f2d19dd | 2387 | |
0f2d19dd | 2388 | /* Pick a size for the new heap segment. |
a00c95d9 | 2389 | * The rule for picking the size of a segment is explained in |
0f2d19dd JB |
2390 | * gc.h |
2391 | */ | |
4c48ba06 | 2392 | { |
1811ebce MD |
2393 | /* Assure that the new segment is predicted to be large enough. |
2394 | * | |
2395 | * New yield should at least equal GC fraction of new heap size, i.e. | |
2396 | * | |
2397 | * y + dh > f * (h + dh) | |
2398 | * | |
2399 | * y : yield | |
8fef55a8 | 2400 | * f : min yield fraction |
1811ebce MD |
2401 | * h : heap size |
2402 | * dh : size of new heap segment | |
2403 | * | |
2404 | * This gives dh > (f * h - y) / (1 - f) | |
bda1446c | 2405 | */ |
8fef55a8 | 2406 | int f = freelist->min_yield_fraction; |
c014a02e | 2407 | unsigned long h = SCM_HEAP_SIZE; |
1be6b49c | 2408 | size_t min_cells = (f * h - 100 * (long) scm_gc_yield) / (99 - f); |
4c48ba06 MD |
2409 | len = SCM_EXPHEAP (freelist->heap_size); |
2410 | #ifdef DEBUGINFO | |
1be6b49c | 2411 | fprintf (stderr, "(%ld < %ld)", (long) len, (long) min_cells); |
4c48ba06 MD |
2412 | #endif |
2413 | if (len < min_cells) | |
1811ebce | 2414 | len = min_cells + freelist->cluster_size; |
4c48ba06 | 2415 | len *= sizeof (scm_cell); |
1811ebce MD |
2416 | /* force new sampling */ |
2417 | freelist->collected = LONG_MAX; | |
4c48ba06 | 2418 | } |
a00c95d9 | 2419 | |
4c48ba06 MD |
2420 | if (len > scm_max_segment_size) |
2421 | len = scm_max_segment_size; | |
0f2d19dd JB |
2422 | |
2423 | { | |
1be6b49c | 2424 | size_t smallest; |
0f2d19dd | 2425 | |
a00c95d9 | 2426 | smallest = CLUSTER_SIZE_IN_BYTES (freelist); |
a00c95d9 | 2427 | |
0f2d19dd | 2428 | if (len < smallest) |
a00c95d9 | 2429 | len = smallest; |
0f2d19dd JB |
2430 | |
2431 | /* Allocate with decaying ambition. */ | |
2432 | while ((len >= SCM_MIN_HEAP_SEG_SIZE) | |
2433 | && (len >= smallest)) | |
2434 | { | |
1be6b49c | 2435 | size_t rounded_len = round_to_cluster_size (freelist, len); |
a00c95d9 | 2436 | SCM_SYSCALL (ptr = (SCM_CELLPTR) malloc (rounded_len)); |
0f2d19dd JB |
2437 | if (ptr) |
2438 | { | |
a00c95d9 | 2439 | init_heap_seg (ptr, rounded_len, freelist); |
0f2d19dd JB |
2440 | return; |
2441 | } | |
2442 | len /= 2; | |
2443 | } | |
2444 | } | |
2445 | ||
b6efc951 DH |
2446 | if (error_policy == abort_on_error) |
2447 | { | |
2448 | fprintf (stderr, "alloc_some_heap: Could not grow heap.\n"); | |
2449 | abort (); | |
2450 | } | |
0f2d19dd | 2451 | } |
acf4331f | 2452 | #undef FUNC_NAME |
0f2d19dd | 2453 | |
0f2d19dd JB |
2454 | \f |
2455 | /* {GC Protection Helper Functions} | |
2456 | */ | |
2457 | ||
2458 | ||
5d2b97cd DH |
2459 | /* |
2460 | * If within a function you need to protect one or more scheme objects from | |
2461 | * garbage collection, pass them as parameters to one of the | |
2462 | * scm_remember_upto_here* functions below. These functions don't do | |
2463 | * anything, but since the compiler does not know that they are actually | |
2464 | * no-ops, it will generate code that calls these functions with the given | |
2465 | * parameters. Therefore, you can be sure that the compiler will keep those | |
2466 | * scheme values alive (on the stack or in a register) up to the point where | |
2467 | * scm_remember_upto_here* is called. In other words, place the call to | |
592996c9 | 2468 | * scm_remember_upto_here* _behind_ the last code in your function, that |
5d2b97cd DH |
2469 | * depends on the scheme object to exist. |
2470 | * | |
2471 | * Example: We want to make sure, that the string object str does not get | |
2472 | * garbage collected during the execution of 'some_function', because | |
2473 | * otherwise the characters belonging to str would be freed and | |
2474 | * 'some_function' might access freed memory. To make sure that the compiler | |
2475 | * keeps str alive on the stack or in a register such that it is visible to | |
2476 | * the conservative gc we add the call to scm_remember_upto_here_1 _after_ the | |
2477 | * call to 'some_function'. Note that this would not be necessary if str was | |
2478 | * used anyway after the call to 'some_function'. | |
2479 | * char *chars = SCM_STRING_CHARS (str); | |
2480 | * some_function (chars); | |
2481 | * scm_remember_upto_here_1 (str); // str will be alive up to this point. | |
2482 | */ | |
2483 | ||
2484 | void | |
e81d98ec | 2485 | scm_remember_upto_here_1 (SCM obj SCM_UNUSED) |
5d2b97cd DH |
2486 | { |
2487 | /* Empty. Protects a single object from garbage collection. */ | |
2488 | } | |
2489 | ||
2490 | void | |
e81d98ec | 2491 | scm_remember_upto_here_2 (SCM obj1 SCM_UNUSED, SCM obj2 SCM_UNUSED) |
5d2b97cd DH |
2492 | { |
2493 | /* Empty. Protects two objects from garbage collection. */ | |
2494 | } | |
2495 | ||
2496 | void | |
e81d98ec | 2497 | scm_remember_upto_here (SCM obj SCM_UNUSED, ...) |
5d2b97cd DH |
2498 | { |
2499 | /* Empty. Protects any number of objects from garbage collection. */ | |
2500 | } | |
2501 | ||
2502 | ||
2503 | #if (SCM_DEBUG_DEPRECATED == 0) | |
2504 | ||
0f2d19dd | 2505 | void |
6e8d25a6 | 2506 | scm_remember (SCM *ptr) |
b24b5e13 | 2507 | { |
1be6b49c ML |
2508 | scm_c_issue_deprecation_warning ("`scm_remember' is deprecated. " |
2509 | "Use the `scm_remember_upto_here*' family of functions instead."); | |
b24b5e13 | 2510 | } |
0f2d19dd | 2511 | |
6b1b030e ML |
2512 | SCM |
2513 | scm_protect_object (SCM obj) | |
2514 | { | |
2515 | scm_c_issue_deprecation_warning ("`scm_protect_object' is deprecated. " | |
2516 | "Use `scm_gc_protect_object' instead."); | |
2517 | return scm_gc_protect_object (obj); | |
2518 | } | |
2519 | ||
2520 | SCM | |
2521 | scm_unprotect_object (SCM obj) | |
2522 | { | |
2523 | scm_c_issue_deprecation_warning ("`scm_unprotect_object' is deprecated. " | |
2524 | "Use `scm_gc_unprotect_object' instead."); | |
2525 | return scm_gc_unprotect_object (obj); | |
2526 | } | |
2527 | ||
5d2b97cd | 2528 | #endif /* SCM_DEBUG_DEPRECATED == 0 */ |
1cc91f1b | 2529 | |
c209c88e | 2530 | /* |
41b0806d GB |
2531 | These crazy functions prevent garbage collection |
2532 | of arguments after the first argument by | |
2533 | ensuring they remain live throughout the | |
2534 | function because they are used in the last | |
2535 | line of the code block. | |
2536 | It'd be better to have a nice compiler hint to | |
2537 | aid the conservative stack-scanning GC. --03/09/00 gjb */ | |
0f2d19dd JB |
2538 | SCM |
2539 | scm_return_first (SCM elt, ...) | |
0f2d19dd JB |
2540 | { |
2541 | return elt; | |
2542 | } | |
2543 | ||
41b0806d GB |
2544 | int |
2545 | scm_return_first_int (int i, ...) | |
2546 | { | |
2547 | return i; | |
2548 | } | |
2549 | ||
0f2d19dd | 2550 | |
0f2d19dd | 2551 | SCM |
6e8d25a6 | 2552 | scm_permanent_object (SCM obj) |
0f2d19dd JB |
2553 | { |
2554 | SCM_REDEFER_INTS; | |
2555 | scm_permobjs = scm_cons (obj, scm_permobjs); | |
2556 | SCM_REALLOW_INTS; | |
2557 | return obj; | |
2558 | } | |
2559 | ||
2560 | ||
7bd4fbe2 MD |
2561 | /* Protect OBJ from the garbage collector. OBJ will not be freed, even if all |
2562 | other references are dropped, until the object is unprotected by calling | |
6b1b030e | 2563 | scm_gc_unprotect_object (OBJ). Calls to scm_gc_protect/unprotect_object nest, |
7bd4fbe2 MD |
2564 | i. e. it is possible to protect the same object several times, but it is |
2565 | necessary to unprotect the object the same number of times to actually get | |
2566 | the object unprotected. It is an error to unprotect an object more often | |
2567 | than it has been protected before. The function scm_protect_object returns | |
2568 | OBJ. | |
2569 | */ | |
2570 | ||
2571 | /* Implementation note: For every object X, there is a counter which | |
6b1b030e | 2572 | scm_gc_protect_object(X) increments and scm_gc_unprotect_object(X) decrements. |
7bd4fbe2 | 2573 | */ |
686765af | 2574 | |
ef290276 | 2575 | SCM |
6b1b030e | 2576 | scm_gc_protect_object (SCM obj) |
ef290276 | 2577 | { |
686765af | 2578 | SCM handle; |
9d47a1e6 | 2579 | |
686765af | 2580 | /* This critical section barrier will be replaced by a mutex. */ |
2dd6a83a | 2581 | SCM_REDEFER_INTS; |
9d47a1e6 | 2582 | |
0f0f0899 | 2583 | handle = scm_hashq_create_handle_x (scm_protects, obj, SCM_MAKINUM (0)); |
1be6b49c | 2584 | SCM_SETCDR (handle, scm_sum (SCM_CDR (handle), SCM_MAKINUM (1))); |
9d47a1e6 | 2585 | |
2dd6a83a | 2586 | SCM_REALLOW_INTS; |
9d47a1e6 | 2587 | |
ef290276 JB |
2588 | return obj; |
2589 | } | |
2590 | ||
2591 | ||
2592 | /* Remove any protection for OBJ established by a prior call to | |
dab7f566 | 2593 | scm_protect_object. This function returns OBJ. |
ef290276 | 2594 | |
dab7f566 | 2595 | See scm_protect_object for more information. */ |
ef290276 | 2596 | SCM |
6b1b030e | 2597 | scm_gc_unprotect_object (SCM obj) |
ef290276 | 2598 | { |
686765af | 2599 | SCM handle; |
9d47a1e6 | 2600 | |
686765af | 2601 | /* This critical section barrier will be replaced by a mutex. */ |
2dd6a83a | 2602 | SCM_REDEFER_INTS; |
9d47a1e6 | 2603 | |
686765af | 2604 | handle = scm_hashq_get_handle (scm_protects, obj); |
9d47a1e6 | 2605 | |
22a52da1 | 2606 | if (SCM_FALSEP (handle)) |
686765af | 2607 | { |
0f0f0899 MD |
2608 | fprintf (stderr, "scm_unprotect_object called on unprotected object\n"); |
2609 | abort (); | |
686765af | 2610 | } |
6a199940 DH |
2611 | else |
2612 | { | |
1be6b49c ML |
2613 | SCM count = scm_difference (SCM_CDR (handle), SCM_MAKINUM (1)); |
2614 | if (SCM_EQ_P (count, SCM_MAKINUM (0))) | |
6a199940 DH |
2615 | scm_hashq_remove_x (scm_protects, obj); |
2616 | else | |
1be6b49c | 2617 | SCM_SETCDR (handle, count); |
6a199940 | 2618 | } |
686765af | 2619 | |
2dd6a83a | 2620 | SCM_REALLOW_INTS; |
ef290276 JB |
2621 | |
2622 | return obj; | |
2623 | } | |
2624 | ||
6b1b030e ML |
2625 | void |
2626 | scm_gc_register_root (SCM *p) | |
2627 | { | |
2628 | SCM handle; | |
2629 | SCM key = scm_long2num ((long) p); | |
2630 | ||
2631 | /* This critical section barrier will be replaced by a mutex. */ | |
2632 | SCM_REDEFER_INTS; | |
2633 | ||
2634 | handle = scm_hashv_create_handle_x (scm_gc_registered_roots, key, SCM_MAKINUM (0)); | |
2635 | SCM_SETCDR (handle, scm_sum (SCM_CDR (handle), SCM_MAKINUM (1))); | |
2636 | ||
2637 | SCM_REALLOW_INTS; | |
2638 | } | |
2639 | ||
2640 | void | |
2641 | scm_gc_unregister_root (SCM *p) | |
2642 | { | |
2643 | SCM handle; | |
2644 | SCM key = scm_long2num ((long) p); | |
2645 | ||
2646 | /* This critical section barrier will be replaced by a mutex. */ | |
2647 | SCM_REDEFER_INTS; | |
2648 | ||
2649 | handle = scm_hashv_get_handle (scm_gc_registered_roots, key); | |
2650 | ||
2651 | if (SCM_FALSEP (handle)) | |
2652 | { | |
2653 | fprintf (stderr, "scm_gc_unregister_root called on unregistered root\n"); | |
2654 | abort (); | |
2655 | } | |
2656 | else | |
2657 | { | |
2658 | SCM count = scm_difference (SCM_CDR (handle), SCM_MAKINUM (1)); | |
2659 | if (SCM_EQ_P (count, SCM_MAKINUM (0))) | |
2660 | scm_hashv_remove_x (scm_gc_registered_roots, key); | |
2661 | else | |
2662 | SCM_SETCDR (handle, count); | |
2663 | } | |
2664 | ||
2665 | SCM_REALLOW_INTS; | |
2666 | } | |
2667 | ||
2668 | void | |
2669 | scm_gc_register_roots (SCM *b, unsigned long n) | |
2670 | { | |
2671 | SCM *p = b; | |
2672 | for (; p < b + n; ++p) | |
2673 | scm_gc_register_root (p); | |
2674 | } | |
2675 | ||
2676 | void | |
2677 | scm_gc_unregister_roots (SCM *b, unsigned long n) | |
2678 | { | |
2679 | SCM *p = b; | |
2680 | for (; p < b + n; ++p) | |
2681 | scm_gc_unregister_root (p); | |
2682 | } | |
2683 | ||
c45acc34 JB |
2684 | int terminating; |
2685 | ||
2686 | /* called on process termination. */ | |
e52ceaac MD |
2687 | #ifdef HAVE_ATEXIT |
2688 | static void | |
2689 | cleanup (void) | |
2690 | #else | |
2691 | #ifdef HAVE_ON_EXIT | |
51157deb MD |
2692 | extern int on_exit (void (*procp) (), int arg); |
2693 | ||
e52ceaac MD |
2694 | static void |
2695 | cleanup (int status, void *arg) | |
2696 | #else | |
2697 | #error Dont know how to setup a cleanup handler on your system. | |
2698 | #endif | |
2699 | #endif | |
c45acc34 JB |
2700 | { |
2701 | terminating = 1; | |
2702 | scm_flush_all_ports (); | |
2703 | } | |
ef290276 | 2704 | |
0f2d19dd | 2705 | \f |
acb0a19c | 2706 | static int |
92c2555f | 2707 | make_initial_segment (size_t init_heap_size, scm_t_freelist *freelist) |
acb0a19c | 2708 | { |
1be6b49c | 2709 | size_t rounded_size = round_to_cluster_size (freelist, init_heap_size); |
d6884e63 | 2710 | |
a00c95d9 ML |
2711 | if (!init_heap_seg ((SCM_CELLPTR) malloc (rounded_size), |
2712 | rounded_size, | |
4c48ba06 | 2713 | freelist)) |
acb0a19c | 2714 | { |
a00c95d9 ML |
2715 | rounded_size = round_to_cluster_size (freelist, SCM_HEAP_SEG_SIZE); |
2716 | if (!init_heap_seg ((SCM_CELLPTR) malloc (rounded_size), | |
2717 | rounded_size, | |
4c48ba06 | 2718 | freelist)) |
acb0a19c MD |
2719 | return 1; |
2720 | } | |
2721 | else | |
2722 | scm_expmem = 1; | |
2723 | ||
8fef55a8 MD |
2724 | if (freelist->min_yield_fraction) |
2725 | freelist->min_yield = (freelist->heap_size * freelist->min_yield_fraction | |
b37fe1c5 | 2726 | / 100); |
8fef55a8 | 2727 | freelist->grow_heap_p = (freelist->heap_size < freelist->min_yield); |
a00c95d9 | 2728 | |
acb0a19c MD |
2729 | return 0; |
2730 | } | |
2731 | ||
2732 | \f | |
4c48ba06 | 2733 | static void |
92c2555f | 2734 | init_freelist (scm_t_freelist *freelist, |
4c48ba06 | 2735 | int span, |
c014a02e | 2736 | long cluster_size, |
8fef55a8 | 2737 | int min_yield) |
4c48ba06 MD |
2738 | { |
2739 | freelist->clusters = SCM_EOL; | |
2740 | freelist->cluster_size = cluster_size + 1; | |
b37fe1c5 MD |
2741 | freelist->left_to_collect = 0; |
2742 | freelist->clusters_allocated = 0; | |
8fef55a8 MD |
2743 | freelist->min_yield = 0; |
2744 | freelist->min_yield_fraction = min_yield; | |
4c48ba06 MD |
2745 | freelist->span = span; |
2746 | freelist->collected = 0; | |
1811ebce | 2747 | freelist->collected_1 = 0; |
4c48ba06 MD |
2748 | freelist->heap_size = 0; |
2749 | } | |
2750 | ||
85db4a2c DH |
2751 | |
2752 | /* Get an integer from an environment variable. */ | |
2753 | static int | |
2754 | scm_i_getenv_int (const char *var, int def) | |
2755 | { | |
2756 | char *end, *val = getenv (var); | |
2757 | long res; | |
2758 | if (!val) | |
2759 | return def; | |
2760 | res = strtol (val, &end, 10); | |
2761 | if (end == val) | |
2762 | return def; | |
2763 | return res; | |
2764 | } | |
2765 | ||
2766 | ||
4a4c9785 | 2767 | int |
85db4a2c | 2768 | scm_init_storage () |
0f2d19dd | 2769 | { |
1be6b49c ML |
2770 | unsigned long gc_trigger_1; |
2771 | unsigned long gc_trigger_2; | |
2772 | size_t init_heap_size_1; | |
2773 | size_t init_heap_size_2; | |
2774 | size_t j; | |
0f2d19dd | 2775 | |
7c33806a DH |
2776 | #if (SCM_DEBUG_CELL_ACCESSES == 1) |
2777 | scm_tc16_allocated = scm_make_smob_type ("allocated cell", 0); | |
592996c9 | 2778 | scm_set_smob_mark (scm_tc16_allocated, allocated_mark); |
7c33806a DH |
2779 | #endif /* SCM_DEBUG_CELL_ACCESSES == 1 */ |
2780 | ||
0f2d19dd JB |
2781 | j = SCM_NUM_PROTECTS; |
2782 | while (j) | |
2783 | scm_sys_protects[--j] = SCM_BOOL_F; | |
2784 | scm_block_gc = 1; | |
4a4c9785 | 2785 | |
4a4c9785 | 2786 | scm_freelist = SCM_EOL; |
4c48ba06 | 2787 | scm_freelist2 = SCM_EOL; |
85db4a2c DH |
2788 | gc_trigger_1 = scm_i_getenv_int ("GUILE_MIN_YIELD_1", scm_default_min_yield_1); |
2789 | init_freelist (&scm_master_freelist, 1, SCM_CLUSTER_SIZE_1, gc_trigger_1); | |
2790 | gc_trigger_2 = scm_i_getenv_int ("GUILE_MIN_YIELD_2", scm_default_min_yield_2); | |
2791 | init_freelist (&scm_master_freelist2, 2, SCM_CLUSTER_SIZE_2, gc_trigger_2); | |
2792 | scm_max_segment_size = scm_i_getenv_int ("GUILE_MAX_SEGMENT_SIZE", scm_default_max_segment_size); | |
4a4c9785 | 2793 | |
0f2d19dd JB |
2794 | scm_expmem = 0; |
2795 | ||
2796 | j = SCM_HEAP_SEG_SIZE; | |
2797 | scm_mtrigger = SCM_INIT_MALLOC_LIMIT; | |
92c2555f MV |
2798 | scm_heap_table = ((scm_t_heap_seg_data *) |
2799 | scm_must_malloc (sizeof (scm_t_heap_seg_data) * 2, "hplims")); | |
b6efc951 | 2800 | heap_segment_table_size = 2; |
acb0a19c | 2801 | |
d6884e63 ML |
2802 | mark_space_ptr = &mark_space_head; |
2803 | ||
85db4a2c DH |
2804 | init_heap_size_1 = scm_i_getenv_int ("GUILE_INIT_SEGMENT_SIZE_1", scm_default_init_heap_size_1); |
2805 | init_heap_size_2 = scm_i_getenv_int ("GUILE_INIT_SEGMENT_SIZE_2", scm_default_init_heap_size_2); | |
4c48ba06 MD |
2806 | if (make_initial_segment (init_heap_size_1, &scm_master_freelist) || |
2807 | make_initial_segment (init_heap_size_2, &scm_master_freelist2)) | |
4a4c9785 | 2808 | return 1; |
acb0a19c | 2809 | |
801cb5e7 | 2810 | /* scm_hplims[0] can change. do not remove scm_heap_org */ |
a00c95d9 | 2811 | scm_heap_org = CELL_UP (scm_heap_table[0].bounds[0], 1); |
acb0a19c | 2812 | |
801cb5e7 MD |
2813 | scm_c_hook_init (&scm_before_gc_c_hook, 0, SCM_C_HOOK_NORMAL); |
2814 | scm_c_hook_init (&scm_before_mark_c_hook, 0, SCM_C_HOOK_NORMAL); | |
2815 | scm_c_hook_init (&scm_before_sweep_c_hook, 0, SCM_C_HOOK_NORMAL); | |
2816 | scm_c_hook_init (&scm_after_sweep_c_hook, 0, SCM_C_HOOK_NORMAL); | |
2817 | scm_c_hook_init (&scm_after_gc_c_hook, 0, SCM_C_HOOK_NORMAL); | |
0f2d19dd JB |
2818 | |
2819 | /* Initialise the list of ports. */ | |
92c2555f MV |
2820 | scm_t_portable = (scm_t_port **) |
2821 | malloc (sizeof (scm_t_port *) * scm_t_portable_room); | |
2822 | if (!scm_t_portable) | |
0f2d19dd JB |
2823 | return 1; |
2824 | ||
a18bcd0e | 2825 | #ifdef HAVE_ATEXIT |
c45acc34 | 2826 | atexit (cleanup); |
e52ceaac MD |
2827 | #else |
2828 | #ifdef HAVE_ON_EXIT | |
2829 | on_exit (cleanup, 0); | |
2830 | #endif | |
a18bcd0e | 2831 | #endif |
0f2d19dd | 2832 | |
8960e0a0 | 2833 | scm_stand_in_procs = SCM_EOL; |
0f2d19dd | 2834 | scm_permobjs = SCM_EOL; |
00ffa0e7 | 2835 | scm_protects = scm_c_make_hash_table (31); |
6b1b030e | 2836 | scm_gc_registered_roots = scm_c_make_hash_table (31); |
d6884e63 | 2837 | |
0f2d19dd JB |
2838 | return 0; |
2839 | } | |
939794ce | 2840 | |
0f2d19dd JB |
2841 | \f |
2842 | ||
939794ce DH |
2843 | SCM scm_after_gc_hook; |
2844 | ||
939794ce DH |
2845 | static SCM gc_async; |
2846 | ||
939794ce DH |
2847 | /* The function gc_async_thunk causes the execution of the after-gc-hook. It |
2848 | * is run after the gc, as soon as the asynchronous events are handled by the | |
2849 | * evaluator. | |
2850 | */ | |
2851 | static SCM | |
2852 | gc_async_thunk (void) | |
2853 | { | |
2854 | scm_c_run_hook (scm_after_gc_hook, SCM_EOL); | |
939794ce DH |
2855 | return SCM_UNSPECIFIED; |
2856 | } | |
2857 | ||
2858 | ||
2859 | /* The function mark_gc_async is run by the scm_after_gc_c_hook at the end of | |
2860 | * the garbage collection. The only purpose of this function is to mark the | |
2861 | * gc_async (which will eventually lead to the execution of the | |
2862 | * gc_async_thunk). | |
2863 | */ | |
2864 | static void * | |
e81d98ec DH |
2865 | mark_gc_async (void * hook_data SCM_UNUSED, |
2866 | void *func_data SCM_UNUSED, | |
2867 | void *data SCM_UNUSED) | |
2868 | { | |
2869 | /* If cell access debugging is enabled, the user may choose to perform | |
2870 | * additional garbage collections after an arbitrary number of cell | |
2871 | * accesses. We don't want the scheme level after-gc-hook to be performed | |
2872 | * for each of these garbage collections for the following reason: The | |
2873 | * execution of the after-gc-hook causes cell accesses itself. Thus, if the | |
2874 | * after-gc-hook was performed with every gc, and if the gc was performed | |
2875 | * after a very small number of cell accesses, then the number of cell | |
2876 | * accesses during the execution of the after-gc-hook will suffice to cause | |
2877 | * the execution of the next gc. Then, guile would keep executing the | |
2878 | * after-gc-hook over and over again, and would never come to do other | |
2879 | * things. | |
2880 | * | |
2881 | * To overcome this problem, if cell access debugging with additional | |
2882 | * garbage collections is enabled, the after-gc-hook is never run by the | |
2883 | * garbage collecter. When running guile with cell access debugging and the | |
2884 | * execution of the after-gc-hook is desired, then it is necessary to run | |
2885 | * the hook explicitly from the user code. This has the effect, that from | |
2886 | * the scheme level point of view it seems that garbage collection is | |
2887 | * performed with a much lower frequency than it actually is. Obviously, | |
2888 | * this will not work for code that depends on a fixed one to one | |
2889 | * relationship between the execution counts of the C level garbage | |
2890 | * collection hooks and the execution count of the scheme level | |
2891 | * after-gc-hook. | |
2892 | */ | |
2893 | #if (SCM_DEBUG_CELL_ACCESSES == 1) | |
2894 | if (debug_cells_gc_interval == 0) | |
2895 | scm_system_async_mark (gc_async); | |
2896 | #else | |
939794ce | 2897 | scm_system_async_mark (gc_async); |
e81d98ec DH |
2898 | #endif |
2899 | ||
939794ce DH |
2900 | return NULL; |
2901 | } | |
2902 | ||
2903 | ||
0f2d19dd JB |
2904 | void |
2905 | scm_init_gc () | |
0f2d19dd | 2906 | { |
939794ce DH |
2907 | SCM after_gc_thunk; |
2908 | ||
fde50407 ML |
2909 | scm_after_gc_hook = scm_permanent_object (scm_make_hook (SCM_INUM0)); |
2910 | scm_c_define ("after-gc-hook", scm_after_gc_hook); | |
939794ce | 2911 | |
9a441ddb MV |
2912 | after_gc_thunk = scm_c_make_subr ("%gc-thunk", scm_tc7_subr_0, |
2913 | gc_async_thunk); | |
23670993 | 2914 | gc_async = scm_system_async (after_gc_thunk); /* protected via scm_asyncs */ |
939794ce DH |
2915 | |
2916 | scm_c_hook_add (&scm_after_gc_c_hook, mark_gc_async, NULL, 0); | |
2917 | ||
8dc9439f | 2918 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 2919 | #include "libguile/gc.x" |
8dc9439f | 2920 | #endif |
0f2d19dd | 2921 | } |
89e00824 | 2922 | |
56495472 ML |
2923 | #endif /*MARK_DEPENDENCIES*/ |
2924 | ||
89e00824 ML |
2925 | /* |
2926 | Local Variables: | |
2927 | c-file-style: "gnu" | |
2928 | End: | |
2929 | */ |