1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
31 /* Debugging flags. */
33 /* Zap memory before freeing to catch dangling pointers. */
36 /* Collect statistics on how bushy the search tree is. */
39 /* Perform collection every time ggc_collect is invoked. Otherwise,
40 collection is performed only when a significant amount of memory
41 has been allocated since the last collection. */
42 #undef GGC_ALWAYS_COLLECT
44 /* Always verify that the to-be-marked memory is collectable. */
45 #undef GGC_ALWAYS_VERIFY
47 #ifdef ENABLE_GC_CHECKING
49 #define GGC_ALWAYS_VERIFY
51 #ifdef ENABLE_GC_ALWAYS_COLLECT
52 #define GGC_ALWAYS_COLLECT
55 /* Constants for general use. */
59 #ifndef HOST_BITS_PER_PTR
60 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
63 /* We'd like a balanced tree, but we don't really want to pay for the
64 cost of keeping the tree balanced. We'll settle for the next best
65 thing -- nearly balanced.
67 In this context, the most natural key is the node pointer itself,
68 but due to the way memory managers work, we'd be virtually certain
69 to wind up with a completely degenerate straight line. What's needed
70 is to make something more variable, and yet predictable, be more
71 significant in the comparison.
73 The handiest source of variability is the low bits of the pointer
74 value itself. Any sort of bit/byte swap would do, but such machine
75 specific operations are not handy, and we don't want to put that much
78 #define PTR_KEY(p) ((size_t)p << (HOST_BITS_PER_PTR - 8) \
79 | ((size_t)p & 0xff00) << (HOST_BITS_PER_PTR - 24) \
82 /* GC'able memory; a node in a binary search tree. */
86 /* A combination of the standard left/right nodes, indexable by `<'. */
87 struct ggc_mem *sub[2];
89 unsigned int mark : 1;
90 unsigned int context : 7;
91 unsigned int size : 24;
93 /* Make sure the data is reasonably aligned. */
96 #ifdef HAVE_LONG_DOUBLE
104 static struct globals
106 /* Root of the object tree. */
107 struct ggc_mem *root;
109 /* Data bytes currently allocated. */
112 /* Data objects currently allocated. */
115 /* Data bytes allocated at time of last GC. */
116 size_t allocated_last_gc;
118 /* Current context level. */
122 /* Skip garbage collection if the current allocation is not at least
123 this factor times the allocation at the end of the last collection.
124 In other words, total allocation must expand by (this factor minus
125 one) before collection is performed. */
126 #define GGC_MIN_EXPAND_FOR_GC (1.3)
128 /* Bound `allocated_last_gc' to 4MB, to prevent the memory expansion
129 test from triggering too often when the heap is small. */
130 #define GGC_MIN_LAST_ALLOCATED (4 * 1024 * 1024)
132 /* Local function prototypes. */
134 static void tree_insert PARAMS ((struct ggc_mem *));
135 static int tree_lookup PARAMS ((struct ggc_mem *));
136 static void clear_marks PARAMS ((struct ggc_mem *));
137 static void sweep_objs PARAMS ((struct ggc_mem **));
138 static void ggc_pop_context_1 PARAMS ((struct ggc_mem *, int));
141 extern void debug_ggc_balance PARAMS ((void));
142 static void tally_leaves PARAMS ((struct ggc_mem *, int, size_t *, size_t *));
145 /* Insert V into the search tree. */
151 size_t v_key = PTR_KEY (v);
152 struct ggc_mem *p, **pp;
154 for (pp = &G.root, p = *pp; p ; p = *pp)
156 size_t p_key = PTR_KEY (p);
157 pp = &p->sub[v_key < p_key];
162 /* Return true if V is in the tree. */
168 size_t v_key = PTR_KEY (v);
169 struct ggc_mem *p = G.root;
173 size_t p_key = PTR_KEY (p);
176 p = p->sub[v_key < p_key];
182 /* Alloc SIZE bytes of GC'able memory. If ZERO, clear the memory. */
185 ggc_alloc_obj (size, zero)
191 x = (struct ggc_mem *) xmalloc (offsetof (struct ggc_mem, u) + size);
195 x->context = G.context;
199 memset (&x->u, 0, size);
202 memset (&x->u, 0xaf, size);
220 x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
221 #ifdef GGC_ALWAYS_VERIFY
222 if (! tree_lookup (x))
230 G.allocated += x->size;
236 /* Mark a node, but check first to see that it's really gc-able memory. */
239 ggc_mark_if_gcable (p)
247 x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
248 if (! tree_lookup (x))
255 G.allocated += x->size;
259 /* Return the size of the gc-able object P. */
266 = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
270 /* Unmark all objects. */
278 clear_marks (x->sub[0]);
280 clear_marks (x->sub[1]);
283 /* Free all objects in the current context that are not marked. */
287 struct ggc_mem **root;
289 struct ggc_mem *x = *root;
293 sweep_objs (&x->sub[0]);
294 sweep_objs (&x->sub[1]);
296 if (! x->mark && x->context >= G.context)
298 struct ggc_mem *l, *r;
321 } while ((l = *root) != NULL);
326 memset (&x->u, 0xA5, x->size);
333 /* The top level mark-and-sweep routine. */
338 #ifndef GGC_ALWAYS_COLLECT
339 if (G.allocated < GGC_MIN_EXPAND_FOR_GC * G.allocated_last_gc)
344 debug_ggc_balance ();
347 timevar_push (TV_GC);
349 fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
354 clear_marks (G.root);
356 sweep_objs (&G.root);
358 G.allocated_last_gc = G.allocated;
359 if (G.allocated_last_gc < GGC_MIN_LAST_ALLOCATED)
360 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
365 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
368 debug_ggc_balance ();
372 /* Called once to initialize the garbage collector. */
377 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
379 empty_string = ggc_alloc_string ("", 0);
380 ggc_add_string_root (&empty_string, 1);
383 /* Start a new GGC context. Memory allocated in previous contexts
384 will not be collected while the new context is active. */
391 /* We only allocated 7 bits in the node for the context. This
392 should be more than enough. */
393 if (G.context >= 128)
397 /* Finish a GC context. Any uncollected memory in the new context
398 will be merged with the old context. */
405 ggc_pop_context_1 (G.root, G.context);
409 ggc_pop_context_1 (x, c)
416 ggc_pop_context_1 (x->sub[0], c);
418 ggc_pop_context_1 (x->sub[1], c);
424 debug_ggc_tree (p, indent)
432 fputs ("(nil)\n", stderr);
437 debug_ggc_tree (p->sub[0], indent + 1);
439 for (i = 0; i < indent; ++i)
441 fprintf (stderr, "%lx %p\n", PTR_KEY (p), p);
444 debug_ggc_tree (p->sub[1], indent + 1);
448 /* Collect tree balance metrics */
455 size_t nleaf, sumdepth;
457 nleaf = sumdepth = 0;
458 tally_leaves (G.root, 0, &nleaf, &sumdepth);
460 fprintf (stderr, " {B %.2f,%.1f,%.1f}",
461 /* In a balanced tree, leaf/node should approach 1/2. */
462 (float)nleaf / (float)G.objects,
463 /* In a balanced tree, average leaf depth should approach lg(n). */
464 (float)sumdepth / (float)nleaf,
465 log ((double) G.objects) / M_LN2);
469 tally_leaves (x, depth, nleaf, sumdepth)
475 if (! x->sub[0] && !x->sub[1])
483 tally_leaves (x->sub[0], depth + 1, nleaf, sumdepth);
485 tally_leaves (x->sub[1], depth + 1, nleaf, sumdepth);