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