2 * private-gc.h - private declarations for garbage collection.
4 * Copyright (C) 2002, 03, 04, 05, 06, 07, 08 Free Software Foundation, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 /* {heap tuning parameters}
28 * These are parameters for controlling memory allocation. The heap
29 * is the area out of which scm_cons, and object headers are allocated.
31 * Each heap cell is 8 bytes on a 32 bit machine and 16 bytes on a
32 * 64 bit machine. The units of the _SIZE parameters are bytes.
33 * Cons pairs and object headers occupy one heap cell.
35 * SCM_INIT_HEAP_SIZE is the initial size of heap. If this much heap is
36 * allocated initially the heap will grow by half its current size
37 * each subsequent time more heap is needed.
39 * If SCM_INIT_HEAP_SIZE heap cannot be allocated initially, SCM_HEAP_SEG_SIZE
40 * will be used, and the heap will grow by SCM_HEAP_SEG_SIZE when more
41 * heap is needed. SCM_HEAP_SEG_SIZE must fit into type size_t. This code
42 * is in scm_init_storage() and alloc_some_heap() in sys.c
44 * If SCM_INIT_HEAP_SIZE can be allocated initially, the heap will grow by
45 * SCM_EXPHEAP(scm_heap_size) when more heap is needed.
47 * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap
53 * Heap size 45000 and 40% min yield gives quick startup and no extra
54 * heap allocation. Having higher values on min yield may lead to
55 * large heaps, especially if code behaviour is varying its
56 * maximum consumption between different freelists.
60 These values used to be global C variables. However, they're also
61 available through the environment, and having a double interface is
62 confusing. Now they're #defines --hwn.
65 #define SCM_DEFAULT_INIT_HEAP_SIZE_1 256*1024
66 #define SCM_DEFAULT_MIN_YIELD_1 40
67 #define SCM_DEFAULT_INIT_HEAP_SIZE_2 32*1024
69 /* The following value may seem large, but note that if we get to GC at
70 * all, this means that we have a numerically intensive application
72 #define SCM_DEFAULT_MIN_YIELD_2 40
74 #define SCM_DEFAULT_MAX_SEGMENT_SIZE (20*1024*1024L)
78 #define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_SIZEOF_CARD)
79 #define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_t_cell))
82 #define SCM_DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_t_cell) - 1) & SCM_UNPACK (x)) == 0)
85 #define SCM_GC_CARD_BVEC_SIZE_IN_LONGS \
86 ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LONG_BITS - 1) / SCM_C_BVEC_LONG_BITS)
87 #define SCM_GC_IN_CARD_HEADERP(x) \
88 (scm_t_cell *) (x) < SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS
91 int scm_getenv_int (const char *var
, int def
);
94 typedef enum { return_on_error
, abort_on_error
} policy_on_error
;
101 A struct holding GC statistics on a particular type of cells.
103 typedef struct scm_t_cell_type_statistics
{
106 heap segment where the last cell was allocated
108 int heap_segment_idx
;
110 /* minimum yield on this list in order not to grow the heap
114 /* defines min_yield as percent of total heap size
116 int min_yield_fraction
;
118 /* number of cells per object on this list */
121 /* number of collected cells during last GC */
122 unsigned long collected
;
124 /* number of collected cells during penultimate GC */
125 unsigned long collected_1
;
127 /* total number of cells in heap segments
128 * belonging to this list.
130 unsigned long heap_size
;
133 } scm_t_cell_type_statistics
;
136 /* Sweep statistics. */
137 typedef struct scm_sweep_statistics
139 /* Number of cells "swept", i.e., visited during the sweep operation. */
142 /* Number of cells collected during the sweep operation. This number must
143 alsways be lower than or equal to SWEPT. */
145 } scm_t_sweep_statistics
;
147 #define scm_i_sweep_statistics_init(_stats) \
150 (_stats)->swept = (_stats)->collected = 0; \
154 #define scm_i_sweep_statistics_sum(_sum, _addition) \
157 (_sum)->swept += (_addition).swept; \
158 (_sum)->collected += (_addition).collected; \
163 extern scm_t_cell_type_statistics scm_i_master_freelist
;
164 extern scm_t_cell_type_statistics scm_i_master_freelist2
;
167 void scm_i_adjust_min_yield (scm_t_cell_type_statistics
*freelist
,
168 scm_t_sweep_statistics sweep_stats
,
169 scm_t_sweep_statistics sweep_stats_1
);
171 void scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics
*freelist
);
173 int scm_i_gc_grow_heap_p (scm_t_cell_type_statistics
* freelist
);
176 #define SCM_HEAP_SIZE \
177 (scm_i_master_freelist.heap_size + scm_i_master_freelist2.heap_size)
180 #define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
181 #define SCM_MIN(A, B) ((A) < (B) ? (A) : (B))
183 /* CELL_P checks a random word whether it has the right form for a
184 pointer to a cell. Use scm_i_find_heap_segment_containing_object
185 to find out whether it actually points to a real cell.
187 The right form for a cell pointer is this: the low three bits must
188 be scm_tc3_cons, and when the scm_tc3_cons tag is stripped, the
189 resulting pointer must be correctly aligned.
190 scm_i_initialize_heap_segment_data guarantees that the test below
193 #define CELL_P(x) ((SCM_UNPACK(x) & (sizeof(scm_t_cell)-1)) == scm_tc3_cons)
200 void scm_mark_all (void);
213 Cells are stored in a heap-segment: it is a contiguous chunk of
214 memory, that associated with one freelist.
217 typedef struct scm_t_heap_segment
220 {lower, upper} bounds of the segment
222 The upper bound is also the start of the mark space.
224 scm_t_cell
*bounds
[2];
227 If we ever decide to give it back, we could do it with this ptr.
229 Note that giving back memory is not very useful; as long we don't
230 touch a chunk of memory, the virtual memory system will keep it
231 swapped out. We could simply forget about a block.
233 (not that we do that, but anyway.)
238 scm_t_cell
* next_free_card
;
240 /* address of the head-of-freelist pointer for this segment's cells.
241 All segments usually point to the same one, scm_i_freelist. */
242 scm_t_cell_type_statistics
*freelist
;
244 /* number of cells per object in this segment */
249 Is this the first time that the cells are accessed?
253 } scm_t_heap_segment
;
259 A table of segment records is kept that records the upper and
260 lower extents of the segment; this is used during the conservative
261 phase of gc to identify probably gc roots (because they point
262 into valid segments at reasonable offsets).
265 extern scm_t_heap_segment
** scm_i_heap_segment_table
;
266 extern size_t scm_i_heap_segment_table_size
;
269 SCM_INTERNAL
int scm_i_init_card_freelist (scm_t_cell
* card
, SCM
*free_list
,
270 scm_t_heap_segment
*);
271 SCM_INTERNAL
int scm_i_sweep_card (scm_t_cell
*card
, SCM
*free_list
,
272 scm_t_heap_segment
*);
273 SCM_INTERNAL
void scm_i_card_statistics (scm_t_cell
*p
, SCM hashtab
,
274 scm_t_heap_segment
*seg
);
275 SCM_INTERNAL
char const *scm_i_tag_name (scm_t_bits tag
); /* MOVEME */
277 SCM_INTERNAL
int scm_i_initialize_heap_segment_data (scm_t_heap_segment
*seg
,
279 SCM_INTERNAL
int scm_i_segment_card_count (scm_t_heap_segment
*seg
);
280 SCM_INTERNAL
int scm_i_segment_cell_count (scm_t_heap_segment
*seg
);
282 SCM_INTERNAL
void scm_i_clear_segment_mark_space (scm_t_heap_segment
*seg
);
283 SCM_INTERNAL scm_t_heap_segment
*
284 scm_i_make_empty_heap_segment (scm_t_cell_type_statistics
*);
285 SCM_INTERNAL SCM
scm_i_sweep_some_cards (scm_t_heap_segment
*seg
,
286 scm_t_sweep_statistics
*sweep_stats
);
287 SCM_INTERNAL
void scm_i_sweep_segment (scm_t_heap_segment
*seg
,
288 scm_t_sweep_statistics
*sweep_stats
);
290 SCM_INTERNAL
void scm_i_heap_segment_statistics (scm_t_heap_segment
*seg
,
294 SCM_INTERNAL
int scm_i_insert_segment (scm_t_heap_segment
*seg
);
295 SCM_INTERNAL
long int scm_i_find_heap_segment_containing_object (SCM obj
);
296 SCM_INTERNAL
int scm_i_get_new_heap_segment (scm_t_cell_type_statistics
*,
297 scm_t_sweep_statistics
,
299 SCM_INTERNAL
void scm_i_clear_mark_space (void);
300 SCM_INTERNAL
void scm_i_sweep_segments (void);
301 SCM_INTERNAL SCM
scm_i_sweep_some_segments (scm_t_cell_type_statistics
*fl
,
302 scm_t_sweep_statistics
*sweep_stats
);
303 SCM_INTERNAL
void scm_i_reset_segments (void);
304 SCM_INTERNAL
void scm_i_sweep_all_segments (char const *reason
,
305 scm_t_sweep_statistics
*sweep_stats
);
306 SCM_INTERNAL SCM
scm_i_all_segments_statistics (SCM hashtab
);
307 SCM_INTERNAL
void scm_i_make_initial_segment (int init_heap_size
,
308 scm_t_cell_type_statistics
*fl
);
310 extern long int scm_i_deprecated_memory_return
;
316 void scm_gc_init_malloc (void);
317 void scm_gc_init_freelist (void);
318 void scm_gc_init_segments (void);
319 void scm_gc_init_mark (void);