Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / gcc-3.4 / 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 struct alloc_zone *rtl_zone = NULL;
128 struct alloc_zone *tree_zone = NULL;
129 struct alloc_zone *garbage_zone = NULL;
130
131 /* Insert V into the search tree.  */
132
133 static inline void
134 tree_insert (struct ggc_mem *v)
135 {
136   size_t v_key = PTR_KEY (v);
137   struct ggc_mem *p, **pp;
138
139   for (pp = &G.root, p = *pp; p ; p = *pp)
140     {
141       size_t p_key = PTR_KEY (p);
142       pp = &p->sub[v_key < p_key];
143     }
144   *pp = v;
145 }
146
147 /* Return true if V is in the tree.  */
148
149 static inline int
150 tree_lookup (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 /* Typed allocation function.  Does nothing special in this collector.  */
167
168 void *
169 ggc_alloc_typed (enum gt_types_enum type ATTRIBUTE_UNUSED, size_t size)
170 {
171   return ggc_alloc (size);
172 }
173
174 /* Zone allocation function.  Does nothing special in this collector.  */
175
176 void *
177 ggc_alloc_zone (size_t size, struct alloc_zone *zone ATTRIBUTE_UNUSED)
178 {
179   return ggc_alloc (size);
180 }
181
182 /* Alloc SIZE bytes of GC'able memory.  If ZERO, clear the memory.  */
183
184 void *
185 ggc_alloc (size_t size)
186 {
187   struct ggc_mem *x;
188
189   x = xmalloc (offsetof (struct ggc_mem, u) + size);
190   x->sub[0] = NULL;
191   x->sub[1] = NULL;
192   x->mark = 0;
193   x->context = G.context;
194   x->size = size;
195
196 #ifdef GGC_POISON
197   memset (&x->u, 0xaf, size);
198 #endif
199
200   tree_insert (x);
201   G.allocated += size;
202   G.objects += 1;
203
204   return &x->u;
205 }
206
207 /* Mark a node.  */
208
209 int
210 ggc_set_mark (const void *p)
211 {
212   struct ggc_mem *x;
213
214   x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
215 #ifdef GGC_ALWAYS_VERIFY
216   if (! tree_lookup (x))
217     abort ();
218 #endif
219
220   if (x->mark)
221     return 1;
222
223   x->mark = 1;
224   G.allocated += x->size;
225   G.objects += 1;
226
227   return 0;
228 }
229
230 /* Return 1 if P has been marked, zero otherwise.  */
231
232 int
233 ggc_marked_p (const void *p)
234 {
235   struct ggc_mem *x;
236
237   x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
238 #ifdef GGC_ALWAYS_VERIFY
239   if (! tree_lookup (x))
240     abort ();
241 #endif
242
243    return x->mark;
244 }
245
246 /* Return the size of the gc-able object P.  */
247
248 size_t
249 ggc_get_size (const void *p)
250 {
251   struct ggc_mem *x
252     = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
253   return x->size;
254 }
255
256 /* Unmark all objects.  */
257
258 static void
259 clear_marks (struct ggc_mem *x)
260 {
261   x->mark = 0;
262   if (x->sub[0])
263     clear_marks (x->sub[0]);
264   if (x->sub[1])
265     clear_marks (x->sub[1]);
266 }
267
268 /* Free all objects in the current context that are not marked.  */
269
270 static void
271 sweep_objs (struct ggc_mem **root)
272 {
273   struct ggc_mem *x = *root;
274   if (!x)
275     return;
276
277   sweep_objs (&x->sub[0]);
278   sweep_objs (&x->sub[1]);
279
280   if (! x->mark && x->context >= G.context)
281     {
282       struct ggc_mem *l, *r;
283
284       l = x->sub[0];
285       r = x->sub[1];
286       if (!l)
287         *root = r;
288       else if (!r)
289         *root = l;
290       else if (!l->sub[1])
291         {
292           *root = l;
293           l->sub[1] = r;
294         }
295       else if (!r->sub[0])
296         {
297           *root = r;
298           r->sub[0] = l;
299         }
300       else
301         {
302           *root = l;
303           do {
304             root = &l->sub[1];
305           } while ((l = *root) != NULL);
306           *root = r;
307         }
308
309 #ifdef GGC_POISON
310       memset (&x->u, 0xA5, x->size);
311 #endif
312
313       free (x);
314     }
315 }
316
317 /* The top level mark-and-sweep routine.  */
318
319 void
320 ggc_collect (void)
321 {
322   /* Avoid frequent unnecessary work by skipping collection if the
323      total allocations haven't expanded much since the last
324      collection.  */
325   size_t allocated_last_gc =
326     MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
327
328   size_t min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
329
330   if (G.allocated < allocated_last_gc + min_expand)
331     return;
332
333 #ifdef GGC_BALANCE
334   debug_ggc_balance ();
335 #endif
336
337   timevar_push (TV_GC);
338   if (!quiet_flag)
339     fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
340
341   G.allocated = 0;
342   G.objects = 0;
343
344   clear_marks (G.root);
345   ggc_mark_roots ();
346   sweep_objs (&G.root);
347
348   G.allocated_last_gc = G.allocated;
349
350   timevar_pop (TV_GC);
351
352   if (!quiet_flag)
353     fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
354
355 #ifdef GGC_BALANCE
356   debug_ggc_balance ();
357 #endif
358 }
359
360 /* Called once to initialize the garbage collector.  */
361
362 void
363 init_ggc (void)
364 {
365 }
366
367 /* Start a new GGC zone.  */
368
369 struct alloc_zone *
370 new_ggc_zone (const char *name ATTRIBUTE_UNUSED)
371 {
372   return NULL;
373 }
374
375 /* Destroy a GGC zone.  */
376 void
377 destroy_ggc_zone (struct alloc_zone *zone ATTRIBUTE_UNUSED)
378 {
379 }
380
381 /* Start a new GGC context.  Memory allocated in previous contexts
382    will not be collected while the new context is active.  */
383
384 void
385 ggc_push_context (void)
386 {
387   G.context++;
388
389   /* We only allocated 7 bits in the node for the context.  This
390      should be more than enough.  */
391   if (G.context >= 128)
392     abort ();
393 }
394
395 /* Finish a GC context.  Any uncollected memory in the new context
396    will be merged with the old context.  */
397
398 void
399 ggc_pop_context (void)
400 {
401   G.context--;
402   if (G.root)
403     ggc_pop_context_1 (G.root, G.context);
404 }
405
406 static void
407 ggc_pop_context_1 (struct ggc_mem *x, int c)
408 {
409   if (x->context > c)
410     x->context = c;
411   if (x->sub[0])
412     ggc_pop_context_1 (x->sub[0], c);
413   if (x->sub[1])
414     ggc_pop_context_1 (x->sub[1], c);
415 }
416
417 /* Dump a tree.  */
418
419 void
420 debug_ggc_tree (struct ggc_mem *p, int indent)
421 {
422   int i;
423
424   if (!p)
425     {
426       fputs ("(nil)\n", stderr);
427       return;
428     }
429
430   if (p->sub[0])
431     debug_ggc_tree (p->sub[0], indent + 1);
432
433   for (i = 0; i < indent; ++i)
434     putc (' ', stderr);
435   fprintf (stderr, "%lx %p\n", (unsigned long)PTR_KEY (p), (void *) p);
436
437   if (p->sub[1])
438     debug_ggc_tree (p->sub[1], indent + 1);
439 }
440
441 #ifdef GGC_BALANCE
442 /* Collect tree balance metrics  */
443
444 #include <math.h>
445
446 void
447 debug_ggc_balance (void)
448 {
449   size_t nleaf, sumdepth;
450
451   nleaf = sumdepth = 0;
452   tally_leaves (G.root, 0, &nleaf, &sumdepth);
453
454   fprintf (stderr, " {B %.2f,%.1f,%.1f}",
455            /* In a balanced tree, leaf/node should approach 1/2.  */
456            (float)nleaf / (float)G.objects,
457            /* In a balanced tree, average leaf depth should approach lg(n).  */
458            (float)sumdepth / (float)nleaf,
459            log ((double) G.objects) / M_LN2);
460 }
461 #endif
462
463 /* Used by debug_ggc_balance, and also by ggc_print_statistics.  */
464 static void
465 tally_leaves (struct ggc_mem *x, int depth, size_t *nleaf, size_t *sumdepth)
466 {
467   if (! x->sub[0] && !x->sub[1])
468     {
469       *nleaf += 1;
470       *sumdepth += depth;
471     }
472   else
473     {
474       if (x->sub[0])
475         tally_leaves (x->sub[0], depth + 1, nleaf, sumdepth);
476       if (x->sub[1])
477         tally_leaves (x->sub[1], depth + 1, nleaf, sumdepth);
478     }
479 }
480
481 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
482                   ? (x) \
483                   : ((x) < 1024*1024*10 \
484                      ? (x) / 1024 \
485                      : (x) / (1024*1024))))
486 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
487
488 /* Report on GC memory usage.  */
489 void
490 ggc_print_statistics (void)
491 {
492   struct ggc_statistics stats;
493   size_t nleaf = 0, sumdepth = 0;
494
495   /* Clear the statistics.  */
496   memset (&stats, 0, sizeof (stats));
497
498   /* Make sure collection will really occur.  */
499   G.allocated_last_gc = 0;
500
501   /* Collect and print the statistics common across collectors.  */
502   ggc_print_common_statistics (stderr, &stats);
503
504   /* Report on tree balancing.  */
505   tally_leaves (G.root, 0, &nleaf, &sumdepth);
506
507   fprintf (stderr, "\n\
508 Total internal data (bytes)\t%ld%c\n\
509 Number of leaves in tree\t%lu\n\
510 Average leaf depth\t\t%.1f\n",
511            SCALE(G.objects * offsetof (struct ggc_mem, u)),
512            LABEL(G.objects * offsetof (struct ggc_mem, u)),
513            (unsigned long)nleaf, (double)sumdepth / (double)nleaf);
514
515   /* Report overall memory usage.  */
516   fprintf (stderr, "\n\
517 Total objects allocated\t\t%ld\n\
518 Total memory in GC arena\t%ld%c\n",
519            (unsigned long)G.objects,
520            SCALE(G.allocated), LABEL(G.allocated));
521 }
522 \f
523 struct ggc_pch_data *
524 init_ggc_pch (void)
525 {
526   sorry ("Generating PCH files is not supported when using ggc-simple.c");
527   /* It could be supported, but the code is not yet written.  */
528   return NULL;
529 }
530
531 void
532 ggc_pch_count_object (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
533                       void *x ATTRIBUTE_UNUSED,
534                       size_t size ATTRIBUTE_UNUSED,
535                       bool is_string ATTRIBUTE_UNUSED)
536 {
537 }
538
539 size_t
540 ggc_pch_total_size (struct ggc_pch_data *d ATTRIBUTE_UNUSED)
541 {
542   return 0;
543 }
544
545 void
546 ggc_pch_this_base (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
547                    void *base ATTRIBUTE_UNUSED)
548 {
549 }
550
551
552 char *
553 ggc_pch_alloc_object (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
554                       void *x ATTRIBUTE_UNUSED,
555                       size_t size ATTRIBUTE_UNUSED,
556                       bool is_string ATTRIBUTE_UNUSED)
557 {
558   return NULL;
559 }
560
561 void
562 ggc_pch_prepare_write (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
563                        FILE * f ATTRIBUTE_UNUSED)
564 {
565 }
566
567 void
568 ggc_pch_write_object (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
569                       FILE *f ATTRIBUTE_UNUSED, void *x ATTRIBUTE_UNUSED,
570                       void *newx ATTRIBUTE_UNUSED,
571                       size_t size ATTRIBUTE_UNUSED,
572                       bool is_string ATTRIBUTE_UNUSED)
573 {
574 }
575
576 void
577 ggc_pch_finish (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
578                 FILE *f ATTRIBUTE_UNUSED)
579 {
580 }
581
582 void
583 ggc_pch_read (FILE *f ATTRIBUTE_UNUSED, void *addr ATTRIBUTE_UNUSED)
584 {
585   /* This should be impossible, since we won't generate any valid PCH
586      files for this configuration.  */
587   abort ();
588 }