Whitespace and formatting fixes.
[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 *
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.
38 *
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
43 *
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.
46 *
47 * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap
48 * is needed.
49 */
50
51
52/*
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.
57 */
58
59/*
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.
63 */
64
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
68
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
71 */
72#define SCM_DEFAULT_MIN_YIELD_2 40
322a2bf7
MV
73
74#define SCM_DEFAULT_MAX_SEGMENT_SIZE (20*1024*1024L)
c7743d02 75
c7743d02
HWN
76#define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_SIZEOF_CARD)
77#define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_t_cell))
78
1383773b 79#define SCM_DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_t_cell) - 1) & SCM_UNPACK (x)) == 0)
c7743d02
HWN
80
81
1383773b
HWN
82#define SCM_GC_CARD_BVEC_SIZE_IN_LONGS \
83 ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LONG_BITS - 1) / SCM_C_BVEC_LONG_BITS)
84#define SCM_GC_IN_CARD_HEADERP(x) \
85 (scm_t_cell *) (x) < SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS
c7743d02
HWN
86
87
88int scm_getenv_int (const char *var, int def);
89
90
91typedef enum { return_on_error, abort_on_error } policy_on_error;
92
93/* gc-freelist*/
94
95/*
96 FREELIST:
97
98 A struct holding GC statistics on a particular type of cells.
99*/
100typedef struct scm_t_cell_type_statistics {
c7743d02
HWN
101 /*
102 heap segment where the last cell was allocated
103 */
104 int heap_segment_idx;
105
106 /* minimum yield on this list in order not to grow the heap
107 */
108 long min_yield;
109
110 /* defines min_yield as percent of total heap size
111 */
112 int min_yield_fraction;
113
114 /* number of cells per object on this list */
115 int span;
116
117 /* number of collected cells during last GC */
118 unsigned long collected;
119
120 /* number of collected cells during penultimate GC */
121 unsigned long collected_1;
122
123 /* total number of cells in heap segments
124 * belonging to this list.
125 */
126 unsigned long heap_size;
127
c2cbcc57 128
c7743d02
HWN
129} scm_t_cell_type_statistics;
130
131
4c7016dc
HWN
132/* Sweep statistics. */
133typedef struct scm_sweep_statistics
134{
135 /* Number of cells "swept", i.e., visited during the sweep operation. */
136 unsigned swept;
137
138 /* Number of cells collected during the sweep operation. This number must
139 alsways be lower than or equal to SWEPT. */
140 unsigned collected;
141} scm_t_sweep_statistics;
142
143#define scm_i_sweep_statistics_init(_stats) \
144 do \
145 { \
146 (_stats)->swept = (_stats)->collected = 0; \
147 } \
148 while (0)
149
150#define scm_i_sweep_statistics_sum(_sum, _addition) \
151 do \
152 { \
153 (_sum)->swept += (_addition).swept; \
154 (_sum)->collected += (_addition).collected; \
155 } \
156 while (0)
157
4c7016dc 158\f
c7743d02
HWN
159extern scm_t_cell_type_statistics scm_i_master_freelist;
160extern scm_t_cell_type_statistics scm_i_master_freelist2;
c7743d02 161
102dbb6f 162SCM_INTERNAL
4c7016dc 163void scm_i_adjust_min_yield (scm_t_cell_type_statistics *freelist,
d9f71a07
LC
164 scm_t_sweep_statistics sweep_stats,
165 scm_t_sweep_statistics sweep_stats_1);
102dbb6f 166SCM_INTERNAL
c7743d02 167void scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics *freelist);
102dbb6f 168SCM_INTERNAL
c7743d02 169int scm_i_gc_grow_heap_p (scm_t_cell_type_statistics * freelist);
dac04e9f
HWN
170
171
c7743d02
HWN
172#define SCM_HEAP_SIZE \
173 (scm_i_master_freelist.heap_size + scm_i_master_freelist2.heap_size)
174
175
176#define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
177#define SCM_MIN(A, B) ((A) < (B) ? (A) : (B))
178
d3774e2c
MV
179/* CELL_P checks a random word whether it has the right form for a
180 pointer to a cell. Use scm_i_find_heap_segment_containing_object
181 to find out whether it actually points to a real cell.
182
183 The right form for a cell pointer is this: the low three bits must
184 be scm_tc3_cons, and when the scm_tc3_cons tag is stripped, the
185 resulting pointer must be correctly aligned.
186 scm_i_initialize_heap_segment_data guarantees that the test below
187 works.
188*/
189#define CELL_P(x) ((SCM_UNPACK(x) & (sizeof(scm_t_cell)-1)) == scm_tc3_cons)
c7743d02
HWN
190
191/*
192 gc-mark
193 */
c7743d02
HWN
194void scm_mark_all (void);
195
c7743d02
HWN
196/*
197gc-segment:
198*/
199
c7743d02
HWN
200/*
201
202 Cells are stored in a heap-segment: it is a contiguous chunk of
203 memory, that associated with one freelist.
204*/
c7743d02
HWN
205typedef struct scm_t_heap_segment
206{
207 /*
208 {lower, upper} bounds of the segment
209
210 The upper bound is also the start of the mark space.
211 */
212 scm_t_cell *bounds[2];
213
214 /*
215 If we ever decide to give it back, we could do it with this ptr.
216
217 Note that giving back memory is not very useful; as long we don't
218 touch a chunk of memory, the virtual memory system will keep it
219 swapped out. We could simply forget about a block.
220
221 (not that we do that, but anyway.)
222 */
223
224 void* malloced;
225
226 scm_t_cell * next_free_card;
227
228 /* address of the head-of-freelist pointer for this segment's cells.
229 All segments usually point to the same one, scm_i_freelist. */
230 scm_t_cell_type_statistics *freelist;
231
232 /* number of cells per object in this segment */
233 int span;
234
235
236 /*
237 Is this the first time that the cells are accessed?
238 */
239 int first_time;
240
241} scm_t_heap_segment;
242
243
244
245/*
c7743d02
HWN
246 A table of segment records is kept that records the upper and
247 lower extents of the segment; this is used during the conservative
248 phase of gc to identify probably gc roots (because they point
249 into valid segments at reasonable offsets).
c7743d02
HWN
250*/
251extern scm_t_heap_segment ** scm_i_heap_segment_table;
252extern size_t scm_i_heap_segment_table_size;
253
254
102dbb6f
LC
255SCM_INTERNAL int scm_i_init_card_freelist (scm_t_cell * card, SCM *free_list,
256 scm_t_heap_segment*);
257SCM_INTERNAL int scm_i_sweep_card (scm_t_cell *card, SCM *free_list,
258 scm_t_heap_segment *);
259SCM_INTERNAL void scm_i_card_statistics (scm_t_cell *p, SCM hashtab,
260 scm_t_heap_segment *seg);
261SCM_INTERNAL char const *scm_i_tag_name (scm_t_bits tag); /* MOVEME */
262
263SCM_INTERNAL int scm_i_initialize_heap_segment_data (scm_t_heap_segment *seg,
264 size_t requested);
265SCM_INTERNAL int scm_i_segment_card_count (scm_t_heap_segment *seg);
266SCM_INTERNAL int scm_i_segment_cell_count (scm_t_heap_segment *seg);
267
268SCM_INTERNAL void scm_i_clear_segment_mark_space (scm_t_heap_segment *seg);
269SCM_INTERNAL scm_t_heap_segment *
270scm_i_make_empty_heap_segment (scm_t_cell_type_statistics*);
271SCM_INTERNAL SCM scm_i_sweep_some_cards (scm_t_heap_segment *seg,
272 scm_t_sweep_statistics *sweep_stats);
273SCM_INTERNAL void scm_i_sweep_segment (scm_t_heap_segment *seg,
274 scm_t_sweep_statistics *sweep_stats);
275
276SCM_INTERNAL void scm_i_heap_segment_statistics (scm_t_heap_segment *seg,
277 SCM tab);
278
279
280SCM_INTERNAL int scm_i_insert_segment (scm_t_heap_segment *seg);
281SCM_INTERNAL long int scm_i_find_heap_segment_containing_object (SCM obj);
282SCM_INTERNAL int scm_i_get_new_heap_segment (scm_t_cell_type_statistics *,
283 scm_t_sweep_statistics,
284 policy_on_error);
285SCM_INTERNAL void scm_i_clear_mark_space (void);
286SCM_INTERNAL void scm_i_sweep_segments (void);
287SCM_INTERNAL SCM scm_i_sweep_some_segments (scm_t_cell_type_statistics *fl,
288 scm_t_sweep_statistics *sweep_stats);
289SCM_INTERNAL void scm_i_reset_segments (void);
290SCM_INTERNAL void scm_i_sweep_all_segments (char const *reason,
291 scm_t_sweep_statistics *sweep_stats);
292SCM_INTERNAL SCM scm_i_all_segments_statistics (SCM hashtab);
293SCM_INTERNAL void scm_i_make_initial_segment (int init_heap_size,
294 scm_t_cell_type_statistics *fl);
c7743d02
HWN
295
296extern long int scm_i_deprecated_memory_return;
297
c7743d02
HWN
298/*
299 global init funcs.
300 */
301void scm_gc_init_malloc (void);
302void scm_gc_init_freelist (void);
303void scm_gc_init_segments (void);
304void scm_gc_init_mark (void);
eab1b259
HWN
305
306
c7743d02 307#endif