1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 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
23 #include "coretypes.h"
35 /* Debugging flags. */
37 /* Zap memory before freeing to catch dangling pointers. */
40 /* Collect statistics on how bushy the search tree is. */
43 /* Always verify that the to-be-marked memory is collectable. */
44 #undef GGC_ALWAYS_VERIFY
46 #ifdef ENABLE_GC_CHECKING
48 #define GGC_ALWAYS_VERIFY
51 #ifndef HOST_BITS_PER_PTR
52 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
55 /* We'd like a balanced tree, but we don't really want to pay for the
56 cost of keeping the tree balanced. We'll settle for the next best
57 thing -- nearly balanced.
59 In this context, the most natural key is the node pointer itself,
60 but due to the way memory managers work, we'd be virtually certain
61 to wind up with a completely degenerate straight line. What's needed
62 is to make something more variable, and yet predictable, be more
63 significant in the comparison.
65 The handiest source of variability is the low bits of the pointer
66 value itself. Any sort of bit/byte swap would do, but such machine
67 specific operations are not handy, and we don't want to put that much
70 #define PTR_KEY(p) ((size_t)p << (HOST_BITS_PER_PTR - 8) \
71 | ((size_t)p & 0xff00) << (HOST_BITS_PER_PTR - 24) \
74 /* GC'able memory; a node in a binary search tree. */
78 /* A combination of the standard left/right nodes, indexable by `<'. */
79 struct ggc_mem
*sub
[2];
81 unsigned int mark
: 1;
82 unsigned int context
: 7;
83 unsigned int size
: 24;
85 /* Make sure the data is reasonably aligned. */
88 #ifdef HAVE_LONG_DOUBLE
98 /* Root of the object tree. */
101 /* Data bytes currently allocated. */
104 /* Data objects currently allocated. */
107 /* Data bytes allocated at time of last GC. */
108 size_t allocated_last_gc
;
110 /* Current context level. */
114 /* Local function prototypes. */
116 static void tree_insert
PARAMS ((struct ggc_mem
*));
117 static int tree_lookup
PARAMS ((struct ggc_mem
*));
118 static void clear_marks
PARAMS ((struct ggc_mem
*));
119 static void sweep_objs
PARAMS ((struct ggc_mem
**));
120 static void ggc_pop_context_1
PARAMS ((struct ggc_mem
*, int));
122 /* For use from debugger. */
123 extern void debug_ggc_tree
PARAMS ((struct ggc_mem
*, int));
126 extern void debug_ggc_balance
PARAMS ((void));
128 static void tally_leaves
PARAMS ((struct ggc_mem
*, int, size_t *, size_t *));
130 /* Insert V into the search tree. */
136 size_t v_key
= PTR_KEY (v
);
137 struct ggc_mem
*p
, **pp
;
139 for (pp
= &G
.root
, p
= *pp
; p
; p
= *pp
)
141 size_t p_key
= PTR_KEY (p
);
142 pp
= &p
->sub
[v_key
< p_key
];
147 /* Return true if V is in the tree. */
153 size_t v_key
= PTR_KEY (v
);
154 struct ggc_mem
*p
= G
.root
;
158 size_t p_key
= PTR_KEY (p
);
161 p
= p
->sub
[v_key
< p_key
];
167 /* Alloc SIZE bytes of GC'able memory. If ZERO, clear the memory. */
175 x
= (struct ggc_mem
*) xmalloc (offsetof (struct ggc_mem
, u
) + size
);
179 x
->context
= G
.context
;
183 memset (&x
->u
, 0xaf, size
);
201 x
= (struct ggc_mem
*) ((const char *)p
- offsetof (struct ggc_mem
, u
));
202 #ifdef GGC_ALWAYS_VERIFY
203 if (! tree_lookup (x
))
211 G
.allocated
+= x
->size
;
217 /* Return 1 if P has been marked, zero otherwise. */
225 x
= (struct ggc_mem
*) ((const char *)p
- offsetof (struct ggc_mem
, u
));
226 #ifdef GGC_ALWAYS_VERIFY
227 if (! tree_lookup (x
))
234 /* Return the size of the gc-able object P. */
241 = (struct ggc_mem
*) ((const char *)p
- offsetof (struct ggc_mem
, u
));
245 /* Unmark all objects. */
253 clear_marks (x
->sub
[0]);
255 clear_marks (x
->sub
[1]);
258 /* Free all objects in the current context that are not marked. */
262 struct ggc_mem
**root
;
264 struct ggc_mem
*x
= *root
;
268 sweep_objs (&x
->sub
[0]);
269 sweep_objs (&x
->sub
[1]);
271 if (! x
->mark
&& x
->context
>= G
.context
)
273 struct ggc_mem
*l
, *r
;
296 } while ((l
= *root
) != NULL
);
301 memset (&x
->u
, 0xA5, x
->size
);
308 /* The top level mark-and-sweep routine. */
313 /* Avoid frequent unnecessary work by skipping collection if the
314 total allocations haven't expanded much since the last
316 size_t allocated_last_gc
=
317 MAX (G
.allocated_last_gc
, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE
) * 1024);
319 size_t min_expand
= allocated_last_gc
* PARAM_VALUE (GGC_MIN_EXPAND
) / 100;
321 if (G
.allocated
< allocated_last_gc
+ min_expand
)
325 debug_ggc_balance ();
328 timevar_push (TV_GC
);
330 fprintf (stderr
, " {GC %luk -> ", (unsigned long)G
.allocated
/ 1024);
335 clear_marks (G
.root
);
337 sweep_objs (&G
.root
);
339 G
.allocated_last_gc
= G
.allocated
;
344 fprintf (stderr
, "%luk}", (unsigned long) G
.allocated
/ 1024);
347 debug_ggc_balance ();
351 /* Called once to initialize the garbage collector. */
358 /* Start a new GGC context. Memory allocated in previous contexts
359 will not be collected while the new context is active. */
366 /* We only allocated 7 bits in the node for the context. This
367 should be more than enough. */
368 if (G
.context
>= 128)
372 /* Finish a GC context. Any uncollected memory in the new context
373 will be merged with the old context. */
380 ggc_pop_context_1 (G
.root
, G
.context
);
384 ggc_pop_context_1 (x
, c
)
391 ggc_pop_context_1 (x
->sub
[0], c
);
393 ggc_pop_context_1 (x
->sub
[1], c
);
399 debug_ggc_tree (p
, indent
)
407 fputs ("(nil)\n", stderr
);
412 debug_ggc_tree (p
->sub
[0], indent
+ 1);
414 for (i
= 0; i
< indent
; ++i
)
416 fprintf (stderr
, "%lx %p\n", (unsigned long)PTR_KEY (p
), (PTR
) p
);
419 debug_ggc_tree (p
->sub
[1], indent
+ 1);
423 /* Collect tree balance metrics */
430 size_t nleaf
, sumdepth
;
432 nleaf
= sumdepth
= 0;
433 tally_leaves (G
.root
, 0, &nleaf
, &sumdepth
);
435 fprintf (stderr
, " {B %.2f,%.1f,%.1f}",
436 /* In a balanced tree, leaf/node should approach 1/2. */
437 (float)nleaf
/ (float)G
.objects
,
438 /* In a balanced tree, average leaf depth should approach lg(n). */
439 (float)sumdepth
/ (float)nleaf
,
440 log ((double) G
.objects
) / M_LN2
);
444 /* Used by debug_ggc_balance, and also by ggc_print_statistics. */
446 tally_leaves (x
, depth
, nleaf
, sumdepth
)
452 if (! x
->sub
[0] && !x
->sub
[1])
460 tally_leaves (x
->sub
[0], depth
+ 1, nleaf
, sumdepth
);
462 tally_leaves (x
->sub
[1], depth
+ 1, nleaf
, sumdepth
);
466 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
468 : ((x) < 1024*1024*10 \
470 : (x) / (1024*1024))))
471 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
473 /* Report on GC memory usage. */
475 ggc_print_statistics ()
477 struct ggc_statistics stats
;
478 size_t nleaf
= 0, sumdepth
= 0;
480 /* Clear the statistics. */
481 memset (&stats
, 0, sizeof (stats
));
483 /* Make sure collection will really occur. */
484 G
.allocated_last_gc
= 0;
486 /* Collect and print the statistics common across collectors. */
487 ggc_print_common_statistics (stderr
, &stats
);
489 /* Report on tree balancing. */
490 tally_leaves (G
.root
, 0, &nleaf
, &sumdepth
);
492 fprintf (stderr
, "\n\
493 Total internal data (bytes)\t%ld%c\n\
494 Number of leaves in tree\t%lu\n\
495 Average leaf depth\t\t%.1f\n",
496 SCALE(G
.objects
* offsetof (struct ggc_mem
, u
)),
497 LABEL(G
.objects
* offsetof (struct ggc_mem
, u
)),
498 (unsigned long)nleaf
, (double)sumdepth
/ (double)nleaf
);
500 /* Report overall memory usage. */
501 fprintf (stderr
, "\n\
502 Total objects allocated\t\t%ld\n\
503 Total memory in GC arena\t%ld%c\n",
504 (unsigned long)G
.objects
,
505 SCALE(G
.allocated
), LABEL(G
.allocated
));
508 struct ggc_pch_data
*
511 sorry ("Generating PCH files is not supported when using ggc-simple.c");
512 /* It could be supported, but the code is not yet written. */
517 ggc_pch_count_object (d
, x
, size
)
518 struct ggc_pch_data
*d ATTRIBUTE_UNUSED
;
519 void *x ATTRIBUTE_UNUSED
;
520 size_t size ATTRIBUTE_UNUSED
;
525 ggc_pch_total_size (d
)
526 struct ggc_pch_data
*d ATTRIBUTE_UNUSED
;
532 ggc_pch_this_base (d
, base
)
533 struct ggc_pch_data
*d ATTRIBUTE_UNUSED
;
534 void *base ATTRIBUTE_UNUSED
;
540 ggc_pch_alloc_object (d
, x
, size
)
541 struct ggc_pch_data
*d ATTRIBUTE_UNUSED
;
542 void *x ATTRIBUTE_UNUSED
;
543 size_t size ATTRIBUTE_UNUSED
;
549 ggc_pch_prepare_write (d
, f
)
550 struct ggc_pch_data
* d ATTRIBUTE_UNUSED
;
551 FILE * f ATTRIBUTE_UNUSED
;
556 ggc_pch_write_object (d
, f
, x
, newx
, size
)
557 struct ggc_pch_data
* d ATTRIBUTE_UNUSED
;
558 FILE *f ATTRIBUTE_UNUSED
;
559 void *x ATTRIBUTE_UNUSED
;
560 void *newx ATTRIBUTE_UNUSED
;
561 size_t size ATTRIBUTE_UNUSED
;
566 ggc_pch_finish (d
, f
)
567 struct ggc_pch_data
* d ATTRIBUTE_UNUSED
;
568 FILE *f ATTRIBUTE_UNUSED
;
573 ggc_pch_read (f
, addr
)
574 FILE *f ATTRIBUTE_UNUSED
;
575 void *addr ATTRIBUTE_UNUSED
;
577 /* This should be impossible, since we won't generate any valid PCH
578 files for this configuration. */