* validate.h (SCM_VALIDATE_SUBSTRING_SPEC_COPY): new macro.
[bpt/guile.git] / libguile / gc.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
3#ifndef GCH
4#define GCH
16d35552 5/* Copyright (C) 1995, 96, 98, 99, 2000 Free Software Foundation, Inc.
a00c95d9 6 *
0f2d19dd
JB
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.
a00c95d9 11 *
0f2d19dd
JB
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.
a00c95d9 16 *
0f2d19dd
JB
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
82892bed
JB
19 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307 USA
0f2d19dd
JB
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.
82892bed 44 * If you do not wish that, delete this exception notice. */
d3a6bc94
GB
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 */
0f2d19dd
JB
48\f
49
b4309c3c 50#include "libguile/__scm.h"
2549a709 51
9b3e180c
MD
52#include "libguile/hooks.h"
53
0f2d19dd
JB
54\f
55
2549a709
DH
56typedef struct scm_cell
57{
e828cb75
DH
58 scm_bits_t word_0;
59 scm_bits_t word_1;
2549a709
DH
60} scm_cell;
61
62
63/* SCM_CELLPTR is a pointer to a cons cell which may be compared or
64 * differenced.
65 */
56100716 66typedef scm_cell * SCM_CELLPTR;
2549a709
DH
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,
e618c9a3 74 * pointers to scm_vector elts, functions, &c are not munged.
2549a709
DH
75 */
76#ifdef _UNICOS
076d6063
MD
77# define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x) >> 3))
78# define PTR2SCM(x) (SCM_PACK (((scm_bits_t) (x)) << 3))
2549a709 79#else
076d6063
MD
80# define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x)))
81# define PTR2SCM(x) (SCM_PACK ((scm_bits_t) (x)))
2549a709
DH
82#endif /* def _UNICOS */
83
e618c9a3 84#define SCM_GC_CARD_N_HEADER_CELLS 1
f91f77e6 85#define SCM_GC_CARD_N_CELLS 256
e618c9a3
ML
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
98#define SCM_GC_GET_CARD_FLAGS(card) ((long) ((card)->word_1))
99#define SCM_GC_SET_CARD_FLAGS(card, flags) (SCM_GC_GET_CARD_FLAGS (card) = (flags))
100#define SCM_GC_CLR_CARD_FLAGS(card) (SCM_GC_GET_CARD_FLAGS (card) = 0L)
101
102#define SCM_GC_GET_CARD_FLAG(card, shift) (SCM_GC_GET_CARD_FLAGS (card) & (1L << (shift)))
103#define SCM_GC_SET_CARD_FLAG(card, shift) (SCM_GC_GET_CARD_FLAGS (card) |= (1L << (shift)))
104#define SCM_GC_CLR_CARD_FLAG(card, shift) (SCM_GC_GET_CARD_FLAGS (card) &= ~(1L << (shift)))
105
106#define SCM_GC_CARDF_DOUBLECELL 0
107
108#define SCM_GC_CARD_DOUBLECELLP(card) SCM_GC_GET_CARD_FLAG (card, SCM_GC_CARDF_DOUBLECELL)
109#define SCM_GC_SET_CARD_DOUBLECELL(card) SCM_GC_SET_CARD_FLAG (card, SCM_GC_CARDF_DOUBLECELL)
110
111/* card addressing. for efficiency, cards are *always* aligned to
112 SCM_GC_CARD_SIZE. */
113
114#define SCM_GC_CARD_SIZE_MASK (SCM_GC_CARD_SIZE - 1)
115#define SCM_GC_CARD_ADDR_MASK (~SCM_GC_CARD_SIZE_MASK)
116
117#define SCM_GC_CELL_CARD(x) ((SCM_CELLPTR) ((long) (x) & SCM_GC_CARD_ADDR_MASK))
118#define SCM_GC_CELL_SPAN(x) ((SCM_GC_CARD_DOUBLECELLP (SCM_GC_CELL_CARD (x))) ? 2 : 1)
119#define SCM_GC_CELL_OFFSET(x) (((long) (x) & SCM_GC_CARD_SIZE_MASK) >> SCM_CELL_SIZE_SHIFT)
120#define SCM_GC_CELL_BVEC(x) SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (x))
121#define SCM_GC_CELL_GET_BIT(x) SCM_C_BVEC_GET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
122#define SCM_GC_CELL_SET_BIT(x) SCM_C_BVEC_SET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
123#define SCM_GC_CELL_CLR_BIT(x) SCM_C_BVEC_CLR (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
124
125#define SCM_GC_CARD_UP(x) SCM_GC_CELL_CARD ((char *) (x) + SCM_GC_CARD_SIZE - 1)
126#define SCM_GC_CARD_DOWN SCM_GC_CELL_CARD
127
128/* low level bit banging aids */
129
130typedef unsigned long scm_c_bvec_limb_t;
131
132#if (SIZEOF_LONG == 8)
133# define SCM_C_BVEC_LIMB_BITS 64
134# define SCM_C_BVEC_OFFSET_SHIFT 6
135# define SCM_C_BVEC_POS_MASK 63
136# define SCM_CELL_SIZE_SHIFT 4
137#else
138# define SCM_C_BVEC_LIMB_BITS 32
139# define SCM_C_BVEC_OFFSET_SHIFT 5
140# define SCM_C_BVEC_POS_MASK 31
141# define SCM_CELL_SIZE_SHIFT 3
142#endif
143
144#define SCM_C_BVEC_OFFSET(pos) (pos >> SCM_C_BVEC_OFFSET_SHIFT)
145
146#define SCM_C_BVEC_GET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] & (1L << (pos & SCM_C_BVEC_POS_MASK)))
147#define SCM_C_BVEC_SET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] |= (1L << (pos & SCM_C_BVEC_POS_MASK)))
148#define SCM_C_BVEC_CLR(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] &= ~(1L << (pos & SCM_C_BVEC_POS_MASK)))
149
150#define SCM_C_BVEC_BITS2BYTES(bits) \
151 (sizeof (scm_c_bvec_limb_t) * ((((bits) & SCM_C_BVEC_POS_MASK) ? 1L : 0L) + SCM_C_BVEC_OFFSET (bits)))
152
153#define SCM_C_BVEC_SET_BYTES(bvec, bytes) (memset (bvec, 0xff, bytes))
154#define SCM_C_BVEC_SET_ALL_BITS(bvec, bits) SCM_C_BVEC_SET_BYTES (bvec, SCM_C_BVEC_BITS2BYTES (bits))
155
156#define SCM_C_BVEC_CLR_BYTES(bvec, bytes) (memset (bvec, 0, bytes))
157#define SCM_C_BVEC_CLR_ALL_BITS(bvec, bits) SCM_C_BVEC_CLR_BYTES (bvec, SCM_C_BVEC_BITS2BYTES (bits))
158
159/* testing and changing GC marks */
160
161#define SCM_GCMARKP(x) SCM_GC_CELL_GET_BIT (x)
162#define SCM_SETGCMARK(x) SCM_GC_CELL_SET_BIT (x)
163#define SCM_CLRGCMARK(x) SCM_GC_CELL_CLR_BIT (x)
164
2549a709
DH
165/* Low level cell data accessing macros:
166 */
167
406c7d90
DH
168#if (SCM_DEBUG_CELL_ACCESSES == 1)
169# define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
708cb87c 170#else
406c7d90 171# define SCM_VALIDATE_CELL(cell, expr) expr
708cb87c 172#endif
46d53380 173
f706a58b
DH
174#define SCM_CELL_WORD(x, n) \
175 SCM_VALIDATE_CELL ((x), ((const scm_bits_t *) SCM2PTR (x)) [n])
2549a709
DH
176#define SCM_CELL_WORD_0(x) SCM_CELL_WORD (x, 0)
177#define SCM_CELL_WORD_1(x) SCM_CELL_WORD (x, 1)
178#define SCM_CELL_WORD_2(x) SCM_CELL_WORD (x, 2)
179#define SCM_CELL_WORD_3(x) SCM_CELL_WORD (x, 3)
180
f706a58b
DH
181#define SCM_CELL_OBJECT(x, n) \
182 SCM_VALIDATE_CELL ((x), SCM_PACK (((const scm_bits_t *) SCM2PTR (x)) [n]))
2549a709
DH
183#define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT (x, 0)
184#define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT (x, 1)
185#define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT (x, 2)
186#define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT (x, 3)
187
f706a58b
DH
188#define SCM_SET_CELL_WORD(x, n, v) \
189 SCM_VALIDATE_CELL ((x), ((scm_bits_t *) SCM2PTR (x)) [n] = (scm_bits_t) (v))
2549a709
DH
190#define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD (x, 0, v)
191#define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD (x, 1, v)
192#define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD (x, 2, v)
193#define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD (x, 3, v)
194
f706a58b
DH
195#define SCM_SET_CELL_OBJECT(x, n, v) \
196 SCM_VALIDATE_CELL ((x), ((scm_bits_t *) SCM2PTR (x)) [n] = SCM_UNPACK (v))
2549a709
DH
197#define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT (x, 0, v)
198#define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT (x, 1, v)
199#define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT (x, 2, v)
200#define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT (x, 3, v)
201
202#define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
203#define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 (x, t)
204
e828cb75
DH
205#define SCM_SETAND_CAR(x, y) \
206 (SCM_SETCAR ((x), SCM_PACK (SCM_UNPACK (SCM_CAR (x)) & (y))))
2549a709 207#define SCM_SETAND_CDR(x, y)\
e828cb75 208 (SCM_SETCDR ((x), SCM_PACK (SCM_UNPACK (SCM_CDR (x)) & (y))))
2549a709 209#define SCM_SETOR_CAR(x, y)\
e828cb75 210 (SCM_SETCAR ((x), SCM_PACK (SCM_UNPACK (SCM_CAR (x)) | (y))))
2549a709 211#define SCM_SETOR_CDR(x, y)\
e828cb75 212 (SCM_SETCDR ((x), SCM_PACK (SCM_UNPACK (SCM_CDR (x)) | (y))))
2549a709 213
f706a58b
DH
214#define SCM_CELL_WORD_LOC(x, n) ((scm_bits_t *) & SCM_CELL_WORD (x, n))
215#define SCM_CARLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 0))
216#define SCM_CDRLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 1))
2549a709
DH
217
218
6ba93e5e
DH
219/* SCM_PTR_LT and friends define how to compare two SCM_CELLPTRs (which may
220 * point to cells in different heap segments).
2549a709 221 */
6ba93e5e
DH
222#define SCM_PTR_LT(x, y) ((x) < (y))
223#define SCM_PTR_GT(x, y) (SCM_PTR_LT (y, x))
2549a709
DH
224#define SCM_PTR_LE(x, y) (!SCM_PTR_GT (x, y))
225#define SCM_PTR_GE(x, y) (!SCM_PTR_LT (x, y))
226
227
2d67e390
DH
228/* Freelists consist of linked cells where the type entry holds the value
229 * scm_tc_free_cell and the second entry holds a pointer to the next cell of
230 * the freelist. Due to this structure, freelist cells are not cons cells
231 * and thus may not be accessed using SCM_CAR and SCM_CDR.
232 */
233
3f5d82cd
DH
234#define SCM_FREE_CELL_P(x) \
235 (!SCM_IMP (x) && (* (const scm_bits_t *) SCM2PTR (x) == scm_tc_free_cell))
236#define SCM_FREE_CELL_CDR(x) \
78a3503e 237 (SCM_PACK (((const scm_bits_t *) SCM2PTR (x)) [1]))
3f5d82cd
DH
238#define SCM_SET_FREE_CELL_TYPE(x, v) \
239 (((scm_bits_t *) SCM2PTR (x)) [0] = (v))
240#define SCM_SET_FREE_CELL_CDR(x, v) \
78a3503e 241 (((scm_bits_t *) SCM2PTR (x)) [1] = SCM_UNPACK (v))
3f5d82cd 242
2549a709
DH
243#ifdef GUILE_DEBUG_FREELIST
244#define SCM_NEWCELL(_into) do { _into = scm_debug_newcell (); } while (0)
245#define SCM_NEWCELL2(_into) do { _into = scm_debug_newcell2 (); } while (0)
246#else
2549a709 247/* When we introduce POSIX threads support, every thread will have
3c8018e6 248 a freelist of its own. */
2549a709
DH
249#define SCM_NEWCELL(_into) \
250 do { \
251 if (SCM_IMP (scm_freelist)) \
252 _into = scm_gc_for_newcell (&scm_master_freelist, \
253 &scm_freelist); \
254 else \
255 { \
256 _into = scm_freelist; \
3f5d82cd 257 scm_freelist = SCM_FREE_CELL_CDR (scm_freelist); \
2549a709
DH
258 } \
259 } while(0)
260#define SCM_NEWCELL2(_into) \
261 do { \
262 if (SCM_IMP (scm_freelist2)) \
263 _into = scm_gc_for_newcell (&scm_master_freelist2, \
264 &scm_freelist2); \
265 else \
266 { \
267 _into = scm_freelist2; \
3f5d82cd 268 scm_freelist2 = SCM_FREE_CELL_CDR (scm_freelist2); \
2549a709
DH
269 } \
270 } while(0)
2549a709
DH
271#endif
272
273
e618c9a3 274#define SCM_MARKEDP SCM_GCMARKP
2d67e390 275#define SCM_NMARKEDP(x) (!SCM_MARKEDP (x))
79e3f9e2 276
a00c95d9 277extern struct scm_heap_seg_data_t *scm_heap_table;
0f2d19dd 278extern int scm_n_heap_segs;
0f2d19dd
JB
279extern int scm_block_gc;
280extern int scm_gc_heap_lock;
406c7d90 281extern unsigned int scm_gc_running_p;
0f2d19dd
JB
282\f
283
aeacfc8f
MD
284extern int scm_default_init_heap_size_1;
285extern int scm_default_min_yield_1;
286extern int scm_default_init_heap_size_2;
287extern int scm_default_min_yield_2;
288extern int scm_default_max_segment_size;
289
4c48ba06 290extern scm_sizet scm_max_segment_size;
0f2d19dd 291extern SCM_CELLPTR scm_heap_org;
4a4c9785 292extern SCM scm_freelist;
a00c95d9 293extern struct scm_freelist_t scm_master_freelist;
4a4c9785 294extern SCM scm_freelist2;
a00c95d9 295extern struct scm_freelist_t scm_master_freelist2;
0f2d19dd 296extern unsigned long scm_gc_cells_collected;
8b0d194f 297extern unsigned long scm_gc_yield;
0f2d19dd
JB
298extern unsigned long scm_gc_malloc_collected;
299extern unsigned long scm_gc_ports_collected;
300extern unsigned long scm_cells_allocated;
a5c314c8 301extern long scm_mallocated;
15e9d186 302extern unsigned long scm_mtrigger;
0f2d19dd 303
9b3e180c
MD
304extern SCM scm_after_gc_hook;
305
306extern scm_c_hook_t scm_before_gc_c_hook;
307extern scm_c_hook_t scm_before_mark_c_hook;
308extern scm_c_hook_t scm_before_sweep_c_hook;
309extern scm_c_hook_t scm_after_sweep_c_hook;
310extern scm_c_hook_t scm_after_gc_c_hook;
311
406c7d90
DH
312#if (SCM_DEBUG_CELL_ACCESSES == 1)
313extern void scm_assert_cell_valid (SCM);
314extern unsigned int scm_debug_cell_accesses_p;
315extern SCM scm_set_debug_cell_accesses_x (SCM flag);
316#endif
317
bb2c57fa 318#if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
25748c78 319extern SCM scm_map_free_list (void);
5384bc5b 320extern SCM scm_free_list_length (void);
bb2c57fa
MD
321#endif
322#ifdef GUILE_DEBUG_FREELIST
f2333166 323extern SCM scm_debug_newcell (void);
16d35552 324extern SCM scm_debug_newcell2 (void);
25748c78 325extern SCM scm_gc_set_debug_check_freelist_x (SCM flag);
88256b2e
JB
326#endif
327
0f2d19dd 328\f
0f2d19dd 329
8163d1e4 330extern SCM scm_object_address (SCM obj);
fe1a46f0
JB
331extern SCM scm_unhash_name (SCM name);
332extern SCM scm_gc_stats (void);
fe1a46f0 333extern SCM scm_gc (void);
a00c95d9 334extern void scm_gc_for_alloc (struct scm_freelist_t *freelist);
a00c95d9 335extern SCM scm_gc_for_newcell (struct scm_freelist_t *master, SCM *freelist);
4c48ba06 336#if 0
a00c95d9 337extern void scm_alloc_cluster (struct scm_freelist_t *master);
4c48ba06 338#endif
3eeba8d4 339extern void scm_igc (const char *what);
fe1a46f0 340extern void scm_gc_mark (SCM p);
56495472 341extern void scm_gc_mark_dependencies (SCM p);
fe1a46f0
JB
342extern void scm_mark_locations (SCM_STACKITEM x[], scm_sizet n);
343extern int scm_cellp (SCM value);
344extern void scm_gc_sweep (void);
07806695
JB
345extern void * scm_must_malloc (scm_sizet len, const char *what);
346extern void * scm_must_realloc (void *where,
fe1a46f0 347 scm_sizet olen, scm_sizet len,
3eeba8d4 348 const char *what);
fe1a46f0 349extern void scm_done_malloc (long size);
9d47a1e6 350extern void scm_done_free (long size);
07806695 351extern void scm_must_free (void *obj);
5d2b97cd
DH
352extern void scm_remember_upto_here_1 (SCM obj);
353extern void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
354extern void scm_remember_upto_here (SCM obj1, ...);
fe1a46f0 355extern SCM scm_return_first (SCM elt, ...);
41b0806d 356extern int scm_return_first_int (int x, ...);
fe1a46f0
JB
357extern SCM scm_permanent_object (SCM obj);
358extern SCM scm_protect_object (SCM obj);
359extern SCM scm_unprotect_object (SCM obj);
85db4a2c 360extern int scm_init_storage (void);
92082045 361extern void *scm_get_stack_base (void);
fe1a46f0 362extern void scm_init_gc (void);
d1ca2c64
DH
363
364\f
365
366#if (SCM_DEBUG_DEPRECATED == 0)
367
368#define SCM_FREEP(x) (SCM_FREE_CELL_P (x))
369#define SCM_NFREEP(x) (!SCM_FREE_CELL_P (x))
fd336365
DH
370#define SCM_GC8MARKP(x) SCM_GCMARKP (x)
371#define SCM_SETGC8MARK(x) SCM_SETGCMARK (x)
372#define SCM_CLRGC8MARK(x) SCM_CLRGCMARK (x)
373#define SCM_GCTYP16(x) SCM_TYP16 (x)
374#define SCM_GCCDR(x) SCM_CDR (x)
5d2b97cd 375extern void scm_remember (SCM * ptr);
d1ca2c64
DH
376
377#endif /* SCM_DEBUG_DEPRECATED == 0 */
378
0f2d19dd 379#endif /* GCH */
89e00824
ML
380
381/*
382 Local Variables:
383 c-file-style: "gnu"
384 End:
385*/