* Extend checks performed with SCM_DEBUG_CELL_ACCESSES=1 and make them
[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
85 /* Low level cell data accessing macros:
86 */
87
88 #if (SCM_DEBUG_CELL_ACCESSES == 1)
89 # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
90 #else
91 # define SCM_VALIDATE_CELL(cell, expr) expr
92 #endif
93
94 #define SCM_CELL_WORD(x, n) \
95 SCM_VALIDATE_CELL ((x), ((const scm_bits_t *) SCM2PTR (x)) [n])
96 #define SCM_CELL_WORD_0(x) SCM_CELL_WORD (x, 0)
97 #define SCM_CELL_WORD_1(x) SCM_CELL_WORD (x, 1)
98 #define SCM_CELL_WORD_2(x) SCM_CELL_WORD (x, 2)
99 #define SCM_CELL_WORD_3(x) SCM_CELL_WORD (x, 3)
100
101 #define SCM_CELL_OBJECT(x, n) \
102 SCM_VALIDATE_CELL ((x), SCM_PACK (((const scm_bits_t *) SCM2PTR (x)) [n]))
103 #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT (x, 0)
104 #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT (x, 1)
105 #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT (x, 2)
106 #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT (x, 3)
107
108 #define SCM_SET_CELL_WORD(x, n, v) \
109 SCM_VALIDATE_CELL ((x), ((scm_bits_t *) SCM2PTR (x)) [n] = (scm_bits_t) (v))
110 #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD (x, 0, v)
111 #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD (x, 1, v)
112 #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD (x, 2, v)
113 #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD (x, 3, v)
114
115 #define SCM_SET_CELL_OBJECT(x, n, v) \
116 SCM_VALIDATE_CELL ((x), ((scm_bits_t *) SCM2PTR (x)) [n] = SCM_UNPACK (v))
117 #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT (x, 0, v)
118 #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT (x, 1, v)
119 #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT (x, 2, v)
120 #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT (x, 3, v)
121
122 #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
123 #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 (x, t)
124
125 #define SCM_SETAND_CAR(x, y) \
126 (SCM_SETCAR ((x), SCM_PACK (SCM_UNPACK (SCM_CAR (x)) & (y))))
127 #define SCM_SETAND_CDR(x, y)\
128 (SCM_SETCDR ((x), SCM_PACK (SCM_UNPACK (SCM_CDR (x)) & (y))))
129 #define SCM_SETOR_CAR(x, y)\
130 (SCM_SETCAR ((x), SCM_PACK (SCM_UNPACK (SCM_CAR (x)) | (y))))
131 #define SCM_SETOR_CDR(x, y)\
132 (SCM_SETCDR ((x), SCM_PACK (SCM_UNPACK (SCM_CDR (x)) | (y))))
133
134 #define SCM_CELL_WORD_LOC(x, n) ((scm_bits_t *) & SCM_CELL_WORD (x, n))
135 #define SCM_CARLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 0))
136 #define SCM_CDRLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 1))
137
138
139 /* SCM_PTR_LT and friends define how to compare two SCM_CELLPTRs (which may
140 * point to cells in different heap segments).
141 */
142 #define SCM_PTR_LT(x, y) ((x) < (y))
143 #define SCM_PTR_GT(x, y) (SCM_PTR_LT (y, x))
144 #define SCM_PTR_LE(x, y) (!SCM_PTR_GT (x, y))
145 #define SCM_PTR_GE(x, y) (!SCM_PTR_LT (x, y))
146
147
148 /* Freelists consist of linked cells where the type entry holds the value
149 * scm_tc_free_cell and the second entry holds a pointer to the next cell of
150 * the freelist. Due to this structure, freelist cells are not cons cells
151 * and thus may not be accessed using SCM_CAR and SCM_CDR.
152 */
153
154 #define SCM_FREE_CELL_P(x) \
155 (!SCM_IMP (x) && (* (const scm_bits_t *) SCM2PTR (x) == scm_tc_free_cell))
156 #define SCM_FREE_CELL_CDR(x) \
157 (((const scm_bits_t *) SCM2PTR (x)) [1])
158 #define SCM_SET_FREE_CELL_TYPE(x, v) \
159 (((scm_bits_t *) SCM2PTR (x)) [0] = (v))
160 #define SCM_SET_FREE_CELL_CDR(x, v) \
161 (((scm_bits_t *) SCM2PTR (x)) [1] = (v))
162
163 /* the allocated thing: The car of new cells is set to
164 scm_tc16_allocated to avoid the fragile state of newcells wrt the
165 gc. If it stays as a freecell, any allocation afterwards could
166 cause the cell to go back on the freelist, which will bite you
167 sometime afterwards. */
168
169 #ifdef GUILE_DEBUG_FREELIST
170 #define SCM_NEWCELL(_into) do { _into = scm_debug_newcell (); } while (0)
171 #define SCM_NEWCELL2(_into) do { _into = scm_debug_newcell2 (); } while (0)
172 #else
173 /* When we introduce POSIX threads support, every thread will have
174 a freelist of its own. Then it won't any longer be necessary to
175 initialize cells with scm_tc16_allocated. */
176 #define SCM_NEWCELL(_into) \
177 do { \
178 if (SCM_IMP (scm_freelist)) \
179 _into = scm_gc_for_newcell (&scm_master_freelist, \
180 &scm_freelist); \
181 else \
182 { \
183 _into = scm_freelist; \
184 scm_freelist = SCM_FREE_CELL_CDR (scm_freelist); \
185 SCM_SET_FREE_CELL_TYPE (_into, scm_tc16_allocated); \
186 } \
187 } while(0)
188 #define SCM_NEWCELL2(_into) \
189 do { \
190 if (SCM_IMP (scm_freelist2)) \
191 _into = scm_gc_for_newcell (&scm_master_freelist2, \
192 &scm_freelist2); \
193 else \
194 { \
195 _into = scm_freelist2; \
196 scm_freelist2 = SCM_FREE_CELL_CDR (scm_freelist2); \
197 SCM_SET_FREE_CELL_TYPE (_into, scm_tc16_allocated); \
198 } \
199 } while(0)
200 #endif
201
202
203 #define SCM_FREEP(x) (SCM_FREE_CELL_P (x))
204 #define SCM_NFREEP(x) (!SCM_FREEP (x))
205
206 /* 1. This shouldn't be used on immediates.
207 2. It thinks that subrs are always unmarked (harmless). */
208 #define SCM_MARKEDP(x) ((SCM_CELL_TYPE (x) & 5) == 5 \
209 ? SCM_GC8MARKP (x) \
210 : SCM_GCMARKP (x))
211 #define SCM_NMARKEDP(x) (!SCM_MARKEDP (x))
212
213 extern struct scm_heap_seg_data_t *scm_heap_table;
214 extern int scm_n_heap_segs;
215 extern int scm_block_gc;
216 extern int scm_gc_heap_lock;
217 extern unsigned int scm_gc_running_p;
218 \f
219
220 extern int scm_default_init_heap_size_1;
221 extern int scm_default_min_yield_1;
222 extern int scm_default_init_heap_size_2;
223 extern int scm_default_min_yield_2;
224 extern int scm_default_max_segment_size;
225
226 extern scm_sizet scm_max_segment_size;
227 extern SCM_CELLPTR scm_heap_org;
228 extern SCM scm_freelist;
229 extern struct scm_freelist_t scm_master_freelist;
230 extern SCM scm_freelist2;
231 extern struct scm_freelist_t scm_master_freelist2;
232 extern unsigned long scm_gc_cells_collected;
233 extern unsigned long scm_gc_yield;
234 extern unsigned long scm_gc_malloc_collected;
235 extern unsigned long scm_gc_ports_collected;
236 extern unsigned long scm_cells_allocated;
237 extern long scm_mallocated;
238 extern unsigned long scm_mtrigger;
239
240 extern SCM scm_after_gc_hook;
241
242 extern scm_c_hook_t scm_before_gc_c_hook;
243 extern scm_c_hook_t scm_before_mark_c_hook;
244 extern scm_c_hook_t scm_before_sweep_c_hook;
245 extern scm_c_hook_t scm_after_sweep_c_hook;
246 extern scm_c_hook_t scm_after_gc_c_hook;
247
248 #if (SCM_DEBUG_CELL_ACCESSES == 1)
249 extern void scm_assert_cell_valid (SCM);
250 extern unsigned int scm_debug_cell_accesses_p;
251 extern SCM scm_set_debug_cell_accesses_x (SCM flag);
252 #endif
253
254 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
255 extern SCM scm_map_free_list (void);
256 extern SCM scm_free_list_length (void);
257 #endif
258 #ifdef GUILE_DEBUG_FREELIST
259 extern SCM scm_debug_newcell (void);
260 extern SCM scm_debug_newcell2 (void);
261 extern SCM scm_gc_set_debug_check_freelist_x (SCM flag);
262 #endif
263
264 \f
265
266 extern SCM scm_object_address (SCM obj);
267 extern SCM scm_unhash_name (SCM name);
268 extern SCM scm_gc_stats (void);
269 extern void scm_gc_start (const char *what);
270 extern void scm_gc_end (void);
271 extern SCM scm_gc (void);
272 extern void scm_gc_for_alloc (struct scm_freelist_t *freelist);
273 extern SCM scm_gc_for_newcell (struct scm_freelist_t *master, SCM *freelist);
274 #if 0
275 extern void scm_alloc_cluster (struct scm_freelist_t *master);
276 #endif
277 extern void scm_igc (const char *what);
278 extern void scm_gc_mark (SCM p);
279 extern void scm_mark_locations (SCM_STACKITEM x[], scm_sizet n);
280 extern int scm_cellp (SCM value);
281 extern void scm_gc_sweep (void);
282 extern void * scm_must_malloc (scm_sizet len, const char *what);
283 extern void * scm_must_realloc (void *where,
284 scm_sizet olen, scm_sizet len,
285 const char *what);
286 extern void scm_done_malloc (long size);
287 extern void scm_must_free (void *obj);
288 extern void scm_remember (SCM * ptr);
289 extern SCM scm_return_first (SCM elt, ...);
290 extern int scm_return_first_int (int x, ...);
291 extern SCM scm_permanent_object (SCM obj);
292 extern SCM scm_protect_object (SCM obj);
293 extern SCM scm_unprotect_object (SCM obj);
294 extern int scm_init_storage (scm_sizet init_heap_size, int trig,
295 scm_sizet init_heap2_size, int trig2,
296 scm_sizet max_segment_size);
297 extern void scm_init_gc (void);
298 #endif /* GCH */
299
300 /*
301 Local Variables:
302 c-file-style: "gnu"
303 End:
304 */