Fix dot dump bug
[official-gcc.git] / gcc / ggc-common.c
blobb11a10c00726911b77c4c044bb767ca0ce98340f
1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999-2014 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 "hash-table.h"
27 #include "ggc.h"
28 #include "ggc-internal.h"
29 #include "diagnostic-core.h"
30 #include "params.h"
31 #include "hosthooks.h"
32 #include "hosthooks-def.h"
33 #include "plugin.h"
34 #include "vec.h"
35 #include "timevar.h"
37 /* When set, ggc_collect will do collection. */
38 bool ggc_force_collect;
40 /* When true, protect the contents of the identifier hash table. */
41 bool ggc_protect_identifiers = true;
43 /* Statistics about the allocation. */
44 static ggc_statistics *ggc_stats;
46 struct traversal_state;
48 static int ggc_htab_delete (void **, void *);
49 static int compare_ptr_data (const void *, const void *);
50 static void relocate_ptrs (void *, void *);
51 static void write_pch_globals (const struct ggc_root_tab * const *tab,
52 struct traversal_state *state);
54 /* Maintain global roots that are preserved during GC. */
56 /* Process a slot of an htab by deleting it if it has not been marked. */
58 static int
59 ggc_htab_delete (void **slot, void *info)
61 const struct ggc_cache_tab *r = (const struct ggc_cache_tab *) info;
63 if (! (*r->marked_p) (*slot))
64 htab_clear_slot (*r->base, slot);
65 else
66 (*r->cb) (*slot);
68 return 1;
72 /* This extra vector of dynamically registered root_tab-s is used by
73 ggc_mark_roots and gives the ability to dynamically add new GGC root
74 tables, for instance from some plugins; this vector is on the heap
75 since it is used by GGC internally. */
76 typedef const struct ggc_root_tab *const_ggc_root_tab_t;
77 static vec<const_ggc_root_tab_t> extra_root_vec;
79 /* Dynamically register a new GGC root table RT. This is useful for
80 plugins. */
82 void
83 ggc_register_root_tab (const struct ggc_root_tab* rt)
85 if (rt)
86 extra_root_vec.safe_push (rt);
89 /* This extra vector of dynamically registered cache_tab-s is used by
90 ggc_mark_roots and gives the ability to dynamically add new GGC cache
91 tables, for instance from some plugins; this vector is on the heap
92 since it is used by GGC internally. */
93 typedef const struct ggc_cache_tab *const_ggc_cache_tab_t;
94 static vec<const_ggc_cache_tab_t> extra_cache_vec;
96 /* Dynamically register a new GGC cache table CT. This is useful for
97 plugins. */
99 void
100 ggc_register_cache_tab (const struct ggc_cache_tab* ct)
102 if (ct)
103 extra_cache_vec.safe_push (ct);
106 /* Scan a hash table that has objects which are to be deleted if they are not
107 already marked. */
109 static void
110 ggc_scan_cache_tab (const_ggc_cache_tab_t ctp)
112 const struct ggc_cache_tab *cti;
114 for (cti = ctp; cti->base != NULL; cti++)
115 if (*cti->base)
117 ggc_set_mark (*cti->base);
118 htab_traverse_noresize (*cti->base, ggc_htab_delete,
119 CONST_CAST (void *, (const void *)cti));
120 ggc_set_mark ((*cti->base)->entries);
124 /* Mark all the roots in the table RT. */
126 static void
127 ggc_mark_root_tab (const_ggc_root_tab_t rt)
129 size_t i;
131 for ( ; rt->base != NULL; rt++)
132 for (i = 0; i < rt->nelt; i++)
133 (*rt->cb) (*(void **) ((char *)rt->base + rt->stride * i));
136 /* Iterate through all registered roots and mark each element. */
138 void
139 ggc_mark_roots (void)
141 const struct ggc_root_tab *const *rt;
142 const_ggc_root_tab_t rtp, rti;
143 const struct ggc_cache_tab *const *ct;
144 const_ggc_cache_tab_t ctp;
145 size_t i;
147 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
148 for (rti = *rt; rti->base != NULL; rti++)
149 memset (rti->base, 0, rti->stride);
151 for (rt = gt_ggc_rtab; *rt; rt++)
152 ggc_mark_root_tab (*rt);
154 FOR_EACH_VEC_ELT (extra_root_vec, i, rtp)
155 ggc_mark_root_tab (rtp);
157 if (ggc_protect_identifiers)
158 ggc_mark_stringpool ();
160 /* Now scan all hash tables that have objects which are to be deleted if
161 they are not already marked. */
162 for (ct = gt_ggc_cache_rtab; *ct; ct++)
163 ggc_scan_cache_tab (*ct);
165 FOR_EACH_VEC_ELT (extra_cache_vec, i, ctp)
166 ggc_scan_cache_tab (ctp);
168 if (! ggc_protect_identifiers)
169 ggc_purge_stringpool ();
171 /* Some plugins may call ggc_set_mark from here. */
172 invoke_plugin_callbacks (PLUGIN_GGC_MARKING, NULL);
175 /* Allocate a block of memory, then clear it. */
176 void *
177 ggc_internal_cleared_alloc (size_t size, void (*f)(void *), size_t s, size_t n
178 MEM_STAT_DECL)
180 void *buf = ggc_internal_alloc (size, f, s, n PASS_MEM_STAT);
181 memset (buf, 0, size);
182 return buf;
185 /* Resize a block of memory, possibly re-allocating it. */
186 void *
187 ggc_realloc (void *x, size_t size MEM_STAT_DECL)
189 void *r;
190 size_t old_size;
192 if (x == NULL)
193 return ggc_internal_alloc (size PASS_MEM_STAT);
195 old_size = ggc_get_size (x);
197 if (size <= old_size)
199 /* Mark the unwanted memory as unaccessible. We also need to make
200 the "new" size accessible, since ggc_get_size returns the size of
201 the pool, not the size of the individually allocated object, the
202 size which was previously made accessible. Unfortunately, we
203 don't know that previously allocated size. Without that
204 knowledge we have to lose some initialization-tracking for the
205 old parts of the object. An alternative is to mark the whole
206 old_size as reachable, but that would lose tracking of writes
207 after the end of the object (by small offsets). Discard the
208 handle to avoid handle leak. */
209 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x + size,
210 old_size - size));
211 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, size));
212 return x;
215 r = ggc_internal_alloc (size PASS_MEM_STAT);
217 /* Since ggc_get_size returns the size of the pool, not the size of the
218 individually allocated object, we'd access parts of the old object
219 that were marked invalid with the memcpy below. We lose a bit of the
220 initialization-tracking since some of it may be uninitialized. */
221 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, old_size));
223 memcpy (r, x, old_size);
225 /* The old object is not supposed to be used anymore. */
226 ggc_free (x);
228 return r;
231 void *
232 ggc_cleared_alloc_htab_ignore_args (size_t c ATTRIBUTE_UNUSED,
233 size_t n ATTRIBUTE_UNUSED)
235 gcc_assert (c * n == sizeof (struct htab));
236 return ggc_cleared_alloc<htab> ();
239 /* TODO: once we actually use type information in GGC, create a new tag
240 gt_gcc_ptr_array and use it for pointer arrays. */
241 void *
242 ggc_cleared_alloc_ptr_array_two_args (size_t c, size_t n)
244 gcc_assert (sizeof (PTR *) == n);
245 return ggc_cleared_vec_alloc<PTR *> (c);
248 /* These are for splay_tree_new_ggc. */
249 void *
250 ggc_splay_alloc (int sz, void *nl)
252 gcc_assert (!nl);
253 return ggc_internal_alloc (sz);
256 void
257 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl)
259 gcc_assert (!nl);
262 /* Print statistics that are independent of the collector in use. */
263 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
264 ? (x) \
265 : ((x) < 1024*1024*10 \
266 ? (x) / 1024 \
267 : (x) / (1024*1024))))
268 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
270 void
271 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED,
272 ggc_statistics *stats)
274 /* Set the pointer so that during collection we will actually gather
275 the statistics. */
276 ggc_stats = stats;
278 /* Then do one collection to fill in the statistics. */
279 ggc_collect ();
281 /* At present, we don't really gather any interesting statistics. */
283 /* Don't gather statistics any more. */
284 ggc_stats = NULL;
287 /* Functions for saving and restoring GCable memory to disk. */
289 struct ptr_data
291 void *obj;
292 void *note_ptr_cookie;
293 gt_note_pointers note_ptr_fn;
294 gt_handle_reorder reorder_fn;
295 size_t size;
296 void *new_addr;
299 #define POINTER_HASH(x) (hashval_t)((intptr_t)x >> 3)
301 /* Helper for hashing saving_htab. */
303 struct saving_hasher : typed_free_remove <ptr_data>
305 typedef ptr_data value_type;
306 typedef void compare_type;
307 static inline hashval_t hash (const value_type *);
308 static inline bool equal (const value_type *, const compare_type *);
311 inline hashval_t
312 saving_hasher::hash (const value_type *p)
314 return POINTER_HASH (p->obj);
317 inline bool
318 saving_hasher::equal (const value_type *p1, const compare_type *p2)
320 return p1->obj == p2;
323 static hash_table <saving_hasher> saving_htab;
325 /* Register an object in the hash table. */
328 gt_pch_note_object (void *obj, void *note_ptr_cookie,
329 gt_note_pointers note_ptr_fn)
331 struct ptr_data **slot;
333 if (obj == NULL || obj == (void *) 1)
334 return 0;
336 slot = (struct ptr_data **)
337 saving_htab.find_slot_with_hash (obj, POINTER_HASH (obj), INSERT);
338 if (*slot != NULL)
340 gcc_assert ((*slot)->note_ptr_fn == note_ptr_fn
341 && (*slot)->note_ptr_cookie == note_ptr_cookie);
342 return 0;
345 *slot = XCNEW (struct ptr_data);
346 (*slot)->obj = obj;
347 (*slot)->note_ptr_fn = note_ptr_fn;
348 (*slot)->note_ptr_cookie = note_ptr_cookie;
349 if (note_ptr_fn == gt_pch_p_S)
350 (*slot)->size = strlen ((const char *)obj) + 1;
351 else
352 (*slot)->size = ggc_get_size (obj);
353 return 1;
356 /* Register an object in the hash table. */
358 void
359 gt_pch_note_reorder (void *obj, void *note_ptr_cookie,
360 gt_handle_reorder reorder_fn)
362 struct ptr_data *data;
364 if (obj == NULL || obj == (void *) 1)
365 return;
367 data = (struct ptr_data *)
368 saving_htab.find_with_hash (obj, POINTER_HASH (obj));
369 gcc_assert (data && data->note_ptr_cookie == note_ptr_cookie);
371 data->reorder_fn = reorder_fn;
374 /* Handy state for the traversal functions. */
376 struct traversal_state
378 FILE *f;
379 struct ggc_pch_data *d;
380 size_t count;
381 struct ptr_data **ptrs;
382 size_t ptrs_i;
385 /* Callbacks for htab_traverse. */
388 ggc_call_count (ptr_data **slot, traversal_state *state)
390 struct ptr_data *d = *slot;
392 ggc_pch_count_object (state->d, d->obj, d->size,
393 d->note_ptr_fn == gt_pch_p_S);
394 state->count++;
395 return 1;
399 ggc_call_alloc (ptr_data **slot, traversal_state *state)
401 struct ptr_data *d = *slot;
403 d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size,
404 d->note_ptr_fn == gt_pch_p_S);
405 state->ptrs[state->ptrs_i++] = d;
406 return 1;
409 /* Callback for qsort. */
411 static int
412 compare_ptr_data (const void *p1_p, const void *p2_p)
414 const struct ptr_data *const p1 = *(const struct ptr_data *const *)p1_p;
415 const struct ptr_data *const p2 = *(const struct ptr_data *const *)p2_p;
416 return (((size_t)p1->new_addr > (size_t)p2->new_addr)
417 - ((size_t)p1->new_addr < (size_t)p2->new_addr));
420 /* Callbacks for note_ptr_fn. */
422 static void
423 relocate_ptrs (void *ptr_p, void *state_p)
425 void **ptr = (void **)ptr_p;
426 struct traversal_state *state ATTRIBUTE_UNUSED
427 = (struct traversal_state *)state_p;
428 struct ptr_data *result;
430 if (*ptr == NULL || *ptr == (void *)1)
431 return;
433 result = (struct ptr_data *)
434 saving_htab.find_with_hash (*ptr, POINTER_HASH (*ptr));
435 gcc_assert (result);
436 *ptr = result->new_addr;
439 /* Write out, after relocation, the pointers in TAB. */
440 static void
441 write_pch_globals (const struct ggc_root_tab * const *tab,
442 struct traversal_state *state)
444 const struct ggc_root_tab *const *rt;
445 const struct ggc_root_tab *rti;
446 size_t i;
448 for (rt = tab; *rt; rt++)
449 for (rti = *rt; rti->base != NULL; rti++)
450 for (i = 0; i < rti->nelt; i++)
452 void *ptr = *(void **)((char *)rti->base + rti->stride * i);
453 struct ptr_data *new_ptr;
454 if (ptr == NULL || ptr == (void *)1)
456 if (fwrite (&ptr, sizeof (void *), 1, state->f)
457 != 1)
458 fatal_error ("can%'t write PCH file: %m");
460 else
462 new_ptr = (struct ptr_data *)
463 saving_htab.find_with_hash (ptr, POINTER_HASH (ptr));
464 if (fwrite (&new_ptr->new_addr, sizeof (void *), 1, state->f)
465 != 1)
466 fatal_error ("can%'t write PCH file: %m");
471 /* Hold the information we need to mmap the file back in. */
473 struct mmap_info
475 size_t offset;
476 size_t size;
477 void *preferred_base;
480 /* Write out the state of the compiler to F. */
482 void
483 gt_pch_save (FILE *f)
485 const struct ggc_root_tab *const *rt;
486 const struct ggc_root_tab *rti;
487 size_t i;
488 struct traversal_state state;
489 char *this_object = NULL;
490 size_t this_object_size = 0;
491 struct mmap_info mmi;
492 const size_t mmap_offset_alignment = host_hooks.gt_pch_alloc_granularity ();
494 gt_pch_save_stringpool ();
496 timevar_push (TV_PCH_PTR_REALLOC);
497 saving_htab.create (50000);
499 for (rt = gt_ggc_rtab; *rt; rt++)
500 for (rti = *rt; rti->base != NULL; rti++)
501 for (i = 0; i < rti->nelt; i++)
502 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
504 for (rt = gt_pch_cache_rtab; *rt; rt++)
505 for (rti = *rt; rti->base != NULL; rti++)
506 for (i = 0; i < rti->nelt; i++)
507 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
509 /* Prepare the objects for writing, determine addresses and such. */
510 state.f = f;
511 state.d = init_ggc_pch ();
512 state.count = 0;
513 saving_htab.traverse <traversal_state *, ggc_call_count> (&state);
515 mmi.size = ggc_pch_total_size (state.d);
517 /* Try to arrange things so that no relocation is necessary, but
518 don't try very hard. On most platforms, this will always work,
519 and on the rest it's a lot of work to do better.
520 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
521 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */
522 mmi.preferred_base = host_hooks.gt_pch_get_address (mmi.size, fileno (f));
524 ggc_pch_this_base (state.d, mmi.preferred_base);
526 state.ptrs = XNEWVEC (struct ptr_data *, state.count);
527 state.ptrs_i = 0;
529 saving_htab.traverse <traversal_state *, ggc_call_alloc> (&state);
530 timevar_pop (TV_PCH_PTR_REALLOC);
532 timevar_push (TV_PCH_PTR_SORT);
533 qsort (state.ptrs, state.count, sizeof (*state.ptrs), compare_ptr_data);
534 timevar_pop (TV_PCH_PTR_SORT);
536 /* Write out all the scalar variables. */
537 for (rt = gt_pch_scalar_rtab; *rt; rt++)
538 for (rti = *rt; rti->base != NULL; rti++)
539 if (fwrite (rti->base, rti->stride, 1, f) != 1)
540 fatal_error ("can%'t write PCH file: %m");
542 /* Write out all the global pointers, after translation. */
543 write_pch_globals (gt_ggc_rtab, &state);
544 write_pch_globals (gt_pch_cache_rtab, &state);
546 /* Pad the PCH file so that the mmapped area starts on an allocation
547 granularity (usually page) boundary. */
549 long o;
550 o = ftell (state.f) + sizeof (mmi);
551 if (o == -1)
552 fatal_error ("can%'t get position in PCH file: %m");
553 mmi.offset = mmap_offset_alignment - o % mmap_offset_alignment;
554 if (mmi.offset == mmap_offset_alignment)
555 mmi.offset = 0;
556 mmi.offset += o;
558 if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1)
559 fatal_error ("can%'t write PCH file: %m");
560 if (mmi.offset != 0
561 && fseek (state.f, mmi.offset, SEEK_SET) != 0)
562 fatal_error ("can%'t write padding to PCH file: %m");
564 ggc_pch_prepare_write (state.d, state.f);
566 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
567 vec<char> vbits = vNULL;
568 #endif
570 /* Actually write out the objects. */
571 for (i = 0; i < state.count; i++)
573 if (this_object_size < state.ptrs[i]->size)
575 this_object_size = state.ptrs[i]->size;
576 this_object = XRESIZEVAR (char, this_object, this_object_size);
578 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
579 /* obj might contain uninitialized bytes, e.g. in the trailing
580 padding of the object. Avoid warnings by making the memory
581 temporarily defined and then restoring previous state. */
582 int get_vbits = 0;
583 size_t valid_size = state.ptrs[i]->size;
584 if (__builtin_expect (RUNNING_ON_VALGRIND, 0))
586 if (vbits.length () < valid_size)
587 vbits.safe_grow (valid_size);
588 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
589 vbits.address (), valid_size);
590 if (get_vbits == 3)
592 /* We assume that first part of obj is addressable, and
593 the rest is unaddressable. Find out where the boundary is
594 using binary search. */
595 size_t lo = 0, hi = valid_size;
596 while (hi > lo)
598 size_t mid = (lo + hi) / 2;
599 get_vbits = VALGRIND_GET_VBITS ((char *) state.ptrs[i]->obj
600 + mid, vbits.address (),
602 if (get_vbits == 3)
603 hi = mid;
604 else if (get_vbits == 1)
605 lo = mid + 1;
606 else
607 break;
609 if (get_vbits == 1 || get_vbits == 3)
611 valid_size = lo;
612 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
613 vbits.address (),
614 valid_size);
617 if (get_vbits == 1)
618 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (state.ptrs[i]->obj,
619 state.ptrs[i]->size));
621 #endif
622 memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size);
623 if (state.ptrs[i]->reorder_fn != NULL)
624 state.ptrs[i]->reorder_fn (state.ptrs[i]->obj,
625 state.ptrs[i]->note_ptr_cookie,
626 relocate_ptrs, &state);
627 state.ptrs[i]->note_ptr_fn (state.ptrs[i]->obj,
628 state.ptrs[i]->note_ptr_cookie,
629 relocate_ptrs, &state);
630 ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj,
631 state.ptrs[i]->new_addr, state.ptrs[i]->size,
632 state.ptrs[i]->note_ptr_fn == gt_pch_p_S);
633 if (state.ptrs[i]->note_ptr_fn != gt_pch_p_S)
634 memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size);
635 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
636 if (__builtin_expect (get_vbits == 1, 0))
638 (void) VALGRIND_SET_VBITS (state.ptrs[i]->obj, vbits.address (),
639 valid_size);
640 if (valid_size != state.ptrs[i]->size)
641 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *)
642 state.ptrs[i]->obj
643 + valid_size,
644 state.ptrs[i]->size
645 - valid_size));
647 #endif
649 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
650 vbits.release ();
651 #endif
653 ggc_pch_finish (state.d, state.f);
654 gt_pch_fixup_stringpool ();
656 XDELETE (state.ptrs);
657 XDELETE (this_object);
658 saving_htab.dispose ();
661 /* Read the state of the compiler back in from F. */
663 void
664 gt_pch_restore (FILE *f)
666 const struct ggc_root_tab *const *rt;
667 const struct ggc_root_tab *rti;
668 size_t i;
669 struct mmap_info mmi;
670 int result;
672 /* Delete any deletable objects. This makes ggc_pch_read much
673 faster, as it can be sure that no GCable objects remain other
674 than the ones just read in. */
675 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
676 for (rti = *rt; rti->base != NULL; rti++)
677 memset (rti->base, 0, rti->stride);
679 /* Read in all the scalar variables. */
680 for (rt = gt_pch_scalar_rtab; *rt; rt++)
681 for (rti = *rt; rti->base != NULL; rti++)
682 if (fread (rti->base, rti->stride, 1, f) != 1)
683 fatal_error ("can%'t read PCH file: %m");
685 /* Read in all the global pointers, in 6 easy loops. */
686 for (rt = gt_ggc_rtab; *rt; rt++)
687 for (rti = *rt; rti->base != NULL; rti++)
688 for (i = 0; i < rti->nelt; i++)
689 if (fread ((char *)rti->base + rti->stride * i,
690 sizeof (void *), 1, f) != 1)
691 fatal_error ("can%'t read PCH file: %m");
693 for (rt = gt_pch_cache_rtab; *rt; rt++)
694 for (rti = *rt; rti->base != NULL; rti++)
695 for (i = 0; i < rti->nelt; i++)
696 if (fread ((char *)rti->base + rti->stride * i,
697 sizeof (void *), 1, f) != 1)
698 fatal_error ("can%'t read PCH file: %m");
700 if (fread (&mmi, sizeof (mmi), 1, f) != 1)
701 fatal_error ("can%'t read PCH file: %m");
703 result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size,
704 fileno (f), mmi.offset);
705 if (result < 0)
706 fatal_error ("had to relocate PCH");
707 if (result == 0)
709 if (fseek (f, mmi.offset, SEEK_SET) != 0
710 || fread (mmi.preferred_base, mmi.size, 1, f) != 1)
711 fatal_error ("can%'t read PCH file: %m");
713 else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0)
714 fatal_error ("can%'t read PCH file: %m");
716 ggc_pch_read (f, mmi.preferred_base);
718 gt_pch_restore_stringpool ();
721 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
722 Select no address whatsoever, and let gt_pch_save choose what it will with
723 malloc, presumably. */
725 void *
726 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED,
727 int fd ATTRIBUTE_UNUSED)
729 return NULL;
732 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
733 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
734 same as base, indicating that the memory has been allocated but needs to
735 be read in from the file. Return -1 if the address differs, to relocation
736 of the PCH file would be required. */
739 default_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED,
740 size_t offset ATTRIBUTE_UNUSED)
742 void *addr = xmalloc (size);
743 return (addr == base) - 1;
746 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the
747 alignment required for allocating virtual memory. Usually this is the
748 same as pagesize. */
750 size_t
751 default_gt_pch_alloc_granularity (void)
753 return getpagesize ();
756 #if HAVE_MMAP_FILE
757 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
758 We temporarily allocate SIZE bytes, and let the kernel place the data
759 wherever it will. If it worked, that's our spot, if not we're likely
760 to be in trouble. */
762 void *
763 mmap_gt_pch_get_address (size_t size, int fd)
765 void *ret;
767 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
768 if (ret == (void *) MAP_FAILED)
769 ret = NULL;
770 else
771 munmap ((caddr_t) ret, size);
773 return ret;
776 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
777 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
778 mapping the data at BASE, -1 if we couldn't.
780 This version assumes that the kernel honors the START operand of mmap
781 even without MAP_FIXED if START through START+SIZE are not currently
782 mapped with something. */
785 mmap_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
787 void *addr;
789 /* We're called with size == 0 if we're not planning to load a PCH
790 file at all. This allows the hook to free any static space that
791 we might have allocated at link time. */
792 if (size == 0)
793 return -1;
795 addr = mmap ((caddr_t) base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
796 fd, offset);
798 return addr == base ? 1 : -1;
800 #endif /* HAVE_MMAP_FILE */
802 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
804 /* Modify the bound based on rlimits. */
805 static double
806 ggc_rlimit_bound (double limit)
808 #if defined(HAVE_GETRLIMIT)
809 struct rlimit rlim;
810 # if defined (RLIMIT_AS)
811 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably
812 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */
813 if (getrlimit (RLIMIT_AS, &rlim) == 0
814 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
815 && rlim.rlim_cur < limit)
816 limit = rlim.rlim_cur;
817 # elif defined (RLIMIT_DATA)
818 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
819 might be on an OS that has a broken mmap. (Others don't bound
820 mmap at all, apparently.) */
821 if (getrlimit (RLIMIT_DATA, &rlim) == 0
822 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
823 && rlim.rlim_cur < limit
824 /* Darwin has this horribly bogus default setting of
825 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA
826 appears to be ignored. Ignore such silliness. If a limit
827 this small was actually effective for mmap, GCC wouldn't even
828 start up. */
829 && rlim.rlim_cur >= 8 * 1024 * 1024)
830 limit = rlim.rlim_cur;
831 # endif /* RLIMIT_AS or RLIMIT_DATA */
832 #endif /* HAVE_GETRLIMIT */
834 return limit;
837 /* Heuristic to set a default for GGC_MIN_EXPAND. */
838 static int
839 ggc_min_expand_heuristic (void)
841 double min_expand = physmem_total ();
843 /* Adjust for rlimits. */
844 min_expand = ggc_rlimit_bound (min_expand);
846 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
847 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
848 min_expand /= 1024*1024*1024;
849 min_expand *= 70;
850 min_expand = MIN (min_expand, 70);
851 min_expand += 30;
853 return min_expand;
856 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
857 static int
858 ggc_min_heapsize_heuristic (void)
860 double phys_kbytes = physmem_total ();
861 double limit_kbytes = ggc_rlimit_bound (phys_kbytes * 2);
863 phys_kbytes /= 1024; /* Convert to Kbytes. */
864 limit_kbytes /= 1024;
866 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
867 bound of 128M (when RAM >= 1GB). */
868 phys_kbytes /= 8;
870 #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
871 /* Try not to overrun the RSS limit while doing garbage collection.
872 The RSS limit is only advisory, so no margin is subtracted. */
874 struct rlimit rlim;
875 if (getrlimit (RLIMIT_RSS, &rlim) == 0
876 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
877 phys_kbytes = MIN (phys_kbytes, rlim.rlim_cur / 1024);
879 # endif
881 /* Don't blindly run over our data limit; do GC at least when the
882 *next* GC would be within 20Mb of the limit or within a quarter of
883 the limit, whichever is larger. If GCC does hit the data limit,
884 compilation will fail, so this tries to be conservative. */
885 limit_kbytes = MAX (0, limit_kbytes - MAX (limit_kbytes / 4, 20 * 1024));
886 limit_kbytes = (limit_kbytes * 100) / (110 + ggc_min_expand_heuristic ());
887 phys_kbytes = MIN (phys_kbytes, limit_kbytes);
889 phys_kbytes = MAX (phys_kbytes, 4 * 1024);
890 phys_kbytes = MIN (phys_kbytes, 128 * 1024);
892 return phys_kbytes;
894 #endif
896 void
897 init_ggc_heuristics (void)
899 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
900 set_default_param_value (GGC_MIN_EXPAND, ggc_min_expand_heuristic ());
901 set_default_param_value (GGC_MIN_HEAPSIZE, ggc_min_heapsize_heuristic ());
902 #endif
905 /* Datastructure used to store per-call-site statistics. */
906 struct loc_descriptor
908 const char *file;
909 int line;
910 const char *function;
911 int times;
912 size_t allocated;
913 size_t overhead;
914 size_t freed;
915 size_t collected;
918 /* Hash table helper. */
920 struct loc_desc_hasher : typed_noop_remove <loc_descriptor>
922 typedef loc_descriptor value_type;
923 typedef loc_descriptor compare_type;
924 static inline hashval_t hash (const value_type *);
925 static inline bool equal (const value_type *, const compare_type *);
928 inline hashval_t
929 loc_desc_hasher::hash (const value_type *d)
931 return htab_hash_pointer (d->function) | d->line;
934 inline bool
935 loc_desc_hasher::equal (const value_type *d, const compare_type *d2)
937 return (d->file == d2->file && d->line == d2->line
938 && d->function == d2->function);
941 /* Hashtable used for statistics. */
942 static hash_table <loc_desc_hasher> loc_hash;
944 struct ptr_hash_entry
946 void *ptr;
947 struct loc_descriptor *loc;
948 size_t size;
951 /* Helper for ptr_hash table. */
953 struct ptr_hash_hasher : typed_noop_remove <ptr_hash_entry>
955 typedef ptr_hash_entry value_type;
956 typedef void compare_type;
957 static inline hashval_t hash (const value_type *);
958 static inline bool equal (const value_type *, const compare_type *);
961 inline hashval_t
962 ptr_hash_hasher::hash (const value_type *d)
964 return htab_hash_pointer (d->ptr);
967 inline bool
968 ptr_hash_hasher::equal (const value_type *p, const compare_type *p2)
970 return (p->ptr == p2);
973 /* Hashtable converting address of allocated field to loc descriptor. */
974 static hash_table <ptr_hash_hasher> ptr_hash;
976 /* Return descriptor for given call site, create new one if needed. */
977 static struct loc_descriptor *
978 make_loc_descriptor (const char *name, int line, const char *function)
980 struct loc_descriptor loc;
981 struct loc_descriptor **slot;
983 loc.file = name;
984 loc.line = line;
985 loc.function = function;
986 if (!loc_hash.is_created ())
987 loc_hash.create (10);
989 slot = loc_hash.find_slot (&loc, INSERT);
990 if (*slot)
991 return *slot;
992 *slot = XCNEW (struct loc_descriptor);
993 (*slot)->file = name;
994 (*slot)->line = line;
995 (*slot)->function = function;
996 return *slot;
999 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
1000 void
1001 ggc_record_overhead (size_t allocated, size_t overhead, void *ptr,
1002 const char *name, int line, const char *function)
1004 struct loc_descriptor *loc = make_loc_descriptor (name, line, function);
1005 struct ptr_hash_entry *p = XNEW (struct ptr_hash_entry);
1006 ptr_hash_entry **slot;
1008 p->ptr = ptr;
1009 p->loc = loc;
1010 p->size = allocated + overhead;
1011 if (!ptr_hash.is_created ())
1012 ptr_hash.create (10);
1013 slot = ptr_hash.find_slot_with_hash (ptr, htab_hash_pointer (ptr), INSERT);
1014 gcc_assert (!*slot);
1015 *slot = p;
1017 loc->times++;
1018 loc->allocated+=allocated;
1019 loc->overhead+=overhead;
1022 /* Helper function for prune_overhead_list. See if SLOT is still marked and
1023 remove it from hashtable if it is not. */
1025 ggc_prune_ptr (ptr_hash_entry **slot, void *b ATTRIBUTE_UNUSED)
1027 struct ptr_hash_entry *p = *slot;
1028 if (!ggc_marked_p (p->ptr))
1030 p->loc->collected += p->size;
1031 ptr_hash.clear_slot (slot);
1032 free (p);
1034 return 1;
1037 /* After live values has been marked, walk all recorded pointers and see if
1038 they are still live. */
1039 void
1040 ggc_prune_overhead_list (void)
1042 ptr_hash.traverse <void *, ggc_prune_ptr> (NULL);
1045 /* Notice that the pointer has been freed. */
1046 void
1047 ggc_free_overhead (void *ptr)
1049 ptr_hash_entry **slot;
1050 slot = ptr_hash.find_slot_with_hash (ptr, htab_hash_pointer (ptr), NO_INSERT);
1051 struct ptr_hash_entry *p;
1052 /* The pointer might be not found if a PCH read happened between allocation
1053 and ggc_free () call. FIXME: account memory properly in the presence of
1054 PCH. */
1055 if (!slot)
1056 return;
1057 p = (struct ptr_hash_entry *) *slot;
1058 p->loc->freed += p->size;
1059 ptr_hash.clear_slot (slot);
1060 free (p);
1063 /* Helper for qsort; sort descriptors by amount of memory consumed. */
1064 static int
1065 final_cmp_statistic (const void *loc1, const void *loc2)
1067 const struct loc_descriptor *const l1 =
1068 *(const struct loc_descriptor *const *) loc1;
1069 const struct loc_descriptor *const l2 =
1070 *(const struct loc_descriptor *const *) loc2;
1071 long diff;
1072 diff = ((long)(l1->allocated + l1->overhead - l1->freed) -
1073 (l2->allocated + l2->overhead - l2->freed));
1074 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
1077 /* Helper for qsort; sort descriptors by amount of memory consumed. */
1078 static int
1079 cmp_statistic (const void *loc1, const void *loc2)
1081 const struct loc_descriptor *const l1 =
1082 *(const struct loc_descriptor *const *) loc1;
1083 const struct loc_descriptor *const l2 =
1084 *(const struct loc_descriptor *const *) loc2;
1085 long diff;
1087 diff = ((long)(l1->allocated + l1->overhead - l1->freed - l1->collected) -
1088 (l2->allocated + l2->overhead - l2->freed - l2->collected));
1089 if (diff)
1090 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
1091 diff = ((long)(l1->allocated + l1->overhead - l1->freed) -
1092 (l2->allocated + l2->overhead - l2->freed));
1093 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
1096 /* Collect array of the descriptors from hashtable. */
1097 static struct loc_descriptor **loc_array;
1099 ggc_add_statistics (loc_descriptor **slot, int *n)
1101 loc_array[*n] = *slot;
1102 (*n)++;
1103 return 1;
1106 /* Dump per-site memory statistics. */
1108 void
1109 dump_ggc_loc_statistics (bool final)
1111 int nentries = 0;
1112 char s[4096];
1113 size_t collected = 0, freed = 0, allocated = 0, overhead = 0, times = 0;
1114 int i;
1116 if (! GATHER_STATISTICS)
1117 return;
1119 ggc_force_collect = true;
1120 ggc_collect ();
1122 loc_array = XCNEWVEC (struct loc_descriptor *,
1123 loc_hash.elements_with_deleted ());
1124 fprintf (stderr, "-------------------------------------------------------\n");
1125 fprintf (stderr, "\n%-48s %10s %10s %10s %10s %10s\n",
1126 "source location", "Garbage", "Freed", "Leak", "Overhead", "Times");
1127 fprintf (stderr, "-------------------------------------------------------\n");
1128 loc_hash.traverse <int *, ggc_add_statistics> (&nentries);
1129 qsort (loc_array, nentries, sizeof (*loc_array),
1130 final ? final_cmp_statistic : cmp_statistic);
1131 for (i = 0; i < nentries; i++)
1133 struct loc_descriptor *d = loc_array[i];
1134 allocated += d->allocated;
1135 times += d->times;
1136 freed += d->freed;
1137 collected += d->collected;
1138 overhead += d->overhead;
1140 for (i = 0; i < nentries; i++)
1142 struct loc_descriptor *d = loc_array[i];
1143 if (d->allocated)
1145 const char *s1 = d->file;
1146 const char *s2;
1147 while ((s2 = strstr (s1, "gcc/")))
1148 s1 = s2 + 4;
1149 sprintf (s, "%s:%i (%s)", s1, d->line, d->function);
1150 s[48] = 0;
1151 fprintf (stderr, "%-48s %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li\n", s,
1152 (long)d->collected,
1153 (d->collected) * 100.0 / collected,
1154 (long)d->freed,
1155 (d->freed) * 100.0 / freed,
1156 (long)(d->allocated + d->overhead - d->freed - d->collected),
1157 (d->allocated + d->overhead - d->freed - d->collected) * 100.0
1158 / (allocated + overhead - freed - collected),
1159 (long)d->overhead,
1160 d->overhead * 100.0 / overhead,
1161 (long)d->times);
1164 fprintf (stderr, "%-48s %10ld %10ld %10ld %10ld %10ld\n",
1165 "Total", (long)collected, (long)freed,
1166 (long)(allocated + overhead - freed - collected), (long)overhead,
1167 (long)times);
1168 fprintf (stderr, "%-48s %10s %10s %10s %10s %10s\n",
1169 "source location", "Garbage", "Freed", "Leak", "Overhead", "Times");
1170 fprintf (stderr, "-------------------------------------------------------\n");
1171 ggc_force_collect = false;