1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 /* Generic garbage collection (GC) functions and data, not specific to
22 any particular GC implementation. */
32 #include "langhooks.h"
34 /* Statistics about the allocation. */
35 static ggc_statistics *ggc_stats;
37 static int ggc_htab_delete PARAMS ((void **, void *));
39 /* Maintain global roots that are preserved during GC. */
41 /* Global roots that are preserved during calls to gc. */
45 struct ggc_root *next;
49 void (*cb) PARAMS ((void *));
52 static struct ggc_root *roots;
54 /* Add BASE as a new garbage collection root. It is an array of
55 length NELT with each element SIZE bytes long. CB is a
56 function that will be called with a pointer to each element
57 of the array; it is the intention that CB call the appropriate
58 routine to mark gc-able memory for that element. */
61 ggc_add_root (base, nelt, size, cb)
64 void (*cb) PARAMS ((void *));
66 struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
77 /* Process a slot of an htab by deleting it if it has not been marked. */
80 ggc_htab_delete (slot, info)
84 const struct ggc_cache_tab *r = (const struct ggc_cache_tab *) info;
86 if (! (*r->marked_p) (*slot))
87 htab_clear_slot (*r->base, slot);
94 /* Iterate through all registered roots and mark each element. */
100 const struct ggc_root_tab *const *rt;
101 const struct ggc_root_tab *rti;
102 const struct ggc_cache_tab *const *ct;
103 const struct ggc_cache_tab *cti;
106 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
107 for (rti = *rt; rti->base != NULL; rti++)
108 memset (rti->base, 0, rti->stride);
110 for (rt = gt_ggc_rtab; *rt; rt++)
111 for (rti = *rt; rti->base != NULL; rti++)
112 for (i = 0; i < rti->nelt; i++)
113 (*rti->cb)(*(void **)((char *)rti->base + rti->stride * i));
115 for (x = roots; x != NULL; x = x->next)
118 int s = x->size, n = x->nelt;
119 void (*cb) PARAMS ((void *)) = x->cb;
122 for (i = 0; i < n; ++i, elt += s)
126 /* Now scan all hash tables that have objects which are to be deleted if
127 they are not already marked. */
128 for (ct = gt_ggc_cache_rtab; *ct; ct++)
129 for (cti = *ct; cti->base != NULL; cti++)
131 htab_traverse (*cti->base, ggc_htab_delete, (PTR) cti);
134 /* Allocate a block of memory, then clear it. */
136 ggc_alloc_cleared (size)
139 void *buf = ggc_alloc (size);
140 memset (buf, 0, size);
144 /* Resize a block of memory, possibly re-allocating it. */
146 ggc_realloc (x, size)
154 return ggc_alloc (size);
156 old_size = ggc_get_size (x);
157 if (size <= old_size)
160 r = ggc_alloc (size);
161 memcpy (r, x, old_size);
165 /* Like ggc_alloc_cleared, but performs a multiplication. */
170 return ggc_alloc_cleared (s1 * s2);
173 /* Print statistics that are independent of the collector in use. */
174 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
176 : ((x) < 1024*1024*10 \
178 : (x) / (1024*1024))))
179 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
182 ggc_print_common_statistics (stream, stats)
184 ggc_statistics *stats;
188 /* Set the pointer so that during collection we will actually gather
192 /* Then do one collection to fill in the statistics. */
195 /* Total the statistics. */
196 for (code = 0; code < MAX_TREE_CODES; ++code)
198 stats->total_num_trees += stats->num_trees[code];
199 stats->total_size_trees += stats->size_trees[code];
201 for (code = 0; code < NUM_RTX_CODE; ++code)
203 stats->total_num_rtxs += stats->num_rtxs[code];
204 stats->total_size_rtxs += stats->size_rtxs[code];
207 /* Print the statistics for trees. */
208 fprintf (stream, "\n%-17s%10s %16s %10s\n", "Tree",
209 "Number", "Bytes", "% Total");
210 for (code = 0; code < MAX_TREE_CODES; ++code)
211 if (ggc_stats->num_trees[code])
213 fprintf (stream, "%-17s%10u%16ld%c %10.3f\n",
214 tree_code_name[code],
215 ggc_stats->num_trees[code],
216 SCALE (ggc_stats->size_trees[code]),
217 LABEL (ggc_stats->size_trees[code]),
218 (100 * ((double) ggc_stats->size_trees[code])
219 / ggc_stats->total_size_trees));
222 "%-17s%10u%16ld%c\n", "Total",
223 ggc_stats->total_num_trees,
224 SCALE (ggc_stats->total_size_trees),
225 LABEL (ggc_stats->total_size_trees));
227 /* Print the statistics for RTL. */
228 fprintf (stream, "\n%-17s%10s %16s %10s\n", "RTX",
229 "Number", "Bytes", "% Total");
230 for (code = 0; code < NUM_RTX_CODE; ++code)
231 if (ggc_stats->num_rtxs[code])
233 fprintf (stream, "%-17s%10u%16ld%c %10.3f\n",
235 ggc_stats->num_rtxs[code],
236 SCALE (ggc_stats->size_rtxs[code]),
237 LABEL (ggc_stats->size_rtxs[code]),
238 (100 * ((double) ggc_stats->size_rtxs[code])
239 / ggc_stats->total_size_rtxs));
242 "%-17s%10u%16ld%c\n", "Total",
243 ggc_stats->total_num_rtxs,
244 SCALE (ggc_stats->total_size_rtxs),
245 LABEL (ggc_stats->total_size_rtxs));
247 /* Don't gather statistics any more. */