1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library 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 GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <count-one-bits.h>
28 #include "libguile/_scm.h"
29 #include "libguile/async.h"
30 #include "libguile/deprecation.h"
31 #include "libguile/eval.h"
32 #include "libguile/gc.h"
33 #include "libguile/hashtab.h"
34 #include "libguile/numbers.h"
35 #include "libguile/ports.h"
36 #include "libguile/private-gc.h"
37 #include "libguile/root.h"
38 #include "libguile/smob.h"
39 #include "libguile/srfi-4.h"
40 #include "libguile/stackchk.h"
41 #include "libguile/stime.h"
42 #include "libguile/strings.h"
43 #include "libguile/struct.h"
44 #include "libguile/tags.h"
45 #include "libguile/unif.h"
46 #include "libguile/validate.h"
47 #include "libguile/vectors.h"
48 #include "libguile/weaks.h"
50 #include "libguile/private-gc.h"
52 long int scm_i_deprecated_memory_return
;
55 /* During collection, this accumulates structures which are to be freed.
57 SCM scm_i_structs_to_free
;
60 Init all the free cells in CARD, prepending to *FREE_LIST.
62 Return: FREE_COUNT, the number of cells collected. This is
63 typically the length of the *FREE_LIST, but for some special cases,
64 we do not actually free the cell. To make the numbers match up, we
65 do increase the FREE_COUNT.
67 It would be cleaner to have a separate function sweep_value (), but
68 that is too slow (functions with switch statements can't be
73 For many types of cells, allocation and a de-allocation involves
74 calling malloc () and free (). This is costly for small objects (due
75 to malloc/free overhead.) (should measure this).
77 It might also be bad for threads: if several threads are allocating
78 strings concurrently, then mallocs for both threads may have to
81 It might be interesting to add a separate memory pool for small
82 objects to each freelist.
87 scm_i_sweep_card (scm_t_cell
*card
, SCM
*free_list
, scm_t_heap_segment
*seg
)
88 #define FUNC_NAME "sweep_card"
90 scm_t_c_bvec_long
*bitvec
= SCM_GC_CARD_BVEC (card
);
91 scm_t_cell
*end
= card
+ SCM_GC_CARD_N_CELLS
;
94 int offset
= SCM_MAX (SCM_GC_CARD_N_HEADER_CELLS
, span
);
98 I tried something fancy with shifting by one bit every word from
99 the bitvec in turn, but it wasn't any faster, but quite a bit
102 for (p
+= offset
; p
< end
; p
+= span
, offset
+= span
)
104 SCM scmptr
= PTR2SCM (p
);
105 if (SCM_C_BVEC_GET (bitvec
, offset
))
108 switch (SCM_TYP7 (scmptr
))
111 /* The card can be swept more than once. Check that it's
114 if (!SCM_STRUCT_GC_CHAIN (scmptr
))
116 /* Structs need to be freed in a special order.
117 * This is handled by GC C hooks in struct.c.
119 SCM_SET_STRUCT_GC_CHAIN (scmptr
, scm_i_structs_to_free
);
120 scm_i_structs_to_free
= scmptr
;
124 case scm_tcs_cons_imcar
:
125 case scm_tcs_cons_nimcar
:
126 case scm_tcs_closures
:
131 scm_i_vector_free (scmptr
);
136 scm_gc_free (SCM_CCLO_BASE (scmptr
),
137 SCM_CCLO_LENGTH (scmptr
) * sizeof (SCM
),
143 switch SCM_TYP16 (scmptr
)
148 mpz_clear (SCM_I_BIG_MPZ (scmptr
));
149 /* nothing else to do here since the mpz is in a double cell */
151 case scm_tc16_complex
:
152 scm_gc_free (SCM_COMPLEX_MEM (scmptr
), sizeof (scm_t_complex
),
155 case scm_tc16_fraction
:
156 /* nothing to do here since the num/denum of a fraction
157 are proper SCM objects themselves. */
162 scm_i_string_free (scmptr
);
164 case scm_tc7_stringbuf
:
165 scm_i_stringbuf_free (scmptr
);
168 scm_i_symbol_free (scmptr
);
170 case scm_tc7_variable
:
173 /* the various "subrs" (primitives) are never freed */
176 if SCM_OPENP (scmptr
)
178 int k
= SCM_PTOBNUM (scmptr
);
180 #if (SCM_DEBUG_CELL_ACCESSES == 1)
181 if (!(k
< scm_numptob
))
183 fprintf (stderr
, "undefined port type");
187 /* Keep "revealed" ports alive. */
188 if (scm_revealed_count (scmptr
) > 0)
191 /* Yes, I really do mean scm_ptobs[k].free */
192 /* rather than ftobs[k].close. .close */
193 /* is for explicit CLOSE-PORT by user */
194 mm
= scm_ptobs
[k
].free (scmptr
);
198 #if SCM_ENABLE_DEPRECATED == 1
199 scm_c_issue_deprecation_warning
200 ("Returning non-0 from a port free function is "
201 "deprecated. Use scm_gc_free et al instead.");
202 scm_c_issue_deprecation_warning_fmt
203 ("(You just returned non-0 while freeing a %s.)",
205 scm_i_deprecated_memory_return
+= mm
;
211 SCM_SETSTREAM (scmptr
, 0);
212 scm_i_remove_port (scmptr
);
213 SCM_CLR_PORT_OPEN_FLAG (scmptr
);
217 switch SCM_TYP16 (scmptr
)
219 case scm_tc_free_cell
:
224 k
= SCM_SMOBNUM (scmptr
);
225 #if (SCM_DEBUG_CELL_ACCESSES == 1)
226 if (!(k
< scm_numsmob
))
228 fprintf (stderr
, "undefined smob type");
232 if (scm_smobs
[k
].free
)
235 mm
= scm_smobs
[k
].free (scmptr
);
238 #if SCM_ENABLE_DEPRECATED == 1
239 scm_c_issue_deprecation_warning
240 ("Returning non-0 from a smob free function is "
241 "deprecated. Use scm_gc_free et al instead.");
242 scm_c_issue_deprecation_warning_fmt
243 ("(You just returned non-0 while freeing a %s.)",
245 scm_i_deprecated_memory_return
+= mm
;
256 fprintf (stderr
, "unknown type");
260 SCM_GC_SET_CELL_WORD (scmptr
, 0, scm_tc_free_cell
);
261 SCM_SET_FREE_CELL_CDR (scmptr
, PTR2SCM (*free_list
));
271 Like sweep, but no complicated logic to do the sweeping.
274 scm_i_init_card_freelist (scm_t_cell
*card
, SCM
*free_list
,
275 scm_t_heap_segment
*seg
)
277 int span
= seg
->span
;
278 scm_t_cell
*end
= card
+ SCM_GC_CARD_N_CELLS
;
279 scm_t_cell
*p
= end
- span
;
281 scm_t_c_bvec_long
*bvec_ptr
= (scm_t_c_bvec_long
*) seg
->bounds
[1];
282 int idx
= (card
- seg
->bounds
[0]) / SCM_GC_CARD_N_CELLS
;
284 bvec_ptr
+= idx
* SCM_GC_CARD_BVEC_SIZE_IN_LONGS
;
285 SCM_GC_SET_CELL_BVEC (card
, bvec_ptr
);
288 ASSUMPTION: n_header_cells <= 2.
290 for (; p
> card
; p
-= span
)
292 const SCM scmptr
= PTR2SCM (p
);
293 SCM_GC_SET_CELL_WORD (scmptr
, 0, scm_tc_free_cell
);
294 SCM_SET_FREE_CELL_CDR (scmptr
, PTR2SCM (*free_list
));
303 Amount of cells marked in this cell, measured in 1-cells.
306 scm_i_card_marked_count (scm_t_cell
*card
, int span
)
308 scm_t_c_bvec_long
* bvec
= SCM_GC_CARD_BVEC (card
);
309 scm_t_c_bvec_long
* bvec_end
= (bvec
+ SCM_GC_CARD_BVEC_SIZE_IN_LONGS
);
312 while (bvec
< bvec_end
)
314 count
+= count_one_bits_l (*bvec
);
321 scm_i_card_statistics (scm_t_cell
*p
, SCM hashtab
, scm_t_heap_segment
*seg
)
323 scm_t_c_bvec_long
*bitvec
= SCM_GC_CARD_BVEC (p
);
324 scm_t_cell
* end
= p
+ SCM_GC_CARD_N_CELLS
;
325 int span
= seg
->span
;
326 int offset
= SCM_MAX (SCM_GC_CARD_N_HEADER_CELLS
, span
);
329 /* Card P hasn't been initialized yet by `scm_i_init_card_freelist ()'. */
332 for (p
+= offset
; p
< end
; p
+= span
, offset
+= span
)
335 SCM scmptr
= PTR2SCM (p
);
337 if (!SCM_C_BVEC_GET (bitvec
, offset
))
340 tag
= SCM_TYP7 (scmptr
);
341 if (tag
== scm_tc7_smob
|| tag
== scm_tc7_number
)
343 /* Record smobs and numbers under 16 bits of the tag, so the
344 different smob objects are distinguished, and likewise the
345 different numbers big, real, complex and fraction. */
346 tag
= SCM_TYP16(scmptr
);
351 case scm_tcs_cons_imcar
:
354 case scm_tcs_cons_nimcar
:
359 tag
= scm_tc3_struct
;
361 case scm_tcs_closures
:
362 tag
= scm_tc3_closure
;
370 SCM handle
= scm_hashq_create_handle_x (hashtab
,
371 scm_from_int (tag
), SCM_INUM0
);
372 SCM_SETCDR (handle
, scm_from_int (scm_to_int (SCM_CDR (handle
)) + 1));
377 /* TAG is the tag word of a cell, return a string which is its name, or NULL
378 if unknown. Currently this is only used by gc-live-object-stats and the
379 distinctions between types are oriented towards what that code records
380 while scanning what's alive. */
382 scm_i_tag_name (scm_t_bits tag
)
384 switch (tag
& 0x7F) /* 7 bits */
388 case scm_tcs_cons_imcar
:
389 return "cons (immediate car)";
390 case scm_tcs_cons_nimcar
:
391 return "cons (non-immediate car)";
392 case scm_tcs_closures
:
397 return "weak vector";
402 return "compiled closure";
411 case scm_tc16_complex
:
412 return "complex number";
413 case scm_tc16_fraction
:
416 /* shouldn't reach here unless there's a new class of numbers */
420 case scm_tc7_stringbuf
:
421 return "string buffer";
424 case scm_tc7_variable
:
431 /* scm_tc_free_cell is smob 0, the name field in that scm_smobs[]
432 entry should be ok for our return here */
433 return scm_smobs
[SCM_TC2SMOBNUM (tag
)].name
;
440 #if (SCM_DEBUG_DEBUGGING_SUPPORT == 1)
442 typedef struct scm_dbg_t_list_cell
{
444 struct scm_dbg_t_list_cell
* cdr
;
445 } scm_dbg_t_list_cell
;
448 typedef struct scm_dbg_t_double_cell
{
453 } scm_dbg_t_double_cell
;
456 int scm_dbg_gc_marked_p (SCM obj
);
457 scm_t_cell
* scm_dbg_gc_get_card (SCM obj
);
458 scm_t_c_bvec_long
* scm_dbg_gc_get_bvec (SCM obj
);
462 scm_dbg_gc_marked_p (SCM obj
)
465 return SCM_GC_MARK_P (obj
);
471 scm_dbg_gc_get_card (SCM obj
)
474 return SCM_GC_CELL_CARD (obj
);
480 scm_dbg_gc_get_bvec (SCM obj
)
483 return SCM_GC_CARD_BVEC (SCM_GC_CELL_CARD (obj
));