1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 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 under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
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
22 /* Generic garbage collection (GC) functions and data, not specific to
23 any particular GC implementation. */
27 #include "coretypes.h"
33 #ifdef HAVE_SYS_RESOURCE_H
34 # include <sys/resource.h>
38 # include <sys/mman.h>
41 #ifdef ENABLE_VALGRIND_CHECKING
42 # ifdef HAVE_MEMCHECK_H
43 # include <memcheck.h>
45 # include <valgrind.h>
48 /* Avoid #ifdef:s when we can help it. */
49 #define VALGRIND_DISCARD(x)
52 /* Statistics about the allocation. */
53 static ggc_statistics
*ggc_stats
;
55 struct traversal_state
;
57 static int ggc_htab_delete (void **, void *);
58 static hashval_t
saving_htab_hash (const void *);
59 static int saving_htab_eq (const void *, const void *);
60 static int call_count (void **, void *);
61 static int call_alloc (void **, void *);
62 static int compare_ptr_data (const void *, const void *);
63 static void relocate_ptrs (void *, void *);
64 static void write_pch_globals (const struct ggc_root_tab
* const *tab
,
65 struct traversal_state
*state
);
66 static double ggc_rlimit_bound (double);
68 /* Maintain global roots that are preserved during GC. */
70 /* Process a slot of an htab by deleting it if it has not been marked. */
73 ggc_htab_delete (void **slot
, void *info
)
75 const struct ggc_cache_tab
*r
= (const struct ggc_cache_tab
*) info
;
77 if (! (*r
->marked_p
) (*slot
))
78 htab_clear_slot (*r
->base
, slot
);
85 /* Iterate through all registered roots and mark each element. */
90 const struct ggc_root_tab
*const *rt
;
91 const struct ggc_root_tab
*rti
;
92 const struct ggc_cache_tab
*const *ct
;
93 const struct ggc_cache_tab
*cti
;
96 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
97 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
98 memset (rti
->base
, 0, rti
->stride
);
100 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
101 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
102 for (i
= 0; i
< rti
->nelt
; i
++)
103 (*rti
->cb
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
105 ggc_mark_stringpool ();
107 /* Now scan all hash tables that have objects which are to be deleted if
108 they are not already marked. */
109 for (ct
= gt_ggc_cache_rtab
; *ct
; ct
++)
110 for (cti
= *ct
; cti
->base
!= NULL
; cti
++)
113 ggc_set_mark (*cti
->base
);
114 htab_traverse_noresize (*cti
->base
, ggc_htab_delete
, (void *) cti
);
115 ggc_set_mark ((*cti
->base
)->entries
);
119 /* Allocate a block of memory, then clear it. */
121 ggc_alloc_cleared (size_t size
)
123 void *buf
= ggc_alloc (size
);
124 memset (buf
, 0, size
);
128 /* Resize a block of memory, possibly re-allocating it. */
130 ggc_realloc (void *x
, size_t size
)
136 return ggc_alloc (size
);
138 old_size
= ggc_get_size (x
);
139 if (size
<= old_size
)
141 /* Mark the unwanted memory as unaccessible. We also need to make
142 the "new" size accessible, since ggc_get_size returns the size of
143 the pool, not the size of the individually allocated object, the
144 size which was previously made accessible. Unfortunately, we
145 don't know that previously allocated size. Without that
146 knowledge we have to lose some initialization-tracking for the
147 old parts of the object. An alternative is to mark the whole
148 old_size as reachable, but that would lose tracking of writes
149 after the end of the object (by small offsets). Discard the
150 handle to avoid handle leak. */
151 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) x
+ size
,
153 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x
, size
));
157 r
= ggc_alloc (size
);
159 /* Since ggc_get_size returns the size of the pool, not the size of the
160 individually allocated object, we'd access parts of the old object
161 that were marked invalid with the memcpy below. We lose a bit of the
162 initialization-tracking since some of it may be uninitialized. */
163 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x
, old_size
));
165 memcpy (r
, x
, old_size
);
167 /* The old object is not supposed to be used anymore. */
168 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (x
, old_size
));
173 /* Like ggc_alloc_cleared, but performs a multiplication. */
175 ggc_calloc (size_t s1
, size_t s2
)
177 return ggc_alloc_cleared (s1
* s2
);
180 /* These are for splay_tree_new_ggc. */
182 ggc_splay_alloc (int sz
, void *nl
)
186 return ggc_alloc (sz
);
190 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED
, void *nl
)
196 /* Print statistics that are independent of the collector in use. */
197 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
199 : ((x) < 1024*1024*10 \
201 : (x) / (1024*1024))))
202 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
205 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED
,
206 ggc_statistics
*stats
)
208 /* Set the pointer so that during collection we will actually gather
212 /* Then do one collection to fill in the statistics. */
215 /* At present, we don't really gather any interesting statistics. */
217 /* Don't gather statistics any more. */
221 /* Functions for saving and restoring GCable memory to disk. */
223 static htab_t saving_htab
;
228 void *note_ptr_cookie
;
229 gt_note_pointers note_ptr_fn
;
230 gt_handle_reorder reorder_fn
;
235 #define POINTER_HASH(x) (hashval_t)((long)x >> 3)
237 /* Register an object in the hash table. */
240 gt_pch_note_object (void *obj
, void *note_ptr_cookie
,
241 gt_note_pointers note_ptr_fn
)
243 struct ptr_data
**slot
;
245 if (obj
== NULL
|| obj
== (void *) 1)
248 slot
= (struct ptr_data
**)
249 htab_find_slot_with_hash (saving_htab
, obj
, POINTER_HASH (obj
),
253 if ((*slot
)->note_ptr_fn
!= note_ptr_fn
254 || (*slot
)->note_ptr_cookie
!= note_ptr_cookie
)
259 *slot
= xcalloc (sizeof (struct ptr_data
), 1);
261 (*slot
)->note_ptr_fn
= note_ptr_fn
;
262 (*slot
)->note_ptr_cookie
= note_ptr_cookie
;
263 if (note_ptr_fn
== gt_pch_p_S
)
264 (*slot
)->size
= strlen (obj
) + 1;
266 (*slot
)->size
= ggc_get_size (obj
);
270 /* Register an object in the hash table. */
273 gt_pch_note_reorder (void *obj
, void *note_ptr_cookie
,
274 gt_handle_reorder reorder_fn
)
276 struct ptr_data
*data
;
278 if (obj
== NULL
|| obj
== (void *) 1)
281 data
= htab_find_with_hash (saving_htab
, obj
, POINTER_HASH (obj
));
283 || data
->note_ptr_cookie
!= note_ptr_cookie
)
286 data
->reorder_fn
= reorder_fn
;
289 /* Hash and equality functions for saving_htab, callbacks for htab_create. */
292 saving_htab_hash (const void *p
)
294 return POINTER_HASH (((struct ptr_data
*)p
)->obj
);
298 saving_htab_eq (const void *p1
, const void *p2
)
300 return ((struct ptr_data
*)p1
)->obj
== p2
;
303 /* Handy state for the traversal functions. */
305 struct traversal_state
308 struct ggc_pch_data
*d
;
310 struct ptr_data
**ptrs
;
314 /* Callbacks for htab_traverse. */
317 call_count (void **slot
, void *state_p
)
319 struct ptr_data
*d
= (struct ptr_data
*)*slot
;
320 struct traversal_state
*state
= (struct traversal_state
*)state_p
;
322 ggc_pch_count_object (state
->d
, d
->obj
, d
->size
);
328 call_alloc (void **slot
, void *state_p
)
330 struct ptr_data
*d
= (struct ptr_data
*)*slot
;
331 struct traversal_state
*state
= (struct traversal_state
*)state_p
;
333 d
->new_addr
= ggc_pch_alloc_object (state
->d
, d
->obj
, d
->size
);
334 state
->ptrs
[state
->ptrs_i
++] = d
;
338 /* Callback for qsort. */
341 compare_ptr_data (const void *p1_p
, const void *p2_p
)
343 struct ptr_data
*p1
= *(struct ptr_data
*const *)p1_p
;
344 struct ptr_data
*p2
= *(struct ptr_data
*const *)p2_p
;
345 return (((size_t)p1
->new_addr
> (size_t)p2
->new_addr
)
346 - ((size_t)p1
->new_addr
< (size_t)p2
->new_addr
));
349 /* Callbacks for note_ptr_fn. */
352 relocate_ptrs (void *ptr_p
, void *state_p
)
354 void **ptr
= (void **)ptr_p
;
355 struct traversal_state
*state ATTRIBUTE_UNUSED
356 = (struct traversal_state
*)state_p
;
357 struct ptr_data
*result
;
359 if (*ptr
== NULL
|| *ptr
== (void *)1)
362 result
= htab_find_with_hash (saving_htab
, *ptr
, POINTER_HASH (*ptr
));
365 *ptr
= result
->new_addr
;
368 /* Write out, after relocation, the pointers in TAB. */
370 write_pch_globals (const struct ggc_root_tab
* const *tab
,
371 struct traversal_state
*state
)
373 const struct ggc_root_tab
*const *rt
;
374 const struct ggc_root_tab
*rti
;
377 for (rt
= tab
; *rt
; rt
++)
378 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
379 for (i
= 0; i
< rti
->nelt
; i
++)
381 void *ptr
= *(void **)((char *)rti
->base
+ rti
->stride
* i
);
382 struct ptr_data
*new_ptr
;
383 if (ptr
== NULL
|| ptr
== (void *)1)
385 if (fwrite (&ptr
, sizeof (void *), 1, state
->f
)
387 fatal_error ("can't write PCH file: %m");
391 new_ptr
= htab_find_with_hash (saving_htab
, ptr
,
393 if (fwrite (&new_ptr
->new_addr
, sizeof (void *), 1, state
->f
)
395 fatal_error ("can't write PCH file: %m");
400 /* Hold the information we need to mmap the file back in. */
406 void *preferred_base
;
409 /* Write out the state of the compiler to F. */
412 gt_pch_save (FILE *f
)
414 const struct ggc_root_tab
*const *rt
;
415 const struct ggc_root_tab
*rti
;
417 struct traversal_state state
;
418 char *this_object
= NULL
;
419 size_t this_object_size
= 0;
420 struct mmap_info mmi
;
421 size_t page_size
= getpagesize();
423 gt_pch_save_stringpool ();
425 saving_htab
= htab_create (50000, saving_htab_hash
, saving_htab_eq
, free
);
427 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
428 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
429 for (i
= 0; i
< rti
->nelt
; i
++)
430 (*rti
->pchw
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
432 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
433 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
434 for (i
= 0; i
< rti
->nelt
; i
++)
435 (*rti
->pchw
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
437 /* Prepare the objects for writing, determine addresses and such. */
439 state
.d
= init_ggc_pch();
441 htab_traverse (saving_htab
, call_count
, &state
);
443 mmi
.size
= ggc_pch_total_size (state
.d
);
445 /* Try to arrange things so that no relocation is necessary,
446 but don't try very hard. On most platforms, this will always work,
447 and on the rest it's a lot of work to do better. */
449 mmi
.preferred_base
= mmap (NULL
, mmi
.size
,
450 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
451 fileno (state
.f
), 0);
452 if (mmi
.preferred_base
== (void *)-1)
453 mmi
.preferred_base
= NULL
;
455 munmap (mmi
.preferred_base
, mmi
.size
);
456 #else /* HAVE_MMAP_FILE */
457 mmi
.preferred_base
= NULL
;
458 #endif /* HAVE_MMAP_FILE */
460 ggc_pch_this_base (state
.d
, mmi
.preferred_base
);
462 state
.ptrs
= xmalloc (state
.count
* sizeof (*state
.ptrs
));
464 htab_traverse (saving_htab
, call_alloc
, &state
);
465 qsort (state
.ptrs
, state
.count
, sizeof (*state
.ptrs
), compare_ptr_data
);
467 /* Write out all the scalar variables. */
468 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
469 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
470 if (fwrite (rti
->base
, rti
->stride
, 1, f
) != 1)
471 fatal_error ("can't write PCH file: %m");
473 /* Write out all the global pointers, after translation. */
474 write_pch_globals (gt_ggc_rtab
, &state
);
475 write_pch_globals (gt_pch_cache_rtab
, &state
);
477 ggc_pch_prepare_write (state
.d
, state
.f
);
479 /* Pad the PCH file so that the mmaped area starts on a page boundary. */
482 o
= ftell (state
.f
) + sizeof (mmi
);
484 fatal_error ("can't get position in PCH file: %m");
485 mmi
.offset
= page_size
- o
% page_size
;
486 if (mmi
.offset
== page_size
)
490 if (fwrite (&mmi
, sizeof (mmi
), 1, state
.f
) != 1)
491 fatal_error ("can't write PCH file: %m");
493 && fseek (state
.f
, mmi
.offset
, SEEK_SET
) != 0)
494 fatal_error ("can't write padding to PCH file: %m");
496 /* Actually write out the objects. */
497 for (i
= 0; i
< state
.count
; i
++)
499 if (this_object_size
< state
.ptrs
[i
]->size
)
501 this_object_size
= state
.ptrs
[i
]->size
;
502 this_object
= xrealloc (this_object
, this_object_size
);
504 memcpy (this_object
, state
.ptrs
[i
]->obj
, state
.ptrs
[i
]->size
);
505 if (state
.ptrs
[i
]->reorder_fn
!= NULL
)
506 state
.ptrs
[i
]->reorder_fn (state
.ptrs
[i
]->obj
,
507 state
.ptrs
[i
]->note_ptr_cookie
,
508 relocate_ptrs
, &state
);
509 state
.ptrs
[i
]->note_ptr_fn (state
.ptrs
[i
]->obj
,
510 state
.ptrs
[i
]->note_ptr_cookie
,
511 relocate_ptrs
, &state
);
512 ggc_pch_write_object (state
.d
, state
.f
, state
.ptrs
[i
]->obj
,
513 state
.ptrs
[i
]->new_addr
, state
.ptrs
[i
]->size
);
514 if (state
.ptrs
[i
]->note_ptr_fn
!= gt_pch_p_S
)
515 memcpy (state
.ptrs
[i
]->obj
, this_object
, state
.ptrs
[i
]->size
);
517 ggc_pch_finish (state
.d
, state
.f
);
518 gt_pch_fixup_stringpool ();
521 htab_delete (saving_htab
);
524 /* Read the state of the compiler back in from F. */
527 gt_pch_restore (FILE *f
)
529 const struct ggc_root_tab
*const *rt
;
530 const struct ggc_root_tab
*rti
;
532 struct mmap_info mmi
;
535 /* Delete any deletable objects. This makes ggc_pch_read much
536 faster, as it can be sure that no GCable objects remain other
537 than the ones just read in. */
538 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
539 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
540 memset (rti
->base
, 0, rti
->stride
);
542 /* Read in all the scalar variables. */
543 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
544 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
545 if (fread (rti
->base
, rti
->stride
, 1, f
) != 1)
546 fatal_error ("can't read PCH file: %m");
548 /* Read in all the global pointers, in 6 easy loops. */
549 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
550 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
551 for (i
= 0; i
< rti
->nelt
; i
++)
552 if (fread ((char *)rti
->base
+ rti
->stride
* i
,
553 sizeof (void *), 1, f
) != 1)
554 fatal_error ("can't read PCH file: %m");
556 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
557 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
558 for (i
= 0; i
< rti
->nelt
; i
++)
559 if (fread ((char *)rti
->base
+ rti
->stride
* i
,
560 sizeof (void *), 1, f
) != 1)
561 fatal_error ("can't read PCH file: %m");
563 if (fread (&mmi
, sizeof (mmi
), 1, f
) != 1)
564 fatal_error ("can't read PCH file: %m");
567 addr
= mmap (mmi
.preferred_base
, mmi
.size
,
568 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
569 fileno (f
), mmi
.offset
);
573 if (addr
== (void *)-1)
575 addr
= xmalloc (mmi
.size
);
576 if (fseek (f
, mmi
.offset
, SEEK_SET
) != 0
577 || fread (&mmi
, mmi
.size
, 1, f
) != 1)
578 fatal_error ("can't read PCH file: %m");
580 else if (fseek (f
, mmi
.offset
+ mmi
.size
, SEEK_SET
) != 0)
581 fatal_error ("can't read PCH file: %m");
583 ggc_pch_read (f
, addr
);
585 if (addr
!= mmi
.preferred_base
)
587 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
588 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
589 for (i
= 0; i
< rti
->nelt
; i
++)
591 char **ptr
= (char **)((char *)rti
->base
+ rti
->stride
* i
);
593 *ptr
+= (size_t)addr
- (size_t)mmi
.preferred_base
;
596 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
597 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
598 for (i
= 0; i
< rti
->nelt
; i
++)
600 char **ptr
= (char **)((char *)rti
->base
+ rti
->stride
* i
);
602 *ptr
+= (size_t)addr
- (size_t)mmi
.preferred_base
;
605 sorry ("had to relocate PCH");
608 gt_pch_restore_stringpool ();
611 /* Modify the bound based on rlimits. Keep the smallest number found. */
613 ggc_rlimit_bound (double limit
)
615 #if defined(HAVE_GETRLIMIT)
618 if (getrlimit (RLIMIT_RSS
, &rlim
) == 0
619 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
620 && rlim
.rlim_cur
< limit
)
621 limit
= rlim
.rlim_cur
;
624 if (getrlimit (RLIMIT_DATA
, &rlim
) == 0
625 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
626 && rlim
.rlim_cur
< limit
)
627 limit
= rlim
.rlim_cur
;
630 if (getrlimit (RLIMIT_AS
, &rlim
) == 0
631 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
632 && rlim
.rlim_cur
< limit
)
633 limit
= rlim
.rlim_cur
;
635 #endif /* HAVE_GETRLIMIT */
640 /* Heuristic to set a default for GGC_MIN_EXPAND. */
642 ggc_min_expand_heuristic (void)
644 double min_expand
= physmem_total();
646 /* Adjust for rlimits. */
647 min_expand
= ggc_rlimit_bound (min_expand
);
649 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
650 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
651 min_expand
/= 1024*1024*1024;
653 min_expand
= MIN (min_expand
, 70);
659 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
661 ggc_min_heapsize_heuristic (void)
663 double min_heap_kbytes
= physmem_total();
665 /* Adjust for rlimits. */
666 min_heap_kbytes
= ggc_rlimit_bound (min_heap_kbytes
);
668 min_heap_kbytes
/= 1024; /* convert to Kbytes. */
670 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
671 bound of 128M (when RAM >= 1GB). */
672 min_heap_kbytes
/= 8;
673 min_heap_kbytes
= MAX (min_heap_kbytes
, 4 * 1024);
674 min_heap_kbytes
= MIN (min_heap_kbytes
, 128 * 1024);
676 return min_heap_kbytes
;
680 init_ggc_heuristics (void)
682 #ifndef ENABLE_GC_ALWAYS_COLLECT
683 set_param_value ("ggc-min-expand", ggc_min_expand_heuristic());
684 set_param_value ("ggc-min-heapsize", ggc_min_heapsize_heuristic());