1 /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2006, 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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful, but
9 * 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
27 #include <count-one-bits.h>
29 #include "libguile/_scm.h"
30 #include "libguile/pairs.h"
31 #include "libguile/gc.h"
32 #include "libguile/private-gc.h"
34 size_t scm_max_segment_size
;
36 /* Important entry point: try to grab some memory, and make it into a
37 segment; return the index of the segment. SWEEP_STATS should contain
38 global GC sweep statistics collected since the last full GC.
40 Returns the index of the segment. If error_policy !=
41 abort_on_error, we return -1 on failure.
44 scm_i_get_new_heap_segment (scm_t_cell_type_statistics
*freelist
,
46 policy_on_error error_policy
)
48 if (len
> scm_max_segment_size
)
49 len
= scm_max_segment_size
;
51 if (len
< SCM_MIN_HEAP_SEG_SIZE
)
52 len
= SCM_MIN_HEAP_SEG_SIZE
;
54 /* todo: consider having a more flexible lower bound. */
56 scm_t_heap_segment
*seg
= scm_i_make_empty_heap_segment (freelist
);
58 /* Allocate with decaying ambition. */
59 while (len
>= SCM_MIN_HEAP_SEG_SIZE
)
61 if (scm_i_initialize_heap_segment_data (seg
, len
))
62 return scm_i_insert_segment (seg
);
68 if (error_policy
== abort_on_error
)
70 fprintf (stderr
, "scm_i_get_new_heap_segment: Could not grow heap.\n");
78 scm_i_make_empty_heap_segment (scm_t_cell_type_statistics
*fl
)
80 scm_t_heap_segment
*shs
= calloc (1, sizeof (scm_t_heap_segment
));
84 fprintf (stderr
, "scm_i_get_new_heap_segment: out of memory.\n");
95 scm_i_heap_segment_statistics (scm_t_heap_segment
*seg
, SCM tab
)
97 scm_t_cell
*p
= seg
->bounds
[0];
98 while (p
< seg
->bounds
[1])
100 scm_i_card_statistics (p
, tab
, seg
);
101 p
+= SCM_GC_CARD_N_CELLS
;
106 count number of marked bits, so we know how much cells are live.
109 scm_i_heap_segment_marked_count (scm_t_heap_segment
*seg
)
111 scm_t_c_bvec_long
*bvec
= (scm_t_c_bvec_long
*) seg
->bounds
[1];
112 scm_t_c_bvec_long
*bvec_end
=
114 scm_i_segment_card_count (seg
) * SCM_GC_CARD_BVEC_SIZE_IN_LONGS
);
117 while (bvec
< bvec_end
)
119 count
+= count_one_bits_l (*bvec
);
122 return count
* seg
->span
;
126 scm_i_segment_card_number (scm_t_heap_segment
*seg
,
129 return (card
- seg
->bounds
[0]) / SCM_GC_CARD_N_CELLS
;
133 Fill SEGMENT with memory both for data and mark bits.
135 RETURN: 1 on success, 0 failure
138 scm_i_initialize_heap_segment_data (scm_t_heap_segment
*segment
, size_t requested
)
143 int card_data_cell_count
= (SCM_GC_CARD_N_CELLS
- SCM_GC_CARD_N_HEADER_CELLS
);
144 int card_count
= 1 + (requested
/ sizeof (scm_t_cell
)) / card_data_cell_count
;
147 one card extra due to alignment
149 size_t mem_needed
= (1 + card_count
) * SCM_GC_SIZEOF_CARD
150 + SCM_GC_CARD_BVEC_SIZE_IN_LONGS
* card_count
* SCM_SIZEOF_LONG
;
151 scm_t_cell
*memory
= 0;
154 We use calloc to alloc the heap, so it is nicely initialized.
156 SCM_SYSCALL (memory
= (scm_t_cell
*) calloc (1, mem_needed
));
161 segment
->malloced
= memory
;
162 segment
->bounds
[0] = SCM_GC_CARD_UP (memory
);
163 segment
->bounds
[1] = segment
->bounds
[0] + card_count
* SCM_GC_CARD_N_CELLS
;
164 segment
->freelist
->heap_total_cells
+= scm_i_segment_cell_count (segment
);
167 Don't init the mem or the bitvector. This is handled by lazy
170 segment
->next_free_card
= segment
->bounds
[0];
171 segment
->first_time
= 1;
176 scm_i_segment_card_count (scm_t_heap_segment
*seg
)
178 return (seg
->bounds
[1] - seg
->bounds
[0]) / SCM_GC_CARD_N_CELLS
;
182 Return the number of available single-cell data cells.
185 scm_i_segment_cell_count (scm_t_heap_segment
*seg
)
187 return scm_i_segment_card_count (seg
)
188 * scm_i_segment_cells_per_card (seg
);
192 scm_i_segment_cells_per_card (scm_t_heap_segment
*seg
)
194 return (SCM_GC_CARD_N_CELLS
- SCM_GC_CARD_N_HEADER_CELLS
195 + ((seg
->span
== 2) ? -1 : 0));
199 scm_i_clear_segment_mark_space (scm_t_heap_segment
*seg
)
201 scm_t_cell
*markspace
= seg
->bounds
[1];
203 memset (markspace
, 0x00,
204 scm_i_segment_card_count (seg
) * SCM_GC_CARD_BVEC_SIZE_IN_LONGS
* SCM_SIZEOF_LONG
);
209 Force a sweep of this entire segment.
212 scm_i_sweep_segment (scm_t_heap_segment
*seg
,
213 scm_t_sweep_statistics
*sweep_stats
)
215 int infinity
= 1 << 30;
216 scm_t_cell
*remember
= seg
->next_free_card
;
217 while (scm_i_sweep_some_cards (seg
, sweep_stats
, infinity
) != SCM_EOL
)
219 seg
->next_free_card
= remember
;
223 /* Sweep cards from SEG until we've gathered THRESHOLD cells. On
224 return, SWEEP_STATS, if non-NULL, contains the number of cells that
225 have been visited and collected. A freelist is returned,
226 potentially empty. */
228 scm_i_sweep_some_cards (scm_t_heap_segment
*seg
,
229 scm_t_sweep_statistics
*sweep_stats
,
234 int (*sweeper
) (scm_t_cell
*, SCM
*, scm_t_heap_segment
*)
235 = (seg
->first_time
) ? &scm_i_init_card_freelist
: &scm_i_sweep_card
;
237 scm_t_cell
*next_free
= seg
->next_free_card
;
239 while (collected
< threshold
&& next_free
< seg
->bounds
[1])
241 collected
+= (*sweeper
) (next_free
, &cells
, seg
);
242 next_free
+= SCM_GC_CARD_N_CELLS
;
246 if (sweep_stats
!= NULL
)
248 int swept
= cards_swept
249 * ((SCM_GC_CARD_N_CELLS
- SCM_GC_CARD_N_HEADER_CELLS
)
251 int collected_cells
= collected
* seg
->span
;
252 sweep_stats
->swept
+= swept
;
253 sweep_stats
->collected
+= collected_cells
;
256 if (next_free
== seg
->bounds
[1])
261 seg
->next_free_card
= next_free
;
268 scm_i_sweep_for_freelist (scm_t_cell_type_statistics
*freelist
)
270 scm_t_sweep_statistics stats
= { 0 };
271 SCM result
= scm_i_sweep_some_segments (freelist
, &stats
);
273 scm_i_gc_sweep_stats
.collected
+= stats
.collected
;
274 scm_i_gc_sweep_stats
.swept
+= stats
.swept
;
276 freelist
->collected
+= stats
.collected
;
277 freelist
->swept
+= stats
.swept
;