1 /* Copyright (C) 1995, 96, 97, 98, 99, 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 unsigned int scm_debug_cell_accesses_p
= 0;
107 /* Assert that the given object is a valid reference to a valid cell. This
108 * test involves to determine whether the object is a cell pointer, whether
109 * this pointer actually points into a heap segment and whether the cell
110 * pointed to is not a free cell.
113 scm_assert_cell_valid (SCM cell
)
115 if (scm_debug_cell_accesses_p
)
117 scm_debug_cell_accesses_p
= 0; /* disable to avoid recursion */
119 if (!scm_cellp (cell
))
121 fprintf (stderr
, "scm_assert_cell_valid: Not a cell object: %lx\n", SCM_UNPACK (cell
));
124 else if (!scm_gc_running_p
)
126 /* Dirk::FIXME:: During garbage collection there occur references to
127 free cells. This is allright during conservative marking, but
128 should not happen otherwise (I think). The case of free cells
129 accessed during conservative marking is handled in function
130 scm_mark_locations. However, there still occur accesses to free
131 cells during gc. I don't understand why this happens. If it is
132 a bug and gets fixed, the following test should also work while
135 if (SCM_FREE_CELL_P (cell
))
137 fprintf (stderr
, "scm_assert_cell_valid: Accessing free cell: %lx\n", SCM_UNPACK (cell
));
141 scm_debug_cell_accesses_p
= 1; /* re-enable */
146 SCM_DEFINE (scm_set_debug_cell_accesses_x
, "set-debug-cell-accesses!", 1, 0, 0,
148 "If FLAG is #f, cell access checking is disabled.\n"
149 "If FLAG is #t, cell access checking is enabled.\n"
150 "This procedure only exists because the compile-time flag\n"
151 "SCM_DEBUG_CELL_ACCESSES was set to 1.\n")
152 #define FUNC_NAME s_scm_set_debug_cell_accesses_x
154 if (SCM_FALSEP (flag
)) {
155 scm_debug_cell_accesses_p
= 0;
156 } else if (SCM_EQ_P (flag
, SCM_BOOL_T
)) {
157 scm_debug_cell_accesses_p
= 1;
159 SCM_WRONG_TYPE_ARG (1, flag
);
161 return SCM_UNSPECIFIED
;
165 #endif /* SCM_DEBUG_CELL_ACCESSES == 1 */
169 /* {heap tuning parameters}
171 * These are parameters for controlling memory allocation. The heap
172 * is the area out of which scm_cons, and object headers are allocated.
174 * Each heap cell is 8 bytes on a 32 bit machine and 16 bytes on a
175 * 64 bit machine. The units of the _SIZE parameters are bytes.
176 * Cons pairs and object headers occupy one heap cell.
178 * SCM_INIT_HEAP_SIZE is the initial size of heap. If this much heap is
179 * allocated initially the heap will grow by half its current size
180 * each subsequent time more heap is needed.
182 * If SCM_INIT_HEAP_SIZE heap cannot be allocated initially, SCM_HEAP_SEG_SIZE
183 * will be used, and the heap will grow by SCM_HEAP_SEG_SIZE when more
184 * heap is needed. SCM_HEAP_SEG_SIZE must fit into type scm_sizet. This code
185 * is in scm_init_storage() and alloc_some_heap() in sys.c
187 * If SCM_INIT_HEAP_SIZE can be allocated initially, the heap will grow by
188 * SCM_EXPHEAP(scm_heap_size) when more heap is needed.
190 * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap
193 * INIT_MALLOC_LIMIT is the initial amount of malloc usage which will
196 * SCM_MTRIGGER_HYSTERESIS is the amount of malloc storage that must be
197 * reclaimed by a GC triggered by must_malloc. If less than this is
198 * reclaimed, the trigger threshold is raised. [I don't know what a
199 * good value is. I arbitrarily chose 1/10 of the INIT_MALLOC_LIMIT to
200 * work around a oscillation that caused almost constant GC.]
204 * Heap size 45000 and 40% min yield gives quick startup and no extra
205 * heap allocation. Having higher values on min yield may lead to
206 * large heaps, especially if code behaviour is varying its
207 * maximum consumption between different freelists.
210 #define SCM_DATA_CELLS2CARDS(n) (((n) + SCM_GC_CARD_N_DATA_CELLS - 1) / SCM_GC_CARD_N_DATA_CELLS)
211 #define SCM_CARDS_PER_CLUSTER SCM_DATA_CELLS2CARDS (2000L)
212 #define SCM_CLUSTER_SIZE_1 (SCM_CARDS_PER_CLUSTER * SCM_GC_CARD_N_DATA_CELLS)
213 int scm_default_init_heap_size_1
= (((SCM_DATA_CELLS2CARDS (45000L) + SCM_CARDS_PER_CLUSTER
- 1)
214 / SCM_CARDS_PER_CLUSTER
) * SCM_GC_CARD_SIZE
);
215 int scm_default_min_yield_1
= 40;
217 #define SCM_CLUSTER_SIZE_2 (SCM_CARDS_PER_CLUSTER * (SCM_GC_CARD_N_DATA_CELLS / 2))
218 int scm_default_init_heap_size_2
= (((SCM_DATA_CELLS2CARDS (2500L * 2) + SCM_CARDS_PER_CLUSTER
- 1)
219 / SCM_CARDS_PER_CLUSTER
) * SCM_GC_CARD_SIZE
);
220 /* The following value may seem large, but note that if we get to GC at
221 * all, this means that we have a numerically intensive application
223 int scm_default_min_yield_2
= 40;
225 int scm_default_max_segment_size
= 2097000L;/* a little less (adm) than 2 Mb */
227 #define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_CARD_SIZE)
229 # define SCM_HEAP_SEG_SIZE 32768L
232 # define SCM_HEAP_SEG_SIZE (7000L * sizeof (scm_cell))
234 # define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_cell))
237 /* Make heap grow with factor 1.5 */
238 #define SCM_EXPHEAP(scm_heap_size) (scm_heap_size / 2)
239 #define SCM_INIT_MALLOC_LIMIT 100000
240 #define SCM_MTRIGGER_HYSTERESIS (SCM_INIT_MALLOC_LIMIT/10)
242 /* CELL_UP and CELL_DN are used by scm_init_heap_seg to find (scm_cell * span)
243 aligned inner bounds for allocated storage */
246 /*in 386 protected mode we must only adjust the offset */
247 # define CELL_UP(p, span) MK_FP(FP_SEG(p), ~(8*(span)-1)&(FP_OFF(p)+8*(span)-1))
248 # define CELL_DN(p, span) MK_FP(FP_SEG(p), ~(8*(span)-1)&FP_OFF(p))
251 # define CELL_UP(p, span) (SCM_CELLPTR)(~(span) & ((long)(p)+(span)))
252 # define CELL_DN(p, span) (SCM_CELLPTR)(~(span) & (long)(p))
254 # define CELL_UP(p, span) (SCM_CELLPTR)(~(sizeof(scm_cell)*(span)-1L) & ((long)(p)+sizeof(scm_cell)*(span)-1L))
255 # define CELL_DN(p, span) (SCM_CELLPTR)(~(sizeof(scm_cell)*(span)-1L) & (long)(p))
259 #define DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_cell) - 1) & SCM_UNPACK (x)) == 0)
261 #define ALIGNMENT_SLACK(freelist) (SCM_GC_CARD_SIZE - 1)
262 #define CLUSTER_SIZE_IN_BYTES(freelist) \
263 (((freelist)->cluster_size / (SCM_GC_CARD_N_DATA_CELLS / (freelist)->span)) * SCM_GC_CARD_SIZE)
269 typedef struct scm_freelist_t
{
270 /* collected cells */
272 /* number of cells left to collect before cluster is full */
273 unsigned int left_to_collect
;
274 /* number of clusters which have been allocated */
275 unsigned int clusters_allocated
;
276 /* a list of freelists, each of size cluster_size,
277 * except the last one which may be shorter
281 /* this is the number of objects in each cluster, including the spine cell */
283 /* indicates that we should grow heap instead of GC:ing
286 /* minimum yield on this list in order not to grow the heap
289 /* defines min_yield as percent of total heap size
291 int min_yield_fraction
;
292 /* number of cells per object on this list */
294 /* number of collected cells during last GC */
296 /* number of collected cells during penultimate GC */
298 /* total number of cells in heap segments
299 * belonging to this list.
304 SCM scm_freelist
= SCM_EOL
;
305 scm_freelist_t scm_master_freelist
= {
306 SCM_EOL
, 0, 0, SCM_EOL
, 0, SCM_CLUSTER_SIZE_1
, 0, 0, 0, 1, 0, 0
308 SCM scm_freelist2
= SCM_EOL
;
309 scm_freelist_t scm_master_freelist2
= {
310 SCM_EOL
, 0, 0, SCM_EOL
, 0, SCM_CLUSTER_SIZE_2
, 0, 0, 0, 2, 0, 0
314 * is the number of bytes of must_malloc allocation needed to trigger gc.
316 unsigned long scm_mtrigger
;
319 * If set, don't expand the heap. Set only during gc, during which no allocation
320 * is supposed to take place anyway.
322 int scm_gc_heap_lock
= 0;
325 * Don't pause for collection if this is set -- just
328 int scm_block_gc
= 1;
330 /* During collection, this accumulates objects holding
333 SCM scm_weak_vectors
;
335 /* During collection, this accumulates structures which are to be freed.
337 SCM scm_structs_to_free
;
339 /* GC Statistics Keeping
341 unsigned long scm_cells_allocated
= 0;
342 long scm_mallocated
= 0;
343 unsigned long scm_gc_cells_collected
;
344 unsigned long scm_gc_yield
;
345 static unsigned long scm_gc_yield_1
= 0; /* previous GC yield */
346 unsigned long scm_gc_malloc_collected
;
347 unsigned long scm_gc_ports_collected
;
348 unsigned long scm_gc_time_taken
= 0;
349 static unsigned long t_before_gc
;
350 static unsigned long t_before_sweep
;
351 unsigned long scm_gc_mark_time_taken
= 0;
352 unsigned long scm_gc_sweep_time_taken
= 0;
353 unsigned long scm_gc_times
= 0;
354 unsigned long scm_gc_cells_swept
= 0;
355 double scm_gc_cells_marked_acc
= 0.;
356 double scm_gc_cells_swept_acc
= 0.;
358 SCM_SYMBOL (sym_cells_allocated
, "cells-allocated");
359 SCM_SYMBOL (sym_heap_size
, "cell-heap-size");
360 SCM_SYMBOL (sym_mallocated
, "bytes-malloced");
361 SCM_SYMBOL (sym_mtrigger
, "gc-malloc-threshold");
362 SCM_SYMBOL (sym_heap_segments
, "cell-heap-segments");
363 SCM_SYMBOL (sym_gc_time_taken
, "gc-time-taken");
364 SCM_SYMBOL (sym_gc_mark_time_taken
, "gc-mark-time-taken");
365 SCM_SYMBOL (sym_gc_sweep_time_taken
, "gc-sweep-time-taken");
366 SCM_SYMBOL (sym_times
, "gc-times");
367 SCM_SYMBOL (sym_cells_marked
, "cells-marked");
368 SCM_SYMBOL (sym_cells_swept
, "cells-swept");
370 typedef struct scm_heap_seg_data_t
372 /* lower and upper bounds of the segment */
373 SCM_CELLPTR bounds
[2];
375 /* address of the head-of-freelist pointer for this segment's cells.
376 All segments usually point to the same one, scm_freelist. */
377 scm_freelist_t
*freelist
;
379 /* number of cells per object in this segment */
381 } scm_heap_seg_data_t
;
385 static scm_sizet
init_heap_seg (SCM_CELLPTR
, scm_sizet
, scm_freelist_t
*);
387 typedef enum { return_on_error
, abort_on_error
} policy_on_error
;
388 static void alloc_some_heap (scm_freelist_t
*, policy_on_error
);
391 #define SCM_HEAP_SIZE \
392 (scm_master_freelist.heap_size + scm_master_freelist2.heap_size)
393 #define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
395 #define BVEC_GROW_SIZE 256
396 #define BVEC_GROW_SIZE_IN_LIMBS (SCM_GC_CARD_BVEC_SIZE_IN_LIMBS * BVEC_GROW_SIZE)
397 #define BVEC_GROW_SIZE_IN_BYTES (BVEC_GROW_SIZE_IN_LIMBS * sizeof (scm_c_bvec_limb_t))
399 /* mark space allocation */
401 typedef struct scm_mark_space_t
403 scm_c_bvec_limb_t
*bvec_space
;
404 struct scm_mark_space_t
*next
;
407 static scm_mark_space_t
*current_mark_space
;
408 static scm_mark_space_t
**mark_space_ptr
;
409 static int current_mark_space_offset
;
410 static scm_mark_space_t
*mark_space_head
;
412 static scm_c_bvec_limb_t
*
414 #define FUNC_NAME "get_bvec"
416 scm_c_bvec_limb_t
*res
;
418 if (!current_mark_space
)
420 SCM_SYSCALL (current_mark_space
= (scm_mark_space_t
*) malloc (sizeof (scm_mark_space_t
)));
421 if (!current_mark_space
)
422 SCM_MISC_ERROR ("could not grow heap", SCM_EOL
);
424 current_mark_space
->bvec_space
= NULL
;
425 current_mark_space
->next
= NULL
;
427 *mark_space_ptr
= current_mark_space
;
428 mark_space_ptr
= &(current_mark_space
->next
);
433 if (!(current_mark_space
->bvec_space
))
435 SCM_SYSCALL (current_mark_space
->bvec_space
=
436 (scm_c_bvec_limb_t
*) calloc (BVEC_GROW_SIZE_IN_BYTES
, 1));
437 if (!(current_mark_space
->bvec_space
))
438 SCM_MISC_ERROR ("could not grow heap", SCM_EOL
);
440 current_mark_space_offset
= 0;
445 if (current_mark_space_offset
== BVEC_GROW_SIZE_IN_LIMBS
)
447 current_mark_space
= NULL
;
452 res
= current_mark_space
->bvec_space
+ current_mark_space_offset
;
453 current_mark_space_offset
+= SCM_GC_CARD_BVEC_SIZE_IN_LIMBS
;
463 scm_mark_space_t
*ms
;
465 for (ms
= mark_space_head
; ms
; ms
= ms
->next
)
466 memset (ms
->bvec_space
, 0, BVEC_GROW_SIZE_IN_BYTES
);
471 /* Debugging functions. */
473 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
475 /* Return the number of the heap segment containing CELL. */
481 for (i
= 0; i
< scm_n_heap_segs
; i
++)
482 if (SCM_PTR_LE (scm_heap_table
[i
].bounds
[0], SCM2PTR (cell
))
483 && SCM_PTR_GT (scm_heap_table
[i
].bounds
[1], SCM2PTR (cell
)))
485 fprintf (stderr
, "which_seg: can't find segment containing cell %lx\n",
492 map_free_list (scm_freelist_t
*master
, SCM freelist
)
494 int last_seg
= -1, count
= 0;
497 for (f
= freelist
; !SCM_NULLP (f
); f
= SCM_FREE_CELL_CDR (f
))
499 int this_seg
= which_seg (f
);
501 if (this_seg
!= last_seg
)
504 fprintf (stderr
, " %5d %d-cells in segment %d\n",
505 count
, master
->span
, last_seg
);
512 fprintf (stderr
, " %5d %d-cells in segment %d\n",
513 count
, master
->span
, last_seg
);
516 SCM_DEFINE (scm_map_free_list
, "map-free-list", 0, 0, 0,
518 "Print debugging information about the free-list.\n"
519 "`map-free-list' is only included in --enable-guile-debug builds of Guile.")
520 #define FUNC_NAME s_scm_map_free_list
523 fprintf (stderr
, "%d segments total (%d:%d",
525 scm_heap_table
[0].span
,
526 scm_heap_table
[0].bounds
[1] - scm_heap_table
[0].bounds
[0]);
527 for (i
= 1; i
< scm_n_heap_segs
; i
++)
528 fprintf (stderr
, ", %d:%d",
529 scm_heap_table
[i
].span
,
530 scm_heap_table
[i
].bounds
[1] - scm_heap_table
[i
].bounds
[0]);
531 fprintf (stderr
, ")\n");
532 map_free_list (&scm_master_freelist
, scm_freelist
);
533 map_free_list (&scm_master_freelist2
, scm_freelist2
);
536 return SCM_UNSPECIFIED
;
540 static int last_cluster
;
541 static int last_size
;
544 free_list_length (char *title
, int i
, SCM freelist
)
548 for (ls
= freelist
; !SCM_NULLP (ls
); ls
= SCM_FREE_CELL_CDR (ls
))
549 if (SCM_FREE_CELL_P (ls
))
553 fprintf (stderr
, "bad cell in %s at position %d\n", title
, n
);
560 if (last_cluster
== i
- 1)
561 fprintf (stderr
, "\t%d\n", last_size
);
563 fprintf (stderr
, "-%d\t%d\n", i
- 1, last_size
);
566 fprintf (stderr
, "%s %d", title
, i
);
568 fprintf (stderr
, "%s\t%d\n", title
, n
);
576 free_list_lengths (char *title
, scm_freelist_t
*master
, SCM freelist
)
579 int i
= 0, len
, n
= 0;
580 fprintf (stderr
, "%s\n\n", title
);
581 n
+= free_list_length ("free list", -1, freelist
);
582 for (clusters
= master
->clusters
;
583 SCM_NNULLP (clusters
);
584 clusters
= SCM_CDR (clusters
))
586 len
= free_list_length ("cluster", i
++, SCM_CAR (clusters
));
589 if (last_cluster
== i
- 1)
590 fprintf (stderr
, "\t%d\n", last_size
);
592 fprintf (stderr
, "-%d\t%d\n", i
- 1, last_size
);
593 fprintf (stderr
, "\ntotal %d objects\n\n", n
);
596 SCM_DEFINE (scm_free_list_length
, "free-list-length", 0, 0, 0,
598 "Print debugging information about the free-list.\n"
599 "`free-list-length' is only included in --enable-guile-debug builds of Guile.")
600 #define FUNC_NAME s_scm_free_list_length
602 free_list_lengths ("1-cells", &scm_master_freelist
, scm_freelist
);
603 free_list_lengths ("2-cells", &scm_master_freelist2
, scm_freelist2
);
604 return SCM_UNSPECIFIED
;
610 #ifdef GUILE_DEBUG_FREELIST
612 /* Number of calls to SCM_NEWCELL since startup. */
613 static unsigned long scm_newcell_count
;
614 static unsigned long scm_newcell2_count
;
616 /* Search freelist for anything that isn't marked as a free cell.
617 Abort if we find something. */
619 scm_check_freelist (SCM freelist
)
624 for (f
= freelist
; !SCM_NULLP (f
); f
= SCM_FREE_CELL_CDR (f
), i
++)
625 if (!SCM_FREE_CELL_P (f
))
627 fprintf (stderr
, "Bad cell in freelist on newcell %lu: %d'th elt\n",
628 scm_newcell_count
, i
);
633 SCM_DEFINE (scm_gc_set_debug_check_freelist_x
, "gc-set-debug-check-freelist!", 1, 0, 0,
635 "If FLAG is #t, check the freelist for consistency on each cell allocation.\n"
636 "This procedure only exists because the GUILE_DEBUG_FREELIST \n"
637 "compile-time flag was selected.\n")
638 #define FUNC_NAME s_scm_gc_set_debug_check_freelist_x
640 /* [cmm] I did a double-take when I read this code the first time.
642 SCM_VALIDATE_BOOL_COPY (1, flag
, scm_debug_check_freelist
);
643 return SCM_UNSPECIFIED
;
649 scm_debug_newcell (void)
654 if (scm_debug_check_freelist
)
656 scm_check_freelist (scm_freelist
);
660 /* The rest of this is supposed to be identical to the SCM_NEWCELL
662 if (SCM_NULLP (scm_freelist
))
663 new = scm_gc_for_newcell (&scm_master_freelist
, &scm_freelist
);
667 scm_freelist
= SCM_FREE_CELL_CDR (scm_freelist
);
674 scm_debug_newcell2 (void)
678 scm_newcell2_count
++;
679 if (scm_debug_check_freelist
)
681 scm_check_freelist (scm_freelist2
);
685 /* The rest of this is supposed to be identical to the SCM_NEWCELL
687 if (SCM_NULLP (scm_freelist2
))
688 new = scm_gc_for_newcell (&scm_master_freelist2
, &scm_freelist2
);
692 scm_freelist2
= SCM_FREE_CELL_CDR (scm_freelist2
);
698 #endif /* GUILE_DEBUG_FREELIST */
703 master_cells_allocated (scm_freelist_t
*master
)
705 /* the '- 1' below is to ignore the cluster spine cells. */
706 int objects
= master
->clusters_allocated
* (master
->cluster_size
- 1);
707 if (SCM_NULLP (master
->clusters
))
708 objects
-= master
->left_to_collect
;
709 return master
->span
* objects
;
713 freelist_length (SCM freelist
)
716 for (n
= 0; !SCM_NULLP (freelist
); freelist
= SCM_FREE_CELL_CDR (freelist
))
722 compute_cells_allocated ()
724 return (scm_cells_allocated
725 + master_cells_allocated (&scm_master_freelist
)
726 + master_cells_allocated (&scm_master_freelist2
)
727 - scm_master_freelist
.span
* freelist_length (scm_freelist
)
728 - scm_master_freelist2
.span
* freelist_length (scm_freelist2
));
731 /* {Scheme Interface to GC}
734 SCM_DEFINE (scm_gc_stats
, "gc-stats", 0, 0, 0,
736 "Returns an association list of statistics about Guile's current use of storage. ")
737 #define FUNC_NAME s_scm_gc_stats
742 long int local_scm_mtrigger
;
743 long int local_scm_mallocated
;
744 long int local_scm_heap_size
;
745 long int local_scm_cells_allocated
;
746 long int local_scm_gc_time_taken
;
747 long int local_scm_gc_times
;
748 long int local_scm_gc_mark_time_taken
;
749 long int local_scm_gc_sweep_time_taken
;
750 double local_scm_gc_cells_swept
;
751 double local_scm_gc_cells_marked
;
761 for (i
= scm_n_heap_segs
; i
--; )
762 heap_segs
= scm_cons (scm_cons (scm_ulong2num ((unsigned long)scm_heap_table
[i
].bounds
[1]),
763 scm_ulong2num ((unsigned long)scm_heap_table
[i
].bounds
[0])),
765 if (scm_n_heap_segs
!= n
)
770 /* Below, we cons to produce the resulting list. We want a snapshot of
771 * the heap situation before consing.
773 local_scm_mtrigger
= scm_mtrigger
;
774 local_scm_mallocated
= scm_mallocated
;
775 local_scm_heap_size
= SCM_HEAP_SIZE
;
776 local_scm_cells_allocated
= compute_cells_allocated ();
777 local_scm_gc_time_taken
= scm_gc_time_taken
;
778 local_scm_gc_mark_time_taken
= scm_gc_mark_time_taken
;
779 local_scm_gc_sweep_time_taken
= scm_gc_sweep_time_taken
;
780 local_scm_gc_times
= scm_gc_times
;
781 local_scm_gc_cells_swept
= scm_gc_cells_swept_acc
;
782 local_scm_gc_cells_marked
= scm_gc_cells_marked_acc
;
784 answer
= scm_listify (scm_cons (sym_gc_time_taken
, scm_ulong2num (local_scm_gc_time_taken
)),
785 scm_cons (sym_cells_allocated
, scm_ulong2num (local_scm_cells_allocated
)),
786 scm_cons (sym_heap_size
, scm_ulong2num (local_scm_heap_size
)),
787 scm_cons (sym_mallocated
, scm_ulong2num (local_scm_mallocated
)),
788 scm_cons (sym_mtrigger
, scm_ulong2num (local_scm_mtrigger
)),
789 scm_cons (sym_times
, scm_ulong2num (local_scm_gc_times
)),
790 scm_cons (sym_gc_mark_time_taken
, scm_ulong2num (local_scm_gc_mark_time_taken
)),
791 scm_cons (sym_gc_sweep_time_taken
, scm_ulong2num (local_scm_gc_sweep_time_taken
)),
792 scm_cons (sym_cells_marked
, scm_dbl2big (local_scm_gc_cells_marked
)),
793 scm_cons (sym_cells_swept
, scm_dbl2big (local_scm_gc_cells_swept
)),
794 scm_cons (sym_heap_segments
, heap_segs
),
803 gc_start_stats (const char *what
)
805 t_before_gc
= scm_c_get_internal_run_time ();
806 scm_gc_cells_swept
= 0;
807 scm_gc_cells_collected
= 0;
808 scm_gc_yield_1
= scm_gc_yield
;
809 scm_gc_yield
= (scm_cells_allocated
810 + master_cells_allocated (&scm_master_freelist
)
811 + master_cells_allocated (&scm_master_freelist2
));
812 scm_gc_malloc_collected
= 0;
813 scm_gc_ports_collected
= 0;
820 unsigned long t
= scm_c_get_internal_run_time ();
821 scm_gc_time_taken
+= (t
- t_before_gc
);
822 scm_gc_sweep_time_taken
+= (t
- t_before_sweep
);
825 scm_gc_cells_marked_acc
+= scm_gc_cells_swept
- scm_gc_cells_collected
;
826 scm_gc_cells_swept_acc
+= scm_gc_cells_swept
;
830 SCM_DEFINE (scm_object_address
, "object-address", 1, 0, 0,
832 "Return an integer that for the lifetime of @var{obj} is uniquely\n"
833 "returned by this function for @var{obj}")
834 #define FUNC_NAME s_scm_object_address
836 return scm_ulong2num ((unsigned long) SCM_UNPACK (obj
));
841 SCM_DEFINE (scm_gc
, "gc", 0, 0, 0,
843 "Scans all of SCM objects and reclaims for further use those that are\n"
844 "no longer accessible.")
845 #define FUNC_NAME s_scm_gc
850 return SCM_UNSPECIFIED
;
856 /* {C Interface For When GC is Triggered}
860 adjust_min_yield (scm_freelist_t
*freelist
)
862 /* min yield is adjusted upwards so that next predicted total yield
863 * (allocated cells actually freed by GC) becomes
864 * `min_yield_fraction' of total heap size. Note, however, that
865 * the absolute value of min_yield will correspond to `collected'
866 * on one master (the one which currently is triggering GC).
868 * The reason why we look at total yield instead of cells collected
869 * on one list is that we want to take other freelists into account.
870 * On this freelist, we know that (local) yield = collected cells,
871 * but that's probably not the case on the other lists.
873 * (We might consider computing a better prediction, for example
874 * by computing an average over multiple GC:s.)
876 if (freelist
->min_yield_fraction
)
878 /* Pick largest of last two yields. */
879 int delta
= ((SCM_HEAP_SIZE
* freelist
->min_yield_fraction
/ 100)
880 - (long) SCM_MAX (scm_gc_yield_1
, scm_gc_yield
));
882 fprintf (stderr
, " after GC = %d, delta = %d\n",
887 freelist
->min_yield
+= delta
;
892 /* When we get POSIX threads support, the master will be global and
893 * common while the freelist will be individual for each thread.
897 scm_gc_for_newcell (scm_freelist_t
*master
, SCM
*freelist
)
903 if (SCM_NULLP (master
->clusters
))
905 if (master
->grow_heap_p
|| scm_block_gc
)
907 /* In order to reduce gc frequency, try to allocate a new heap
908 * segment first, even if gc might find some free cells. If we
909 * can't obtain a new heap segment, we will try gc later.
911 master
->grow_heap_p
= 0;
912 alloc_some_heap (master
, return_on_error
);
914 if (SCM_NULLP (master
->clusters
))
916 /* The heap was not grown, either because it wasn't scheduled to
917 * grow, or because there was not enough memory available. In
918 * both cases we have to try gc to get some free cells.
921 fprintf (stderr
, "allocated = %d, ",
923 + master_cells_allocated (&scm_master_freelist
)
924 + master_cells_allocated (&scm_master_freelist2
));
927 adjust_min_yield (master
);
928 if (SCM_NULLP (master
->clusters
))
930 /* gc could not free any cells. Now, we _must_ allocate a
931 * new heap segment, because there is no other possibility
932 * to provide a new cell for the caller.
934 alloc_some_heap (master
, abort_on_error
);
938 cell
= SCM_CAR (master
->clusters
);
939 master
->clusters
= SCM_CDR (master
->clusters
);
940 ++master
->clusters_allocated
;
942 while (SCM_NULLP (cell
));
944 #ifdef GUILE_DEBUG_FREELIST
945 scm_check_freelist (cell
);
949 *freelist
= SCM_FREE_CELL_CDR (cell
);
955 /* This is a support routine which can be used to reserve a cluster
956 * for some special use, such as debugging. It won't be useful until
957 * free cells are preserved between garbage collections.
961 scm_alloc_cluster (scm_freelist_t
*master
)
964 cell
= scm_gc_for_newcell (master
, &freelist
);
965 SCM_SETCDR (cell
, freelist
);
971 scm_c_hook_t scm_before_gc_c_hook
;
972 scm_c_hook_t scm_before_mark_c_hook
;
973 scm_c_hook_t scm_before_sweep_c_hook
;
974 scm_c_hook_t scm_after_sweep_c_hook
;
975 scm_c_hook_t scm_after_gc_c_hook
;
979 scm_igc (const char *what
)
984 scm_c_hook_run (&scm_before_gc_c_hook
, 0);
987 SCM_NULLP (scm_freelist
)
989 : (SCM_NULLP (scm_freelist2
) ? "o" : "m"));
992 /* During the critical section, only the current thread may run. */
993 SCM_THREAD_CRITICAL_SECTION_START
;
996 /* fprintf (stderr, "gc: %s\n", what); */
998 if (!scm_stack_base
|| scm_block_gc
)
1004 gc_start_stats (what
);
1006 if (scm_mallocated
< 0)
1007 /* The byte count of allocated objects has underflowed. This is
1008 probably because you forgot to report the sizes of objects you
1009 have allocated, by calling scm_done_malloc or some such. When
1010 the GC freed them, it subtracted their size from
1011 scm_mallocated, which underflowed. */
1014 if (scm_gc_heap_lock
)
1015 /* We've invoked the collector while a GC is already in progress.
1016 That should never happen. */
1021 /* flush dead entries from the continuation stack */
1026 elts
= SCM_VELTS (scm_continuation_stack
);
1027 bound
= SCM_VECTOR_LENGTH (scm_continuation_stack
);
1028 x
= SCM_INUM (scm_continuation_stack_ptr
);
1031 elts
[x
] = SCM_BOOL_F
;
1036 scm_c_hook_run (&scm_before_mark_c_hook
, 0);
1038 clear_mark_space ();
1042 /* Mark objects on the C stack. */
1043 SCM_FLUSH_REGISTER_WINDOWS
;
1044 /* This assumes that all registers are saved into the jmp_buf */
1045 setjmp (scm_save_regs_gc_mark
);
1046 scm_mark_locations ((SCM_STACKITEM
*) scm_save_regs_gc_mark
,
1047 ( (scm_sizet
) (sizeof (SCM_STACKITEM
) - 1 +
1048 sizeof scm_save_regs_gc_mark
)
1049 / sizeof (SCM_STACKITEM
)));
1052 scm_sizet stack_len
= scm_stack_size (scm_stack_base
);
1053 #ifdef SCM_STACK_GROWS_UP
1054 scm_mark_locations (scm_stack_base
, stack_len
);
1056 scm_mark_locations (scm_stack_base
- stack_len
, stack_len
);
1060 #else /* USE_THREADS */
1062 /* Mark every thread's stack and registers */
1063 scm_threads_mark_stacks ();
1065 #endif /* USE_THREADS */
1067 j
= SCM_NUM_PROTECTS
;
1069 scm_gc_mark (scm_sys_protects
[j
]);
1071 /* FIXME: we should have a means to register C functions to be run
1072 * in different phases of GC
1074 scm_mark_subr_table ();
1077 scm_gc_mark (scm_root
->handle
);
1080 t_before_sweep
= scm_c_get_internal_run_time ();
1081 scm_gc_mark_time_taken
+= (t_before_sweep
- t_before_gc
);
1083 scm_c_hook_run (&scm_before_sweep_c_hook
, 0);
1087 scm_c_hook_run (&scm_after_sweep_c_hook
, 0);
1093 SCM_THREAD_CRITICAL_SECTION_END
;
1095 scm_c_hook_run (&scm_after_gc_c_hook
, 0);
1104 #define MARK scm_gc_mark
1105 #define FNAME "scm_gc_mark"
1107 #endif /*!MARK_DEPENDENCIES*/
1109 /* Mark an object precisely.
1113 #define FUNC_NAME FNAME
1118 #ifndef MARK_DEPENDENCIES
1119 # define RECURSE scm_gc_mark
1121 /* go through the usual marking, but not for self-cycles. */
1122 # define RECURSE(x) do { if ((x) != p) scm_gc_mark (x); } while (0)
1126 #ifdef MARK_DEPENDENCIES
1127 goto gc_mark_loop_first_time
;
1136 #ifdef MARK_DEPENDENCIES
1137 if (SCM_EQ_P (ptr
, p
))
1143 gc_mark_loop_first_time
:
1146 if (!SCM_CELLP (ptr
))
1147 SCM_MISC_ERROR ("rogue pointer in heap", SCM_EOL
);
1149 #if (defined (GUILE_DEBUG_FREELIST))
1151 if (SCM_GC_IN_CARD_HEADERP (SCM2PTR (ptr
)))
1152 SCM_MISC_ERROR ("rogue pointer in heap", SCM_EOL
);
1156 #ifndef MARK_DEPENDENCIES
1158 if (SCM_GCMARKP (ptr
))
1161 SCM_SETGCMARK (ptr
);
1165 switch (SCM_TYP7 (ptr
))
1167 case scm_tcs_cons_nimcar
:
1168 if (SCM_IMP (SCM_CDR (ptr
)))
1170 ptr
= SCM_CAR (ptr
);
1173 RECURSE (SCM_CAR (ptr
));
1174 ptr
= SCM_CDR (ptr
);
1176 case scm_tcs_cons_imcar
:
1177 ptr
= SCM_CDR (ptr
);
1180 RECURSE (SCM_CELL_OBJECT_2 (ptr
));
1181 ptr
= SCM_CDR (ptr
);
1183 case scm_tcs_cons_gloc
:
1185 /* Dirk:FIXME:: The following code is super ugly: ptr may be a struct
1186 * or a gloc. If it is a gloc, the cell word #0 of ptr is a pointer
1187 * to a heap cell. If it is a struct, the cell word #0 of ptr is a
1188 * pointer to a struct vtable data region. The fact that these are
1189 * accessed in the same way restricts the possibilites to change the
1190 * data layout of structs or heap cells.
1192 scm_bits_t word0
= SCM_CELL_WORD_0 (ptr
) - scm_tc3_cons_gloc
;
1193 scm_bits_t
* vtable_data
= (scm_bits_t
*) word0
; /* access as struct */
1194 if (vtable_data
[scm_vtable_index_vcell
] != 0)
1197 SCM gloc_car
= SCM_PACK (word0
);
1199 ptr
= SCM_CDR (ptr
);
1204 /* ptr is a struct */
1205 SCM layout
= SCM_PACK (vtable_data
[scm_vtable_index_layout
]);
1206 int len
= SCM_SYMBOL_LENGTH (layout
);
1207 char * fields_desc
= SCM_SYMBOL_CHARS (layout
);
1208 scm_bits_t
* struct_data
= (scm_bits_t
*) SCM_STRUCT_DATA (ptr
);
1210 if (vtable_data
[scm_struct_i_flags
] & SCM_STRUCTF_ENTITY
)
1212 RECURSE (SCM_PACK (struct_data
[scm_struct_i_procedure
]));
1213 RECURSE (SCM_PACK (struct_data
[scm_struct_i_setter
]));
1219 for (x
= 0; x
< len
- 2; x
+= 2, ++struct_data
)
1220 if (fields_desc
[x
] == 'p')
1221 RECURSE (SCM_PACK (*struct_data
));
1222 if (fields_desc
[x
] == 'p')
1224 if (SCM_LAYOUT_TAILP (fields_desc
[x
+ 1]))
1225 for (x
= *struct_data
++; x
; --x
, ++struct_data
)
1226 RECURSE (SCM_PACK (*struct_data
));
1228 RECURSE (SCM_PACK (*struct_data
));
1232 ptr
= SCM_PACK (vtable_data
[scm_vtable_index_vtable
]);
1237 case scm_tcs_closures
:
1238 if (SCM_IMP (SCM_CDR (ptr
)))
1240 ptr
= SCM_CLOSCAR (ptr
);
1243 RECURSE (SCM_CLOSCAR (ptr
));
1244 ptr
= SCM_CDR (ptr
);
1246 case scm_tc7_vector
:
1247 i
= SCM_VECTOR_LENGTH (ptr
);
1251 if (SCM_NIMP (SCM_VELTS (ptr
)[i
]))
1252 RECURSE (SCM_VELTS (ptr
)[i
]);
1253 ptr
= SCM_VELTS (ptr
)[0];
1258 unsigned long int i
= SCM_CCLO_LENGTH (ptr
);
1259 unsigned long int j
;
1260 for (j
= 1; j
!= i
; ++j
)
1262 SCM obj
= SCM_CCLO_REF (ptr
, j
);
1266 ptr
= SCM_CCLO_REF (ptr
, 0);
1272 case scm_tc7_byvect
:
1279 #ifdef HAVE_LONG_LONGS
1280 case scm_tc7_llvect
:
1283 case scm_tc7_string
:
1286 case scm_tc7_substring
:
1287 ptr
= SCM_CDR (ptr
);
1291 SCM_WVECT_GC_CHAIN (ptr
) = scm_weak_vectors
;
1292 scm_weak_vectors
= ptr
;
1293 if (SCM_IS_WHVEC_ANY (ptr
))
1300 len
= SCM_VECTOR_LENGTH (ptr
);
1301 weak_keys
= SCM_IS_WHVEC (ptr
) || SCM_IS_WHVEC_B (ptr
);
1302 weak_values
= SCM_IS_WHVEC_V (ptr
) || SCM_IS_WHVEC_B (ptr
);
1304 for (x
= 0; x
< len
; ++x
)
1307 alist
= SCM_VELTS (ptr
)[x
];
1309 /* mark everything on the alist except the keys or
1310 * values, according to weak_values and weak_keys. */
1311 while ( SCM_CONSP (alist
)
1312 && !SCM_GCMARKP (alist
)
1313 && SCM_CONSP (SCM_CAR (alist
)))
1318 kvpair
= SCM_CAR (alist
);
1319 next_alist
= SCM_CDR (alist
);
1322 * SCM_SETGCMARK (alist);
1323 * SCM_SETGCMARK (kvpair);
1325 * It may be that either the key or value is protected by
1326 * an escaped reference to part of the spine of this alist.
1327 * If we mark the spine here, and only mark one or neither of the
1328 * key and value, they may never be properly marked.
1329 * This leads to a horrible situation in which an alist containing
1330 * freelist cells is exported.
1332 * So only mark the spines of these arrays last of all marking.
1333 * If somebody confuses us by constructing a weak vector
1334 * with a circular alist then we are hosed, but at least we
1335 * won't prematurely drop table entries.
1338 RECURSE (SCM_CAR (kvpair
));
1340 RECURSE (SCM_CDR (kvpair
));
1343 if (SCM_NIMP (alist
))
1349 case scm_tc7_symbol
:
1350 ptr
= SCM_PROP_SLOTS (ptr
);
1355 i
= SCM_PTOBNUM (ptr
);
1356 if (!(i
< scm_numptob
))
1358 if (SCM_PTAB_ENTRY(ptr
))
1359 RECURSE (SCM_FILENAME (ptr
));
1360 if (scm_ptobs
[i
].mark
)
1362 ptr
= (scm_ptobs
[i
].mark
) (ptr
);
1369 switch (SCM_TYP16 (ptr
))
1370 { /* should be faster than going through scm_smobs */
1371 case scm_tc_free_cell
:
1372 /* printf("found free_cell %X ", ptr); fflush(stdout); */
1375 case scm_tc16_complex
:
1378 i
= SCM_SMOBNUM (ptr
);
1379 if (!(i
< scm_numsmob
))
1381 if (scm_smobs
[i
].mark
)
1383 ptr
= (scm_smobs
[i
].mark
) (ptr
);
1392 SCM_MISC_ERROR ("unknown type", SCM_EOL
);
1398 #ifndef MARK_DEPENDENCIES
1403 /* And here we define `scm_gc_mark_dependencies', by including this
1404 * same file in itself.
1406 #define MARK scm_gc_mark_dependencies
1407 #define FNAME "scm_gc_mark_dependencies"
1408 #define MARK_DEPENDENCIES
1410 #undef MARK_DEPENDENCIES
1415 /* Mark a Region Conservatively
1419 scm_mark_locations (SCM_STACKITEM x
[], scm_sizet n
)
1423 for (m
= 0; m
< n
; ++m
)
1425 SCM obj
= * (SCM
*) &x
[m
];
1426 if (SCM_CELLP (obj
))
1428 SCM_CELLPTR ptr
= SCM2PTR (obj
);
1430 int j
= scm_n_heap_segs
- 1;
1431 if (SCM_PTR_LE (scm_heap_table
[i
].bounds
[0], ptr
)
1432 && SCM_PTR_GT (scm_heap_table
[j
].bounds
[1], ptr
))
1439 || SCM_PTR_GT (scm_heap_table
[i
].bounds
[1], ptr
))
1441 else if (SCM_PTR_LE (scm_heap_table
[j
].bounds
[0], ptr
))
1449 if (SCM_PTR_GT (scm_heap_table
[k
].bounds
[1], ptr
))
1453 if (SCM_PTR_LE (scm_heap_table
[i
].bounds
[0], ptr
))
1458 else if (SCM_PTR_LE (scm_heap_table
[k
].bounds
[0], ptr
))
1462 if (SCM_PTR_GT (scm_heap_table
[j
].bounds
[1], ptr
))
1469 if (SCM_GC_IN_CARD_HEADERP (ptr
))
1472 if (scm_heap_table
[seg_id
].span
== 1
1473 || DOUBLECELL_ALIGNED_P (obj
))
1484 /* The function scm_cellp determines whether an SCM value can be regarded as a
1485 * pointer to a cell on the heap. Binary search is used in order to determine
1486 * the heap segment that contains the cell.
1489 scm_cellp (SCM value
)
1491 if (SCM_CELLP (value
)) {
1492 scm_cell
* ptr
= SCM2PTR (value
);
1494 unsigned int j
= scm_n_heap_segs
- 1;
1497 int k
= (i
+ j
) / 2;
1498 if (SCM_PTR_GT (scm_heap_table
[k
].bounds
[1], ptr
)) {
1500 } else if (SCM_PTR_LE (scm_heap_table
[k
].bounds
[0], ptr
)) {
1505 if (SCM_PTR_LE (scm_heap_table
[i
].bounds
[0], ptr
)
1506 && SCM_PTR_GT (scm_heap_table
[i
].bounds
[1], ptr
)
1507 && (scm_heap_table
[i
].span
== 1 || DOUBLECELL_ALIGNED_P (value
))
1508 && !SCM_GC_IN_CARD_HEADERP (ptr
)
1519 gc_sweep_freelist_start (scm_freelist_t
*freelist
)
1521 freelist
->cells
= SCM_EOL
;
1522 freelist
->left_to_collect
= freelist
->cluster_size
;
1523 freelist
->clusters_allocated
= 0;
1524 freelist
->clusters
= SCM_EOL
;
1525 freelist
->clustertail
= &freelist
->clusters
;
1526 freelist
->collected_1
= freelist
->collected
;
1527 freelist
->collected
= 0;
1531 gc_sweep_freelist_finish (scm_freelist_t
*freelist
)
1534 *freelist
->clustertail
= freelist
->cells
;
1535 if (!SCM_NULLP (freelist
->cells
))
1537 SCM c
= freelist
->cells
;
1538 SCM_SETCAR (c
, SCM_CDR (c
));
1539 SCM_SETCDR (c
, SCM_EOL
);
1540 freelist
->collected
+=
1541 freelist
->span
* (freelist
->cluster_size
- freelist
->left_to_collect
);
1543 scm_gc_cells_collected
+= freelist
->collected
;
1545 /* Although freelist->min_yield is used to test freelist->collected
1546 * (which is the local GC yield for freelist), it is adjusted so
1547 * that *total* yield is freelist->min_yield_fraction of total heap
1548 * size. This means that a too low yield is compensated by more
1549 * heap on the list which is currently doing most work, which is
1550 * just what we want.
1552 collected
= SCM_MAX (freelist
->collected_1
, freelist
->collected
);
1553 freelist
->grow_heap_p
= (collected
< freelist
->min_yield
);
1556 #define NEXT_DATA_CELL(ptr, span) \
1558 scm_cell *nxt__ = CELL_UP ((char *) (ptr) + 1, (span)); \
1559 (ptr) = (SCM_GC_IN_CARD_HEADERP (nxt__) ? \
1560 CELL_UP (SCM_GC_CELL_CARD (nxt__) + SCM_GC_CARD_N_HEADER_CELLS, span) \
1566 #define FUNC_NAME "scm_gc_sweep"
1568 register SCM_CELLPTR ptr
;
1569 register SCM nfreelist
;
1570 register scm_freelist_t
*freelist
;
1578 gc_sweep_freelist_start (&scm_master_freelist
);
1579 gc_sweep_freelist_start (&scm_master_freelist2
);
1581 for (i
= 0; i
< scm_n_heap_segs
; i
++)
1583 register unsigned int left_to_collect
;
1584 register scm_sizet j
;
1586 /* Unmarked cells go onto the front of the freelist this heap
1587 segment points to. Rather than updating the real freelist
1588 pointer as we go along, we accumulate the new head in
1589 nfreelist. Then, if it turns out that the entire segment is
1590 free, we free (i.e., malloc's free) the whole segment, and
1591 simply don't assign nfreelist back into the real freelist. */
1592 freelist
= scm_heap_table
[i
].freelist
;
1593 nfreelist
= freelist
->cells
;
1594 left_to_collect
= freelist
->left_to_collect
;
1595 span
= scm_heap_table
[i
].span
;
1597 ptr
= CELL_UP (scm_heap_table
[i
].bounds
[0], span
);
1598 seg_size
= CELL_DN (scm_heap_table
[i
].bounds
[1], span
) - ptr
;
1600 /* use only data cells in seg_size */
1601 seg_size
= (seg_size
/ SCM_GC_CARD_N_CELLS
) * (SCM_GC_CARD_N_DATA_CELLS
/ span
) * span
;
1603 scm_gc_cells_swept
+= seg_size
;
1605 for (j
= seg_size
+ span
; j
-= span
; ptr
+= span
)
1609 if (SCM_GC_IN_CARD_HEADERP (ptr
))
1615 NEXT_DATA_CELL (nxt
, span
);
1622 scmptr
= PTR2SCM (ptr
);
1624 if (SCM_GCMARKP (scmptr
))
1627 switch SCM_TYP7 (scmptr
)
1629 case scm_tcs_cons_gloc
:
1631 /* Dirk:FIXME:: Again, super ugly code: scmptr may be a
1632 * struct or a gloc. See the corresponding comment in
1635 scm_bits_t word0
= (SCM_CELL_WORD_0 (scmptr
)
1636 - scm_tc3_cons_gloc
);
1637 /* access as struct */
1638 scm_bits_t
* vtable_data
= (scm_bits_t
*) word0
;
1639 if (vtable_data
[scm_vtable_index_vcell
] == 0)
1641 /* Structs need to be freed in a special order.
1642 * This is handled by GC C hooks in struct.c.
1644 SCM_SET_STRUCT_GC_CHAIN (scmptr
, scm_structs_to_free
);
1645 scm_structs_to_free
= scmptr
;
1648 /* fall through so that scmptr gets collected */
1651 case scm_tcs_cons_imcar
:
1652 case scm_tcs_cons_nimcar
:
1653 case scm_tcs_closures
:
1657 m
+= (2 + SCM_VECTOR_LENGTH (scmptr
)) * sizeof (SCM
);
1658 scm_must_free (SCM_VECTOR_BASE (scmptr
) - 2);
1660 case scm_tc7_vector
:
1662 unsigned long int length
= SCM_VECTOR_LENGTH (scmptr
);
1665 m
+= length
* sizeof (scm_bits_t
);
1666 scm_must_free (SCM_VECTOR_BASE (scmptr
));
1672 m
+= (SCM_CCLO_LENGTH (scmptr
) * sizeof (SCM
));
1673 scm_must_free (SCM_CCLO_BASE (scmptr
));
1679 unsigned long int length
= SCM_BITVECTOR_LENGTH (scmptr
);
1682 m
+= sizeof (long) * ((length
+ SCM_LONG_BIT
- 1) / SCM_LONG_BIT
);
1683 scm_must_free (SCM_BITVECTOR_BASE (scmptr
));
1687 case scm_tc7_byvect
:
1691 #ifdef HAVE_LONG_LONGS
1692 case scm_tc7_llvect
:
1697 m
+= SCM_UVECTOR_LENGTH (scmptr
) * scm_uniform_element_size (scmptr
);
1698 scm_must_free (SCM_UVECTOR_BASE (scmptr
));
1701 case scm_tc7_substring
:
1703 case scm_tc7_string
:
1704 m
+= SCM_STRING_LENGTH (scmptr
) + 1;
1705 scm_must_free (SCM_STRING_CHARS (scmptr
));
1707 case scm_tc7_symbol
:
1708 m
+= SCM_SYMBOL_LENGTH (scmptr
) + 1;
1709 scm_must_free (SCM_SYMBOL_CHARS (scmptr
));
1712 /* the various "subrs" (primitives) are never freed */
1715 if SCM_OPENP (scmptr
)
1717 int k
= SCM_PTOBNUM (scmptr
);
1718 if (!(k
< scm_numptob
))
1720 /* Keep "revealed" ports alive. */
1721 if (scm_revealed_count (scmptr
) > 0)
1723 /* Yes, I really do mean scm_ptobs[k].free */
1724 /* rather than ftobs[k].close. .close */
1725 /* is for explicit CLOSE-PORT by user */
1726 m
+= (scm_ptobs
[k
].free
) (scmptr
);
1727 SCM_SETSTREAM (scmptr
, 0);
1728 scm_remove_from_port_table (scmptr
);
1729 scm_gc_ports_collected
++;
1730 SCM_SETAND_CAR (scmptr
, ~SCM_OPN
);
1734 switch SCM_TYP16 (scmptr
)
1736 case scm_tc_free_cell
:
1741 m
+= (SCM_NUMDIGS (scmptr
) * SCM_BITSPERDIG
/ SCM_CHAR_BIT
);
1742 scm_must_free (SCM_BDIGITS (scmptr
));
1744 #endif /* def SCM_BIGDIG */
1745 case scm_tc16_complex
:
1746 m
+= sizeof (scm_complex_t
);
1747 scm_must_free (SCM_COMPLEX_MEM (scmptr
));
1752 k
= SCM_SMOBNUM (scmptr
);
1753 if (!(k
< scm_numsmob
))
1755 m
+= (scm_smobs
[k
].free
) (scmptr
);
1762 SCM_MISC_ERROR ("unknown type", SCM_EOL
);
1765 if (!--left_to_collect
)
1767 SCM_SETCAR (scmptr
, nfreelist
);
1768 *freelist
->clustertail
= scmptr
;
1769 freelist
->clustertail
= SCM_CDRLOC (scmptr
);
1771 nfreelist
= SCM_EOL
;
1772 freelist
->collected
+= span
* freelist
->cluster_size
;
1773 left_to_collect
= freelist
->cluster_size
;
1777 /* Stick the new cell on the front of nfreelist. It's
1778 critical that we mark this cell as freed; otherwise, the
1779 conservative collector might trace it as some other type
1781 SCM_SET_CELL_TYPE (scmptr
, scm_tc_free_cell
);
1782 SCM_SET_FREE_CELL_CDR (scmptr
, nfreelist
);
1787 #ifdef GC_FREE_SEGMENTS
1792 freelist
->heap_size
-= seg_size
;
1793 free ((char *) scm_heap_table
[i
].bounds
[0]);
1794 scm_heap_table
[i
].bounds
[0] = 0;
1795 for (j
= i
+ 1; j
< scm_n_heap_segs
; j
++)
1796 scm_heap_table
[j
- 1] = scm_heap_table
[j
];
1797 scm_n_heap_segs
-= 1;
1798 i
--; /* We need to scan the segment just moved. */
1801 #endif /* ifdef GC_FREE_SEGMENTS */
1803 /* Update the real freelist pointer to point to the head of
1804 the list of free cells we've built for this segment. */
1805 freelist
->cells
= nfreelist
;
1806 freelist
->left_to_collect
= left_to_collect
;
1809 #ifdef GUILE_DEBUG_FREELIST
1810 scm_map_free_list ();
1814 gc_sweep_freelist_finish (&scm_master_freelist
);
1815 gc_sweep_freelist_finish (&scm_master_freelist2
);
1817 /* When we move to POSIX threads private freelists should probably
1818 be GC-protected instead. */
1819 scm_freelist
= SCM_EOL
;
1820 scm_freelist2
= SCM_EOL
;
1822 scm_cells_allocated
= (SCM_HEAP_SIZE
- scm_gc_cells_collected
);
1823 scm_gc_yield
-= scm_cells_allocated
;
1824 scm_mallocated
-= m
;
1825 scm_gc_malloc_collected
= m
;
1831 /* {Front end to malloc}
1833 * scm_must_malloc, scm_must_realloc, scm_must_free, scm_done_malloc,
1836 * These functions provide services comperable to malloc, realloc, and
1837 * free. They are for allocating malloced parts of scheme objects.
1838 * The primary purpose of the front end is to impose calls to gc. */
1842 * Return newly malloced storage or throw an error.
1844 * The parameter WHAT is a string for error reporting.
1845 * If the threshold scm_mtrigger will be passed by this
1846 * allocation, or if the first call to malloc fails,
1847 * garbage collect -- on the presumption that some objects
1848 * using malloced storage may be collected.
1850 * The limit scm_mtrigger may be raised by this allocation.
1853 scm_must_malloc (scm_sizet size
, const char *what
)
1856 unsigned long nm
= scm_mallocated
+ size
;
1858 if (nm
<= scm_mtrigger
)
1860 SCM_SYSCALL (ptr
= malloc (size
));
1863 scm_mallocated
= nm
;
1864 #ifdef GUILE_DEBUG_MALLOC
1865 scm_malloc_register (ptr
, what
);
1873 nm
= scm_mallocated
+ size
;
1874 SCM_SYSCALL (ptr
= malloc (size
));
1877 scm_mallocated
= nm
;
1878 if (nm
> scm_mtrigger
- SCM_MTRIGGER_HYSTERESIS
) {
1879 if (nm
> scm_mtrigger
)
1880 scm_mtrigger
= nm
+ nm
/ 2;
1882 scm_mtrigger
+= scm_mtrigger
/ 2;
1884 #ifdef GUILE_DEBUG_MALLOC
1885 scm_malloc_register (ptr
, what
);
1891 scm_memory_error (what
);
1896 * is similar to scm_must_malloc.
1899 scm_must_realloc (void *where
,
1905 scm_sizet nm
= scm_mallocated
+ size
- old_size
;
1907 if (nm
<= scm_mtrigger
)
1909 SCM_SYSCALL (ptr
= realloc (where
, size
));
1912 scm_mallocated
= nm
;
1913 #ifdef GUILE_DEBUG_MALLOC
1914 scm_malloc_reregister (where
, ptr
, what
);
1922 nm
= scm_mallocated
+ size
- old_size
;
1923 SCM_SYSCALL (ptr
= realloc (where
, size
));
1926 scm_mallocated
= nm
;
1927 if (nm
> scm_mtrigger
- SCM_MTRIGGER_HYSTERESIS
) {
1928 if (nm
> scm_mtrigger
)
1929 scm_mtrigger
= nm
+ nm
/ 2;
1931 scm_mtrigger
+= scm_mtrigger
/ 2;
1933 #ifdef GUILE_DEBUG_MALLOC
1934 scm_malloc_reregister (where
, ptr
, what
);
1939 scm_memory_error (what
);
1944 scm_must_free (void *obj
)
1945 #define FUNC_NAME "scm_must_free"
1947 #ifdef GUILE_DEBUG_MALLOC
1948 scm_malloc_unregister (obj
);
1953 SCM_MISC_ERROR ("freeing NULL pointer", SCM_EOL
);
1958 /* Announce that there has been some malloc done that will be freed
1959 * during gc. A typical use is for a smob that uses some malloced
1960 * memory but can not get it from scm_must_malloc (for whatever
1961 * reason). When a new object of this smob is created you call
1962 * scm_done_malloc with the size of the object. When your smob free
1963 * function is called, be sure to include this size in the return
1966 * If you can't actually free the memory in the smob free function,
1967 * for whatever reason (like reference counting), you still can (and
1968 * should) report the amount of memory freed when you actually free it.
1969 * Do it by calling scm_done_malloc with the _negated_ size. Clever,
1970 * eh? Or even better, call scm_done_free. */
1973 scm_done_malloc (long size
)
1975 scm_mallocated
+= size
;
1977 if (scm_mallocated
> scm_mtrigger
)
1979 scm_igc ("foreign mallocs");
1980 if (scm_mallocated
> scm_mtrigger
- SCM_MTRIGGER_HYSTERESIS
)
1982 if (scm_mallocated
> scm_mtrigger
)
1983 scm_mtrigger
= scm_mallocated
+ scm_mallocated
/ 2;
1985 scm_mtrigger
+= scm_mtrigger
/ 2;
1991 scm_done_free (long size
)
1993 scm_mallocated
-= size
;
2000 * Each heap segment is an array of objects of a particular size.
2001 * Every segment has an associated (possibly shared) freelist.
2002 * A table of segment records is kept that records the upper and
2003 * lower extents of the segment; this is used during the conservative
2004 * phase of gc to identify probably gc roots (because they point
2005 * into valid segments at reasonable offsets). */
2008 * is true if the first segment was smaller than INIT_HEAP_SEG.
2009 * If scm_expmem is set to one, subsequent segment allocations will
2010 * allocate segments of size SCM_EXPHEAP(scm_heap_size).
2014 scm_sizet scm_max_segment_size
;
2017 * is the lowest base address of any heap segment.
2019 SCM_CELLPTR scm_heap_org
;
2021 scm_heap_seg_data_t
* scm_heap_table
= 0;
2022 static unsigned int heap_segment_table_size
= 0;
2023 int scm_n_heap_segs
= 0;
2026 * initializes a new heap segment and returns the number of objects it contains.
2028 * The segment origin and segment size in bytes are input parameters.
2029 * The freelist is both input and output.
2031 * This function presumes that the scm_heap_table has already been expanded
2032 * to accomodate a new segment record and that the markbit space was reserved
2033 * for all the cards in this segment.
2036 #define INIT_CARD(card, span) \
2038 SCM_GC_SET_CARD_BVEC (card, get_bvec ()); \
2040 SCM_GC_SET_CARD_DOUBLECELL (card); \
2044 init_heap_seg (SCM_CELLPTR seg_org
, scm_sizet size
, scm_freelist_t
*freelist
)
2046 register SCM_CELLPTR ptr
;
2047 SCM_CELLPTR seg_end
;
2050 int span
= freelist
->span
;
2052 if (seg_org
== NULL
)
2055 /* Align the begin ptr up.
2057 ptr
= SCM_GC_CARD_UP (seg_org
);
2059 /* Compute the ceiling on valid object pointers w/in this segment.
2061 seg_end
= SCM_GC_CARD_DOWN ((char *)seg_org
+ size
);
2063 /* Find the right place and insert the segment record.
2066 for (new_seg_index
= 0;
2067 ( (new_seg_index
< scm_n_heap_segs
)
2068 && SCM_PTR_LE (scm_heap_table
[new_seg_index
].bounds
[0], seg_org
));
2074 for (i
= scm_n_heap_segs
; i
> new_seg_index
; --i
)
2075 scm_heap_table
[i
] = scm_heap_table
[i
- 1];
2080 scm_heap_table
[new_seg_index
].span
= span
;
2081 scm_heap_table
[new_seg_index
].freelist
= freelist
;
2082 scm_heap_table
[new_seg_index
].bounds
[0] = ptr
;
2083 scm_heap_table
[new_seg_index
].bounds
[1] = seg_end
;
2086 n_new_cells
= seg_end
- ptr
;
2088 freelist
->heap_size
+= n_new_cells
;
2090 /* Partition objects in this segment into clusters */
2093 SCM
*clusterp
= &clusters
;
2095 NEXT_DATA_CELL (ptr
, span
);
2096 while (ptr
< seg_end
)
2098 scm_cell
*nxt
= ptr
;
2099 scm_cell
*prv
= NULL
;
2100 scm_cell
*last_card
= NULL
;
2101 int n_data_cells
= (SCM_GC_CARD_N_DATA_CELLS
/ span
) * SCM_CARDS_PER_CLUSTER
- 1;
2102 NEXT_DATA_CELL(nxt
, span
);
2104 /* Allocate cluster spine
2106 *clusterp
= PTR2SCM (ptr
);
2107 SCM_SETCAR (*clusterp
, PTR2SCM (nxt
));
2108 clusterp
= SCM_CDRLOC (*clusterp
);
2111 while (n_data_cells
--)
2113 scm_cell
*card
= SCM_GC_CELL_CARD (ptr
);
2114 SCM scmptr
= PTR2SCM (ptr
);
2116 NEXT_DATA_CELL (nxt
, span
);
2119 if (card
!= last_card
)
2121 INIT_CARD (card
, span
);
2125 SCM_SET_CELL_TYPE (scmptr
, scm_tc_free_cell
);
2126 SCM_SETCDR (scmptr
, PTR2SCM (nxt
));
2131 SCM_SET_FREE_CELL_CDR (PTR2SCM (prv
), SCM_EOL
);
2136 scm_cell
*ref
= seg_end
;
2137 NEXT_DATA_CELL (ref
, span
);
2139 /* [cmm] looks like the segment size doesn't divide cleanly by
2140 cluster size. bad cmm! */
2144 /* Patch up the last cluster pointer in the segment
2145 * to join it to the input freelist.
2147 *clusterp
= freelist
->clusters
;
2148 freelist
->clusters
= clusters
;
2152 fprintf (stderr
, "H");
2158 round_to_cluster_size (scm_freelist_t
*freelist
, scm_sizet len
)
2160 scm_sizet cluster_size_in_bytes
= CLUSTER_SIZE_IN_BYTES (freelist
);
2163 (len
+ cluster_size_in_bytes
- 1) / cluster_size_in_bytes
* cluster_size_in_bytes
2164 + ALIGNMENT_SLACK (freelist
);
2168 alloc_some_heap (scm_freelist_t
*freelist
, policy_on_error error_policy
)
2169 #define FUNC_NAME "alloc_some_heap"
2174 if (scm_gc_heap_lock
)
2176 /* Critical code sections (such as the garbage collector) aren't
2177 * supposed to add heap segments.
2179 fprintf (stderr
, "alloc_some_heap: Can not extend locked heap.\n");
2183 if (scm_n_heap_segs
== heap_segment_table_size
)
2185 /* We have to expand the heap segment table to have room for the new
2186 * segment. Do not yet increment scm_n_heap_segs -- that is done by
2187 * init_heap_seg only if the allocation of the segment itself succeeds.
2189 unsigned int new_table_size
= scm_n_heap_segs
+ 1;
2190 size_t size
= new_table_size
* sizeof (scm_heap_seg_data_t
);
2191 scm_heap_seg_data_t
* new_heap_table
;
2193 SCM_SYSCALL (new_heap_table
= ((scm_heap_seg_data_t
*)
2194 realloc ((char *)scm_heap_table
, size
)));
2195 if (!new_heap_table
)
2197 if (error_policy
== abort_on_error
)
2199 fprintf (stderr
, "alloc_some_heap: Could not grow heap segment table.\n");
2209 scm_heap_table
= new_heap_table
;
2210 heap_segment_table_size
= new_table_size
;
2214 /* Pick a size for the new heap segment.
2215 * The rule for picking the size of a segment is explained in
2219 /* Assure that the new segment is predicted to be large enough.
2221 * New yield should at least equal GC fraction of new heap size, i.e.
2223 * y + dh > f * (h + dh)
2226 * f : min yield fraction
2228 * dh : size of new heap segment
2230 * This gives dh > (f * h - y) / (1 - f)
2232 int f
= freelist
->min_yield_fraction
;
2233 long h
= SCM_HEAP_SIZE
;
2234 long min_cells
= (f
* h
- 100 * (long) scm_gc_yield
) / (99 - f
);
2235 len
= SCM_EXPHEAP (freelist
->heap_size
);
2237 fprintf (stderr
, "(%d < %d)", len
, min_cells
);
2239 if (len
< min_cells
)
2240 len
= min_cells
+ freelist
->cluster_size
;
2241 len
*= sizeof (scm_cell
);
2242 /* force new sampling */
2243 freelist
->collected
= LONG_MAX
;
2246 if (len
> scm_max_segment_size
)
2247 len
= scm_max_segment_size
;
2252 smallest
= CLUSTER_SIZE_IN_BYTES (freelist
);
2257 /* Allocate with decaying ambition. */
2258 while ((len
>= SCM_MIN_HEAP_SEG_SIZE
)
2259 && (len
>= smallest
))
2261 scm_sizet rounded_len
= round_to_cluster_size (freelist
, len
);
2262 SCM_SYSCALL (ptr
= (SCM_CELLPTR
) malloc (rounded_len
));
2265 init_heap_seg (ptr
, rounded_len
, freelist
);
2272 if (error_policy
== abort_on_error
)
2274 fprintf (stderr
, "alloc_some_heap: Could not grow heap.\n");
2281 SCM_DEFINE (scm_unhash_name
, "unhash-name", 1, 0, 0,
2283 "Flushes the glocs for @var{name}, or all glocs if @var{name}\n"
2285 #define FUNC_NAME s_scm_unhash_name
2289 SCM_VALIDATE_SYMBOL (1,name
);
2291 bound
= scm_n_heap_segs
;
2292 for (x
= 0; x
< bound
; ++x
)
2296 p
= scm_heap_table
[x
].bounds
[0];
2297 pbound
= scm_heap_table
[x
].bounds
[1];
2300 SCM cell
= PTR2SCM (p
);
2301 if (SCM_TYP3 (cell
) == scm_tc3_cons_gloc
)
2303 /* Dirk:FIXME:: Again, super ugly code: cell may be a gloc or a
2304 * struct cell. See the corresponding comment in scm_gc_mark.
2306 scm_bits_t word0
= SCM_CELL_WORD_0 (cell
) - scm_tc3_cons_gloc
;
2307 SCM gloc_car
= SCM_PACK (word0
); /* access as gloc */
2308 SCM vcell
= SCM_CELL_OBJECT_1 (gloc_car
);
2309 if ((SCM_EQ_P (name
, SCM_BOOL_T
) || SCM_EQ_P (SCM_CAR (gloc_car
), name
))
2310 && (SCM_UNPACK (vcell
) != 0) && (SCM_UNPACK (vcell
) != 1))
2312 SCM_SET_CELL_OBJECT_0 (cell
, name
);
2325 /* {GC Protection Helper Functions}
2330 * If within a function you need to protect one or more scheme objects from
2331 * garbage collection, pass them as parameters to one of the
2332 * scm_remember_upto_here* functions below. These functions don't do
2333 * anything, but since the compiler does not know that they are actually
2334 * no-ops, it will generate code that calls these functions with the given
2335 * parameters. Therefore, you can be sure that the compiler will keep those
2336 * scheme values alive (on the stack or in a register) up to the point where
2337 * scm_remember_upto_here* is called. In other words, place the call to
2338 * scm_remember_upt_here* _behind_ the last code in your function, that
2339 * depends on the scheme object to exist.
2341 * Example: We want to make sure, that the string object str does not get
2342 * garbage collected during the execution of 'some_function', because
2343 * otherwise the characters belonging to str would be freed and
2344 * 'some_function' might access freed memory. To make sure that the compiler
2345 * keeps str alive on the stack or in a register such that it is visible to
2346 * the conservative gc we add the call to scm_remember_upto_here_1 _after_ the
2347 * call to 'some_function'. Note that this would not be necessary if str was
2348 * used anyway after the call to 'some_function'.
2349 * char *chars = SCM_STRING_CHARS (str);
2350 * some_function (chars);
2351 * scm_remember_upto_here_1 (str); // str will be alive up to this point.
2355 scm_remember_upto_here_1 (SCM obj
)
2357 /* Empty. Protects a single object from garbage collection. */
2361 scm_remember_upto_here_2 (SCM obj1
, SCM obj2
)
2363 /* Empty. Protects two objects from garbage collection. */
2367 scm_remember_upto_here (SCM obj
, ...)
2369 /* Empty. Protects any number of objects from garbage collection. */
2373 #if (SCM_DEBUG_DEPRECATED == 0)
2376 scm_remember (SCM
*ptr
)
2381 #endif /* SCM_DEBUG_DEPRECATED == 0 */
2384 These crazy functions prevent garbage collection
2385 of arguments after the first argument by
2386 ensuring they remain live throughout the
2387 function because they are used in the last
2388 line of the code block.
2389 It'd be better to have a nice compiler hint to
2390 aid the conservative stack-scanning GC. --03/09/00 gjb */
2392 scm_return_first (SCM elt
, ...)
2398 scm_return_first_int (int i
, ...)
2405 scm_permanent_object (SCM obj
)
2408 scm_permobjs
= scm_cons (obj
, scm_permobjs
);
2414 /* Protect OBJ from the garbage collector. OBJ will not be freed, even if all
2415 other references are dropped, until the object is unprotected by calling
2416 scm_unprotect_object (OBJ). Calls to scm_protect/unprotect_object nest,
2417 i. e. it is possible to protect the same object several times, but it is
2418 necessary to unprotect the object the same number of times to actually get
2419 the object unprotected. It is an error to unprotect an object more often
2420 than it has been protected before. The function scm_protect_object returns
2424 /* Implementation note: For every object X, there is a counter which
2425 scm_protect_object(X) increments and scm_unprotect_object(X) decrements.
2429 scm_protect_object (SCM obj
)
2433 /* This critical section barrier will be replaced by a mutex. */
2436 handle
= scm_hashq_create_handle_x (scm_protects
, obj
, SCM_MAKINUM (0));
2437 SCM_SETCDR (handle
, SCM_MAKINUM (SCM_INUM (SCM_CDR (handle
)) + 1));
2445 /* Remove any protection for OBJ established by a prior call to
2446 scm_protect_object. This function returns OBJ.
2448 See scm_protect_object for more information. */
2450 scm_unprotect_object (SCM obj
)
2454 /* This critical section barrier will be replaced by a mutex. */
2457 handle
= scm_hashq_get_handle (scm_protects
, obj
);
2459 if (SCM_IMP (handle
))
2461 fprintf (stderr
, "scm_unprotect_object called on unprotected object\n");
2466 unsigned long int count
= SCM_INUM (SCM_CDR (handle
)) - 1;
2468 scm_hashq_remove_x (scm_protects
, obj
);
2470 SCM_SETCDR (handle
, SCM_MAKINUM (count
));
2480 /* called on process termination. */
2486 extern int on_exit (void (*procp
) (), int arg
);
2489 cleanup (int status
, void *arg
)
2491 #error Dont know how to setup a cleanup handler on your system.
2496 scm_flush_all_ports ();
2501 make_initial_segment (scm_sizet init_heap_size
, scm_freelist_t
*freelist
)
2503 scm_sizet rounded_size
= round_to_cluster_size (freelist
, init_heap_size
);
2505 if (!init_heap_seg ((SCM_CELLPTR
) malloc (rounded_size
),
2509 rounded_size
= round_to_cluster_size (freelist
, SCM_HEAP_SEG_SIZE
);
2510 if (!init_heap_seg ((SCM_CELLPTR
) malloc (rounded_size
),
2518 if (freelist
->min_yield_fraction
)
2519 freelist
->min_yield
= (freelist
->heap_size
* freelist
->min_yield_fraction
2521 freelist
->grow_heap_p
= (freelist
->heap_size
< freelist
->min_yield
);
2528 init_freelist (scm_freelist_t
*freelist
,
2533 freelist
->clusters
= SCM_EOL
;
2534 freelist
->cluster_size
= cluster_size
+ 1;
2535 freelist
->left_to_collect
= 0;
2536 freelist
->clusters_allocated
= 0;
2537 freelist
->min_yield
= 0;
2538 freelist
->min_yield_fraction
= min_yield
;
2539 freelist
->span
= span
;
2540 freelist
->collected
= 0;
2541 freelist
->collected_1
= 0;
2542 freelist
->heap_size
= 0;
2546 /* Get an integer from an environment variable. */
2548 scm_i_getenv_int (const char *var
, int def
)
2550 char *end
, *val
= getenv (var
);
2554 res
= strtol (val
, &end
, 10);
2564 scm_sizet gc_trigger_1
;
2565 scm_sizet gc_trigger_2
;
2566 scm_sizet init_heap_size_1
;
2567 scm_sizet init_heap_size_2
;
2570 j
= SCM_NUM_PROTECTS
;
2572 scm_sys_protects
[--j
] = SCM_BOOL_F
;
2575 scm_freelist
= SCM_EOL
;
2576 scm_freelist2
= SCM_EOL
;
2577 gc_trigger_1
= scm_i_getenv_int ("GUILE_MIN_YIELD_1", scm_default_min_yield_1
);
2578 init_freelist (&scm_master_freelist
, 1, SCM_CLUSTER_SIZE_1
, gc_trigger_1
);
2579 gc_trigger_2
= scm_i_getenv_int ("GUILE_MIN_YIELD_2", scm_default_min_yield_2
);
2580 init_freelist (&scm_master_freelist2
, 2, SCM_CLUSTER_SIZE_2
, gc_trigger_2
);
2581 scm_max_segment_size
= scm_i_getenv_int ("GUILE_MAX_SEGMENT_SIZE", scm_default_max_segment_size
);
2585 j
= SCM_HEAP_SEG_SIZE
;
2586 scm_mtrigger
= SCM_INIT_MALLOC_LIMIT
;
2587 scm_heap_table
= ((scm_heap_seg_data_t
*)
2588 scm_must_malloc (sizeof (scm_heap_seg_data_t
) * 2, "hplims"));
2589 heap_segment_table_size
= 2;
2591 mark_space_ptr
= &mark_space_head
;
2593 init_heap_size_1
= scm_i_getenv_int ("GUILE_INIT_SEGMENT_SIZE_1", scm_default_init_heap_size_1
);
2594 init_heap_size_2
= scm_i_getenv_int ("GUILE_INIT_SEGMENT_SIZE_2", scm_default_init_heap_size_2
);
2595 if (make_initial_segment (init_heap_size_1
, &scm_master_freelist
) ||
2596 make_initial_segment (init_heap_size_2
, &scm_master_freelist2
))
2599 /* scm_hplims[0] can change. do not remove scm_heap_org */
2600 scm_heap_org
= CELL_UP (scm_heap_table
[0].bounds
[0], 1);
2602 scm_c_hook_init (&scm_before_gc_c_hook
, 0, SCM_C_HOOK_NORMAL
);
2603 scm_c_hook_init (&scm_before_mark_c_hook
, 0, SCM_C_HOOK_NORMAL
);
2604 scm_c_hook_init (&scm_before_sweep_c_hook
, 0, SCM_C_HOOK_NORMAL
);
2605 scm_c_hook_init (&scm_after_sweep_c_hook
, 0, SCM_C_HOOK_NORMAL
);
2606 scm_c_hook_init (&scm_after_gc_c_hook
, 0, SCM_C_HOOK_NORMAL
);
2608 /* Initialise the list of ports. */
2609 scm_port_table
= (scm_port
**)
2610 malloc (sizeof (scm_port
*) * scm_port_table_room
);
2611 if (!scm_port_table
)
2618 on_exit (cleanup
, 0);
2622 scm_undefineds
= scm_cons (SCM_UNDEFINED
, SCM_EOL
);
2623 SCM_SETCDR (scm_undefineds
, scm_undefineds
);
2625 scm_listofnull
= scm_cons (SCM_EOL
, SCM_EOL
);
2626 scm_nullstr
= scm_makstr (0L, 0);
2627 scm_nullvect
= scm_c_make_vector (0, SCM_UNDEFINED
);
2629 #define DEFAULT_SYMHASH_SIZE 277
2630 scm_symhash
= scm_c_make_hash_table (DEFAULT_SYMHASH_SIZE
);
2631 scm_symhash_vars
= scm_c_make_hash_table (DEFAULT_SYMHASH_SIZE
);
2633 scm_stand_in_procs
= SCM_EOL
;
2634 scm_permobjs
= SCM_EOL
;
2635 scm_protects
= scm_c_make_hash_table (31);
2642 SCM scm_after_gc_hook
;
2644 #if (SCM_DEBUG_DEPRECATED == 0)
2645 static SCM scm_gc_vcell
; /* the vcell for gc-thunk. */
2646 #endif /* SCM_DEBUG_DEPRECATED == 0 */
2647 static SCM gc_async
;
2650 /* The function gc_async_thunk causes the execution of the after-gc-hook. It
2651 * is run after the gc, as soon as the asynchronous events are handled by the
2655 gc_async_thunk (void)
2657 scm_c_run_hook (scm_after_gc_hook
, SCM_EOL
);
2659 #if (SCM_DEBUG_DEPRECATED == 0)
2661 /* The following code will be removed in Guile 1.5. */
2662 if (SCM_NFALSEP (scm_gc_vcell
))
2664 SCM proc
= SCM_CDR (scm_gc_vcell
);
2666 if (SCM_NFALSEP (proc
) && !SCM_UNBNDP (proc
))
2667 scm_apply (proc
, SCM_EOL
, SCM_EOL
);
2670 #endif /* SCM_DEBUG_DEPRECATED == 0 */
2672 return SCM_UNSPECIFIED
;
2676 /* The function mark_gc_async is run by the scm_after_gc_c_hook at the end of
2677 * the garbage collection. The only purpose of this function is to mark the
2678 * gc_async (which will eventually lead to the execution of the
2682 mark_gc_async (void * hook_data
, void *func_data
, void *data
)
2684 scm_system_async_mark (gc_async
);
2694 scm_after_gc_hook
= scm_create_hook ("after-gc-hook", 0);
2696 #if (SCM_DEBUG_DEPRECATED == 0)
2697 scm_gc_vcell
= scm_sysintern ("gc-thunk", SCM_BOOL_F
);
2698 #endif /* SCM_DEBUG_DEPRECATED == 0 */
2699 after_gc_thunk
= scm_make_subr_opt ("%gc-thunk", scm_tc7_subr_0
, gc_async_thunk
, 0);
2700 gc_async
= scm_system_async (after_gc_thunk
); /* protected via scm_asyncs */
2702 scm_c_hook_add (&scm_after_gc_c_hook
, mark_gc_async
, NULL
, 0);
2704 #ifndef SCM_MAGIC_SNARFER
2705 #include "libguile/gc.x"
2709 #endif /*MARK_DEPENDENCIES*/