OSDN Git Service

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