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