PR libstdc++/3584
[official-gcc.git] / gcc / ggc-common.c
blob2674cec52a14565b09734ea0c663e3032e7052e5
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
9 version.
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
14 for more details.
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
19 02111-1307, USA. */
21 /* Generic garbage collection (GC) functions and data, not specific to
22 any particular GC implementation. */
24 #include "config.h"
25 #include "system.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "hashtab.h"
30 #include "varray.h"
31 #include "ggc.h"
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. */
43 struct ggc_root
45 struct ggc_root *next;
46 void *base;
47 int nelt;
48 int size;
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. */
60 void
61 ggc_add_root (base, nelt, size, cb)
62 void *base;
63 int nelt, size;
64 void (*cb) PARAMS ((void *));
66 struct ggc_root *x = (struct ggc_root *) xmalloc (sizeof (*x));
68 x->next = roots;
69 x->base = base;
70 x->nelt = nelt;
71 x->size = size;
72 x->cb = cb;
74 roots = x;
77 /* Process a slot of an htab by deleting it if it has not been marked. */
79 static int
80 ggc_htab_delete (slot, info)
81 void **slot;
82 void *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);
88 else
89 (*r->cb) (*slot);
91 return 1;
94 /* Iterate through all registered roots and mark each element. */
96 void
97 ggc_mark_roots ()
99 struct ggc_root *x;
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;
104 size_t i;
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)
117 char *elt = x->base;
118 int s = x->size, n = x->nelt;
119 void (*cb) PARAMS ((void *)) = x->cb;
120 int i;
122 for (i = 0; i < n; ++i, elt += s)
123 (*cb)(elt);
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++)
130 if (*cti->base)
131 htab_traverse (*cti->base, ggc_htab_delete, (PTR) cti);
134 /* Allocate a block of memory, then clear it. */
135 void *
136 ggc_alloc_cleared (size)
137 size_t size;
139 void *buf = ggc_alloc (size);
140 memset (buf, 0, size);
141 return buf;
144 /* Resize a block of memory, possibly re-allocating it. */
145 void *
146 ggc_realloc (x, size)
147 void *x;
148 size_t size;
150 void *r;
151 size_t old_size;
153 if (x == NULL)
154 return ggc_alloc (size);
156 old_size = ggc_get_size (x);
157 if (size <= old_size)
158 return x;
160 r = ggc_alloc (size);
161 memcpy (r, x, old_size);
162 return r;
165 /* Like ggc_alloc_cleared, but performs a multiplication. */
166 void *
167 ggc_calloc (s1, s2)
168 size_t s1, s2;
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 \
175 ? (x) \
176 : ((x) < 1024*1024*10 \
177 ? (x) / 1024 \
178 : (x) / (1024*1024))))
179 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
181 void
182 ggc_print_common_statistics (stream, stats)
183 FILE *stream;
184 ggc_statistics *stats;
186 int code;
188 /* Set the pointer so that during collection we will actually gather
189 the statistics. */
190 ggc_stats = stats;
192 /* Then do one collection to fill in the statistics. */
193 ggc_collect ();
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));
221 fprintf (stream,
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",
234 rtx_name[code],
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));
241 fprintf (stream,
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. */
248 ggc_stats = NULL;