index.html (3.10): Add note about mips atomicity.h.
[official-gcc.git] / gcc / ggc-simple.c
blob9964b891e0df83696abc011cc8b8f080d208e5f4
1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1998, 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
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.
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.
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 #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 "toplev.h"
32 #include "timevar.h"
33 #include "params.h"
35 /* Debugging flags. */
37 /* Zap memory before freeing to catch dangling pointers. */
38 #undef GGC_POISON
40 /* Collect statistics on how bushy the search tree is. */
41 #undef GGC_BALANCE
43 /* Always verify that the to-be-marked memory is collectable. */
44 #undef GGC_ALWAYS_VERIFY
46 #ifdef ENABLE_GC_CHECKING
47 #define GGC_POISON
48 #define GGC_ALWAYS_VERIFY
49 #endif
51 #ifndef HOST_BITS_PER_PTR
52 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
53 #endif
55 /* We'd like a balanced tree, but we don't really want to pay for the
56 cost of keeping the tree balanced. We'll settle for the next best
57 thing -- nearly balanced.
59 In this context, the most natural key is the node pointer itself,
60 but due to the way memory managers work, we'd be virtually certain
61 to wind up with a completely degenerate straight line. What's needed
62 is to make something more variable, and yet predictable, be more
63 significant in the comparison.
65 The handiest source of variability is the low bits of the pointer
66 value itself. Any sort of bit/byte swap would do, but such machine
67 specific operations are not handy, and we don't want to put that much
68 effort into it. */
70 #define PTR_KEY(p) ((size_t)p << (HOST_BITS_PER_PTR - 8) \
71 | ((size_t)p & 0xff00) << (HOST_BITS_PER_PTR - 24) \
72 | (size_t)p >> 16)
74 /* GC'able memory; a node in a binary search tree. */
76 struct ggc_mem
78 /* A combination of the standard left/right nodes, indexable by `<'. */
79 struct ggc_mem *sub[2];
81 unsigned int mark : 1;
82 unsigned int context : 7;
83 unsigned int size : 24;
85 /* Make sure the data is reasonably aligned. */
86 union {
87 HOST_WIDEST_INT i;
88 #ifdef HAVE_LONG_DOUBLE
89 long double d;
90 #else
91 double d;
92 #endif
93 } u;
96 static struct globals
98 /* Root of the object tree. */
99 struct ggc_mem *root;
101 /* Data bytes currently allocated. */
102 size_t allocated;
104 /* Data objects currently allocated. */
105 size_t objects;
107 /* Data bytes allocated at time of last GC. */
108 size_t allocated_last_gc;
110 /* Current context level. */
111 int context;
112 } G;
114 /* Local function prototypes. */
116 static void tree_insert PARAMS ((struct ggc_mem *));
117 static int tree_lookup PARAMS ((struct ggc_mem *));
118 static void clear_marks PARAMS ((struct ggc_mem *));
119 static void sweep_objs PARAMS ((struct ggc_mem **));
120 static void ggc_pop_context_1 PARAMS ((struct ggc_mem *, int));
122 /* For use from debugger. */
123 extern void debug_ggc_tree PARAMS ((struct ggc_mem *, int));
125 #ifdef GGC_BALANCE
126 extern void debug_ggc_balance PARAMS ((void));
127 #endif
128 static void tally_leaves PARAMS ((struct ggc_mem *, int, size_t *, size_t *));
130 /* Insert V into the search tree. */
132 static inline void
133 tree_insert (v)
134 struct ggc_mem *v;
136 size_t v_key = PTR_KEY (v);
137 struct ggc_mem *p, **pp;
139 for (pp = &G.root, p = *pp; p ; p = *pp)
141 size_t p_key = PTR_KEY (p);
142 pp = &p->sub[v_key < p_key];
144 *pp = v;
147 /* Return true if V is in the tree. */
149 static inline int
150 tree_lookup (v)
151 struct ggc_mem *v;
153 size_t v_key = PTR_KEY (v);
154 struct ggc_mem *p = G.root;
156 while (p)
158 size_t p_key = PTR_KEY (p);
159 if (p == v)
160 return 1;
161 p = p->sub[v_key < p_key];
164 return 0;
167 /* Alloc SIZE bytes of GC'able memory. If ZERO, clear the memory. */
169 void *
170 ggc_alloc (size)
171 size_t size;
173 struct ggc_mem *x;
175 x = (struct ggc_mem *) xmalloc (offsetof (struct ggc_mem, u) + size);
176 x->sub[0] = NULL;
177 x->sub[1] = NULL;
178 x->mark = 0;
179 x->context = G.context;
180 x->size = size;
182 #ifdef GGC_POISON
183 memset (&x->u, 0xaf, size);
184 #endif
186 tree_insert (x);
187 G.allocated += size;
188 G.objects += 1;
190 return &x->u;
193 /* Mark a node. */
196 ggc_set_mark (p)
197 const void *p;
199 struct ggc_mem *x;
201 x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
202 #ifdef GGC_ALWAYS_VERIFY
203 if (! tree_lookup (x))
204 abort ();
205 #endif
207 if (x->mark)
208 return 1;
210 x->mark = 1;
211 G.allocated += x->size;
212 G.objects += 1;
214 return 0;
217 /* Return 1 if P has been marked, zero otherwise. */
220 ggc_marked_p (p)
221 const void *p;
223 struct ggc_mem *x;
225 x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
226 #ifdef GGC_ALWAYS_VERIFY
227 if (! tree_lookup (x))
228 abort ();
229 #endif
231 return x->mark;
234 /* Return the size of the gc-able object P. */
236 size_t
237 ggc_get_size (p)
238 const void *p;
240 struct ggc_mem *x
241 = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
242 return x->size;
245 /* Unmark all objects. */
247 static void
248 clear_marks (x)
249 struct ggc_mem *x;
251 x->mark = 0;
252 if (x->sub[0])
253 clear_marks (x->sub[0]);
254 if (x->sub[1])
255 clear_marks (x->sub[1]);
258 /* Free all objects in the current context that are not marked. */
260 static void
261 sweep_objs (root)
262 struct ggc_mem **root;
264 struct ggc_mem *x = *root;
265 if (!x)
266 return;
268 sweep_objs (&x->sub[0]);
269 sweep_objs (&x->sub[1]);
271 if (! x->mark && x->context >= G.context)
273 struct ggc_mem *l, *r;
275 l = x->sub[0];
276 r = x->sub[1];
277 if (!l)
278 *root = r;
279 else if (!r)
280 *root = l;
281 else if (!l->sub[1])
283 *root = l;
284 l->sub[1] = r;
286 else if (!r->sub[0])
288 *root = r;
289 r->sub[0] = l;
291 else
293 *root = l;
294 do {
295 root = &l->sub[1];
296 } while ((l = *root) != NULL);
297 *root = r;
300 #ifdef GGC_POISON
301 memset (&x->u, 0xA5, x->size);
302 #endif
304 free (x);
308 /* The top level mark-and-sweep routine. */
310 void
311 ggc_collect ()
313 /* Avoid frequent unnecessary work by skipping collection if the
314 total allocations haven't expanded much since the last
315 collection. */
316 size_t allocated_last_gc =
317 MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
319 size_t min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
321 if (G.allocated < allocated_last_gc + min_expand)
322 return;
324 #ifdef GGC_BALANCE
325 debug_ggc_balance ();
326 #endif
328 timevar_push (TV_GC);
329 if (!quiet_flag)
330 fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
332 G.allocated = 0;
333 G.objects = 0;
335 clear_marks (G.root);
336 ggc_mark_roots ();
337 sweep_objs (&G.root);
339 G.allocated_last_gc = G.allocated;
341 timevar_pop (TV_GC);
343 if (!quiet_flag)
344 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
346 #ifdef GGC_BALANCE
347 debug_ggc_balance ();
348 #endif
351 /* Called once to initialize the garbage collector. */
353 void
354 init_ggc ()
358 /* Start a new GGC context. Memory allocated in previous contexts
359 will not be collected while the new context is active. */
361 void
362 ggc_push_context ()
364 G.context++;
366 /* We only allocated 7 bits in the node for the context. This
367 should be more than enough. */
368 if (G.context >= 128)
369 abort ();
372 /* Finish a GC context. Any uncollected memory in the new context
373 will be merged with the old context. */
375 void
376 ggc_pop_context ()
378 G.context--;
379 if (G.root)
380 ggc_pop_context_1 (G.root, G.context);
383 static void
384 ggc_pop_context_1 (x, c)
385 struct ggc_mem *x;
386 int c;
388 if (x->context > c)
389 x->context = c;
390 if (x->sub[0])
391 ggc_pop_context_1 (x->sub[0], c);
392 if (x->sub[1])
393 ggc_pop_context_1 (x->sub[1], c);
396 /* Dump a tree. */
398 void
399 debug_ggc_tree (p, indent)
400 struct ggc_mem *p;
401 int indent;
403 int i;
405 if (!p)
407 fputs ("(nil)\n", stderr);
408 return;
411 if (p->sub[0])
412 debug_ggc_tree (p->sub[0], indent + 1);
414 for (i = 0; i < indent; ++i)
415 putc (' ', stderr);
416 fprintf (stderr, "%lx %p\n", (unsigned long)PTR_KEY (p), (PTR) p);
418 if (p->sub[1])
419 debug_ggc_tree (p->sub[1], indent + 1);
422 #ifdef GGC_BALANCE
423 /* Collect tree balance metrics */
425 #include <math.h>
427 void
428 debug_ggc_balance ()
430 size_t nleaf, sumdepth;
432 nleaf = sumdepth = 0;
433 tally_leaves (G.root, 0, &nleaf, &sumdepth);
435 fprintf (stderr, " {B %.2f,%.1f,%.1f}",
436 /* In a balanced tree, leaf/node should approach 1/2. */
437 (float)nleaf / (float)G.objects,
438 /* In a balanced tree, average leaf depth should approach lg(n). */
439 (float)sumdepth / (float)nleaf,
440 log ((double) G.objects) / M_LN2);
442 #endif
444 /* Used by debug_ggc_balance, and also by ggc_print_statistics. */
445 static void
446 tally_leaves (x, depth, nleaf, sumdepth)
447 struct ggc_mem *x;
448 int depth;
449 size_t *nleaf;
450 size_t *sumdepth;
452 if (! x->sub[0] && !x->sub[1])
454 *nleaf += 1;
455 *sumdepth += depth;
457 else
459 if (x->sub[0])
460 tally_leaves (x->sub[0], depth + 1, nleaf, sumdepth);
461 if (x->sub[1])
462 tally_leaves (x->sub[1], depth + 1, nleaf, sumdepth);
466 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
467 ? (x) \
468 : ((x) < 1024*1024*10 \
469 ? (x) / 1024 \
470 : (x) / (1024*1024))))
471 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
473 /* Report on GC memory usage. */
474 void
475 ggc_print_statistics ()
477 struct ggc_statistics stats;
478 size_t nleaf = 0, sumdepth = 0;
480 /* Clear the statistics. */
481 memset (&stats, 0, sizeof (stats));
483 /* Make sure collection will really occur. */
484 G.allocated_last_gc = 0;
486 /* Collect and print the statistics common across collectors. */
487 ggc_print_common_statistics (stderr, &stats);
489 /* Report on tree balancing. */
490 tally_leaves (G.root, 0, &nleaf, &sumdepth);
492 fprintf (stderr, "\n\
493 Total internal data (bytes)\t%ld%c\n\
494 Number of leaves in tree\t%lu\n\
495 Average leaf depth\t\t%.1f\n",
496 SCALE(G.objects * offsetof (struct ggc_mem, u)),
497 LABEL(G.objects * offsetof (struct ggc_mem, u)),
498 (unsigned long)nleaf, (double)sumdepth / (double)nleaf);
500 /* Report overall memory usage. */
501 fprintf (stderr, "\n\
502 Total objects allocated\t\t%ld\n\
503 Total memory in GC arena\t%ld%c\n",
504 (unsigned long)G.objects,
505 SCALE(G.allocated), LABEL(G.allocated));
508 struct ggc_pch_data *
509 init_ggc_pch ()
511 sorry ("Generating PCH files is not supported when using ggc-simple.c");
512 /* It could be supported, but the code is not yet written. */
513 return NULL;
516 void
517 ggc_pch_count_object (d, x, size)
518 struct ggc_pch_data *d ATTRIBUTE_UNUSED;
519 void *x ATTRIBUTE_UNUSED;
520 size_t size ATTRIBUTE_UNUSED;
524 size_t
525 ggc_pch_total_size (d)
526 struct ggc_pch_data *d ATTRIBUTE_UNUSED;
528 return 0;
531 void
532 ggc_pch_this_base (d, base)
533 struct ggc_pch_data *d ATTRIBUTE_UNUSED;
534 void *base ATTRIBUTE_UNUSED;
539 char *
540 ggc_pch_alloc_object (d, x, size)
541 struct ggc_pch_data *d ATTRIBUTE_UNUSED;
542 void *x ATTRIBUTE_UNUSED;
543 size_t size ATTRIBUTE_UNUSED;
545 return NULL;
548 void
549 ggc_pch_prepare_write (d, f)
550 struct ggc_pch_data * d ATTRIBUTE_UNUSED;
551 FILE * f ATTRIBUTE_UNUSED;
555 void
556 ggc_pch_write_object (d, f, x, newx, size)
557 struct ggc_pch_data * d ATTRIBUTE_UNUSED;
558 FILE *f ATTRIBUTE_UNUSED;
559 void *x ATTRIBUTE_UNUSED;
560 void *newx ATTRIBUTE_UNUSED;
561 size_t size ATTRIBUTE_UNUSED;
565 void
566 ggc_pch_finish (d, f)
567 struct ggc_pch_data * d ATTRIBUTE_UNUSED;
568 FILE *f ATTRIBUTE_UNUSED;
572 void
573 ggc_pch_read (f, addr)
574 FILE *f ATTRIBUTE_UNUSED;
575 void *addr ATTRIBUTE_UNUSED;
577 /* This should be impossible, since we won't generate any valid PCH
578 files for this configuration. */
579 abort ();