1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1998, 1999, 2000, 2001 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)
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
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 #ifndef HOST_BITS_PER_PTR
56 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
59 /* We'd like a balanced tree, but we don't really want to pay for the
60 cost of keeping the tree balanced. We'll settle for the next best
61 thing -- nearly balanced.
63 In this context, the most natural key is the node pointer itself,
64 but due to the way memory managers work, we'd be virtually certain
65 to wind up with a completely degenerate straight line. What's needed
66 is to make something more variable, and yet predictable, be more
67 significant in the comparison.
69 The handiest source of variability is the low bits of the pointer
70 value itself. Any sort of bit/byte swap would do, but such machine
71 specific operations are not handy, and we don't want to put that much
74 #define PTR_KEY(p) ((size_t)p << (HOST_BITS_PER_PTR - 8) \
75 | ((size_t)p & 0xff00) << (HOST_BITS_PER_PTR - 24) \
78 /* GC'able memory; a node in a binary search tree. */
82 /* A combination of the standard left/right nodes, indexable by `<'. */
83 struct ggc_mem
*sub
[2];
85 unsigned int mark
: 1;
86 unsigned int context
: 7;
87 unsigned int size
: 24;
89 /* Make sure the data is reasonably aligned. */
92 #ifdef HAVE_LONG_DOUBLE
100 static struct globals
102 /* Root of the object tree. */
103 struct ggc_mem
*root
;
105 /* Data bytes currently allocated. */
108 /* Data objects currently allocated. */
111 /* Data bytes allocated at time of last GC. */
112 size_t allocated_last_gc
;
114 /* Current context level. */
118 /* Skip garbage collection if the current allocation is not at least
119 this factor times the allocation at the end of the last collection.
120 In other words, total allocation must expand by (this factor minus
121 one) before collection is performed. */
122 #define GGC_MIN_EXPAND_FOR_GC (1.3)
124 /* Bound `allocated_last_gc' to 4MB, to prevent the memory expansion
125 test from triggering too often when the heap is small. */
126 #define GGC_MIN_LAST_ALLOCATED (4 * 1024 * 1024)
128 /* Local function prototypes. */
130 static void tree_insert
PARAMS ((struct ggc_mem
*));
131 static int tree_lookup
PARAMS ((struct ggc_mem
*));
132 static void clear_marks
PARAMS ((struct ggc_mem
*));
133 static void sweep_objs
PARAMS ((struct ggc_mem
**));
134 static void ggc_pop_context_1
PARAMS ((struct ggc_mem
*, int));
136 /* For use from debugger. */
137 extern void debug_ggc_tree
PARAMS ((struct ggc_mem
*, int));
140 extern void debug_ggc_balance
PARAMS ((void));
142 static void tally_leaves
PARAMS ((struct ggc_mem
*, int, size_t *, size_t *));
144 /* Insert V into the search tree. */
150 size_t v_key
= PTR_KEY (v
);
151 struct ggc_mem
*p
, **pp
;
153 for (pp
= &G
.root
, p
= *pp
; p
; p
= *pp
)
155 size_t p_key
= PTR_KEY (p
);
156 pp
= &p
->sub
[v_key
< p_key
];
161 /* Return true if V is in the tree. */
167 size_t v_key
= PTR_KEY (v
);
168 struct ggc_mem
*p
= G
.root
;
172 size_t p_key
= PTR_KEY (p
);
175 p
= p
->sub
[v_key
< p_key
];
181 /* Alloc SIZE bytes of GC'able memory. If ZERO, clear the memory. */
189 x
= (struct ggc_mem
*) xmalloc (offsetof (struct ggc_mem
, u
) + size
);
193 x
->context
= G
.context
;
197 memset (&x
->u
, 0xaf, size
);
215 x
= (struct ggc_mem
*) ((const char *)p
- offsetof (struct ggc_mem
, u
));
216 #ifdef GGC_ALWAYS_VERIFY
217 if (! tree_lookup (x
))
225 G
.allocated
+= x
->size
;
231 /* Return 1 if P has been marked, zero otherwise. */
239 x
= (struct ggc_mem
*) ((const char *)p
- offsetof (struct ggc_mem
, u
));
240 #ifdef GGC_ALWAYS_VERIFY
241 if (! tree_lookup (x
))
248 /* Return the size of the gc-able object P. */
255 = (struct ggc_mem
*) ((const char *)p
- offsetof (struct ggc_mem
, u
));
259 /* Unmark all objects. */
267 clear_marks (x
->sub
[0]);
269 clear_marks (x
->sub
[1]);
272 /* Free all objects in the current context that are not marked. */
276 struct ggc_mem
**root
;
278 struct ggc_mem
*x
= *root
;
282 sweep_objs (&x
->sub
[0]);
283 sweep_objs (&x
->sub
[1]);
285 if (! x
->mark
&& x
->context
>= G
.context
)
287 struct ggc_mem
*l
, *r
;
310 } while ((l
= *root
) != NULL
);
315 memset (&x
->u
, 0xA5, x
->size
);
322 /* The top level mark-and-sweep routine. */
327 #ifndef GGC_ALWAYS_COLLECT
328 if (G
.allocated
< GGC_MIN_EXPAND_FOR_GC
* G
.allocated_last_gc
)
333 debug_ggc_balance ();
336 timevar_push (TV_GC
);
338 fprintf (stderr
, " {GC %luk -> ", (unsigned long)G
.allocated
/ 1024);
343 clear_marks (G
.root
);
345 sweep_objs (&G
.root
);
347 G
.allocated_last_gc
= G
.allocated
;
348 if (G
.allocated_last_gc
< GGC_MIN_LAST_ALLOCATED
)
349 G
.allocated_last_gc
= GGC_MIN_LAST_ALLOCATED
;
354 fprintf (stderr
, "%luk}", (unsigned long) G
.allocated
/ 1024);
357 debug_ggc_balance ();
361 /* Called once to initialize the garbage collector. */
366 G
.allocated_last_gc
= GGC_MIN_LAST_ALLOCATED
;
369 /* Start a new GGC context. Memory allocated in previous contexts
370 will not be collected while the new context is active. */
377 /* We only allocated 7 bits in the node for the context. This
378 should be more than enough. */
379 if (G
.context
>= 128)
383 /* Finish a GC context. Any uncollected memory in the new context
384 will be merged with the old context. */
391 ggc_pop_context_1 (G
.root
, G
.context
);
395 ggc_pop_context_1 (x
, c
)
402 ggc_pop_context_1 (x
->sub
[0], c
);
404 ggc_pop_context_1 (x
->sub
[1], c
);
410 debug_ggc_tree (p
, indent
)
418 fputs ("(nil)\n", stderr
);
423 debug_ggc_tree (p
->sub
[0], indent
+ 1);
425 for (i
= 0; i
< indent
; ++i
)
427 fprintf (stderr
, "%lx %p\n", (unsigned long)PTR_KEY (p
), p
);
430 debug_ggc_tree (p
->sub
[1], indent
+ 1);
434 /* Collect tree balance metrics */
441 size_t nleaf
, sumdepth
;
443 nleaf
= sumdepth
= 0;
444 tally_leaves (G
.root
, 0, &nleaf
, &sumdepth
);
446 fprintf (stderr
, " {B %.2f,%.1f,%.1f}",
447 /* In a balanced tree, leaf/node should approach 1/2. */
448 (float)nleaf
/ (float)G
.objects
,
449 /* In a balanced tree, average leaf depth should approach lg(n). */
450 (float)sumdepth
/ (float)nleaf
,
451 log ((double) G
.objects
) / M_LN2
);
455 /* Used by debug_ggc_balance, and also by ggc_print_statistics. */
457 tally_leaves (x
, depth
, nleaf
, sumdepth
)
463 if (! x
->sub
[0] && !x
->sub
[1])
471 tally_leaves (x
->sub
[0], depth
+ 1, nleaf
, sumdepth
);
473 tally_leaves (x
->sub
[1], depth
+ 1, nleaf
, sumdepth
);
477 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
479 : ((x) < 1024*1024*10 \
481 : (x) / (1024*1024))))
482 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
484 /* Report on GC memory usage. */
486 ggc_print_statistics ()
488 struct ggc_statistics stats
;
489 size_t nleaf
= 0, sumdepth
= 0;
491 /* Clear the statistics. */
492 memset (&stats
, 0, sizeof (stats
));
494 /* Make sure collection will really occur. */
495 G
.allocated_last_gc
= 0;
497 /* Collect and print the statistics common across collectors. */
498 ggc_print_common_statistics (stderr
, &stats
);
500 /* Report on tree balancing. */
501 tally_leaves (G
.root
, 0, &nleaf
, &sumdepth
);
503 fprintf (stderr
, "\n\
504 Total internal data (bytes)\t%ld%c\n\
505 Number of leaves in tree\t%d\n\
506 Average leaf depth\t\t%.1f\n",
507 SCALE(G
.objects
* offsetof (struct ggc_mem
, u
)),
508 LABEL(G
.objects
* offsetof (struct ggc_mem
, u
)),
509 nleaf
, (double)sumdepth
/ (double)nleaf
);
511 /* Report overall memory usage. */
512 fprintf (stderr
, "\n\
513 Total objects allocated\t\t%d\n\
514 Total memory in GC arena\t%ld%c\n",
516 SCALE(G
.allocated
), LABEL(G
.allocated
));