1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
36 /* Debugging flags. */
38 /* Zap memory before freeing to catch dangling pointers. */
41 /* Collect statistics on how bushy the search tree is. */
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
52 #ifndef HOST_BITS_PER_PTR
53 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
56 /* We'd like a balanced tree, but we don't really want to pay for the
57 cost of keeping the tree balanced. We'll settle for the next best
58 thing -- nearly balanced.
60 In this context, the most natural key is the node pointer itself,
61 but due to the way memory managers work, we'd be virtually certain
62 to wind up with a completely degenerate straight line. What's needed
63 is to make something more variable, and yet predictable, be more
64 significant in the comparison.
66 The handiest source of variability is the low bits of the pointer
67 value itself. Any sort of bit/byte swap would do, but such machine
68 specific operations are not handy, and we don't want to put that much
71 #define PTR_KEY(p) ((size_t)p << (HOST_BITS_PER_PTR - 8) \
72 | ((size_t)p & 0xff00) << (HOST_BITS_PER_PTR - 24) \
75 /* GC'able memory; a node in a binary search tree. */
79 /* A combination of the standard left/right nodes, indexable by `<'. */
80 struct ggc_mem
*sub
[2];
82 unsigned int mark
: 1;
83 unsigned int context
: 7;
84 unsigned int size
: 24;
86 /* Make sure the data is reasonably aligned. */
95 /* Root of the object tree. */
98 /* Data bytes currently allocated. */
101 /* Data objects currently allocated. */
104 /* Data bytes allocated at time of last GC. */
105 size_t allocated_last_gc
;
107 /* Current context level. */
111 /* Local function prototypes. */
113 static void tree_insert (struct ggc_mem
*);
114 static int tree_lookup (struct ggc_mem
*);
115 static void clear_marks (struct ggc_mem
*);
116 static void sweep_objs (struct ggc_mem
**);
117 static void ggc_pop_context_1 (struct ggc_mem
*, int);
119 /* For use from debugger. */
120 extern void debug_ggc_tree (struct ggc_mem
*, int);
123 extern void debug_ggc_balance (void);
125 static void tally_leaves (struct ggc_mem
*, int, size_t *, size_t *);
127 /* Insert V into the search tree. */
130 tree_insert (struct ggc_mem
*v
)
132 size_t v_key
= PTR_KEY (v
);
133 struct ggc_mem
*p
, **pp
;
135 for (pp
= &G
.root
, p
= *pp
; p
; p
= *pp
)
137 size_t p_key
= PTR_KEY (p
);
138 pp
= &p
->sub
[v_key
< p_key
];
143 /* Return true if V is in the tree. */
146 tree_lookup (struct ggc_mem
*v
)
148 size_t v_key
= PTR_KEY (v
);
149 struct ggc_mem
*p
= G
.root
;
153 size_t p_key
= PTR_KEY (p
);
156 p
= p
->sub
[v_key
< p_key
];
162 /* Alloc SIZE bytes of GC'able memory. If ZERO, clear the memory. */
165 ggc_alloc (size_t size
)
169 x
= xmalloc (offsetof (struct ggc_mem
, u
) + size
);
173 x
->context
= G
.context
;
177 memset (&x
->u
, 0xaf, size
);
190 ggc_set_mark (const void *p
)
194 x
= (struct ggc_mem
*) ((const char *)p
- offsetof (struct ggc_mem
, u
));
195 #ifdef GGC_ALWAYS_VERIFY
196 if (! tree_lookup (x
))
204 G
.allocated
+= x
->size
;
210 /* Return 1 if P has been marked, zero otherwise. */
213 ggc_marked_p (const void *p
)
217 x
= (struct ggc_mem
*) ((const char *)p
- offsetof (struct ggc_mem
, u
));
218 #ifdef GGC_ALWAYS_VERIFY
219 if (! tree_lookup (x
))
226 /* Return the size of the gc-able object P. */
229 ggc_get_size (const void *p
)
232 = (struct ggc_mem
*) ((const char *)p
- offsetof (struct ggc_mem
, u
));
236 /* Unmark all objects. */
239 clear_marks (struct ggc_mem
*x
)
243 clear_marks (x
->sub
[0]);
245 clear_marks (x
->sub
[1]);
248 /* Free all objects in the current context that are not marked. */
251 sweep_objs (struct ggc_mem
**root
)
253 struct ggc_mem
*x
= *root
;
257 sweep_objs (&x
->sub
[0]);
258 sweep_objs (&x
->sub
[1]);
260 if (! x
->mark
&& x
->context
>= G
.context
)
262 struct ggc_mem
*l
, *r
;
285 } while ((l
= *root
) != NULL
);
290 memset (&x
->u
, 0xA5, x
->size
);
297 /* The top level mark-and-sweep routine. */
302 /* Avoid frequent unnecessary work by skipping collection if the
303 total allocations haven't expanded much since the last
305 size_t allocated_last_gc
=
306 MAX (G
.allocated_last_gc
, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE
) * 1024);
308 size_t min_expand
= allocated_last_gc
* PARAM_VALUE (GGC_MIN_EXPAND
) / 100;
310 if (G
.allocated
< allocated_last_gc
+ min_expand
)
314 debug_ggc_balance ();
317 timevar_push (TV_GC
);
319 fprintf (stderr
, " {GC %luk -> ", (unsigned long)G
.allocated
/ 1024);
324 clear_marks (G
.root
);
326 sweep_objs (&G
.root
);
328 G
.allocated_last_gc
= G
.allocated
;
333 fprintf (stderr
, "%luk}", (unsigned long) G
.allocated
/ 1024);
336 debug_ggc_balance ();
340 /* Called once to initialize the garbage collector. */
347 /* Start a new GGC context. Memory allocated in previous contexts
348 will not be collected while the new context is active. */
351 ggc_push_context (void)
355 /* We only allocated 7 bits in the node for the context. This
356 should be more than enough. */
357 if (G
.context
>= 128)
361 /* Finish a GC context. Any uncollected memory in the new context
362 will be merged with the old context. */
365 ggc_pop_context (void)
369 ggc_pop_context_1 (G
.root
, G
.context
);
373 ggc_pop_context_1 (struct ggc_mem
*x
, int c
)
378 ggc_pop_context_1 (x
->sub
[0], c
);
380 ggc_pop_context_1 (x
->sub
[1], c
);
386 debug_ggc_tree (struct ggc_mem
*p
, int indent
)
392 fputs ("(nil)\n", stderr
);
397 debug_ggc_tree (p
->sub
[0], indent
+ 1);
399 for (i
= 0; i
< indent
; ++i
)
401 fprintf (stderr
, "%lx %p\n", (unsigned long)PTR_KEY (p
), (void *) p
);
404 debug_ggc_tree (p
->sub
[1], indent
+ 1);
408 /* Collect tree balance metrics */
413 debug_ggc_balance (void)
415 size_t nleaf
, sumdepth
;
417 nleaf
= sumdepth
= 0;
418 tally_leaves (G
.root
, 0, &nleaf
, &sumdepth
);
420 fprintf (stderr
, " {B %.2f,%.1f,%.1f}",
421 /* In a balanced tree, leaf/node should approach 1/2. */
422 (float)nleaf
/ (float)G
.objects
,
423 /* In a balanced tree, average leaf depth should approach lg(n). */
424 (float)sumdepth
/ (float)nleaf
,
425 log ((double) G
.objects
) / M_LN2
);
429 /* Used by debug_ggc_balance, and also by ggc_print_statistics. */
431 tally_leaves (struct ggc_mem
*x
, int depth
, size_t *nleaf
, size_t *sumdepth
)
433 if (! x
->sub
[0] && !x
->sub
[1])
441 tally_leaves (x
->sub
[0], depth
+ 1, nleaf
, sumdepth
);
443 tally_leaves (x
->sub
[1], depth
+ 1, nleaf
, sumdepth
);
447 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
449 : ((x) < 1024*1024*10 \
451 : (x) / (1024*1024))))
452 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
454 /* Report on GC memory usage. */
456 ggc_print_statistics (void)
458 struct ggc_statistics stats
;
459 size_t nleaf
= 0, sumdepth
= 0;
461 /* Clear the statistics. */
462 memset (&stats
, 0, sizeof (stats
));
464 /* Make sure collection will really occur. */
465 G
.allocated_last_gc
= 0;
467 /* Collect and print the statistics common across collectors. */
468 ggc_print_common_statistics (stderr
, &stats
);
470 /* Report on tree balancing. */
471 tally_leaves (G
.root
, 0, &nleaf
, &sumdepth
);
473 fprintf (stderr
, "\n\
474 Total internal data (bytes)\t%ld%c\n\
475 Number of leaves in tree\t%lu\n\
476 Average leaf depth\t\t%.1f\n",
477 SCALE(G
.objects
* offsetof (struct ggc_mem
, u
)),
478 LABEL(G
.objects
* offsetof (struct ggc_mem
, u
)),
479 (unsigned long)nleaf
, (double)sumdepth
/ (double)nleaf
);
481 /* Report overall memory usage. */
482 fprintf (stderr
, "\n\
483 Total objects allocated\t\t%ld\n\
484 Total memory in GC arena\t%ld%c\n",
485 (unsigned long)G
.objects
,
486 SCALE(G
.allocated
), LABEL(G
.allocated
));
489 struct ggc_pch_data
*
492 sorry ("Generating PCH files is not supported when using ggc-simple.c");
493 /* It could be supported, but the code is not yet written. */
498 ggc_pch_count_object (struct ggc_pch_data
*d ATTRIBUTE_UNUSED
,
499 void *x ATTRIBUTE_UNUSED
,
500 size_t size ATTRIBUTE_UNUSED
)
505 ggc_pch_total_size (struct ggc_pch_data
*d ATTRIBUTE_UNUSED
)
511 ggc_pch_this_base (struct ggc_pch_data
*d ATTRIBUTE_UNUSED
,
512 void *base ATTRIBUTE_UNUSED
)
518 ggc_pch_alloc_object (struct ggc_pch_data
*d ATTRIBUTE_UNUSED
,
519 void *x ATTRIBUTE_UNUSED
,
520 size_t size ATTRIBUTE_UNUSED
)
526 ggc_pch_prepare_write (struct ggc_pch_data
*d ATTRIBUTE_UNUSED
,
527 FILE * f ATTRIBUTE_UNUSED
)
532 ggc_pch_write_object (struct ggc_pch_data
*d ATTRIBUTE_UNUSED
,
533 FILE *f ATTRIBUTE_UNUSED
, void *x ATTRIBUTE_UNUSED
,
534 void *newx ATTRIBUTE_UNUSED
,
535 size_t size ATTRIBUTE_UNUSED
)
540 ggc_pch_finish (struct ggc_pch_data
*d ATTRIBUTE_UNUSED
,
541 FILE *f ATTRIBUTE_UNUSED
)
546 ggc_pch_read (FILE *f ATTRIBUTE_UNUSED
, void *addr ATTRIBUTE_UNUSED
)
548 /* This should be impossible, since we won't generate any valid PCH
549 files for this configuration. */