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