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