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
PARAMS ((void **, void *));
58 static hashval_t saving_htab_hash
PARAMS ((const PTR
));
59 static int saving_htab_eq
PARAMS ((const PTR
, const PTR
));
60 static int call_count
PARAMS ((void **, void *));
61 static int call_alloc
PARAMS ((void **, void *));
62 static int compare_ptr_data
PARAMS ((const void *, const void *));
63 static void relocate_ptrs
PARAMS ((void *, void *));
64 static void write_pch_globals
PARAMS ((const struct ggc_root_tab
* const *tab
,
65 struct traversal_state
*state
));
66 static double ggc_rlimit_bound
PARAMS ((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 (slot
, info
)
77 const struct ggc_cache_tab
*r
= (const struct ggc_cache_tab
*) info
;
79 if (! (*r
->marked_p
) (*slot
))
80 htab_clear_slot (*r
->base
, slot
);
87 /* Iterate through all registered roots and mark each element. */
92 const struct ggc_root_tab
*const *rt
;
93 const struct ggc_root_tab
*rti
;
94 const struct ggc_cache_tab
*const *ct
;
95 const struct ggc_cache_tab
*cti
;
98 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
99 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
100 memset (rti
->base
, 0, rti
->stride
);
102 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
103 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
104 for (i
= 0; i
< rti
->nelt
; i
++)
105 (*rti
->cb
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
107 ggc_mark_stringpool ();
109 /* Now scan all hash tables that have objects which are to be deleted if
110 they are not already marked. */
111 for (ct
= gt_ggc_cache_rtab
; *ct
; ct
++)
112 for (cti
= *ct
; cti
->base
!= NULL
; cti
++)
115 ggc_set_mark (*cti
->base
);
116 htab_traverse (*cti
->base
, ggc_htab_delete
, (PTR
) cti
);
117 ggc_set_mark ((*cti
->base
)->entries
);
121 /* Allocate a block of memory, then clear it. */
123 ggc_alloc_cleared (size
)
126 void *buf
= ggc_alloc (size
);
127 memset (buf
, 0, size
);
131 /* Resize a block of memory, possibly re-allocating it. */
133 ggc_realloc (x
, size
)
141 return ggc_alloc (size
);
143 old_size
= ggc_get_size (x
);
144 if (size
<= old_size
)
146 /* Mark the unwanted memory as unaccessible. We also need to make
147 the "new" size accessible, since ggc_get_size returns the size of
148 the pool, not the size of the individually allocated object, the
149 size which was previously made accessible. Unfortunately, we
150 don't know that previously allocated size. Without that
151 knowledge we have to lose some initialization-tracking for the
152 old parts of the object. An alternative is to mark the whole
153 old_size as reachable, but that would lose tracking of writes
154 after the end of the object (by small offsets). Discard the
155 handle to avoid handle leak. */
156 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) x
+ size
,
158 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x
, size
));
162 r
= ggc_alloc (size
);
164 /* Since ggc_get_size returns the size of the pool, not the size of the
165 individually allocated object, we'd access parts of the old object
166 that were marked invalid with the memcpy below. We lose a bit of the
167 initialization-tracking since some of it may be uninitialized. */
168 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x
, old_size
));
170 memcpy (r
, x
, old_size
);
172 /* The old object is not supposed to be used anymore. */
173 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (x
, old_size
));
178 /* Like ggc_alloc_cleared, but performs a multiplication. */
183 return ggc_alloc_cleared (s1
* s2
);
186 /* These are for splay_tree_new_ggc. */
188 ggc_splay_alloc (sz
, nl
)
194 return ggc_alloc (sz
);
198 ggc_splay_dont_free (x
, nl
)
199 PTR x ATTRIBUTE_UNUSED
;
206 /* Print statistics that are independent of the collector in use. */
207 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
209 : ((x) < 1024*1024*10 \
211 : (x) / (1024*1024))))
212 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
215 ggc_print_common_statistics (stream
, stats
)
216 FILE *stream ATTRIBUTE_UNUSED
;
217 ggc_statistics
*stats
;
219 /* Set the pointer so that during collection we will actually gather
223 /* Then do one collection to fill in the statistics. */
226 /* At present, we don't really gather any interesting statistics. */
228 /* Don't gather statistics any more. */
232 /* Functions for saving and restoring GCable memory to disk. */
234 static htab_t saving_htab
;
239 void *note_ptr_cookie
;
240 gt_note_pointers note_ptr_fn
;
241 gt_handle_reorder reorder_fn
;
246 #define POINTER_HASH(x) (hashval_t)((long)x >> 3)
248 /* Register an object in the hash table. */
251 gt_pch_note_object (obj
, note_ptr_cookie
, note_ptr_fn
)
253 void *note_ptr_cookie
;
254 gt_note_pointers note_ptr_fn
;
256 struct ptr_data
**slot
;
258 if (obj
== NULL
|| obj
== (void *) 1)
261 slot
= (struct ptr_data
**)
262 htab_find_slot_with_hash (saving_htab
, obj
, POINTER_HASH (obj
),
266 if ((*slot
)->note_ptr_fn
!= note_ptr_fn
267 || (*slot
)->note_ptr_cookie
!= note_ptr_cookie
)
272 *slot
= xcalloc (sizeof (struct ptr_data
), 1);
274 (*slot
)->note_ptr_fn
= note_ptr_fn
;
275 (*slot
)->note_ptr_cookie
= note_ptr_cookie
;
276 if (note_ptr_fn
== gt_pch_p_S
)
277 (*slot
)->size
= strlen (obj
) + 1;
279 (*slot
)->size
= ggc_get_size (obj
);
283 /* Register an object in the hash table. */
286 gt_pch_note_reorder (obj
, note_ptr_cookie
, reorder_fn
)
288 void *note_ptr_cookie
;
289 gt_handle_reorder reorder_fn
;
291 struct ptr_data
*data
;
293 if (obj
== NULL
|| obj
== (void *) 1)
296 data
= htab_find_with_hash (saving_htab
, obj
, POINTER_HASH (obj
));
298 || data
->note_ptr_cookie
!= note_ptr_cookie
)
301 data
->reorder_fn
= reorder_fn
;
304 /* Hash and equality functions for saving_htab, callbacks for htab_create. */
310 return POINTER_HASH (((struct ptr_data
*)p
)->obj
);
314 saving_htab_eq (p1
, p2
)
318 return ((struct ptr_data
*)p1
)->obj
== p2
;
321 /* Handy state for the traversal functions. */
323 struct traversal_state
326 struct ggc_pch_data
*d
;
328 struct ptr_data
**ptrs
;
332 /* Callbacks for htab_traverse. */
335 call_count (slot
, state_p
)
339 struct ptr_data
*d
= (struct ptr_data
*)*slot
;
340 struct traversal_state
*state
= (struct traversal_state
*)state_p
;
342 ggc_pch_count_object (state
->d
, d
->obj
, d
->size
);
348 call_alloc (slot
, state_p
)
352 struct ptr_data
*d
= (struct ptr_data
*)*slot
;
353 struct traversal_state
*state
= (struct traversal_state
*)state_p
;
355 d
->new_addr
= ggc_pch_alloc_object (state
->d
, d
->obj
, d
->size
);
356 state
->ptrs
[state
->ptrs_i
++] = d
;
360 /* Callback for qsort. */
363 compare_ptr_data (p1_p
, p2_p
)
367 struct ptr_data
*p1
= *(struct ptr_data
*const *)p1_p
;
368 struct ptr_data
*p2
= *(struct ptr_data
*const *)p2_p
;
369 return (((size_t)p1
->new_addr
> (size_t)p2
->new_addr
)
370 - ((size_t)p1
->new_addr
< (size_t)p2
->new_addr
));
373 /* Callbacks for note_ptr_fn. */
376 relocate_ptrs (ptr_p
, state_p
)
380 void **ptr
= (void **)ptr_p
;
381 struct traversal_state
*state ATTRIBUTE_UNUSED
382 = (struct traversal_state
*)state_p
;
383 struct ptr_data
*result
;
385 if (*ptr
== NULL
|| *ptr
== (void *)1)
388 result
= htab_find_with_hash (saving_htab
, *ptr
, POINTER_HASH (*ptr
));
391 *ptr
= result
->new_addr
;
394 /* Write out, after relocation, the pointers in TAB. */
396 write_pch_globals (tab
, state
)
397 const struct ggc_root_tab
* const *tab
;
398 struct traversal_state
*state
;
400 const struct ggc_root_tab
*const *rt
;
401 const struct ggc_root_tab
*rti
;
404 for (rt
= tab
; *rt
; rt
++)
405 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
406 for (i
= 0; i
< rti
->nelt
; i
++)
408 void *ptr
= *(void **)((char *)rti
->base
+ rti
->stride
* i
);
409 struct ptr_data
*new_ptr
;
410 if (ptr
== NULL
|| ptr
== (void *)1)
412 if (fwrite (&ptr
, sizeof (void *), 1, state
->f
)
414 fatal_io_error ("can't write PCH file");
418 new_ptr
= htab_find_with_hash (saving_htab
, ptr
,
420 if (fwrite (&new_ptr
->new_addr
, sizeof (void *), 1, state
->f
)
422 fatal_io_error ("can't write PCH file");
427 /* Hold the information we need to mmap the file back in. */
433 void *preferred_base
;
436 /* Write out the state of the compiler to F. */
442 const struct ggc_root_tab
*const *rt
;
443 const struct ggc_root_tab
*rti
;
445 struct traversal_state state
;
446 char *this_object
= NULL
;
447 size_t this_object_size
= 0;
448 struct mmap_info mmi
;
449 size_t page_size
= getpagesize();
451 gt_pch_save_stringpool ();
453 saving_htab
= htab_create (50000, saving_htab_hash
, saving_htab_eq
, free
);
455 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
456 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
457 for (i
= 0; i
< rti
->nelt
; i
++)
458 (*rti
->pchw
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
460 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
461 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
462 for (i
= 0; i
< rti
->nelt
; i
++)
463 (*rti
->pchw
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
465 /* Prepare the objects for writing, determine addresses and such. */
467 state
.d
= init_ggc_pch();
469 htab_traverse (saving_htab
, call_count
, &state
);
471 mmi
.size
= ggc_pch_total_size (state
.d
);
473 /* Try to arrange things so that no relocation is necessary,
474 but don't try very hard. On most platforms, this will always work,
475 and on the rest it's a lot of work to do better. */
477 mmi
.preferred_base
= mmap (NULL
, mmi
.size
,
478 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
479 fileno (state
.f
), 0);
480 if (mmi
.preferred_base
== (void *)-1)
481 mmi
.preferred_base
= NULL
;
483 munmap (mmi
.preferred_base
, mmi
.size
);
484 #else /* HAVE_MMAP_FILE */
485 mmi
.preferred_base
= NULL
;
486 #endif /* HAVE_MMAP_FILE */
488 ggc_pch_this_base (state
.d
, mmi
.preferred_base
);
490 state
.ptrs
= xmalloc (state
.count
* sizeof (*state
.ptrs
));
492 htab_traverse (saving_htab
, call_alloc
, &state
);
493 qsort (state
.ptrs
, state
.count
, sizeof (*state
.ptrs
), compare_ptr_data
);
495 /* Write out all the scalar variables. */
496 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
497 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
498 if (fwrite (rti
->base
, rti
->stride
, 1, f
) != 1)
499 fatal_io_error ("can't write PCH file");
501 /* Write out all the global pointers, after translation. */
502 write_pch_globals (gt_ggc_rtab
, &state
);
503 write_pch_globals (gt_pch_cache_rtab
, &state
);
505 ggc_pch_prepare_write (state
.d
, state
.f
);
507 /* Pad the PCH file so that the mmaped area starts on a page boundary. */
510 o
= ftell (state
.f
) + sizeof (mmi
);
512 fatal_io_error ("can't get position in PCH file");
513 mmi
.offset
= page_size
- o
% page_size
;
514 if (mmi
.offset
== page_size
)
518 if (fwrite (&mmi
, sizeof (mmi
), 1, state
.f
) != 1)
519 fatal_io_error ("can't write PCH file");
521 && fseek (state
.f
, mmi
.offset
, SEEK_SET
) != 0)
522 fatal_io_error ("can't write padding to PCH file");
524 /* Actually write out the objects. */
525 for (i
= 0; i
< state
.count
; i
++)
527 if (this_object_size
< state
.ptrs
[i
]->size
)
529 this_object_size
= state
.ptrs
[i
]->size
;
530 this_object
= xrealloc (this_object
, this_object_size
);
532 memcpy (this_object
, state
.ptrs
[i
]->obj
, state
.ptrs
[i
]->size
);
533 if (state
.ptrs
[i
]->reorder_fn
!= NULL
)
534 state
.ptrs
[i
]->reorder_fn (state
.ptrs
[i
]->obj
,
535 state
.ptrs
[i
]->note_ptr_cookie
,
536 relocate_ptrs
, &state
);
537 state
.ptrs
[i
]->note_ptr_fn (state
.ptrs
[i
]->obj
,
538 state
.ptrs
[i
]->note_ptr_cookie
,
539 relocate_ptrs
, &state
);
540 ggc_pch_write_object (state
.d
, state
.f
, state
.ptrs
[i
]->obj
,
541 state
.ptrs
[i
]->new_addr
, state
.ptrs
[i
]->size
);
542 if (state
.ptrs
[i
]->note_ptr_fn
!= gt_pch_p_S
)
543 memcpy (state
.ptrs
[i
]->obj
, this_object
, state
.ptrs
[i
]->size
);
545 ggc_pch_finish (state
.d
, state
.f
);
548 htab_delete (saving_htab
);
551 /* Read the state of the compiler back in from F. */
557 const struct ggc_root_tab
*const *rt
;
558 const struct ggc_root_tab
*rti
;
560 struct mmap_info mmi
;
563 /* Delete any deletable objects. This makes ggc_pch_read much
564 faster, as it can be sure that no GCable objects remain other
565 than the ones just read in. */
566 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
567 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
568 memset (rti
->base
, 0, rti
->stride
);
570 /* Read in all the scalar variables. */
571 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
572 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
573 if (fread (rti
->base
, rti
->stride
, 1, f
) != 1)
574 fatal_io_error ("can't read PCH file");
576 /* Read in all the global pointers, in 6 easy loops. */
577 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
578 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
579 for (i
= 0; i
< rti
->nelt
; i
++)
580 if (fread ((char *)rti
->base
+ rti
->stride
* i
,
581 sizeof (void *), 1, f
) != 1)
582 fatal_io_error ("can't read PCH file");
584 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
585 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
586 for (i
= 0; i
< rti
->nelt
; i
++)
587 if (fread ((char *)rti
->base
+ rti
->stride
* i
,
588 sizeof (void *), 1, f
) != 1)
589 fatal_io_error ("can't read PCH file");
591 if (fread (&mmi
, sizeof (mmi
), 1, f
) != 1)
592 fatal_io_error ("can't read PCH file");
595 addr
= mmap (mmi
.preferred_base
, mmi
.size
,
596 PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
597 fileno (f
), mmi
.offset
);
601 if (addr
== (void *)-1)
603 addr
= xmalloc (mmi
.size
);
604 if (fseek (f
, mmi
.offset
, SEEK_SET
) != 0
605 || fread (&mmi
, mmi
.size
, 1, f
) != 1)
606 fatal_io_error ("can't read PCH file");
608 else if (fseek (f
, mmi
.offset
+ mmi
.size
, SEEK_SET
) != 0)
609 fatal_io_error ("can't read PCH file");
611 ggc_pch_read (f
, addr
);
613 if (addr
!= mmi
.preferred_base
)
615 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
616 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
617 for (i
= 0; i
< rti
->nelt
; i
++)
619 char **ptr
= (char **)((char *)rti
->base
+ rti
->stride
* i
);
621 *ptr
+= (size_t)addr
- (size_t)mmi
.preferred_base
;
624 for (rt
= gt_pch_cache_rtab
; *rt
; rt
++)
625 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
626 for (i
= 0; i
< rti
->nelt
; i
++)
628 char **ptr
= (char **)((char *)rti
->base
+ rti
->stride
* i
);
630 *ptr
+= (size_t)addr
- (size_t)mmi
.preferred_base
;
633 sorry ("had to relocate PCH");
636 gt_pch_restore_stringpool ();
639 /* Modify the bound based on rlimits. Keep the smallest number found. */
641 ggc_rlimit_bound (limit
)
644 #if defined(HAVE_GETRLIMIT)
647 if (getrlimit (RLIMIT_RSS
, &rlim
) == 0
648 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
649 && rlim
.rlim_cur
< limit
)
650 limit
= rlim
.rlim_cur
;
653 if (getrlimit (RLIMIT_DATA
, &rlim
) == 0
654 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
655 && rlim
.rlim_cur
< limit
)
656 limit
= rlim
.rlim_cur
;
659 if (getrlimit (RLIMIT_AS
, &rlim
) == 0
660 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
661 && rlim
.rlim_cur
< limit
)
662 limit
= rlim
.rlim_cur
;
664 #endif /* HAVE_GETRLIMIT */
669 /* Heuristic to set a default for GGC_MIN_EXPAND. */
671 ggc_min_expand_heuristic()
673 double min_expand
= physmem_total();
675 /* Adjust for rlimits. */
676 min_expand
= ggc_rlimit_bound (min_expand
);
678 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
679 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
680 min_expand
/= 1024*1024*1024;
682 min_expand
= MIN (min_expand
, 70);
688 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
690 ggc_min_heapsize_heuristic()
692 double min_heap_kbytes
= physmem_total();
694 /* Adjust for rlimits. */
695 min_heap_kbytes
= ggc_rlimit_bound (min_heap_kbytes
);
697 min_heap_kbytes
/= 1024; /* convert to Kbytes. */
699 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
700 bound of 128M (when RAM >= 1GB). */
701 min_heap_kbytes
/= 8;
702 min_heap_kbytes
= MAX (min_heap_kbytes
, 4 * 1024);
703 min_heap_kbytes
= MIN (min_heap_kbytes
, 128 * 1024);
705 return min_heap_kbytes
;
709 init_ggc_heuristics ()
711 #ifndef ENABLE_GC_ALWAYS_COLLECT
712 set_param_value ("ggc-min-expand", ggc_min_expand_heuristic());
713 set_param_value ("ggc-min-heapsize", ggc_min_heapsize_heuristic());