Merge branch 'master' into boehm-demers-weiser-gc
[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
6f03035f 6/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
a00c95d9 7 *
73be1d9e
MV
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.
a00c95d9 12 *
73be1d9e 13 * This library is distributed in the hope that it will be useful,
0f2d19dd 14 * but 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
92205699 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 21 */
d3a6bc94 22
0f2d19dd
JB
23\f
24
b4309c3c 25#include "libguile/__scm.h"
2549a709 26
9b3e180c 27#include "libguile/hooks.h"
9de87eea 28#include "libguile/threads.h"
9bc4701c 29
0f2d19dd 30\f
228a24ef 31typedef struct scm_t_cell
2549a709 32{
33c527ec
MV
33 SCM word_0;
34 SCM word_1;
228a24ef 35} scm_t_cell;
2549a709 36
33c527ec
MV
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,
e618c9a3 42 * pointers to scm_vector elts, functions, &c are not munged.
2549a709
DH
43 */
44#ifdef _UNICOS
c8a1bdc4 45# define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x) >> 3))
92c2555f 46# define PTR2SCM(x) (SCM_PACK (((scm_t_bits) (x)) << 3))
2549a709 47#else
c8a1bdc4 48# define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x)))
92c2555f 49# define PTR2SCM(x) (SCM_PACK ((scm_t_bits) (x)))
2549a709
DH
50#endif /* def _UNICOS */
51
c8a1bdc4 52
1fc8902f
DH
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
33c527ec
MV
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))
1fc8902f
DH
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. */
2549a709 76
406c7d90
DH
77#if (SCM_DEBUG_CELL_ACCESSES == 1)
78# define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
708cb87c 79#else
61045190 80# define SCM_VALIDATE_CELL(cell, expr) (expr)
708cb87c 81#endif
46d53380 82
f706a58b 83#define SCM_CELL_WORD(x, n) \
1fc8902f 84 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_WORD ((x), (n)))
33c527ec
MV
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)
2549a709 89
f706a58b 90#define SCM_CELL_OBJECT(x, n) \
1fc8902f 91 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_OBJECT ((x), (n)))
33c527ec
MV
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)
2549a709 96
f706a58b 97#define SCM_SET_CELL_WORD(x, n, v) \
1fc8902f 98 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_WORD ((x), (n), (v)))
33c527ec
MV
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))
2549a709 103
f706a58b 104#define SCM_SET_CELL_OBJECT(x, n, v) \
1fc8902f 105 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_OBJECT ((x), (n), (v)))
33c527ec
MV
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))
2549a709 110
b17e0ac3
MV
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
2549a709 115#define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
33c527ec 116#define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 ((x), (t))
2549a709 117
1fc8902f
DH
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. */
61045190 122
1fc8902f
DH
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)))
61045190 127
61045190 128#if (SCM_DEBUG_CELL_ACCESSES == 1)
eab1b259
HWN
129/* Set this to != 0 if every cell that is accessed shall be checked:
130 */
131SCM_API int scm_debug_cell_accesses_p;
132SCM_API int scm_expensive_debug_cell_accesses_p;
133SCM_API int scm_debug_cells_gc_interval ;
1e71eafb 134void scm_i_expensive_validation_check (SCM cell);
61045190
DH
135#endif
136
102dbb6f 137SCM_INTERNAL scm_i_pthread_mutex_t scm_i_gc_admin_mutex;
eb01cb64 138
6033d326 139#define scm_gc_running_p 0
102dbb6f 140SCM_INTERNAL scm_i_pthread_mutex_t scm_i_sweep_mutex;
b17e0ac3 141
9a5fa6e9
NJ
142#ifdef __ia64__
143void *scm_ia64_register_backing_store_base (void);
144void *scm_ia64_ar_bsp (const void *);
145#endif
146
0f2d19dd
JB
147\f
148
c8a1bdc4 149#if (SCM_ENABLE_DEPRECATED == 1)
33b001fd
MV
150SCM_API size_t scm_default_init_heap_size_1;
151SCM_API int scm_default_min_yield_1;
152SCM_API size_t scm_default_init_heap_size_2;
153SCM_API int scm_default_min_yield_2;
154SCM_API size_t scm_default_max_segment_size;
c8a1bdc4
HWN
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
33b001fd
MV
163
164SCM_API size_t scm_max_segment_size;
c8a1bdc4 165
9de87eea
MV
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))
2e945bcc
SJ
168SCM_API struct scm_t_cell_type_statistics scm_i_master_freelist;
169SCM_API struct scm_t_cell_type_statistics scm_i_master_freelist2;
eab1b259 170
33b001fd 171SCM_API unsigned long scm_mallocated;
b74e86cf 172SCM_API unsigned long scm_gc_ports_collected;
33b001fd
MV
173SCM_API unsigned long scm_mtrigger;
174
175SCM_API SCM scm_after_gc_hook;
176
177SCM_API scm_t_c_hook scm_before_gc_c_hook;
178SCM_API scm_t_c_hook scm_before_mark_c_hook;
179SCM_API scm_t_c_hook scm_before_sweep_c_hook;
180SCM_API scm_t_c_hook scm_after_sweep_c_hook;
181SCM_API scm_t_c_hook scm_after_gc_c_hook;
9b3e180c 182
bb2c57fa 183#if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
2e945bcc
SJ
184#if (SCM_ENABLE_DEPRECATED == 1)
185SCM scm_map_free_list (void);
186#else
c8a1bdc4
HWN
187#define scm_map_free_list deprecated
188#define scm_free_list_length deprecated
bb2c57fa 189#endif
2e945bcc 190#endif
c8a1bdc4
HWN
191
192#if (SCM_ENABLE_DEPRECATED == 1) && defined (GUILE_DEBUG_FREELIST)
33b001fd 193SCM_API SCM scm_gc_set_debug_check_freelist_x (SCM flag);
88256b2e 194#endif
0f2d19dd 195\f
0f2d19dd 196
61045190 197#if (SCM_DEBUG_CELL_ACCESSES == 1)
33b001fd 198SCM_API void scm_assert_cell_valid (SCM);
61045190 199#endif
c8a1bdc4
HWN
200
201SCM_API SCM scm_set_debug_cell_accesses_x (SCM flag);
202
203
33b001fd 204SCM_API SCM scm_object_address (SCM obj);
915b3f9f
LC
205SCM_API SCM scm_gc_enable (void);
206SCM_API SCM scm_gc_disable (void);
7f9ec18a 207SCM_API SCM scm_gc_dump (void);
33b001fd 208SCM_API SCM scm_gc_stats (void);
1367aa5e 209SCM_API SCM scm_gc_live_object_stats (void);
33b001fd 210SCM_API SCM scm_gc (void);
b17e0ac3 211SCM_API void scm_i_gc (const char *what);
33b001fd 212SCM_API void scm_gc_mark (SCM p);
c8a1bdc4 213SCM_API int scm_in_heap_p (SCM value);
33b001fd 214SCM_API void scm_gc_sweep (void);
4c9419ac
MV
215
216SCM_API void *scm_malloc (size_t size);
ba1b2226 217SCM_API void *scm_calloc (size_t size);
4c9419ac
MV
218SCM_API void *scm_realloc (void *mem, size_t size);
219SCM_API char *scm_strdup (const char *str);
220SCM_API char *scm_strndup (const char *str, size_t n);
221SCM_API void scm_gc_register_collectable_memory (void *mem, size_t size,
222 const char *what);
223SCM_API void scm_gc_unregister_collectable_memory (void *mem, size_t size,
224 const char *what);
c5018a2b 225SCM_API void *scm_gc_malloc_pointerless (size_t size, const char *what);
39e8f371 226SCM_API void *scm_gc_calloc (size_t size, const char *what);
4c9419ac
MV
227SCM_API void *scm_gc_malloc (size_t size, const char *what);
228SCM_API void *scm_gc_realloc (void *mem, size_t old_size,
229 size_t new_size, const char *what);
230SCM_API void scm_gc_free (void *mem, size_t size, const char *what);
231SCM_API char *scm_gc_strdup (const char *str, const char *what);
232SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what);
233
33b001fd
MV
234SCM_API void scm_remember_upto_here_1 (SCM obj);
235SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
236SCM_API void scm_remember_upto_here (SCM obj1, ...);
aca3618f 237
c1ffdc6a
KR
238/* In GCC we can force a reference to an SCM by making it an input to an
239 empty asm. This avoids the code size and slowdown of an actual function
240 call. Unfortunately there doesn't seem to be any way to do the varargs
241 scm_remember_upto_here like this.
242
243 __volatile__ ensures nothing will be moved across the asm, and it won't
244 be optimized away (or only if proved unreachable). Constraint "g" can be
245 used on all processors and allows any memory or general register (or
246 immediate) operand. The actual asm syntax doesn't matter, we don't want
247 to use it, just ensure the operand is still alive. See "Extended Asm" in
248 the GCC manual for more. */
aca3618f
KR
249
250#ifdef __GNUC__
251#define scm_remember_upto_here_1(x) \
252 do { \
253 __asm__ __volatile__ ("" : : "g" (x)); \
254 } while (0)
255#define scm_remember_upto_here_2(x, y) \
256 do { \
257 scm_remember_upto_here_1 (x); \
258 scm_remember_upto_here_1 (y); \
259 } while (0)
260#endif
261
33b001fd
MV
262SCM_API SCM scm_return_first (SCM elt, ...);
263SCM_API int scm_return_first_int (int x, ...);
264SCM_API SCM scm_permanent_object (SCM obj);
265SCM_API SCM scm_gc_protect_object (SCM obj);
266SCM_API SCM scm_gc_unprotect_object (SCM obj);
267SCM_API void scm_gc_register_root (SCM *p);
268SCM_API void scm_gc_unregister_root (SCM *p);
269SCM_API void scm_gc_register_roots (SCM *b, unsigned long n);
270SCM_API void scm_gc_unregister_roots (SCM *b, unsigned long n);
c35738c1 271SCM_API void scm_storage_prehistory (void);
33b001fd 272SCM_API int scm_init_storage (void);
33b001fd 273SCM_API void scm_init_gc (void);
d1ca2c64 274
d678e25c
MV
275#if SCM_ENABLE_DEPRECATED == 1
276
277SCM_API SCM scm_deprecated_newcell (void);
278SCM_API SCM scm_deprecated_newcell2 (void);
279
280#define SCM_NEWCELL(_into) \
281 do { _into = scm_deprecated_newcell (); } while (0)
282#define SCM_NEWCELL2(_into) \
283 do { _into = scm_deprecated_newcell2 (); } while (0)
284
539b08a4
MV
285SCM_API void * scm_must_malloc (size_t len, const char *what);
286SCM_API void * scm_must_realloc (void *where,
287 size_t olen, size_t len,
288 const char *what);
289SCM_API char *scm_must_strdup (const char *str);
290SCM_API char *scm_must_strndup (const char *str, size_t n);
291SCM_API void scm_done_malloc (long size);
292SCM_API void scm_done_free (long size);
293SCM_API void scm_must_free (void *obj);
294
d678e25c
MV
295#endif
296
61045190 297#endif /* SCM_GC_H */
89e00824
ML
298
299/*
300 Local Variables:
301 c-file-style: "gnu"
302 End:
303*/