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
90 This function is quite efficient. However, for many types of cells,
91 allocation and a de-allocation involves calling malloc() and
94 This is costly for small objects (due to malloc/free overhead.)
95 (should measure this).
97 It might also be bad for threads: if several threads are allocating
98 strings concurrently, then mallocs for both threads may have to
101 It might be interesting to add a separate memory pool for small
102 objects to each freelist.
107 scm_i_sweep_card (scm_t_cell
* p
, SCM
*free_list
, scm_t_heap_segment
*seg
)
108 #define FUNC_NAME "sweep_card"
110 scm_t_c_bvec_long
*bitvec
= SCM_GC_CARD_BVEC(p
);
111 scm_t_cell
* end
= p
+ SCM_GC_CARD_N_CELLS
;
112 int span
= seg
->span
;
113 int offset
=SCM_MAX (SCM_GC_CARD_N_HEADER_CELLS
, span
);
119 I tried something fancy with shifting by one bit every word from
120 the bitvec in turn, but it wasn't any faster, but quite bit
123 for (p
+= offset
; p
< end
; p
+= span
, offset
+= span
)
125 SCM scmptr
= PTR2SCM(p
);
126 if (SCM_C_BVEC_GET (bitvec
, offset
))
129 switch (SCM_TYP7 (scmptr
))
132 /* The card can be swept more than once. Check that it's
135 if (!SCM_STRUCT_GC_CHAIN (p
))
137 /* Structs need to be freed in a special order.
138 * This is handled by GC C hooks in struct.c.
140 SCM_SET_STRUCT_GC_CHAIN (p
, scm_i_structs_to_free
);
141 scm_i_structs_to_free
= scmptr
;
145 case scm_tcs_cons_imcar
:
146 case scm_tcs_cons_nimcar
:
147 case scm_tcs_closures
:
153 unsigned long int length
= SCM_VECTOR_LENGTH (scmptr
);
156 scm_gc_free (SCM_VECTOR_BASE (scmptr
),
157 length
* sizeof (scm_t_bits
),
164 scm_gc_free (SCM_CCLO_BASE (scmptr
),
165 SCM_CCLO_LENGTH (scmptr
) * sizeof (SCM
),
172 unsigned long int length
= SCM_BITVECTOR_LENGTH (scmptr
);
175 scm_gc_free (SCM_BITVECTOR_BASE (scmptr
),
177 * ((length
+SCM_LONG_BIT
-1) / SCM_LONG_BIT
)),
186 #if SCM_SIZEOF_LONG_LONG != 0
192 scm_gc_free (SCM_UVECTOR_BASE (scmptr
),
193 (SCM_UVECTOR_LENGTH (scmptr
)
194 * scm_uniform_element_size (scmptr
)),
199 scm_gc_free (SCM_STRING_CHARS (scmptr
),
200 SCM_STRING_LENGTH (scmptr
) + 1, "string");
203 scm_gc_free (SCM_SYMBOL_CHARS (scmptr
),
204 SCM_SYMBOL_LENGTH (scmptr
) + 1, "symbol");
206 case scm_tc7_variable
:
209 /* the various "subrs" (primitives) are never freed */
212 if SCM_OPENP (scmptr
)
214 int k
= SCM_PTOBNUM (scmptr
);
216 #if (SCM_DEBUG_CELL_ACCESSES == 1)
217 if (!(k
< scm_numptob
))
219 fprintf (stderr
, "undefined port type");
223 /* Keep "revealed" ports alive. */
224 if (scm_revealed_count (scmptr
) > 0)
227 /* Yes, I really do mean scm_ptobs[k].free */
228 /* rather than ftobs[k].close. .close */
229 /* is for explicit CLOSE-PORT by user */
230 mm
= scm_ptobs
[k
].free (scmptr
);
234 #if SCM_ENABLE_DEPRECATED == 1
235 scm_c_issue_deprecation_warning
236 ("Returning non-0 from a port free function is "
237 "deprecated. Use scm_gc_free et al instead.");
238 scm_c_issue_deprecation_warning_fmt
239 ("(You just returned non-0 while freeing a %s.)",
241 scm_i_deprecated_memory_return
+= mm
;
247 SCM_SETSTREAM (scmptr
, 0);
248 scm_remove_from_port_table (scmptr
);
249 scm_gc_ports_collected
++;
250 SCM_CLR_PORT_OPEN_FLAG (scmptr
);
254 switch SCM_TYP16 (scmptr
)
256 case scm_tc_free_cell
:
261 scm_gc_free (SCM_BDIGITS (scmptr
),
262 ((SCM_NUMDIGS (scmptr
) * SCM_BITSPERDIG
263 / SCM_CHAR_BIT
)), "bignum");
265 #endif /* def SCM_BIGDIG */
266 case scm_tc16_complex
:
267 scm_gc_free (SCM_COMPLEX_MEM (scmptr
), 2*sizeof (double),
273 k
= SCM_SMOBNUM (scmptr
);
274 #if (SCM_DEBUG_CELL_ACCESSES == 1)
275 if (!(k
< scm_numsmob
))
277 fprintf (stderr
, "undefined smob type");
281 if (scm_smobs
[k
].free
)
284 mm
= scm_smobs
[k
].free (scmptr
);
287 #if SCM_ENABLE_DEPRECATED == 1
288 scm_c_issue_deprecation_warning
289 ("Returning non-0 from a smob free function is "
290 "deprecated. Use scm_gc_free et al instead.");
291 scm_c_issue_deprecation_warning_fmt
292 ("(You just returned non-0 while freeing a %s.)",
294 scm_i_deprecated_memory_return
+= mm
;
305 fprintf (stderr
, "unknown type");
310 SCM_SET_CELL_TYPE (p
, scm_tc_free_cell
);
311 SCM_SET_FREE_CELL_CDR (p
, PTR2SCM (*free_list
));
312 *free_list
= PTR2SCM (p
);
323 Like sweep, but no complicated logic to do the sweeping.
326 scm_i_init_card_freelist (scm_t_cell
* card
, SCM
*free_list
,
327 scm_t_heap_segment
*seg
)
329 int span
= seg
->span
;
330 scm_t_cell
*end
= card
+ SCM_GC_CARD_N_CELLS
;
331 scm_t_cell
*p
= end
- span
;
333 scm_t_c_bvec_long
* bvec_ptr
= (scm_t_c_bvec_long
* ) seg
->bounds
[1];
334 int idx
= (card
- seg
->bounds
[0]) / SCM_GC_CARD_N_CELLS
;
336 bvec_ptr
+= idx
*SCM_GC_CARD_BVEC_SIZE_IN_LONGS
;
337 SCM_GC_CELL_BVEC (card
) = bvec_ptr
;
340 ASSUMPTION: n_header_cells <= 2.
342 for (; p
> card
; p
-= span
)
344 SCM_SET_CELL_TYPE (p
, scm_tc_free_cell
);
345 SCM_SET_FREE_CELL_CDR (p
, PTR2SCM (*free_list
));
346 *free_list
= PTR2SCM (p
);
349 return SCM_GC_CARD_N_CELLS
- SCM_MAX(span
, SCM_GC_CARD_N_HEADER_CELLS
);
353 #if (SCM_DEBUG_CELL_ACCESSES == 1)
355 scm_gc_marked_p (SCM obj
)
357 return SCM_GC_MARK_P(obj
);
363 These functions are meant to be called from GDB as a debug aid.
365 I've left them as a convenience for future generations. --hwn.
369 int scm_gc_marked_p (SCM obj
);
370 scm_t_cell
* scm_gc_get_card (SCM obj
);
371 long * scm_gc_get_bvec (SCM obj
);
373 typedef struct scm_t_list_cell_struct
{
375 struct scm_t_list_cell_struct
* cdr
;
379 typedef struct scm_t_double_cell
390 scm_gc_get_card (SCM obj
)
392 return SCM_GC_CELL_CARD(obj
);
396 scm_gc_get_bvec (SCM obj
)
398 return SCM_GC_CARD_BVEC(SCM_GC_CELL_CARD(obj
));