2018-11-11 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ggc-common.c
blob9fdba23ce4c268957957dfae55d25779156c1778
1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999-2018 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
9 version.
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
14 for more details.
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. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "timevar.h"
27 #include "diagnostic-core.h"
28 #include "ggc-internal.h"
29 #include "params.h"
30 #include "hosthooks.h"
31 #include "plugin.h"
33 /* When set, ggc_collect will do collection. */
34 bool ggc_force_collect;
36 /* When true, protect the contents of the identifier hash table. */
37 bool ggc_protect_identifiers = true;
39 /* Statistics about the allocation. */
40 static ggc_statistics *ggc_stats;
42 struct traversal_state;
44 static int compare_ptr_data (const void *, const void *);
45 static void relocate_ptrs (void *, void *);
46 static void write_pch_globals (const struct ggc_root_tab * const *tab,
47 struct traversal_state *state);
49 /* Maintain global roots that are preserved during GC. */
51 /* This extra vector of dynamically registered root_tab-s is used by
52 ggc_mark_roots and gives the ability to dynamically add new GGC root
53 tables, for instance from some plugins; this vector is on the heap
54 since it is used by GGC internally. */
55 typedef const struct ggc_root_tab *const_ggc_root_tab_t;
56 static vec<const_ggc_root_tab_t> extra_root_vec;
58 /* Dynamically register a new GGC root table RT. This is useful for
59 plugins. */
61 void
62 ggc_register_root_tab (const struct ggc_root_tab* rt)
64 if (rt)
65 extra_root_vec.safe_push (rt);
68 /* Mark all the roots in the table RT. */
70 static void
71 ggc_mark_root_tab (const_ggc_root_tab_t rt)
73 size_t i;
75 for ( ; rt->base != NULL; rt++)
76 for (i = 0; i < rt->nelt; i++)
77 (*rt->cb) (*(void **) ((char *)rt->base + rt->stride * i));
80 /* Iterate through all registered roots and mark each element. */
82 void
83 ggc_mark_roots (void)
85 const struct ggc_root_tab *const *rt;
86 const_ggc_root_tab_t rtp, rti;
87 size_t i;
89 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
90 for (rti = *rt; rti->base != NULL; rti++)
91 memset (rti->base, 0, rti->stride);
93 for (rt = gt_ggc_rtab; *rt; rt++)
94 ggc_mark_root_tab (*rt);
96 FOR_EACH_VEC_ELT (extra_root_vec, i, rtp)
97 ggc_mark_root_tab (rtp);
99 if (ggc_protect_identifiers)
100 ggc_mark_stringpool ();
102 gt_clear_caches ();
104 if (! ggc_protect_identifiers)
105 ggc_purge_stringpool ();
107 /* Some plugins may call ggc_set_mark from here. */
108 invoke_plugin_callbacks (PLUGIN_GGC_MARKING, NULL);
111 /* Allocate a block of memory, then clear it. */
112 void *
113 ggc_internal_cleared_alloc (size_t size, void (*f)(void *), size_t s, size_t n
114 MEM_STAT_DECL)
116 void *buf = ggc_internal_alloc (size, f, s, n PASS_MEM_STAT);
117 memset (buf, 0, size);
118 return buf;
121 /* Resize a block of memory, possibly re-allocating it. */
122 void *
123 ggc_realloc (void *x, size_t size MEM_STAT_DECL)
125 void *r;
126 size_t old_size;
128 if (x == NULL)
129 return ggc_internal_alloc (size PASS_MEM_STAT);
131 old_size = ggc_get_size (x);
133 if (size <= old_size)
135 /* Mark the unwanted memory as unaccessible. We also need to make
136 the "new" size accessible, since ggc_get_size returns the size of
137 the pool, not the size of the individually allocated object, the
138 size which was previously made accessible. Unfortunately, we
139 don't know that previously allocated size. Without that
140 knowledge we have to lose some initialization-tracking for the
141 old parts of the object. An alternative is to mark the whole
142 old_size as reachable, but that would lose tracking of writes
143 after the end of the object (by small offsets). Discard the
144 handle to avoid handle leak. */
145 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x + size,
146 old_size - size));
147 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, size));
148 return x;
151 r = ggc_internal_alloc (size PASS_MEM_STAT);
153 /* Since ggc_get_size returns the size of the pool, not the size of the
154 individually allocated object, we'd access parts of the old object
155 that were marked invalid with the memcpy below. We lose a bit of the
156 initialization-tracking since some of it may be uninitialized. */
157 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, old_size));
159 memcpy (r, x, old_size);
161 /* The old object is not supposed to be used anymore. */
162 ggc_free (x);
164 return r;
167 void *
168 ggc_cleared_alloc_htab_ignore_args (size_t c ATTRIBUTE_UNUSED,
169 size_t n ATTRIBUTE_UNUSED)
171 gcc_assert (c * n == sizeof (struct htab));
172 return ggc_cleared_alloc<htab> ();
175 /* TODO: once we actually use type information in GGC, create a new tag
176 gt_gcc_ptr_array and use it for pointer arrays. */
177 void *
178 ggc_cleared_alloc_ptr_array_two_args (size_t c, size_t n)
180 gcc_assert (sizeof (PTR *) == n);
181 return ggc_cleared_vec_alloc<PTR *> (c);
184 /* These are for splay_tree_new_ggc. */
185 void *
186 ggc_splay_alloc (int sz, void *nl)
188 gcc_assert (!nl);
189 return ggc_internal_alloc (sz);
192 void
193 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl)
195 gcc_assert (!nl);
198 void
199 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED,
200 ggc_statistics *stats)
202 /* Set the pointer so that during collection we will actually gather
203 the statistics. */
204 ggc_stats = stats;
206 /* Then do one collection to fill in the statistics. */
207 ggc_collect ();
209 /* At present, we don't really gather any interesting statistics. */
211 /* Don't gather statistics any more. */
212 ggc_stats = NULL;
215 /* Functions for saving and restoring GCable memory to disk. */
217 struct ptr_data
219 void *obj;
220 void *note_ptr_cookie;
221 gt_note_pointers note_ptr_fn;
222 gt_handle_reorder reorder_fn;
223 size_t size;
224 void *new_addr;
227 #define POINTER_HASH(x) (hashval_t)((intptr_t)x >> 3)
229 /* Helper for hashing saving_htab. */
231 struct saving_hasher : free_ptr_hash <ptr_data>
233 typedef void *compare_type;
234 static inline hashval_t hash (const ptr_data *);
235 static inline bool equal (const ptr_data *, const void *);
238 inline hashval_t
239 saving_hasher::hash (const ptr_data *p)
241 return POINTER_HASH (p->obj);
244 inline bool
245 saving_hasher::equal (const ptr_data *p1, const void *p2)
247 return p1->obj == p2;
250 static hash_table<saving_hasher> *saving_htab;
252 /* Register an object in the hash table. */
255 gt_pch_note_object (void *obj, void *note_ptr_cookie,
256 gt_note_pointers note_ptr_fn)
258 struct ptr_data **slot;
260 if (obj == NULL || obj == (void *) 1)
261 return 0;
263 slot = (struct ptr_data **)
264 saving_htab->find_slot_with_hash (obj, POINTER_HASH (obj), INSERT);
265 if (*slot != NULL)
267 gcc_assert ((*slot)->note_ptr_fn == note_ptr_fn
268 && (*slot)->note_ptr_cookie == note_ptr_cookie);
269 return 0;
272 *slot = XCNEW (struct ptr_data);
273 (*slot)->obj = obj;
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 ((const char *)obj) + 1;
278 else
279 (*slot)->size = ggc_get_size (obj);
280 return 1;
283 /* Register an object in the hash table. */
285 void
286 gt_pch_note_reorder (void *obj, void *note_ptr_cookie,
287 gt_handle_reorder reorder_fn)
289 struct ptr_data *data;
291 if (obj == NULL || obj == (void *) 1)
292 return;
294 data = (struct ptr_data *)
295 saving_htab->find_with_hash (obj, POINTER_HASH (obj));
296 gcc_assert (data && data->note_ptr_cookie == note_ptr_cookie);
298 data->reorder_fn = reorder_fn;
301 /* Handy state for the traversal functions. */
303 struct traversal_state
305 FILE *f;
306 struct ggc_pch_data *d;
307 size_t count;
308 struct ptr_data **ptrs;
309 size_t ptrs_i;
312 /* Callbacks for htab_traverse. */
315 ggc_call_count (ptr_data **slot, traversal_state *state)
317 struct ptr_data *d = *slot;
319 ggc_pch_count_object (state->d, d->obj, d->size,
320 d->note_ptr_fn == gt_pch_p_S);
321 state->count++;
322 return 1;
326 ggc_call_alloc (ptr_data **slot, traversal_state *state)
328 struct ptr_data *d = *slot;
330 d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size,
331 d->note_ptr_fn == gt_pch_p_S);
332 state->ptrs[state->ptrs_i++] = d;
333 return 1;
336 /* Callback for qsort. */
338 static int
339 compare_ptr_data (const void *p1_p, const void *p2_p)
341 const struct ptr_data *const p1 = *(const struct ptr_data *const *)p1_p;
342 const struct ptr_data *const p2 = *(const struct ptr_data *const *)p2_p;
343 return (((size_t)p1->new_addr > (size_t)p2->new_addr)
344 - ((size_t)p1->new_addr < (size_t)p2->new_addr));
347 /* Callbacks for note_ptr_fn. */
349 static void
350 relocate_ptrs (void *ptr_p, void *state_p)
352 void **ptr = (void **)ptr_p;
353 struct traversal_state *state ATTRIBUTE_UNUSED
354 = (struct traversal_state *)state_p;
355 struct ptr_data *result;
357 if (*ptr == NULL || *ptr == (void *)1)
358 return;
360 result = (struct ptr_data *)
361 saving_htab->find_with_hash (*ptr, POINTER_HASH (*ptr));
362 gcc_assert (result);
363 *ptr = result->new_addr;
366 /* Write out, after relocation, the pointers in TAB. */
367 static void
368 write_pch_globals (const struct ggc_root_tab * const *tab,
369 struct traversal_state *state)
371 const struct ggc_root_tab *const *rt;
372 const struct ggc_root_tab *rti;
373 size_t i;
375 for (rt = tab; *rt; rt++)
376 for (rti = *rt; rti->base != NULL; rti++)
377 for (i = 0; i < rti->nelt; i++)
379 void *ptr = *(void **)((char *)rti->base + rti->stride * i);
380 struct ptr_data *new_ptr;
381 if (ptr == NULL || ptr == (void *)1)
383 if (fwrite (&ptr, sizeof (void *), 1, state->f)
384 != 1)
385 fatal_error (input_location, "can%'t write PCH file: %m");
387 else
389 new_ptr = (struct ptr_data *)
390 saving_htab->find_with_hash (ptr, POINTER_HASH (ptr));
391 if (fwrite (&new_ptr->new_addr, sizeof (void *), 1, state->f)
392 != 1)
393 fatal_error (input_location, "can%'t write PCH file: %m");
398 /* Hold the information we need to mmap the file back in. */
400 struct mmap_info
402 size_t offset;
403 size_t size;
404 void *preferred_base;
407 /* Write out the state of the compiler to F. */
409 void
410 gt_pch_save (FILE *f)
412 const struct ggc_root_tab *const *rt;
413 const struct ggc_root_tab *rti;
414 size_t i;
415 struct traversal_state state;
416 char *this_object = NULL;
417 size_t this_object_size = 0;
418 struct mmap_info mmi;
419 const size_t mmap_offset_alignment = host_hooks.gt_pch_alloc_granularity ();
421 gt_pch_save_stringpool ();
423 timevar_push (TV_PCH_PTR_REALLOC);
424 saving_htab = new hash_table<saving_hasher> (50000);
426 for (rt = gt_ggc_rtab; *rt; rt++)
427 for (rti = *rt; rti->base != NULL; rti++)
428 for (i = 0; i < rti->nelt; i++)
429 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
431 /* Prepare the objects for writing, determine addresses and such. */
432 state.f = f;
433 state.d = init_ggc_pch ();
434 state.count = 0;
435 saving_htab->traverse <traversal_state *, ggc_call_count> (&state);
437 mmi.size = ggc_pch_total_size (state.d);
439 /* Try to arrange things so that no relocation is necessary, but
440 don't try very hard. On most platforms, this will always work,
441 and on the rest it's a lot of work to do better.
442 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
443 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */
444 mmi.preferred_base = host_hooks.gt_pch_get_address (mmi.size, fileno (f));
446 ggc_pch_this_base (state.d, mmi.preferred_base);
448 state.ptrs = XNEWVEC (struct ptr_data *, state.count);
449 state.ptrs_i = 0;
451 saving_htab->traverse <traversal_state *, ggc_call_alloc> (&state);
452 timevar_pop (TV_PCH_PTR_REALLOC);
454 timevar_push (TV_PCH_PTR_SORT);
455 qsort (state.ptrs, state.count, sizeof (*state.ptrs), compare_ptr_data);
456 timevar_pop (TV_PCH_PTR_SORT);
458 /* Write out all the scalar variables. */
459 for (rt = gt_pch_scalar_rtab; *rt; rt++)
460 for (rti = *rt; rti->base != NULL; rti++)
461 if (fwrite (rti->base, rti->stride, 1, f) != 1)
462 fatal_error (input_location, "can%'t write PCH file: %m");
464 /* Write out all the global pointers, after translation. */
465 write_pch_globals (gt_ggc_rtab, &state);
467 /* Pad the PCH file so that the mmapped area starts on an allocation
468 granularity (usually page) boundary. */
470 long o;
471 o = ftell (state.f) + sizeof (mmi);
472 if (o == -1)
473 fatal_error (input_location, "can%'t get position in PCH file: %m");
474 mmi.offset = mmap_offset_alignment - o % mmap_offset_alignment;
475 if (mmi.offset == mmap_offset_alignment)
476 mmi.offset = 0;
477 mmi.offset += o;
479 if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1)
480 fatal_error (input_location, "can%'t write PCH file: %m");
481 if (mmi.offset != 0
482 && fseek (state.f, mmi.offset, SEEK_SET) != 0)
483 fatal_error (input_location, "can%'t write padding to PCH file: %m");
485 ggc_pch_prepare_write (state.d, state.f);
487 #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
488 vec<char> vbits = vNULL;
489 #endif
491 /* Actually write out the objects. */
492 for (i = 0; i < state.count; i++)
494 if (this_object_size < state.ptrs[i]->size)
496 this_object_size = state.ptrs[i]->size;
497 this_object = XRESIZEVAR (char, this_object, this_object_size);
499 #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
500 /* obj might contain uninitialized bytes, e.g. in the trailing
501 padding of the object. Avoid warnings by making the memory
502 temporarily defined and then restoring previous state. */
503 int get_vbits = 0;
504 size_t valid_size = state.ptrs[i]->size;
505 if (__builtin_expect (RUNNING_ON_VALGRIND, 0))
507 if (vbits.length () < valid_size)
508 vbits.safe_grow (valid_size);
509 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
510 vbits.address (), valid_size);
511 if (get_vbits == 3)
513 /* We assume that first part of obj is addressable, and
514 the rest is unaddressable. Find out where the boundary is
515 using binary search. */
516 size_t lo = 0, hi = valid_size;
517 while (hi > lo)
519 size_t mid = (lo + hi) / 2;
520 get_vbits = VALGRIND_GET_VBITS ((char *) state.ptrs[i]->obj
521 + mid, vbits.address (),
523 if (get_vbits == 3)
524 hi = mid;
525 else if (get_vbits == 1)
526 lo = mid + 1;
527 else
528 break;
530 if (get_vbits == 1 || get_vbits == 3)
532 valid_size = lo;
533 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
534 vbits.address (),
535 valid_size);
538 if (get_vbits == 1)
539 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (state.ptrs[i]->obj,
540 state.ptrs[i]->size));
542 #endif
543 memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size);
544 if (state.ptrs[i]->reorder_fn != NULL)
545 state.ptrs[i]->reorder_fn (state.ptrs[i]->obj,
546 state.ptrs[i]->note_ptr_cookie,
547 relocate_ptrs, &state);
548 state.ptrs[i]->note_ptr_fn (state.ptrs[i]->obj,
549 state.ptrs[i]->note_ptr_cookie,
550 relocate_ptrs, &state);
551 ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj,
552 state.ptrs[i]->new_addr, state.ptrs[i]->size,
553 state.ptrs[i]->note_ptr_fn == gt_pch_p_S);
554 if (state.ptrs[i]->note_ptr_fn != gt_pch_p_S)
555 memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size);
556 #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
557 if (__builtin_expect (get_vbits == 1, 0))
559 (void) VALGRIND_SET_VBITS (state.ptrs[i]->obj, vbits.address (),
560 valid_size);
561 if (valid_size != state.ptrs[i]->size)
562 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *)
563 state.ptrs[i]->obj
564 + valid_size,
565 state.ptrs[i]->size
566 - valid_size));
568 #endif
570 #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
571 vbits.release ();
572 #endif
574 ggc_pch_finish (state.d, state.f);
575 gt_pch_fixup_stringpool ();
577 XDELETE (state.ptrs);
578 XDELETE (this_object);
579 delete saving_htab;
580 saving_htab = NULL;
583 /* Read the state of the compiler back in from F. */
585 void
586 gt_pch_restore (FILE *f)
588 const struct ggc_root_tab *const *rt;
589 const struct ggc_root_tab *rti;
590 size_t i;
591 struct mmap_info mmi;
592 int result;
594 /* Delete any deletable objects. This makes ggc_pch_read much
595 faster, as it can be sure that no GCable objects remain other
596 than the ones just read in. */
597 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
598 for (rti = *rt; rti->base != NULL; rti++)
599 memset (rti->base, 0, rti->stride);
601 /* Read in all the scalar variables. */
602 for (rt = gt_pch_scalar_rtab; *rt; rt++)
603 for (rti = *rt; rti->base != NULL; rti++)
604 if (fread (rti->base, rti->stride, 1, f) != 1)
605 fatal_error (input_location, "can%'t read PCH file: %m");
607 /* Read in all the global pointers, in 6 easy loops. */
608 for (rt = gt_ggc_rtab; *rt; rt++)
609 for (rti = *rt; rti->base != NULL; rti++)
610 for (i = 0; i < rti->nelt; i++)
611 if (fread ((char *)rti->base + rti->stride * i,
612 sizeof (void *), 1, f) != 1)
613 fatal_error (input_location, "can%'t read PCH file: %m");
615 if (fread (&mmi, sizeof (mmi), 1, f) != 1)
616 fatal_error (input_location, "can%'t read PCH file: %m");
618 result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size,
619 fileno (f), mmi.offset);
620 if (result < 0)
621 fatal_error (input_location, "had to relocate PCH");
622 if (result == 0)
624 if (fseek (f, mmi.offset, SEEK_SET) != 0
625 || fread (mmi.preferred_base, mmi.size, 1, f) != 1)
626 fatal_error (input_location, "can%'t read PCH file: %m");
628 else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0)
629 fatal_error (input_location, "can%'t read PCH file: %m");
631 ggc_pch_read (f, mmi.preferred_base);
633 gt_pch_restore_stringpool ();
636 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
637 Select no address whatsoever, and let gt_pch_save choose what it will with
638 malloc, presumably. */
640 void *
641 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED,
642 int fd ATTRIBUTE_UNUSED)
644 return NULL;
647 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
648 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
649 same as base, indicating that the memory has been allocated but needs to
650 be read in from the file. Return -1 if the address differs, to relocation
651 of the PCH file would be required. */
654 default_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED,
655 size_t offset ATTRIBUTE_UNUSED)
657 void *addr = xmalloc (size);
658 return (addr == base) - 1;
661 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the
662 alignment required for allocating virtual memory. Usually this is the
663 same as pagesize. */
665 size_t
666 default_gt_pch_alloc_granularity (void)
668 return getpagesize ();
671 #if HAVE_MMAP_FILE
672 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
673 We temporarily allocate SIZE bytes, and let the kernel place the data
674 wherever it will. If it worked, that's our spot, if not we're likely
675 to be in trouble. */
677 void *
678 mmap_gt_pch_get_address (size_t size, int fd)
680 void *ret;
682 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
683 if (ret == (void *) MAP_FAILED)
684 ret = NULL;
685 else
686 munmap ((caddr_t) ret, size);
688 return ret;
691 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
692 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
693 mapping the data at BASE, -1 if we couldn't.
695 This version assumes that the kernel honors the START operand of mmap
696 even without MAP_FIXED if START through START+SIZE are not currently
697 mapped with something. */
700 mmap_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
702 void *addr;
704 /* We're called with size == 0 if we're not planning to load a PCH
705 file at all. This allows the hook to free any static space that
706 we might have allocated at link time. */
707 if (size == 0)
708 return -1;
710 addr = mmap ((caddr_t) base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
711 fd, offset);
713 return addr == base ? 1 : -1;
715 #endif /* HAVE_MMAP_FILE */
717 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
719 /* Modify the bound based on rlimits. */
720 static double
721 ggc_rlimit_bound (double limit)
723 #if defined(HAVE_GETRLIMIT)
724 struct rlimit rlim;
725 # if defined (RLIMIT_AS)
726 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably
727 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */
728 if (getrlimit (RLIMIT_AS, &rlim) == 0
729 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
730 && rlim.rlim_cur < limit)
731 limit = rlim.rlim_cur;
732 # elif defined (RLIMIT_DATA)
733 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
734 might be on an OS that has a broken mmap. (Others don't bound
735 mmap at all, apparently.) */
736 if (getrlimit (RLIMIT_DATA, &rlim) == 0
737 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
738 && rlim.rlim_cur < limit
739 /* Darwin has this horribly bogus default setting of
740 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA
741 appears to be ignored. Ignore such silliness. If a limit
742 this small was actually effective for mmap, GCC wouldn't even
743 start up. */
744 && rlim.rlim_cur >= 8 * 1024 * 1024)
745 limit = rlim.rlim_cur;
746 # endif /* RLIMIT_AS or RLIMIT_DATA */
747 #endif /* HAVE_GETRLIMIT */
749 return limit;
752 /* Heuristic to set a default for GGC_MIN_EXPAND. */
753 static int
754 ggc_min_expand_heuristic (void)
756 double min_expand = physmem_total ();
758 /* Adjust for rlimits. */
759 min_expand = ggc_rlimit_bound (min_expand);
761 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
762 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
763 min_expand /= 1024*1024*1024;
764 min_expand *= 70;
765 min_expand = MIN (min_expand, 70);
766 min_expand += 30;
768 return min_expand;
771 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
772 static int
773 ggc_min_heapsize_heuristic (void)
775 double phys_kbytes = physmem_total ();
776 double limit_kbytes = ggc_rlimit_bound (phys_kbytes * 2);
778 phys_kbytes /= 1024; /* Convert to Kbytes. */
779 limit_kbytes /= 1024;
781 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
782 bound of 128M (when RAM >= 1GB). */
783 phys_kbytes /= 8;
785 #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
786 /* Try not to overrun the RSS limit while doing garbage collection.
787 The RSS limit is only advisory, so no margin is subtracted. */
789 struct rlimit rlim;
790 if (getrlimit (RLIMIT_RSS, &rlim) == 0
791 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
792 phys_kbytes = MIN (phys_kbytes, rlim.rlim_cur / 1024);
794 # endif
796 /* Don't blindly run over our data limit; do GC at least when the
797 *next* GC would be within 20Mb of the limit or within a quarter of
798 the limit, whichever is larger. If GCC does hit the data limit,
799 compilation will fail, so this tries to be conservative. */
800 limit_kbytes = MAX (0, limit_kbytes - MAX (limit_kbytes / 4, 20 * 1024));
801 limit_kbytes = (limit_kbytes * 100) / (110 + ggc_min_expand_heuristic ());
802 phys_kbytes = MIN (phys_kbytes, limit_kbytes);
804 phys_kbytes = MAX (phys_kbytes, 4 * 1024);
805 phys_kbytes = MIN (phys_kbytes, 128 * 1024);
807 return phys_kbytes;
809 #endif
811 void
812 init_ggc_heuristics (void)
814 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
815 set_default_param_value (GGC_MIN_EXPAND, ggc_min_expand_heuristic ());
816 set_default_param_value (GGC_MIN_HEAPSIZE, ggc_min_heapsize_heuristic ());
817 #endif
820 /* GGC memory usage. */
821 struct ggc_usage: public mem_usage
823 /* Default constructor. */
824 ggc_usage (): m_freed (0), m_collected (0), m_overhead (0) {}
825 /* Constructor. */
826 ggc_usage (size_t allocated, size_t times, size_t peak,
827 size_t freed, size_t collected, size_t overhead)
828 : mem_usage (allocated, times, peak),
829 m_freed (freed), m_collected (collected), m_overhead (overhead) {}
831 /* Equality operator. */
832 inline bool
833 operator== (const ggc_usage &second) const
835 return (get_balance () == second.get_balance ()
836 && m_peak == second.m_peak
837 && m_times == second.m_times);
840 /* Comparison operator. */
841 inline bool
842 operator< (const ggc_usage &second) const
844 if (*this == second)
845 return false;
847 return (get_balance () == second.get_balance () ?
848 (m_peak == second.m_peak ? m_times < second.m_times
849 : m_peak < second.m_peak)
850 : get_balance () < second.get_balance ());
853 /* Register overhead of ALLOCATED and OVERHEAD bytes. */
854 inline void
855 register_overhead (size_t allocated, size_t overhead)
857 m_allocated += allocated;
858 m_overhead += overhead;
859 m_times++;
862 /* Release overhead of SIZE bytes. */
863 inline void
864 release_overhead (size_t size)
866 m_freed += size;
869 /* Sum the usage with SECOND usage. */
870 ggc_usage
871 operator+ (const ggc_usage &second)
873 return ggc_usage (m_allocated + second.m_allocated,
874 m_times + second.m_times,
875 m_peak + second.m_peak,
876 m_freed + second.m_freed,
877 m_collected + second.m_collected,
878 m_overhead + second.m_overhead);
881 /* Dump usage with PREFIX, where TOTAL is sum of all rows. */
882 inline void
883 dump (const char *prefix, ggc_usage &total) const
885 size_t balance = get_balance ();
886 fprintf (stderr,
887 "%-48s %9zu%c:%5.1f%%%9zu%c:%5.1f%%"
888 "%9zu%c:%5.1f%%%9zu%c:%5.1f%%%9zu%c\n",
889 prefix, SIZE_AMOUNT (m_collected),
890 get_percent (m_collected, total.m_collected),
891 SIZE_AMOUNT (m_freed), get_percent (m_freed, total.m_freed),
892 SIZE_AMOUNT (balance), get_percent (balance, total.get_balance ()),
893 SIZE_AMOUNT (m_overhead),
894 get_percent (m_overhead, total.m_overhead),
895 SIZE_AMOUNT (m_times));
898 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
899 inline void
900 dump (mem_location *loc, ggc_usage &total) const
902 char *location_string = loc->to_string ();
904 dump (location_string, total);
906 free (location_string);
909 /* Dump footer. */
910 inline void
911 dump_footer ()
913 print_dash_line ();
914 dump ("Total", *this);
915 print_dash_line ();
918 /* Get balance which is GGC allocation leak. */
919 inline size_t
920 get_balance () const
922 return m_allocated + m_overhead - m_collected - m_freed;
925 typedef std::pair<mem_location *, ggc_usage *> mem_pair_t;
927 /* Compare wrapper used by qsort method. */
928 static int
929 compare (const void *first, const void *second)
931 const mem_pair_t f = *(const mem_pair_t *)first;
932 const mem_pair_t s = *(const mem_pair_t *)second;
934 return s.second->get_balance () - f.second->get_balance ();
937 /* Compare rows in final GGC summary dump. */
938 static int
939 compare_final (const void *first, const void *second)
941 typedef std::pair<mem_location *, ggc_usage *> mem_pair_t;
943 const ggc_usage *f = ((const mem_pair_t *)first)->second;
944 const ggc_usage *s = ((const mem_pair_t *)second)->second;
946 size_t a = f->m_allocated + f->m_overhead - f->m_freed;
947 size_t b = s->m_allocated + s->m_overhead - s->m_freed;
949 return a == b ? 0 : (a < b ? 1 : -1);
952 /* Dump header with NAME. */
953 static inline void
954 dump_header (const char *name)
956 fprintf (stderr, "%-48s %11s%17s%17s%16s%17s\n", name, "Garbage", "Freed",
957 "Leak", "Overhead", "Times");
958 print_dash_line ();
961 /* Freed memory in bytes. */
962 size_t m_freed;
963 /* Collected memory in bytes. */
964 size_t m_collected;
965 /* Overhead memory in bytes. */
966 size_t m_overhead;
969 /* GCC memory description. */
970 static mem_alloc_description<ggc_usage> ggc_mem_desc;
972 /* Dump per-site memory statistics. */
974 void
975 dump_ggc_loc_statistics (bool final)
977 if (! GATHER_STATISTICS)
978 return;
980 ggc_force_collect = true;
981 ggc_collect ();
983 ggc_mem_desc.dump (GGC_ORIGIN, final ? ggc_usage::compare_final : NULL);
985 ggc_force_collect = false;
988 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
989 void
990 ggc_record_overhead (size_t allocated, size_t overhead, void *ptr MEM_STAT_DECL)
992 ggc_usage *usage = ggc_mem_desc.register_descriptor (ptr, GGC_ORIGIN, false
993 FINAL_PASS_MEM_STAT);
995 ggc_mem_desc.register_object_overhead (usage, allocated + overhead, ptr);
996 usage->register_overhead (allocated, overhead);
999 /* Notice that the pointer has been freed. */
1000 void
1001 ggc_free_overhead (void *ptr)
1003 ggc_mem_desc.release_object_overhead (ptr);
1006 /* After live values has been marked, walk all recorded pointers and see if
1007 they are still live. */
1008 void
1009 ggc_prune_overhead_list (void)
1011 typedef hash_map<const void *, std::pair<ggc_usage *, size_t > > map_t;
1013 map_t::iterator it = ggc_mem_desc.m_reverse_object_map->begin ();
1015 for (; it != ggc_mem_desc.m_reverse_object_map->end (); ++it)
1016 if (!ggc_marked_p ((*it).first))
1017 (*it).second.first->m_collected += (*it).second.second;
1019 delete ggc_mem_desc.m_reverse_object_map;
1020 ggc_mem_desc.m_reverse_object_map = new map_t (13, false, false);