Use Gnulib's `count-one-bits' as a replacement for `scm_i_uint_bit_count ()'.
[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
c7743d02
HWN
81int scm_getenv_int (const char *var, int def);
82
83
84typedef enum { return_on_error, abort_on_error } policy_on_error;
85
82ae1b8e 86/* gc-freelist */
c7743d02
HWN
87
88/*
89 FREELIST:
90
91 A struct holding GC statistics on a particular type of cells.
82ae1b8e
HWN
92
93 Counts in cells are mainly for heap statistics, and for
94 double-cells, they are still measured in single-cell units.
c7743d02
HWN
95*/
96typedef struct scm_t_cell_type_statistics {
c7743d02
HWN
97 /*
98 heap segment where the last cell was allocated
99 */
100 int heap_segment_idx;
101
82ae1b8e 102 /* defines min_yield as fraction of total heap size
c7743d02 103 */
82ae1b8e 104 float min_yield_fraction;
c7743d02
HWN
105
106 /* number of cells per object on this list */
107 int span;
108
82ae1b8e 109 /* number of collected cells during last GC. */
c7743d02
HWN
110 unsigned long collected;
111
82ae1b8e 112 unsigned long swept;
c2cbcc57 113
82ae1b8e
HWN
114 /*
115 Total number of cells in heap segments belonging to this list.
116 */
117 unsigned long heap_total_cells;
c7743d02
HWN
118} scm_t_cell_type_statistics;
119
120
4c7016dc
HWN
121/* Sweep statistics. */
122typedef struct scm_sweep_statistics
123{
124 /* Number of cells "swept", i.e., visited during the sweep operation. */
125 unsigned swept;
126
127 /* Number of cells collected during the sweep operation. This number must
82ae1b8e 128 always be lower than or equal to SWEPT. */
4c7016dc
HWN
129 unsigned collected;
130} scm_t_sweep_statistics;
131
82ae1b8e 132SCM_INTERNAL scm_t_sweep_statistics scm_i_gc_sweep_stats;
4c7016dc 133
4c7016dc 134\f
c7743d02
HWN
135extern scm_t_cell_type_statistics scm_i_master_freelist;
136extern scm_t_cell_type_statistics scm_i_master_freelist2;
c7743d02 137
102dbb6f 138SCM_INTERNAL
4c7016dc 139void scm_i_adjust_min_yield (scm_t_cell_type_statistics *freelist,
d9f71a07
LC
140 scm_t_sweep_statistics sweep_stats,
141 scm_t_sweep_statistics sweep_stats_1);
102dbb6f 142SCM_INTERNAL
c7743d02 143void scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics *freelist);
82ae1b8e
HWN
144SCM_INTERNAL float
145scm_i_gc_heap_size_delta (scm_t_cell_type_statistics * freelist);
c7743d02
HWN
146
147
148#define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
149#define SCM_MIN(A, B) ((A) < (B) ? (A) : (B))
150
d3774e2c
MV
151/* CELL_P checks a random word whether it has the right form for a
152 pointer to a cell. Use scm_i_find_heap_segment_containing_object
153 to find out whether it actually points to a real cell.
154
155 The right form for a cell pointer is this: the low three bits must
156 be scm_tc3_cons, and when the scm_tc3_cons tag is stripped, the
157 resulting pointer must be correctly aligned.
158 scm_i_initialize_heap_segment_data guarantees that the test below
159 works.
160*/
161#define CELL_P(x) ((SCM_UNPACK(x) & (sizeof(scm_t_cell)-1)) == scm_tc3_cons)
c7743d02
HWN
162
163/*
164 gc-mark
165 */
d09752ff
HWN
166
167/* this can be used to ensure that set/clear gc marks only happen when
168 allowed. */
169int scm_i_marking;
170
c7743d02
HWN
171void scm_mark_all (void);
172
c7743d02
HWN
173/*
174gc-segment:
175*/
176
c7743d02
HWN
177/*
178
179 Cells are stored in a heap-segment: it is a contiguous chunk of
180 memory, that associated with one freelist.
181*/
c7743d02
HWN
182typedef struct scm_t_heap_segment
183{
184 /*
185 {lower, upper} bounds of the segment
186
187 The upper bound is also the start of the mark space.
188 */
189 scm_t_cell *bounds[2];
190
191 /*
192 If we ever decide to give it back, we could do it with this ptr.
193
194 Note that giving back memory is not very useful; as long we don't
195 touch a chunk of memory, the virtual memory system will keep it
196 swapped out. We could simply forget about a block.
197
198 (not that we do that, but anyway.)
199 */
82ae1b8e 200 void *malloced;
c7743d02 201
82ae1b8e 202 scm_t_cell *next_free_card;
c7743d02
HWN
203
204 /* address of the head-of-freelist pointer for this segment's cells.
205 All segments usually point to the same one, scm_i_freelist. */
206 scm_t_cell_type_statistics *freelist;
207
208 /* number of cells per object in this segment */
209 int span;
210
c7743d02
HWN
211 /*
212 Is this the first time that the cells are accessed?
213 */
214 int first_time;
c7743d02
HWN
215} scm_t_heap_segment;
216
c7743d02 217/*
c7743d02
HWN
218 A table of segment records is kept that records the upper and
219 lower extents of the segment; this is used during the conservative
220 phase of gc to identify probably gc roots (because they point
221 into valid segments at reasonable offsets).
c7743d02
HWN
222*/
223extern scm_t_heap_segment ** scm_i_heap_segment_table;
224extern size_t scm_i_heap_segment_table_size;
225
226
102dbb6f
LC
227SCM_INTERNAL int scm_i_init_card_freelist (scm_t_cell * card, SCM *free_list,
228 scm_t_heap_segment*);
229SCM_INTERNAL int scm_i_sweep_card (scm_t_cell *card, SCM *free_list,
230 scm_t_heap_segment *);
82ae1b8e 231SCM_INTERNAL int scm_i_card_marked_count (scm_t_cell *card, int span);
102dbb6f
LC
232SCM_INTERNAL void scm_i_card_statistics (scm_t_cell *p, SCM hashtab,
233 scm_t_heap_segment *seg);
234SCM_INTERNAL char const *scm_i_tag_name (scm_t_bits tag); /* MOVEME */
235
236SCM_INTERNAL int scm_i_initialize_heap_segment_data (scm_t_heap_segment *seg,
237 size_t requested);
82ae1b8e
HWN
238
239SCM_INTERNAL int scm_i_segment_cells_per_card (scm_t_heap_segment *seg);
240SCM_INTERNAL int scm_i_segment_card_number (scm_t_heap_segment *seg,
241 scm_t_cell *card);
102dbb6f
LC
242SCM_INTERNAL int scm_i_segment_card_count (scm_t_heap_segment *seg);
243SCM_INTERNAL int scm_i_segment_cell_count (scm_t_heap_segment *seg);
82ae1b8e
HWN
244SCM_INTERNAL int scm_i_heap_segment_marked_count (scm_t_heap_segment *seg);
245
102dbb6f
LC
246SCM_INTERNAL void scm_i_clear_segment_mark_space (scm_t_heap_segment *seg);
247SCM_INTERNAL scm_t_heap_segment *
248scm_i_make_empty_heap_segment (scm_t_cell_type_statistics*);
82ae1b8e 249SCM_INTERNAL SCM scm_i_sweep_for_freelist (scm_t_cell_type_statistics *seg);
102dbb6f 250SCM_INTERNAL SCM scm_i_sweep_some_cards (scm_t_heap_segment *seg,
82ae1b8e
HWN
251 scm_t_sweep_statistics *sweep_stats,
252 int threshold);
102dbb6f
LC
253SCM_INTERNAL void scm_i_sweep_segment (scm_t_heap_segment *seg,
254 scm_t_sweep_statistics *sweep_stats);
255
256SCM_INTERNAL void scm_i_heap_segment_statistics (scm_t_heap_segment *seg,
257 SCM tab);
258
259
260SCM_INTERNAL int scm_i_insert_segment (scm_t_heap_segment *seg);
82ae1b8e
HWN
261SCM_INTERNAL int scm_i_find_heap_segment_containing_object (SCM obj);
262SCM_INTERNAL int scm_i_get_new_heap_segment (scm_t_cell_type_statistics *freelist,
263 size_t length,
102dbb6f 264 policy_on_error);
82ae1b8e 265SCM_INTERNAL int scm_i_marked_count (void);
102dbb6f
LC
266SCM_INTERNAL void scm_i_clear_mark_space (void);
267SCM_INTERNAL void scm_i_sweep_segments (void);
268SCM_INTERNAL SCM scm_i_sweep_some_segments (scm_t_cell_type_statistics *fl,
269 scm_t_sweep_statistics *sweep_stats);
270SCM_INTERNAL void scm_i_reset_segments (void);
271SCM_INTERNAL void scm_i_sweep_all_segments (char const *reason,
272 scm_t_sweep_statistics *sweep_stats);
273SCM_INTERNAL SCM scm_i_all_segments_statistics (SCM hashtab);
82ae1b8e 274SCM_INTERNAL unsigned long *scm_i_segment_table_info(int *size);
c7743d02
HWN
275
276extern long int scm_i_deprecated_memory_return;
40945e5e 277extern long int scm_i_find_heap_calls;
c7743d02 278
c7743d02
HWN
279/*
280 global init funcs.
281 */
282void scm_gc_init_malloc (void);
283void scm_gc_init_freelist (void);
284void scm_gc_init_segments (void);
285void scm_gc_init_mark (void);
eab1b259
HWN
286
287
c7743d02 288#endif