Add a statistic for tracking how many cells are marked conservatively.
[bpt/guile.git] / libguile / private-gc.h
CommitLineData
c7743d02 1/*
cb90e2cb
HWN
2 * private-gc.h - private declarations for garbage collection.
3 *
102dbb6f 4 * Copyright (C) 2002, 03, 04, 05, 06, 07, 08 Free Software Foundation, Inc.
cb90e2cb
HWN
5 *
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.
10 *
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.
15 *
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
19 */
c7743d02
HWN
20
21#ifndef PRIVATE_GC
22#define PRIVATE_GC
23
24#include "_scm.h"
25
26/* {heap tuning parameters}
27 *
28 * These are parameters for controlling memory allocation. The heap
29 * is the area out of which scm_cons, and object headers are allocated.
30 *
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.
34 *
c7743d02
HWN
35 * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap
36 * is needed.
37 */
38
39
40/*
41 * Heap size 45000 and 40% min yield gives quick startup and no extra
42 * heap allocation. Having higher values on min yield may lead to
43 * large heaps, especially if code behaviour is varying its
44 * maximum consumption between different freelists.
45 */
46
47/*
48 These values used to be global C variables. However, they're also
49 available through the environment, and having a double interface is
50 confusing. Now they're #defines --hwn.
51 */
52
53#define SCM_DEFAULT_INIT_HEAP_SIZE_1 256*1024
54#define SCM_DEFAULT_MIN_YIELD_1 40
55#define SCM_DEFAULT_INIT_HEAP_SIZE_2 32*1024
56
82ae1b8e
HWN
57/*
58 How many cells to collect during one sweep call. This is the pool
59 size of each thread.
60 */
61#define DEFAULT_SWEEP_AMOUNT 512
62
c7743d02
HWN
63/* The following value may seem large, but note that if we get to GC at
64 * all, this means that we have a numerically intensive application
65 */
66#define SCM_DEFAULT_MIN_YIELD_2 40
322a2bf7
MV
67
68#define SCM_DEFAULT_MAX_SEGMENT_SIZE (20*1024*1024L)
c7743d02 69
c7743d02
HWN
70#define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_SIZEOF_CARD)
71#define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_t_cell))
72
1383773b 73#define SCM_DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_t_cell) - 1) & SCM_UNPACK (x)) == 0)
c7743d02
HWN
74
75
1383773b
HWN
76#define SCM_GC_CARD_BVEC_SIZE_IN_LONGS \
77 ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LONG_BITS - 1) / SCM_C_BVEC_LONG_BITS)
78#define SCM_GC_IN_CARD_HEADERP(x) \
79 (scm_t_cell *) (x) < SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS
c7743d02 80
82ae1b8e 81int scm_i_uint_bit_count (unsigned int u);
c7743d02
HWN
82int scm_getenv_int (const char *var, int def);
83
84
85typedef enum { return_on_error, abort_on_error } policy_on_error;
86
82ae1b8e 87/* gc-freelist */
c7743d02
HWN
88
89/*
90 FREELIST:
91
92 A struct holding GC statistics on a particular type of cells.
82ae1b8e
HWN
93
94 Counts in cells are mainly for heap statistics, and for
95 double-cells, they are still measured in single-cell units.
c7743d02
HWN
96*/
97typedef struct scm_t_cell_type_statistics {
c7743d02
HWN
98 /*
99 heap segment where the last cell was allocated
100 */
101 int heap_segment_idx;
102
82ae1b8e 103 /* defines min_yield as fraction of total heap size
c7743d02 104 */
82ae1b8e 105 float min_yield_fraction;
c7743d02
HWN
106
107 /* number of cells per object on this list */
108 int span;
109
82ae1b8e 110 /* number of collected cells during last GC. */
c7743d02
HWN
111 unsigned long collected;
112
82ae1b8e 113 unsigned long swept;
c2cbcc57 114
82ae1b8e
HWN
115 /*
116 Total number of cells in heap segments belonging to this list.
117 */
118 unsigned long heap_total_cells;
c7743d02
HWN
119} scm_t_cell_type_statistics;
120
121
4c7016dc
HWN
122/* Sweep statistics. */
123typedef struct scm_sweep_statistics
124{
125 /* Number of cells "swept", i.e., visited during the sweep operation. */
126 unsigned swept;
127
128 /* Number of cells collected during the sweep operation. This number must
82ae1b8e 129 always be lower than or equal to SWEPT. */
4c7016dc
HWN
130 unsigned collected;
131} scm_t_sweep_statistics;
132
82ae1b8e 133SCM_INTERNAL scm_t_sweep_statistics scm_i_gc_sweep_stats;
4c7016dc 134
4c7016dc 135\f
c7743d02
HWN
136extern scm_t_cell_type_statistics scm_i_master_freelist;
137extern scm_t_cell_type_statistics scm_i_master_freelist2;
c7743d02 138
102dbb6f 139SCM_INTERNAL
4c7016dc 140void scm_i_adjust_min_yield (scm_t_cell_type_statistics *freelist,
d9f71a07
LC
141 scm_t_sweep_statistics sweep_stats,
142 scm_t_sweep_statistics sweep_stats_1);
102dbb6f 143SCM_INTERNAL
c7743d02 144void scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics *freelist);
82ae1b8e
HWN
145SCM_INTERNAL float
146scm_i_gc_heap_size_delta (scm_t_cell_type_statistics * freelist);
c7743d02
HWN
147
148
149#define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
150#define SCM_MIN(A, B) ((A) < (B) ? (A) : (B))
151
d3774e2c
MV
152/* CELL_P checks a random word whether it has the right form for a
153 pointer to a cell. Use scm_i_find_heap_segment_containing_object
154 to find out whether it actually points to a real cell.
155
156 The right form for a cell pointer is this: the low three bits must
157 be scm_tc3_cons, and when the scm_tc3_cons tag is stripped, the
158 resulting pointer must be correctly aligned.
159 scm_i_initialize_heap_segment_data guarantees that the test below
160 works.
161*/
162#define CELL_P(x) ((SCM_UNPACK(x) & (sizeof(scm_t_cell)-1)) == scm_tc3_cons)
c7743d02
HWN
163
164/*
165 gc-mark
166 */
d09752ff
HWN
167
168/* this can be used to ensure that set/clear gc marks only happen when
169 allowed. */
170int scm_i_marking;
171
c7743d02
HWN
172void scm_mark_all (void);
173
c7743d02
HWN
174/*
175gc-segment:
176*/
177
c7743d02
HWN
178/*
179
180 Cells are stored in a heap-segment: it is a contiguous chunk of
181 memory, that associated with one freelist.
182*/
c7743d02
HWN
183typedef struct scm_t_heap_segment
184{
185 /*
186 {lower, upper} bounds of the segment
187
188 The upper bound is also the start of the mark space.
189 */
190 scm_t_cell *bounds[2];
191
192 /*
193 If we ever decide to give it back, we could do it with this ptr.
194
195 Note that giving back memory is not very useful; as long we don't
196 touch a chunk of memory, the virtual memory system will keep it
197 swapped out. We could simply forget about a block.
198
199 (not that we do that, but anyway.)
200 */
82ae1b8e 201 void *malloced;
c7743d02 202
82ae1b8e 203 scm_t_cell *next_free_card;
c7743d02
HWN
204
205 /* address of the head-of-freelist pointer for this segment's cells.
206 All segments usually point to the same one, scm_i_freelist. */
207 scm_t_cell_type_statistics *freelist;
208
209 /* number of cells per object in this segment */
210 int span;
211
c7743d02
HWN
212 /*
213 Is this the first time that the cells are accessed?
214 */
215 int first_time;
c7743d02
HWN
216} scm_t_heap_segment;
217
c7743d02 218/*
c7743d02
HWN
219 A table of segment records is kept that records the upper and
220 lower extents of the segment; this is used during the conservative
221 phase of gc to identify probably gc roots (because they point
222 into valid segments at reasonable offsets).
c7743d02
HWN
223*/
224extern scm_t_heap_segment ** scm_i_heap_segment_table;
225extern size_t scm_i_heap_segment_table_size;
226
227
102dbb6f
LC
228SCM_INTERNAL int scm_i_init_card_freelist (scm_t_cell * card, SCM *free_list,
229 scm_t_heap_segment*);
230SCM_INTERNAL int scm_i_sweep_card (scm_t_cell *card, SCM *free_list,
231 scm_t_heap_segment *);
82ae1b8e 232SCM_INTERNAL int scm_i_card_marked_count (scm_t_cell *card, int span);
102dbb6f
LC
233SCM_INTERNAL void scm_i_card_statistics (scm_t_cell *p, SCM hashtab,
234 scm_t_heap_segment *seg);
235SCM_INTERNAL char const *scm_i_tag_name (scm_t_bits tag); /* MOVEME */
236
237SCM_INTERNAL int scm_i_initialize_heap_segment_data (scm_t_heap_segment *seg,
238 size_t requested);
82ae1b8e
HWN
239
240SCM_INTERNAL int scm_i_segment_cells_per_card (scm_t_heap_segment *seg);
241SCM_INTERNAL int scm_i_segment_card_number (scm_t_heap_segment *seg,
242 scm_t_cell *card);
102dbb6f
LC
243SCM_INTERNAL int scm_i_segment_card_count (scm_t_heap_segment *seg);
244SCM_INTERNAL int scm_i_segment_cell_count (scm_t_heap_segment *seg);
82ae1b8e
HWN
245SCM_INTERNAL int scm_i_heap_segment_marked_count (scm_t_heap_segment *seg);
246
102dbb6f
LC
247SCM_INTERNAL void scm_i_clear_segment_mark_space (scm_t_heap_segment *seg);
248SCM_INTERNAL scm_t_heap_segment *
249scm_i_make_empty_heap_segment (scm_t_cell_type_statistics*);
82ae1b8e 250SCM_INTERNAL SCM scm_i_sweep_for_freelist (scm_t_cell_type_statistics *seg);
102dbb6f 251SCM_INTERNAL SCM scm_i_sweep_some_cards (scm_t_heap_segment *seg,
82ae1b8e
HWN
252 scm_t_sweep_statistics *sweep_stats,
253 int threshold);
102dbb6f
LC
254SCM_INTERNAL void scm_i_sweep_segment (scm_t_heap_segment *seg,
255 scm_t_sweep_statistics *sweep_stats);
256
257SCM_INTERNAL void scm_i_heap_segment_statistics (scm_t_heap_segment *seg,
258 SCM tab);
259
260
261SCM_INTERNAL int scm_i_insert_segment (scm_t_heap_segment *seg);
82ae1b8e
HWN
262SCM_INTERNAL int scm_i_find_heap_segment_containing_object (SCM obj);
263SCM_INTERNAL int scm_i_get_new_heap_segment (scm_t_cell_type_statistics *freelist,
264 size_t length,
102dbb6f 265 policy_on_error);
82ae1b8e 266SCM_INTERNAL int scm_i_marked_count (void);
102dbb6f
LC
267SCM_INTERNAL void scm_i_clear_mark_space (void);
268SCM_INTERNAL void scm_i_sweep_segments (void);
269SCM_INTERNAL SCM scm_i_sweep_some_segments (scm_t_cell_type_statistics *fl,
270 scm_t_sweep_statistics *sweep_stats);
271SCM_INTERNAL void scm_i_reset_segments (void);
272SCM_INTERNAL void scm_i_sweep_all_segments (char const *reason,
273 scm_t_sweep_statistics *sweep_stats);
274SCM_INTERNAL SCM scm_i_all_segments_statistics (SCM hashtab);
82ae1b8e 275SCM_INTERNAL unsigned long *scm_i_segment_table_info(int *size);
c7743d02
HWN
276
277extern long int scm_i_deprecated_memory_return;
40945e5e 278extern long int scm_i_find_heap_calls;
c7743d02 279
c7743d02
HWN
280/*
281 global init funcs.
282 */
283void scm_gc_init_malloc (void);
284void scm_gc_init_freelist (void);
285void scm_gc_init_segments (void);
286void scm_gc_init_mark (void);
eab1b259
HWN
287
288
c7743d02 289#endif