* hashtab.c: Undid thread safety. (We decided that it's better to
[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
9bc4701c 6/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002 Free Software Foundation, Inc.
a00c95d9 7 *
0f2d19dd
JB
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)
11 * any later version.
a00c95d9 12 *
0f2d19dd
JB
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.
a00c95d9 17 *
0f2d19dd
JB
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
82892bed
JB
20 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 * Boston, MA 02111-1307 USA
0f2d19dd
JB
22 *
23 * As a special exception, the Free Software Foundation gives permission
24 * for additional uses of the text contained in its release of GUILE.
25 *
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.
31 *
32 * This exception does not however invalidate any other reasons why
33 * the executable file might be covered by the GNU General Public License.
34 *
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.
42 *
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.
82892bed 45 * If you do not wish that, delete this exception notice. */
d3a6bc94 46
0f2d19dd
JB
47\f
48
b4309c3c 49#include "libguile/__scm.h"
2549a709 50
9b3e180c
MD
51#include "libguile/hooks.h"
52
9bc4701c
MD
53#ifdef USE_PTHREAD_THREADS
54#include "libguile/pthread-threads.h"
55#else
56#include "libguile/null-threads.h"
57#endif
58
0f2d19dd
JB
59\f
60
228a24ef 61typedef struct scm_t_cell
2549a709 62{
92c2555f
MV
63 scm_t_bits word_0;
64 scm_t_bits word_1;
228a24ef 65} scm_t_cell;
2549a709 66
c8a1bdc4
HWN
67/*
68 CARDS
69
70 A card is a small `page' of memory; it will be the unit for lazy
71 sweeping, generations, etc. The first cell of a card contains a
72 pointer to the mark bitvector, so that we can find the bitvector efficiently: we
73 knock off some lowerorder bits.
74
75 The size on a 32 bit machine is 256 cells = 2kb. The card
76*/
2549a709 77
2549a709
DH
78
79
80/* Cray machines have pointers that are incremented once for each word,
81 * rather than each byte, the 3 most significant bits encode the byte
82 * within the word. The following macros deal with this by storing the
83 * native Cray pointers like the ones that looks like scm expects. This
228a24ef 84 * is done for any pointers that might appear in the car of a scm_t_cell,
e618c9a3 85 * pointers to scm_vector elts, functions, &c are not munged.
2549a709
DH
86 */
87#ifdef _UNICOS
c8a1bdc4 88# define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x) >> 3))
92c2555f 89# define PTR2SCM(x) (SCM_PACK (((scm_t_bits) (x)) << 3))
2549a709 90#else
c8a1bdc4 91# define SCM2PTR(x) ((scm_t_cell *) (SCM_UNPACK (x)))
92c2555f 92# define PTR2SCM(x) (SCM_PACK ((scm_t_bits) (x)))
2549a709
DH
93#endif /* def _UNICOS */
94
c8a1bdc4 95
e618c9a3 96#define SCM_GC_CARD_N_HEADER_CELLS 1
f91f77e6 97#define SCM_GC_CARD_N_CELLS 256
c8a1bdc4 98#define SCM_GC_SIZEOF_CARD SCM_GC_CARD_N_CELLS * sizeof (scm_t_cell)
e618c9a3 99
c8a1bdc4 100#define SCM_GC_CARD_BVEC(card) ((scm_t_c_bvec_long *) ((card)->word_0))
34d19ef6
HWN
101#define SCM_GC_SET_CARD_BVEC(card, bvec) \
102 ((card)->word_0 = (scm_t_bits) (bvec))
34d19ef6
HWN
103
104
c014a02e 105#define SCM_GC_GET_CARD_FLAGS(card) ((long) ((card)->word_1))
322ec19d 106#define SCM_GC_SET_CARD_FLAGS(card, flags) \
92c2555f 107 ((card)->word_1 = (scm_t_bits) (flags))
c8a1bdc4 108#define SCM_GC_CLEAR_CARD_FLAGS(card) (SCM_GC_SET_CARD_FLAGS (card, 0L))
e618c9a3
ML
109
110#define SCM_GC_GET_CARD_FLAG(card, shift) (SCM_GC_GET_CARD_FLAGS (card) & (1L << (shift)))
322ec19d
ML
111#define SCM_GC_SET_CARD_FLAG(card, shift) \
112 (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) | (1L << (shift))))
c8a1bdc4 113#define SCM_GC_CLEAR_CARD_FLAG(card, shift) \
322ec19d 114 (SCM_GC_SET_CARD_FLAGS (card, SCM_GC_GET_CARD_FLAGS(card) & ~(1L << (shift))))
e618c9a3 115
1383773b
HWN
116/*
117 Remove card flags. They hamper lazy initialization, and aren't used
118 anyways.
119 */
e618c9a3
ML
120
121/* card addressing. for efficiency, cards are *always* aligned to
122 SCM_GC_CARD_SIZE. */
123
c8a1bdc4 124#define SCM_GC_CARD_SIZE_MASK (SCM_GC_CARD_N_CELLS * sizeof (scm_t_cell) - 1)
e618c9a3
ML
125#define SCM_GC_CARD_ADDR_MASK (~SCM_GC_CARD_SIZE_MASK)
126
c8a1bdc4 127#define SCM_GC_CELL_CARD(x) ((scm_t_cell *) ((long) (x) & SCM_GC_CARD_ADDR_MASK))
c014a02e 128#define SCM_GC_CELL_OFFSET(x) (((long) (x) & SCM_GC_CARD_SIZE_MASK) >> SCM_CELL_SIZE_SHIFT)
e618c9a3
ML
129#define SCM_GC_CELL_BVEC(x) SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (x))
130#define SCM_GC_CELL_GET_BIT(x) SCM_C_BVEC_GET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
131#define SCM_GC_CELL_SET_BIT(x) SCM_C_BVEC_SET (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
c8a1bdc4 132#define SCM_GC_CELL_CLEAR_BIT(x) SCM_C_BVEC_CLEAR (SCM_GC_CELL_BVEC (x), SCM_GC_CELL_OFFSET (x))
e618c9a3 133
c8a1bdc4 134#define SCM_GC_CARD_UP(x) SCM_GC_CELL_CARD ((char *) (x) + SCM_GC_SIZEOF_CARD - 1)
e618c9a3
ML
135#define SCM_GC_CARD_DOWN SCM_GC_CELL_CARD
136
137/* low level bit banging aids */
c8a1bdc4 138typedef unsigned long scm_t_c_bvec_long;
e618c9a3
ML
139
140#if (SIZEOF_LONG == 8)
c8a1bdc4 141# define SCM_C_BVEC_LONG_BITS 64
e618c9a3
ML
142# define SCM_C_BVEC_OFFSET_SHIFT 6
143# define SCM_C_BVEC_POS_MASK 63
144# define SCM_CELL_SIZE_SHIFT 4
c8a1bdc4 145# define SCM_SIZEOF_LONG SIZEOF_LONG
e618c9a3 146#else
c8a1bdc4
HWN
147# define SCM_C_BVEC_LONG_BITS 32
148# define SCM_SIZEOF_LONG SIZEOF_LONG
e618c9a3
ML
149# define SCM_C_BVEC_OFFSET_SHIFT 5
150# define SCM_C_BVEC_POS_MASK 31
151# define SCM_CELL_SIZE_SHIFT 3
152#endif
153
154#define SCM_C_BVEC_OFFSET(pos) (pos >> SCM_C_BVEC_OFFSET_SHIFT)
155
156#define SCM_C_BVEC_GET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] & (1L << (pos & SCM_C_BVEC_POS_MASK)))
157#define SCM_C_BVEC_SET(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] |= (1L << (pos & SCM_C_BVEC_POS_MASK)))
c8a1bdc4 158#define SCM_C_BVEC_CLEAR(bvec, pos) (bvec[SCM_C_BVEC_OFFSET (pos)] &= ~(1L << (pos & SCM_C_BVEC_POS_MASK)))
e618c9a3
ML
159
160/* testing and changing GC marks */
c8a1bdc4
HWN
161#define SCM_GC_MARK_P(x) SCM_GC_CELL_GET_BIT (x)
162#define SCM_SET_GC_MARK(x) SCM_GC_CELL_SET_BIT (x)
163#define SCM_CLEAR_GC_MARK(x) SCM_GC_CELL_CLEAR_BIT (x)
1fc8902f
DH
164
165/* Low level cell data accessing macros. These macros should only be used
166 * from within code related to garbage collection issues, since they will
167 * never check the cells they are applied to - not even if guile is compiled
168 * in debug mode. In particular these macros will even work for free cells,
169 * which should never be encountered by user code. */
170
171#define SCM_GC_CELL_WORD(x, n) \
172 (((const scm_t_bits *) SCM2PTR (x)) [n])
173#define SCM_GC_CELL_OBJECT(x, n) \
174 (SCM_PACK (((const scm_t_bits *) SCM2PTR (x)) [n]))
175#define SCM_GC_SET_CELL_WORD(x, n, v) \
176 (((scm_t_bits *) SCM2PTR (x)) [n] = (scm_t_bits) (v))
177#define SCM_GC_SET_CELL_OBJECT(x, n, v) \
c8a1bdc4 178 (((scm_t_bits *) SCM2PTR (x)) [n] = SCM_UNPACK (v))
1fc8902f
DH
179#define SCM_GC_CELL_TYPE(x) SCM_GC_CELL_WORD (x, 0)
180
181
182/* Except for the garbage collector, no part of guile should ever run over a
183 * free cell. Thus, if guile is compiled in debug mode the SCM_CELL_* and
184 * SCM_SET_CELL_* macros below report an error if they are applied to a free
185 * cell. Some other plausibility checks are also performed. However, if
186 * guile is not compiled in debug mode, there won't be any time penalty at all
187 * when using these macros. */
2549a709 188
406c7d90
DH
189#if (SCM_DEBUG_CELL_ACCESSES == 1)
190# define SCM_VALIDATE_CELL(cell, expr) (scm_assert_cell_valid (cell), (expr))
708cb87c 191#else
61045190 192# define SCM_VALIDATE_CELL(cell, expr) (expr)
708cb87c 193#endif
46d53380 194
f706a58b 195#define SCM_CELL_WORD(x, n) \
1fc8902f 196 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_WORD ((x), (n)))
2549a709
DH
197#define SCM_CELL_WORD_0(x) SCM_CELL_WORD (x, 0)
198#define SCM_CELL_WORD_1(x) SCM_CELL_WORD (x, 1)
199#define SCM_CELL_WORD_2(x) SCM_CELL_WORD (x, 2)
200#define SCM_CELL_WORD_3(x) SCM_CELL_WORD (x, 3)
201
f706a58b 202#define SCM_CELL_OBJECT(x, n) \
1fc8902f 203 SCM_VALIDATE_CELL ((x), SCM_GC_CELL_OBJECT ((x), (n)))
2549a709
DH
204#define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT (x, 0)
205#define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT (x, 1)
206#define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT (x, 2)
207#define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT (x, 3)
208
f706a58b 209#define SCM_SET_CELL_WORD(x, n, v) \
1fc8902f 210 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_WORD ((x), (n), (v)))
2549a709
DH
211#define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD (x, 0, v)
212#define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD (x, 1, v)
213#define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD (x, 2, v)
214#define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD (x, 3, v)
215
f706a58b 216#define SCM_SET_CELL_OBJECT(x, n, v) \
1fc8902f 217 SCM_VALIDATE_CELL ((x), SCM_GC_SET_CELL_OBJECT ((x), (n), (v)))
2549a709
DH
218#define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT (x, 0, v)
219#define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT (x, 1, v)
220#define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT (x, 2, v)
221#define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT (x, 3, v)
222
223#define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
224#define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 (x, t)
225
61045190 226
eab1b259 227
1fc8902f
DH
228/* Freelists consist of linked cells where the type entry holds the value
229 * scm_tc_free_cell and the second entry holds a pointer to the next cell of
230 * the freelist. Due to this structure, freelist cells are not cons cells
231 * and thus may not be accessed using SCM_CAR and SCM_CDR. */
61045190 232
c8a1bdc4
HWN
233/*
234 SCM_FREECELL_P removed ; the semantics are ambiguous with lazy
235 sweeping. Could mean "this cell is no longer in use (will be swept)"
236 or "this cell has just been swept, and is not yet in use".
237 */
238
239#define SCM_FREECELL_P this_macro_has_been_removed_see_gc_header_file
240
1fc8902f
DH
241#define SCM_FREE_CELL_CDR(x) \
242 (SCM_GC_CELL_OBJECT ((x), 1))
243#define SCM_SET_FREE_CELL_CDR(x, v) \
244 (SCM_GC_SET_CELL_OBJECT ((x), 1, (v)))
61045190
DH
245
246
92c2555f 247#define SCM_CELL_WORD_LOC(x, n) ((scm_t_bits *) & SCM_CELL_WORD (x, n))
f706a58b
DH
248#define SCM_CARLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 0))
249#define SCM_CDRLOC(x) ((SCM *) SCM_CELL_WORD_LOC ((x), 1))
2549a709
DH
250
251
2549a709 252
79e3f9e2 253
61045190 254#if (SCM_DEBUG_CELL_ACCESSES == 1)
eab1b259
HWN
255/* Set this to != 0 if every cell that is accessed shall be checked:
256 */
257SCM_API int scm_debug_cell_accesses_p;
258SCM_API int scm_expensive_debug_cell_accesses_p;
259SCM_API int scm_debug_cells_gc_interval ;
1e71eafb 260void scm_i_expensive_validation_check (SCM cell);
61045190
DH
261#endif
262
33b001fd
MV
263SCM_API int scm_block_gc;
264SCM_API int scm_gc_heap_lock;
265SCM_API unsigned int scm_gc_running_p;
fb50ef08 266extern scm_t_rec_mutex scm_i_sweep_mutex;
0f2d19dd
JB
267\f
268
c8a1bdc4 269#if (SCM_ENABLE_DEPRECATED == 1)
33b001fd
MV
270SCM_API size_t scm_default_init_heap_size_1;
271SCM_API int scm_default_min_yield_1;
272SCM_API size_t scm_default_init_heap_size_2;
273SCM_API int scm_default_min_yield_2;
274SCM_API size_t scm_default_max_segment_size;
c8a1bdc4
HWN
275#else
276#define scm_default_init_heap_size_1 deprecated
277#define scm_default_min_yield_1 deprecated
278#define scm_default_init_heap_size_2 deprecated
279#define scm_default_min_yield_2 deprecated
280#define scm_default_max_segment_size deprecated
281#endif
282
33b001fd
MV
283
284SCM_API size_t scm_max_segment_size;
c8a1bdc4 285
9bc4701c
MD
286#define SCM_FREELIST_CREATE(key) \
287 do { SCM *ls = (SCM *) malloc (sizeof (SCM)); \
288 *ls = SCM_EOL; \
289 scm_setspecific ((key), ls); } while (0)
290#define SCM_FREELIST_LOC(key) ((SCM *) scm_getspecific (key))
291extern scm_t_key scm_i_freelist;
292extern scm_t_key scm_i_freelist2;
eab1b259 293extern struct scm_t_cell_type_statistics scm_i_master_freelist;
eab1b259
HWN
294extern struct scm_t_cell_type_statistics scm_i_master_freelist2;
295
c8a1bdc4
HWN
296
297SCM_API unsigned long scm_gc_cells_swept;
298SCM_API unsigned long scm_gc_cells_collected;
33b001fd 299SCM_API unsigned long scm_gc_cells_collected;
33b001fd
MV
300SCM_API unsigned long scm_gc_malloc_collected;
301SCM_API unsigned long scm_gc_ports_collected;
f2893a25 302SCM_API unsigned long scm_cells_allocated;
c2cbcc57
HWN
303SCM_API int scm_gc_cell_yield_percentage;
304SCM_API int scm_gc_malloc_yield_percentage;
33b001fd
MV
305SCM_API unsigned long scm_mallocated;
306SCM_API unsigned long scm_mtrigger;
307
c8a1bdc4
HWN
308
309
33b001fd
MV
310SCM_API SCM scm_after_gc_hook;
311
312SCM_API scm_t_c_hook scm_before_gc_c_hook;
313SCM_API scm_t_c_hook scm_before_mark_c_hook;
314SCM_API scm_t_c_hook scm_before_sweep_c_hook;
315SCM_API scm_t_c_hook scm_after_sweep_c_hook;
316SCM_API scm_t_c_hook scm_after_gc_c_hook;
9b3e180c 317
bb2c57fa 318#if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
c8a1bdc4
HWN
319#define scm_map_free_list deprecated
320#define scm_free_list_length deprecated
bb2c57fa 321#endif
c8a1bdc4
HWN
322
323#if (SCM_ENABLE_DEPRECATED == 1) && defined (GUILE_DEBUG_FREELIST)
33b001fd 324SCM_API SCM scm_gc_set_debug_check_freelist_x (SCM flag);
88256b2e 325#endif
0f2d19dd 326\f
0f2d19dd 327
61045190 328#if (SCM_DEBUG_CELL_ACCESSES == 1)
33b001fd 329SCM_API void scm_assert_cell_valid (SCM);
61045190 330#endif
c8a1bdc4
HWN
331
332SCM_API SCM scm_set_debug_cell_accesses_x (SCM flag);
333
334
33b001fd 335SCM_API SCM scm_object_address (SCM obj);
33b001fd
MV
336SCM_API SCM scm_gc_stats (void);
337SCM_API SCM scm_gc (void);
c8a1bdc4
HWN
338SCM_API void scm_gc_for_alloc (struct scm_t_cell_type_statistics *freelist);
339SCM_API SCM scm_gc_for_newcell (struct scm_t_cell_type_statistics *master, SCM *freelist);
33b001fd
MV
340SCM_API void scm_igc (const char *what);
341SCM_API void scm_gc_mark (SCM p);
342SCM_API void scm_gc_mark_dependencies (SCM p);
343SCM_API void scm_mark_locations (SCM_STACKITEM x[], unsigned long n);
c8a1bdc4 344SCM_API int scm_in_heap_p (SCM value);
33b001fd 345SCM_API void scm_gc_sweep (void);
4c9419ac
MV
346
347SCM_API void *scm_malloc (size_t size);
ba1b2226 348SCM_API void *scm_calloc (size_t size);
4c9419ac
MV
349SCM_API void *scm_realloc (void *mem, size_t size);
350SCM_API char *scm_strdup (const char *str);
351SCM_API char *scm_strndup (const char *str, size_t n);
352SCM_API void scm_gc_register_collectable_memory (void *mem, size_t size,
353 const char *what);
354SCM_API void scm_gc_unregister_collectable_memory (void *mem, size_t size,
355 const char *what);
39e8f371 356SCM_API void *scm_gc_calloc (size_t size, const char *what);
4c9419ac
MV
357SCM_API void *scm_gc_malloc (size_t size, const char *what);
358SCM_API void *scm_gc_realloc (void *mem, size_t old_size,
359 size_t new_size, const char *what);
360SCM_API void scm_gc_free (void *mem, size_t size, const char *what);
361SCM_API char *scm_gc_strdup (const char *str, const char *what);
362SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what);
363
33b001fd
MV
364SCM_API void scm_remember_upto_here_1 (SCM obj);
365SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);
366SCM_API void scm_remember_upto_here (SCM obj1, ...);
367SCM_API SCM scm_return_first (SCM elt, ...);
368SCM_API int scm_return_first_int (int x, ...);
369SCM_API SCM scm_permanent_object (SCM obj);
370SCM_API SCM scm_gc_protect_object (SCM obj);
371SCM_API SCM scm_gc_unprotect_object (SCM obj);
372SCM_API void scm_gc_register_root (SCM *p);
373SCM_API void scm_gc_unregister_root (SCM *p);
374SCM_API void scm_gc_register_roots (SCM *b, unsigned long n);
375SCM_API void scm_gc_unregister_roots (SCM *b, unsigned long n);
376SCM_API int scm_init_storage (void);
377SCM_API void *scm_get_stack_base (void);
378SCM_API void scm_init_gc (void);
d1ca2c64 379
d678e25c
MV
380#if SCM_ENABLE_DEPRECATED == 1
381
382SCM_API SCM scm_deprecated_newcell (void);
383SCM_API SCM scm_deprecated_newcell2 (void);
384
385#define SCM_NEWCELL(_into) \
386 do { _into = scm_deprecated_newcell (); } while (0)
387#define SCM_NEWCELL2(_into) \
388 do { _into = scm_deprecated_newcell2 (); } while (0)
389
539b08a4
MV
390SCM_API void * scm_must_malloc (size_t len, const char *what);
391SCM_API void * scm_must_realloc (void *where,
392 size_t olen, size_t len,
393 const char *what);
394SCM_API char *scm_must_strdup (const char *str);
395SCM_API char *scm_must_strndup (const char *str, size_t n);
396SCM_API void scm_done_malloc (long size);
397SCM_API void scm_done_free (long size);
398SCM_API void scm_must_free (void *obj);
399
d678e25c
MV
400#endif
401
61045190 402#endif /* SCM_GC_H */
89e00824
ML
403
404/*
405 Local Variables:
406 c-file-style: "gnu"
407 End:
408*/