Merge commit 'f30e1bdf97ae8b2b2918da585f887a4d3a23a347' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / gc.h
1 /* classes: h_files */
2
3 #ifndef SCM_GC_H
4 #define SCM_GC_H
5
6 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 \f
24
25 #include "libguile/__scm.h"
26
27 #include "libguile/hooks.h"
28 #include "libguile/threads.h"
29
30 \f
31 typedef struct scm_t_cell
32 {
33 SCM word_0;
34 SCM word_1;
35 } scm_t_cell;
36
37 /* Cray machines have pointers that are incremented once for each
38 * word, rather than each byte, the 3 most significant bits encode the
39 * byte within the word. The following macros deal with this by
40 * storing the native Cray pointers like the ones that looks like scm
41 * expects. This is done for any pointers that point to a cell,
42 * pointers to scm_vector elts, functions, &c are not munged.
43 */
44 #ifdef _UNICOS
45 # define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x) >> 3))
46 # define PTR2SCM(x) (SCM_PACK (((scm_t_bits) (x)) << 3))
47 #else
48 # define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x)))
49 # define PTR2SCM(x) (SCM_PACK ((scm_t_bits) (x)))
50 #endif /* def _UNICOS */
51
52
53
54 /* Low level cell data accessing macros. These macros should only be used
55 * from within code related to garbage collection issues, since they will
56 * never check the cells they are applied to - not even if guile is compiled
57 * in debug mode. In particular these macros will even work for free cells,
58 * which should never be encountered by user code. */
59
60 #define SCM_GC_CELL_OBJECT(x, n) (((SCM *)SCM2PTR (x)) [n])
61 #define SCM_GC_CELL_WORD(x, n) (SCM_UNPACK (SCM_GC_CELL_OBJECT ((x), (n))))
62
63 #define SCM_GC_SET_CELL_OBJECT(x, n, v) ((((SCM *)SCM2PTR (x)) [n]) = (v))
64 #define SCM_GC_SET_CELL_WORD(x, n, v) \
65 (SCM_GC_SET_CELL_OBJECT ((x), (n), SCM_PACK (v)))
66
67 #define SCM_GC_CELL_TYPE(x) (SCM_GC_CELL_OBJECT ((x), 0))
68
69
70 /* Except for the garbage collector, no part of guile should ever run over a
71 * free cell. Thus, if guile is compiled in debug mode the SCM_CELL_* and
72 * SCM_SET_CELL_* macros below report an error if they are applied to a free
73 * cell. Some other plausibility checks are also performed. However, if
74 * guile is not compiled in debug mode, there won't be any time penalty at all
75 * when using these macros. */
76
77 #if (SCM_DEBUG_CELL_ACCESSES == 1)
78 # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
79 #else
80 # define SCM_VALIDATE_CELL(cell, expr) (expr)
81 #endif
82
83 #define SCM_CELL_WORD(x, n) \
84 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_WORD ((x), (n)))
85 #define SCM_CELL_WORD_0(x) SCM_CELL_WORD ((x), 0)
86 #define SCM_CELL_WORD_1(x) SCM_CELL_WORD ((x), 1)
87 #define SCM_CELL_WORD_2(x) SCM_CELL_WORD ((x), 2)
88 #define SCM_CELL_WORD_3(x) SCM_CELL_WORD ((x), 3)
89
90 #define SCM_CELL_OBJECT(x, n) \
91 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_OBJECT ((x), (n)))
92 #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT ((x), 0)
93 #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT ((x), 1)
94 #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT ((x), 2)
95 #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT ((x), 3)
96
97 #define SCM_SET_CELL_WORD(x, n, v) \
98 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_WORD ((x), (n), (v)))
99 #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD ((x), 0, (v))
100 #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD ((x), 1, (v))
101 #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD ((x), 2, (v))
102 #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD ((x), 3, (v))
103
104 #define SCM_SET_CELL_OBJECT(x, n, v) \
105 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_OBJECT ((x), (n), (v)))
106 #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT ((x), 0, (v))
107 #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT ((x), 1, (v))
108 #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT ((x), 2, (v))
109 #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT ((x), 3, (v))
110
111 #define SCM_CELL_OBJECT_LOC(x, n) (SCM_VALIDATE_CELL((x), &SCM_GC_CELL_OBJECT ((x), (n))))
112 #define SCM_CARLOC(x) (SCM_CELL_OBJECT_LOC ((x), 0))
113 #define SCM_CDRLOC(x) (SCM_CELL_OBJECT_LOC ((x), 1))
114
115 #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
116 #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 ((x), (t))
117
118 /* Freelists consist of linked cells where the type entry holds the value
119 * scm_tc_free_cell and the second entry holds a pointer to the next cell of
120 * the freelist. Due to this structure, freelist cells are not cons cells
121 * and thus may not be accessed using SCM_CAR and SCM_CDR. */
122
123 #define SCM_FREE_CELL_CDR(x) \
124 (SCM_GC_CELL_OBJECT ((x), 1))
125 #define SCM_SET_FREE_CELL_CDR(x, v) \
126 (SCM_GC_SET_CELL_OBJECT ((x), 1, (v)))
127
128 #if (SCM_DEBUG_CELL_ACCESSES == 1)
129 /* Set this to != 0 if every cell that is accessed shall be checked:
130 */
131 SCM_API int scm_debug_cell_accesses_p;
132 SCM_API int scm_expensive_debug_cell_accesses_p;
133 SCM_API int scm_debug_cells_gc_interval ;
134 void scm_i_expensive_validation_check (SCM cell);
135 #endif
136
137 SCM_API scm_i_pthread_mutex_t scm_i_gc_admin_mutex;
138
139 #define scm_gc_running_p (SCM_I_CURRENT_THREAD->gc_running_p)
140 SCM_API scm_i_pthread_mutex_t scm_i_sweep_mutex;
141
142 #ifdef __ia64__
143 void *scm_ia64_register_backing_store_base (void);
144 void *scm_ia64_ar_bsp (const void *);
145 #endif
146
147 \f
148
149 #if (SCM_ENABLE_DEPRECATED == 1)
150 SCM_API size_t scm_default_init_heap_size_1;
151 SCM_API int scm_default_min_yield_1;
152 SCM_API size_t scm_default_init_heap_size_2;
153 SCM_API int scm_default_min_yield_2;
154 SCM_API size_t scm_default_max_segment_size;
155 #else
156 #define scm_default_init_heap_size_1 deprecated
157 #define scm_default_min_yield_1 deprecated
158 #define scm_default_init_heap_size_2 deprecated
159 #define scm_default_min_yield_2 deprecated
160 #define scm_default_max_segment_size deprecated
161 #endif
162
163
164 SCM_API size_t scm_max_segment_size;
165
166 #define SCM_SET_FREELIST_LOC(key,ptr) scm_i_pthread_setspecific ((key), (ptr))
167 #define SCM_FREELIST_LOC(key) ((SCM *) scm_i_pthread_getspecific (key))
168 SCM_API scm_i_pthread_key_t scm_i_freelist;
169 SCM_API scm_i_pthread_key_t scm_i_freelist2;
170 SCM_API struct scm_t_cell_type_statistics scm_i_master_freelist;
171 SCM_API struct scm_t_cell_type_statistics scm_i_master_freelist2;
172
173 SCM_API unsigned long scm_gc_malloc_collected;
174 SCM_API unsigned long scm_gc_ports_collected;
175 SCM_API unsigned long scm_cells_allocated;
176 SCM_API int scm_gc_malloc_yield_percentage;
177 SCM_API unsigned long scm_mallocated;
178 SCM_API unsigned long scm_mtrigger;
179
180
181
182 SCM_API SCM scm_after_gc_hook;
183
184 SCM_API scm_t_c_hook scm_before_gc_c_hook;
185 SCM_API scm_t_c_hook scm_before_mark_c_hook;
186 SCM_API scm_t_c_hook scm_before_sweep_c_hook;
187 SCM_API scm_t_c_hook scm_after_sweep_c_hook;
188 SCM_API scm_t_c_hook scm_after_gc_c_hook;
189
190 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
191 #if (SCM_ENABLE_DEPRECATED == 1)
192 SCM scm_map_free_list (void);
193 #else
194 #define scm_map_free_list deprecated
195 #define scm_free_list_length deprecated
196 #endif
197 #endif
198
199 #if (SCM_ENABLE_DEPRECATED == 1) && defined (GUILE_DEBUG_FREELIST)
200 SCM_API SCM scm_gc_set_debug_check_freelist_x (SCM flag);
201 #endif
202 \f
203
204 #if (SCM_DEBUG_CELL_ACCESSES == 1)
205 SCM_API void scm_assert_cell_valid (SCM);
206 #endif
207
208 SCM_API SCM scm_set_debug_cell_accesses_x (SCM flag);
209
210
211 SCM_API SCM scm_object_address (SCM obj);
212 SCM_API SCM scm_gc_enable (void);
213 SCM_API SCM scm_gc_disable (void);
214 SCM_API SCM scm_gc_stats (void);
215 SCM_API SCM scm_gc_live_object_stats (void);
216 SCM_API SCM scm_gc (void);
217 SCM_API void scm_i_gc (const char *what);
218 SCM_API void scm_gc_mark (SCM p);
219 SCM_API int scm_in_heap_p (SCM value);
220 SCM_API void scm_gc_sweep (void);
221
222 SCM_API void *scm_malloc (size_t size);
223 SCM_API void *scm_calloc (size_t size);
224 SCM_API void *scm_realloc (void *mem, size_t size);
225 SCM_API char *scm_strdup (const char *str);
226 SCM_API char *scm_strndup (const char *str, size_t n);
227 SCM_API void scm_gc_register_collectable_memory (void *mem, size_t size,
228 const char *what);
229 SCM_API void scm_gc_unregister_collectable_memory (void *mem, size_t size,
230 const char *what);
231 SCM_API void *scm_gc_malloc_pointerless (size_t size, const char *what);
232 SCM_API void *scm_gc_calloc (size_t size, const char *what);
233 SCM_API void *scm_gc_malloc (size_t size, const char *what);
234 SCM_API void *scm_gc_realloc (void *mem, size_t old_size,
235 size_t new_size, const char *what);
236 SCM_API void scm_gc_free (void *mem, size_t size, const char *what);
237 SCM_API char *scm_gc_strdup (const char *str, const char *what);
238 SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what);
239
240 SCM_API void scm_remember_upto_here_1 (SCM obj);
241 SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
242 SCM_API void scm_remember_upto_here (SCM obj1, ...);
243
244 /* In GCC we can force a reference to an SCM by making it an input to an
245 empty asm. This avoids the code size and slowdown of an actual function
246 call. Unfortunately there doesn't seem to be any way to do the varargs
247 scm_remember_upto_here like this.
248
249 __volatile__ ensures nothing will be moved across the asm, and it won't
250 be optimized away (or only if proved unreachable). Constraint "g" can be
251 used on all processors and allows any memory or general register (or
252 immediate) operand. The actual asm syntax doesn't matter, we don't want
253 to use it, just ensure the operand is still alive. See "Extended Asm" in
254 the GCC manual for more. */
255
256 #ifdef __GNUC__
257 #define scm_remember_upto_here_1(x) \
258 do { \
259 __asm__ __volatile__ ("" : : "g" (x)); \
260 } while (0)
261 #define scm_remember_upto_here_2(x, y) \
262 do { \
263 scm_remember_upto_here_1 (x); \
264 scm_remember_upto_here_1 (y); \
265 } while (0)
266 #endif
267
268 SCM_API SCM scm_return_first (SCM elt, ...);
269 SCM_API int scm_return_first_int (int x, ...);
270 SCM_API SCM scm_permanent_object (SCM obj);
271 SCM_API SCM scm_gc_protect_object (SCM obj);
272 SCM_API SCM scm_gc_unprotect_object (SCM obj);
273 SCM_API void scm_gc_register_root (SCM *p);
274 SCM_API void scm_gc_unregister_root (SCM *p);
275 SCM_API void scm_gc_register_roots (SCM *b, unsigned long n);
276 SCM_API void scm_gc_unregister_roots (SCM *b, unsigned long n);
277 SCM_API void scm_storage_prehistory (void);
278 SCM_API int scm_init_storage (void);
279 SCM_API void *scm_get_stack_base (void);
280 SCM_API void scm_init_gc (void);
281
282 #if SCM_ENABLE_DEPRECATED == 1
283
284 SCM_API SCM scm_deprecated_newcell (void);
285 SCM_API SCM scm_deprecated_newcell2 (void);
286
287 #define SCM_NEWCELL(_into) \
288 do { _into = scm_deprecated_newcell (); } while (0)
289 #define SCM_NEWCELL2(_into) \
290 do { _into = scm_deprecated_newcell2 (); } while (0)
291
292 SCM_API void * scm_must_malloc (size_t len, const char *what);
293 SCM_API void * scm_must_realloc (void *where,
294 size_t olen, size_t len,
295 const char *what);
296 SCM_API char *scm_must_strdup (const char *str);
297 SCM_API char *scm_must_strndup (const char *str, size_t n);
298 SCM_API void scm_done_malloc (long size);
299 SCM_API void scm_done_free (long size);
300 SCM_API void scm_must_free (void *obj);
301
302 #endif
303
304 #endif /* SCM_GC_H */
305
306 /*
307 Local Variables:
308 c-file-style: "gnu"
309 End:
310 */