6 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
8 * This library is free software; you can redistribute it and/or
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.
13 * This library is distributed in the hope that it will be useful, but
14 * 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.
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
26 #include "libguile/__scm.h"
28 #include "libguile/hooks.h"
29 #include "libguile/threads.h"
32 typedef struct scm_t_cell
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,
43 * pointers to scm_vector elts, functions, &c are not munged.
46 # define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x) >> 3))
47 # define PTR2SCM(x) (SCM_PACK (((scm_t_bits) (x)) << 3))
49 # define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x)))
50 # define PTR2SCM(x) (SCM_PACK ((scm_t_bits) (x)))
51 #endif /* def _UNICOS */
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. */
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))))
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)))
68 #define SCM_GC_CELL_TYPE(x) (SCM_GC_CELL_OBJECT ((x), 0))
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. */
78 #if (SCM_DEBUG_CELL_ACCESSES == 1)
79 # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
81 # define SCM_VALIDATE_CELL(cell, expr) (expr)
84 #define SCM_CELL_WORD(x, n) \
85 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_WORD ((x), (n)))
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)
91 #define SCM_CELL_OBJECT(x, n) \
92 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_OBJECT ((x), (n)))
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)
98 #define SCM_SET_CELL_WORD(x, n, v) \
99 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_WORD ((x), (n), (v)))
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))
105 #define SCM_SET_CELL_OBJECT(x, n, v) \
106 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_OBJECT ((x), (n), (v)))
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))
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))
116 #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
117 #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 ((x), (t))
120 #if (SCM_DEBUG_CELL_ACCESSES == 1)
121 /* Set this to != 0 if every cell that is accessed shall be checked:
123 SCM_API
int scm_debug_cell_accesses_p
;
124 SCM_API
int scm_expensive_debug_cell_accesses_p
;
125 SCM_API
int scm_debug_cells_gc_interval
;
126 void scm_i_expensive_validation_check (SCM cell
);
129 SCM_INTERNAL scm_i_pthread_mutex_t scm_i_gc_admin_mutex
;
131 #define scm_gc_running_p 0
132 SCM_INTERNAL scm_i_pthread_mutex_t scm_i_sweep_mutex
;
135 void *scm_ia64_register_backing_store_base (void);
136 void *scm_ia64_ar_bsp (const void *);
141 #if (SCM_ENABLE_DEPRECATED == 1)
142 SCM_API
size_t scm_default_init_heap_size_1
;
143 SCM_API
int scm_default_min_yield_1
;
144 SCM_API
size_t scm_default_init_heap_size_2
;
145 SCM_API
int scm_default_min_yield_2
;
146 SCM_API
size_t scm_default_max_segment_size
;
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
156 SCM_API
size_t scm_max_segment_size
;
158 #define SCM_SET_FREELIST_LOC(key,ptr) scm_i_pthread_setspecific ((key), (ptr))
159 #define SCM_FREELIST_LOC(key) ((SCM *) scm_i_pthread_getspecific (key))
160 SCM_API
struct scm_t_cell_type_statistics scm_i_master_freelist
;
161 SCM_API
struct scm_t_cell_type_statistics scm_i_master_freelist2
;
163 SCM_API
unsigned long scm_mallocated
;
164 SCM_API
unsigned long scm_gc_ports_collected
;
165 SCM_API
unsigned long scm_mtrigger
;
167 SCM_API SCM scm_after_gc_hook
;
169 SCM_API scm_t_c_hook scm_before_gc_c_hook
;
170 SCM_API scm_t_c_hook scm_before_mark_c_hook
;
171 SCM_API scm_t_c_hook scm_before_sweep_c_hook
;
172 SCM_API scm_t_c_hook scm_after_sweep_c_hook
;
173 SCM_API scm_t_c_hook scm_after_gc_c_hook
;
175 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
176 #if (SCM_ENABLE_DEPRECATED == 1)
177 SCM
scm_map_free_list (void);
179 #define scm_map_free_list deprecated
180 #define scm_free_list_length deprecated
184 #if (SCM_ENABLE_DEPRECATED == 1) && defined (GUILE_DEBUG_FREELIST)
185 SCM_API SCM
scm_gc_set_debug_check_freelist_x (SCM flag
);
189 #if (SCM_DEBUG_CELL_ACCESSES == 1)
190 SCM_API
void scm_assert_cell_valid (SCM
);
193 SCM_API SCM
scm_set_debug_cell_accesses_x (SCM flag
);
196 SCM_API SCM
scm_object_address (SCM obj
);
197 SCM_API SCM
scm_gc_enable (void);
198 SCM_API SCM
scm_gc_disable (void);
199 SCM_API SCM
scm_gc_dump (void);
200 SCM_API SCM
scm_gc_stats (void);
201 SCM_API SCM
scm_gc_live_object_stats (void);
202 SCM_API SCM
scm_gc (void);
203 SCM_API
void scm_i_gc (const char *what
);
204 SCM_API
void scm_gc_mark (SCM p
);
205 SCM_API
void scm_gc_sweep (void);
207 SCM_API
void *scm_malloc (size_t size
);
208 SCM_API
void *scm_calloc (size_t size
);
209 SCM_API
void *scm_realloc (void *mem
, size_t size
);
210 SCM_API
char *scm_strdup (const char *str
);
211 SCM_API
char *scm_strndup (const char *str
, size_t n
);
212 SCM_API
void scm_gc_register_collectable_memory (void *mem
, size_t size
,
214 SCM_API
void scm_gc_unregister_collectable_memory (void *mem
, size_t size
,
216 SCM_API
void *scm_gc_malloc_pointerless (size_t size
, const char *what
);
217 SCM_API
void *scm_gc_calloc (size_t size
, const char *what
);
218 SCM_API
void *scm_gc_malloc (size_t size
, const char *what
);
219 SCM_API
void *scm_gc_realloc (void *mem
, size_t old_size
,
220 size_t new_size
, const char *what
);
221 SCM_API
void scm_gc_free (void *mem
, size_t size
, const char *what
);
222 SCM_API
char *scm_gc_strdup (const char *str
, const char *what
);
223 SCM_API
char *scm_gc_strndup (const char *str
, size_t n
, const char *what
);
225 SCM_API
void scm_remember_upto_here_1 (SCM obj
);
226 SCM_API
void scm_remember_upto_here_2 (SCM obj1
, SCM obj2
);
227 SCM_API
void scm_remember_upto_here (SCM obj1
, ...);
229 /* In GCC we can force a reference to an SCM by making it an input to an
230 empty asm. This avoids the code size and slowdown of an actual function
231 call. Unfortunately there doesn't seem to be any way to do the varargs
232 scm_remember_upto_here like this.
234 __volatile__ ensures nothing will be moved across the asm, and it won't
235 be optimized away (or only if proved unreachable). Constraint "g" can be
236 used on all processors and allows any memory or general register (or
237 immediate) operand. The actual asm syntax doesn't matter, we don't want
238 to use it, just ensure the operand is still alive. See "Extended Asm" in
239 the GCC manual for more. */
242 #define scm_remember_upto_here_1(x) \
244 __asm__ __volatile__ ("" : : "g" (x)); \
246 #define scm_remember_upto_here_2(x, y) \
248 scm_remember_upto_here_1 (x); \
249 scm_remember_upto_here_1 (y); \
253 SCM_API SCM
scm_return_first (SCM elt
, ...);
254 SCM_API
int scm_return_first_int (int x
, ...);
255 SCM_API SCM
scm_permanent_object (SCM obj
);
256 SCM_API SCM
scm_gc_protect_object (SCM obj
);
257 SCM_API SCM
scm_gc_unprotect_object (SCM obj
);
258 SCM_API
void scm_gc_register_root (SCM
*p
);
259 SCM_API
void scm_gc_unregister_root (SCM
*p
);
260 SCM_API
void scm_gc_register_roots (SCM
*b
, unsigned long n
);
261 SCM_API
void scm_gc_unregister_roots (SCM
*b
, unsigned long n
);
262 SCM_API
void scm_storage_prehistory (void);
263 SCM_API
int scm_init_storage (void);
264 SCM_API
void scm_init_gc (void);
266 #if SCM_ENABLE_DEPRECATED == 1
268 SCM_API SCM
scm_deprecated_newcell (void);
269 SCM_API SCM
scm_deprecated_newcell2 (void);
271 #define SCM_NEWCELL(_into) \
272 do { _into = scm_deprecated_newcell (); } while (0)
273 #define SCM_NEWCELL2(_into) \
274 do { _into = scm_deprecated_newcell2 (); } while (0)
276 SCM_API
void * scm_must_malloc (size_t len
, const char *what
);
277 SCM_API
void * scm_must_realloc (void *where
,
278 size_t olen
, size_t len
,
280 SCM_API
char *scm_must_strdup (const char *str
);
281 SCM_API
char *scm_must_strndup (const char *str
, size_t n
);
282 SCM_API
void scm_done_malloc (long size
);
283 SCM_API
void scm_done_free (long size
);
284 SCM_API
void scm_must_free (void *obj
);
288 #endif /* SCM_GC_H */