Reify bytevector? in the correct module
[bpt/guile.git] / libguile / gc.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
61045190
DH
3#ifndef SCM_GC_H
4#define SCM_GC_H
8c494e99 5
c3365149 6/* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006,
c2247b78 7 * 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
a00c95d9 8 *
73be1d9e 9 * This library is free software; you can redistribute it and/or
53befeb7
NJ
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
a00c95d9 13 *
53befeb7
NJ
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
a00c95d9 18 *
73be1d9e
MV
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
53befeb7
NJ
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301 USA
73be1d9e 23 */
d3a6bc94 24
0f2d19dd
JB
25\f
26
b4309c3c 27#include "libguile/__scm.h"
2549a709 28
9b3e180c 29#include "libguile/hooks.h"
9de87eea 30#include "libguile/threads.h"
9bc4701c 31
0f2d19dd 32\f
228a24ef 33typedef struct scm_t_cell
2549a709 34{
33c527ec
MV
35 SCM word_0;
36 SCM word_1;
228a24ef 37} scm_t_cell;
2549a709 38
21041372
AW
39/* FIXME: deprecate. */
40#define PTR2SCM(x) (SCM_PACK_POINTER (x))
41#define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK_POINTER (x)))
1fc8902f
DH
42
43/* Low level cell data accessing macros. These macros should only be used
44 * from within code related to garbage collection issues, since they will
45 * never check the cells they are applied to - not even if guile is compiled
46 * in debug mode. In particular these macros will even work for free cells,
47 * which should never be encountered by user code. */
48
0aed71aa
AW
49#define SCM_GC_CELL_OBJECT(x, n) (((SCM *)SCM2PTR (x)) [n])
50#define SCM_GC_CELL_WORD(x, n) (SCM_UNPACK (SCM_GC_CELL_OBJECT ((x), (n))))
33c527ec 51
0aed71aa 52#define SCM_GC_SET_CELL_OBJECT(x, n, v) ((((SCM *)SCM2PTR (x)) [n]) = (v))
33c527ec 53#define SCM_GC_SET_CELL_WORD(x, n, v) \
0aed71aa 54 (SCM_GC_SET_CELL_OBJECT ((x), (n), SCM_PACK (v)))
33c527ec
MV
55
56#define SCM_GC_CELL_TYPE(x) (SCM_GC_CELL_OBJECT ((x), 0))
1fc8902f
DH
57
58
59/* Except for the garbage collector, no part of guile should ever run over a
60 * free cell. Thus, if guile is compiled in debug mode the SCM_CELL_* and
61 * SCM_SET_CELL_* macros below report an error if they are applied to a free
62 * cell. Some other plausibility checks are also performed. However, if
63 * guile is not compiled in debug mode, there won't be any time penalty at all
64 * when using these macros. */
2549a709 65
406c7d90
DH
66#if (SCM_DEBUG_CELL_ACCESSES == 1)
67# define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
708cb87c 68#else
61045190 69# define SCM_VALIDATE_CELL(cell, expr) (expr)
708cb87c 70#endif
46d53380 71
f706a58b 72#define SCM_CELL_WORD(x, n) \
1fc8902f 73 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_WORD ((x), (n)))
33c527ec
MV
74#define SCM_CELL_WORD_0(x) SCM_CELL_WORD ((x), 0)
75#define SCM_CELL_WORD_1(x) SCM_CELL_WORD ((x), 1)
76#define SCM_CELL_WORD_2(x) SCM_CELL_WORD ((x), 2)
77#define SCM_CELL_WORD_3(x) SCM_CELL_WORD ((x), 3)
2549a709 78
f706a58b 79#define SCM_CELL_OBJECT(x, n) \
1fc8902f 80 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_OBJECT ((x), (n)))
33c527ec
MV
81#define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT ((x), 0)
82#define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT ((x), 1)
83#define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT ((x), 2)
84#define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT ((x), 3)
2549a709 85
f706a58b 86#define SCM_SET_CELL_WORD(x, n, v) \
1fc8902f 87 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_WORD ((x), (n), (v)))
33c527ec
MV
88#define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD ((x), 0, (v))
89#define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD ((x), 1, (v))
90#define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD ((x), 2, (v))
91#define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD ((x), 3, (v))
2549a709 92
f706a58b 93#define SCM_SET_CELL_OBJECT(x, n, v) \
1fc8902f 94 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_OBJECT ((x), (n), (v)))
33c527ec
MV
95#define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT ((x), 0, (v))
96#define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT ((x), 1, (v))
97#define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT ((x), 2, (v))
98#define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT ((x), 3, (v))
2549a709 99
0aed71aa 100#define SCM_CELL_OBJECT_LOC(x, n) (SCM_VALIDATE_CELL((x), &SCM_GC_CELL_OBJECT ((x), (n))))
b17e0ac3
MV
101#define SCM_CARLOC(x) (SCM_CELL_OBJECT_LOC ((x), 0))
102#define SCM_CDRLOC(x) (SCM_CELL_OBJECT_LOC ((x), 1))
103
2549a709 104#define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
33c527ec 105#define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 ((x), (t))
2549a709 106
61045190 107
61045190 108#if (SCM_DEBUG_CELL_ACCESSES == 1)
eab1b259
HWN
109/* Set this to != 0 if every cell that is accessed shall be checked:
110 */
111SCM_API int scm_debug_cell_accesses_p;
112SCM_API int scm_expensive_debug_cell_accesses_p;
113SCM_API int scm_debug_cells_gc_interval ;
5f5e7a2c 114SCM_API void scm_i_expensive_validation_check (SCM cell);
61045190
DH
115#endif
116
102dbb6f 117SCM_INTERNAL scm_i_pthread_mutex_t scm_i_gc_admin_mutex;
eb01cb64 118
6033d326 119#define scm_gc_running_p 0
102dbb6f 120SCM_INTERNAL scm_i_pthread_mutex_t scm_i_sweep_mutex;
b17e0ac3 121
9a5fa6e9
NJ
122#ifdef __ia64__
123void *scm_ia64_register_backing_store_base (void);
124void *scm_ia64_ar_bsp (const void *);
125#endif
126
0f2d19dd
JB
127\f
128
b74e86cf 129SCM_API unsigned long scm_gc_ports_collected;
33b001fd
MV
130
131SCM_API SCM scm_after_gc_hook;
132
133SCM_API scm_t_c_hook scm_before_gc_c_hook;
134SCM_API scm_t_c_hook scm_before_mark_c_hook;
135SCM_API scm_t_c_hook scm_before_sweep_c_hook;
136SCM_API scm_t_c_hook scm_after_sweep_c_hook;
137SCM_API scm_t_c_hook scm_after_gc_c_hook;
9b3e180c 138
0f2d19dd 139\f
0f2d19dd 140
61045190 141#if (SCM_DEBUG_CELL_ACCESSES == 1)
33b001fd 142SCM_API void scm_assert_cell_valid (SCM);
61045190 143#endif
c8a1bdc4
HWN
144
145SCM_API SCM scm_set_debug_cell_accesses_x (SCM flag);
146
147
33b001fd 148SCM_API SCM scm_object_address (SCM obj);
915b3f9f
LC
149SCM_API SCM scm_gc_enable (void);
150SCM_API SCM scm_gc_disable (void);
7f9ec18a 151SCM_API SCM scm_gc_dump (void);
33b001fd
MV
152SCM_API SCM scm_gc_stats (void);
153SCM_API SCM scm_gc (void);
8c93b597 154SCM_INTERNAL void scm_i_gc (const char *what);
33b001fd 155SCM_API void scm_gc_mark (SCM p);
33b001fd 156SCM_API void scm_gc_sweep (void);
4c9419ac 157
fd51e661
AW
158SCM_API void scm_gc_register_allocation (size_t size);
159
e3401c65
LC
160SCM_API void *scm_malloc (size_t size) SCM_MALLOC;
161SCM_API void *scm_calloc (size_t size) SCM_MALLOC;
4c9419ac 162SCM_API void *scm_realloc (void *mem, size_t size);
e3401c65
LC
163SCM_API char *scm_strdup (const char *str) SCM_MALLOC;
164SCM_API char *scm_strndup (const char *str, size_t n) SCM_MALLOC;
4c9419ac
MV
165SCM_API void scm_gc_register_collectable_memory (void *mem, size_t size,
166 const char *what);
167SCM_API void scm_gc_unregister_collectable_memory (void *mem, size_t size,
168 const char *what);
e3401c65
LC
169SCM_API void *scm_gc_malloc_pointerless (size_t size, const char *what)
170 SCM_MALLOC;
171SCM_API void *scm_gc_calloc (size_t size, const char *what)
172 SCM_MALLOC;
173SCM_API void *scm_gc_malloc (size_t size, const char *what)
174 SCM_MALLOC;
4c9419ac
MV
175SCM_API void *scm_gc_realloc (void *mem, size_t old_size,
176 size_t new_size, const char *what);
177SCM_API void scm_gc_free (void *mem, size_t size, const char *what);
e3401c65
LC
178SCM_API char *scm_gc_strdup (const char *str, const char *what)
179 SCM_MALLOC;
180SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what)
181 SCM_MALLOC;
4c9419ac 182
663780bb 183#define scm_gc_typed_calloc(t) ((t *) scm_gc_calloc (sizeof (t), #t))
4cf77f09
AW
184
185#ifdef BUILDING_LIBGUILE
186#include "libguile/bdw-gc.h"
187#define SCM_GC_MALLOC(size) GC_MALLOC (size)
188#define SCM_GC_MALLOC_POINTERLESS(size) GC_MALLOC_ATOMIC (size)
189#else
190#define SCM_GC_MALLOC(size) scm_gc_malloc (size, NULL)
191#define SCM_GC_MALLOC_POINTERLESS(size) scm_gc_malloc_pointerless (size, NULL)
192#endif
193
194
ff670362
AW
195SCM_INLINE SCM scm_cell (scm_t_bits car, scm_t_bits cdr);
196SCM_INLINE SCM scm_double_cell (scm_t_bits car, scm_t_bits cbr,
197 scm_t_bits ccr, scm_t_bits cdr);
c3365149 198SCM_INLINE SCM scm_words (scm_t_bits car, scm_t_uint32 n_words);
ff670362
AW
199
200#if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
201
202SCM_INLINE_IMPLEMENTATION SCM
203scm_cell (scm_t_bits car, scm_t_bits cdr)
204{
21041372 205 SCM cell = SCM_PACK_POINTER (SCM_GC_MALLOC (sizeof (scm_t_cell)));
ff670362
AW
206
207 /* Initialize the type slot last so that the cell is ignored by the GC
208 until it is completely initialized. This is only relevant when the GC
209 can actually run during this code, which it can't since the GC only runs
210 when all other threads are stopped. */
211 SCM_GC_SET_CELL_WORD (cell, 1, cdr);
212 SCM_GC_SET_CELL_WORD (cell, 0, car);
213
214 return cell;
215}
216
217SCM_INLINE_IMPLEMENTATION SCM
218scm_double_cell (scm_t_bits car, scm_t_bits cbr,
219 scm_t_bits ccr, scm_t_bits cdr)
220{
221 SCM z;
222
21041372 223 z = SCM_PACK_POINTER (SCM_GC_MALLOC (2 * sizeof (scm_t_cell)));
ff670362
AW
224 /* Initialize the type slot last so that the cell is ignored by the
225 GC until it is completely initialized. This is only relevant
226 when the GC can actually run during this code, which it can't
227 since the GC only runs when all other threads are stopped.
228 */
229 SCM_GC_SET_CELL_WORD (z, 1, cbr);
230 SCM_GC_SET_CELL_WORD (z, 2, ccr);
231 SCM_GC_SET_CELL_WORD (z, 3, cdr);
232 SCM_GC_SET_CELL_WORD (z, 0, car);
233
234 /* When this function is inlined, it's possible that the last
235 SCM_GC_SET_CELL_WORD above will be adjacent to a following
236 initialization of z. E.g., it occurred in scm_make_real. GCC
237 from around version 3 (e.g., certainly 3.2) began taking
238 advantage of strict C aliasing rules which say that it's OK to
239 interchange the initialization above and the one below when the
240 pointer types appear to differ sufficiently. We don't want that,
241 of course. GCC allows this behaviour to be disabled with the
242 -fno-strict-aliasing option, but would also need to be supplied
243 by Guile users. Instead, the following statements prevent the
244 reordering.
245 */
246#ifdef __GNUC__
247 __asm__ volatile ("" : : : "memory");
248#else
249 /* portable version, just in case any other compiler does the same
250 thing. */
251 scm_remember_upto_here_1 (z);
252#endif
253
254 return z;
255}
256
257SCM_INLINE_IMPLEMENTATION SCM
c3365149 258scm_words (scm_t_bits car, scm_t_uint32 n_words)
ff670362
AW
259{
260 SCM z;
261
21041372 262 z = SCM_PACK_POINTER (SCM_GC_MALLOC (sizeof (scm_t_bits) * n_words));
ff670362
AW
263 SCM_GC_SET_CELL_WORD (z, 0, car);
264
265 /* FIXME: is the following concern even relevant with BDW-GC? */
266
267 /* When this function is inlined, it's possible that the last
268 SCM_GC_SET_CELL_WORD above will be adjacent to a following
269 initialization of z. E.g., it occurred in scm_make_real. GCC
270 from around version 3 (e.g., certainly 3.2) began taking
271 advantage of strict C aliasing rules which say that it's OK to
272 interchange the initialization above and the one below when the
273 pointer types appear to differ sufficiently. We don't want that,
274 of course. GCC allows this behaviour to be disabled with the
275 -fno-strict-aliasing option, but would also need to be supplied
276 by Guile users. Instead, the following statements prevent the
277 reordering.
278 */
279#ifdef __GNUC__
280 __asm__ volatile ("" : : : "memory");
281#else
282 /* portable version, just in case any other compiler does the same
283 thing. */
284 scm_remember_upto_here_1 (z);
285#endif
286
287 return z;
288}
289
290#endif /* SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES */
291
33b001fd
MV
292SCM_API void scm_remember_upto_here_1 (SCM obj);
293SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
294SCM_API void scm_remember_upto_here (SCM obj1, ...);
aca3618f 295
c1ffdc6a
KR
296/* In GCC we can force a reference to an SCM by making it an input to an
297 empty asm. This avoids the code size and slowdown of an actual function
298 call. Unfortunately there doesn't seem to be any way to do the varargs
299 scm_remember_upto_here like this.
300
301 __volatile__ ensures nothing will be moved across the asm, and it won't
302 be optimized away (or only if proved unreachable). Constraint "g" can be
303 used on all processors and allows any memory or general register (or
304 immediate) operand. The actual asm syntax doesn't matter, we don't want
305 to use it, just ensure the operand is still alive. See "Extended Asm" in
306 the GCC manual for more. */
aca3618f
KR
307
308#ifdef __GNUC__
309#define scm_remember_upto_here_1(x) \
310 do { \
311 __asm__ __volatile__ ("" : : "g" (x)); \
312 } while (0)
313#define scm_remember_upto_here_2(x, y) \
314 do { \
315 scm_remember_upto_here_1 (x); \
316 scm_remember_upto_here_1 (y); \
317 } while (0)
318#endif
319
33b001fd
MV
320SCM_API SCM scm_return_first (SCM elt, ...);
321SCM_API int scm_return_first_int (int x, ...);
322SCM_API SCM scm_permanent_object (SCM obj);
323SCM_API SCM scm_gc_protect_object (SCM obj);
324SCM_API SCM scm_gc_unprotect_object (SCM obj);
325SCM_API void scm_gc_register_root (SCM *p);
326SCM_API void scm_gc_unregister_root (SCM *p);
327SCM_API void scm_gc_register_roots (SCM *b, unsigned long n);
328SCM_API void scm_gc_unregister_roots (SCM *b, unsigned long n);
c2247b78 329SCM_INTERNAL void scm_gc_after_nonlocal_exit (void);
562cd1b8
AW
330SCM_INTERNAL void scm_storage_prehistory (void);
331SCM_INTERNAL void scm_init_gc_protect_object (void);
332SCM_INTERNAL void scm_init_gc (void);
d1ca2c64 333
61045190 334#endif /* SCM_GC_H */
89e00824
ML
335
336/*
337 Local Variables:
338 c-file-style: "gnu"
339 End:
340*/