* validate.h
[bpt/guile.git] / libguile / gc.h
1 /* classes: h_files */
2
3 #ifndef SCM_GC_H
4 #define SCM_GC_H
5 /* Copyright (C) 1995,1996,1998,1999,2000,2001 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 #define SCM_GC_CARD_N_HEADER_CELLS 1
85 #define SCM_GC_CARD_N_CELLS 256
86
87 #define SCM_GC_CARD_SIZE (SCM_GC_CARD_N_CELLS * sizeof (scm_cell))
88 #define SCM_GC_CARD_N_DATA_CELLS (SCM_GC_CARD_N_CELLS - SCM_GC_CARD_N_HEADER_CELLS)
89
90 #define SCM_GC_CARD_BVEC_SIZE_IN_LIMBS \
91 ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LIMB_BITS - 1) / SCM_C_BVEC_LIMB_BITS)
92
93 #define SCM_GC_IN_CARD_HEADERP(x) \
94 SCM_PTR_LT ((scm_cell *) (x), SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS)
95
96 #define SCM_GC_CARD_BVEC(card) ((scm_c_bvec_limb_t *) ((card)->word_0))
97 #define SCM_GC_SET_CARD_BVEC(card, bvec) \
98 ((card)->word_0 = (scm_bits_t) (bvec))
99
100 #define SCM_GC_GET_CARD_FLAGS(card) ((scm_ubits_t) ((card)->word_1))
101 #define SCM_GC_SET_CARD_FLAGS(card, flags) \
102 ((card)->word_1 = (scm_bits_t) (flags))
103 #define SCM_GC_CLR_CARD_FLAGS(card) (SCM_GC_SET_CARD_FLAGS (card, 0L))
104
105 #define SCM_GC_GET_CARD_FLAG(card, shift) (SCM_GC_GET_CARD_FLAGS (card) & (1L << (shift)))
106 #define SCM_GC_SET_CARD_FLAG(card, shift) \
107 (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) | (1L << (shift))))
108 #define SCM_GC_CLR_CARD_FLAG(card, shift) \
109 (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) & ~(1L << (shift))))
110
111 #define SCM_GC_CARDF_DOUBLECELL 0
112
113 #define SCM_GC_CARD_DOUBLECELLP(card) SCM_GC_GET_CARD_FLAG (card, SCM_GC_CARDF_DOUBLECELL)
114 #define SCM_GC_SET_CARD_DOUBLECELL(card) SCM_GC_SET_CARD_FLAG (card, SCM_GC_CARDF_DOUBLECELL)
115
116 /* card addressing. for efficiency, cards are *always* aligned to
117 SCM_GC_CARD_SIZE. */
118
119 #define SCM_GC_CARD_SIZE_MASK (SCM_GC_CARD_SIZE - 1)
120 #define SCM_GC_CARD_ADDR_MASK (~SCM_GC_CARD_SIZE_MASK)
121
122 #define SCM_GC_CELL_CARD(x) ((SCM_CELLPTR) ((scm_bits_t) (x) & SCM_GC_CARD_ADDR_MASK))
123 #define SCM_GC_CELL_SPAN(x) ((SCM_GC_CARD_DOUBLECELLP (SCM_GC_CELL_CARD (x))) ? 2 : 1)
124 #define SCM_GC_CELL_OFFSET(x) (((scm_bits_t) (x) & SCM_GC_CARD_SIZE_MASK) >> SCM_CELL_SIZE_SHIFT)
125 #define SCM_GC_CELL_BVEC(x) SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (x))
126 #define SCM_GC_CELL_GET_BIT(x) SCM_C_BVEC_GET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
127 #define SCM_GC_CELL_SET_BIT(x) SCM_C_BVEC_SET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
128 #define SCM_GC_CELL_CLR_BIT(x) SCM_C_BVEC_CLR (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
129
130 #define SCM_GC_CARD_UP(x) SCM_GC_CELL_CARD ((char *) (x) + SCM_GC_CARD_SIZE - 1)
131 #define SCM_GC_CARD_DOWN SCM_GC_CELL_CARD
132
133 /* low level bit banging aids */
134
135 typedef unsigned long scm_c_bvec_limb_t;
136
137 #if (SIZEOF_LONG == 8)
138 # define SCM_C_BVEC_LIMB_BITS 64
139 # define SCM_C_BVEC_OFFSET_SHIFT 6
140 # define SCM_C_BVEC_POS_MASK 63
141 # define SCM_CELL_SIZE_SHIFT 4
142 #else
143 # define SCM_C_BVEC_LIMB_BITS 32
144 # define SCM_C_BVEC_OFFSET_SHIFT 5
145 # define SCM_C_BVEC_POS_MASK 31
146 # define SCM_CELL_SIZE_SHIFT 3
147 #endif
148
149 #define SCM_C_BVEC_OFFSET(pos) (pos >> SCM_C_BVEC_OFFSET_SHIFT)
150
151 #define SCM_C_BVEC_GET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] & (1L << (pos & SCM_C_BVEC_POS_MASK)))
152 #define SCM_C_BVEC_SET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] |= (1L << (pos & SCM_C_BVEC_POS_MASK)))
153 #define SCM_C_BVEC_CLR(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] &= ~(1L << (pos & SCM_C_BVEC_POS_MASK)))
154
155 #define SCM_C_BVEC_BITS2BYTES(bits) \
156 (sizeof (scm_c_bvec_limb_t) * ((((bits) & SCM_C_BVEC_POS_MASK) ? 1L : 0L) + SCM_C_BVEC_OFFSET (bits)))
157
158 #define SCM_C_BVEC_SET_BYTES(bvec, bytes) (memset (bvec, 0xff, bytes))
159 #define SCM_C_BVEC_SET_ALL_BITS(bvec, bits) SCM_C_BVEC_SET_BYTES (bvec, SCM_C_BVEC_BITS2BYTES (bits))
160
161 #define SCM_C_BVEC_CLR_BYTES(bvec, bytes) (memset (bvec, 0, bytes))
162 #define SCM_C_BVEC_CLR_ALL_BITS(bvec, bits) SCM_C_BVEC_CLR_BYTES (bvec, SCM_C_BVEC_BITS2BYTES (bits))
163
164 /* testing and changing GC marks */
165
166 #define SCM_GCMARKP(x) SCM_GC_CELL_GET_BIT (x)
167 #define SCM_SETGCMARK(x) SCM_GC_CELL_SET_BIT (x)
168 #define SCM_CLRGCMARK(x) SCM_GC_CELL_CLR_BIT (x)
169
170 /* Low level cell data accessing macros:
171 */
172
173 #if (SCM_DEBUG_CELL_ACCESSES == 1)
174 # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
175 #else
176 # define SCM_VALIDATE_CELL(cell, expr) (expr)
177 #endif
178
179 #define SCM_CELL_WORD(x, n) \
180 SCM_VALIDATE_CELL ((x), ((const scm_bits_t *) SCM2PTR (x)) [n])
181 #define SCM_CELL_WORD_0(x) SCM_CELL_WORD (x, 0)
182 #define SCM_CELL_WORD_1(x) SCM_CELL_WORD (x, 1)
183 #define SCM_CELL_WORD_2(x) SCM_CELL_WORD (x, 2)
184 #define SCM_CELL_WORD_3(x) SCM_CELL_WORD (x, 3)
185
186 #define SCM_CELL_OBJECT(x, n) \
187 SCM_VALIDATE_CELL ((x), SCM_PACK (((const scm_bits_t *) SCM2PTR (x)) [n]))
188 #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT (x, 0)
189 #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT (x, 1)
190 #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT (x, 2)
191 #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT (x, 3)
192
193 #define SCM_SET_CELL_WORD(x, n, v) \
194 SCM_VALIDATE_CELL ((x), ((scm_bits_t *) SCM2PTR (x)) [n] = (scm_bits_t) (v))
195 #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD (x, 0, v)
196 #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD (x, 1, v)
197 #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD (x, 2, v)
198 #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD (x, 3, v)
199
200 #define SCM_SET_CELL_OBJECT(x, n, v) \
201 SCM_VALIDATE_CELL ((x), ((scm_bits_t *) SCM2PTR (x)) [n] = SCM_UNPACK (v))
202 #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT (x, 0, v)
203 #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT (x, 1, v)
204 #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT (x, 2, v)
205 #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT (x, 3, v)
206
207 #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
208 #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 (x, t)
209
210
211 /* Except for the garbage collector, no part of guile should ever run over a
212 * free cell. Thus, in debug mode the above macros report an error if they
213 * are applied to a free cell. Since the garbage collector is allowed to
214 * access free cells, it needs its own way to access cells which will not
215 * result in errors when in debug mode. */
216
217 #define SCM_GC_CELL_TYPE(x) \
218 (((const scm_bits_t *) SCM2PTR (x)) [0])
219
220
221 #define SCM_CELL_WORD_LOC(x, n) ((scm_bits_t *) & SCM_CELL_WORD (x, n))
222 #define SCM_CARLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 0))
223 #define SCM_CDRLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 1))
224
225
226 /* SCM_PTR_LT and friends define how to compare two SCM_CELLPTRs (which may
227 * point to cells in different heap segments).
228 */
229 #define SCM_PTR_LT(x, y) ((x) < (y))
230 #define SCM_PTR_GT(x, y) (SCM_PTR_LT (y, x))
231 #define SCM_PTR_LE(x, y) (!SCM_PTR_GT (x, y))
232 #define SCM_PTR_GE(x, y) (!SCM_PTR_LT (x, y))
233
234
235 /* Freelists consist of linked cells where the type entry holds the value
236 * scm_tc_free_cell and the second entry holds a pointer to the next cell of
237 * the freelist. Due to this structure, freelist cells are not cons cells
238 * and thus may not be accessed using SCM_CAR and SCM_CDR.
239 */
240
241 #define SCM_FREE_CELL_P(x) \
242 (!SCM_IMP (x) && (* (const scm_bits_t *) SCM2PTR (x) == scm_tc_free_cell))
243 #define SCM_FREE_CELL_CDR(x) \
244 (SCM_PACK (((const scm_bits_t *) SCM2PTR (x)) [1]))
245 #define SCM_SET_FREE_CELL_CDR(x, v) \
246 (((scm_bits_t *) SCM2PTR (x)) [1] = SCM_UNPACK (v))
247
248
249 #if (SCM_DEBUG_CELL_ACCESSES == 1)
250 # define SCM_GC_SET_ALLOCATED(x) \
251 (((scm_bits_t *) SCM2PTR (x)) [0] = scm_tc16_allocated)
252 #else
253 # define SCM_GC_SET_ALLOCATED(x)
254 #endif
255
256 #ifdef GUILE_DEBUG_FREELIST
257 #define SCM_NEWCELL(_into) do { _into = scm_debug_newcell (); } while (0)
258 #define SCM_NEWCELL2(_into) do { _into = scm_debug_newcell2 (); } while (0)
259 #else
260 /* When we introduce POSIX threads support, every thread will have
261 a freelist of its own. */
262 #define SCM_NEWCELL(_into) \
263 do { \
264 if (SCM_NULLP (scm_freelist)) \
265 { \
266 _into = scm_gc_for_newcell (&scm_master_freelist, \
267 &scm_freelist); \
268 SCM_GC_SET_ALLOCATED (_into); \
269 } \
270 else \
271 { \
272 _into = scm_freelist; \
273 scm_freelist = SCM_FREE_CELL_CDR (scm_freelist); \
274 SCM_GC_SET_ALLOCATED (_into); \
275 } \
276 } while(0)
277 #define SCM_NEWCELL2(_into) \
278 do { \
279 if (SCM_NULLP (scm_freelist2)) \
280 { \
281 _into = scm_gc_for_newcell (&scm_master_freelist2, \
282 &scm_freelist2); \
283 SCM_GC_SET_ALLOCATED (_into); \
284 } \
285 else \
286 { \
287 _into = scm_freelist2; \
288 scm_freelist2 = SCM_FREE_CELL_CDR (scm_freelist2); \
289 SCM_GC_SET_ALLOCATED (_into); \
290 } \
291 } while(0)
292 #endif
293
294
295 #define SCM_MARKEDP SCM_GCMARKP
296 #define SCM_NMARKEDP(x) (!SCM_MARKEDP (x))
297
298 #if (SCM_DEBUG_CELL_ACCESSES == 1)
299 extern scm_bits_t scm_tc16_allocated;
300 extern unsigned int scm_debug_cell_accesses_p;
301 #endif
302
303 extern struct scm_heap_seg_data_t *scm_heap_table;
304 extern size_t scm_n_heap_segs;
305 extern int scm_block_gc;
306 extern int scm_gc_heap_lock;
307 extern unsigned int scm_gc_running_p;
308 \f
309
310 extern size_t scm_default_init_heap_size_1;
311 extern int scm_default_min_yield_1;
312 extern size_t scm_default_init_heap_size_2;
313 extern int scm_default_min_yield_2;
314 extern size_t scm_default_max_segment_size;
315
316 extern size_t scm_max_segment_size;
317 extern SCM_CELLPTR scm_heap_org;
318 extern SCM scm_freelist;
319 extern struct scm_freelist_t scm_master_freelist;
320 extern SCM scm_freelist2;
321 extern struct scm_freelist_t scm_master_freelist2;
322 extern scm_ubits_t scm_gc_cells_collected;
323 extern scm_ubits_t scm_gc_yield;
324 extern scm_ubits_t scm_gc_malloc_collected;
325 extern scm_ubits_t scm_gc_ports_collected;
326 extern scm_ubits_t scm_cells_allocated;
327 extern scm_ubits_t scm_mallocated;
328 extern scm_ubits_t scm_mtrigger;
329
330 extern SCM scm_after_gc_hook;
331
332 extern scm_c_hook_t scm_before_gc_c_hook;
333 extern scm_c_hook_t scm_before_mark_c_hook;
334 extern scm_c_hook_t scm_before_sweep_c_hook;
335 extern scm_c_hook_t scm_after_sweep_c_hook;
336 extern scm_c_hook_t scm_after_gc_c_hook;
337
338 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
339 extern SCM scm_map_free_list (void);
340 extern SCM scm_free_list_length (void);
341 #endif
342 #ifdef GUILE_DEBUG_FREELIST
343 extern SCM scm_debug_newcell (void);
344 extern SCM scm_debug_newcell2 (void);
345 extern SCM scm_gc_set_debug_check_freelist_x (SCM flag);
346 #endif
347
348 \f
349
350 #if (SCM_DEBUG_CELL_ACCESSES == 1)
351 extern void scm_assert_cell_valid (SCM);
352 extern SCM scm_set_debug_cell_accesses_x (SCM flag);
353 #endif
354 extern SCM scm_object_address (SCM obj);
355 extern SCM scm_unhash_name (SCM name);
356 extern SCM scm_gc_stats (void);
357 extern SCM scm_gc (void);
358 extern void scm_gc_for_alloc (struct scm_freelist_t *freelist);
359 extern SCM scm_gc_for_newcell (struct scm_freelist_t *master, SCM *freelist);
360 #if 0
361 extern void scm_alloc_cluster (struct scm_freelist_t *master);
362 #endif
363 extern void scm_igc (const char *what);
364 extern void scm_gc_mark (SCM p);
365 extern void scm_gc_mark_dependencies (SCM p);
366 extern void scm_mark_locations (SCM_STACKITEM x[], scm_ubits_t n);
367 extern int scm_cellp (SCM value);
368 extern void scm_gc_sweep (void);
369 extern void * scm_must_malloc (size_t len, const char *what);
370 extern void * scm_must_realloc (void *where,
371 size_t olen, size_t len,
372 const char *what);
373 extern void scm_done_malloc (scm_bits_t size);
374 extern void scm_done_free (scm_bits_t size);
375 extern char *scm_must_strdup (const char *str);
376 extern char *scm_must_strndup (const char *str, size_t n);
377 extern void scm_must_free (void *obj);
378 extern void scm_remember_upto_here_1 (SCM obj);
379 extern void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
380 extern void scm_remember_upto_here (SCM obj1, ...);
381 extern SCM scm_return_first (SCM elt, ...);
382 extern int scm_return_first_int (int x, ...);
383 extern SCM scm_permanent_object (SCM obj);
384 extern SCM scm_protect_object (SCM obj);
385 extern SCM scm_unprotect_object (SCM obj);
386 extern int scm_init_storage (void);
387 extern void *scm_get_stack_base (void);
388 extern void scm_init_gc (void);
389
390 \f
391
392 #if (SCM_DEBUG_DEPRECATED == 0)
393
394 #define SCM_SETAND_CAR(x, y) \
395 (SCM_SETCAR ((x), SCM_PACK (SCM_UNPACK (SCM_CAR (x)) & (y))))
396 #define SCM_SETOR_CAR(x, y)\
397 (SCM_SETCAR ((x), SCM_PACK (SCM_UNPACK (SCM_CAR (x)) | (y))))
398 #define SCM_SETAND_CDR(x, y)\
399 (SCM_SETCDR ((x), SCM_PACK (SCM_UNPACK (SCM_CDR (x)) & (y))))
400 #define SCM_SETOR_CDR(x, y)\
401 (SCM_SETCDR ((x), SCM_PACK (SCM_UNPACK (SCM_CDR (x)) | (y))))
402 #define SCM_FREEP(x) (SCM_FREE_CELL_P (x))
403 #define SCM_NFREEP(x) (!SCM_FREE_CELL_P (x))
404 #define SCM_GC8MARKP(x) SCM_GCMARKP (x)
405 #define SCM_SETGC8MARK(x) SCM_SETGCMARK (x)
406 #define SCM_CLRGC8MARK(x) SCM_CLRGCMARK (x)
407 #define SCM_GCTYP16(x) SCM_TYP16 (x)
408 #define SCM_GCCDR(x) SCM_CDR (x)
409 extern void scm_remember (SCM * ptr);
410
411 #endif /* SCM_DEBUG_DEPRECATED == 0 */
412
413 #endif /* SCM_GC_H */
414
415 /*
416 Local Variables:
417 c-file-style: "gnu"
418 End:
419 */