1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
15
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. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "rtl.h"
24 #include "tree.h"
25 #include "tm_p.h"
26 #include "flags.h"
27 #include "varray.h"
28 #include "ggc.h"
29 #include "timevar.h"
30 #include "params.h"
31
32 /* Debugging flags. */
33
34 /* Zap memory before freeing to catch dangling pointers. */
35 #undef GGC_POISON
36
37 /* Collect statistics on how bushy the search tree is. */
38 #undef GGC_BALANCE
39
40 /* Always verify that the to-be-marked memory is collectable. */
41 #undef GGC_ALWAYS_VERIFY
42
43 #ifdef ENABLE_GC_CHECKING
44 #define GGC_POISON
45 #define GGC_ALWAYS_VERIFY
46 #endif
47
48 #ifndef HOST_BITS_PER_PTR
49 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
50 #endif
51
52 /* We'd like a balanced tree, but we don't really want to pay for the
53 cost of keeping the tree balanced. We'll settle for the next best
54 thing -- nearly balanced.
55
56 In this context, the most natural key is the node pointer itself,
57 but due to the way memory managers work, we'd be virtually certain
58 to wind up with a completely degenerate straight line. What's needed
59 is to make something more variable, and yet predictable, be more
60 significant in the comparison.
61
62 The handiest source of variability is the low bits of the pointer
63 value itself. Any sort of bit/byte swap would do, but such machine
64 specific operations are not handy, and we don't want to put that much
65 effort into it. */
66
67 #define PTR_KEY(p) ((size_t)p << (HOST_BITS_PER_PTR - 8) \
68 | ((size_t)p & 0xff00) << (HOST_BITS_PER_PTR - 24) \
69 | (size_t)p >> 16)
70
71 /* GC'able memory; a node in a binary search tree. */
72
73 struct ggc_mem
74 {
75 /* A combination of the standard left/right nodes, indexable by `<'. */
76 struct ggc_mem *sub[2];
77
78 unsigned int mark : 1;
79 unsigned int context : 7;
80 unsigned int size : 24;
81
82 /* Make sure the data is reasonably aligned. */
83 union {
84 HOST_WIDEST_INT i;
85 #ifdef HAVE_LONG_DOUBLE
86 long double d;
87 #else
88 double d;
89 #endif
90 } u;
91 };
92
93 static struct globals
94 {
95 /* Root of the object tree. */
96 struct ggc_mem *root;
97
98 /* Data bytes currently allocated. */
99 size_t allocated;
100
101 /* Data objects currently allocated. */
102 size_t objects;
103
104 /* Data bytes allocated at time of last GC. */
105 size_t allocated_last_gc;
106
107 /* Current context level. */
108 int context;
109 } G;
110
111 /* Local function prototypes. */
112
113 static void tree_insert PARAMS ((struct ggc_mem *));
114 static int tree_lookup PARAMS ((struct ggc_mem *));
115 static void clear_marks PARAMS ((struct ggc_mem *));
116 static void sweep_objs PARAMS ((struct ggc_mem **));
117 static void ggc_pop_context_1 PARAMS ((struct ggc_mem *, int));
118
119 /* For use from debugger. */
120 extern void debug_ggc_tree PARAMS ((struct ggc_mem *, int));
121
122 #ifdef GGC_BALANCE
123 extern void debug_ggc_balance PARAMS ((void));
124 #endif
125 static void tally_leaves PARAMS ((struct ggc_mem *, int, size_t *, size_t *));
126
127 /* Insert V into the search tree. */
128
129 static inline void
tree_insert(v)130 tree_insert (v)
131 struct ggc_mem *v;
132 {
133 size_t v_key = PTR_KEY (v);
134 struct ggc_mem *p, **pp;
135
136 for (pp = &G.root, p = *pp; p ; p = *pp)
137 {
138 size_t p_key = PTR_KEY (p);
139 pp = &p->sub[v_key < p_key];
140 }
141 *pp = v;
142 }
143
144 /* Return true if V is in the tree. */
145
146 static inline int
tree_lookup(v)147 tree_lookup (v)
148 struct ggc_mem *v;
149 {
150 size_t v_key = PTR_KEY (v);
151 struct ggc_mem *p = G.root;
152
153 while (p)
154 {
155 size_t p_key = PTR_KEY (p);
156 if (p == v)
157 return 1;
158 p = p->sub[v_key < p_key];
159 }
160
161 return 0;
162 }
163
164 /* Alloc SIZE bytes of GC'able memory. If ZERO, clear the memory. */
165
166 void *
ggc_alloc(size)167 ggc_alloc (size)
168 size_t size;
169 {
170 struct ggc_mem *x;
171
172 x = (struct ggc_mem *) xmalloc (offsetof (struct ggc_mem, u) + size);
173 x->sub[0] = NULL;
174 x->sub[1] = NULL;
175 x->mark = 0;
176 x->context = G.context;
177 x->size = size;
178
179 #ifdef GGC_POISON
180 memset (&x->u, 0xaf, size);
181 #endif
182
183 tree_insert (x);
184 G.allocated += size;
185 G.objects += 1;
186
187 return &x->u;
188 }
189
190 /* Mark a node. */
191
192 int
ggc_set_mark(p)193 ggc_set_mark (p)
194 const void *p;
195 {
196 struct ggc_mem *x;
197
198 x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
199 #ifdef GGC_ALWAYS_VERIFY
200 if (! tree_lookup (x))
201 abort ();
202 #endif
203
204 if (x->mark)
205 return 1;
206
207 x->mark = 1;
208 G.allocated += x->size;
209 G.objects += 1;
210
211 return 0;
212 }
213
214 /* Return 1 if P has been marked, zero otherwise. */
215
216 int
ggc_marked_p(p)217 ggc_marked_p (p)
218 const void *p;
219 {
220 struct ggc_mem *x;
221
222 x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
223 #ifdef GGC_ALWAYS_VERIFY
224 if (! tree_lookup (x))
225 abort ();
226 #endif
227
228 return x->mark;
229 }
230
231 /* Return the size of the gc-able object P. */
232
233 size_t
ggc_get_size(p)234 ggc_get_size (p)
235 const void *p;
236 {
237 struct ggc_mem *x
238 = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
239 return x->size;
240 }
241
242 /* Unmark all objects. */
243
244 static void
clear_marks(x)245 clear_marks (x)
246 struct ggc_mem *x;
247 {
248 x->mark = 0;
249 if (x->sub[0])
250 clear_marks (x->sub[0]);
251 if (x->sub[1])
252 clear_marks (x->sub[1]);
253 }
254
255 /* Free all objects in the current context that are not marked. */
256
257 static void
sweep_objs(root)258 sweep_objs (root)
259 struct ggc_mem **root;
260 {
261 struct ggc_mem *x = *root;
262 if (!x)
263 return;
264
265 sweep_objs (&x->sub[0]);
266 sweep_objs (&x->sub[1]);
267
268 if (! x->mark && x->context >= G.context)
269 {
270 struct ggc_mem *l, *r;
271
272 l = x->sub[0];
273 r = x->sub[1];
274 if (!l)
275 *root = r;
276 else if (!r)
277 *root = l;
278 else if (!l->sub[1])
279 {
280 *root = l;
281 l->sub[1] = r;
282 }
283 else if (!r->sub[0])
284 {
285 *root = r;
286 r->sub[0] = l;
287 }
288 else
289 {
290 *root = l;
291 do {
292 root = &l->sub[1];
293 } while ((l = *root) != NULL);
294 *root = r;
295 }
296
297 #ifdef GGC_POISON
298 memset (&x->u, 0xA5, x->size);
299 #endif
300
301 free (x);
302 }
303 }
304
305 /* The top level mark-and-sweep routine. */
306
307 void
ggc_collect()308 ggc_collect ()
309 {
310 /* Avoid frequent unnecessary work by skipping collection if the
311 total allocations haven't expanded much since the last
312 collection. */
313 size_t allocated_last_gc =
314 MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
315
316 size_t min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
317
318 if (G.allocated < allocated_last_gc + min_expand)
319 return;
320
321 #ifdef GGC_BALANCE
322 debug_ggc_balance ();
323 #endif
324
325 timevar_push (TV_GC);
326 if (!quiet_flag)
327 fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
328
329 G.allocated = 0;
330 G.objects = 0;
331
332 clear_marks (G.root);
333 ggc_mark_roots ();
334 sweep_objs (&G.root);
335
336 G.allocated_last_gc = G.allocated;
337
338 timevar_pop (TV_GC);
339
340 if (!quiet_flag)
341 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
342
343 #ifdef GGC_BALANCE
344 debug_ggc_balance ();
345 #endif
346 }
347
348 /* Called once to initialize the garbage collector. */
349
350 void
init_ggc()351 init_ggc ()
352 {
353 }
354
355 /* Start a new GGC context. Memory allocated in previous contexts
356 will not be collected while the new context is active. */
357
358 void
ggc_push_context()359 ggc_push_context ()
360 {
361 G.context++;
362
363 /* We only allocated 7 bits in the node for the context. This
364 should be more than enough. */
365 if (G.context >= 128)
366 abort ();
367 }
368
369 /* Finish a GC context. Any uncollected memory in the new context
370 will be merged with the old context. */
371
372 void
ggc_pop_context()373 ggc_pop_context ()
374 {
375 G.context--;
376 if (G.root)
377 ggc_pop_context_1 (G.root, G.context);
378 }
379
380 static void
ggc_pop_context_1(x,c)381 ggc_pop_context_1 (x, c)
382 struct ggc_mem *x;
383 int c;
384 {
385 if (x->context > c)
386 x->context = c;
387 if (x->sub[0])
388 ggc_pop_context_1 (x->sub[0], c);
389 if (x->sub[1])
390 ggc_pop_context_1 (x->sub[1], c);
391 }
392
393 /* Dump a tree. */
394
395 void
debug_ggc_tree(p,indent)396 debug_ggc_tree (p, indent)
397 struct ggc_mem *p;
398 int indent;
399 {
400 int i;
401
402 if (!p)
403 {
404 fputs ("(nil)\n", stderr);
405 return;
406 }
407
408 if (p->sub[0])
409 debug_ggc_tree (p->sub[0], indent + 1);
410
411 for (i = 0; i < indent; ++i)
412 putc (' ', stderr);
413 fprintf (stderr, "%lx %p\n", (unsigned long)PTR_KEY (p), p);
414
415 if (p->sub[1])
416 debug_ggc_tree (p->sub[1], indent + 1);
417 }
418
419 #ifdef GGC_BALANCE
420 /* Collect tree balance metrics */
421
422 #include <math.h>
423
424 void
debug_ggc_balance()425 debug_ggc_balance ()
426 {
427 size_t nleaf, sumdepth;
428
429 nleaf = sumdepth = 0;
430 tally_leaves (G.root, 0, &nleaf, &sumdepth);
431
432 fprintf (stderr, " {B %.2f,%.1f,%.1f}",
433 /* In a balanced tree, leaf/node should approach 1/2. */
434 (float)nleaf / (float)G.objects,
435 /* In a balanced tree, average leaf depth should approach lg(n). */
436 (float)sumdepth / (float)nleaf,
437 log ((double) G.objects) / M_LN2);
438 }
439 #endif
440
441 /* Used by debug_ggc_balance, and also by ggc_print_statistics. */
442 static void
tally_leaves(x,depth,nleaf,sumdepth)443 tally_leaves (x, depth, nleaf, sumdepth)
444 struct ggc_mem *x;
445 int depth;
446 size_t *nleaf;
447 size_t *sumdepth;
448 {
449 if (! x->sub[0] && !x->sub[1])
450 {
451 *nleaf += 1;
452 *sumdepth += depth;
453 }
454 else
455 {
456 if (x->sub[0])
457 tally_leaves (x->sub[0], depth + 1, nleaf, sumdepth);
458 if (x->sub[1])
459 tally_leaves (x->sub[1], depth + 1, nleaf, sumdepth);
460 }
461 }
462
463 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
464 ? (x) \
465 : ((x) < 1024*1024*10 \
466 ? (x) / 1024 \
467 : (x) / (1024*1024))))
468 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
469
470 /* Report on GC memory usage. */
471 void
ggc_print_statistics()472 ggc_print_statistics ()
473 {
474 struct ggc_statistics stats;
475 size_t nleaf = 0, sumdepth = 0;
476
477 /* Clear the statistics. */
478 memset (&stats, 0, sizeof (stats));
479
480 /* Make sure collection will really occur. */
481 G.allocated_last_gc = 0;
482
483 /* Collect and print the statistics common across collectors. */
484 ggc_print_common_statistics (stderr, &stats);
485
486 /* Report on tree balancing. */
487 tally_leaves (G.root, 0, &nleaf, &sumdepth);
488
489 fprintf (stderr, "\n\
490 Total internal data (bytes)\t%ld%c\n\
491 Number of leaves in tree\t%d\n\
492 Average leaf depth\t\t%.1f\n",
493 SCALE(G.objects * offsetof (struct ggc_mem, u)),
494 LABEL(G.objects * offsetof (struct ggc_mem, u)),
495 nleaf, (double)sumdepth / (double)nleaf);
496
497 /* Report overall memory usage. */
498 fprintf (stderr, "\n\
499 Total objects allocated\t\t%d\n\
500 Total memory in GC arena\t%ld%c\n",
501 G.objects,
502 SCALE(G.allocated), LABEL(G.allocated));
503 }
504