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