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