1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
45 #include "libguile/_scm.h"
46 #include "libguile/eval.h"
47 #include "libguile/stime.h"
48 #include "libguile/stackchk.h"
49 #include "libguile/struct.h"
50 #include "libguile/smob.h"
51 #include "libguile/unif.h"
52 #include "libguile/async.h"
53 #include "libguile/ports.h"
54 #include "libguile/root.h"
55 #include "libguile/strings.h"
56 #include "libguile/vectors.h"
57 #include "libguile/weaks.h"
58 #include "libguile/hashtab.h"
59 #include "libguile/tags.h"
60 #include "libguile/private-gc.h"
61 #include "libguile/validate.h"
62 #include "libguile/deprecation.h"
63 #include "libguile/gc.h"
66 #include "libguile/private-gc.h"
68 long int scm_i_deprecated_memory_return
;
71 /* During collection, this accumulates structures which are to be freed.
73 SCM scm_i_structs_to_free
;
77 Init all the free cells in CARD, prepending to *FREE_LIST.
79 Return: number of free cells found in this card.
81 It would be cleaner to have a separate function sweep_value(), but
82 that is too slow (functions with switch statements can't be
88 scm_i_sweep_card (scm_t_cell
* p
, SCM
*free_list
, scm_t_heap_segment
*seg
)
89 #define FUNC_NAME "sweep_card"
91 scm_t_c_bvec_long
*bitvec
= SCM_GC_CARD_BVEC(p
);
92 scm_t_cell
* end
= p
+ SCM_GC_CARD_N_CELLS
;
94 int offset
=SCM_MAX (SCM_GC_CARD_N_HEADER_CELLS
, span
);
100 I tried something fancy with shifting by one bit every word from
101 the bitvec in turn, but it wasn't any faster, but quite bit
104 for (p
+= offset
; p
< end
; p
+= span
, offset
+= span
)
106 SCM scmptr
= PTR2SCM(p
);
107 if (SCM_C_BVEC_GET (bitvec
, offset
))
110 switch (SCM_TYP7 (scmptr
))
114 /* Structs need to be freed in a special order.
115 * This is handled by GC C hooks in struct.c.
117 SCM_SET_STRUCT_GC_CHAIN (p
, scm_i_structs_to_free
);
118 scm_i_structs_to_free
= scmptr
;
122 case scm_tcs_cons_imcar
:
123 case scm_tcs_cons_nimcar
:
124 case scm_tcs_closures
:
130 unsigned long int length
= SCM_VECTOR_LENGTH (scmptr
);
133 scm_gc_free (SCM_VECTOR_BASE (scmptr
),
134 length
* sizeof (scm_t_bits
),
141 scm_gc_free (SCM_CCLO_BASE (scmptr
),
142 SCM_CCLO_LENGTH (scmptr
) * sizeof (SCM
),
149 unsigned long int length
= SCM_BITVECTOR_LENGTH (scmptr
);
152 scm_gc_free (SCM_BITVECTOR_BASE (scmptr
),
154 * ((length
+SCM_LONG_BIT
-1) / SCM_LONG_BIT
)),
163 #ifdef HAVE_LONG_LONGS
169 scm_gc_free (SCM_UVECTOR_BASE (scmptr
),
170 (SCM_UVECTOR_LENGTH (scmptr
)
171 * scm_uniform_element_size (scmptr
)),
176 scm_gc_free (SCM_STRING_CHARS (scmptr
),
177 SCM_STRING_LENGTH (scmptr
) + 1, "string");
180 scm_gc_free (SCM_SYMBOL_CHARS (scmptr
),
181 SCM_SYMBOL_LENGTH (scmptr
) + 1, "symbol");
183 case scm_tc7_variable
:
186 /* the various "subrs" (primitives) are never freed */
189 if SCM_OPENP (scmptr
)
191 int k
= SCM_PTOBNUM (scmptr
);
193 #if (SCM_DEBUG_CELL_ACCESSES == 1)
194 if (!(k
< scm_numptob
))
196 fprintf (stderr
, "undefined port type");
200 /* Keep "revealed" ports alive. */
201 if (scm_revealed_count (scmptr
) > 0)
204 /* Yes, I really do mean scm_ptobs[k].free */
205 /* rather than ftobs[k].close. .close */
206 /* is for explicit CLOSE-PORT by user */
207 mm
= scm_ptobs
[k
].free (scmptr
);
211 #if SCM_ENABLE_DEPRECATED == 1
212 scm_c_issue_deprecation_warning
213 ("Returning non-0 from a port free function is "
214 "deprecated. Use scm_gc_free et al instead.");
215 scm_c_issue_deprecation_warning_fmt
216 ("(You just returned non-0 while freeing a %s.)",
218 scm_i_deprecated_memory_return
+= mm
;
224 SCM_SETSTREAM (scmptr
, 0);
225 scm_remove_from_port_table (scmptr
);
226 scm_gc_ports_collected
++;
227 SCM_CLR_PORT_OPEN_FLAG (scmptr
);
231 switch SCM_TYP16 (scmptr
)
233 case scm_tc_free_cell
:
238 scm_gc_free (SCM_BDIGITS (scmptr
),
239 ((SCM_NUMDIGS (scmptr
) * SCM_BITSPERDIG
240 / SCM_CHAR_BIT
)), "bignum");
242 #endif /* def SCM_BIGDIG */
243 case scm_tc16_complex
:
244 scm_gc_free (SCM_COMPLEX_MEM (scmptr
), 2*sizeof (double),
250 k
= SCM_SMOBNUM (scmptr
);
251 #if (SCM_DEBUG_CELL_ACCESSES == 1)
252 if (!(k
< scm_numsmob
))
254 fprintf (stderr
, "undefined smob type");
258 if (scm_smobs
[k
].free
)
261 mm
= scm_smobs
[k
].free (scmptr
);
264 #if SCM_ENABLE_DEPRECATED == 1
265 scm_c_issue_deprecation_warning
266 ("Returning non-0 from a smob free function is "
267 "deprecated. Use scm_gc_free et al instead.");
268 scm_c_issue_deprecation_warning_fmt
269 ("(You just returned non-0 while freeing a %s.)",
271 scm_i_deprecated_memory_return
+= mm
;
282 fprintf (stderr
, "unknown type");
287 SCM_SET_CELL_TYPE (p
, scm_tc_free_cell
);
288 SCM_SET_FREE_CELL_CDR (p
, PTR2SCM (*free_list
));
289 *free_list
= PTR2SCM (p
);
300 Like sweep, but no complicated logic to do the sweeping.
303 scm_i_init_card_freelist (scm_t_cell
* card
, SCM
*free_list
,
304 scm_t_heap_segment
*seg
)
306 int span
= seg
->span
;
307 scm_t_cell
*end
= card
+ SCM_GC_CARD_N_CELLS
;
308 scm_t_cell
*p
= end
- span
;
310 scm_t_c_bvec_long
* bvec_ptr
= (scm_t_c_bvec_long
* ) seg
->bounds
[1];
311 int idx
= (card
- seg
->bounds
[0]) / SCM_GC_CARD_N_CELLS
;
313 bvec_ptr
+= idx
*SCM_GC_CARD_BVEC_SIZE_IN_LONGS
;
314 SCM_GC_CELL_BVEC (card
) = bvec_ptr
;
317 ASSUMPTION: n_header_cells <= 2.
319 for (; p
> card
; p
-= span
)
321 SCM_SET_CELL_TYPE (p
, scm_tc_free_cell
);
322 SCM_SET_FREE_CELL_CDR (p
, PTR2SCM (*free_list
));
323 *free_list
= PTR2SCM (p
);
326 return SCM_GC_CARD_N_CELLS
- SCM_MAX(span
, SCM_GC_CARD_N_HEADER_CELLS
);
330 #if (SCM_DEBUG_CELL_ACCESSES == 1)
332 scm_gc_marked_p (SCM obj
)
334 return SCM_GC_MARK_P(obj
);
340 These functions are meant to be called from GDB as a debug aid.
342 I've left them as a convenience for future generations. --hwn.
346 int scm_gc_marked_p (SCM obj
);
347 scm_t_cell
* scm_gc_get_card (SCM obj
);
348 long * scm_gc_get_bvec (SCM obj
);
350 typedef struct scm_t_list_cell_struct
{
352 struct scm_t_list_cell_struct
* cdr
;
356 typedef struct scm_t_double_cell
367 scm_gc_get_card (SCM obj
)
369 return SCM_GC_CELL_CARD(obj
);
373 scm_gc_get_bvec (SCM obj
)
375 return SCM_GC_CARD_BVEC(SCM_GC_CELL_CARD(obj
));