1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Generic garbage collection (GC) functions and data, not specific to
21 any particular GC implementation. */
25 #include "coretypes.h"
26 #include "ggc-internal.h"
27 #include "diagnostic-core.h"
29 #include "hosthooks.h"
30 #include "hosthooks-def.h"
34 /* When set, ggc_collect will do collection. */
35 bool ggc_force_collect
;
37 /* When true, protect the contents of the identifier hash table. */
38 bool ggc_protect_identifiers
= true;
40 /* Statistics about the allocation. */
41 static ggc_statistics
*ggc_stats
;
43 struct traversal_state
;
45 static int compare_ptr_data (const void *, const void *);
46 static void relocate_ptrs (void *, void *);
47 static void write_pch_globals (const struct ggc_root_tab
* const *tab
,
48 struct traversal_state
*state
);
50 /* Maintain global roots that are preserved during GC. */
52 /* This extra vector of dynamically registered root_tab-s is used by
53 ggc_mark_roots and gives the ability to dynamically add new GGC root
54 tables, for instance from some plugins; this vector is on the heap
55 since it is used by GGC internally. */
56 typedef const struct ggc_root_tab
*const_ggc_root_tab_t
;
57 static vec
<const_ggc_root_tab_t
> extra_root_vec
;
59 /* Dynamically register a new GGC root table RT. This is useful for
63 ggc_register_root_tab (const struct ggc_root_tab
* rt
)
66 extra_root_vec
.safe_push (rt
);
69 /* Mark all the roots in the table RT. */
72 ggc_mark_root_tab (const_ggc_root_tab_t rt
)
76 for ( ; rt
->base
!= NULL
; rt
++)
77 for (i
= 0; i
< rt
->nelt
; i
++)
78 (*rt
->cb
) (*(void **) ((char *)rt
->base
+ rt
->stride
* i
));
81 /* Iterate through all registered roots and mark each element. */
86 const struct ggc_root_tab
*const *rt
;
87 const_ggc_root_tab_t rtp
, rti
;
90 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
91 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
92 memset (rti
->base
, 0, rti
->stride
);
94 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
95 ggc_mark_root_tab (*rt
);
97 FOR_EACH_VEC_ELT (extra_root_vec
, i
, rtp
)
98 ggc_mark_root_tab (rtp
);
100 if (ggc_protect_identifiers
)
101 ggc_mark_stringpool ();
105 if (! ggc_protect_identifiers
)
106 ggc_purge_stringpool ();
108 /* Some plugins may call ggc_set_mark from here. */
109 invoke_plugin_callbacks (PLUGIN_GGC_MARKING
, NULL
);
112 /* Allocate a block of memory, then clear it. */
114 ggc_internal_cleared_alloc (size_t size
, void (*f
)(void *), size_t s
, size_t n
117 void *buf
= ggc_internal_alloc (size
, f
, s
, n PASS_MEM_STAT
);
118 memset (buf
, 0, size
);
122 /* Resize a block of memory, possibly re-allocating it. */
124 ggc_realloc (void *x
, size_t size MEM_STAT_DECL
)
130 return ggc_internal_alloc (size PASS_MEM_STAT
);
132 old_size
= ggc_get_size (x
);
134 if (size
<= old_size
)
136 /* Mark the unwanted memory as unaccessible. We also need to make
137 the "new" size accessible, since ggc_get_size returns the size of
138 the pool, not the size of the individually allocated object, the
139 size which was previously made accessible. Unfortunately, we
140 don't know that previously allocated size. Without that
141 knowledge we have to lose some initialization-tracking for the
142 old parts of the object. An alternative is to mark the whole
143 old_size as reachable, but that would lose tracking of writes
144 after the end of the object (by small offsets). Discard the
145 handle to avoid handle leak. */
146 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x
+ size
,
148 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x
, size
));
152 r
= ggc_internal_alloc (size PASS_MEM_STAT
);
154 /* Since ggc_get_size returns the size of the pool, not the size of the
155 individually allocated object, we'd access parts of the old object
156 that were marked invalid with the memcpy below. We lose a bit of the
157 initialization-tracking since some of it may be uninitialized. */
158 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x
, old_size
));
160 memcpy (r
, x
, old_size
);
162 /* The old object is not supposed to be used anymore. */
169 ggc_cleared_alloc_htab_ignore_args (size_t c ATTRIBUTE_UNUSED
,
170 size_t n ATTRIBUTE_UNUSED
)
172 gcc_assert (c
* n
== sizeof (struct htab
));
173 return ggc_cleared_alloc
<htab
> ();
176 /* TODO: once we actually use type information in GGC, create a new tag
177 gt_gcc_ptr_array and use it for pointer arrays. */
179 ggc_cleared_alloc_ptr_array_two_args (size_t c
, size_t n
)
181 gcc_assert (sizeof (PTR
*) == n
);
182 return ggc_cleared_vec_alloc
<PTR
*> (c
);
185 /* These are for splay_tree_new_ggc. */
187 ggc_splay_alloc (int sz
, void *nl
)
190 return ggc_internal_alloc (sz
);
194 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED
, void *nl
)
199 /* Print statistics that are independent of the collector in use. */
200 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
202 : ((x) < 1024*1024*10 \
204 : (x) / (1024*1024))))
205 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
208 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED
,
209 ggc_statistics
*stats
)
211 /* Set the pointer so that during collection we will actually gather
215 /* Then do one collection to fill in the statistics. */
218 /* At present, we don't really gather any interesting statistics. */
220 /* Don't gather statistics any more. */
224 /* Functions for saving and restoring GCable memory to disk. */
229 void *note_ptr_cookie
;
230 gt_note_pointers note_ptr_fn
;
231 gt_handle_reorder reorder_fn
;
236 #define POINTER_HASH(x) (hashval_t)((intptr_t)x >> 3)
238 /* Helper for hashing saving_htab. */
240 struct saving_hasher
: typed_free_remove
<ptr_data
>
242 typedef ptr_data
*value_type
;
243 typedef void *compare_type
;
244 static inline hashval_t
hash (const ptr_data
*);
245 static inline bool equal (const ptr_data
*, const void *);
249 saving_hasher::hash (const ptr_data
*p
)
251 return POINTER_HASH (p
->obj
);
255 saving_hasher::equal (const ptr_data
*p1
, const void *p2
)
257 return p1
->obj
== p2
;
260 static hash_table
<saving_hasher
> *saving_htab
;
262 /* Register an object in the hash table. */
265 gt_pch_note_object (void *obj
, void *note_ptr_cookie
,
266 gt_note_pointers note_ptr_fn
)
268 struct ptr_data
**slot
;
270 if (obj
== NULL
|| obj
== (void *) 1)
273 slot
= (struct ptr_data
**)
274 saving_htab
->find_slot_with_hash (obj
, POINTER_HASH (obj
), INSERT
);
277 gcc_assert ((*slot
)->note_ptr_fn
== note_ptr_fn
278 && (*slot
)->note_ptr_cookie
== note_ptr_cookie
);
282 *slot
= XCNEW (struct ptr_data
);
284 (*slot
)->note_ptr_fn
= note_ptr_fn
;
285 (*slot
)->note_ptr_cookie
= note_ptr_cookie
;
286 if (note_ptr_fn
== gt_pch_p_S
)
287 (*slot
)->size
= strlen ((const char *)obj
) + 1;
289 (*slot
)->size
= ggc_get_size (obj
);
293 /* Register an object in the hash table. */
296 gt_pch_note_reorder (void *obj
, void *note_ptr_cookie
,
297 gt_handle_reorder reorder_fn
)
299 struct ptr_data
*data
;
301 if (obj
== NULL
|| obj
== (void *) 1)
304 data
= (struct ptr_data
*)
305 saving_htab
->find_with_hash (obj
, POINTER_HASH (obj
));
306 gcc_assert (data
&& data
->note_ptr_cookie
== note_ptr_cookie
);
308 data
->reorder_fn
= reorder_fn
;
311 /* Handy state for the traversal functions. */
313 struct traversal_state
316 struct ggc_pch_data
*d
;
318 struct ptr_data
**ptrs
;
322 /* Callbacks for htab_traverse. */
325 ggc_call_count (ptr_data
**slot
, traversal_state
*state
)
327 struct ptr_data
*d
= *slot
;
329 ggc_pch_count_object (state
->d
, d
->obj
, d
->size
,
330 d
->note_ptr_fn
== gt_pch_p_S
);
336 ggc_call_alloc (ptr_data
**slot
, traversal_state
*state
)
338 struct ptr_data
*d
= *slot
;
340 d
->new_addr
= ggc_pch_alloc_object (state
->d
, d
->obj
, d
->size
,
341 d
->note_ptr_fn
== gt_pch_p_S
);
342 state
->ptrs
[state
->ptrs_i
++] = d
;
346 /* Callback for qsort. */
349 compare_ptr_data (const void *p1_p
, const void *p2_p
)
351 const struct ptr_data
*const p1
= *(const struct ptr_data
*const *)p1_p
;
352 const struct ptr_data
*const p2
= *(const struct ptr_data
*const *)p2_p
;
353 return (((size_t)p1
->new_addr
> (size_t)p2
->new_addr
)
354 - ((size_t)p1
->new_addr
< (size_t)p2
->new_addr
));
357 /* Callbacks for note_ptr_fn. */
360 relocate_ptrs (void *ptr_p
, void *state_p
)
362 void **ptr
= (void **)ptr_p
;
363 struct traversal_state
*state ATTRIBUTE_UNUSED
364 = (struct traversal_state
*)state_p
;
365 struct ptr_data
*result
;
367 if (*ptr
== NULL
|| *ptr
== (void *)1)
370 result
= (struct ptr_data
*)
371 saving_htab
->find_with_hash (*ptr
, POINTER_HASH (*ptr
));
373 *ptr
= result
->new_addr
;
376 /* Write out, after relocation, the pointers in TAB. */
378 write_pch_globals (const struct ggc_root_tab
* const *tab
,
379 struct traversal_state
*state
)
381 const struct ggc_root_tab
*const *rt
;
382 const struct ggc_root_tab
*rti
;
385 for (rt
= tab
; *rt
; rt
++)
386 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
387 for (i
= 0; i
< rti
->nelt
; i
++)
389 void *ptr
= *(void **)((char *)rti
->base
+ rti
->stride
* i
);
390 struct ptr_data
*new_ptr
;
391 if (ptr
== NULL
|| ptr
== (void *)1)
393 if (fwrite (&ptr
, sizeof (void *), 1, state
->f
)
395 fatal_error (input_location
, "can%'t write PCH file: %m");
399 new_ptr
= (struct ptr_data
*)
400 saving_htab
->find_with_hash (ptr
, POINTER_HASH (ptr
));
401 if (fwrite (&new_ptr
->new_addr
, sizeof (void *), 1, state
->f
)
403 fatal_error (input_location
, "can%'t write PCH file: %m");
408 /* Hold the information we need to mmap the file back in. */
414 void *preferred_base
;
417 /* Write out the state of the compiler to F. */
420 gt_pch_save (FILE *f
)
422 const struct ggc_root_tab
*const *rt
;
423 const struct ggc_root_tab
*rti
;
425 struct traversal_state state
;
426 char *this_object
= NULL
;
427 size_t this_object_size
= 0;
428 struct mmap_info mmi
;
429 const size_t mmap_offset_alignment
= host_hooks
.gt_pch_alloc_granularity ();
431 gt_pch_save_stringpool ();
433 timevar_push (TV_PCH_PTR_REALLOC
);
434 saving_htab
= new hash_table
<saving_hasher
> (50000);
436 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
437 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
438 for (i
= 0; i
< rti
->nelt
; i
++)
439 (*rti
->pchw
)(*(void **)((char *)rti
->base
+ rti
->stride
* i
));
441 /* Prepare the objects for writing, determine addresses and such. */
443 state
.d
= init_ggc_pch ();
445 saving_htab
->traverse
<traversal_state
*, ggc_call_count
> (&state
);
447 mmi
.size
= ggc_pch_total_size (state
.d
);
449 /* Try to arrange things so that no relocation is necessary, but
450 don't try very hard. On most platforms, this will always work,
451 and on the rest it's a lot of work to do better.
452 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
453 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */
454 mmi
.preferred_base
= host_hooks
.gt_pch_get_address (mmi
.size
, fileno (f
));
456 ggc_pch_this_base (state
.d
, mmi
.preferred_base
);
458 state
.ptrs
= XNEWVEC (struct ptr_data
*, state
.count
);
461 saving_htab
->traverse
<traversal_state
*, ggc_call_alloc
> (&state
);
462 timevar_pop (TV_PCH_PTR_REALLOC
);
464 timevar_push (TV_PCH_PTR_SORT
);
465 qsort (state
.ptrs
, state
.count
, sizeof (*state
.ptrs
), compare_ptr_data
);
466 timevar_pop (TV_PCH_PTR_SORT
);
468 /* Write out all the scalar variables. */
469 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
470 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
471 if (fwrite (rti
->base
, rti
->stride
, 1, f
) != 1)
472 fatal_error (input_location
, "can%'t write PCH file: %m");
474 /* Write out all the global pointers, after translation. */
475 write_pch_globals (gt_ggc_rtab
, &state
);
477 /* Pad the PCH file so that the mmapped area starts on an allocation
478 granularity (usually page) boundary. */
481 o
= ftell (state
.f
) + sizeof (mmi
);
483 fatal_error (input_location
, "can%'t get position in PCH file: %m");
484 mmi
.offset
= mmap_offset_alignment
- o
% mmap_offset_alignment
;
485 if (mmi
.offset
== mmap_offset_alignment
)
489 if (fwrite (&mmi
, sizeof (mmi
), 1, state
.f
) != 1)
490 fatal_error (input_location
, "can%'t write PCH file: %m");
492 && fseek (state
.f
, mmi
.offset
, SEEK_SET
) != 0)
493 fatal_error (input_location
, "can%'t write padding to PCH file: %m");
495 ggc_pch_prepare_write (state
.d
, state
.f
);
497 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
498 vec
<char> vbits
= vNULL
;
501 /* Actually write out the objects. */
502 for (i
= 0; i
< state
.count
; i
++)
504 if (this_object_size
< state
.ptrs
[i
]->size
)
506 this_object_size
= state
.ptrs
[i
]->size
;
507 this_object
= XRESIZEVAR (char, this_object
, this_object_size
);
509 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
510 /* obj might contain uninitialized bytes, e.g. in the trailing
511 padding of the object. Avoid warnings by making the memory
512 temporarily defined and then restoring previous state. */
514 size_t valid_size
= state
.ptrs
[i
]->size
;
515 if (__builtin_expect (RUNNING_ON_VALGRIND
, 0))
517 if (vbits
.length () < valid_size
)
518 vbits
.safe_grow (valid_size
);
519 get_vbits
= VALGRIND_GET_VBITS (state
.ptrs
[i
]->obj
,
520 vbits
.address (), valid_size
);
523 /* We assume that first part of obj is addressable, and
524 the rest is unaddressable. Find out where the boundary is
525 using binary search. */
526 size_t lo
= 0, hi
= valid_size
;
529 size_t mid
= (lo
+ hi
) / 2;
530 get_vbits
= VALGRIND_GET_VBITS ((char *) state
.ptrs
[i
]->obj
531 + mid
, vbits
.address (),
535 else if (get_vbits
== 1)
540 if (get_vbits
== 1 || get_vbits
== 3)
543 get_vbits
= VALGRIND_GET_VBITS (state
.ptrs
[i
]->obj
,
549 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (state
.ptrs
[i
]->obj
,
550 state
.ptrs
[i
]->size
));
553 memcpy (this_object
, state
.ptrs
[i
]->obj
, state
.ptrs
[i
]->size
);
554 if (state
.ptrs
[i
]->reorder_fn
!= NULL
)
555 state
.ptrs
[i
]->reorder_fn (state
.ptrs
[i
]->obj
,
556 state
.ptrs
[i
]->note_ptr_cookie
,
557 relocate_ptrs
, &state
);
558 state
.ptrs
[i
]->note_ptr_fn (state
.ptrs
[i
]->obj
,
559 state
.ptrs
[i
]->note_ptr_cookie
,
560 relocate_ptrs
, &state
);
561 ggc_pch_write_object (state
.d
, state
.f
, state
.ptrs
[i
]->obj
,
562 state
.ptrs
[i
]->new_addr
, state
.ptrs
[i
]->size
,
563 state
.ptrs
[i
]->note_ptr_fn
== gt_pch_p_S
);
564 if (state
.ptrs
[i
]->note_ptr_fn
!= gt_pch_p_S
)
565 memcpy (state
.ptrs
[i
]->obj
, this_object
, state
.ptrs
[i
]->size
);
566 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
567 if (__builtin_expect (get_vbits
== 1, 0))
569 (void) VALGRIND_SET_VBITS (state
.ptrs
[i
]->obj
, vbits
.address (),
571 if (valid_size
!= state
.ptrs
[i
]->size
)
572 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *)
580 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
584 ggc_pch_finish (state
.d
, state
.f
);
585 gt_pch_fixup_stringpool ();
587 XDELETE (state
.ptrs
);
588 XDELETE (this_object
);
593 /* Read the state of the compiler back in from F. */
596 gt_pch_restore (FILE *f
)
598 const struct ggc_root_tab
*const *rt
;
599 const struct ggc_root_tab
*rti
;
601 struct mmap_info mmi
;
604 /* Delete any deletable objects. This makes ggc_pch_read much
605 faster, as it can be sure that no GCable objects remain other
606 than the ones just read in. */
607 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
608 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
609 memset (rti
->base
, 0, rti
->stride
);
611 /* Read in all the scalar variables. */
612 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
613 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
614 if (fread (rti
->base
, rti
->stride
, 1, f
) != 1)
615 fatal_error (input_location
, "can%'t read PCH file: %m");
617 /* Read in all the global pointers, in 6 easy loops. */
618 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
619 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
620 for (i
= 0; i
< rti
->nelt
; i
++)
621 if (fread ((char *)rti
->base
+ rti
->stride
* i
,
622 sizeof (void *), 1, f
) != 1)
623 fatal_error (input_location
, "can%'t read PCH file: %m");
625 if (fread (&mmi
, sizeof (mmi
), 1, f
) != 1)
626 fatal_error (input_location
, "can%'t read PCH file: %m");
628 result
= host_hooks
.gt_pch_use_address (mmi
.preferred_base
, mmi
.size
,
629 fileno (f
), mmi
.offset
);
631 fatal_error (input_location
, "had to relocate PCH");
634 if (fseek (f
, mmi
.offset
, SEEK_SET
) != 0
635 || fread (mmi
.preferred_base
, mmi
.size
, 1, f
) != 1)
636 fatal_error (input_location
, "can%'t read PCH file: %m");
638 else if (fseek (f
, mmi
.offset
+ mmi
.size
, SEEK_SET
) != 0)
639 fatal_error (input_location
, "can%'t read PCH file: %m");
641 ggc_pch_read (f
, mmi
.preferred_base
);
643 gt_pch_restore_stringpool ();
646 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
647 Select no address whatsoever, and let gt_pch_save choose what it will with
648 malloc, presumably. */
651 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED
,
652 int fd ATTRIBUTE_UNUSED
)
657 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
658 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
659 same as base, indicating that the memory has been allocated but needs to
660 be read in from the file. Return -1 if the address differs, to relocation
661 of the PCH file would be required. */
664 default_gt_pch_use_address (void *base
, size_t size
, int fd ATTRIBUTE_UNUSED
,
665 size_t offset ATTRIBUTE_UNUSED
)
667 void *addr
= xmalloc (size
);
668 return (addr
== base
) - 1;
671 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the
672 alignment required for allocating virtual memory. Usually this is the
676 default_gt_pch_alloc_granularity (void)
678 return getpagesize ();
682 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
683 We temporarily allocate SIZE bytes, and let the kernel place the data
684 wherever it will. If it worked, that's our spot, if not we're likely
688 mmap_gt_pch_get_address (size_t size
, int fd
)
692 ret
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
693 if (ret
== (void *) MAP_FAILED
)
696 munmap ((caddr_t
) ret
, size
);
701 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
702 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
703 mapping the data at BASE, -1 if we couldn't.
705 This version assumes that the kernel honors the START operand of mmap
706 even without MAP_FIXED if START through START+SIZE are not currently
707 mapped with something. */
710 mmap_gt_pch_use_address (void *base
, size_t size
, int fd
, size_t offset
)
714 /* We're called with size == 0 if we're not planning to load a PCH
715 file at all. This allows the hook to free any static space that
716 we might have allocated at link time. */
720 addr
= mmap ((caddr_t
) base
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
723 return addr
== base
? 1 : -1;
725 #endif /* HAVE_MMAP_FILE */
727 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
729 /* Modify the bound based on rlimits. */
731 ggc_rlimit_bound (double limit
)
733 #if defined(HAVE_GETRLIMIT)
735 # if defined (RLIMIT_AS)
736 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably
737 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */
738 if (getrlimit (RLIMIT_AS
, &rlim
) == 0
739 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
740 && rlim
.rlim_cur
< limit
)
741 limit
= rlim
.rlim_cur
;
742 # elif defined (RLIMIT_DATA)
743 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
744 might be on an OS that has a broken mmap. (Others don't bound
745 mmap at all, apparently.) */
746 if (getrlimit (RLIMIT_DATA
, &rlim
) == 0
747 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
748 && rlim
.rlim_cur
< limit
749 /* Darwin has this horribly bogus default setting of
750 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA
751 appears to be ignored. Ignore such silliness. If a limit
752 this small was actually effective for mmap, GCC wouldn't even
754 && rlim
.rlim_cur
>= 8 * 1024 * 1024)
755 limit
= rlim
.rlim_cur
;
756 # endif /* RLIMIT_AS or RLIMIT_DATA */
757 #endif /* HAVE_GETRLIMIT */
762 /* Heuristic to set a default for GGC_MIN_EXPAND. */
764 ggc_min_expand_heuristic (void)
766 double min_expand
= physmem_total ();
768 /* Adjust for rlimits. */
769 min_expand
= ggc_rlimit_bound (min_expand
);
771 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
772 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
773 min_expand
/= 1024*1024*1024;
775 min_expand
= MIN (min_expand
, 70);
781 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
783 ggc_min_heapsize_heuristic (void)
785 double phys_kbytes
= physmem_total ();
786 double limit_kbytes
= ggc_rlimit_bound (phys_kbytes
* 2);
788 phys_kbytes
/= 1024; /* Convert to Kbytes. */
789 limit_kbytes
/= 1024;
791 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
792 bound of 128M (when RAM >= 1GB). */
795 #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
796 /* Try not to overrun the RSS limit while doing garbage collection.
797 The RSS limit is only advisory, so no margin is subtracted. */
800 if (getrlimit (RLIMIT_RSS
, &rlim
) == 0
801 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
)
802 phys_kbytes
= MIN (phys_kbytes
, rlim
.rlim_cur
/ 1024);
806 /* Don't blindly run over our data limit; do GC at least when the
807 *next* GC would be within 20Mb of the limit or within a quarter of
808 the limit, whichever is larger. If GCC does hit the data limit,
809 compilation will fail, so this tries to be conservative. */
810 limit_kbytes
= MAX (0, limit_kbytes
- MAX (limit_kbytes
/ 4, 20 * 1024));
811 limit_kbytes
= (limit_kbytes
* 100) / (110 + ggc_min_expand_heuristic ());
812 phys_kbytes
= MIN (phys_kbytes
, limit_kbytes
);
814 phys_kbytes
= MAX (phys_kbytes
, 4 * 1024);
815 phys_kbytes
= MIN (phys_kbytes
, 128 * 1024);
822 init_ggc_heuristics (void)
824 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
825 set_default_param_value (GGC_MIN_EXPAND
, ggc_min_expand_heuristic ());
826 set_default_param_value (GGC_MIN_HEAPSIZE
, ggc_min_heapsize_heuristic ());
830 /* GGC memory usage. */
831 struct ggc_usage
: public mem_usage
833 /* Default constructor. */
834 ggc_usage (): m_freed (0), m_collected (0), m_overhead (0) {}
836 ggc_usage (size_t allocated
, size_t times
, size_t peak
,
837 size_t freed
, size_t collected
, size_t overhead
)
838 : mem_usage (allocated
, times
, peak
),
839 m_freed (freed
), m_collected (collected
), m_overhead (overhead
) {}
841 /* Comparison operator. */
843 operator< (const ggc_usage
&second
) const
845 return (get_balance () == second
.get_balance () ?
846 (m_peak
== second
.m_peak
? m_times
< second
.m_times
847 : m_peak
< second
.m_peak
)
848 : get_balance () < second
.get_balance ());
851 /* Register overhead of ALLOCATED and OVERHEAD bytes. */
853 register_overhead (size_t allocated
, size_t overhead
)
855 m_allocated
+= allocated
;
856 m_overhead
+= overhead
;
860 /* Release overhead of SIZE bytes. */
862 release_overhead (size_t size
)
867 /* Sum the usage with SECOND usage. */
869 operator+ (const ggc_usage
&second
)
871 return ggc_usage (m_allocated
+ second
.m_allocated
,
872 m_times
+ second
.m_times
,
873 m_peak
+ second
.m_peak
,
874 m_freed
+ second
.m_freed
,
875 m_collected
+ second
.m_collected
,
876 m_overhead
+ second
.m_overhead
);
879 /* Dump usage with PREFIX, where TOTAL is sum of all rows. */
881 dump (const char *prefix
, ggc_usage
&total
) const
883 long balance
= get_balance ();
885 "%-48s %10li:%5.1f%%%10li:%5.1f%%"
886 "%10li:%5.1f%%%10li:%5.1f%%%10li\n",
887 prefix
, (long)m_collected
,
888 get_percent (m_collected
, total
.m_collected
),
889 (long)m_freed
, get_percent (m_freed
, total
.m_freed
),
890 (long)balance
, get_percent (balance
, total
.get_balance ()),
891 (long)m_overhead
, get_percent (m_overhead
, total
.m_overhead
),
895 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
897 dump (mem_location
*loc
, ggc_usage
&total
) const
899 char *location_string
= loc
->to_string ();
901 dump (location_string
, total
);
903 free (location_string
);
911 dump ("Total", *this);
915 /* Get balance which is GGC allocation leak. */
919 return m_allocated
+ m_overhead
- m_collected
- m_freed
;
922 typedef std::pair
<mem_location
*, ggc_usage
*> mem_pair_t
;
924 /* Compare wrapper used by qsort method. */
926 compare (const void *first
, const void *second
)
928 const mem_pair_t f
= *(const mem_pair_t
*)first
;
929 const mem_pair_t s
= *(const mem_pair_t
*)second
;
931 return (*f
.second
) < (*s
.second
);
934 /* Compare rows in final GGC summary dump. */
936 compare_final (const void *first
, const void *second
)
938 typedef std::pair
<mem_location
*, ggc_usage
*> mem_pair_t
;
940 const ggc_usage
*f
= ((const mem_pair_t
*)first
)->second
;
941 const ggc_usage
*s
= ((const mem_pair_t
*)second
)->second
;
943 size_t a
= f
->m_allocated
+ f
->m_overhead
- f
->m_freed
;
944 size_t b
= s
->m_allocated
+ s
->m_overhead
- s
->m_freed
;
946 return a
== b
? 0 : (a
< b
? 1 : -1);
949 /* Dump header with NAME. */
951 dump_header (const char *name
)
953 fprintf (stderr
, "%-48s %11s%17s%17s%16s%17s\n", name
, "Garbage", "Freed",
954 "Leak", "Overhead", "Times");
958 /* Freed memory in bytes. */
960 /* Collected memory in bytes. */
962 /* Overhead memory in bytes. */
966 /* GCC memory description. */
967 static mem_alloc_description
<ggc_usage
> ggc_mem_desc
;
969 /* Dump per-site memory statistics. */
972 dump_ggc_loc_statistics (bool final
)
974 if (! GATHER_STATISTICS
)
977 ggc_force_collect
= true;
980 ggc_mem_desc
.dump (GGC_ORIGIN
, final
? ggc_usage::compare_final
: NULL
);
982 ggc_force_collect
= false;
985 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
987 ggc_record_overhead (size_t allocated
, size_t overhead
, void *ptr MEM_STAT_DECL
)
989 ggc_usage
*usage
= ggc_mem_desc
.register_descriptor (ptr
, GGC_ORIGIN
, false
990 FINAL_PASS_MEM_STAT
);
992 ggc_mem_desc
.register_object_overhead (usage
, allocated
+ overhead
, ptr
);
993 usage
->register_overhead (allocated
, overhead
);
996 /* Notice that the pointer has been freed. */
998 ggc_free_overhead (void *ptr
)
1000 ggc_mem_desc
.release_object_overhead (ptr
);
1003 /* After live values has been marked, walk all recorded pointers and see if
1004 they are still live. */
1006 ggc_prune_overhead_list (void)
1008 typedef hash_map
<const void *, std::pair
<ggc_usage
*, size_t > > map_t
;
1010 map_t::iterator it
= ggc_mem_desc
.m_reverse_object_map
->begin ();
1012 for (; it
!= ggc_mem_desc
.m_reverse_object_map
->end (); ++it
)
1013 if (!ggc_marked_p ((*it
).first
))
1014 (*it
).second
.first
->m_collected
+= (*it
).second
.second
;
1016 delete ggc_mem_desc
.m_reverse_object_map
;
1017 ggc_mem_desc
.m_reverse_object_map
= new map_t (13, false, false);