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