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. */
190 x = (struct ggc_mem *) xmalloc (offsetof (struct ggc_mem, u) + size);
194 x->context = G.context;
198 memset (&x->u, 0xaf, size);
216 x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
217 #ifdef GGC_ALWAYS_VERIFY
218 if (! tree_lookup (x))
226 G.allocated += x->size;
232 /* Mark a node, but check first to see that it's really gc-able memory. */
235 ggc_mark_if_gcable (p)
243 x = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
244 if (! tree_lookup (x))
251 G.allocated += x->size;
255 /* Return the size of the gc-able object P. */
262 = (struct ggc_mem *) ((const char *)p - offsetof (struct ggc_mem, u));
266 /* Unmark all objects. */
274 clear_marks (x->sub[0]);
276 clear_marks (x->sub[1]);
279 /* Free all objects in the current context that are not marked. */
283 struct ggc_mem **root;
285 struct ggc_mem *x = *root;
289 sweep_objs (&x->sub[0]);
290 sweep_objs (&x->sub[1]);
292 if (! x->mark && x->context >= G.context)
294 struct ggc_mem *l, *r;
317 } while ((l = *root) != NULL);
322 memset (&x->u, 0xA5, x->size);
329 /* The top level mark-and-sweep routine. */
334 #ifndef GGC_ALWAYS_COLLECT
335 if (G.allocated < GGC_MIN_EXPAND_FOR_GC * G.allocated_last_gc)
340 debug_ggc_balance ();
343 timevar_push (TV_GC);
345 fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
350 clear_marks (G.root);
352 sweep_objs (&G.root);
354 G.allocated_last_gc = G.allocated;
355 if (G.allocated_last_gc < GGC_MIN_LAST_ALLOCATED)
356 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
361 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
364 debug_ggc_balance ();
368 /* Called once to initialize the garbage collector. */
373 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
375 empty_string = ggc_alloc_string ("", 0);
376 ggc_add_string_root (&empty_string, 1);
379 /* Start a new GGC context. Memory allocated in previous contexts
380 will not be collected while the new context is active. */
387 /* We only allocated 7 bits in the node for the context. This
388 should be more than enough. */
389 if (G.context >= 128)
393 /* Finish a GC context. Any uncollected memory in the new context
394 will be merged with the old context. */
401 ggc_pop_context_1 (G.root, G.context);
405 ggc_pop_context_1 (x, c)
412 ggc_pop_context_1 (x->sub[0], c);
414 ggc_pop_context_1 (x->sub[1], c);
420 debug_ggc_tree (p, indent)
428 fputs ("(nil)\n", stderr);
433 debug_ggc_tree (p->sub[0], indent + 1);
435 for (i = 0; i < indent; ++i)
437 fprintf (stderr, "%lx %p\n", PTR_KEY (p), p);
440 debug_ggc_tree (p->sub[1], indent + 1);
444 /* Collect tree balance metrics */
451 size_t nleaf, sumdepth;
453 nleaf = sumdepth = 0;
454 tally_leaves (G.root, 0, &nleaf, &sumdepth);
456 fprintf (stderr, " {B %.2f,%.1f,%.1f}",
457 /* In a balanced tree, leaf/node should approach 1/2. */
458 (float)nleaf / (float)G.objects,
459 /* In a balanced tree, average leaf depth should approach lg(n). */
460 (float)sumdepth / (float)nleaf,
461 log ((double) G.objects) / M_LN2);
465 tally_leaves (x, depth, nleaf, sumdepth)
471 if (! x->sub[0] && !x->sub[1])
479 tally_leaves (x->sub[0], depth + 1, nleaf, sumdepth);
481 tally_leaves (x->sub[1], depth + 1, nleaf, sumdepth);