1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999-2020 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. */
24 #define INCLUDE_MALLOC_H
26 #include "coretypes.h"
28 #include "diagnostic-core.h"
29 #include "ggc-internal.h"
30 #include "hosthooks.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
)
200 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED
,
201 ggc_statistics
*stats
)
203 /* Set the pointer so that during collection we will actually gather
207 /* Then do one collection to fill in the statistics. */
210 /* At present, we don't really gather any interesting statistics. */
212 /* Don't gather statistics any more. */
216 /* Functions for saving and restoring GCable memory to disk. */
221 void *note_ptr_cookie
;
222 gt_note_pointers note_ptr_fn
;
223 gt_handle_reorder reorder_fn
;
228 #define POINTER_HASH(x) (hashval_t)((intptr_t)x >> 3)
230 /* Helper for hashing saving_htab. */
232 struct saving_hasher
: free_ptr_hash
<ptr_data
>
234 typedef void *compare_type
;
235 static inline hashval_t
hash (const ptr_data
*);
236 static inline bool equal (const ptr_data
*, const void *);
240 saving_hasher::hash (const ptr_data
*p
)
242 return POINTER_HASH (p
->obj
);
246 saving_hasher::equal (const ptr_data
*p1
, const void *p2
)
248 return p1
->obj
== p2
;
251 static hash_table
<saving_hasher
> *saving_htab
;
253 /* Register an object in the hash table. */
256 gt_pch_note_object (void *obj
, void *note_ptr_cookie
,
257 gt_note_pointers note_ptr_fn
)
259 struct ptr_data
**slot
;
261 if (obj
== NULL
|| obj
== (void *) 1)
264 slot
= (struct ptr_data
**)
265 saving_htab
->find_slot_with_hash (obj
, POINTER_HASH (obj
), INSERT
);
268 gcc_assert ((*slot
)->note_ptr_fn
== note_ptr_fn
269 && (*slot
)->note_ptr_cookie
== note_ptr_cookie
);
273 *slot
= XCNEW (struct ptr_data
);
275 (*slot
)->note_ptr_fn
= note_ptr_fn
;
276 (*slot
)->note_ptr_cookie
= note_ptr_cookie
;
277 if (note_ptr_fn
== gt_pch_p_S
)
278 (*slot
)->size
= strlen ((const char *)obj
) + 1;
280 (*slot
)->size
= ggc_get_size (obj
);
284 /* Register an object in the hash table. */
287 gt_pch_note_reorder (void *obj
, void *note_ptr_cookie
,
288 gt_handle_reorder reorder_fn
)
290 struct ptr_data
*data
;
292 if (obj
== NULL
|| obj
== (void *) 1)
295 data
= (struct ptr_data
*)
296 saving_htab
->find_with_hash (obj
, POINTER_HASH (obj
));
297 gcc_assert (data
&& data
->note_ptr_cookie
== note_ptr_cookie
);
299 data
->reorder_fn
= reorder_fn
;
302 /* Handy state for the traversal functions. */
304 struct traversal_state
307 struct ggc_pch_data
*d
;
309 struct ptr_data
**ptrs
;
313 /* Callbacks for htab_traverse. */
316 ggc_call_count (ptr_data
**slot
, traversal_state
*state
)
318 struct ptr_data
*d
= *slot
;
320 ggc_pch_count_object (state
->d
, d
->obj
, d
->size
,
321 d
->note_ptr_fn
== gt_pch_p_S
);
327 ggc_call_alloc (ptr_data
**slot
, traversal_state
*state
)
329 struct ptr_data
*d
= *slot
;
331 d
->new_addr
= ggc_pch_alloc_object (state
->d
, d
->obj
, d
->size
,
332 d
->note_ptr_fn
== gt_pch_p_S
);
333 state
->ptrs
[state
->ptrs_i
++] = d
;
337 /* Callback for qsort. */
340 compare_ptr_data (const void *p1_p
, const void *p2_p
)
342 const struct ptr_data
*const p1
= *(const struct ptr_data
*const *)p1_p
;
343 const struct ptr_data
*const p2
= *(const struct ptr_data
*const *)p2_p
;
344 return (((size_t)p1
->new_addr
> (size_t)p2
->new_addr
)
345 - ((size_t)p1
->new_addr
< (size_t)p2
->new_addr
));
348 /* Callbacks for note_ptr_fn. */
351 relocate_ptrs (void *ptr_p
, void *state_p
)
353 void **ptr
= (void **)ptr_p
;
354 struct traversal_state
*state ATTRIBUTE_UNUSED
355 = (struct traversal_state
*)state_p
;
356 struct ptr_data
*result
;
358 if (*ptr
== NULL
|| *ptr
== (void *)1)
361 result
= (struct ptr_data
*)
362 saving_htab
->find_with_hash (*ptr
, POINTER_HASH (*ptr
));
364 *ptr
= result
->new_addr
;
367 /* Write out, after relocation, the pointers in TAB. */
369 write_pch_globals (const struct ggc_root_tab
* const *tab
,
370 struct traversal_state
*state
)
372 const struct ggc_root_tab
*const *rt
;
373 const struct ggc_root_tab
*rti
;
376 for (rt
= tab
; *rt
; rt
++)
377 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
378 for (i
= 0; i
< rti
->nelt
; i
++)
380 void *ptr
= *(void **)((char *)rti
->base
+ rti
->stride
* i
);
381 struct ptr_data
*new_ptr
;
382 if (ptr
== NULL
|| ptr
== (void *)1)
384 if (fwrite (&ptr
, sizeof (void *), 1, state
->f
)
386 fatal_error (input_location
, "cannot write PCH file: %m");
390 new_ptr
= (struct ptr_data
*)
391 saving_htab
->find_with_hash (ptr
, POINTER_HASH (ptr
));
392 if (fwrite (&new_ptr
->new_addr
, sizeof (void *), 1, state
->f
)
394 fatal_error (input_location
, "cannot write PCH file: %m");
399 /* Hold the information we need to mmap the file back in. */
405 void *preferred_base
;
408 /* Write out the state of the compiler to F. */
411 gt_pch_save (FILE *f
)
413 const struct ggc_root_tab
*const *rt
;
414 const struct ggc_root_tab
*rti
;
416 struct traversal_state state
;
417 char *this_object
= NULL
;
418 size_t this_object_size
= 0;
419 struct mmap_info mmi
;
420 const size_t mmap_offset_alignment
= host_hooks
.gt_pch_alloc_granularity ();
422 gt_pch_save_stringpool ();
424 timevar_push (TV_PCH_PTR_REALLOC
);
425 saving_htab
= new hash_table
<saving_hasher
> (50000);
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 /* Prepare the objects for writing, determine addresses and such. */
434 state
.d
= init_ggc_pch ();
436 saving_htab
->traverse
<traversal_state
*, ggc_call_count
> (&state
);
438 mmi
.size
= ggc_pch_total_size (state
.d
);
440 /* Try to arrange things so that no relocation is necessary, but
441 don't try very hard. On most platforms, this will always work,
442 and on the rest it's a lot of work to do better.
443 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
444 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */
445 mmi
.preferred_base
= host_hooks
.gt_pch_get_address (mmi
.size
, fileno (f
));
447 ggc_pch_this_base (state
.d
, mmi
.preferred_base
);
449 state
.ptrs
= XNEWVEC (struct ptr_data
*, state
.count
);
452 saving_htab
->traverse
<traversal_state
*, ggc_call_alloc
> (&state
);
453 timevar_pop (TV_PCH_PTR_REALLOC
);
455 timevar_push (TV_PCH_PTR_SORT
);
456 qsort (state
.ptrs
, state
.count
, sizeof (*state
.ptrs
), compare_ptr_data
);
457 timevar_pop (TV_PCH_PTR_SORT
);
459 /* Write out all the scalar variables. */
460 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
461 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
462 if (fwrite (rti
->base
, rti
->stride
, 1, f
) != 1)
463 fatal_error (input_location
, "cannot write PCH file: %m");
465 /* Write out all the global pointers, after translation. */
466 write_pch_globals (gt_ggc_rtab
, &state
);
468 /* Pad the PCH file so that the mmapped area starts on an allocation
469 granularity (usually page) boundary. */
472 o
= ftell (state
.f
) + sizeof (mmi
);
474 fatal_error (input_location
, "cannot get position in PCH file: %m");
475 mmi
.offset
= mmap_offset_alignment
- o
% mmap_offset_alignment
;
476 if (mmi
.offset
== mmap_offset_alignment
)
480 if (fwrite (&mmi
, sizeof (mmi
), 1, state
.f
) != 1)
481 fatal_error (input_location
, "cannot write PCH file: %m");
483 && fseek (state
.f
, mmi
.offset
, SEEK_SET
) != 0)
484 fatal_error (input_location
, "cannot write padding to PCH file: %m");
486 ggc_pch_prepare_write (state
.d
, state
.f
);
488 #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
489 vec
<char> vbits
= vNULL
;
492 /* Actually write out the objects. */
493 for (i
= 0; i
< state
.count
; i
++)
495 if (this_object_size
< state
.ptrs
[i
]->size
)
497 this_object_size
= state
.ptrs
[i
]->size
;
498 this_object
= XRESIZEVAR (char, this_object
, this_object_size
);
500 #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
501 /* obj might contain uninitialized bytes, e.g. in the trailing
502 padding of the object. Avoid warnings by making the memory
503 temporarily defined and then restoring previous state. */
505 size_t valid_size
= state
.ptrs
[i
]->size
;
506 if (__builtin_expect (RUNNING_ON_VALGRIND
, 0))
508 if (vbits
.length () < valid_size
)
509 vbits
.safe_grow (valid_size
, true);
510 get_vbits
= VALGRIND_GET_VBITS (state
.ptrs
[i
]->obj
,
511 vbits
.address (), valid_size
);
514 /* We assume that first part of obj is addressable, and
515 the rest is unaddressable. Find out where the boundary is
516 using binary search. */
517 size_t lo
= 0, hi
= valid_size
;
520 size_t mid
= (lo
+ hi
) / 2;
521 get_vbits
= VALGRIND_GET_VBITS ((char *) state
.ptrs
[i
]->obj
522 + mid
, vbits
.address (),
526 else if (get_vbits
== 1)
531 if (get_vbits
== 1 || get_vbits
== 3)
534 get_vbits
= VALGRIND_GET_VBITS (state
.ptrs
[i
]->obj
,
540 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (state
.ptrs
[i
]->obj
,
541 state
.ptrs
[i
]->size
));
544 memcpy (this_object
, state
.ptrs
[i
]->obj
, state
.ptrs
[i
]->size
);
545 if (state
.ptrs
[i
]->reorder_fn
!= NULL
)
546 state
.ptrs
[i
]->reorder_fn (state
.ptrs
[i
]->obj
,
547 state
.ptrs
[i
]->note_ptr_cookie
,
548 relocate_ptrs
, &state
);
549 state
.ptrs
[i
]->note_ptr_fn (state
.ptrs
[i
]->obj
,
550 state
.ptrs
[i
]->note_ptr_cookie
,
551 relocate_ptrs
, &state
);
552 ggc_pch_write_object (state
.d
, state
.f
, state
.ptrs
[i
]->obj
,
553 state
.ptrs
[i
]->new_addr
, state
.ptrs
[i
]->size
,
554 state
.ptrs
[i
]->note_ptr_fn
== gt_pch_p_S
);
555 if (state
.ptrs
[i
]->note_ptr_fn
!= gt_pch_p_S
)
556 memcpy (state
.ptrs
[i
]->obj
, this_object
, state
.ptrs
[i
]->size
);
557 #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
558 if (__builtin_expect (get_vbits
== 1, 0))
560 (void) VALGRIND_SET_VBITS (state
.ptrs
[i
]->obj
, vbits
.address (),
562 if (valid_size
!= state
.ptrs
[i
]->size
)
563 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *)
571 #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
575 ggc_pch_finish (state
.d
, state
.f
);
576 gt_pch_fixup_stringpool ();
578 XDELETE (state
.ptrs
);
579 XDELETE (this_object
);
584 /* Read the state of the compiler back in from F. */
587 gt_pch_restore (FILE *f
)
589 const struct ggc_root_tab
*const *rt
;
590 const struct ggc_root_tab
*rti
;
592 struct mmap_info mmi
;
595 /* Delete any deletable objects. This makes ggc_pch_read much
596 faster, as it can be sure that no GCable objects remain other
597 than the ones just read in. */
598 for (rt
= gt_ggc_deletable_rtab
; *rt
; rt
++)
599 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
600 memset (rti
->base
, 0, rti
->stride
);
602 /* Read in all the scalar variables. */
603 for (rt
= gt_pch_scalar_rtab
; *rt
; rt
++)
604 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
605 if (fread (rti
->base
, rti
->stride
, 1, f
) != 1)
606 fatal_error (input_location
, "cannot read PCH file: %m");
608 /* Read in all the global pointers, in 6 easy loops. */
609 for (rt
= gt_ggc_rtab
; *rt
; rt
++)
610 for (rti
= *rt
; rti
->base
!= NULL
; rti
++)
611 for (i
= 0; i
< rti
->nelt
; i
++)
612 if (fread ((char *)rti
->base
+ rti
->stride
* i
,
613 sizeof (void *), 1, f
) != 1)
614 fatal_error (input_location
, "cannot read PCH file: %m");
616 if (fread (&mmi
, sizeof (mmi
), 1, f
) != 1)
617 fatal_error (input_location
, "cannot read PCH file: %m");
619 result
= host_hooks
.gt_pch_use_address (mmi
.preferred_base
, mmi
.size
,
620 fileno (f
), mmi
.offset
);
622 fatal_error (input_location
, "had to relocate PCH");
625 if (fseek (f
, mmi
.offset
, SEEK_SET
) != 0
626 || fread (mmi
.preferred_base
, mmi
.size
, 1, f
) != 1)
627 fatal_error (input_location
, "cannot read PCH file: %m");
629 else if (fseek (f
, mmi
.offset
+ mmi
.size
, SEEK_SET
) != 0)
630 fatal_error (input_location
, "cannot read PCH file: %m");
632 ggc_pch_read (f
, mmi
.preferred_base
);
634 gt_pch_restore_stringpool ();
637 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
638 Select no address whatsoever, and let gt_pch_save choose what it will with
639 malloc, presumably. */
642 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED
,
643 int fd ATTRIBUTE_UNUSED
)
648 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
649 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
650 same as base, indicating that the memory has been allocated but needs to
651 be read in from the file. Return -1 if the address differs, to relocation
652 of the PCH file would be required. */
655 default_gt_pch_use_address (void *base
, size_t size
, int fd ATTRIBUTE_UNUSED
,
656 size_t offset ATTRIBUTE_UNUSED
)
658 void *addr
= xmalloc (size
);
659 return (addr
== base
) - 1;
662 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the
663 alignment required for allocating virtual memory. Usually this is the
667 default_gt_pch_alloc_granularity (void)
669 return getpagesize ();
673 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
674 We temporarily allocate SIZE bytes, and let the kernel place the data
675 wherever it will. If it worked, that's our spot, if not we're likely
679 mmap_gt_pch_get_address (size_t size
, int fd
)
683 ret
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
684 if (ret
== (void *) MAP_FAILED
)
687 munmap ((caddr_t
) ret
, size
);
692 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
693 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
694 mapping the data at BASE, -1 if we couldn't.
696 This version assumes that the kernel honors the START operand of mmap
697 even without MAP_FIXED if START through START+SIZE are not currently
698 mapped with something. */
701 mmap_gt_pch_use_address (void *base
, size_t size
, int fd
, size_t offset
)
705 /* We're called with size == 0 if we're not planning to load a PCH
706 file at all. This allows the hook to free any static space that
707 we might have allocated at link time. */
711 addr
= mmap ((caddr_t
) base
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
714 return addr
== base
? 1 : -1;
716 #endif /* HAVE_MMAP_FILE */
718 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
720 /* Modify the bound based on rlimits. */
722 ggc_rlimit_bound (double limit
)
724 #if defined(HAVE_GETRLIMIT)
726 # if defined (RLIMIT_AS)
727 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably
728 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */
729 if (getrlimit (RLIMIT_AS
, &rlim
) == 0
730 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
731 && rlim
.rlim_cur
< limit
)
732 limit
= rlim
.rlim_cur
;
733 # elif defined (RLIMIT_DATA)
734 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
735 might be on an OS that has a broken mmap. (Others don't bound
736 mmap at all, apparently.) */
737 if (getrlimit (RLIMIT_DATA
, &rlim
) == 0
738 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
739 && rlim
.rlim_cur
< limit
740 /* Darwin has this horribly bogus default setting of
741 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA
742 appears to be ignored. Ignore such silliness. If a limit
743 this small was actually effective for mmap, GCC wouldn't even
745 && rlim
.rlim_cur
>= 8 * ONE_M
)
746 limit
= rlim
.rlim_cur
;
747 # endif /* RLIMIT_AS or RLIMIT_DATA */
748 #endif /* HAVE_GETRLIMIT */
753 /* Heuristic to set a default for GGC_MIN_EXPAND. */
755 ggc_min_expand_heuristic (void)
757 double min_expand
= physmem_total ();
759 /* Adjust for rlimits. */
760 min_expand
= ggc_rlimit_bound (min_expand
);
762 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
763 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
766 min_expand
= MIN (min_expand
, 70);
772 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
774 ggc_min_heapsize_heuristic (void)
776 double phys_kbytes
= physmem_total ();
777 double limit_kbytes
= ggc_rlimit_bound (phys_kbytes
* 2);
779 phys_kbytes
/= ONE_K
; /* Convert to Kbytes. */
780 limit_kbytes
/= ONE_K
;
782 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
783 bound of 128M (when RAM >= 1GB). */
786 #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
787 /* Try not to overrun the RSS limit while doing garbage collection.
788 The RSS limit is only advisory, so no margin is subtracted. */
791 if (getrlimit (RLIMIT_RSS
, &rlim
) == 0
792 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
)
793 phys_kbytes
= MIN (phys_kbytes
, rlim
.rlim_cur
/ ONE_K
);
797 /* Don't blindly run over our data limit; do GC at least when the
798 *next* GC would be within 20Mb of the limit or within a quarter of
799 the limit, whichever is larger. If GCC does hit the data limit,
800 compilation will fail, so this tries to be conservative. */
801 limit_kbytes
= MAX (0, limit_kbytes
- MAX (limit_kbytes
/ 4, 20 * ONE_K
));
802 limit_kbytes
= (limit_kbytes
* 100) / (110 + ggc_min_expand_heuristic ());
803 phys_kbytes
= MIN (phys_kbytes
, limit_kbytes
);
805 phys_kbytes
= MAX (phys_kbytes
, 4 * ONE_K
);
806 phys_kbytes
= MIN (phys_kbytes
, 128 * ONE_K
);
813 init_ggc_heuristics (void)
815 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
816 param_ggc_min_expand
= ggc_min_expand_heuristic ();
817 param_ggc_min_heapsize
= ggc_min_heapsize_heuristic ();
821 /* GGC memory usage. */
822 class ggc_usage
: public mem_usage
825 /* Default constructor. */
826 ggc_usage (): m_freed (0), m_collected (0), m_overhead (0) {}
828 ggc_usage (size_t allocated
, size_t times
, size_t peak
,
829 size_t freed
, size_t collected
, size_t overhead
)
830 : mem_usage (allocated
, times
, peak
),
831 m_freed (freed
), m_collected (collected
), m_overhead (overhead
) {}
833 /* Equality operator. */
835 operator== (const ggc_usage
&second
) const
837 return (get_balance () == second
.get_balance ()
838 && m_peak
== second
.m_peak
839 && m_times
== second
.m_times
);
842 /* Comparison operator. */
844 operator< (const ggc_usage
&second
) const
849 return (get_balance () == second
.get_balance () ?
850 (m_peak
== second
.m_peak
? m_times
< second
.m_times
851 : m_peak
< second
.m_peak
)
852 : get_balance () < second
.get_balance ());
855 /* Register overhead of ALLOCATED and OVERHEAD bytes. */
857 register_overhead (size_t allocated
, size_t overhead
)
859 m_allocated
+= allocated
;
860 m_overhead
+= overhead
;
864 /* Release overhead of SIZE bytes. */
866 release_overhead (size_t size
)
871 /* Sum the usage with SECOND usage. */
873 operator+ (const ggc_usage
&second
)
875 return ggc_usage (m_allocated
+ second
.m_allocated
,
876 m_times
+ second
.m_times
,
877 m_peak
+ second
.m_peak
,
878 m_freed
+ second
.m_freed
,
879 m_collected
+ second
.m_collected
,
880 m_overhead
+ second
.m_overhead
);
883 /* Dump usage with PREFIX, where TOTAL is sum of all rows. */
885 dump (const char *prefix
, ggc_usage
&total
) const
887 size_t balance
= get_balance ();
889 "%-48s " PRsa (9) ":%5.1f%%" PRsa (9) ":%5.1f%%"
890 PRsa (9) ":%5.1f%%" PRsa (9) ":%5.1f%%" PRsa (9) "\n",
892 SIZE_AMOUNT (balance
), get_percent (balance
, total
.get_balance ()),
893 SIZE_AMOUNT (m_collected
),
894 get_percent (m_collected
, total
.m_collected
),
895 SIZE_AMOUNT (m_freed
), get_percent (m_freed
, total
.m_freed
),
896 SIZE_AMOUNT (m_overhead
),
897 get_percent (m_overhead
, total
.m_overhead
),
898 SIZE_AMOUNT (m_times
));
901 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
903 dump (mem_location
*loc
, ggc_usage
&total
) const
905 char *location_string
= loc
->to_string ();
907 dump (location_string
, total
);
909 free (location_string
);
916 dump ("Total", *this);
919 /* Get balance which is GGC allocation leak. */
923 return m_allocated
+ m_overhead
- m_collected
- m_freed
;
926 typedef std::pair
<mem_location
*, ggc_usage
*> mem_pair_t
;
928 /* Compare wrapper used by qsort method. */
930 compare (const void *first
, const void *second
)
932 const mem_pair_t mem1
= *(const mem_pair_t
*) first
;
933 const mem_pair_t mem2
= *(const mem_pair_t
*) second
;
935 size_t balance1
= mem1
.second
->get_balance ();
936 size_t balance2
= mem2
.second
->get_balance ();
938 return balance1
== balance2
? 0 : (balance1
< balance2
? 1 : -1);
941 /* Dump header with NAME. */
943 dump_header (const char *name
)
945 fprintf (stderr
, "%-48s %11s%17s%17s%16s%17s\n", name
, "Leak", "Garbage",
946 "Freed", "Overhead", "Times");
949 /* Freed memory in bytes. */
951 /* Collected memory in bytes. */
953 /* Overhead memory in bytes. */
957 /* GCC memory description. */
958 static mem_alloc_description
<ggc_usage
> ggc_mem_desc
;
960 /* Dump per-site memory statistics. */
963 dump_ggc_loc_statistics ()
965 if (! GATHER_STATISTICS
)
968 ggc_force_collect
= true;
971 ggc_mem_desc
.dump (GGC_ORIGIN
);
973 ggc_force_collect
= false;
976 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
978 ggc_record_overhead (size_t allocated
, size_t overhead
, void *ptr MEM_STAT_DECL
)
980 ggc_usage
*usage
= ggc_mem_desc
.register_descriptor (ptr
, GGC_ORIGIN
, false
981 FINAL_PASS_MEM_STAT
);
983 ggc_mem_desc
.register_object_overhead (usage
, allocated
+ overhead
, ptr
);
984 usage
->register_overhead (allocated
, overhead
);
987 /* Notice that the pointer has been freed. */
989 ggc_free_overhead (void *ptr
)
991 ggc_mem_desc
.release_object_overhead (ptr
);
994 /* After live values has been marked, walk all recorded pointers and see if
995 they are still live. */
997 ggc_prune_overhead_list (void)
999 typedef hash_map
<const void *, std::pair
<ggc_usage
*, size_t > > map_t
;
1001 map_t::iterator it
= ggc_mem_desc
.m_reverse_object_map
->begin ();
1003 for (; it
!= ggc_mem_desc
.m_reverse_object_map
->end (); ++it
)
1004 if (!ggc_marked_p ((*it
).first
))
1006 (*it
).second
.first
->m_collected
+= (*it
).second
.second
;
1007 ggc_mem_desc
.m_reverse_object_map
->remove ((*it
).first
);
1011 /* Print memory used by heap if this info is available. */
1014 report_heap_memory_use ()
1016 #if defined(HAVE_MALLINFO) || defined(HAVE_MALLINFO2)
1017 #ifdef HAVE_MALLINFO2
1018 #define MALLINFO_FN mallinfo2
1020 #define MALLINFO_FN mallinfo
1023 fprintf (stderr
, " {heap " PRsa (0) "}",
1024 SIZE_AMOUNT (MALLINFO_FN ().arena
));