Prefixed each each exported symbol with SCM_API.
[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_cell
56 {
57 scm_t_bits word_0;
58 scm_t_bits word_1;
59 } scm_cell;
60
61
62 /* SCM_CELLPTR is a pointer to a cons cell which may be compared or
63 * differenced.
64 */
65 typedef scm_cell * SCM_CELLPTR;
66
67
68 /* Cray machines have pointers that are incremented once for each word,
69 * rather than each byte, the 3 most significant bits encode the byte
70 * within the word. The following macros deal with this by storing the
71 * native Cray pointers like the ones that looks like scm expects. This
72 * is done for any pointers that might appear in the car of a scm_cell,
73 * pointers to scm_vector elts, functions, &c are not munged.
74 */
75 #ifdef _UNICOS
76 # define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x) >> 3))
77 # define PTR2SCM(x) (SCM_PACK (((scm_t_bits) (x)) << 3))
78 #else
79 # define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x)))
80 # define PTR2SCM(x) (SCM_PACK ((scm_t_bits) (x)))
81 #endif /* def _UNICOS */
82
83 #define SCM_GC_CARD_N_HEADER_CELLS 1
84 #define SCM_GC_CARD_N_CELLS 256
85
86 #define SCM_GC_CARD_SIZE (SCM_GC_CARD_N_CELLS * sizeof (scm_cell))
87 #define SCM_GC_CARD_N_DATA_CELLS (SCM_GC_CARD_N_CELLS - SCM_GC_CARD_N_HEADER_CELLS)
88
89 #define SCM_GC_CARD_BVEC_SIZE_IN_LIMBS \
90 ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LIMB_BITS - 1) / SCM_C_BVEC_LIMB_BITS)
91
92 #define SCM_GC_IN_CARD_HEADERP(x) \
93 SCM_PTR_LT ((scm_cell *) (x), SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS)
94
95 #define SCM_GC_CARD_BVEC(card) ((scm_t_c_bvec_limb *) ((card)->word_0))
96 #define SCM_GC_SET_CARD_BVEC(card, bvec) \
97 ((card)->word_0 = (scm_t_bits) (bvec))
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_CLR_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_CLR_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_SIZE - 1)
119 #define SCM_GC_CARD_ADDR_MASK (~SCM_GC_CARD_SIZE_MASK)
120
121 #define SCM_GC_CELL_CARD(x) ((SCM_CELLPTR) ((long) (x) & SCM_GC_CARD_ADDR_MASK))
122 #define SCM_GC_CELL_SPAN(x) ((SCM_GC_CARD_DOUBLECELLP (SCM_GC_CELL_CARD (x))) ? 2 : 1)
123 #define SCM_GC_CELL_OFFSET(x) (((long) (x) & SCM_GC_CARD_SIZE_MASK) >> SCM_CELL_SIZE_SHIFT)
124 #define SCM_GC_CELL_BVEC(x) SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (x))
125 #define SCM_GC_CELL_GET_BIT(x) SCM_C_BVEC_GET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
126 #define SCM_GC_CELL_SET_BIT(x) SCM_C_BVEC_SET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
127 #define SCM_GC_CELL_CLR_BIT(x) SCM_C_BVEC_CLR (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
128
129 #define SCM_GC_CARD_UP(x) SCM_GC_CELL_CARD ((char *) (x) + SCM_GC_CARD_SIZE - 1)
130 #define SCM_GC_CARD_DOWN SCM_GC_CELL_CARD
131
132 /* low level bit banging aids */
133
134 typedef unsigned long scm_t_c_bvec_limb;
135
136 #if (SIZEOF_LONG == 8)
137 # define SCM_C_BVEC_LIMB_BITS 64
138 # define SCM_C_BVEC_OFFSET_SHIFT 6
139 # define SCM_C_BVEC_POS_MASK 63
140 # define SCM_CELL_SIZE_SHIFT 4
141 #else
142 # define SCM_C_BVEC_LIMB_BITS 32
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_CLR(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] &= ~(1L << (pos & SCM_C_BVEC_POS_MASK)))
153
154 #define SCM_C_BVEC_BITS2BYTES(bits) \
155 (sizeof (scm_t_c_bvec_limb) * ((((bits) & SCM_C_BVEC_POS_MASK) ? 1L : 0L) + SCM_C_BVEC_OFFSET (bits)))
156
157 #define SCM_C_BVEC_SET_BYTES(bvec, bytes) (memset (bvec, 0xff, bytes))
158 #define SCM_C_BVEC_SET_ALL_BITS(bvec, bits) SCM_C_BVEC_SET_BYTES (bvec, SCM_C_BVEC_BITS2BYTES (bits))
159
160 #define SCM_C_BVEC_CLR_BYTES(bvec, bytes) (memset (bvec, 0, bytes))
161 #define SCM_C_BVEC_CLR_ALL_BITS(bvec, bits) SCM_C_BVEC_CLR_BYTES (bvec, SCM_C_BVEC_BITS2BYTES (bits))
162
163 /* testing and changing GC marks */
164
165 #define SCM_GCMARKP(x) SCM_GC_CELL_GET_BIT (x)
166 #define SCM_SETGCMARK(x) SCM_GC_CELL_SET_BIT (x)
167 #define SCM_CLRGCMARK(x) SCM_GC_CELL_CLR_BIT (x)
168
169 /* Low level cell data accessing macros:
170 */
171
172 #if (SCM_DEBUG_CELL_ACCESSES == 1)
173 # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
174 #else
175 # define SCM_VALIDATE_CELL(cell, expr) (expr)
176 #endif
177
178 #define SCM_CELL_WORD(x, n) \
179 SCM_VALIDATE_CELL ((x), ((const scm_t_bits *) SCM2PTR (x)) [n])
180 #define SCM_CELL_WORD_0(x) SCM_CELL_WORD (x, 0)
181 #define SCM_CELL_WORD_1(x) SCM_CELL_WORD (x, 1)
182 #define SCM_CELL_WORD_2(x) SCM_CELL_WORD (x, 2)
183 #define SCM_CELL_WORD_3(x) SCM_CELL_WORD (x, 3)
184
185 #define SCM_CELL_OBJECT(x, n) \
186 SCM_VALIDATE_CELL ((x), SCM_PACK (((const scm_t_bits *) SCM2PTR (x)) [n]))
187 #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT (x, 0)
188 #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT (x, 1)
189 #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT (x, 2)
190 #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT (x, 3)
191
192 #define SCM_SET_CELL_WORD(x, n, v) \
193 SCM_VALIDATE_CELL ((x), ((scm_t_bits *) SCM2PTR (x)) [n] = (scm_t_bits) (v))
194 #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD (x, 0, v)
195 #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD (x, 1, v)
196 #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD (x, 2, v)
197 #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD (x, 3, v)
198
199 #define SCM_SET_CELL_OBJECT(x, n, v) \
200 SCM_VALIDATE_CELL ((x), ((scm_t_bits *) SCM2PTR (x)) [n] = SCM_UNPACK (v))
201 #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT (x, 0, v)
202 #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT (x, 1, v)
203 #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT (x, 2, v)
204 #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT (x, 3, v)
205
206 #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
207 #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 (x, t)
208
209
210 /* Except for the garbage collector, no part of guile should ever run over a
211 * free cell. Thus, in debug mode the above macros report an error if they
212 * are applied to a free cell. Since the garbage collector is allowed to
213 * access free cells, it needs its own way to access cells which will not
214 * result in errors when in debug mode. */
215
216 #define SCM_GC_CELL_TYPE(x) \
217 (((const scm_t_bits *) SCM2PTR (x)) [0])
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 /* SCM_PTR_LT and friends define how to compare two SCM_CELLPTRs (which may
226 * point to cells in different heap segments).
227 */
228 #define SCM_PTR_LT(x, y) ((x) < (y))
229 #define SCM_PTR_GT(x, y) (SCM_PTR_LT (y, x))
230 #define SCM_PTR_LE(x, y) (!SCM_PTR_GT (x, y))
231 #define SCM_PTR_GE(x, y) (!SCM_PTR_LT (x, y))
232
233
234 /* Freelists consist of linked cells where the type entry holds the value
235 * scm_tc_free_cell and the second entry holds a pointer to the next cell of
236 * the freelist. Due to this structure, freelist cells are not cons cells
237 * and thus may not be accessed using SCM_CAR and SCM_CDR.
238 */
239
240 #define SCM_FREE_CELL_P(x) \
241 (!SCM_IMP (x) && (* (const scm_t_bits *) SCM2PTR (x) == scm_tc_free_cell))
242 #define SCM_FREE_CELL_CDR(x) \
243 (SCM_PACK (((const scm_t_bits *) SCM2PTR (x)) [1]))
244 #define SCM_SET_FREE_CELL_CDR(x, v) \
245 (((scm_t_bits *) SCM2PTR (x)) [1] = SCM_UNPACK (v))
246
247
248 #if (SCM_DEBUG_CELL_ACCESSES == 1)
249 # define SCM_GC_SET_ALLOCATED(x) \
250 (((scm_t_bits *) SCM2PTR (x)) [0] = scm_tc16_allocated)
251 #else
252 # define SCM_GC_SET_ALLOCATED(x)
253 #endif
254
255 #ifdef GUILE_DEBUG_FREELIST
256 #define SCM_NEWCELL(_into) do { _into = scm_debug_newcell (); } while (0)
257 #define SCM_NEWCELL2(_into) do { _into = scm_debug_newcell2 (); } while (0)
258 #else
259 /* When we introduce POSIX threads support, every thread will have
260 a freelist of its own. */
261 #define SCM_NEWCELL(_into) \
262 do { \
263 if (SCM_NULLP (scm_freelist)) \
264 { \
265 _into = scm_gc_for_newcell (&scm_master_freelist, \
266 &scm_freelist); \
267 SCM_GC_SET_ALLOCATED (_into); \
268 } \
269 else \
270 { \
271 _into = scm_freelist; \
272 scm_freelist = SCM_FREE_CELL_CDR (scm_freelist); \
273 SCM_GC_SET_ALLOCATED (_into); \
274 } \
275 } while(0)
276 #define SCM_NEWCELL2(_into) \
277 do { \
278 if (SCM_NULLP (scm_freelist2)) \
279 { \
280 _into = scm_gc_for_newcell (&scm_master_freelist2, \
281 &scm_freelist2); \
282 SCM_GC_SET_ALLOCATED (_into); \
283 } \
284 else \
285 { \
286 _into = scm_freelist2; \
287 scm_freelist2 = SCM_FREE_CELL_CDR (scm_freelist2); \
288 SCM_GC_SET_ALLOCATED (_into); \
289 } \
290 } while(0)
291 #endif
292
293
294 #define SCM_MARKEDP SCM_GCMARKP
295 #define SCM_NMARKEDP(x) (!SCM_MARKEDP (x))
296
297 #if (SCM_DEBUG_CELL_ACCESSES == 1)
298 SCM_API scm_t_bits scm_tc16_allocated;
299 SCM_API unsigned int scm_debug_cell_accesses_p;
300 #endif
301
302 SCM_API struct scm_t_heap_seg_data *scm_heap_table;
303 SCM_API size_t scm_n_heap_segs;
304 SCM_API int scm_block_gc;
305 SCM_API int scm_gc_heap_lock;
306 SCM_API unsigned int scm_gc_running_p;
307 \f
308
309 SCM_API size_t scm_default_init_heap_size_1;
310 SCM_API int scm_default_min_yield_1;
311 SCM_API size_t scm_default_init_heap_size_2;
312 SCM_API int scm_default_min_yield_2;
313 SCM_API size_t scm_default_max_segment_size;
314
315 SCM_API size_t scm_max_segment_size;
316 SCM_API SCM_CELLPTR scm_heap_org;
317 SCM_API SCM scm_freelist;
318 SCM_API struct scm_t_freelist scm_master_freelist;
319 SCM_API SCM scm_freelist2;
320 SCM_API struct scm_t_freelist scm_master_freelist2;
321 SCM_API unsigned long scm_gc_cells_collected;
322 SCM_API unsigned long scm_gc_yield;
323 SCM_API unsigned long scm_gc_malloc_collected;
324 SCM_API unsigned long scm_gc_ports_collected;
325 SCM_API unsigned long scm_cells_allocated;
326 SCM_API unsigned long scm_mallocated;
327 SCM_API unsigned long scm_mtrigger;
328
329 SCM_API SCM scm_after_gc_hook;
330
331 SCM_API scm_t_c_hook scm_before_gc_c_hook;
332 SCM_API scm_t_c_hook scm_before_mark_c_hook;
333 SCM_API scm_t_c_hook scm_before_sweep_c_hook;
334 SCM_API scm_t_c_hook scm_after_sweep_c_hook;
335 SCM_API scm_t_c_hook scm_after_gc_c_hook;
336
337 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
338 SCM_API SCM scm_map_free_list (void);
339 SCM_API SCM scm_free_list_length (void);
340 #endif
341 #ifdef GUILE_DEBUG_FREELIST
342 SCM_API SCM scm_debug_newcell (void);
343 SCM_API SCM scm_debug_newcell2 (void);
344 SCM_API SCM scm_gc_set_debug_check_freelist_x (SCM flag);
345 #endif
346
347 \f
348
349 #if (SCM_DEBUG_CELL_ACCESSES == 1)
350 SCM_API void scm_assert_cell_valid (SCM);
351 SCM_API SCM scm_set_debug_cell_accesses_x (SCM flag);
352 #endif
353 SCM_API SCM scm_object_address (SCM obj);
354 SCM_API SCM scm_unhash_name (SCM name);
355 SCM_API SCM scm_gc_stats (void);
356 SCM_API SCM scm_gc (void);
357 SCM_API void scm_gc_for_alloc (struct scm_t_freelist *freelist);
358 SCM_API SCM scm_gc_for_newcell (struct scm_t_freelist *master, SCM *freelist);
359 #if 0
360 SCM_API void scm_alloc_cluster (struct scm_t_freelist *master);
361 #endif
362 SCM_API void scm_igc (const char *what);
363 SCM_API void scm_gc_mark (SCM p);
364 SCM_API void scm_gc_mark_dependencies (SCM p);
365 SCM_API void scm_mark_locations (SCM_STACKITEM x[], unsigned long n);
366 SCM_API int scm_cellp (SCM value);
367 SCM_API void scm_gc_sweep (void);
368 SCM_API void * scm_must_malloc (size_t len, const char *what);
369 SCM_API void * scm_must_realloc (void *where,
370 size_t olen, size_t len,
371 const char *what);
372 SCM_API char *scm_must_strdup (const char *str);
373 SCM_API char *scm_must_strndup (const char *str, size_t n);
374 SCM_API void scm_done_malloc (long size);
375 SCM_API void scm_done_free (long size);
376 SCM_API void scm_must_free (void *obj);
377 SCM_API void scm_remember_upto_here_1 (SCM obj);
378 SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
379 SCM_API void scm_remember_upto_here (SCM obj1, ...);
380 SCM_API SCM scm_return_first (SCM elt, ...);
381 SCM_API int scm_return_first_int (int x, ...);
382 SCM_API SCM scm_permanent_object (SCM obj);
383 SCM_API SCM scm_gc_protect_object (SCM obj);
384 SCM_API SCM scm_gc_unprotect_object (SCM obj);
385 SCM_API void scm_gc_register_root (SCM *p);
386 SCM_API void scm_gc_unregister_root (SCM *p);
387 SCM_API void scm_gc_register_roots (SCM *b, unsigned long n);
388 SCM_API void scm_gc_unregister_roots (SCM *b, unsigned long n);
389 SCM_API int scm_init_storage (void);
390 SCM_API void *scm_get_stack_base (void);
391 SCM_API void scm_init_gc (void);
392
393 #endif /* SCM_GC_H */
394
395 /*
396 Local Variables:
397 c-file-style: "gnu"
398 End:
399 */