1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1998, 1999 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. */
32 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
35 /* Debugging flags. */
37 /* Zap memory before freeing to catch dangling pointers. */
40 /* Collect statistics on how bushy the search tree is. */
43 /* Perform collection every time ggc_collect is invoked. Otherwise,
44 collection is performed only when a significant amount of memory
45 has been allocated since the last collection. */
46 #undef GGC_ALWAYS_COLLECT
48 /* Always verify that the to-be-marked memory is collectable. */
49 #undef GGC_ALWAYS_VERIFY
51 #ifdef ENABLE_CHECKING
53 #define GGC_ALWAYS_COLLECT
54 #define GGC_ALWAYS_VERIFY
57 /* Constants for general use. */
62 #ifndef HOST_BITS_PER_PTR
63 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
66 /* We'd like a balanced tree, but we don't really want to pay for the
67 cost of keeping the tree balanced. We'll settle for the next best
68 thing -- nearly balanced.
70 In this context, the most natural key is the node pointer itself,
71 but due to the way memory managers work, we'd be virtually certain
72 to wind up with a completely degenerate straight line. What's needed
73 is to make something more variable, and yet predictable, be more
74 significant in the comparison.
76 The handiest source of variability is the low bits of the pointer
77 value itself. Any sort of bit/byte swap would do, but such machine
78 specific operations are not handy, and we don't want to put that much
81 #define PTR_KEY(p) ((size_t)p << (HOST_BITS_PER_PTR - 8) \
82 | ((size_t)p & 0xff00) << (HOST_BITS_PER_PTR - 24) \
85 /* GC'able memory; a node in a binary search tree. */
89 /* A combination of the standard left/right nodes, indexable by `<'. */
90 struct ggc_mem
*sub
[2];
92 unsigned int mark
: 1;
93 unsigned int context
: 7;
94 unsigned int size
: 24;
96 /* Make sure the data is reasonably aligned. */
99 #ifdef HAVE_LONG_DOUBLE
107 static struct globals
109 /* Root of the object tree. */
110 struct ggc_mem
*root
;
112 /* Data bytes currently allocated. */
115 /* Data objects currently allocated. */
118 /* Data bytes allocated at time of last GC. */
119 size_t allocated_last_gc
;
121 /* Current context level. */
125 /* Skip garbage collection if the current allocation is not at least
126 this factor times the allocation at the end of the last collection.
127 In other words, total allocation must expand by (this factor minus
128 one) before collection is performed. */
129 #define GGC_MIN_EXPAND_FOR_GC (1.3)
131 /* Bound `allocated_last_gc' to 4MB, to prevent the memory expansion
132 test from triggering too often when the heap is small. */
133 #define GGC_MIN_LAST_ALLOCATED (4 * 1024 * 1024)
135 /* Local function prototypes. */
137 static void tree_insert
PROTO ((struct ggc_mem
*));
138 static int tree_lookup
PROTO ((struct ggc_mem
*));
139 static void clear_marks
PROTO ((struct ggc_mem
*));
140 static void sweep_objs
PROTO ((struct ggc_mem
**));
141 static void ggc_pop_context_1
PROTO ((struct ggc_mem
*, int));
144 extern void debug_ggc_balance
PROTO ((void));
145 static void tally_leaves
PROTO ((struct ggc_mem
*, int, size_t *, size_t *));
148 /* Insert V into the search tree. */
154 size_t v_key
= PTR_KEY (v
);
155 struct ggc_mem
*p
, **pp
;
157 for (pp
= &G
.root
, p
= *pp
; p
; p
= *pp
)
159 size_t p_key
= PTR_KEY (p
);
160 pp
= &p
->sub
[v_key
< p_key
];
165 /* Return true if V is in the tree. */
171 size_t v_key
= PTR_KEY (v
);
172 struct ggc_mem
*p
= G
.root
;
176 size_t p_key
= PTR_KEY (p
);
179 p
= p
->sub
[v_key
< p_key
];
185 /* Alloc SIZE bytes of GC'able memory. If ZERO, clear the memory. */
188 ggc_alloc_obj (size
, zero
)
194 x
= (struct ggc_mem
*) xmalloc (offsetof (struct ggc_mem
, u
) + size
);
198 x
->context
= G
.context
;
202 memset (&x
->u
, 0, size
);
205 memset (&x
->u
, 0xaf, size
);
223 x
= (struct ggc_mem
*) ((char *)p
- offsetof (struct ggc_mem
, u
));
224 #ifdef GGC_ALWAYS_VERIFY
225 if (! tree_lookup (x
))
233 G
.allocated
+= x
->size
;
239 /* Mark a node, but check first to see that it's really gc-able memory. */
242 ggc_mark_if_gcable (p
)
250 x
= (struct ggc_mem
*) ((char *)p
- offsetof (struct ggc_mem
, u
));
251 if (! tree_lookup (x
))
258 G
.allocated
+= x
->size
;
262 /* Return the size of the gc-able object P. */
269 = (struct ggc_mem
*) ((char *)p
- offsetof (struct ggc_mem
, u
));
273 /* Unmark all objects. */
281 clear_marks (x
->sub
[0]);
283 clear_marks (x
->sub
[1]);
286 /* Free all objects in the current context that are not marked. */
290 struct ggc_mem
**root
;
292 struct ggc_mem
*x
= *root
;
296 sweep_objs (&x
->sub
[0]);
297 sweep_objs (&x
->sub
[1]);
299 if (! x
->mark
&& x
->context
>= G
.context
)
301 struct ggc_mem
*l
, *r
;
324 } while ((l
= *root
) != NULL
);
329 memset (&x
->u
, 0xA5, x
->size
);
336 /* The top level mark-and-sweep routine. */
343 #ifndef GGC_ALWAYS_COLLECT
344 if (G
.allocated
< GGC_MIN_EXPAND_FOR_GC
* G
.allocated_last_gc
)
349 debug_ggc_balance ();
352 time
= get_run_time ();
354 fprintf (stderr
, " {GC %luk -> ", (unsigned long)G
.allocated
/ 1024);
359 clear_marks (G
.root
);
361 sweep_objs (&G
.root
);
363 G
.allocated_last_gc
= G
.allocated
;
364 if (G
.allocated_last_gc
< GGC_MIN_LAST_ALLOCATED
)
365 G
.allocated_last_gc
= GGC_MIN_LAST_ALLOCATED
;
367 time
= get_run_time () - time
;
372 fprintf (stderr
, "%luk in %.3f}",
373 (unsigned long) G
.allocated
/ 1024, time
* 1e-6);
377 debug_ggc_balance ();
381 /* Called once to initialize the garbage collector. */
386 G
.allocated_last_gc
= GGC_MIN_LAST_ALLOCATED
;
388 empty_string
= ggc_alloc_string ("", 0);
389 ggc_add_string_root (&empty_string
, 1);
392 /* Start a new GGC context. Memory allocated in previous contexts
393 will not be collected while the new context is active. */
400 /* We only allocated 7 bits in the node for the context. This
401 should be more than enough. */
402 if (G
.context
>= 128)
406 /* Finish a GC context. Any uncollected memory in the new context
407 will be merged with the old context. */
414 ggc_pop_context_1 (G
.root
, G
.context
);
418 ggc_pop_context_1 (x
, c
)
425 ggc_pop_context_1 (x
->sub
[0], c
);
427 ggc_pop_context_1 (x
->sub
[1], c
);
433 debug_ggc_tree (p
, indent
)
441 fputs ("(nil)\n", stderr
);
446 debug_ggc_tree (p
->sub
[0], indent
+ 1);
448 for (i
= 0; i
< indent
; ++i
)
450 fprintf (stderr
, "%lx %p\n", PTR_KEY (p
), p
);
453 debug_ggc_tree (p
->sub
[1], indent
+ 1);
457 /* Collect tree balance metrics */
464 size_t nleaf
, sumdepth
;
466 nleaf
= sumdepth
= 0;
467 tally_leaves (G
.root
, 0, &nleaf
, &sumdepth
);
469 fprintf (stderr
, " {B %.2f,%.1f,%.1f}",
470 /* In a balanced tree, leaf/node should approach 1/2. */
471 (float)nleaf
/ (float)G
.objects
,
472 /* In a balanced tree, average leaf depth should approach lg(n). */
473 (float)sumdepth
/ (float)nleaf
,
474 log ((double) G
.objects
) / M_LN2
);
478 tally_leaves (x
, depth
, nleaf
, sumdepth
)
484 if (! x
->sub
[0] && !x
->sub
[1])
492 tally_leaves (x
->sub
[0], depth
+ 1, nleaf
, sumdepth
);
494 tally_leaves (x
->sub
[1], depth
+ 1, nleaf
, sumdepth
);