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