OSDN Git Service

cp:
[pf3gnuchains/gcc-fork.git] / gcc / ggc-simple.c
1 /* Simple garbage collection for the GNU compiler.
2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "flags.h"
30 #include "varray.h"
31 #include "ggc.h"
32 #include "toplev.h"
33 #include "timevar.h"
34 #include "params.h"
35
36 /* Debugging flags.  */
37
38 /* Zap memory before freeing to catch dangling pointers.  */
39 #undef GGC_POISON
40
41 /* Collect statistics on how bushy the search tree is.  */
42 #undef GGC_BALANCE
43
44 /* Always verify that the to-be-marked memory is collectable.  */
45 #undef GGC_ALWAYS_VERIFY
46
47 #ifdef ENABLE_GC_CHECKING
48 #define GGC_POISON
49 #define GGC_ALWAYS_VERIFY
50 #endif
51
52 #ifndef HOST_BITS_PER_PTR
53 #define HOST_BITS_PER_PTR  HOST_BITS_PER_LONG
54 #endif
55
56 /* We'd like a balanced tree, but we don't really want to pay for the
57    cost of keeping the tree balanced.  We'll settle for the next best
58    thing -- nearly balanced.
59
60    In this context, the most natural key is the node pointer itself,
61    but due to the way memory managers work, we'd be virtually certain
62    to wind up with a completely degenerate straight line.  What's needed
63    is to make something more variable, and yet predictable, be more
64    significant in the comparison.
65
66    The handiest source of variability is the low bits of the pointer
67    value itself.  Any sort of bit/byte swap would do, but such machine
68    specific operations are not handy, and we don't want to put that much
69    effort into it.  */
70
71 #define PTR_KEY(p)      ((size_t)p << (HOST_BITS_PER_PTR - 8)               \
72                          | ((size_t)p & 0xff00) << (HOST_BITS_PER_PTR - 24) \
73                          | (size_t)p >> 16)
74
75 /* GC'able memory; a node in a binary search tree.  */
76
77 struct ggc_mem
78 {
79   /* A combination of the standard left/right nodes, indexable by `<'.  */
80   struct ggc_mem *sub[2];
81
82   unsigned int mark : 1;
83   unsigned int context : 7;
84   unsigned int size : 24;
85
86   /* Make sure the data is reasonably aligned.  */
87   union {
88     HOST_WIDEST_INT i;
89     long double d;
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 (struct ggc_mem *);
114 static int tree_lookup (struct ggc_mem *);
115 static void clear_marks (struct ggc_mem *);
116 static void sweep_objs (struct ggc_mem **);
117 static void ggc_pop_context_1 (struct ggc_mem *, int);
118
119 /* For use from debugger.  */
120 extern void debug_ggc_tree (struct ggc_mem *, int);
121
122 #ifdef GGC_BALANCE
123 extern void debug_ggc_balance (void);
124 #endif
125 static void tally_leaves (struct ggc_mem *, int, size_t *, size_t *);
126
127 /* Insert V into the search tree.  */
128
129 static inline void
130 tree_insert (struct ggc_mem *v)
131 {
132   size_t v_key = PTR_KEY (v);
133   struct ggc_mem *p, **pp;
134
135   for (pp = &G.root, p = *pp; p ; p = *pp)
136     {
137       size_t p_key = PTR_KEY (p);
138       pp = &p->sub[v_key < p_key];
139     }
140   *pp = v;
141 }
142
143 /* Return true if V is in the tree.  */
144
145 static inline int
146 tree_lookup (struct ggc_mem *v)
147 {
148   size_t v_key = PTR_KEY (v);
149   struct ggc_mem *p = G.root;
150
151   while (p)
152     {
153       size_t p_key = PTR_KEY (p);
154       if (p == v)
155         return 1;
156       p = p->sub[v_key < p_key];
157     }
158
159   return 0;
160 }
161
162 /* Alloc SIZE bytes of GC'able memory.  If ZERO, clear the memory.  */
163
164 void *
165 ggc_alloc (size_t size)
166 {
167   struct ggc_mem *x;
168
169   x = xmalloc (offsetof (struct ggc_mem, u) + size);
170   x->sub[0] = NULL;
171   x->sub[1] = NULL;
172   x->mark = 0;
173   x->context = G.context;
174   x->size = size;
175
176 #ifdef GGC_POISON
177   memset (&x->u, 0xaf, size);
178 #endif
179
180   tree_insert (x);
181   G.allocated += size;
182   G.objects += 1;
183
184   return &x->u;
185 }
186
187 /* Mark a node.  */
188
189 int
190 ggc_set_mark (const void *p)
191 {
192   struct ggc_mem *x;
193
194   x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
195 #ifdef GGC_ALWAYS_VERIFY
196   if (! tree_lookup (x))
197     abort ();
198 #endif
199
200   if (x->mark)
201     return 1;
202
203   x->mark = 1;
204   G.allocated += x->size;
205   G.objects += 1;
206
207   return 0;
208 }
209
210 /* Return 1 if P has been marked, zero otherwise.  */
211
212 int
213 ggc_marked_p (const void *p)
214 {
215   struct ggc_mem *x;
216
217   x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
218 #ifdef GGC_ALWAYS_VERIFY
219   if (! tree_lookup (x))
220     abort ();
221 #endif
222
223    return x->mark;
224 }
225
226 /* Return the size of the gc-able object P.  */
227
228 size_t
229 ggc_get_size (const void *p)
230 {
231   struct ggc_mem *x
232     = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
233   return x->size;
234 }
235
236 /* Unmark all objects.  */
237
238 static void
239 clear_marks (struct ggc_mem *x)
240 {
241   x->mark = 0;
242   if (x->sub[0])
243     clear_marks (x->sub[0]);
244   if (x->sub[1])
245     clear_marks (x->sub[1]);
246 }
247
248 /* Free all objects in the current context that are not marked.  */
249
250 static void
251 sweep_objs (struct ggc_mem **root)
252 {
253   struct ggc_mem *x = *root;
254   if (!x)
255     return;
256
257   sweep_objs (&x->sub[0]);
258   sweep_objs (&x->sub[1]);
259
260   if (! x->mark && x->context >= G.context)
261     {
262       struct ggc_mem *l, *r;
263
264       l = x->sub[0];
265       r = x->sub[1];
266       if (!l)
267         *root = r;
268       else if (!r)
269         *root = l;
270       else if (!l->sub[1])
271         {
272           *root = l;
273           l->sub[1] = r;
274         }
275       else if (!r->sub[0])
276         {
277           *root = r;
278           r->sub[0] = l;
279         }
280       else
281         {
282           *root = l;
283           do {
284             root = &l->sub[1];
285           } while ((l = *root) != NULL);
286           *root = r;
287         }
288
289 #ifdef GGC_POISON
290       memset (&x->u, 0xA5, x->size);
291 #endif
292
293       free (x);
294     }
295 }
296
297 /* The top level mark-and-sweep routine.  */
298
299 void
300 ggc_collect (void)
301 {
302   /* Avoid frequent unnecessary work by skipping collection if the
303      total allocations haven't expanded much since the last
304      collection.  */
305   size_t allocated_last_gc =
306     MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
307
308   size_t min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
309
310   if (G.allocated < allocated_last_gc + min_expand)
311     return;
312
313 #ifdef GGC_BALANCE
314   debug_ggc_balance ();
315 #endif
316
317   timevar_push (TV_GC);
318   if (!quiet_flag)
319     fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
320
321   G.allocated = 0;
322   G.objects = 0;
323
324   clear_marks (G.root);
325   ggc_mark_roots ();
326   sweep_objs (&G.root);
327
328   G.allocated_last_gc = G.allocated;
329
330   timevar_pop (TV_GC);
331
332   if (!quiet_flag)
333     fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
334
335 #ifdef GGC_BALANCE
336   debug_ggc_balance ();
337 #endif
338 }
339
340 /* Called once to initialize the garbage collector.  */
341
342 void
343 init_ggc (void)
344 {
345 }
346
347 /* Start a new GGC zone.  */
348
349 struct alloc_zone *
350 new_ggc_zone (const char *name ATTRIBUTE_UNUSED)
351 {
352   return NULL;
353 }
354
355 /* Destroy a GGC zone.  */
356 void
357 destroy_ggc_zone (struct alloc_zone *zone ATTRIBUTE_UNUSED)
358 {
359 }
360
361 /* Start a new GGC context.  Memory allocated in previous contexts
362    will not be collected while the new context is active.  */
363
364 void
365 ggc_push_context (void)
366 {
367   G.context++;
368
369   /* We only allocated 7 bits in the node for the context.  This
370      should be more than enough.  */
371   if (G.context >= 128)
372     abort ();
373 }
374
375 /* Finish a GC context.  Any uncollected memory in the new context
376    will be merged with the old context.  */
377
378 void
379 ggc_pop_context (void)
380 {
381   G.context--;
382   if (G.root)
383     ggc_pop_context_1 (G.root, G.context);
384 }
385
386 static void
387 ggc_pop_context_1 (struct ggc_mem *x, int c)
388 {
389   if (x->context > c)
390     x->context = c;
391   if (x->sub[0])
392     ggc_pop_context_1 (x->sub[0], c);
393   if (x->sub[1])
394     ggc_pop_context_1 (x->sub[1], c);
395 }
396
397 /* Dump a tree.  */
398
399 void
400 debug_ggc_tree (struct ggc_mem *p, 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), (void *) 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 (void)
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 (struct ggc_mem *x, int depth, size_t *nleaf, size_t *sumdepth)
446 {
447   if (! x->sub[0] && !x->sub[1])
448     {
449       *nleaf += 1;
450       *sumdepth += depth;
451     }
452   else
453     {
454       if (x->sub[0])
455         tally_leaves (x->sub[0], depth + 1, nleaf, sumdepth);
456       if (x->sub[1])
457         tally_leaves (x->sub[1], depth + 1, nleaf, sumdepth);
458     }
459 }
460
461 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
462                   ? (x) \
463                   : ((x) < 1024*1024*10 \
464                      ? (x) / 1024 \
465                      : (x) / (1024*1024))))
466 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
467
468 /* Report on GC memory usage.  */
469 void
470 ggc_print_statistics (void)
471 {
472   struct ggc_statistics stats;
473   size_t nleaf = 0, sumdepth = 0;
474
475   /* Clear the statistics.  */
476   memset (&stats, 0, sizeof (stats));
477
478   /* Make sure collection will really occur.  */
479   G.allocated_last_gc = 0;
480
481   /* Collect and print the statistics common across collectors.  */
482   ggc_print_common_statistics (stderr, &stats);
483
484   /* Report on tree balancing.  */
485   tally_leaves (G.root, 0, &nleaf, &sumdepth);
486
487   fprintf (stderr, "\n\
488 Total internal data (bytes)\t%ld%c\n\
489 Number of leaves in tree\t%lu\n\
490 Average leaf depth\t\t%.1f\n",
491            SCALE(G.objects * offsetof (struct ggc_mem, u)),
492            LABEL(G.objects * offsetof (struct ggc_mem, u)),
493            (unsigned long)nleaf, (double)sumdepth / (double)nleaf);
494
495   /* Report overall memory usage.  */
496   fprintf (stderr, "\n\
497 Total objects allocated\t\t%ld\n\
498 Total memory in GC arena\t%ld%c\n",
499            (unsigned long)G.objects,
500            SCALE(G.allocated), LABEL(G.allocated));
501 }
502 \f
503 struct ggc_pch_data *
504 init_ggc_pch (void)
505 {
506   sorry ("Generating PCH files is not supported when using ggc-simple.c");
507   /* It could be supported, but the code is not yet written.  */
508   return NULL;
509 }
510
511 void
512 ggc_pch_count_object (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
513                       void *x ATTRIBUTE_UNUSED,
514                       size_t size ATTRIBUTE_UNUSED)
515 {
516 }
517
518 size_t
519 ggc_pch_total_size (struct ggc_pch_data *d ATTRIBUTE_UNUSED)
520 {
521   return 0;
522 }
523
524 void
525 ggc_pch_this_base (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
526                    void *base ATTRIBUTE_UNUSED)
527 {
528 }
529
530
531 char *
532 ggc_pch_alloc_object (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
533                       void *x ATTRIBUTE_UNUSED,
534                       size_t size ATTRIBUTE_UNUSED)
535 {
536   return NULL;
537 }
538
539 void
540 ggc_pch_prepare_write (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
541                        FILE * f ATTRIBUTE_UNUSED)
542 {
543 }
544
545 void
546 ggc_pch_write_object (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
547                       FILE *f ATTRIBUTE_UNUSED, void *x ATTRIBUTE_UNUSED,
548                       void *newx ATTRIBUTE_UNUSED,
549                       size_t size ATTRIBUTE_UNUSED)
550 {
551 }
552
553 void
554 ggc_pch_finish (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
555                 FILE *f ATTRIBUTE_UNUSED)
556 {
557 }
558
559 void
560 ggc_pch_read (FILE *f ATTRIBUTE_UNUSED, void *addr ATTRIBUTE_UNUSED)
561 {
562   /* This should be impossible, since we won't generate any valid PCH
563      files for this configuration.  */
564   abort ();
565 }