6 /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this software; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307 USA
23 * As a special exception, the Free Software Foundation gives permission
24 * for additional uses of the text contained in its release of GUILE.
26 * The exception is that, if you link the GUILE library with other files
27 * to produce an executable, this does not by itself cause the
28 * resulting executable to be covered by the GNU General Public License.
29 * Your use of that executable is in no way restricted on account of
30 * linking the GUILE library code into it.
32 * This exception does not however invalidate any other reasons why
33 * the executable file might be covered by the GNU General Public License.
35 * This exception applies only to the code released by the
36 * Free Software Foundation under the name GUILE. If you copy
37 * code from other Free Software Foundation releases into a copy of
38 * GUILE, as the General Public License permits, the exception does
39 * not apply to the code that you add in this way. To avoid misleading
40 * anyone as to the status of such modified files, you must delete
41 * this exception notice from them.
43 * If you write modifications of your own for GUILE, it is your choice
44 * whether to permit this exception to apply to your modifications.
45 * If you do not wish that, delete this exception notice. */
49 #include "libguile/__scm.h"
51 #include "libguile/hooks.h"
55 typedef struct scm_t_cell
62 /* SCM_CELLPTR is a pointer to a cons cell which may be compared or
65 typedef scm_t_cell
* SCM_CELLPTR
;
68 /* Cray machines have pointers that are incremented once for each word,
69 * rather than each byte, the 3 most significant bits encode the byte
70 * within the word. The following macros deal with this by storing the
71 * native Cray pointers like the ones that looks like scm expects. This
72 * is done for any pointers that might appear in the car of a scm_t_cell,
73 * pointers to scm_vector elts, functions, &c are not munged.
76 # define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x) >> 3))
77 # define PTR2SCM(x) (SCM_PACK (((scm_t_bits) (x)) << 3))
79 # define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x)))
80 # define PTR2SCM(x) (SCM_PACK ((scm_t_bits) (x)))
81 #endif /* def _UNICOS */
89 #define SCM_GC_CARD_N_HEADER_CELLS 1
90 #define SCM_GC_CARD_N_CELLS 256
92 #define SCM_GC_CARD_GENERATION(card)
93 #define SCM_GC_FLAG_OBJECT_WRITE(x)
95 #define SCM_GC_CARD_BVEC(card) ((scm_t_c_bvec_limb *) ((card)->word_0))
96 #define SCM_GC_SET_CARD_BVEC(card, bvec) \
97 ((card)->word_0 = (scm_t_bits) (bvec))
101 #define SCM_GC_CARD_SIZE (SCM_GC_CARD_N_CELLS * sizeof (scm_t_cell))
102 #define SCM_GC_CARD_N_DATA_CELLS (SCM_GC_CARD_N_CELLS - SCM_GC_CARD_N_HEADER_CELLS)
104 #define SCM_GC_CARD_BVEC_SIZE_IN_LIMBS \
105 ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LIMB_BITS - 1) / SCM_C_BVEC_LIMB_BITS)
107 #define SCM_GC_IN_CARD_HEADERP(x) \
108 SCM_PTR_LT ((scm_t_cell *) (x), SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS)
110 #define SCM_GC_GET_CARD_FLAGS(card) ((long) ((card)->word_1))
111 #define SCM_GC_SET_CARD_FLAGS(card, flags) \
112 ((card)->word_1 = (scm_t_bits) (flags))
113 #define SCM_GC_CLR_CARD_FLAGS(card) (SCM_GC_SET_CARD_FLAGS (card, 0L))
115 #define SCM_GC_GET_CARD_FLAG(card, shift) (SCM_GC_GET_CARD_FLAGS (card) & (1L << (shift)))
116 #define SCM_GC_SET_CARD_FLAG(card, shift) \
117 (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) | (1L << (shift))))
118 #define SCM_GC_CLR_CARD_FLAG(card, shift) \
119 (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) & ~(1L << (shift))))
121 #define SCM_GC_CARDF_DOUBLECELL 0
123 #define SCM_GC_CARD_DOUBLECELLP(card) SCM_GC_GET_CARD_FLAG (card, SCM_GC_CARDF_DOUBLECELL)
124 #define SCM_GC_SET_CARD_DOUBLECELL(card) SCM_GC_SET_CARD_FLAG (card, SCM_GC_CARDF_DOUBLECELL)
126 /* card addressing. for efficiency, cards are *always* aligned to
129 #define SCM_GC_CARD_SIZE_MASK (SCM_GC_CARD_SIZE - 1)
130 #define SCM_GC_CARD_ADDR_MASK (~SCM_GC_CARD_SIZE_MASK)
132 #define SCM_GC_CELL_CARD(x) ((SCM_CELLPTR) ((long) (x) & SCM_GC_CARD_ADDR_MASK))
133 #define SCM_GC_CELL_SPAN(x) ((SCM_GC_CARD_DOUBLECELLP (SCM_GC_CELL_CARD (x))) ? 2 : 1)
134 #define SCM_GC_CELL_OFFSET(x) (((long) (x) & SCM_GC_CARD_SIZE_MASK) >> SCM_CELL_SIZE_SHIFT)
135 #define SCM_GC_CELL_BVEC(x) SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (x))
136 #define SCM_GC_CELL_GET_BIT(x) SCM_C_BVEC_GET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
137 #define SCM_GC_CELL_SET_BIT(x) SCM_C_BVEC_SET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
138 #define SCM_GC_CELL_CLR_BIT(x) SCM_C_BVEC_CLR (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
140 #define SCM_GC_CARD_UP(x) SCM_GC_CELL_CARD ((char *) (x) + SCM_GC_CARD_SIZE - 1)
141 #define SCM_GC_CARD_DOWN SCM_GC_CELL_CARD
143 /* low level bit banging aids */
145 typedef unsigned long scm_t_c_bvec_limb
;
147 #if (SIZEOF_LONG == 8)
148 # define SCM_C_BVEC_LIMB_BITS 64
149 # define SCM_C_BVEC_OFFSET_SHIFT 6
150 # define SCM_C_BVEC_POS_MASK 63
151 # define SCM_CELL_SIZE_SHIFT 4
153 # define SCM_C_BVEC_LIMB_BITS 32
154 # define SCM_C_BVEC_OFFSET_SHIFT 5
155 # define SCM_C_BVEC_POS_MASK 31
156 # define SCM_CELL_SIZE_SHIFT 3
159 #define SCM_C_BVEC_OFFSET(pos) (pos >> SCM_C_BVEC_OFFSET_SHIFT)
161 #define SCM_C_BVEC_GET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] & (1L << (pos & SCM_C_BVEC_POS_MASK)))
162 #define SCM_C_BVEC_SET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] |= (1L << (pos & SCM_C_BVEC_POS_MASK)))
163 #define SCM_C_BVEC_CLR(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] &= ~(1L << (pos & SCM_C_BVEC_POS_MASK)))
165 #define SCM_C_BVEC_BITS2BYTES(bits) \
166 (sizeof (scm_t_c_bvec_limb) * ((((bits) & SCM_C_BVEC_POS_MASK) ? 1L : 0L) + SCM_C_BVEC_OFFSET (bits)))
168 #define SCM_C_BVEC_SET_BYTES(bvec, bytes) (memset (bvec, 0xff, bytes))
169 #define SCM_C_BVEC_SET_ALL_BITS(bvec, bits) SCM_C_BVEC_SET_BYTES (bvec, SCM_C_BVEC_BITS2BYTES (bits))
171 #define SCM_C_BVEC_CLR_BYTES(bvec, bytes) (memset (bvec, 0, bytes))
172 #define SCM_C_BVEC_CLR_ALL_BITS(bvec, bits) SCM_C_BVEC_CLR_BYTES (bvec, SCM_C_BVEC_BITS2BYTES (bits))
174 /* testing and changing GC marks */
176 #define SCM_GCMARKP(x) SCM_GC_CELL_GET_BIT (x)
177 #define SCM_SETGCMARK(x) SCM_GC_CELL_SET_BIT (x)
178 #define SCM_CLRGCMARK(x) SCM_GC_CELL_CLR_BIT (x)
181 /* Low level cell data accessing macros. These macros should only be used
182 * from within code related to garbage collection issues, since they will
183 * never check the cells they are applied to - not even if guile is compiled
184 * in debug mode. In particular these macros will even work for free cells,
185 * which should never be encountered by user code. */
187 #define SCM_GC_CELL_WORD(x, n) \
188 (((const scm_t_bits *) SCM2PTR (x)) [n])
189 #define SCM_GC_CELL_OBJECT(x, n) \
190 (SCM_PACK (((const scm_t_bits *) SCM2PTR (x)) [n]))
191 #define SCM_GC_SET_CELL_WORD(x, n, v) \
192 (((scm_t_bits *) SCM2PTR (x)) [n] = (scm_t_bits) (v))
193 #define SCM_GC_SET_CELL_OBJECT(x, n, v) \
194 (((scm_t_bits *) SCM2PTR (x)) [n] = SCM_UNPACK (v))
195 #define SCM_GC_CELL_TYPE(x) SCM_GC_CELL_WORD (x, 0)
198 /* Except for the garbage collector, no part of guile should ever run over a
199 * free cell. Thus, if guile is compiled in debug mode the SCM_CELL_* and
200 * SCM_SET_CELL_* macros below report an error if they are applied to a free
201 * cell. Some other plausibility checks are also performed. However, if
202 * guile is not compiled in debug mode, there won't be any time penalty at all
203 * when using these macros. */
205 #if (SCM_DEBUG_CELL_ACCESSES == 1)
206 # define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
208 # define SCM_VALIDATE_CELL(cell, expr) (expr)
211 #define SCM_CELL_WORD(x, n) \
212 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_WORD ((x), (n)))
213 #define SCM_CELL_WORD_0(x) SCM_CELL_WORD (x, 0)
214 #define SCM_CELL_WORD_1(x) SCM_CELL_WORD (x, 1)
215 #define SCM_CELL_WORD_2(x) SCM_CELL_WORD (x, 2)
216 #define SCM_CELL_WORD_3(x) SCM_CELL_WORD (x, 3)
218 #define SCM_CELL_OBJECT(x, n) \
219 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_OBJECT ((x), (n)))
220 #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT (x, 0)
221 #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT (x, 1)
222 #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT (x, 2)
223 #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT (x, 3)
225 #define SCM_SET_CELL_WORD(x, n, v) \
226 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_WORD ((x), (n), (v)))
227 #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD (x, 0, v)
228 #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD (x, 1, v)
229 #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD (x, 2, v)
230 #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD (x, 3, v)
232 #define SCM_SET_CELL_OBJECT(x, n, v) \
233 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_OBJECT ((x), (n), (v)))
234 #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT (x, 0, v)
235 #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT (x, 1, v)
236 #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT (x, 2, v)
237 #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT (x, 3, v)
239 #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
240 #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 (x, t)
243 /* Freelists consist of linked cells where the type entry holds the value
244 * scm_tc_free_cell and the second entry holds a pointer to the next cell of
245 * the freelist. Due to this structure, freelist cells are not cons cells
246 * and thus may not be accessed using SCM_CAR and SCM_CDR. */
248 #define SCM_FREE_CELL_P(x) \
249 (!SCM_IMP (x) && (SCM_GC_CELL_TYPE (x) == scm_tc_free_cell))
250 #define SCM_FREE_CELL_CDR(x) \
251 (SCM_GC_CELL_OBJECT ((x), 1))
252 #define SCM_SET_FREE_CELL_CDR(x, v) \
253 (SCM_GC_SET_CELL_OBJECT ((x), 1, (v)))
256 #define SCM_CELL_WORD_LOC(x, n) ((scm_t_bits *) & SCM_CELL_WORD (x, n))
257 #define SCM_CARLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 0))
258 #define SCM_CDRLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 1))
261 /* SCM_PTR_LT and friends define how to compare two SCM_CELLPTRs (which may
262 * point to cells in different heap segments).
264 #define SCM_PTR_LT(x, y) ((x) < (y))
265 #define SCM_PTR_GT(x, y) (SCM_PTR_LT (y, x))
266 #define SCM_PTR_LE(x, y) (!SCM_PTR_GT (x, y))
267 #define SCM_PTR_GE(x, y) (!SCM_PTR_LT (x, y))
270 #define SCM_MARKEDP SCM_GCMARKP
271 #define SCM_NMARKEDP(x) (!SCM_MARKEDP (x))
273 #if (SCM_DEBUG_CELL_ACCESSES == 1)
274 SCM_API
unsigned int scm_debug_cell_accesses_p
;
277 SCM_API
struct scm_t_heap_seg_data
*scm_heap_table
;
278 SCM_API
size_t scm_n_heap_segs
;
279 SCM_API
int scm_block_gc
;
280 SCM_API
int scm_gc_heap_lock
;
281 SCM_API
unsigned int scm_gc_running_p
;
284 SCM_API
size_t scm_default_init_heap_size_1
;
285 SCM_API
int scm_default_min_yield_1
;
286 SCM_API
size_t scm_default_init_heap_size_2
;
287 SCM_API
int scm_default_min_yield_2
;
288 SCM_API
size_t scm_default_max_segment_size
;
290 SCM_API
size_t scm_max_segment_size
;
291 SCM_API SCM_CELLPTR scm_heap_org
;
292 SCM_API SCM scm_freelist
;
293 SCM_API
struct scm_t_freelist scm_master_freelist
;
294 SCM_API SCM scm_freelist2
;
295 SCM_API
struct scm_t_freelist scm_master_freelist2
;
296 SCM_API
unsigned long scm_gc_cells_collected
;
297 SCM_API
unsigned long scm_gc_yield
;
298 SCM_API
unsigned long scm_gc_malloc_collected
;
299 SCM_API
unsigned long scm_gc_ports_collected
;
300 SCM_API
unsigned long scm_cells_allocated
;
301 SCM_API
unsigned long scm_mallocated
;
302 SCM_API
unsigned long scm_mtrigger
;
304 SCM_API SCM scm_after_gc_hook
;
306 SCM_API scm_t_c_hook scm_before_gc_c_hook
;
307 SCM_API scm_t_c_hook scm_before_mark_c_hook
;
308 SCM_API scm_t_c_hook scm_before_sweep_c_hook
;
309 SCM_API scm_t_c_hook scm_after_sweep_c_hook
;
310 SCM_API scm_t_c_hook scm_after_gc_c_hook
;
312 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
313 SCM_API SCM
scm_map_free_list (void);
314 SCM_API SCM
scm_free_list_length (void);
316 #ifdef GUILE_DEBUG_FREELIST
317 SCM_API SCM
scm_gc_set_debug_check_freelist_x (SCM flag
);
322 #if (SCM_DEBUG_CELL_ACCESSES == 1)
323 SCM_API
void scm_assert_cell_valid (SCM
);
324 SCM_API SCM
scm_set_debug_cell_accesses_x (SCM flag
);
326 SCM_API SCM
scm_object_address (SCM obj
);
327 SCM_API SCM
scm_gc_stats (void);
328 SCM_API SCM
scm_gc (void);
329 SCM_API
void scm_gc_for_alloc (struct scm_t_freelist
*freelist
);
330 SCM_API SCM
scm_gc_for_newcell (struct scm_t_freelist
*master
, SCM
*freelist
);
332 SCM_API
void scm_alloc_cluster (struct scm_t_freelist
*master
);
334 SCM_API
void scm_igc (const char *what
);
335 SCM_API
void scm_gc_mark (SCM p
);
336 SCM_API
void scm_gc_mark_dependencies (SCM p
);
337 SCM_API
void scm_mark_locations (SCM_STACKITEM x
[], unsigned long n
);
338 SCM_API
int scm_cellp (SCM value
);
339 SCM_API
void scm_gc_sweep (void);
341 SCM_API
void *scm_malloc (size_t size
);
342 SCM_API
void *scm_realloc (void *mem
, size_t size
);
343 SCM_API
char *scm_strdup (const char *str
);
344 SCM_API
char *scm_strndup (const char *str
, size_t n
);
345 SCM_API
void scm_gc_register_collectable_memory (void *mem
, size_t size
,
347 SCM_API
void scm_gc_unregister_collectable_memory (void *mem
, size_t size
,
349 SCM_API
void *scm_gc_malloc (size_t size
, const char *what
);
350 SCM_API
void *scm_gc_realloc (void *mem
, size_t old_size
,
351 size_t new_size
, const char *what
);
352 SCM_API
void scm_gc_free (void *mem
, size_t size
, const char *what
);
353 SCM_API
char *scm_gc_strdup (const char *str
, const char *what
);
354 SCM_API
char *scm_gc_strndup (const char *str
, size_t n
, const char *what
);
356 SCM_API
void scm_remember_upto_here_1 (SCM obj
);
357 SCM_API
void scm_remember_upto_here_2 (SCM obj1
, SCM obj2
);
358 SCM_API
void scm_remember_upto_here (SCM obj1
, ...);
359 SCM_API SCM
scm_return_first (SCM elt
, ...);
360 SCM_API
int scm_return_first_int (int x
, ...);
361 SCM_API SCM
scm_permanent_object (SCM obj
);
362 SCM_API SCM
scm_gc_protect_object (SCM obj
);
363 SCM_API SCM
scm_gc_unprotect_object (SCM obj
);
364 SCM_API
void scm_gc_register_root (SCM
*p
);
365 SCM_API
void scm_gc_unregister_root (SCM
*p
);
366 SCM_API
void scm_gc_register_roots (SCM
*b
, unsigned long n
);
367 SCM_API
void scm_gc_unregister_roots (SCM
*b
, unsigned long n
);
368 SCM_API
int scm_init_storage (void);
369 SCM_API
void *scm_get_stack_base (void);
370 SCM_API
void scm_init_gc (void);
372 #if SCM_ENABLE_DEPRECATED == 1
374 SCM_API SCM
scm_deprecated_newcell (void);
375 SCM_API SCM
scm_deprecated_newcell2 (void);
377 #define SCM_NEWCELL(_into) \
378 do { _into = scm_deprecated_newcell (); } while (0)
379 #define SCM_NEWCELL2(_into) \
380 do { _into = scm_deprecated_newcell2 (); } while (0)
382 SCM_API
void * scm_must_malloc (size_t len
, const char *what
);
383 SCM_API
void * scm_must_realloc (void *where
,
384 size_t olen
, size_t len
,
386 SCM_API
char *scm_must_strdup (const char *str
);
387 SCM_API
char *scm_must_strndup (const char *str
, size_t n
);
388 SCM_API
void scm_done_malloc (long size
);
389 SCM_API
void scm_done_free (long size
);
390 SCM_API
void scm_must_free (void *obj
);
394 #endif /* SCM_GC_H */