5 /* Copyright (C) 1995, 96, 98, 99, 2000 Free Software Foundation, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this software; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307 USA
22 * As a special exception, the Free Software Foundation gives permission
23 * for additional uses of the text contained in its release of GUILE.
25 * The exception is that, if you link the GUILE library with other files
26 * to produce an executable, this does not by itself cause the
27 * resulting executable to be covered by the GNU General Public License.
28 * Your use of that executable is in no way restricted on account of
29 * linking the GUILE library code into it.
31 * This exception does not however invalidate any other reasons why
32 * the executable file might be covered by the GNU General Public License.
34 * This exception applies only to the code released by the
35 * Free Software Foundation under the name GUILE. If you copy
36 * code from other Free Software Foundation releases into a copy of
37 * GUILE, as the General Public License permits, the exception does
38 * not apply to the code that you add in this way. To avoid misleading
39 * anyone as to the status of such modified files, you must delete
40 * this exception notice from them.
42 * If you write modifications of your own for GUILE, it is your choice
43 * whether to permit this exception to apply to your modifications.
44 * If you do not wish that, delete this exception notice. */
46 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
47 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
50 #include "libguile/__scm.h"
54 typedef struct scm_cell
61 /* SCM_CELLPTR is a pointer to a cons cell which may be compared or
64 #if !defined (__TURBOC__) || defined (__TOS__) || defined (PROT386)
65 typedef scm_cell
* SCM_CELLPTR
;
67 typedef scm_cell huge
* SCM_CELLPTR
;
71 /* Cray machines have pointers that are incremented once for each word,
72 * rather than each byte, the 3 most significant bits encode the byte
73 * within the word. The following macros deal with this by storing the
74 * native Cray pointers like the ones that looks like scm expects. This
75 * is done for any pointers that might appear in the car of a scm_cell,
76 * pointers to scm_vector elts, functions, &c are not munged.
79 #define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x) >> 3))
80 #define PTR2SCM(x) (SCM_PACK (((scm_bits_t) (x)) << 3))
82 #define SCM2PTR(x) ((SCM_CELLPTR) (SCM_UNPACK (x)))
83 #define PTR2SCM(x) (SCM_PACK ((scm_bits_t) (x)))
84 #endif /* def _UNICOS */
87 /* Low level cell data accessing macros:
90 #define SCM_CELL_WORD(x, n) (SCM_UNPACK (((SCM *) SCM2PTR (x))[n]))
91 #define SCM_CELL_WORD_0(x) SCM_CELL_WORD (x, 0)
92 #define SCM_CELL_WORD_1(x) SCM_CELL_WORD (x, 1)
93 #define SCM_CELL_WORD_2(x) SCM_CELL_WORD (x, 2)
94 #define SCM_CELL_WORD_3(x) SCM_CELL_WORD (x, 3)
96 #define SCM_CELL_OBJECT(x, n) (((SCM *) SCM2PTR (x))[n])
97 #define SCM_CELL_OBJECT_0(x) SCM_CELL_OBJECT (x, 0)
98 #define SCM_CELL_OBJECT_1(x) SCM_CELL_OBJECT (x, 1)
99 #define SCM_CELL_OBJECT_2(x) SCM_CELL_OBJECT (x, 2)
100 #define SCM_CELL_OBJECT_3(x) SCM_CELL_OBJECT (x, 3)
102 #define SCM_SET_CELL_WORD(x, n, v) ((((SCM *) SCM2PTR (x))[n]) = SCM_PACK (v))
103 #define SCM_SET_CELL_WORD_0(x, v) SCM_SET_CELL_WORD (x, 0, v)
104 #define SCM_SET_CELL_WORD_1(x, v) SCM_SET_CELL_WORD (x, 1, v)
105 #define SCM_SET_CELL_WORD_2(x, v) SCM_SET_CELL_WORD (x, 2, v)
106 #define SCM_SET_CELL_WORD_3(x, v) SCM_SET_CELL_WORD (x, 3, v)
108 #define SCM_SET_CELL_OBJECT(x, n, v) ((((SCM *) SCM2PTR (x))[n]) = v)
109 #define SCM_SET_CELL_OBJECT_0(x, v) SCM_SET_CELL_OBJECT (x, 0, v)
110 #define SCM_SET_CELL_OBJECT_1(x, v) SCM_SET_CELL_OBJECT (x, 1, v)
111 #define SCM_SET_CELL_OBJECT_2(x, v) SCM_SET_CELL_OBJECT (x, 2, v)
112 #define SCM_SET_CELL_OBJECT_3(x, v) SCM_SET_CELL_OBJECT (x, 3, v)
114 #define SCM_CELL_TYPE(x) SCM_CELL_WORD_0 (x)
115 #define SCM_SET_CELL_TYPE(x, t) SCM_SET_CELL_WORD_0 (x, t)
117 #define SCM_SETAND_CAR(x, y)\
118 (SCM_CAR (x) = SCM_PACK (SCM_UNPACK (SCM_CAR (x)) & (y)))
119 #define SCM_SETAND_CDR(x, y)\
120 (SCM_CDR (x) = SCM_PACK (SCM_UNPACK (SCM_CDR (x)) & (y)))
121 #define SCM_SETOR_CAR(x, y)\
122 (SCM_CAR (x) = SCM_PACK (SCM_UNPACK (SCM_CAR (x)) | (y)))
123 #define SCM_SETOR_CDR(x, y)\
124 (SCM_CDR (x) = SCM_PACK (SCM_UNPACK (SCM_CDR (x)) | (y)))
126 #define SCM_CELL_WORD_LOC(x, n) (&SCM_CELL_WORD (x, n))
127 #define SCM_CARLOC(x) (&SCM_CAR (x))
128 #define SCM_CDRLOC(x) (&SCM_CDR (x))
131 /* SCM_PTR_LT defines how to compare two SCM_CELLPTRs (which may not be in the
135 #if !defined(__TURBOC__) || defined(__TOS__)
137 #define SCM_PTR_MASK 0xffffffffffff
138 #define SCM_PTR_LT(x, y)\
139 (((int) (x) & SCM_PTR_MASK) < ((int) (y) & SCM_PTR_MASK))
141 #define SCM_PTR_LT(x, y) ((x) < (y))
142 #endif /* def nosve */
143 #else /* defined(__TURBOC__) && !defined(__TOS__) */
145 #define SCM_PTR_LT(x, y) (((long) (x)) < ((long) (y)))
147 #define SCM_PTR_LT(x, y) ((x) < (y))
148 #endif /* def PROT386 */
149 #endif /* defined(__TURBOC__) && !defined(__TOS__) */
151 #define SCM_PTR_GT(x, y) SCM_PTR_LT (y, x)
152 #define SCM_PTR_LE(x, y) (!SCM_PTR_GT (x, y))
153 #define SCM_PTR_GE(x, y) (!SCM_PTR_LT (x, y))
156 /* the allocated thing: The car of newcells are set to
157 scm_tc16_allocated to avoid the fragile state of newcells wrt the
158 gc. If it stays as a freecell, any allocation afterwards could
159 cause the cell to go back on the freelist, which will bite you
160 sometime afterwards */
162 #ifdef GUILE_DEBUG_FREELIST
163 #define SCM_NEWCELL(_into) do { _into = scm_debug_newcell (); } while (0)
164 #define SCM_NEWCELL2(_into) do { _into = scm_debug_newcell2 (); } while (0)
166 #ifdef GUILE_NEW_GC_SCHEME
167 /* When we introduce POSIX threads support, every thread will have
168 a freelist of its own. Then it won't any longer be necessary to
169 initialize cells with scm_tc16_allocated. */
170 #define SCM_NEWCELL(_into) \
172 if (SCM_IMP (scm_freelist)) \
173 _into = scm_gc_for_newcell (&scm_master_freelist, \
177 _into = scm_freelist; \
178 scm_freelist = SCM_CDR (scm_freelist);\
179 SCM_SETCAR (_into, scm_tc16_allocated); \
182 #define SCM_NEWCELL2(_into) \
184 if (SCM_IMP (scm_freelist2)) \
185 _into = scm_gc_for_newcell (&scm_master_freelist2, \
189 _into = scm_freelist2; \
190 scm_freelist2 = SCM_CDR (scm_freelist2);\
191 SCM_SETCAR (_into, scm_tc16_allocated); \
194 #else /* GUILE_NEW_GC_SCHEME */
195 #define SCM_NEWCELL(_into) \
197 if (SCM_IMP (scm_freelist.cells)) \
198 _into = scm_gc_for_newcell (&scm_freelist);\
201 _into = scm_freelist.cells; \
202 scm_freelist.cells = SCM_CDR (scm_freelist.cells);\
203 SCM_SETCAR (_into, scm_tc16_allocated); \
204 ++scm_cells_allocated; \
207 #define SCM_NEWCELL2(_into) \
209 if (SCM_IMP (scm_freelist2.cells)) \
210 _into = scm_gc_for_newcell (&scm_freelist2);\
213 _into = scm_freelist2.cells; \
214 scm_freelist2.cells = SCM_CDR (scm_freelist2.cells);\
215 SCM_SETCAR (_into, scm_tc16_allocated); \
216 scm_cells_allocated += 2; \
219 #endif /* GUILE_NEW_GC_SCHEME */
223 #define SCM_FREEP(x) (SCM_NIMP(x) && SCM_UNPACK_CAR (x)==scm_tc_free_cell)
224 #define SCM_NFREEP(x) (!SCM_FREEP(x))
226 /* 1. This shouldn't be used on immediates.
227 2. It thinks that subrs are always unmarked (harmless). */
228 #define SCM_MARKEDP(x) ((SCM_UNPACK_CAR (x) & 5) == 5 \
231 #define SCM_NMARKEDP(x) (!SCM_MARKEDP(x))
233 extern struct scm_heap_seg_data_t
*scm_heap_table
;
234 extern int scm_n_heap_segs
;
235 extern int scm_take_stdin
;
236 extern int scm_block_gc
;
237 extern int scm_gc_heap_lock
;
240 extern scm_sizet scm_max_segment_size
;
241 extern SCM_CELLPTR scm_heap_org
;
242 #ifdef GUILE_NEW_GC_SCHEME
243 extern SCM scm_freelist
;
244 extern struct scm_freelist_t scm_master_freelist
;
245 extern SCM scm_freelist2
;
246 extern struct scm_freelist_t scm_master_freelist2
;
248 extern struct scm_freelist_t scm_freelist
;
249 extern struct scm_freelist_t scm_freelist2
;
251 extern unsigned long scm_gc_cells_collected
;
252 #ifdef GUILE_NEW_GC_SCHEME
253 extern unsigned long scm_gc_yield
;
255 extern unsigned long scm_gc_malloc_collected
;
256 extern unsigned long scm_gc_ports_collected
;
257 extern unsigned long scm_cells_allocated
;
258 extern long scm_mallocated
;
259 extern unsigned long scm_mtrigger
;
261 #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST)
262 extern SCM
scm_map_free_list (void);
263 extern SCM
scm_free_list_length (void);
265 #ifdef GUILE_DEBUG_FREELIST
266 extern SCM
scm_debug_newcell (void);
267 extern SCM
scm_debug_newcell2 (void);
268 extern SCM
scm_gc_set_debug_check_freelist_x (SCM flag
);
273 extern SCM
scm_object_address (SCM obj
);
274 extern SCM
scm_unhash_name (SCM name
);
275 extern SCM
scm_gc_stats (void);
276 extern void scm_gc_start (const char *what
);
277 extern void scm_gc_end (void);
278 extern SCM
scm_gc (void);
279 extern void scm_gc_for_alloc (struct scm_freelist_t
*freelist
);
280 #ifdef GUILE_NEW_GC_SCHEME
281 extern SCM
scm_gc_for_newcell (struct scm_freelist_t
*master
, SCM
*freelist
);
283 extern void scm_alloc_cluster (struct scm_freelist_t
*master
);
286 extern SCM
scm_gc_for_newcell (struct scm_freelist_t
*freelist
);
288 extern void scm_igc (const char *what
);
289 extern void scm_gc_mark (SCM p
);
290 extern void scm_mark_locations (SCM_STACKITEM x
[], scm_sizet n
);
291 extern int scm_cellp (SCM value
);
292 extern void scm_gc_sweep (void);
293 extern void * scm_must_malloc (scm_sizet len
, const char *what
);
294 extern void * scm_must_realloc (void *where
,
295 scm_sizet olen
, scm_sizet len
,
297 extern void scm_done_malloc (long size
);
298 extern void scm_must_free (void *obj
);
299 extern void scm_remember (SCM
* ptr
);
300 extern SCM
scm_return_first (SCM elt
, ...);
301 extern int scm_return_first_int (int x
, ...);
302 extern SCM
scm_permanent_object (SCM obj
);
303 extern SCM
scm_protect_object (SCM obj
);
304 extern SCM
scm_unprotect_object (SCM obj
);
305 #ifdef GUILE_NEW_GC_SCHEME
306 extern int scm_init_storage (scm_sizet init_heap_size
, int trig
,
307 scm_sizet init_heap2_size
, int trig2
,
308 scm_sizet max_segment_size
);
310 extern int scm_init_storage (scm_sizet init_heap_size
,
311 scm_sizet init_heap2_size
);
313 extern void scm_init_gc (void);