1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation, Inc.
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)
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.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
45 /* #define DEBUGINFO */
47 /* SECTION: This code is compiled once.
50 #ifndef MARK_DEPENDENCIES
57 #include "libguile/_scm.h"
58 #include "libguile/eval.h"
59 #include "libguile/stime.h"
60 #include "libguile/stackchk.h"
61 #include "libguile/struct.h"
62 #include "libguile/smob.h"
63 #include "libguile/unif.h"
64 #include "libguile/async.h"
65 #include "libguile/ports.h"
66 #include "libguile/root.h"
67 #include "libguile/strings.h"
68 #include "libguile/vectors.h"
69 #include "libguile/weaks.h"
70 #include "libguile/hashtab.h"
71 #include "libguile/tags.h"
73 #include "libguile/validate.h"
74 #include "libguile/gc.h"
76 #ifdef GUILE_DEBUG_MALLOC
77 #include "libguile/debug-malloc.h"
90 #define var_start(x, y) va_start(x, y)
93 #define var_start(x, y) va_start(x)
98 unsigned int scm_gc_running_p
= 0;
102 #if (SCM_DEBUG_CELL_ACCESSES == 1)
104 scm_bits_t scm_tc16_allocated
;
106 /* Set this to != 0 if every cell that is accessed shall be checked:
108 unsigned int scm_debug_cell_accesses_p
= 1;
111 /* Assert that the given object is a valid reference to a valid cell. This
112 * test involves to determine whether the object is a cell pointer, whether
113 * this pointer actually points into a heap segment and whether the cell
114 * pointed to is not a free cell.
117 scm_assert_cell_valid (SCM cell
)
119 static unsigned int already_running
= 0;
121 if (scm_debug_cell_accesses_p
&& !already_running
)
123 already_running
= 1; /* set to avoid recursion */
125 if (!scm_cellp (cell
))
127 fprintf (stderr
, "scm_assert_cell_valid: Not a cell object: %lx\n", SCM_UNPACK (cell
));
130 else if (!scm_gc_running_p
)
132 /* Dirk::FIXME:: During garbage collection there occur references to
133 free cells. This is allright during conservative marking, but
134 should not happen otherwise (I think). The case of free cells
135 accessed during conservative marking is handled in function
136 scm_mark_locations. However, there still occur accesses to free
137 cells during gc. I don't understand why this happens. If it is
138 a bug and gets fixed, the following test should also work while
141 if (SCM_FREE_CELL_P (cell
))
143 fprintf (stderr
, "scm_assert_cell_valid: Accessing free cell: %lx\n", SCM_UNPACK (cell
));
147 already_running
= 0; /* re-enable */
152 SCM_DEFINE (scm_set_debug_cell_accesses_x
, "set-debug-cell-accesses!", 1, 0, 0,
154 "If @var{flag} is @code{#f}, cell access checking is disabled.\n"
155 "If @var{flag} is @code{#t}, cell access checking is enabled.\n"
156 "This procedure only exists when the compile-time flag\n"
157 "@code{SCM_DEBUG_CELL_ACCESSES} was set to 1.")
158 #define FUNC_NAME s_scm_set_debug_cell_accesses_x
160 if (SCM_FALSEP (flag
)) {
161 scm_debug_cell_accesses_p
= 0;
162 } else if (SCM_EQ_P (flag
, SCM_BOOL_T
)) {
163 scm_debug_cell_accesses_p
= 1;
165 SCM_WRONG_TYPE_ARG (1, flag
);
167 return SCM_UNSPECIFIED
;
171 #endif /* SCM_DEBUG_CELL_ACCESSES == 1 */
175 /* {heap tuning parameters}
177 * These are parameters for controlling memory allocation. The heap
178 * is the area out of which scm_cons, and object headers are allocated.
180 * Each heap cell is 8 bytes on a 32 bit machine and 16 bytes on a
181 * 64 bit machine. The units of the _SIZE parameters are bytes.
182 * Cons pairs and object headers occupy one heap cell.
184 * SCM_INIT_HEAP_SIZE is the initial size of heap. If this much heap is
185 * allocated initially the heap will grow by half its current size
186 * each subsequent time more heap is needed.
188 * If SCM_INIT_HEAP_SIZE heap cannot be allocated initially, SCM_HEAP_SEG_SIZE
189 * will be used, and the heap will grow by SCM_HEAP_SEG_SIZE when more
190 * heap is needed. SCM_HEAP_SEG_SIZE must fit into type scm_sizet. This code
191 * is in scm_init_storage() and alloc_some_heap() in sys.c
193 * If SCM_INIT_HEAP_SIZE can be allocated initially, the heap will grow by
194 * SCM_EXPHEAP(scm_heap_size) when more heap is needed.
196 * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap
199 * INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
202 * SCM_MTRIGGER_HYSTERESIS is the amount of malloc storage that must be
203 * reclaimed by a GC triggered by must_malloc. If less than this is
204 * reclaimed, the trigger threshold is raised. [I don't know what a
205 * good value is. I arbitrarily chose 1/10 of the INIT_MALLOC_LIMIT to
206 * work around a oscillation that caused almost constant GC.]
210 * Heap size 45000 and 40% min yield gives quick startup and no extra
211 * heap allocation. Having higher values on min yield may lead to
212 * large heaps, especially if code behaviour is varying its
213 * maximum consumption between different freelists.
216 #define SCM_DATA_CELLS2CARDS(n) (((n) + SCM_GC_CARD_N_DATA_CELLS - 1) / SCM_GC_CARD_N_DATA_CELLS)
217 #define SCM_CARDS_PER_CLUSTER SCM_DATA_CELLS2CARDS (2000L)
218 #define SCM_CLUSTER_SIZE_1 (SCM_CARDS_PER_CLUSTER * SCM_GC_CARD_N_DATA_CELLS)
219 int scm_default_init_heap_size_1
= (((SCM_DATA_CELLS2CARDS (45000L) + SCM_CARDS_PER_CLUSTER
- 1)
220 / SCM_CARDS_PER_CLUSTER
) * SCM_GC_CARD_SIZE
);
221 int scm_default_min_yield_1
= 40;
223 #define SCM_CLUSTER_SIZE_2 (SCM_CARDS_PER_CLUSTER * (SCM_GC_CARD_N_DATA_CELLS / 2))
224 int scm_default_init_heap_size_2
= (((SCM_DATA_CELLS2CARDS (2500L * 2) + SCM_CARDS_PER_CLUSTER
- 1)
225 / SCM_CARDS_PER_CLUSTER
) * SCM_GC_CARD_SIZE
);
226 /* The following value may seem large, but note that if we get to GC at
227 * all, this means that we have a numerically intensive application
229 int scm_default_min_yield_2
= 40;
231 int scm_default_max_segment_size
= 2097000L;/* a little less (adm) than 2 Mb */
233 #define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_CARD_SIZE)
235 # define SCM_HEAP_SEG_SIZE 32768L
238 # define SCM_HEAP_SEG_SIZE (7000L * sizeof (scm_cell))
240 # define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_cell))
243 /* Make heap grow with factor 1.5 */
244 #define SCM_EXPHEAP(scm_heap_size) (scm_heap_size / 2)
245 #define SCM_INIT_MALLOC_LIMIT 100000
246 #define SCM_MTRIGGER_HYSTERESIS (SCM_INIT_MALLOC_LIMIT/10)
248 /* CELL_UP and CELL_DN are used by scm_init_heap_seg to find (scm_cell * span)
249 aligned inner bounds for allocated storage */
252 /*in 386 protected mode we must only adjust the offset */
253 # define CELL_UP(p, span) MK_FP(FP_SEG(p), ~(8*(span)-1)&(FP_OFF(p)+8*(span)-1))
254 # define CELL_DN(p, span) MK_FP(FP_SEG(p), ~(8*(span)-1)&FP_OFF(p))
257 # define CELL_UP(p, span) (SCM_CELLPTR)(~(span) & ((long)(p)+(span)))
258 # define CELL_DN(p, span) (SCM_CELLPTR)(~(span) & (long)(p))
260 # define CELL_UP(p, span) (SCM_CELLPTR)(~(sizeof(scm_cell)*(span)-1L) & ((long)(p)+sizeof(scm_cell)*(span)-1L))
261 # define CELL_DN(p, span) (SCM_CELLPTR)(~(sizeof(scm_cell)*(span)-1L) & (long)(p))
265 #define DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_cell) - 1) & SCM_UNPACK (x)) == 0)
267 #define ALIGNMENT_SLACK(freelist) (SCM_GC_CARD_SIZE - 1)
268 #define CLUSTER_SIZE_IN_BYTES(freelist) \
269 (((freelist)->cluster_size / (SCM_GC_CARD_N_DATA_CELLS / (freelist)->span)) * SCM_GC_CARD_SIZE)
275 typedef struct scm_freelist_t
{
276 /* collected cells */
278 /* number of cells left to collect before cluster is full */
279 unsigned int left_to_collect
;
280 /* number of clusters which have been allocated */
281 unsigned int clusters_allocated
;
282 /* a list of freelists, each of size cluster_size,
283 * except the last one which may be shorter
287 /* this is the number of objects in each cluster, including the spine cell */
289 /* indicates that we should grow heap instead of GC:ing
292 /* minimum yield on this list in order not to grow the heap
295 /* defines min_yield as percent of total heap size
297 int min_yield_fraction
;
298 /* number of cells per object on this list */
300 /* number of collected cells during last GC */
302 /* number of collected cells during penultimate GC */
304 /* total number of cells in heap segments
305 * belonging to this list.
310 SCM scm_freelist
= SCM_EOL
;
311 scm_freelist_t scm_master_freelist
= {
312 SCM_EOL
, 0, 0, SCM_EOL
, 0, SCM_CLUSTER_SIZE_1
, 0, 0, 0, 1, 0, 0
314 SCM scm_freelist2
= SCM_EOL
;
315 scm_freelist_t scm_master_freelist2
= {
316 SCM_EOL
, 0, 0, SCM_EOL
, 0, SCM_CLUSTER_SIZE_2
, 0, 0, 0, 2, 0, 0
320 * is the number of bytes of must_malloc allocation needed to trigger gc.
322 unsigned long scm_mtrigger
;
325 * If set, don't expand the heap. Set only during gc, during which no allocation
326 * is supposed to take place anyway.
328 int scm_gc_heap_lock
= 0;
331 * Don't pause for collection if this is set -- just
334 int scm_block_gc
= 1;
336 /* During collection, this accumulates objects holding
339 SCM scm_weak_vectors
;
341 /* During collection, this accumulates structures which are to be freed.
343 SCM scm_structs_to_free
;
345 /* GC Statistics Keeping
347 unsigned long scm_cells_allocated
= 0;
348 long scm_mallocated
= 0;
349 unsigned long scm_gc_cells_collected
;
350 unsigned long scm_gc_yield
;
351 static unsigned long scm_gc_yield_1
= 0; /* previous GC yield */
352 unsigned long scm_gc_malloc_collected
;
353 unsigned long scm_gc_ports_collected
;
354 unsigned long scm_gc_time_taken
= 0;
355 static unsigned long t_before_gc
;
356 static unsigned long t_before_sweep
;
357 unsigned long scm_gc_mark_time_taken
= 0;
358 unsigned long scm_gc_sweep_time_taken
= 0;
359 unsigned long scm_gc_times
= 0;
360 unsigned long scm_gc_cells_swept
= 0;
361 double scm_gc_cells_marked_acc
= 0.;
362 double scm_gc_cells_swept_acc
= 0.;
364 SCM_SYMBOL (sym_cells_allocated
, "cells-allocated");
365 SCM_SYMBOL (sym_heap_size
, "cell-heap-size");
366 SCM_SYMBOL (sym_mallocated
, "bytes-malloced");
367 SCM_SYMBOL (sym_mtrigger
, "gc-malloc-threshold");
368 SCM_SYMBOL (sym_heap_segments
, "cell-heap-segments");
369 SCM_SYMBOL (sym_gc_time_taken
, "gc-time-taken");
370 SCM_SYMBOL (sym_gc_mark_time_taken
, "gc-mark-time-taken");
371 SCM_SYMBOL (sym_gc_sweep_time_taken
, "gc-sweep-time-taken");
372 SCM_SYMBOL (sym_times
, "gc-times");
373 SCM_SYMBOL (sym_cells_marked
, "cells-marked");
374 SCM_SYMBOL (sym_cells_swept
, "cells-swept");
376 typedef struct scm_heap_seg_data_t
378 /* lower and upper bounds of the segment */
379 SCM_CELLPTR bounds
[2];
381 /* address of the head-of-freelist pointer for this segment's cells.
382 All segments usually point to the same one, scm_freelist. */
383 scm_freelist_t
*freelist
;
385 /* number of cells per object in this segment */
387 } scm_heap_seg_data_t
;
391 static scm_sizet
init_heap_seg (SCM_CELLPTR
, scm_sizet
, scm_freelist_t
*);
393 typedef enum { return_on_error
, abort_on_error
} policy_on_error
;
394 static void alloc_some_heap (scm_freelist_t
*, policy_on_error
);
397 #define SCM_HEAP_SIZE \
398 (scm_master_freelist.heap_size + scm_master_freelist2.heap_size)
399 #define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
401 #define BVEC_GROW_SIZE 256
402 #define BVEC_GROW_SIZE_IN_LIMBS (SCM_GC_CARD_BVEC_SIZE_IN_LIMBS * BVEC_GROW_SIZE)
403 #define BVEC_GROW_SIZE_IN_BYTES (BVEC_GROW_SIZE_IN_LIMBS * sizeof (scm_c_bvec_limb_t))
405 /* mark space allocation */
407 typedef struct scm_mark_space_t
409 scm_c_bvec_limb_t
*bvec_space
;
410 struct scm_mark_space_t
*next
;
413 static scm_mark_space_t
*current_mark_space
;
414 static scm_mark_space_t
**mark_space_ptr
;
415 static int current_mark_space_offset
;
416 static scm_mark_space_t
*mark_space_head
;
418 static scm_c_bvec_limb_t
*
420 #define FUNC_NAME "get_bvec"
422 scm_c_bvec_limb_t
*res
;
424 if (!current_mark_space
)
426 SCM_SYSCALL (current_mark_space
= (scm_mark_space_t
*) malloc (sizeof (scm_mark_space_t
)));
427 if (!current_mark_space
)
428 SCM_MISC_ERROR ("could not grow heap", SCM_EOL
);
430 current_mark_space
->bvec_space
= NULL
;
431 current_mark_space
->next
= NULL
;
433 *mark_space_ptr
= current_mark_space
;
434 mark_space_ptr
= &(current_mark_space
->next
);
439 if (!(current_mark_space
->bvec_space
))
441 SCM_SYSCALL (current_mark_space
->bvec_space
=
442 (scm_c_bvec_limb_t
*) calloc (BVEC_GROW_SIZE_IN_BYTES
, 1));
443 if (!(current_mark_space
->bvec_space
))
444 SCM_MISC_ERROR ("could not grow heap", SCM_EOL
);
446 current_mark_space_offset
= 0;
451 if (current_mark_space_offset
== BVEC_GROW_SIZE_IN_LIMBS
)
453 current_mark_space
= NULL
;
458 res
= current_mark_space
->bvec_space
+ current_mark_space_offset
;
459 current_mark_space_offset
+= SCM_GC_CARD_BVEC_SIZE_IN_LIMBS
;
469 scm_mark_space_t
*ms
;
471 for (ms
= mark_space_head
; ms
; ms
= ms
->next
)
472 memset (ms
->bvec_space
, 0, BVEC_GROW_SIZE_IN_BYTES
);
477 /* Debugging functions. */
479 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
481 /* Return the number of the heap segment containing CELL. */
487 for (i
= 0; i
< scm_n_heap_segs
; i
++)
488 if (SCM_PTR_LE (scm_heap_table
[i
].bounds
[0], SCM2PTR (cell
))
489 && SCM_PTR_GT (scm_heap_table
[i
].bounds
[1], SCM2PTR (cell
)))
491 fprintf (stderr
, "which_seg: can't find segment containing cell %lx\n",
498 map_free_list (scm_freelist_t
*master
, SCM freelist
)
500 int last_seg
= -1, count
= 0;
503 for (f
= freelist
; !SCM_NULLP (f
); f
= SCM_FREE_CELL_CDR (f
))
505 int this_seg
= which_seg (f
);
507 if (this_seg
!= last_seg
)
510 fprintf (stderr
, " %5d %d-cells in segment %d\n",
511 count
, master
->span
, last_seg
);
518 fprintf (stderr
, " %5d %d-cells in segment %d\n",
519 count
, master
->span
, last_seg
);
522 SCM_DEFINE (scm_map_free_list
, "map-free-list", 0, 0, 0,
524 "Print debugging information about the free-list.\n"
525 "@code{map-free-list} is only included in\n"
526 "@code{--enable-guile-debug} builds of Guile.")
527 #define FUNC_NAME s_scm_map_free_list
530 fprintf (stderr
, "%d segments total (%d:%d",
532 scm_heap_table
[0].span
,
533 scm_heap_table
[0].bounds
[1] - scm_heap_table
[0].bounds
[0]);
534 for (i
= 1; i
< scm_n_heap_segs
; i
++)
535 fprintf (stderr
, ", %d:%d",
536 scm_heap_table
[i
].span
,
537 scm_heap_table
[i
].bounds
[1] - scm_heap_table
[i
].bounds
[0]);
538 fprintf (stderr
, ")\n");
539 map_free_list (&scm_master_freelist
, scm_freelist
);
540 map_free_list (&scm_master_freelist2
, scm_freelist2
);
543 return SCM_UNSPECIFIED
;
547 static int last_cluster
;
548 static int last_size
;
551 free_list_length (char *title
, int i
, SCM freelist
)
555 for (ls
= freelist
; !SCM_NULLP (ls
); ls
= SCM_FREE_CELL_CDR (ls
))
556 if (SCM_FREE_CELL_P (ls
))
560 fprintf (stderr
, "bad cell in %s at position %d\n", title
, n
);
567 if (last_cluster
== i
- 1)
568 fprintf (stderr
, "\t%d\n", last_size
);
570 fprintf (stderr
, "-%d\t%d\n", i
- 1, last_size
);
573 fprintf (stderr
, "%s %d", title
, i
);
575 fprintf (stderr
, "%s\t%d\n", title
, n
);
583 free_list_lengths (char *title
, scm_freelist_t
*master
, SCM freelist
)
586 int i
= 0, len
, n
= 0;
587 fprintf (stderr
, "%s\n\n", title
);
588 n
+= free_list_length ("free list", -1, freelist
);
589 for (clusters
= master
->clusters
;
590 SCM_NNULLP (clusters
);
591 clusters
= SCM_CDR (clusters
))
593 len
= free_list_length ("cluster", i
++, SCM_CAR (clusters
));
596 if (last_cluster
== i
- 1)
597 fprintf (stderr
, "\t%d\n", last_size
);
599 fprintf (stderr
, "-%d\t%d\n", i
- 1, last_size
);
600 fprintf (stderr
, "\ntotal %d objects\n\n", n
);
603 SCM_DEFINE (scm_free_list_length
, "free-list-length", 0, 0, 0,
605 "Print debugging information about the free-list.\n"
606 "@code{free-list-length} is only included in\n"
607 "@code{--enable-guile-debug} builds of Guile.")
608 #define FUNC_NAME s_scm_free_list_length
610 free_list_lengths ("1-cells", &scm_master_freelist
, scm_freelist
);
611 free_list_lengths ("2-cells", &scm_master_freelist2
, scm_freelist2
);
612 return SCM_UNSPECIFIED
;
618 #ifdef GUILE_DEBUG_FREELIST
620 /* Non-zero if freelist debugging is in effect. Set this via
621 `gc-set-debug-check-freelist!'. */
622 static int scm_debug_check_freelist
= 0;
624 /* Number of calls to SCM_NEWCELL since startup. */
625 static unsigned long scm_newcell_count
;
626 static unsigned long scm_newcell2_count
;
628 /* Search freelist for anything that isn't marked as a free cell.
629 Abort if we find something. */
631 scm_check_freelist (SCM freelist
)
636 for (f
= freelist
; !SCM_NULLP (f
); f
= SCM_FREE_CELL_CDR (f
), i
++)
637 if (!SCM_FREE_CELL_P (f
))
639 fprintf (stderr
, "Bad cell in freelist on newcell %lu: %d'th elt\n",
640 scm_newcell_count
, i
);
645 SCM_DEFINE (scm_gc_set_debug_check_freelist_x
, "gc-set-debug-check-freelist!", 1, 0, 0,
647 "If @var{flag} is @code{#t}, check the freelist for consistency\n"
648 "on each cell allocation. This procedure only exists when the\n"
649 "@code{GUILE_DEBUG_FREELIST} compile-time flag was selected.")
650 #define FUNC_NAME s_scm_gc_set_debug_check_freelist_x
652 /* [cmm] I did a double-take when I read this code the first time.
654 SCM_VALIDATE_BOOL_COPY (1, flag
, scm_debug_check_freelist
);
655 return SCM_UNSPECIFIED
;
661 scm_debug_newcell (void)
666 if (scm_debug_check_freelist
)
668 scm_check_freelist (scm_freelist
);
672 /* The rest of this is supposed to be identical to the SCM_NEWCELL
674 if (SCM_NULLP (scm_freelist
))
676 new = scm_gc_for_newcell (&scm_master_freelist
, &scm_freelist
);
677 SCM_GC_SET_ALLOCATED (new);
682 scm_freelist
= SCM_FREE_CELL_CDR (scm_freelist
);
683 SCM_GC_SET_ALLOCATED (new);
690 scm_debug_newcell2 (void)
694 scm_newcell2_count
++;
695 if (scm_debug_check_freelist
)
697 scm_check_freelist (scm_freelist2
);
701 /* The rest of this is supposed to be identical to the SCM_NEWCELL
703 if (SCM_NULLP (scm_freelist2
))
705 new = scm_gc_for_newcell (&scm_master_freelist2
, &scm_freelist2
);
706 SCM_GC_SET_ALLOCATED (new);
711 scm_freelist2
= SCM_FREE_CELL_CDR (scm_freelist2
);
712 SCM_GC_SET_ALLOCATED (new);
718 #endif /* GUILE_DEBUG_FREELIST */
723 master_cells_allocated (scm_freelist_t
*master
)
725 /* the '- 1' below is to ignore the cluster spine cells. */
726 int objects
= master
->clusters_allocated
* (master
->cluster_size
- 1);
727 if (SCM_NULLP (master
->clusters
))
728 objects
-= master
->left_to_collect
;
729 return master
->span
* objects
;
733 freelist_length (SCM freelist
)
736 for (n
= 0; !SCM_NULLP (freelist
); freelist
= SCM_FREE_CELL_CDR (freelist
))
742 compute_cells_allocated ()
744 return (scm_cells_allocated
745 + master_cells_allocated (&scm_master_freelist
)
746 + master_cells_allocated (&scm_master_freelist2
)
747 - scm_master_freelist
.span
* freelist_length (scm_freelist
)
748 - scm_master_freelist2
.span
* freelist_length (scm_freelist2
));
751 /* {Scheme Interface to GC}
754 SCM_DEFINE (scm_gc_stats
, "gc-stats", 0, 0, 0,
756 "Return an association list of statistics about Guile's current\n"
758 #define FUNC_NAME s_scm_gc_stats
763 long int local_scm_mtrigger
;
764 long int local_scm_mallocated
;
765 long int local_scm_heap_size
;
766 long int local_scm_cells_allocated
;
767 long int local_scm_gc_time_taken
;
768 long int local_scm_gc_times
;
769 long int local_scm_gc_mark_time_taken
;
770 long int local_scm_gc_sweep_time_taken
;
771 double local_scm_gc_cells_swept
;
772 double local_scm_gc_cells_marked
;
782 for (i
= scm_n_heap_segs
; i
--; )
783 heap_segs
= scm_cons (scm_cons (scm_ulong2num ((unsigned long)scm_heap_table
[i
].bounds
[1]),
784 scm_ulong2num ((unsigned long)scm_heap_table
[i
].bounds
[0])),
786 if (scm_n_heap_segs
!= n
)
791 /* Below, we cons to produce the resulting list. We want a snapshot of
792 * the heap situation before consing.
794 local_scm_mtrigger
= scm_mtrigger
;
795 local_scm_mallocated
= scm_mallocated
;
796 local_scm_heap_size
= SCM_HEAP_SIZE
;
797 local_scm_cells_allocated
= compute_cells_allocated ();
798 local_scm_gc_time_taken
= scm_gc_time_taken
;
799 local_scm_gc_mark_time_taken
= scm_gc_mark_time_taken
;
800 local_scm_gc_sweep_time_taken
= scm_gc_sweep_time_taken
;
801 local_scm_gc_times
= scm_gc_times
;
802 local_scm_gc_cells_swept
= scm_gc_cells_swept_acc
;
803 local_scm_gc_cells_marked
= scm_gc_cells_marked_acc
;
805 answer
= scm_listify (scm_cons (sym_gc_time_taken
, scm_ulong2num (local_scm_gc_time_taken
)),
806 scm_cons (sym_cells_allocated
, scm_ulong2num (local_scm_cells_allocated
)),
807 scm_cons (sym_heap_size
, scm_ulong2num (local_scm_heap_size
)),
808 scm_cons (sym_mallocated
, scm_ulong2num (local_scm_mallocated
)),
809 scm_cons (sym_mtrigger
, scm_ulong2num (local_scm_mtrigger
)),
810 scm_cons (sym_times
, scm_ulong2num (local_scm_gc_times
)),
811 scm_cons (sym_gc_mark_time_taken
, scm_ulong2num (local_scm_gc_mark_time_taken
)),
812 scm_cons (sym_gc_sweep_time_taken
, scm_ulong2num (local_scm_gc_sweep_time_taken
)),
813 scm_cons (sym_cells_marked
, scm_dbl2big (local_scm_gc_cells_marked
)),
814 scm_cons (sym_cells_swept
, scm_dbl2big (local_scm_gc_cells_swept
)),
815 scm_cons (sym_heap_segments
, heap_segs
),
824 gc_start_stats (const char *what
)
826 t_before_gc
= scm_c_get_internal_run_time ();
827 scm_gc_cells_swept
= 0;
828 scm_gc_cells_collected
= 0;
829 scm_gc_yield_1
= scm_gc_yield
;
830 scm_gc_yield
= (scm_cells_allocated
831 + master_cells_allocated (&scm_master_freelist
)
832 + master_cells_allocated (&scm_master_freelist2
));
833 scm_gc_malloc_collected
= 0;
834 scm_gc_ports_collected
= 0;
841 unsigned long t
= scm_c_get_internal_run_time ();
842 scm_gc_time_taken
+= (t
- t_before_gc
);
843 scm_gc_sweep_time_taken
+= (t
- t_before_sweep
);
846 scm_gc_cells_marked_acc
+= scm_gc_cells_swept
- scm_gc_cells_collected
;
847 scm_gc_cells_swept_acc
+= scm_gc_cells_swept
;
851 SCM_DEFINE (scm_object_address
, "object-address", 1, 0, 0,
853 "Return an integer that for the lifetime of @var{obj} is uniquely\n"
854 "returned by this function for @var{obj}")
855 #define FUNC_NAME s_scm_object_address
857 return scm_ulong2num ((unsigned long) SCM_UNPACK (obj
));
862 SCM_DEFINE (scm_gc
, "gc", 0, 0, 0,
864 "Scans all of SCM objects and reclaims for further use those that are\n"
865 "no longer accessible.")
866 #define FUNC_NAME s_scm_gc
871 return SCM_UNSPECIFIED
;
877 /* {C Interface For When GC is Triggered}
881 adjust_min_yield (scm_freelist_t
*freelist
)
883 /* min yield is adjusted upwards so that next predicted total yield
884 * (allocated cells actually freed by GC) becomes
885 * `min_yield_fraction' of total heap size. Note, however, that
886 * the absolute value of min_yield will correspond to `collected'
887 * on one master (the one which currently is triggering GC).
889 * The reason why we look at total yield instead of cells collected
890 * on one list is that we want to take other freelists into account.
891 * On this freelist, we know that (local) yield = collected cells,
892 * but that's probably not the case on the other lists.
894 * (We might consider computing a better prediction, for example
895 * by computing an average over multiple GC:s.)
897 if (freelist
->min_yield_fraction
)
899 /* Pick largest of last two yields. */
900 int delta
= ((SCM_HEAP_SIZE
* freelist
->min_yield_fraction
/ 100)
901 - (long) SCM_MAX (scm_gc_yield_1
, scm_gc_yield
));
903 fprintf (stderr
, " after GC = %d, delta = %d\n",
908 freelist
->min_yield
+= delta
;
913 /* When we get POSIX threads support, the master will be global and
914 * common while the freelist will be individual for each thread.
918 scm_gc_for_newcell (scm_freelist_t
*master
, SCM
*freelist
)
924 if (SCM_NULLP (master
->clusters
))
926 if (master
->grow_heap_p
|| scm_block_gc
)
928 /* In order to reduce gc frequency, try to allocate a new heap
929 * segment first, even if gc might find some free cells. If we
930 * can't obtain a new heap segment, we will try gc later.
932 master
->grow_heap_p
= 0;
933 alloc_some_heap (master
, return_on_error
);
935 if (SCM_NULLP (master
->clusters
))
937 /* The heap was not grown, either because it wasn't scheduled to
938 * grow, or because there was not enough memory available. In
939 * both cases we have to try gc to get some free cells.
942 fprintf (stderr
, "allocated = %d, ",
944 + master_cells_allocated (&scm_master_freelist
)
945 + master_cells_allocated (&scm_master_freelist2
));
948 adjust_min_yield (master
);
949 if (SCM_NULLP (master
->clusters
))
951 /* gc could not free any cells. Now, we _must_ allocate a
952 * new heap segment, because there is no other possibility
953 * to provide a new cell for the caller.
955 alloc_some_heap (master
, abort_on_error
);
959 cell
= SCM_CAR (master
->clusters
);
960 master
->clusters
= SCM_CDR (master
->clusters
);
961 ++master
->clusters_allocated
;
963 while (SCM_NULLP (cell
));
965 #ifdef GUILE_DEBUG_FREELIST
966 scm_check_freelist (cell
);
970 *freelist
= SCM_FREE_CELL_CDR (cell
);
976 /* This is a support routine which can be used to reserve a cluster
977 * for some special use, such as debugging. It won't be useful until
978 * free cells are preserved between garbage collections.
982 scm_alloc_cluster (scm_freelist_t
*master
)
985 cell
= scm_gc_for_newcell (master
, &freelist
);
986 SCM_SETCDR (cell
, freelist
);
992 scm_c_hook_t scm_before_gc_c_hook
;
993 scm_c_hook_t scm_before_mark_c_hook
;
994 scm_c_hook_t scm_before_sweep_c_hook
;
995 scm_c_hook_t scm_after_sweep_c_hook
;
996 scm_c_hook_t scm_after_gc_c_hook
;
1000 scm_igc (const char *what
)
1005 scm_c_hook_run (&scm_before_gc_c_hook
, 0);
1008 SCM_NULLP (scm_freelist
)
1010 : (SCM_NULLP (scm_freelist2
) ? "o" : "m"));
1012 /* During the critical section, only the current thread may run. */
1013 SCM_CRITICAL_SECTION_START
;
1015 /* fprintf (stderr, "gc: %s\n", what); */
1017 if (!scm_stack_base
|| scm_block_gc
)
1023 gc_start_stats (what
);
1025 if (scm_mallocated
< 0)
1026 /* The byte count of allocated objects has underflowed. This is
1027 probably because you forgot to report the sizes of objects you
1028 have allocated, by calling scm_done_malloc or some such. When
1029 the GC freed them, it subtracted their size from
1030 scm_mallocated, which underflowed. */
1033 if (scm_gc_heap_lock
)
1034 /* We've invoked the collector while a GC is already in progress.
1035 That should never happen. */
1040 /* flush dead entries from the continuation stack */
1045 elts
= SCM_VELTS (scm_continuation_stack
);
1046 bound
= SCM_VECTOR_LENGTH (scm_continuation_stack
);
1047 x
= SCM_INUM (scm_continuation_stack_ptr
);
1050 elts
[x
] = SCM_BOOL_F
;
1055 scm_c_hook_run (&scm_before_mark_c_hook
, 0);
1057 clear_mark_space ();
1061 /* Mark objects on the C stack. */
1062 SCM_FLUSH_REGISTER_WINDOWS
;
1063 /* This assumes that all registers are saved into the jmp_buf */
1064 setjmp (scm_save_regs_gc_mark
);
1065 scm_mark_locations ((SCM_STACKITEM
*) scm_save_regs_gc_mark
,
1066 ( (scm_sizet
) (sizeof (SCM_STACKITEM
) - 1 +
1067 sizeof scm_save_regs_gc_mark
)
1068 / sizeof (SCM_STACKITEM
)));
1071 scm_sizet stack_len
= scm_stack_size (scm_stack_base
);
1072 #ifdef SCM_STACK_GROWS_UP
1073 scm_mark_locations (scm_stack_base
, stack_len
);
1075 scm_mark_locations (scm_stack_base
- stack_len
, stack_len
);
1079 #else /* USE_THREADS */
1081 /* Mark every thread's stack and registers */
1082 scm_threads_mark_stacks ();
1084 #endif /* USE_THREADS */
1086 j
= SCM_NUM_PROTECTS
;
1088 scm_gc_mark (scm_sys_protects
[j
]);
1090 /* FIXME: we should have a means to register C functions to be run
1091 * in different phases of GC
1093 scm_mark_subr_table ();
1096 scm_gc_mark (scm_root
->handle
);
1099 t_before_sweep
= scm_c_get_internal_run_time ();
1100 scm_gc_mark_time_taken
+= (t_before_sweep
- t_before_gc
);
1102 scm_c_hook_run (&scm_before_sweep_c_hook
, 0);
1106 scm_c_hook_run (&scm_after_sweep_c_hook
, 0);
1111 SCM_CRITICAL_SECTION_END
;
1112 scm_c_hook_run (&scm_after_gc_c_hook
, 0);
1121 #define MARK scm_gc_mark
1122 #define FNAME "scm_gc_mark"
1124 #endif /*!MARK_DEPENDENCIES*/
1126 /* Mark an object precisely.
1130 #define FUNC_NAME FNAME
1134 scm_bits_t cell_type
;
1136 #ifndef MARK_DEPENDENCIES
1137 # define RECURSE scm_gc_mark
1139 /* go through the usual marking, but not for self-cycles. */
1140 # define RECURSE(x) do { if ((x) != p) scm_gc_mark (x); } while (0)
1144 #ifdef MARK_DEPENDENCIES
1145 goto gc_mark_loop_first_time
;
1148 /* A simple hack for debugging. Chose the second branch to get a
1149 meaningful backtrace for crashes inside the GC.
1152 #define goto_gc_mark_loop goto gc_mark_loop
1153 #define goto_gc_mark_nimp goto gc_mark_nimp
1155 #define goto_gc_mark_loop RECURSE(ptr); return
1156 #define goto_gc_mark_nimp RECURSE(ptr); return
1165 #ifdef MARK_DEPENDENCIES
1166 if (SCM_EQ_P (ptr
, p
))
1172 gc_mark_loop_first_time
:
1175 #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST))
1176 /* We are in debug mode. Check the ptr exhaustively. */
1177 if (!scm_cellp (ptr
))
1178 SCM_MISC_ERROR ("rogue pointer in heap", SCM_EOL
);
1180 /* In non-debug mode, do at least some cheap testing. */
1181 if (!SCM_CELLP (ptr
))
1182 SCM_MISC_ERROR ("rogue pointer in heap", SCM_EOL
);
1185 #ifndef MARK_DEPENDENCIES
1187 if (SCM_GCMARKP (ptr
))
1190 SCM_SETGCMARK (ptr
);
1194 cell_type
= SCM_GC_CELL_TYPE (ptr
);
1195 switch (SCM_ITAG7 (cell_type
))
1197 case scm_tcs_cons_nimcar
:
1198 if (SCM_IMP (SCM_CDR (ptr
)))
1200 ptr
= SCM_CAR (ptr
);
1203 RECURSE (SCM_CAR (ptr
));
1204 ptr
= SCM_CDR (ptr
);
1206 case scm_tcs_cons_imcar
:
1207 ptr
= SCM_CDR (ptr
);
1210 RECURSE (SCM_SETTER (ptr
));
1211 ptr
= SCM_PROCEDURE (ptr
);
1213 case scm_tcs_cons_gloc
:
1215 /* Dirk:FIXME:: The following code is super ugly: ptr may be a
1216 * struct or a gloc. If it is a gloc, the cell word #0 of ptr
1217 * is the address of a scm_tc16_variable smob. If it is a
1218 * struct, the cell word #0 of ptr is a pointer to a struct
1219 * vtable data region. (The fact that these are accessed in
1220 * the same way restricts the possibilites to change the data
1221 * layout of structs or heap cells.) To discriminate between
1222 * the two, it is guaranteed that the scm_vtable_index_vcell
1223 * element of the prospective vtable is always zero. For a
1224 * gloc, this location has the CDR of the variable smob, which
1225 * is guaranteed to be non-zero.
1227 scm_bits_t word0
= SCM_CELL_WORD_0 (ptr
) - scm_tc3_cons_gloc
;
1228 scm_bits_t
* vtable_data
= (scm_bits_t
*) word0
; /* access as struct */
1229 if (vtable_data
[scm_vtable_index_vcell
] != 0)
1232 SCM gloc_car
= SCM_PACK (word0
);
1234 ptr
= SCM_CDR (ptr
);
1239 /* ptr is a struct */
1240 SCM layout
= SCM_PACK (vtable_data
[scm_vtable_index_layout
]);
1241 int len
= SCM_SYMBOL_LENGTH (layout
);
1242 char * fields_desc
= SCM_SYMBOL_CHARS (layout
);
1243 scm_bits_t
* struct_data
= (scm_bits_t
*) SCM_STRUCT_DATA (ptr
);
1245 if (vtable_data
[scm_struct_i_flags
] & SCM_STRUCTF_ENTITY
)
1247 RECURSE (SCM_PACK (struct_data
[scm_struct_i_procedure
]));
1248 RECURSE (SCM_PACK (struct_data
[scm_struct_i_setter
]));
1254 for (x
= 0; x
< len
- 2; x
+= 2, ++struct_data
)
1255 if (fields_desc
[x
] == 'p')
1256 RECURSE (SCM_PACK (*struct_data
));
1257 if (fields_desc
[x
] == 'p')
1259 if (SCM_LAYOUT_TAILP (fields_desc
[x
+ 1]))
1260 for (x
= *struct_data
++; x
; --x
, ++struct_data
)
1261 RECURSE (SCM_PACK (*struct_data
));
1263 RECURSE (SCM_PACK (*struct_data
));
1267 ptr
= SCM_PACK (vtable_data
[scm_vtable_index_vtable
]);
1272 case scm_tcs_closures
:
1273 if (SCM_IMP (SCM_ENV (ptr
)))
1275 ptr
= SCM_CLOSCAR (ptr
);
1278 RECURSE (SCM_CLOSCAR (ptr
));
1279 ptr
= SCM_ENV (ptr
);
1281 case scm_tc7_vector
:
1282 i
= SCM_VECTOR_LENGTH (ptr
);
1286 if (SCM_NIMP (SCM_VELTS (ptr
)[i
]))
1287 RECURSE (SCM_VELTS (ptr
)[i
]);
1288 ptr
= SCM_VELTS (ptr
)[0];
1293 unsigned long int i
= SCM_CCLO_LENGTH (ptr
);
1294 unsigned long int j
;
1295 for (j
= 1; j
!= i
; ++j
)
1297 SCM obj
= SCM_CCLO_REF (ptr
, j
);
1301 ptr
= SCM_CCLO_REF (ptr
, 0);
1307 case scm_tc7_byvect
:
1314 #ifdef HAVE_LONG_LONGS
1315 case scm_tc7_llvect
:
1318 case scm_tc7_string
:
1321 case scm_tc7_substring
:
1322 ptr
= SCM_CDR (ptr
);
1326 SCM_WVECT_GC_CHAIN (ptr
) = scm_weak_vectors
;
1327 scm_weak_vectors
= ptr
;
1328 if (SCM_IS_WHVEC_ANY (ptr
))
1335 len
= SCM_VECTOR_LENGTH (ptr
);
1336 weak_keys
= SCM_IS_WHVEC (ptr
) || SCM_IS_WHVEC_B (ptr
);
1337 weak_values
= SCM_IS_WHVEC_V (ptr
) || SCM_IS_WHVEC_B (ptr
);
1339 for (x
= 0; x
< len
; ++x
)
1342 alist
= SCM_VELTS (ptr
)[x
];
1344 /* mark everything on the alist except the keys or
1345 * values, according to weak_values and weak_keys. */
1346 while ( SCM_CONSP (alist
)
1347 && !SCM_GCMARKP (alist
)
1348 && SCM_CONSP (SCM_CAR (alist
)))
1353 kvpair
= SCM_CAR (alist
);
1354 next_alist
= SCM_CDR (alist
);
1357 * SCM_SETGCMARK (alist);
1358 * SCM_SETGCMARK (kvpair);
1360 * It may be that either the key or value is protected by
1361 * an escaped reference to part of the spine of this alist.
1362 * If we mark the spine here, and only mark one or neither of the
1363 * key and value, they may never be properly marked.
1364 * This leads to a horrible situation in which an alist containing
1365 * freelist cells is exported.
1367 * So only mark the spines of these arrays last of all marking.
1368 * If somebody confuses us by constructing a weak vector
1369 * with a circular alist then we are hosed, but at least we
1370 * won't prematurely drop table entries.
1373 RECURSE (SCM_CAR (kvpair
));
1375 RECURSE (SCM_CDR (kvpair
));
1378 if (SCM_NIMP (alist
))
1384 case scm_tc7_symbol
:
1385 ptr
= SCM_PROP_SLOTS (ptr
);
1390 i
= SCM_PTOBNUM (ptr
);
1391 #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST))
1392 if (!(i
< scm_numptob
))
1393 SCM_MISC_ERROR ("undefined port type", SCM_EOL
);
1395 if (SCM_PTAB_ENTRY(ptr
))
1396 RECURSE (SCM_FILENAME (ptr
));
1397 if (scm_ptobs
[i
].mark
)
1399 ptr
= (scm_ptobs
[i
].mark
) (ptr
);
1406 switch (SCM_TYP16 (ptr
))
1407 { /* should be faster than going through scm_smobs */
1408 case scm_tc_free_cell
:
1409 /* printf("found free_cell %X ", ptr); fflush(stdout); */
1412 case scm_tc16_complex
:
1415 i
= SCM_SMOBNUM (ptr
);
1416 #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST))
1417 if (!(i
< scm_numsmob
))
1418 SCM_MISC_ERROR ("undefined smob type", SCM_EOL
);
1420 if (scm_smobs
[i
].mark
)
1422 ptr
= (scm_smobs
[i
].mark
) (ptr
);
1430 SCM_MISC_ERROR ("unknown type", SCM_EOL
);
1436 #ifndef MARK_DEPENDENCIES
1441 /* And here we define `scm_gc_mark_dependencies', by including this
1442 * same file in itself.
1444 #define MARK scm_gc_mark_dependencies
1445 #define FNAME "scm_gc_mark_dependencies"
1446 #define MARK_DEPENDENCIES
1448 #undef MARK_DEPENDENCIES
1453 /* Mark a Region Conservatively
1457 scm_mark_locations (SCM_STACKITEM x
[], scm_sizet n
)
1461 for (m
= 0; m
< n
; ++m
)
1463 SCM obj
= * (SCM
*) &x
[m
];
1464 if (SCM_CELLP (obj
))
1466 SCM_CELLPTR ptr
= SCM2PTR (obj
);
1468 int j
= scm_n_heap_segs
- 1;
1469 if (SCM_PTR_LE (scm_heap_table
[i
].bounds
[0], ptr
)
1470 && SCM_PTR_GT (scm_heap_table
[j
].bounds
[1], ptr
))
1477 || SCM_PTR_GT (scm_heap_table
[i
].bounds
[1], ptr
))
1479 else if (SCM_PTR_LE (scm_heap_table
[j
].bounds
[0], ptr
))
1487 if (SCM_PTR_GT (scm_heap_table
[k
].bounds
[1], ptr
))
1491 if (SCM_PTR_LE (scm_heap_table
[i
].bounds
[0], ptr
))
1496 else if (SCM_PTR_LE (scm_heap_table
[k
].bounds
[0], ptr
))
1500 if (SCM_PTR_GT (scm_heap_table
[j
].bounds
[1], ptr
))
1507 if (SCM_GC_IN_CARD_HEADERP (ptr
))
1510 if (scm_heap_table
[seg_id
].span
== 1
1511 || DOUBLECELL_ALIGNED_P (obj
))
1522 /* The function scm_cellp determines whether an SCM value can be regarded as a
1523 * pointer to a cell on the heap. Binary search is used in order to determine
1524 * the heap segment that contains the cell.
1527 scm_cellp (SCM value
)
1529 if (SCM_CELLP (value
)) {
1530 scm_cell
* ptr
= SCM2PTR (value
);
1532 unsigned int j
= scm_n_heap_segs
- 1;
1534 if (SCM_GC_IN_CARD_HEADERP (ptr
))
1538 int k
= (i
+ j
) / 2;
1539 if (SCM_PTR_GT (scm_heap_table
[k
].bounds
[1], ptr
)) {
1541 } else if (SCM_PTR_LE (scm_heap_table
[k
].bounds
[0], ptr
)) {
1546 if (SCM_PTR_LE (scm_heap_table
[i
].bounds
[0], ptr
)
1547 && SCM_PTR_GT (scm_heap_table
[i
].bounds
[1], ptr
)
1548 && (scm_heap_table
[i
].span
== 1 || DOUBLECELL_ALIGNED_P (value
))
1549 && !SCM_GC_IN_CARD_HEADERP (ptr
)
1560 gc_sweep_freelist_start (scm_freelist_t
*freelist
)
1562 freelist
->cells
= SCM_EOL
;
1563 freelist
->left_to_collect
= freelist
->cluster_size
;
1564 freelist
->clusters_allocated
= 0;
1565 freelist
->clusters
= SCM_EOL
;
1566 freelist
->clustertail
= &freelist
->clusters
;
1567 freelist
->collected_1
= freelist
->collected
;
1568 freelist
->collected
= 0;
1572 gc_sweep_freelist_finish (scm_freelist_t
*freelist
)
1575 *freelist
->clustertail
= freelist
->cells
;
1576 if (!SCM_NULLP (freelist
->cells
))
1578 SCM c
= freelist
->cells
;
1579 SCM_SET_CELL_WORD_0 (c
, SCM_FREE_CELL_CDR (c
));
1580 SCM_SET_CELL_WORD_1 (c
, SCM_EOL
);
1581 freelist
->collected
+=
1582 freelist
->span
* (freelist
->cluster_size
- freelist
->left_to_collect
);
1584 scm_gc_cells_collected
+= freelist
->collected
;
1586 /* Although freelist->min_yield is used to test freelist->collected
1587 * (which is the local GC yield for freelist), it is adjusted so
1588 * that *total* yield is freelist->min_yield_fraction of total heap
1589 * size. This means that a too low yield is compensated by more
1590 * heap on the list which is currently doing most work, which is
1591 * just what we want.
1593 collected
= SCM_MAX (freelist
->collected_1
, freelist
->collected
);
1594 freelist
->grow_heap_p
= (collected
< freelist
->min_yield
);
1597 #define NEXT_DATA_CELL(ptr, span) \
1599 scm_cell *nxt__ = CELL_UP ((char *) (ptr) + 1, (span)); \
1600 (ptr) = (SCM_GC_IN_CARD_HEADERP (nxt__) ? \
1601 CELL_UP (SCM_GC_CELL_CARD (nxt__) + SCM_GC_CARD_N_HEADER_CELLS, span) \
1607 #define FUNC_NAME "scm_gc_sweep"
1609 register SCM_CELLPTR ptr
;
1610 register SCM nfreelist
;
1611 register scm_freelist_t
*freelist
;
1619 gc_sweep_freelist_start (&scm_master_freelist
);
1620 gc_sweep_freelist_start (&scm_master_freelist2
);
1622 for (i
= 0; i
< scm_n_heap_segs
; i
++)
1624 register unsigned int left_to_collect
;
1625 register scm_sizet j
;
1627 /* Unmarked cells go onto the front of the freelist this heap
1628 segment points to. Rather than updating the real freelist
1629 pointer as we go along, we accumulate the new head in
1630 nfreelist. Then, if it turns out that the entire segment is
1631 free, we free (i.e., malloc's free) the whole segment, and
1632 simply don't assign nfreelist back into the real freelist. */
1633 freelist
= scm_heap_table
[i
].freelist
;
1634 nfreelist
= freelist
->cells
;
1635 left_to_collect
= freelist
->left_to_collect
;
1636 span
= scm_heap_table
[i
].span
;
1638 ptr
= CELL_UP (scm_heap_table
[i
].bounds
[0], span
);
1639 seg_size
= CELL_DN (scm_heap_table
[i
].bounds
[1], span
) - ptr
;
1641 /* use only data cells in seg_size */
1642 seg_size
= (seg_size
/ SCM_GC_CARD_N_CELLS
) * (SCM_GC_CARD_N_DATA_CELLS
/ span
) * span
;
1644 scm_gc_cells_swept
+= seg_size
;
1646 for (j
= seg_size
+ span
; j
-= span
; ptr
+= span
)
1650 if (SCM_GC_IN_CARD_HEADERP (ptr
))
1656 NEXT_DATA_CELL (nxt
, span
);
1663 scmptr
= PTR2SCM (ptr
);
1665 if (SCM_GCMARKP (scmptr
))
1668 switch SCM_TYP7 (scmptr
)
1670 case scm_tcs_cons_gloc
:
1672 /* Dirk:FIXME:: Again, super ugly code: scmptr may be a
1673 * struct or a gloc. See the corresponding comment in
1676 scm_bits_t word0
= (SCM_CELL_WORD_0 (scmptr
)
1677 - scm_tc3_cons_gloc
);
1678 /* access as struct */
1679 scm_bits_t
* vtable_data
= (scm_bits_t
*) word0
;
1680 if (vtable_data
[scm_vtable_index_vcell
] == 0)
1682 /* Structs need to be freed in a special order.
1683 * This is handled by GC C hooks in struct.c.
1685 SCM_SET_STRUCT_GC_CHAIN (scmptr
, scm_structs_to_free
);
1686 scm_structs_to_free
= scmptr
;
1689 /* fall through so that scmptr gets collected */
1692 case scm_tcs_cons_imcar
:
1693 case scm_tcs_cons_nimcar
:
1694 case scm_tcs_closures
:
1698 m
+= (2 + SCM_VECTOR_LENGTH (scmptr
)) * sizeof (SCM
);
1699 scm_must_free (SCM_VECTOR_BASE (scmptr
) - 2);
1701 case scm_tc7_vector
:
1703 unsigned long int length
= SCM_VECTOR_LENGTH (scmptr
);
1706 m
+= length
* sizeof (scm_bits_t
);
1707 scm_must_free (SCM_VECTOR_BASE (scmptr
));
1713 m
+= (SCM_CCLO_LENGTH (scmptr
) * sizeof (SCM
));
1714 scm_must_free (SCM_CCLO_BASE (scmptr
));
1720 unsigned long int length
= SCM_BITVECTOR_LENGTH (scmptr
);
1723 m
+= sizeof (long) * ((length
+ SCM_LONG_BIT
- 1) / SCM_LONG_BIT
);
1724 scm_must_free (SCM_BITVECTOR_BASE (scmptr
));
1728 case scm_tc7_byvect
:
1732 #ifdef HAVE_LONG_LONGS
1733 case scm_tc7_llvect
:
1738 m
+= SCM_UVECTOR_LENGTH (scmptr
) * scm_uniform_element_size (scmptr
);
1739 scm_must_free (SCM_UVECTOR_BASE (scmptr
));
1742 case scm_tc7_substring
:
1744 case scm_tc7_string
:
1745 m
+= SCM_STRING_LENGTH (scmptr
) + 1;
1746 scm_must_free (SCM_STRING_CHARS (scmptr
));
1748 case scm_tc7_symbol
:
1749 m
+= SCM_SYMBOL_LENGTH (scmptr
) + 1;
1750 scm_must_free (SCM_SYMBOL_CHARS (scmptr
));
1753 /* the various "subrs" (primitives) are never freed */
1756 if SCM_OPENP (scmptr
)
1758 int k
= SCM_PTOBNUM (scmptr
);
1759 #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST))
1760 if (!(k
< scm_numptob
))
1761 SCM_MISC_ERROR ("undefined port type", SCM_EOL
);
1763 /* Keep "revealed" ports alive. */
1764 if (scm_revealed_count (scmptr
) > 0)
1766 /* Yes, I really do mean scm_ptobs[k].free */
1767 /* rather than ftobs[k].close. .close */
1768 /* is for explicit CLOSE-PORT by user */
1769 m
+= (scm_ptobs
[k
].free
) (scmptr
);
1770 SCM_SETSTREAM (scmptr
, 0);
1771 scm_remove_from_port_table (scmptr
);
1772 scm_gc_ports_collected
++;
1773 SCM_CLR_PORT_OPEN_FLAG (scmptr
);
1777 switch SCM_TYP16 (scmptr
)
1779 case scm_tc_free_cell
:
1784 m
+= (SCM_NUMDIGS (scmptr
) * SCM_BITSPERDIG
/ SCM_CHAR_BIT
);
1785 scm_must_free (SCM_BDIGITS (scmptr
));
1787 #endif /* def SCM_BIGDIG */
1788 case scm_tc16_complex
:
1789 m
+= sizeof (scm_complex_t
);
1790 scm_must_free (SCM_COMPLEX_MEM (scmptr
));
1795 k
= SCM_SMOBNUM (scmptr
);
1796 #if (SCM_DEBUG_CELL_ACCESSES == 1) || (defined (GUILE_DEBUG_FREELIST))
1797 if (!(k
< scm_numsmob
))
1798 SCM_MISC_ERROR ("undefined smob type", SCM_EOL
);
1800 if (scm_smobs
[k
].free
)
1801 m
+= (scm_smobs
[k
].free
) (scmptr
);
1807 SCM_MISC_ERROR ("unknown type", SCM_EOL
);
1810 if (!--left_to_collect
)
1812 SCM_SET_CELL_WORD_0 (scmptr
, nfreelist
);
1813 *freelist
->clustertail
= scmptr
;
1814 freelist
->clustertail
= SCM_CDRLOC (scmptr
);
1816 nfreelist
= SCM_EOL
;
1817 freelist
->collected
+= span
* freelist
->cluster_size
;
1818 left_to_collect
= freelist
->cluster_size
;
1822 /* Stick the new cell on the front of nfreelist. It's
1823 critical that we mark this cell as freed; otherwise, the
1824 conservative collector might trace it as some other type
1826 SCM_SET_CELL_TYPE (scmptr
, scm_tc_free_cell
);
1827 SCM_SET_FREE_CELL_CDR (scmptr
, nfreelist
);
1832 #ifdef GC_FREE_SEGMENTS
1837 freelist
->heap_size
-= seg_size
;
1838 free ((char *) scm_heap_table
[i
].bounds
[0]);
1839 scm_heap_table
[i
].bounds
[0] = 0;
1840 for (j
= i
+ 1; j
< scm_n_heap_segs
; j
++)
1841 scm_heap_table
[j
- 1] = scm_heap_table
[j
];
1842 scm_n_heap_segs
-= 1;
1843 i
--; /* We need to scan the segment just moved. */
1846 #endif /* ifdef GC_FREE_SEGMENTS */
1848 /* Update the real freelist pointer to point to the head of
1849 the list of free cells we've built for this segment. */
1850 freelist
->cells
= nfreelist
;
1851 freelist
->left_to_collect
= left_to_collect
;
1854 #ifdef GUILE_DEBUG_FREELIST
1855 scm_map_free_list ();
1859 gc_sweep_freelist_finish (&scm_master_freelist
);
1860 gc_sweep_freelist_finish (&scm_master_freelist2
);
1862 /* When we move to POSIX threads private freelists should probably
1863 be GC-protected instead. */
1864 scm_freelist
= SCM_EOL
;
1865 scm_freelist2
= SCM_EOL
;
1867 scm_cells_allocated
= (SCM_HEAP_SIZE
- scm_gc_cells_collected
);
1868 scm_gc_yield
-= scm_cells_allocated
;
1869 scm_mallocated
-= m
;
1870 scm_gc_malloc_collected
= m
;
1876 /* {Front end to malloc}
1878 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
1881 * These functions provide services comparable to malloc, realloc, and
1882 * free. They should be used when allocating memory that will be under
1883 * control of the garbage collector, i.e., if the memory may be freed
1884 * during garbage collection.
1888 * Return newly malloced storage or throw an error.
1890 * The parameter WHAT is a string for error reporting.
1891 * If the threshold scm_mtrigger will be passed by this
1892 * allocation, or if the first call to malloc fails,
1893 * garbage collect -- on the presumption that some objects
1894 * using malloced storage may be collected.
1896 * The limit scm_mtrigger may be raised by this allocation.
1899 scm_must_malloc (scm_sizet size
, const char *what
)
1902 unsigned long nm
= scm_mallocated
+ size
;
1904 if (nm
<= scm_mtrigger
)
1906 SCM_SYSCALL (ptr
= malloc (size
));
1909 scm_mallocated
= nm
;
1910 #ifdef GUILE_DEBUG_MALLOC
1911 scm_malloc_register (ptr
, what
);
1919 nm
= scm_mallocated
+ size
;
1920 SCM_SYSCALL (ptr
= malloc (size
));
1923 scm_mallocated
= nm
;
1924 if (nm
> scm_mtrigger
- SCM_MTRIGGER_HYSTERESIS
) {
1925 if (nm
> scm_mtrigger
)
1926 scm_mtrigger
= nm
+ nm
/ 2;
1928 scm_mtrigger
+= scm_mtrigger
/ 2;
1930 #ifdef GUILE_DEBUG_MALLOC
1931 scm_malloc_register (ptr
, what
);
1937 scm_memory_error (what
);
1942 * is similar to scm_must_malloc.
1945 scm_must_realloc (void *where
,
1951 scm_sizet nm
= scm_mallocated
+ size
- old_size
;
1953 if (nm
<= scm_mtrigger
)
1955 SCM_SYSCALL (ptr
= realloc (where
, size
));
1958 scm_mallocated
= nm
;
1959 #ifdef GUILE_DEBUG_MALLOC
1960 scm_malloc_reregister (where
, ptr
, what
);
1968 nm
= scm_mallocated
+ size
- old_size
;
1969 SCM_SYSCALL (ptr
= realloc (where
, size
));
1972 scm_mallocated
= nm
;
1973 if (nm
> scm_mtrigger
- SCM_MTRIGGER_HYSTERESIS
) {
1974 if (nm
> scm_mtrigger
)
1975 scm_mtrigger
= nm
+ nm
/ 2;
1977 scm_mtrigger
+= scm_mtrigger
/ 2;
1979 #ifdef GUILE_DEBUG_MALLOC
1980 scm_malloc_reregister (where
, ptr
, what
);
1985 scm_memory_error (what
);
1990 scm_must_free (void *obj
)
1991 #define FUNC_NAME "scm_must_free"
1993 #ifdef GUILE_DEBUG_MALLOC
1994 scm_malloc_unregister (obj
);
1999 SCM_MISC_ERROR ("freeing NULL pointer", SCM_EOL
);
2004 /* Announce that there has been some malloc done that will be freed
2005 * during gc. A typical use is for a smob that uses some malloced
2006 * memory but can not get it from scm_must_malloc (for whatever
2007 * reason). When a new object of this smob is created you call
2008 * scm_done_malloc with the size of the object. When your smob free
2009 * function is called, be sure to include this size in the return
2012 * If you can't actually free the memory in the smob free function,
2013 * for whatever reason (like reference counting), you still can (and
2014 * should) report the amount of memory freed when you actually free it.
2015 * Do it by calling scm_done_malloc with the _negated_ size. Clever,
2016 * eh? Or even better, call scm_done_free. */
2019 scm_done_malloc (long size
)
2021 scm_mallocated
+= size
;
2023 if (scm_mallocated
> scm_mtrigger
)
2025 scm_igc ("foreign mallocs");
2026 if (scm_mallocated
> scm_mtrigger
- SCM_MTRIGGER_HYSTERESIS
)
2028 if (scm_mallocated
> scm_mtrigger
)
2029 scm_mtrigger
= scm_mallocated
+ scm_mallocated
/ 2;
2031 scm_mtrigger
+= scm_mtrigger
/ 2;
2037 scm_done_free (long size
)
2039 scm_mallocated
-= size
;
2046 * Each heap segment is an array of objects of a particular size.
2047 * Every segment has an associated (possibly shared) freelist.
2048 * A table of segment records is kept that records the upper and
2049 * lower extents of the segment; this is used during the conservative
2050 * phase of gc to identify probably gc roots (because they point
2051 * into valid segments at reasonable offsets). */
2054 * is true if the first segment was smaller than INIT_HEAP_SEG.
2055 * If scm_expmem is set to one, subsequent segment allocations will
2056 * allocate segments of size SCM_EXPHEAP(scm_heap_size).
2060 scm_sizet scm_max_segment_size
;
2063 * is the lowest base address of any heap segment.
2065 SCM_CELLPTR scm_heap_org
;
2067 scm_heap_seg_data_t
* scm_heap_table
= 0;
2068 static unsigned int heap_segment_table_size
= 0;
2069 int scm_n_heap_segs
= 0;
2072 * initializes a new heap segment and returns the number of objects it contains.
2074 * The segment origin and segment size in bytes are input parameters.
2075 * The freelist is both input and output.
2077 * This function presumes that the scm_heap_table has already been expanded
2078 * to accomodate a new segment record and that the markbit space was reserved
2079 * for all the cards in this segment.
2082 #define INIT_CARD(card, span) \
2084 SCM_GC_SET_CARD_BVEC (card, get_bvec ()); \
2086 SCM_GC_SET_CARD_DOUBLECELL (card); \
2090 init_heap_seg (SCM_CELLPTR seg_org
, scm_sizet size
, scm_freelist_t
*freelist
)
2092 register SCM_CELLPTR ptr
;
2093 SCM_CELLPTR seg_end
;
2096 int span
= freelist
->span
;
2098 if (seg_org
== NULL
)
2101 /* Align the begin ptr up.
2103 ptr
= SCM_GC_CARD_UP (seg_org
);
2105 /* Compute the ceiling on valid object pointers w/in this segment.
2107 seg_end
= SCM_GC_CARD_DOWN ((char *)seg_org
+ size
);
2109 /* Find the right place and insert the segment record.
2112 for (new_seg_index
= 0;
2113 ( (new_seg_index
< scm_n_heap_segs
)
2114 && SCM_PTR_LE (scm_heap_table
[new_seg_index
].bounds
[0], seg_org
));
2120 for (i
= scm_n_heap_segs
; i
> new_seg_index
; --i
)
2121 scm_heap_table
[i
] = scm_heap_table
[i
- 1];
2126 scm_heap_table
[new_seg_index
].span
= span
;
2127 scm_heap_table
[new_seg_index
].freelist
= freelist
;
2128 scm_heap_table
[new_seg_index
].bounds
[0] = ptr
;
2129 scm_heap_table
[new_seg_index
].bounds
[1] = seg_end
;
2132 n_new_cells
= seg_end
- ptr
;
2134 freelist
->heap_size
+= n_new_cells
;
2136 /* Partition objects in this segment into clusters */
2139 SCM
*clusterp
= &clusters
;
2141 NEXT_DATA_CELL (ptr
, span
);
2142 while (ptr
< seg_end
)
2144 scm_cell
*nxt
= ptr
;
2145 scm_cell
*prv
= NULL
;
2146 scm_cell
*last_card
= NULL
;
2147 int n_data_cells
= (SCM_GC_CARD_N_DATA_CELLS
/ span
) * SCM_CARDS_PER_CLUSTER
- 1;
2148 NEXT_DATA_CELL(nxt
, span
);
2150 /* Allocate cluster spine
2152 *clusterp
= PTR2SCM (ptr
);
2153 SCM_SETCAR (*clusterp
, PTR2SCM (nxt
));
2154 clusterp
= SCM_CDRLOC (*clusterp
);
2157 while (n_data_cells
--)
2159 scm_cell
*card
= SCM_GC_CELL_CARD (ptr
);
2160 SCM scmptr
= PTR2SCM (ptr
);
2162 NEXT_DATA_CELL (nxt
, span
);
2165 if (card
!= last_card
)
2167 INIT_CARD (card
, span
);
2171 SCM_SET_CELL_TYPE (scmptr
, scm_tc_free_cell
);
2172 SCM_SET_FREE_CELL_CDR (scmptr
, PTR2SCM (nxt
));
2177 SCM_SET_FREE_CELL_CDR (PTR2SCM (prv
), SCM_EOL
);
2182 scm_cell
*ref
= seg_end
;
2183 NEXT_DATA_CELL (ref
, span
);
2185 /* [cmm] looks like the segment size doesn't divide cleanly by
2186 cluster size. bad cmm! */
2190 /* Patch up the last cluster pointer in the segment
2191 * to join it to the input freelist.
2193 *clusterp
= freelist
->clusters
;
2194 freelist
->clusters
= clusters
;
2198 fprintf (stderr
, "H");
2204 round_to_cluster_size (scm_freelist_t
*freelist
, scm_sizet len
)
2206 scm_sizet cluster_size_in_bytes
= CLUSTER_SIZE_IN_BYTES (freelist
);
2209 (len
+ cluster_size_in_bytes
- 1) / cluster_size_in_bytes
* cluster_size_in_bytes
2210 + ALIGNMENT_SLACK (freelist
);
2214 alloc_some_heap (scm_freelist_t
*freelist
, policy_on_error error_policy
)
2215 #define FUNC_NAME "alloc_some_heap"
2220 if (scm_gc_heap_lock
)
2222 /* Critical code sections (such as the garbage collector) aren't
2223 * supposed to add heap segments.
2225 fprintf (stderr
, "alloc_some_heap: Can not extend locked heap.\n");
2229 if (scm_n_heap_segs
== heap_segment_table_size
)
2231 /* We have to expand the heap segment table to have room for the new
2232 * segment. Do not yet increment scm_n_heap_segs -- that is done by
2233 * init_heap_seg only if the allocation of the segment itself succeeds.
2235 unsigned int new_table_size
= scm_n_heap_segs
+ 1;
2236 size_t size
= new_table_size
* sizeof (scm_heap_seg_data_t
);
2237 scm_heap_seg_data_t
* new_heap_table
;
2239 SCM_SYSCALL (new_heap_table
= ((scm_heap_seg_data_t
*)
2240 realloc ((char *)scm_heap_table
, size
)));
2241 if (!new_heap_table
)
2243 if (error_policy
== abort_on_error
)
2245 fprintf (stderr
, "alloc_some_heap: Could not grow heap segment table.\n");
2255 scm_heap_table
= new_heap_table
;
2256 heap_segment_table_size
= new_table_size
;
2260 /* Pick a size for the new heap segment.
2261 * The rule for picking the size of a segment is explained in
2265 /* Assure that the new segment is predicted to be large enough.
2267 * New yield should at least equal GC fraction of new heap size, i.e.
2269 * y + dh > f * (h + dh)
2272 * f : min yield fraction
2274 * dh : size of new heap segment
2276 * This gives dh > (f * h - y) / (1 - f)
2278 int f
= freelist
->min_yield_fraction
;
2279 long h
= SCM_HEAP_SIZE
;
2280 long min_cells
= (f
* h
- 100 * (long) scm_gc_yield
) / (99 - f
);
2281 len
= SCM_EXPHEAP (freelist
->heap_size
);
2283 fprintf (stderr
, "(%d < %d)", len
, min_cells
);
2285 if (len
< min_cells
)
2286 len
= min_cells
+ freelist
->cluster_size
;
2287 len
*= sizeof (scm_cell
);
2288 /* force new sampling */
2289 freelist
->collected
= LONG_MAX
;
2292 if (len
> scm_max_segment_size
)
2293 len
= scm_max_segment_size
;
2298 smallest
= CLUSTER_SIZE_IN_BYTES (freelist
);
2303 /* Allocate with decaying ambition. */
2304 while ((len
>= SCM_MIN_HEAP_SEG_SIZE
)
2305 && (len
>= smallest
))
2307 scm_sizet rounded_len
= round_to_cluster_size (freelist
, len
);
2308 SCM_SYSCALL (ptr
= (SCM_CELLPTR
) malloc (rounded_len
));
2311 init_heap_seg (ptr
, rounded_len
, freelist
);
2318 if (error_policy
== abort_on_error
)
2320 fprintf (stderr
, "alloc_some_heap: Could not grow heap.\n");
2327 /* {GC Protection Helper Functions}
2332 * If within a function you need to protect one or more scheme objects from
2333 * garbage collection, pass them as parameters to one of the
2334 * scm_remember_upto_here* functions below. These functions don't do
2335 * anything, but since the compiler does not know that they are actually
2336 * no-ops, it will generate code that calls these functions with the given
2337 * parameters. Therefore, you can be sure that the compiler will keep those
2338 * scheme values alive (on the stack or in a register) up to the point where
2339 * scm_remember_upto_here* is called. In other words, place the call to
2340 * scm_remember_upt_here* _behind_ the last code in your function, that
2341 * depends on the scheme object to exist.
2343 * Example: We want to make sure, that the string object str does not get
2344 * garbage collected during the execution of 'some_function', because
2345 * otherwise the characters belonging to str would be freed and
2346 * 'some_function' might access freed memory. To make sure that the compiler
2347 * keeps str alive on the stack or in a register such that it is visible to
2348 * the conservative gc we add the call to scm_remember_upto_here_1 _after_ the
2349 * call to 'some_function'. Note that this would not be necessary if str was
2350 * used anyway after the call to 'some_function'.
2351 * char *chars = SCM_STRING_CHARS (str);
2352 * some_function (chars);
2353 * scm_remember_upto_here_1 (str); // str will be alive up to this point.
2357 scm_remember_upto_here_1 (SCM obj
)
2359 /* Empty. Protects a single object from garbage collection. */
2363 scm_remember_upto_here_2 (SCM obj1
, SCM obj2
)
2365 /* Empty. Protects two objects from garbage collection. */
2369 scm_remember_upto_here (SCM obj
, ...)
2371 /* Empty. Protects any number of objects from garbage collection. */
2375 #if (SCM_DEBUG_DEPRECATED == 0)
2378 scm_remember (SCM
*ptr
)
2383 #endif /* SCM_DEBUG_DEPRECATED == 0 */
2386 These crazy functions prevent garbage collection
2387 of arguments after the first argument by
2388 ensuring they remain live throughout the
2389 function because they are used in the last
2390 line of the code block.
2391 It'd be better to have a nice compiler hint to
2392 aid the conservative stack-scanning GC. --03/09/00 gjb */
2394 scm_return_first (SCM elt
, ...)
2400 scm_return_first_int (int i
, ...)
2407 scm_permanent_object (SCM obj
)
2410 scm_permobjs
= scm_cons (obj
, scm_permobjs
);
2416 /* Protect OBJ from the garbage collector. OBJ will not be freed, even if all
2417 other references are dropped, until the object is unprotected by calling
2418 scm_unprotect_object (OBJ). Calls to scm_protect/unprotect_object nest,
2419 i. e. it is possible to protect the same object several times, but it is
2420 necessary to unprotect the object the same number of times to actually get
2421 the object unprotected. It is an error to unprotect an object more often
2422 than it has been protected before. The function scm_protect_object returns
2426 /* Implementation note: For every object X, there is a counter which
2427 scm_protect_object(X) increments and scm_unprotect_object(X) decrements.
2431 scm_protect_object (SCM obj
)
2435 /* This critical section barrier will be replaced by a mutex. */
2438 handle
= scm_hashq_create_handle_x (scm_protects
, obj
, SCM_MAKINUM (0));
2439 SCM_SETCDR (handle
, SCM_MAKINUM (SCM_INUM (SCM_CDR (handle
)) + 1));
2447 /* Remove any protection for OBJ established by a prior call to
2448 scm_protect_object. This function returns OBJ.
2450 See scm_protect_object for more information. */
2452 scm_unprotect_object (SCM obj
)
2456 /* This critical section barrier will be replaced by a mutex. */
2459 handle
= scm_hashq_get_handle (scm_protects
, obj
);
2461 if (SCM_FALSEP (handle
))
2463 fprintf (stderr
, "scm_unprotect_object called on unprotected object\n");
2468 unsigned long int count
= SCM_INUM (SCM_CDR (handle
)) - 1;
2470 scm_hashq_remove_x (scm_protects
, obj
);
2472 SCM_SETCDR (handle
, SCM_MAKINUM (count
));
2482 /* called on process termination. */
2488 extern int on_exit (void (*procp
) (), int arg
);
2491 cleanup (int status
, void *arg
)
2493 #error Dont know how to setup a cleanup handler on your system.
2498 scm_flush_all_ports ();
2503 make_initial_segment (scm_sizet init_heap_size
, scm_freelist_t
*freelist
)
2505 scm_sizet rounded_size
= round_to_cluster_size (freelist
, init_heap_size
);
2507 if (!init_heap_seg ((SCM_CELLPTR
) malloc (rounded_size
),
2511 rounded_size
= round_to_cluster_size (freelist
, SCM_HEAP_SEG_SIZE
);
2512 if (!init_heap_seg ((SCM_CELLPTR
) malloc (rounded_size
),
2520 if (freelist
->min_yield_fraction
)
2521 freelist
->min_yield
= (freelist
->heap_size
* freelist
->min_yield_fraction
2523 freelist
->grow_heap_p
= (freelist
->heap_size
< freelist
->min_yield
);
2530 init_freelist (scm_freelist_t
*freelist
,
2535 freelist
->clusters
= SCM_EOL
;
2536 freelist
->cluster_size
= cluster_size
+ 1;
2537 freelist
->left_to_collect
= 0;
2538 freelist
->clusters_allocated
= 0;
2539 freelist
->min_yield
= 0;
2540 freelist
->min_yield_fraction
= min_yield
;
2541 freelist
->span
= span
;
2542 freelist
->collected
= 0;
2543 freelist
->collected_1
= 0;
2544 freelist
->heap_size
= 0;
2548 /* Get an integer from an environment variable. */
2550 scm_i_getenv_int (const char *var
, int def
)
2552 char *end
, *val
= getenv (var
);
2556 res
= strtol (val
, &end
, 10);
2566 scm_sizet gc_trigger_1
;
2567 scm_sizet gc_trigger_2
;
2568 scm_sizet init_heap_size_1
;
2569 scm_sizet init_heap_size_2
;
2572 #if (SCM_DEBUG_CELL_ACCESSES == 1)
2573 scm_tc16_allocated
= scm_make_smob_type ("allocated cell", 0);
2574 #endif /* SCM_DEBUG_CELL_ACCESSES == 1 */
2576 j
= SCM_NUM_PROTECTS
;
2578 scm_sys_protects
[--j
] = SCM_BOOL_F
;
2581 scm_freelist
= SCM_EOL
;
2582 scm_freelist2
= SCM_EOL
;
2583 gc_trigger_1
= scm_i_getenv_int ("GUILE_MIN_YIELD_1", scm_default_min_yield_1
);
2584 init_freelist (&scm_master_freelist
, 1, SCM_CLUSTER_SIZE_1
, gc_trigger_1
);
2585 gc_trigger_2
= scm_i_getenv_int ("GUILE_MIN_YIELD_2", scm_default_min_yield_2
);
2586 init_freelist (&scm_master_freelist2
, 2, SCM_CLUSTER_SIZE_2
, gc_trigger_2
);
2587 scm_max_segment_size
= scm_i_getenv_int ("GUILE_MAX_SEGMENT_SIZE", scm_default_max_segment_size
);
2591 j
= SCM_HEAP_SEG_SIZE
;
2592 scm_mtrigger
= SCM_INIT_MALLOC_LIMIT
;
2593 scm_heap_table
= ((scm_heap_seg_data_t
*)
2594 scm_must_malloc (sizeof (scm_heap_seg_data_t
) * 2, "hplims"));
2595 heap_segment_table_size
= 2;
2597 mark_space_ptr
= &mark_space_head
;
2599 init_heap_size_1
= scm_i_getenv_int ("GUILE_INIT_SEGMENT_SIZE_1", scm_default_init_heap_size_1
);
2600 init_heap_size_2
= scm_i_getenv_int ("GUILE_INIT_SEGMENT_SIZE_2", scm_default_init_heap_size_2
);
2601 if (make_initial_segment (init_heap_size_1
, &scm_master_freelist
) ||
2602 make_initial_segment (init_heap_size_2
, &scm_master_freelist2
))
2605 /* scm_hplims[0] can change. do not remove scm_heap_org */
2606 scm_heap_org
= CELL_UP (scm_heap_table
[0].bounds
[0], 1);
2608 scm_c_hook_init (&scm_before_gc_c_hook
, 0, SCM_C_HOOK_NORMAL
);
2609 scm_c_hook_init (&scm_before_mark_c_hook
, 0, SCM_C_HOOK_NORMAL
);
2610 scm_c_hook_init (&scm_before_sweep_c_hook
, 0, SCM_C_HOOK_NORMAL
);
2611 scm_c_hook_init (&scm_after_sweep_c_hook
, 0, SCM_C_HOOK_NORMAL
);
2612 scm_c_hook_init (&scm_after_gc_c_hook
, 0, SCM_C_HOOK_NORMAL
);
2614 /* Initialise the list of ports. */
2615 scm_port_table
= (scm_port
**)
2616 malloc (sizeof (scm_port
*) * scm_port_table_room
);
2617 if (!scm_port_table
)
2624 on_exit (cleanup
, 0);
2628 scm_stand_in_procs
= SCM_EOL
;
2629 scm_permobjs
= SCM_EOL
;
2630 scm_protects
= scm_c_make_hash_table (31);
2637 SCM scm_after_gc_hook
;
2639 static SCM gc_async
;
2641 /* The function gc_async_thunk causes the execution of the after-gc-hook. It
2642 * is run after the gc, as soon as the asynchronous events are handled by the
2646 gc_async_thunk (void)
2648 scm_c_run_hook (scm_after_gc_hook
, SCM_EOL
);
2649 return SCM_UNSPECIFIED
;
2653 /* The function mark_gc_async is run by the scm_after_gc_c_hook at the end of
2654 * the garbage collection. The only purpose of this function is to mark the
2655 * gc_async (which will eventually lead to the execution of the
2659 mark_gc_async (void * hook_data
, void *func_data
, void *data
)
2661 scm_system_async_mark (gc_async
);
2671 /* Dirk:FIXME:: scm_create_hook is strange. */
2672 scm_after_gc_hook
= scm_create_hook ("after-gc-hook", 0);
2674 after_gc_thunk
= scm_c_make_subr ("%gc-thunk", scm_tc7_subr_0
,
2676 gc_async
= scm_system_async (after_gc_thunk
); /* protected via scm_asyncs */
2678 scm_c_hook_add (&scm_after_gc_c_hook
, mark_gc_async
, NULL
, 0);
2680 #ifndef SCM_MAGIC_SNARFER
2681 #include "libguile/gc.x"
2685 #endif /*MARK_DEPENDENCIES*/