2 * private-gc.h - private declarations for garbage collection.
4 * Copyright (C) 2002, 03, 04, 05, 06, 07, 08 Free Software Foundation, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 /* {heap tuning parameters}
28 * These are parameters for controlling memory allocation. The heap
29 * is the area out of which scm_cons, and object headers are allocated.
31 * Each heap cell is 8 bytes on a 32 bit machine and 16 bytes on a
32 * 64 bit machine. The units of the _SIZE parameters are bytes.
33 * Cons pairs and object headers occupy one heap cell.
35 * SCM_MIN_HEAP_SEG_SIZE is minimum size of heap to accept when more heap
41 * Heap size 45000 and 40% min yield gives quick startup and no extra
42 * heap allocation. Having higher values on min yield may lead to
43 * large heaps, especially if code behaviour is varying its
44 * maximum consumption between different freelists.
48 These values used to be global C variables. However, they're also
49 available through the environment, and having a double interface is
50 confusing. Now they're #defines --hwn.
53 #define SCM_DEFAULT_INIT_HEAP_SIZE_1 256*1024
54 #define SCM_DEFAULT_MIN_YIELD_1 40
55 #define SCM_DEFAULT_INIT_HEAP_SIZE_2 32*1024
58 How many cells to collect during one sweep call. This is the pool
61 #define DEFAULT_SWEEP_AMOUNT 512
63 /* The following value may seem large, but note that if we get to GC at
64 * all, this means that we have a numerically intensive application
66 #define SCM_DEFAULT_MIN_YIELD_2 40
68 #define SCM_DEFAULT_MAX_SEGMENT_SIZE (20*1024*1024L)
70 #define SCM_MIN_HEAP_SEG_SIZE (8 * SCM_GC_SIZEOF_CARD)
71 #define SCM_HEAP_SEG_SIZE (16384L * sizeof (scm_t_cell))
73 #define SCM_DOUBLECELL_ALIGNED_P(x) (((2 * sizeof (scm_t_cell) - 1) & SCM_UNPACK (x)) == 0)
76 #define SCM_GC_CARD_BVEC_SIZE_IN_LONGS \
77 ((SCM_GC_CARD_N_CELLS + SCM_C_BVEC_LONG_BITS - 1) / SCM_C_BVEC_LONG_BITS)
78 #define SCM_GC_IN_CARD_HEADERP(x) \
79 (scm_t_cell *) (x) < SCM_GC_CELL_CARD (x) + SCM_GC_CARD_N_HEADER_CELLS
81 int scm_getenv_int (const char *var
, int def
);
84 typedef enum { return_on_error
, abort_on_error
} policy_on_error
;
91 A struct holding GC statistics on a particular type of cells.
93 Counts in cells are mainly for heap statistics, and for
94 double-cells, they are still measured in single-cell units.
96 typedef struct scm_t_cell_type_statistics
{
98 heap segment where the last cell was allocated
100 int heap_segment_idx
;
102 /* defines min_yield as fraction of total heap size
104 float min_yield_fraction
;
106 /* number of cells per object on this list */
109 /* number of collected cells during last GC. */
110 unsigned long collected
;
115 Total number of cells in heap segments belonging to this list.
117 unsigned long heap_total_cells
;
118 } scm_t_cell_type_statistics
;
121 /* Sweep statistics. */
122 typedef struct scm_sweep_statistics
124 /* Number of cells "swept", i.e., visited during the sweep operation. */
127 /* Number of cells collected during the sweep operation. This number must
128 always be lower than or equal to SWEPT. */
130 } scm_t_sweep_statistics
;
132 SCM_INTERNAL scm_t_sweep_statistics scm_i_gc_sweep_stats
;
135 extern scm_t_cell_type_statistics scm_i_master_freelist
;
136 extern scm_t_cell_type_statistics scm_i_master_freelist2
;
139 void scm_i_adjust_min_yield (scm_t_cell_type_statistics
*freelist
,
140 scm_t_sweep_statistics sweep_stats
,
141 scm_t_sweep_statistics sweep_stats_1
);
143 void scm_i_gc_sweep_freelist_reset (scm_t_cell_type_statistics
*freelist
);
145 scm_i_gc_heap_size_delta (scm_t_cell_type_statistics
* freelist
);
148 #define SCM_MAX(A, B) ((A) > (B) ? (A) : (B))
149 #define SCM_MIN(A, B) ((A) < (B) ? (A) : (B))
151 /* CELL_P checks a random word whether it has the right form for a
152 pointer to a cell. Use scm_i_find_heap_segment_containing_object
153 to find out whether it actually points to a real cell.
155 The right form for a cell pointer is this: the low three bits must
156 be scm_tc3_cons, and when the scm_tc3_cons tag is stripped, the
157 resulting pointer must be correctly aligned.
158 scm_i_initialize_heap_segment_data guarantees that the test below
161 #define CELL_P(x) ((SCM_UNPACK(x) & (sizeof(scm_t_cell)-1)) == scm_tc3_cons)
167 /* this can be used to ensure that set/clear gc marks only happen when
171 void scm_mark_all (void);
179 Cells are stored in a heap-segment: it is a contiguous chunk of
180 memory, that associated with one freelist.
182 typedef struct scm_t_heap_segment
185 {lower, upper} bounds of the segment
187 The upper bound is also the start of the mark space.
189 scm_t_cell
*bounds
[2];
192 If we ever decide to give it back, we could do it with this ptr.
194 Note that giving back memory is not very useful; as long we don't
195 touch a chunk of memory, the virtual memory system will keep it
196 swapped out. We could simply forget about a block.
198 (not that we do that, but anyway.)
202 scm_t_cell
*next_free_card
;
204 /* address of the head-of-freelist pointer for this segment's cells.
205 All segments usually point to the same one, scm_i_freelist. */
206 scm_t_cell_type_statistics
*freelist
;
208 /* number of cells per object in this segment */
212 Is this the first time that the cells are accessed?
215 } scm_t_heap_segment
;
218 A table of segment records is kept that records the upper and
219 lower extents of the segment; this is used during the conservative
220 phase of gc to identify probably gc roots (because they point
221 into valid segments at reasonable offsets).
223 extern scm_t_heap_segment
** scm_i_heap_segment_table
;
224 extern size_t scm_i_heap_segment_table_size
;
227 SCM_INTERNAL
int scm_i_init_card_freelist (scm_t_cell
* card
, SCM
*free_list
,
228 scm_t_heap_segment
*);
229 SCM_INTERNAL
int scm_i_sweep_card (scm_t_cell
*card
, SCM
*free_list
,
230 scm_t_heap_segment
*);
231 SCM_INTERNAL
int scm_i_card_marked_count (scm_t_cell
*card
, int span
);
232 SCM_INTERNAL
void scm_i_card_statistics (scm_t_cell
*p
, SCM hashtab
,
233 scm_t_heap_segment
*seg
);
234 SCM_INTERNAL
char const *scm_i_tag_name (scm_t_bits tag
); /* MOVEME */
236 SCM_INTERNAL
int scm_i_initialize_heap_segment_data (scm_t_heap_segment
*seg
,
239 SCM_INTERNAL
int scm_i_segment_cells_per_card (scm_t_heap_segment
*seg
);
240 SCM_INTERNAL
int scm_i_segment_card_number (scm_t_heap_segment
*seg
,
242 SCM_INTERNAL
int scm_i_segment_card_count (scm_t_heap_segment
*seg
);
243 SCM_INTERNAL
int scm_i_segment_cell_count (scm_t_heap_segment
*seg
);
244 SCM_INTERNAL
int scm_i_heap_segment_marked_count (scm_t_heap_segment
*seg
);
246 SCM_INTERNAL
void scm_i_clear_segment_mark_space (scm_t_heap_segment
*seg
);
247 SCM_INTERNAL scm_t_heap_segment
*
248 scm_i_make_empty_heap_segment (scm_t_cell_type_statistics
*);
249 SCM_INTERNAL SCM
scm_i_sweep_for_freelist (scm_t_cell_type_statistics
*seg
);
250 SCM_INTERNAL SCM
scm_i_sweep_some_cards (scm_t_heap_segment
*seg
,
251 scm_t_sweep_statistics
*sweep_stats
,
253 SCM_INTERNAL
void scm_i_sweep_segment (scm_t_heap_segment
*seg
,
254 scm_t_sweep_statistics
*sweep_stats
);
256 SCM_INTERNAL
void scm_i_heap_segment_statistics (scm_t_heap_segment
*seg
,
260 SCM_INTERNAL
int scm_i_insert_segment (scm_t_heap_segment
*seg
);
261 SCM_INTERNAL
int scm_i_find_heap_segment_containing_object (SCM obj
);
262 SCM_INTERNAL
int scm_i_get_new_heap_segment (scm_t_cell_type_statistics
*freelist
,
265 SCM_INTERNAL
int scm_i_marked_count (void);
266 SCM_INTERNAL
void scm_i_clear_mark_space (void);
267 SCM_INTERNAL
void scm_i_sweep_segments (void);
268 SCM_INTERNAL SCM
scm_i_sweep_some_segments (scm_t_cell_type_statistics
*fl
,
269 scm_t_sweep_statistics
*sweep_stats
);
270 SCM_INTERNAL
void scm_i_reset_segments (void);
271 SCM_INTERNAL
void scm_i_sweep_all_segments (char const *reason
,
272 scm_t_sweep_statistics
*sweep_stats
);
273 SCM_INTERNAL SCM
scm_i_all_segments_statistics (SCM hashtab
);
274 SCM_INTERNAL
unsigned long *scm_i_segment_table_info(int *size
);
276 SCM_INTERNAL
long int scm_i_deprecated_memory_return
;
277 SCM_INTERNAL
long int scm_i_find_heap_calls
;
278 SCM_INTERNAL
long int scm_i_last_marked_cell_count
;
283 void scm_gc_init_malloc (void);
284 void scm_gc_init_freelist (void);
285 void scm_gc_init_segments (void);
286 void scm_gc_init_mark (void);