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