make scm_cells_allocated unsigned again. Thanks to
[bpt/guile.git] / libguile / gc.h
1 /* classes: h_files */
2
3 #ifndef SCM_GC_H
4 #define SCM_GC_H
5
6 /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this software; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307 USA
22 *
23 * As a special exception, the Free Software Foundation gives permission
24 * for additional uses of the text contained in its release of GUILE.
25 *
26 * The exception is that, if you link the GUILE library with other files
27 * to produce an executable, this does not by itself cause the
28 * resulting executable to be covered by the GNU General Public License.
29 * Your use of that executable is in no way restricted on account of
30 * linking the GUILE library code into it.
31 *
32 * This exception does not however invalidate any other reasons why
33 * the executable file might be covered by the GNU General Public License.
34 *
35 * This exception applies only to the code released by the
36 * Free Software Foundation under the name GUILE. If you copy
37 * code from other Free Software Foundation releases into a copy of
38 * GUILE, as the General Public License permits, the exception does
39 * not apply to the code that you add in this way. To avoid misleading
40 * anyone as to the status of such modified files, you must delete
41 * this exception notice from them.
42 *
43 * If you write modifications of your own for GUILE, it is your choice
44 * whether to permit this exception to apply to your modifications.
45 * If you do not wish that, delete this exception notice. */
46
47 \f
48
49 #include "libguile/__scm.h"
50
51 #include "libguile/hooks.h"
52
53 \f
54
55 typedef struct scm_t_cell
56 {
57 scm_t_bits word_0;
58 scm_t_bits word_1;
59 } scm_t_cell;
60
61 /*
62 CARDS
63
64 A card is a small `page' of memory; it will be the unit for lazy
65 sweeping, generations, etc. The first cell of a card contains a
66 pointer to the mark bitvector, so that we can find the bitvector efficiently: we
67 knock off some lowerorder bits.
68
69 The size on a 32 bit machine is 256 cells = 2kb. The card
70 */
71
72
73
74 /* Cray machines have pointers that are incremented once for each word,
75 * rather than each byte, the 3 most significant bits encode the byte
76 * within the word. The following macros deal with this by storing the
77 * native Cray pointers like the ones that looks like scm expects. This
78 * is done for any pointers that might appear in the car of a scm_t_cell,
79 * pointers to scm_vector elts, functions, &c are not munged.
80 */
81 #ifdef _UNICOS
82 # define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x) >> 3))
83 # define PTR2SCM(x) (SCM_PACK (((scm_t_bits) (x)) << 3))
84 #else
85 # define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x)))
86 # define PTR2SCM(x) (SCM_PACK ((scm_t_bits) (x)))
87 #endif /* def _UNICOS */
88
89
90 #define SCM_GC_CARD_N_HEADER_CELLS 1
91 #define SCM_GC_CARD_N_CELLS 256
92 #define SCM_GC_SIZEOF_CARD SCM_GC_CARD_N_CELLS * sizeof (scm_t_cell)
93
94 #define SCM_GC_CARD_BVEC(card) ((scm_t_c_bvec_long *) ((card)->word_0))
95 #define SCM_GC_SET_CARD_BVEC(card, bvec) \
96 ((card)->word_0 = (scm_t_bits) (bvec))
97
98
99 #define SCM_GC_GET_CARD_FLAGS(card) ((long) ((card)->word_1))
100 #define SCM_GC_SET_CARD_FLAGS(card, flags) \
101 ((card)->word_1 = (scm_t_bits) (flags))
102 #define SCM_GC_CLEAR_CARD_FLAGS(card) (SCM_GC_SET_CARD_FLAGS (card, 0L))
103
104 #define SCM_GC_GET_CARD_FLAG(card, shift) (SCM_GC_GET_CARD_FLAGS (card) & (1L << (shift)))
105 #define SCM_GC_SET_CARD_FLAG(card, shift) \
106 (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) | (1L << (shift))))
107 #define SCM_GC_CLEAR_CARD_FLAG(card, shift) \
108 (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) & ~(1L << (shift))))
109
110 #define SCM_GC_CARDF_DOUBLECELL 0
111
112 #define SCM_GC_CARD_DOUBLECELLP(card) SCM_GC_GET_CARD_FLAG (card, SCM_GC_CARDF_DOUBLECELL)
113 #define SCM_GC_SET_CARD_DOUBLECELL(card) SCM_GC_SET_CARD_FLAG (card, SCM_GC_CARDF_DOUBLECELL)
114
115 /* card addressing. for efficiency, cards are *always* aligned to
116 SCM_GC_CARD_SIZE. */
117
118 #define SCM_GC_CARD_SIZE_MASK (SCM_GC_CARD_N_CELLS * sizeof (scm_t_cell) - 1)
119 #define SCM_GC_CARD_ADDR_MASK (~SCM_GC_CARD_SIZE_MASK)
120
121 #define SCM_GC_CELL_CARD(x) ((scm_t_cell *) ((long) (x) & SCM_GC_CARD_ADDR_MASK))
122 #define SCM_GC_CELL_OFFSET(x) (((long) (x) & SCM_GC_CARD_SIZE_MASK) >> SCM_CELL_SIZE_SHIFT)
123 #define SCM_GC_CELL_BVEC(x) SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (x))
124 #define SCM_GC_CELL_GET_BIT(x) SCM_C_BVEC_GET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
125 #define SCM_GC_CELL_SET_BIT(x) SCM_C_BVEC_SET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
126 #define SCM_GC_CELL_CLEAR_BIT(x) SCM_C_BVEC_CLEAR (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
127
128 #define SCM_GC_CARD_UP(x) SCM_GC_CELL_CARD ((char *) (x) + SCM_GC_SIZEOF_CARD - 1)
129 #define SCM_GC_CARD_DOWN SCM_GC_CELL_CARD
130
131 /* low level bit banging aids */
132 typedef unsigned long scm_t_c_bvec_long;
133
134 #if (SIZEOF_LONG == 8)
135 # define SCM_C_BVEC_LONG_BITS 64
136 # define SCM_C_BVEC_OFFSET_SHIFT 6
137 # define SCM_C_BVEC_POS_MASK 63
138 # define SCM_CELL_SIZE_SHIFT 4
139 # define SCM_SIZEOF_LONG SIZEOF_LONG
140 #else
141 # define SCM_C_BVEC_LONG_BITS 32
142 # define SCM_SIZEOF_LONG SIZEOF_LONG
143 # define SCM_C_BVEC_OFFSET_SHIFT 5
144 # define SCM_C_BVEC_POS_MASK 31
145 # define SCM_CELL_SIZE_SHIFT 3
146 #endif
147
148 #define SCM_C_BVEC_OFFSET(pos) (pos >> SCM_C_BVEC_OFFSET_SHIFT)
149
150 #define SCM_C_BVEC_GET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] & (1L << (pos & SCM_C_BVEC_POS_MASK)))
151 #define SCM_C_BVEC_SET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] |= (1L << (pos & SCM_C_BVEC_POS_MASK)))
152 #define SCM_C_BVEC_CLEAR(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] &= ~(1L << (pos & SCM_C_BVEC_POS_MASK)))
153
154 /* testing and changing GC marks */
155 #define SCM_GC_MARK_P(x) SCM_GC_CELL_GET_BIT (x)
156 #define SCM_SET_GC_MARK(x) SCM_GC_CELL_SET_BIT (x)
157 #define SCM_CLEAR_GC_MARK(x) SCM_GC_CELL_CLEAR_BIT (x)
158
159 /* Low level cell data accessing macros. These macros should only be used
160 * from within code related to garbage collection issues, since they will
161 * never check the cells they are applied to - not even if guile is compiled
162 * in debug mode. In particular these macros will even work for free cells,
163 * which should never be encountered by user code. */
164
165 #define SCM_GC_CELL_WORD(x, n) \
166 (((const scm_t_bits *) SCM2PTR (x)) [n])
167 #define SCM_GC_CELL_OBJECT(x, n) \
168 (SCM_PACK (((const scm_t_bits *) SCM2PTR (x)) [n]))
169 #define SCM_GC_SET_CELL_WORD(x, n, v) \
170 (((scm_t_bits *) SCM2PTR (x)) [n] = (scm_t_bits) (v))
171 #define SCM_GC_SET_CELL_OBJECT(x, n, v) \
172 (((scm_t_bits *) SCM2PTR (x)) [n] = SCM_UNPACK (v))
173 #define SCM_GC_CELL_TYPE(x) SCM_GC_CELL_WORD (x, 0)
174
175
176 /* Except for the garbage collector, no part of guile should ever run over a
177 * free cell. Thus, if guile is compiled in debug mode the SCM_CELL_* and
178 * SCM_SET_CELL_* macros below report an error if they are applied to a free
179 * cell. Some other plausibility checks are also performed. However, if
180 * guile is not compiled in debug mode, there won't be any time penalty at all
181 * when using these macros. */
182
183 #if (SCM_DEBUG_CELL_ACCESSES == 1)
184 # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
185 #else
186 # define SCM_VALIDATE_CELL(cell, expr) (expr)
187 #endif
188
189 #define SCM_CELL_WORD(x, n) \
190 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_WORD ((x), (n)))
191 #define SCM_CELL_WORD_0(x) SCM_CELL_WORD (x, 0)
192 #define SCM_CELL_WORD_1(x) SCM_CELL_WORD (x, 1)
193 #define SCM_CELL_WORD_2(x) SCM_CELL_WORD (x, 2)
194 #define SCM_CELL_WORD_3(x) SCM_CELL_WORD (x, 3)
195
196 #define SCM_CELL_OBJECT(x, n) \
197 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_OBJECT ((x), (n)))
198 #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT (x, 0)
199 #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT (x, 1)
200 #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT (x, 2)
201 #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT (x, 3)
202
203 #define SCM_SET_CELL_WORD(x, n, v) \
204 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_WORD ((x), (n), (v)))
205 #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD (x, 0, v)
206 #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD (x, 1, v)
207 #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD (x, 2, v)
208 #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD (x, 3, v)
209
210 #define SCM_SET_CELL_OBJECT(x, n, v) \
211 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_OBJECT ((x), (n), (v)))
212 #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT (x, 0, v)
213 #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT (x, 1, v)
214 #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT (x, 2, v)
215 #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT (x, 3, v)
216
217 #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
218 #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 (x, t)
219
220
221
222 /* Freelists consist of linked cells where the type entry holds the value
223 * scm_tc_free_cell and the second entry holds a pointer to the next cell of
224 * the freelist. Due to this structure, freelist cells are not cons cells
225 * and thus may not be accessed using SCM_CAR and SCM_CDR. */
226
227 /*
228 SCM_FREECELL_P removed ; the semantics are ambiguous with lazy
229 sweeping. Could mean "this cell is no longer in use (will be swept)"
230 or "this cell has just been swept, and is not yet in use".
231 */
232
233 #define SCM_FREECELL_P this_macro_has_been_removed_see_gc_header_file
234
235 #define SCM_FREE_CELL_CDR(x) \
236 (SCM_GC_CELL_OBJECT ((x), 1))
237 #define SCM_SET_FREE_CELL_CDR(x, v) \
238 (SCM_GC_SET_CELL_OBJECT ((x), 1, (v)))
239
240
241 #define SCM_CELL_WORD_LOC(x, n) ((scm_t_bits *) & SCM_CELL_WORD (x, n))
242 #define SCM_CARLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 0))
243 #define SCM_CDRLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 1))
244
245
246
247
248 #if (SCM_DEBUG_CELL_ACCESSES == 1)
249 /* Set this to != 0 if every cell that is accessed shall be checked:
250 */
251 SCM_API int scm_debug_cell_accesses_p;
252 SCM_API int scm_expensive_debug_cell_accesses_p;
253 SCM_API int scm_debug_cells_gc_interval ;
254 #endif
255
256 SCM_API int scm_block_gc;
257 SCM_API int scm_gc_heap_lock;
258 SCM_API unsigned int scm_gc_running_p;
259 \f
260
261 #if (SCM_ENABLE_DEPRECATED == 1)
262 SCM_API size_t scm_default_init_heap_size_1;
263 SCM_API int scm_default_min_yield_1;
264 SCM_API size_t scm_default_init_heap_size_2;
265 SCM_API int scm_default_min_yield_2;
266 SCM_API size_t scm_default_max_segment_size;
267 #else
268 #define scm_default_init_heap_size_1 deprecated
269 #define scm_default_min_yield_1 deprecated
270 #define scm_default_init_heap_size_2 deprecated
271 #define scm_default_min_yield_2 deprecated
272 #define scm_default_max_segment_size deprecated
273 #endif
274
275
276 SCM_API size_t scm_max_segment_size;
277
278 /*
279 Deprecated scm_freelist, scm_master_freelist.
280 No warning; this is not a user serviceable part.
281 */
282 extern SCM scm_i_freelist;
283 extern struct scm_t_cell_type_statistics scm_i_master_freelist;
284 extern SCM scm_i_freelist2;
285 extern struct scm_t_cell_type_statistics scm_i_master_freelist2;
286
287
288 SCM_API unsigned long scm_gc_cells_swept;
289 SCM_API unsigned long scm_gc_cells_collected;
290 SCM_API unsigned long scm_gc_cells_collected;
291 SCM_API unsigned long scm_gc_malloc_collected;
292 SCM_API unsigned long scm_gc_ports_collected;
293 SCM_API unsigned long scm_cells_allocated;
294 SCM_API int scm_gc_cell_yield_percentage;
295 SCM_API int scm_gc_malloc_yield_percentage;
296 SCM_API unsigned long scm_mallocated;
297 SCM_API unsigned long scm_mtrigger;
298
299
300
301 SCM_API SCM scm_after_gc_hook;
302
303 SCM_API scm_t_c_hook scm_before_gc_c_hook;
304 SCM_API scm_t_c_hook scm_before_mark_c_hook;
305 SCM_API scm_t_c_hook scm_before_sweep_c_hook;
306 SCM_API scm_t_c_hook scm_after_sweep_c_hook;
307 SCM_API scm_t_c_hook scm_after_gc_c_hook;
308
309 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
310 #define scm_map_free_list deprecated
311 #define scm_free_list_length deprecated
312 #endif
313
314 #if (SCM_ENABLE_DEPRECATED == 1) && defined (GUILE_DEBUG_FREELIST)
315 SCM_API SCM scm_gc_set_debug_check_freelist_x (SCM flag);
316 #endif
317 \f
318
319 #if (SCM_DEBUG_CELL_ACCESSES == 1)
320 SCM_API void scm_assert_cell_valid (SCM);
321 #endif
322
323 SCM_API SCM scm_set_debug_cell_accesses_x (SCM flag);
324
325
326 SCM_API SCM scm_object_address (SCM obj);
327 SCM_API SCM scm_gc_stats (void);
328 SCM_API SCM scm_gc (void);
329 SCM_API void scm_gc_for_alloc (struct scm_t_cell_type_statistics *freelist);
330 SCM_API SCM scm_gc_for_newcell (struct scm_t_cell_type_statistics *master, SCM *freelist);
331 SCM_API void scm_igc (const char *what);
332 SCM_API void scm_gc_mark (SCM p);
333 SCM_API void scm_gc_mark_dependencies (SCM p);
334 SCM_API void scm_mark_locations (SCM_STACKITEM x[], unsigned long n);
335 SCM_API int scm_in_heap_p (SCM value);
336 SCM_API void scm_gc_sweep (void);
337
338 SCM_API void *scm_malloc (size_t size);
339 SCM_API void *scm_calloc (size_t size);
340 SCM_API void *scm_realloc (void *mem, size_t size);
341 SCM_API char *scm_strdup (const char *str);
342 SCM_API char *scm_strndup (const char *str, size_t n);
343 SCM_API void scm_gc_register_collectable_memory (void *mem, size_t size,
344 const char *what);
345 SCM_API void scm_gc_unregister_collectable_memory (void *mem, size_t size,
346 const char *what);
347 SCM_API void *scm_gc_calloc (size_t size, const char *what);
348 SCM_API void *scm_gc_malloc (size_t size, const char *what);
349 SCM_API void *scm_gc_realloc (void *mem, size_t old_size,
350 size_t new_size, const char *what);
351 SCM_API void scm_gc_free (void *mem, size_t size, const char *what);
352 SCM_API char *scm_gc_strdup (const char *str, const char *what);
353 SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what);
354
355 SCM_API void scm_remember_upto_here_1 (SCM obj);
356 SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
357 SCM_API void scm_remember_upto_here (SCM obj1, ...);
358 SCM_API SCM scm_return_first (SCM elt, ...);
359 SCM_API int scm_return_first_int (int x, ...);
360 SCM_API SCM scm_permanent_object (SCM obj);
361 SCM_API SCM scm_gc_protect_object (SCM obj);
362 SCM_API SCM scm_gc_unprotect_object (SCM obj);
363 SCM_API void scm_gc_register_root (SCM *p);
364 SCM_API void scm_gc_unregister_root (SCM *p);
365 SCM_API void scm_gc_register_roots (SCM *b, unsigned long n);
366 SCM_API void scm_gc_unregister_roots (SCM *b, unsigned long n);
367 SCM_API int scm_init_storage (void);
368 SCM_API void *scm_get_stack_base (void);
369 SCM_API void scm_init_gc (void);
370
371 #if SCM_ENABLE_DEPRECATED == 1
372
373 SCM_API SCM scm_deprecated_newcell (void);
374 SCM_API SCM scm_deprecated_newcell2 (void);
375
376 #define SCM_NEWCELL(_into) \
377 do { _into = scm_deprecated_newcell (); } while (0)
378 #define SCM_NEWCELL2(_into) \
379 do { _into = scm_deprecated_newcell2 (); } while (0)
380
381 SCM_API void * scm_must_malloc (size_t len, const char *what);
382 SCM_API void * scm_must_realloc (void *where,
383 size_t olen, size_t len,
384 const char *what);
385 SCM_API char *scm_must_strdup (const char *str);
386 SCM_API char *scm_must_strndup (const char *str, size_t n);
387 SCM_API void scm_done_malloc (long size);
388 SCM_API void scm_done_free (long size);
389 SCM_API void scm_must_free (void *obj);
390
391 #endif
392
393 #endif /* SCM_GC_H */
394
395 /*
396 Local Variables:
397 c-file-style: "gnu"
398 End:
399 */