* pthread-threads.h: New, implement pthread-like API by deferring to
[bpt/guile.git] / libguile / private-gc.h
CommitLineData
c7743d02
HWN
1/*
2 (c) FSF 2002.
3*/
4
5
6#ifndef PRIVATE_GC
7#define PRIVATE_GC
8
9#include "_scm.h"
10
11/* {heap tuning parameters}
12 *
13 * These are parameters for controlling memory allocation. The heap
14 * is the area out of which scm_cons, and object headers are allocated.
15 *
16 * Each heap cell is 8 bytes on a 32 bit machine and 16 bytes on a
17 * 64 bit machine. The units of the _SIZE parameters are bytes.
18 * Cons pairs and object headers occupy one heap cell.
19 *
20 * SCM_INIT_HEAP_SIZE is the initial size of heap. If this much heap is
21 * allocated initially the heap will grow by half its current size
22 * each subsequent time more heap is needed.
23 *
24 * If SCM_INIT_HEAP_SIZE heap cannot be allocated initially, SCM_HEAP_SEG_SIZE
25 * will be used, and the heap will grow by SCM_HEAP_SEG_SIZE when more
26 * heap is needed. SCM_HEAP_SEG_SIZE must fit into type size_t. This code
27 * is in scm_init_storage() and alloc_some_heap() in sys.c
28 *
29 * If SCM_INIT_HEAP_SIZE can be allocated initially, the heap will grow by
30 * SCM_EXPHEAP(scm_heap_size) when more heap is needed.
31 *
32 * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap
33 * is needed.
34 */
35
36
37/*
38 * Heap size 45000 and 40% min yield gives quick startup and no extra
39 * heap allocation. Having higher values on min yield may lead to
40 * large heaps, especially if code behaviour is varying its
41 * maximum consumption between different freelists.
42 */
43
44/*
45 These values used to be global C variables. However, they're also
46 available through the environment, and having a double interface is
47 confusing. Now they're #defines --hwn.
48 */
49
50#define SCM_DEFAULT_INIT_HEAP_SIZE_1 256*1024
51#define SCM_DEFAULT_MIN_YIELD_1 40
52#define SCM_DEFAULT_INIT_HEAP_SIZE_2 32*1024
53
54/* The following value may seem large, but note that if we get to GC at
55 * all, this means that we have a numerically intensive application
56 */
57#define SCM_DEFAULT_MIN_YIELD_2 40
58#define SCM_DEFAULT_MAX_SEGMENT_SIZE 2097000L /* a little less (adm) than 2 Mb */
59
60
61
62#define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_SIZEOF_CARD)
63#define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_t_cell))
64
65
1383773b 66#define SCM_DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_t_cell) - 1) & SCM_UNPACK (x)) == 0)
c7743d02
HWN
67
68
1383773b
HWN
69#define SCM_GC_CARD_BVEC_SIZE_IN_LONGS \
70 ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LONG_BITS - 1) / SCM_C_BVEC_LONG_BITS)
71#define SCM_GC_IN_CARD_HEADERP(x) \
72 (scm_t_cell *) (x) < SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS
c7743d02
HWN
73
74
75int scm_getenv_int (const char *var, int def);
76
77
78typedef enum { return_on_error, abort_on_error } policy_on_error;
79
80/* gc-freelist*/
81
82/*
83 FREELIST:
84
85 A struct holding GC statistics on a particular type of cells.
86*/
87typedef struct scm_t_cell_type_statistics {
88
89 /*
90 heap segment where the last cell was allocated
91 */
92 int heap_segment_idx;
93
94 /* minimum yield on this list in order not to grow the heap
95 */
96 long min_yield;
97
98 /* defines min_yield as percent of total heap size
99 */
100 int min_yield_fraction;
101
102 /* number of cells per object on this list */
103 int span;
104
105 /* number of collected cells during last GC */
106 unsigned long collected;
107
108 /* number of collected cells during penultimate GC */
109 unsigned long collected_1;
110
111 /* total number of cells in heap segments
112 * belonging to this list.
113 */
114 unsigned long heap_size;
115
c2cbcc57 116
c7743d02
HWN
117} scm_t_cell_type_statistics;
118
119
120extern scm_t_cell_type_statistics scm_i_master_freelist;
121extern scm_t_cell_type_statistics scm_i_master_freelist2;
122extern unsigned long scm_gc_cells_collected_1;
123
124void scm_i_adjust_min_yield (scm_t_cell_type_statistics *freelist);
125void scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics *freelist);
126int scm_i_gc_grow_heap_p (scm_t_cell_type_statistics * freelist);
dac04e9f
HWN
127
128
c7743d02
HWN
129#define SCM_HEAP_SIZE \
130 (scm_i_master_freelist.heap_size + scm_i_master_freelist2.heap_size)
131
132
133#define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
134#define SCM_MIN(A, B) ((A) < (B) ? (A) : (B))
135
136#define CELL_P(x) (SCM_ITAG3 (x) == scm_tc3_cons)
137
138/*
139 gc-mark
140 */
141
142
143void scm_mark_all (void);
144
145
146
147/*
148gc-segment:
149*/
150
151
152
153
154/*
155
156 Cells are stored in a heap-segment: it is a contiguous chunk of
157 memory, that associated with one freelist.
158*/
159
160typedef struct scm_t_heap_segment
161{
162 /*
163 {lower, upper} bounds of the segment
164
165 The upper bound is also the start of the mark space.
166 */
167 scm_t_cell *bounds[2];
168
169 /*
170 If we ever decide to give it back, we could do it with this ptr.
171
172 Note that giving back memory is not very useful; as long we don't
173 touch a chunk of memory, the virtual memory system will keep it
174 swapped out. We could simply forget about a block.
175
176 (not that we do that, but anyway.)
177 */
178
179 void* malloced;
180
181 scm_t_cell * next_free_card;
182
183 /* address of the head-of-freelist pointer for this segment's cells.
184 All segments usually point to the same one, scm_i_freelist. */
185 scm_t_cell_type_statistics *freelist;
186
187 /* number of cells per object in this segment */
188 int span;
189
190
191 /*
192 Is this the first time that the cells are accessed?
193 */
194 int first_time;
195
196} scm_t_heap_segment;
197
198
199
200/*
201
202 A table of segment records is kept that records the upper and
203 lower extents of the segment; this is used during the conservative
204 phase of gc to identify probably gc roots (because they point
205 into valid segments at reasonable offsets).
206
207*/
208extern scm_t_heap_segment ** scm_i_heap_segment_table;
209extern size_t scm_i_heap_segment_table_size;
210
211
1383773b
HWN
212int scm_i_init_card_freelist (scm_t_cell * card, SCM *free_list,scm_t_heap_segment*);
213int scm_i_sweep_card (scm_t_cell * card, SCM *free_list, scm_t_heap_segment*);
c7743d02
HWN
214int scm_i_initialize_heap_segment_data (scm_t_heap_segment * segment, size_t requested);
215int scm_i_segment_card_count (scm_t_heap_segment * seg);
216int scm_i_segment_cell_count (scm_t_heap_segment * seg);
217
218void scm_i_clear_segment_mark_space (scm_t_heap_segment *seg);
219scm_t_heap_segment * scm_i_make_empty_heap_segment (scm_t_cell_type_statistics*);
220SCM scm_i_sweep_some_cards (scm_t_heap_segment *seg);
221void scm_i_sweep_segment (scm_t_heap_segment * seg);
222
223
224int scm_i_insert_segment (scm_t_heap_segment * seg);
225long int scm_i_find_heap_segment_containing_object (SCM obj);
226int scm_i_get_new_heap_segment (scm_t_cell_type_statistics *, policy_on_error);
227void scm_i_clear_mark_space (void);
228void scm_i_sweep_segments (void);
229SCM scm_i_sweep_some_segments (scm_t_cell_type_statistics * fl);
230void scm_i_reset_segments (void);
231void scm_i_sweep_all_segments (char const *reason);
dac04e9f 232void scm_i_make_initial_segment (int init_heap_size, scm_t_cell_type_statistics *freelist);
c7743d02
HWN
233
234extern long int scm_i_deprecated_memory_return;
235
236
237/*
238 global init funcs.
239 */
240void scm_gc_init_malloc (void);
241void scm_gc_init_freelist (void);
242void scm_gc_init_segments (void);
243void scm_gc_init_mark (void);
eab1b259
HWN
244
245
c7743d02 246#endif