Use SCM_{SET_}?CELL_TYPE when accessing the type entry of a cell.
[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 \f
53
54 typedef struct scm_cell
55 {
56 SCM car;
57 SCM cdr;
58 } scm_cell;
59
60
61 /* SCM_CELLPTR is a pointer to a cons cell which may be compared or
62 * differenced.
63 */
64 #if !defined (__TURBOC__) || defined (__TOS__) || defined (PROT386)
65 typedef scm_cell * SCM_CELLPTR;
66 #else
67 typedef scm_cell huge * SCM_CELLPTR;
68 #endif
69
70
71 /* Cray machines have pointers that are incremented once for each word,
72 * rather than each byte, the 3 most significant bits encode the byte
73 * within the word. The following macros deal with this by storing the
74 * native Cray pointers like the ones that looks like scm expects. This
75 * is done for any pointers that might appear in the car of a scm_cell,
76 * pointers to scm_vector elts, functions, &c are not munged.
77 */
78 #ifdef _UNICOS
79 #define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x) >> 3))
80 #define PTR2SCM(x) (SCM_PACK (((scm_bits_t) (x)) << 3))
81 #else
82 #define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x)))
83 #define PTR2SCM(x) (SCM_PACK ((scm_bits_t) (x)))
84 #endif /* def _UNICOS */
85
86
87 /* Low level cell data accessing macros:
88 */
89
90 #define SCM_CELL_WORD(x, n) (SCM_UNPACK (((SCM *) SCM2PTR (x))[n]))
91 #define SCM_CELL_WORD_0(x) SCM_CELL_WORD (x, 0)
92 #define SCM_CELL_WORD_1(x) SCM_CELL_WORD (x, 1)
93 #define SCM_CELL_WORD_2(x) SCM_CELL_WORD (x, 2)
94 #define SCM_CELL_WORD_3(x) SCM_CELL_WORD (x, 3)
95
96 #define SCM_CELL_OBJECT(x, n) (((SCM *) SCM2PTR (x))[n])
97 #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT (x, 0)
98 #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT (x, 1)
99 #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT (x, 2)
100 #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT (x, 3)
101
102 #define SCM_SET_CELL_WORD(x, n, v) ((((SCM *) SCM2PTR (x))[n]) = SCM_PACK (v))
103 #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD (x, 0, v)
104 #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD (x, 1, v)
105 #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD (x, 2, v)
106 #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD (x, 3, v)
107
108 #define SCM_SET_CELL_OBJECT(x, n, v) ((((SCM *) SCM2PTR (x))[n]) = v)
109 #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT (x, 0, v)
110 #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT (x, 1, v)
111 #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT (x, 2, v)
112 #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT (x, 3, v)
113
114 #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
115 #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 (x, t)
116
117 #define SCM_SETAND_CAR(x, y)\
118 (SCM_CAR (x) = SCM_PACK (SCM_UNPACK (SCM_CAR (x)) & (y)))
119 #define SCM_SETAND_CDR(x, y)\
120 (SCM_CDR (x) = SCM_PACK (SCM_UNPACK (SCM_CDR (x)) & (y)))
121 #define SCM_SETOR_CAR(x, y)\
122 (SCM_CAR (x) = SCM_PACK (SCM_UNPACK (SCM_CAR (x)) | (y)))
123 #define SCM_SETOR_CDR(x, y)\
124 (SCM_CDR (x) = SCM_PACK (SCM_UNPACK (SCM_CDR (x)) | (y)))
125
126 #define SCM_CELL_WORD_LOC(x, n) (&SCM_CELL_WORD (x, n))
127 #define SCM_CARLOC(x) (&SCM_CAR (x))
128 #define SCM_CDRLOC(x) (&SCM_CDR (x))
129
130
131 /* SCM_PTR_LT defines how to compare two SCM_CELLPTRs (which may not be in the
132 * same scm_array).
133 */
134
135 #if !defined(__TURBOC__) || defined(__TOS__)
136 #ifdef nosve
137 #define SCM_PTR_MASK 0xffffffffffff
138 #define SCM_PTR_LT(x, y)\
139 (((int) (x) & SCM_PTR_MASK) < ((int) (y) & SCM_PTR_MASK))
140 #else
141 #define SCM_PTR_LT(x, y) ((x) < (y))
142 #endif /* def nosve */
143 #else /* defined(__TURBOC__) && !defined(__TOS__) */
144 #ifdef PROT386
145 #define SCM_PTR_LT(x, y) (((long) (x)) < ((long) (y)))
146 #else
147 #define SCM_PTR_LT(x, y) ((x) < (y))
148 #endif /* def PROT386 */
149 #endif /* defined(__TURBOC__) && !defined(__TOS__) */
150
151 #define SCM_PTR_GT(x, y) SCM_PTR_LT (y, x)
152 #define SCM_PTR_LE(x, y) (!SCM_PTR_GT (x, y))
153 #define SCM_PTR_GE(x, y) (!SCM_PTR_LT (x, y))
154
155
156 /* Dirk:FIXME:: */
157 /* Freelists consist of linked cells where the type entry holds the value
158 * scm_tc_free_cell and the second entry holds a pointer to the next cell of
159 * the freelist. Due to this structure, freelist cells are not cons cells
160 * and thus may not be accessed using SCM_CAR and SCM_CDR.
161 */
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 #ifdef GUILE_NEW_GC_SCHEME
174 /* When we introduce POSIX threads support, every thread will have
175 a freelist of its own. Then it won't any longer be necessary to
176 initialize cells with scm_tc16_allocated. */
177 #define SCM_NEWCELL(_into) \
178 do { \
179 if (SCM_IMP (scm_freelist)) \
180 _into = scm_gc_for_newcell (&scm_master_freelist, \
181 &scm_freelist); \
182 else \
183 { \
184 _into = scm_freelist; \
185 scm_freelist = SCM_CDR (scm_freelist); \
186 SCM_SET_CELL_TYPE (_into, scm_tc16_allocated); \
187 } \
188 } while(0)
189 #define SCM_NEWCELL2(_into) \
190 do { \
191 if (SCM_IMP (scm_freelist2)) \
192 _into = scm_gc_for_newcell (&scm_master_freelist2, \
193 &scm_freelist2); \
194 else \
195 { \
196 _into = scm_freelist2; \
197 scm_freelist2 = SCM_CDR (scm_freelist2); \
198 SCM_SET_CELL_TYPE (_into, scm_tc16_allocated); \
199 } \
200 } while(0)
201 #else /* GUILE_NEW_GC_SCHEME */
202 #define SCM_NEWCELL(_into) \
203 do { \
204 if (SCM_IMP (scm_freelist.cells)) \
205 _into = scm_gc_for_newcell (&scm_freelist); \
206 else \
207 { \
208 _into = scm_freelist.cells; \
209 scm_freelist.cells = SCM_CDR (scm_freelist.cells); \
210 SCM_SET_CELL_TYPE (_into, scm_tc16_allocated); \
211 ++scm_cells_allocated; \
212 } \
213 } while(0)
214 #define SCM_NEWCELL2(_into) \
215 do { \
216 if (SCM_IMP (scm_freelist2.cells)) \
217 _into = scm_gc_for_newcell (&scm_freelist2); \
218 else \
219 { \
220 _into = scm_freelist2.cells; \
221 scm_freelist2.cells = SCM_CDR (scm_freelist2.cells); \
222 SCM_SET_CELL_TYPE (_into, scm_tc16_allocated); \
223 scm_cells_allocated += 2; \
224 } \
225 } while(0)
226 #endif /* GUILE_NEW_GC_SCHEME */
227 #endif
228
229
230 #define SCM_FREEP(x) (SCM_NIMP (x) && (SCM_CELL_TYPE (x) == scm_tc_free_cell))
231 #define SCM_NFREEP(x) (!SCM_FREEP (x))
232
233 /* 1. This shouldn't be used on immediates.
234 2. It thinks that subrs are always unmarked (harmless). */
235 #define SCM_MARKEDP(x) ((SCM_CELL_TYPE (x) & 5) == 5 \
236 ? SCM_GC8MARKP (x) \
237 : SCM_GCMARKP (x))
238 #define SCM_NMARKEDP(x) (!SCM_MARKEDP (x))
239
240 extern struct scm_heap_seg_data_t *scm_heap_table;
241 extern int scm_n_heap_segs;
242 extern int scm_take_stdin;
243 extern int scm_block_gc;
244 extern int scm_gc_heap_lock;
245 \f
246
247 extern scm_sizet scm_max_segment_size;
248 extern SCM_CELLPTR scm_heap_org;
249 #ifdef GUILE_NEW_GC_SCHEME
250 extern SCM scm_freelist;
251 extern struct scm_freelist_t scm_master_freelist;
252 extern SCM scm_freelist2;
253 extern struct scm_freelist_t scm_master_freelist2;
254 #else
255 extern struct scm_freelist_t scm_freelist;
256 extern struct scm_freelist_t scm_freelist2;
257 #endif
258 extern unsigned long scm_gc_cells_collected;
259 #ifdef GUILE_NEW_GC_SCHEME
260 extern unsigned long scm_gc_yield;
261 #endif
262 extern unsigned long scm_gc_malloc_collected;
263 extern unsigned long scm_gc_ports_collected;
264 extern unsigned long scm_cells_allocated;
265 extern long scm_mallocated;
266 extern unsigned long scm_mtrigger;
267
268 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
269 extern SCM scm_map_free_list (void);
270 extern SCM scm_free_list_length (void);
271 #endif
272 #ifdef GUILE_DEBUG_FREELIST
273 extern SCM scm_debug_newcell (void);
274 extern SCM scm_debug_newcell2 (void);
275 extern SCM scm_gc_set_debug_check_freelist_x (SCM flag);
276 #endif
277
278 \f
279
280 extern SCM scm_object_address (SCM obj);
281 extern SCM scm_unhash_name (SCM name);
282 extern SCM scm_gc_stats (void);
283 extern void scm_gc_start (const char *what);
284 extern void scm_gc_end (void);
285 extern SCM scm_gc (void);
286 extern void scm_gc_for_alloc (struct scm_freelist_t *freelist);
287 #ifdef GUILE_NEW_GC_SCHEME
288 extern SCM scm_gc_for_newcell (struct scm_freelist_t *master, SCM *freelist);
289 #if 0
290 extern void scm_alloc_cluster (struct scm_freelist_t *master);
291 #endif
292 #else
293 extern SCM scm_gc_for_newcell (struct scm_freelist_t *freelist);
294 #endif
295 extern void scm_igc (const char *what);
296 extern void scm_gc_mark (SCM p);
297 extern void scm_mark_locations (SCM_STACKITEM x[], scm_sizet n);
298 extern int scm_cellp (SCM value);
299 extern void scm_gc_sweep (void);
300 extern void * scm_must_malloc (scm_sizet len, const char *what);
301 extern void * scm_must_realloc (void *where,
302 scm_sizet olen, scm_sizet len,
303 const char *what);
304 extern void scm_done_malloc (long size);
305 extern void scm_must_free (void *obj);
306 extern void scm_remember (SCM * ptr);
307 extern SCM scm_return_first (SCM elt, ...);
308 extern int scm_return_first_int (int x, ...);
309 extern SCM scm_permanent_object (SCM obj);
310 extern SCM scm_protect_object (SCM obj);
311 extern SCM scm_unprotect_object (SCM obj);
312 #ifdef GUILE_NEW_GC_SCHEME
313 extern int scm_init_storage (scm_sizet init_heap_size, int trig,
314 scm_sizet init_heap2_size, int trig2,
315 scm_sizet max_segment_size);
316 #else
317 extern int scm_init_storage (scm_sizet init_heap_size,
318 scm_sizet init_heap2_size);
319 #endif
320 extern void scm_init_gc (void);
321 #endif /* GCH */
322
323 /*
324 Local Variables:
325 c-file-style: "gnu"
326 End:
327 */