* gc.h (SCM_MARKEDP): simplified, there are no different mark bit
[bpt/guile.git] / libguile / gc.h
1 /* classes: h_files */
2
3 #ifndef GCH
4 #define GCH
5 /* Copyright (C) 1995, 96, 98, 99, 2000 Free Software Foundation, Inc.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this software; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307 USA
21 *
22 * As a special exception, the Free Software Foundation gives permission
23 * for additional uses of the text contained in its release of GUILE.
24 *
25 * The exception is that, if you link the GUILE library with other files
26 * to produce an executable, this does not by itself cause the
27 * resulting executable to be covered by the GNU General Public License.
28 * Your use of that executable is in no way restricted on account of
29 * linking the GUILE library code into it.
30 *
31 * This exception does not however invalidate any other reasons why
32 * the executable file might be covered by the GNU General Public License.
33 *
34 * This exception applies only to the code released by the
35 * Free Software Foundation under the name GUILE. If you copy
36 * code from other Free Software Foundation releases into a copy of
37 * GUILE, as the General Public License permits, the exception does
38 * not apply to the code that you add in this way. To avoid misleading
39 * anyone as to the status of such modified files, you must delete
40 * this exception notice from them.
41 *
42 * If you write modifications of your own for GUILE, it is your choice
43 * whether to permit this exception to apply to your modifications.
44 * If you do not wish that, delete this exception notice. */
45
46 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
47 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48 \f
49
50 #include "libguile/__scm.h"
51
52 #include "libguile/hooks.h"
53
54 \f
55
56 typedef struct scm_cell
57 {
58 scm_bits_t word_0;
59 scm_bits_t word_1;
60 } scm_cell;
61
62
63 /* SCM_CELLPTR is a pointer to a cons cell which may be compared or
64 * differenced.
65 */
66 typedef scm_cell * SCM_CELLPTR;
67
68
69 /* Cray machines have pointers that are incremented once for each word,
70 * rather than each byte, the 3 most significant bits encode the byte
71 * within the word. The following macros deal with this by storing the
72 * native Cray pointers like the ones that looks like scm expects. This
73 * is done for any pointers that might appear in the car of a scm_cell,
74 * pointers to scm_vector elts, functions, &c are not munged.
75 */
76 #ifdef _UNICOS
77 # define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x) >> 3))
78 # define PTR2SCM(x) (SCM_PACK (((scm_bits_t) (x)) << 3))
79 #else
80 # define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x)))
81 # define PTR2SCM(x) (SCM_PACK ((scm_bits_t) (x)))
82 #endif /* def _UNICOS */
83
84 /* This mess was copied from the GNU getpagesize.h. */
85
86 #ifndef HAVE_GETPAGESIZE
87
88 /* Assume that all systems that can run configure have sys/param.h. */
89 # ifndef HAVE_SYS_PARAM_H
90 # define HAVE_SYS_PARAM_H 1
91 # endif
92
93 # ifdef _SC_PAGESIZE
94 # define getpagesize() sysconf(_SC_PAGESIZE)
95 # else /* no _SC_PAGESIZE */
96 # ifdef HAVE_SYS_PARAM_H
97 # include <sys/param.h>
98 # ifdef EXEC_PAGESIZE
99 # define getpagesize() EXEC_PAGESIZE
100 # else /* no EXEC_PAGESIZE */
101 # ifdef NBPG
102 # define getpagesize() NBPG * CLSIZE
103 # ifndef CLSIZE
104 # define CLSIZE 1
105 # endif /* no CLSIZE */
106 # else /* no NBPG */
107 # ifdef NBPC
108 # define getpagesize() NBPC
109 # else /* no NBPC */
110 # ifdef PAGESIZE
111 # define getpagesize() PAGESIZE
112 # endif /* PAGESIZE */
113 # endif /* no NBPC */
114 # endif /* no NBPG */
115 # endif /* no EXEC_PAGESIZE */
116 # else /* no HAVE_SYS_PARAM_H */
117 # define getpagesize() 8192 /* punt totally */
118 # endif /* no HAVE_SYS_PARAM_H */
119 # endif /* no _SC_PAGESIZE */
120
121 #endif /* no HAVE_GETPAGESIZE */
122
123 #define SCM_GC_CARD_N_HEADER_CELLS 1
124 #define SCM_GC_CARD_N_CELLS (8 * sizeof (scm_cell) * 4)
125
126 #define SCM_GC_CARD_SIZE (SCM_GC_CARD_N_CELLS * sizeof (scm_cell))
127 #define SCM_GC_CARD_N_DATA_CELLS (SCM_GC_CARD_N_CELLS - SCM_GC_CARD_N_HEADER_CELLS)
128
129 #define SCM_GC_CARD_BVEC_SIZE_IN_LIMBS \
130 ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LIMB_BITS - 1) / SCM_C_BVEC_LIMB_BITS)
131
132 #define SCM_GC_IN_CARD_HEADERP(x) \
133 SCM_PTR_LT ((scm_cell *) (x), SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS)
134
135 #define SCM_GC_CARD_BVEC(card) ((scm_c_bvec_limb_t *) ((card)->word_0))
136
137 #define SCM_GC_GET_CARD_FLAGS(card) ((long) ((card)->word_1))
138 #define SCM_GC_SET_CARD_FLAGS(card, flags) (SCM_GC_GET_CARD_FLAGS (card) = (flags))
139 #define SCM_GC_CLR_CARD_FLAGS(card) (SCM_GC_GET_CARD_FLAGS (card) = 0L)
140
141 #define SCM_GC_GET_CARD_FLAG(card, shift) (SCM_GC_GET_CARD_FLAGS (card) & (1L << (shift)))
142 #define SCM_GC_SET_CARD_FLAG(card, shift) (SCM_GC_GET_CARD_FLAGS (card) |= (1L << (shift)))
143 #define SCM_GC_CLR_CARD_FLAG(card, shift) (SCM_GC_GET_CARD_FLAGS (card) &= ~(1L << (shift)))
144
145 #define SCM_GC_CARDF_DOUBLECELL 0
146
147 #define SCM_GC_CARD_DOUBLECELLP(card) SCM_GC_GET_CARD_FLAG (card, SCM_GC_CARDF_DOUBLECELL)
148 #define SCM_GC_SET_CARD_DOUBLECELL(card) SCM_GC_SET_CARD_FLAG (card, SCM_GC_CARDF_DOUBLECELL)
149
150 /* card addressing. for efficiency, cards are *always* aligned to
151 SCM_GC_CARD_SIZE. */
152
153 #define SCM_GC_CARD_SIZE_MASK (SCM_GC_CARD_SIZE - 1)
154 #define SCM_GC_CARD_ADDR_MASK (~SCM_GC_CARD_SIZE_MASK)
155
156 #define SCM_GC_CELL_CARD(x) ((SCM_CELLPTR) ((long) (x) & SCM_GC_CARD_ADDR_MASK))
157 #define SCM_GC_CELL_SPAN(x) ((SCM_GC_CARD_DOUBLECELLP (SCM_GC_CELL_CARD (x))) ? 2 : 1)
158 #define SCM_GC_CELL_OFFSET(x) (((long) (x) & SCM_GC_CARD_SIZE_MASK) >> SCM_CELL_SIZE_SHIFT)
159 #define SCM_GC_CELL_BVEC(x) SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (x))
160 #define SCM_GC_CELL_GET_BIT(x) SCM_C_BVEC_GET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
161 #define SCM_GC_CELL_SET_BIT(x) SCM_C_BVEC_SET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
162 #define SCM_GC_CELL_CLR_BIT(x) SCM_C_BVEC_CLR (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
163
164 #define SCM_GC_CARD_UP(x) SCM_GC_CELL_CARD ((char *) (x) + SCM_GC_CARD_SIZE - 1)
165 #define SCM_GC_CARD_DOWN SCM_GC_CELL_CARD
166
167 /* low level bit banging aids */
168
169 typedef unsigned long scm_c_bvec_limb_t;
170
171 #if (SIZEOF_LONG == 8)
172 # define SCM_C_BVEC_LIMB_BITS 64
173 # define SCM_C_BVEC_OFFSET_SHIFT 6
174 # define SCM_C_BVEC_POS_MASK 63
175 # define SCM_CELL_SIZE_SHIFT 4
176 #else
177 # define SCM_C_BVEC_LIMB_BITS 32
178 # define SCM_C_BVEC_OFFSET_SHIFT 5
179 # define SCM_C_BVEC_POS_MASK 31
180 # define SCM_CELL_SIZE_SHIFT 3
181 #endif
182
183 #define SCM_C_BVEC_OFFSET(pos) (pos >> SCM_C_BVEC_OFFSET_SHIFT)
184
185 #define SCM_C_BVEC_GET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] & (1L << (pos & SCM_C_BVEC_POS_MASK)))
186 #define SCM_C_BVEC_SET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] |= (1L << (pos & SCM_C_BVEC_POS_MASK)))
187 #define SCM_C_BVEC_CLR(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] &= ~(1L << (pos & SCM_C_BVEC_POS_MASK)))
188
189 #define SCM_C_BVEC_BITS2BYTES(bits) \
190 (sizeof (scm_c_bvec_limb_t) * ((((bits) & SCM_C_BVEC_POS_MASK) ? 1L : 0L) + SCM_C_BVEC_OFFSET (bits)))
191
192 #define SCM_C_BVEC_SET_BYTES(bvec, bytes) (memset (bvec, 0xff, bytes))
193 #define SCM_C_BVEC_SET_ALL_BITS(bvec, bits) SCM_C_BVEC_SET_BYTES (bvec, SCM_C_BVEC_BITS2BYTES (bits))
194
195 #define SCM_C_BVEC_CLR_BYTES(bvec, bytes) (memset (bvec, 0, bytes))
196 #define SCM_C_BVEC_CLR_ALL_BITS(bvec, bits) SCM_C_BVEC_CLR_BYTES (bvec, SCM_C_BVEC_BITS2BYTES (bits))
197
198 /* testing and changing GC marks */
199
200 #define SCM_GCMARKP(x) SCM_GC_CELL_GET_BIT (x)
201 #define SCM_SETGCMARK(x) SCM_GC_CELL_SET_BIT (x)
202 #define SCM_CLRGCMARK(x) SCM_GC_CELL_CLR_BIT (x)
203
204 /* compatibility stuff: */
205
206 #define SCM_GC8MARKP(x) SCM_GCMARKP (x)
207 #define SCM_SETGC8MARK(x) SCM_SETGCMARK (x)
208 #define SCM_CLRGC8MARK(x) SCM_CLRGCMARK (x)
209
210 #define SCM_GCTYP16(x) SCM_TYP16 (x)
211 #define SCM_GCCDR(x) SCM_CDR (x)
212
213 /* Low level cell data accessing macros:
214 */
215
216 #if (SCM_DEBUG_CELL_ACCESSES == 1)
217 # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
218 #else
219 # define SCM_VALIDATE_CELL(cell, expr) expr
220 #endif
221
222 #define SCM_CELL_WORD(x, n) \
223 SCM_VALIDATE_CELL ((x), ((const scm_bits_t *) SCM2PTR (x)) [n])
224 #define SCM_CELL_WORD_0(x) SCM_CELL_WORD (x, 0)
225 #define SCM_CELL_WORD_1(x) SCM_CELL_WORD (x, 1)
226 #define SCM_CELL_WORD_2(x) SCM_CELL_WORD (x, 2)
227 #define SCM_CELL_WORD_3(x) SCM_CELL_WORD (x, 3)
228
229 #define SCM_CELL_OBJECT(x, n) \
230 SCM_VALIDATE_CELL ((x), SCM_PACK (((const scm_bits_t *) SCM2PTR (x)) [n]))
231 #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT (x, 0)
232 #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT (x, 1)
233 #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT (x, 2)
234 #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT (x, 3)
235
236 #define SCM_SET_CELL_WORD(x, n, v) \
237 SCM_VALIDATE_CELL ((x), ((scm_bits_t *) SCM2PTR (x)) [n] = (scm_bits_t) (v))
238 #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD (x, 0, v)
239 #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD (x, 1, v)
240 #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD (x, 2, v)
241 #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD (x, 3, v)
242
243 #define SCM_SET_CELL_OBJECT(x, n, v) \
244 SCM_VALIDATE_CELL ((x), ((scm_bits_t *) SCM2PTR (x)) [n] = SCM_UNPACK (v))
245 #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT (x, 0, v)
246 #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT (x, 1, v)
247 #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT (x, 2, v)
248 #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT (x, 3, v)
249
250 #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
251 #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 (x, t)
252
253 #define SCM_SETAND_CAR(x, y) \
254 (SCM_SETCAR ((x), SCM_PACK (SCM_UNPACK (SCM_CAR (x)) & (y))))
255 #define SCM_SETAND_CDR(x, y)\
256 (SCM_SETCDR ((x), SCM_PACK (SCM_UNPACK (SCM_CDR (x)) & (y))))
257 #define SCM_SETOR_CAR(x, y)\
258 (SCM_SETCAR ((x), SCM_PACK (SCM_UNPACK (SCM_CAR (x)) | (y))))
259 #define SCM_SETOR_CDR(x, y)\
260 (SCM_SETCDR ((x), SCM_PACK (SCM_UNPACK (SCM_CDR (x)) | (y))))
261
262 #define SCM_CELL_WORD_LOC(x, n) ((scm_bits_t *) & SCM_CELL_WORD (x, n))
263 #define SCM_CARLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 0))
264 #define SCM_CDRLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 1))
265
266
267 /* SCM_PTR_LT and friends define how to compare two SCM_CELLPTRs (which may
268 * point to cells in different heap segments).
269 */
270 #define SCM_PTR_LT(x, y) ((x) < (y))
271 #define SCM_PTR_GT(x, y) (SCM_PTR_LT (y, x))
272 #define SCM_PTR_LE(x, y) (!SCM_PTR_GT (x, y))
273 #define SCM_PTR_GE(x, y) (!SCM_PTR_LT (x, y))
274
275
276 /* Freelists consist of linked cells where the type entry holds the value
277 * scm_tc_free_cell and the second entry holds a pointer to the next cell of
278 * the freelist. Due to this structure, freelist cells are not cons cells
279 * and thus may not be accessed using SCM_CAR and SCM_CDR.
280 */
281
282 #define SCM_FREE_CELL_P(x) \
283 (!SCM_IMP (x) && (* (const scm_bits_t *) SCM2PTR (x) == scm_tc_free_cell))
284 #define SCM_FREE_CELL_CDR(x) \
285 (SCM_PACK (((const scm_bits_t *) SCM2PTR (x)) [1]))
286 #define SCM_SET_FREE_CELL_TYPE(x, v) \
287 (((scm_bits_t *) SCM2PTR (x)) [0] = (v))
288 #define SCM_SET_FREE_CELL_CDR(x, v) \
289 (((scm_bits_t *) SCM2PTR (x)) [1] = SCM_UNPACK (v))
290
291 /* the allocated thing: The car of new cells is set to
292 scm_tc16_allocated to avoid the fragile state of newcells wrt the
293 gc. If it stays as a freecell, any allocation afterwards could
294 cause the cell to go back on the freelist, which will bite you
295 sometime afterwards. */
296
297 #ifdef GUILE_DEBUG_FREELIST
298 #define SCM_NEWCELL(_into) do { _into = scm_debug_newcell (); } while (0)
299 #define SCM_NEWCELL2(_into) do { _into = scm_debug_newcell2 (); } while (0)
300 #else
301 /* When we introduce POSIX threads support, every thread will have
302 a freelist of its own. Then it won't any longer be necessary to
303 initialize cells with scm_tc16_allocated. */
304 #define SCM_NEWCELL(_into) \
305 do { \
306 if (SCM_IMP (scm_freelist)) \
307 _into = scm_gc_for_newcell (&scm_master_freelist, \
308 &scm_freelist); \
309 else \
310 { \
311 _into = scm_freelist; \
312 scm_freelist = SCM_FREE_CELL_CDR (scm_freelist); \
313 SCM_SET_FREE_CELL_TYPE (_into, scm_tc16_allocated); \
314 } \
315 } while(0)
316 #define SCM_NEWCELL2(_into) \
317 do { \
318 if (SCM_IMP (scm_freelist2)) \
319 _into = scm_gc_for_newcell (&scm_master_freelist2, \
320 &scm_freelist2); \
321 else \
322 { \
323 _into = scm_freelist2; \
324 scm_freelist2 = SCM_FREE_CELL_CDR (scm_freelist2); \
325 SCM_SET_FREE_CELL_TYPE (_into, scm_tc16_allocated); \
326 } \
327 } while(0)
328 #endif
329
330
331 #define SCM_FREEP(x) (SCM_FREE_CELL_P (x))
332 #define SCM_NFREEP(x) (!SCM_FREEP (x))
333
334 #define SCM_MARKEDP SCM_GCMARKP
335 #define SCM_NMARKEDP(x) (!SCM_MARKEDP (x))
336
337 extern struct scm_heap_seg_data_t *scm_heap_table;
338 extern int scm_n_heap_segs;
339 extern int scm_block_gc;
340 extern int scm_gc_heap_lock;
341 extern unsigned int scm_gc_running_p;
342 \f
343
344 extern int scm_default_init_heap_size_1;
345 extern int scm_default_min_yield_1;
346 extern int scm_default_init_heap_size_2;
347 extern int scm_default_min_yield_2;
348 extern int scm_default_max_segment_size;
349
350 extern scm_sizet scm_max_segment_size;
351 extern SCM_CELLPTR scm_heap_org;
352 extern SCM scm_freelist;
353 extern struct scm_freelist_t scm_master_freelist;
354 extern SCM scm_freelist2;
355 extern struct scm_freelist_t scm_master_freelist2;
356 extern unsigned long scm_gc_cells_collected;
357 extern unsigned long scm_gc_yield;
358 extern unsigned long scm_gc_malloc_collected;
359 extern unsigned long scm_gc_ports_collected;
360 extern unsigned long scm_cells_allocated;
361 extern long scm_mallocated;
362 extern unsigned long scm_mtrigger;
363
364 extern SCM scm_after_gc_hook;
365
366 extern scm_c_hook_t scm_before_gc_c_hook;
367 extern scm_c_hook_t scm_before_mark_c_hook;
368 extern scm_c_hook_t scm_before_sweep_c_hook;
369 extern scm_c_hook_t scm_after_sweep_c_hook;
370 extern scm_c_hook_t scm_after_gc_c_hook;
371
372 #if (SCM_DEBUG_CELL_ACCESSES == 1)
373 extern void scm_assert_cell_valid (SCM);
374 extern unsigned int scm_debug_cell_accesses_p;
375 extern SCM scm_set_debug_cell_accesses_x (SCM flag);
376 #endif
377
378 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
379 extern SCM scm_map_free_list (void);
380 extern SCM scm_free_list_length (void);
381 #endif
382 #ifdef GUILE_DEBUG_FREELIST
383 extern SCM scm_debug_newcell (void);
384 extern SCM scm_debug_newcell2 (void);
385 extern SCM scm_gc_set_debug_check_freelist_x (SCM flag);
386 #endif
387
388 \f
389
390 extern SCM scm_object_address (SCM obj);
391 extern SCM scm_unhash_name (SCM name);
392 extern SCM scm_gc_stats (void);
393 extern SCM scm_gc (void);
394 extern void scm_gc_for_alloc (struct scm_freelist_t *freelist);
395 extern SCM scm_gc_for_newcell (struct scm_freelist_t *master, SCM *freelist);
396 #if 0
397 extern void scm_alloc_cluster (struct scm_freelist_t *master);
398 #endif
399 extern void scm_igc (const char *what);
400 extern void scm_gc_mark (SCM p);
401 extern void scm_mark_locations (SCM_STACKITEM x[], scm_sizet n);
402 extern int scm_cellp (SCM value);
403 extern void scm_gc_sweep (void);
404 extern void * scm_must_malloc (scm_sizet len, const char *what);
405 extern void * scm_must_realloc (void *where,
406 scm_sizet olen, scm_sizet len,
407 const char *what);
408 extern void scm_done_malloc (long size);
409 extern void scm_done_free (long size);
410 extern void scm_must_free (void *obj);
411 extern void scm_remember (SCM * ptr);
412 extern SCM scm_return_first (SCM elt, ...);
413 extern int scm_return_first_int (int x, ...);
414 extern SCM scm_permanent_object (SCM obj);
415 extern SCM scm_protect_object (SCM obj);
416 extern SCM scm_unprotect_object (SCM obj);
417 extern int scm_init_storage (scm_sizet init_heap_size, int trig,
418 scm_sizet init_heap2_size, int trig2,
419 scm_sizet max_segment_size);
420 extern void scm_init_gc (void);
421 #endif /* GCH */
422
423 /*
424 Local Variables:
425 c-file-style: "gnu"
426 End:
427 */