re PR inline-asm/61692 (ICE in extract_insn in recog.c for asm with many parameters)
[official-gcc.git] / gcc / ggc-common.c
blob06f70c2f35303b01e888ed6ec7a946832aed587b
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 gt_clear_caches ();
167 FOR_EACH_VEC_ELT (extra_cache_vec, i, ctp)
168 ggc_scan_cache_tab (ctp);
170 if (! ggc_protect_identifiers)
171 ggc_purge_stringpool ();
173 /* Some plugins may call ggc_set_mark from here. */
174 invoke_plugin_callbacks (PLUGIN_GGC_MARKING, NULL);
177 /* Allocate a block of memory, then clear it. */
178 void *
179 ggc_internal_cleared_alloc (size_t size, void (*f)(void *), size_t s, size_t n
180 MEM_STAT_DECL)
182 void *buf = ggc_internal_alloc (size, f, s, n PASS_MEM_STAT);
183 memset (buf, 0, size);
184 return buf;
187 /* Resize a block of memory, possibly re-allocating it. */
188 void *
189 ggc_realloc (void *x, size_t size MEM_STAT_DECL)
191 void *r;
192 size_t old_size;
194 if (x == NULL)
195 return ggc_internal_alloc (size PASS_MEM_STAT);
197 old_size = ggc_get_size (x);
199 if (size <= old_size)
201 /* Mark the unwanted memory as unaccessible. We also need to make
202 the "new" size accessible, since ggc_get_size returns the size of
203 the pool, not the size of the individually allocated object, the
204 size which was previously made accessible. Unfortunately, we
205 don't know that previously allocated size. Without that
206 knowledge we have to lose some initialization-tracking for the
207 old parts of the object. An alternative is to mark the whole
208 old_size as reachable, but that would lose tracking of writes
209 after the end of the object (by small offsets). Discard the
210 handle to avoid handle leak. */
211 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x + size,
212 old_size - size));
213 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, size));
214 return x;
217 r = ggc_internal_alloc (size PASS_MEM_STAT);
219 /* Since ggc_get_size returns the size of the pool, not the size of the
220 individually allocated object, we'd access parts of the old object
221 that were marked invalid with the memcpy below. We lose a bit of the
222 initialization-tracking since some of it may be uninitialized. */
223 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, old_size));
225 memcpy (r, x, old_size);
227 /* The old object is not supposed to be used anymore. */
228 ggc_free (x);
230 return r;
233 void *
234 ggc_cleared_alloc_htab_ignore_args (size_t c ATTRIBUTE_UNUSED,
235 size_t n ATTRIBUTE_UNUSED)
237 gcc_assert (c * n == sizeof (struct htab));
238 return ggc_cleared_alloc<htab> ();
241 /* TODO: once we actually use type information in GGC, create a new tag
242 gt_gcc_ptr_array and use it for pointer arrays. */
243 void *
244 ggc_cleared_alloc_ptr_array_two_args (size_t c, size_t n)
246 gcc_assert (sizeof (PTR *) == n);
247 return ggc_cleared_vec_alloc<PTR *> (c);
250 /* These are for splay_tree_new_ggc. */
251 void *
252 ggc_splay_alloc (int sz, void *nl)
254 gcc_assert (!nl);
255 return ggc_internal_alloc (sz);
258 void
259 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl)
261 gcc_assert (!nl);
264 /* Print statistics that are independent of the collector in use. */
265 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
266 ? (x) \
267 : ((x) < 1024*1024*10 \
268 ? (x) / 1024 \
269 : (x) / (1024*1024))))
270 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
272 void
273 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED,
274 ggc_statistics *stats)
276 /* Set the pointer so that during collection we will actually gather
277 the statistics. */
278 ggc_stats = stats;
280 /* Then do one collection to fill in the statistics. */
281 ggc_collect ();
283 /* At present, we don't really gather any interesting statistics. */
285 /* Don't gather statistics any more. */
286 ggc_stats = NULL;
289 /* Functions for saving and restoring GCable memory to disk. */
291 struct ptr_data
293 void *obj;
294 void *note_ptr_cookie;
295 gt_note_pointers note_ptr_fn;
296 gt_handle_reorder reorder_fn;
297 size_t size;
298 void *new_addr;
301 #define POINTER_HASH(x) (hashval_t)((intptr_t)x >> 3)
303 /* Helper for hashing saving_htab. */
305 struct saving_hasher : typed_free_remove <ptr_data>
307 typedef ptr_data value_type;
308 typedef void compare_type;
309 static inline hashval_t hash (const value_type *);
310 static inline bool equal (const value_type *, const compare_type *);
313 inline hashval_t
314 saving_hasher::hash (const value_type *p)
316 return POINTER_HASH (p->obj);
319 inline bool
320 saving_hasher::equal (const value_type *p1, const compare_type *p2)
322 return p1->obj == p2;
325 static hash_table<saving_hasher> *saving_htab;
327 /* Register an object in the hash table. */
330 gt_pch_note_object (void *obj, void *note_ptr_cookie,
331 gt_note_pointers note_ptr_fn)
333 struct ptr_data **slot;
335 if (obj == NULL || obj == (void *) 1)
336 return 0;
338 slot = (struct ptr_data **)
339 saving_htab->find_slot_with_hash (obj, POINTER_HASH (obj), INSERT);
340 if (*slot != NULL)
342 gcc_assert ((*slot)->note_ptr_fn == note_ptr_fn
343 && (*slot)->note_ptr_cookie == note_ptr_cookie);
344 return 0;
347 *slot = XCNEW (struct ptr_data);
348 (*slot)->obj = obj;
349 (*slot)->note_ptr_fn = note_ptr_fn;
350 (*slot)->note_ptr_cookie = note_ptr_cookie;
351 if (note_ptr_fn == gt_pch_p_S)
352 (*slot)->size = strlen ((const char *)obj) + 1;
353 else
354 (*slot)->size = ggc_get_size (obj);
355 return 1;
358 /* Register an object in the hash table. */
360 void
361 gt_pch_note_reorder (void *obj, void *note_ptr_cookie,
362 gt_handle_reorder reorder_fn)
364 struct ptr_data *data;
366 if (obj == NULL || obj == (void *) 1)
367 return;
369 data = (struct ptr_data *)
370 saving_htab->find_with_hash (obj, POINTER_HASH (obj));
371 gcc_assert (data && data->note_ptr_cookie == note_ptr_cookie);
373 data->reorder_fn = reorder_fn;
376 /* Handy state for the traversal functions. */
378 struct traversal_state
380 FILE *f;
381 struct ggc_pch_data *d;
382 size_t count;
383 struct ptr_data **ptrs;
384 size_t ptrs_i;
387 /* Callbacks for htab_traverse. */
390 ggc_call_count (ptr_data **slot, traversal_state *state)
392 struct ptr_data *d = *slot;
394 ggc_pch_count_object (state->d, d->obj, d->size,
395 d->note_ptr_fn == gt_pch_p_S);
396 state->count++;
397 return 1;
401 ggc_call_alloc (ptr_data **slot, traversal_state *state)
403 struct ptr_data *d = *slot;
405 d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size,
406 d->note_ptr_fn == gt_pch_p_S);
407 state->ptrs[state->ptrs_i++] = d;
408 return 1;
411 /* Callback for qsort. */
413 static int
414 compare_ptr_data (const void *p1_p, const void *p2_p)
416 const struct ptr_data *const p1 = *(const struct ptr_data *const *)p1_p;
417 const struct ptr_data *const p2 = *(const struct ptr_data *const *)p2_p;
418 return (((size_t)p1->new_addr > (size_t)p2->new_addr)
419 - ((size_t)p1->new_addr < (size_t)p2->new_addr));
422 /* Callbacks for note_ptr_fn. */
424 static void
425 relocate_ptrs (void *ptr_p, void *state_p)
427 void **ptr = (void **)ptr_p;
428 struct traversal_state *state ATTRIBUTE_UNUSED
429 = (struct traversal_state *)state_p;
430 struct ptr_data *result;
432 if (*ptr == NULL || *ptr == (void *)1)
433 return;
435 result = (struct ptr_data *)
436 saving_htab->find_with_hash (*ptr, POINTER_HASH (*ptr));
437 gcc_assert (result);
438 *ptr = result->new_addr;
441 /* Write out, after relocation, the pointers in TAB. */
442 static void
443 write_pch_globals (const struct ggc_root_tab * const *tab,
444 struct traversal_state *state)
446 const struct ggc_root_tab *const *rt;
447 const struct ggc_root_tab *rti;
448 size_t i;
450 for (rt = tab; *rt; rt++)
451 for (rti = *rt; rti->base != NULL; rti++)
452 for (i = 0; i < rti->nelt; i++)
454 void *ptr = *(void **)((char *)rti->base + rti->stride * i);
455 struct ptr_data *new_ptr;
456 if (ptr == NULL || ptr == (void *)1)
458 if (fwrite (&ptr, sizeof (void *), 1, state->f)
459 != 1)
460 fatal_error ("can%'t write PCH file: %m");
462 else
464 new_ptr = (struct ptr_data *)
465 saving_htab->find_with_hash (ptr, POINTER_HASH (ptr));
466 if (fwrite (&new_ptr->new_addr, sizeof (void *), 1, state->f)
467 != 1)
468 fatal_error ("can%'t write PCH file: %m");
473 /* Hold the information we need to mmap the file back in. */
475 struct mmap_info
477 size_t offset;
478 size_t size;
479 void *preferred_base;
482 /* Write out the state of the compiler to F. */
484 void
485 gt_pch_save (FILE *f)
487 const struct ggc_root_tab *const *rt;
488 const struct ggc_root_tab *rti;
489 size_t i;
490 struct traversal_state state;
491 char *this_object = NULL;
492 size_t this_object_size = 0;
493 struct mmap_info mmi;
494 const size_t mmap_offset_alignment = host_hooks.gt_pch_alloc_granularity ();
496 gt_pch_save_stringpool ();
498 timevar_push (TV_PCH_PTR_REALLOC);
499 saving_htab = new hash_table<saving_hasher> (50000);
501 for (rt = gt_ggc_rtab; *rt; rt++)
502 for (rti = *rt; rti->base != NULL; rti++)
503 for (i = 0; i < rti->nelt; i++)
504 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
506 for (rt = gt_pch_cache_rtab; *rt; rt++)
507 for (rti = *rt; rti->base != NULL; rti++)
508 for (i = 0; i < rti->nelt; i++)
509 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
511 /* Prepare the objects for writing, determine addresses and such. */
512 state.f = f;
513 state.d = init_ggc_pch ();
514 state.count = 0;
515 saving_htab->traverse <traversal_state *, ggc_call_count> (&state);
517 mmi.size = ggc_pch_total_size (state.d);
519 /* Try to arrange things so that no relocation is necessary, but
520 don't try very hard. On most platforms, this will always work,
521 and on the rest it's a lot of work to do better.
522 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
523 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */
524 mmi.preferred_base = host_hooks.gt_pch_get_address (mmi.size, fileno (f));
526 ggc_pch_this_base (state.d, mmi.preferred_base);
528 state.ptrs = XNEWVEC (struct ptr_data *, state.count);
529 state.ptrs_i = 0;
531 saving_htab->traverse <traversal_state *, ggc_call_alloc> (&state);
532 timevar_pop (TV_PCH_PTR_REALLOC);
534 timevar_push (TV_PCH_PTR_SORT);
535 qsort (state.ptrs, state.count, sizeof (*state.ptrs), compare_ptr_data);
536 timevar_pop (TV_PCH_PTR_SORT);
538 /* Write out all the scalar variables. */
539 for (rt = gt_pch_scalar_rtab; *rt; rt++)
540 for (rti = *rt; rti->base != NULL; rti++)
541 if (fwrite (rti->base, rti->stride, 1, f) != 1)
542 fatal_error ("can%'t write PCH file: %m");
544 /* Write out all the global pointers, after translation. */
545 write_pch_globals (gt_ggc_rtab, &state);
546 write_pch_globals (gt_pch_cache_rtab, &state);
548 /* Pad the PCH file so that the mmapped area starts on an allocation
549 granularity (usually page) boundary. */
551 long o;
552 o = ftell (state.f) + sizeof (mmi);
553 if (o == -1)
554 fatal_error ("can%'t get position in PCH file: %m");
555 mmi.offset = mmap_offset_alignment - o % mmap_offset_alignment;
556 if (mmi.offset == mmap_offset_alignment)
557 mmi.offset = 0;
558 mmi.offset += o;
560 if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1)
561 fatal_error ("can%'t write PCH file: %m");
562 if (mmi.offset != 0
563 && fseek (state.f, mmi.offset, SEEK_SET) != 0)
564 fatal_error ("can%'t write padding to PCH file: %m");
566 ggc_pch_prepare_write (state.d, state.f);
568 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
569 vec<char> vbits = vNULL;
570 #endif
572 /* Actually write out the objects. */
573 for (i = 0; i < state.count; i++)
575 if (this_object_size < state.ptrs[i]->size)
577 this_object_size = state.ptrs[i]->size;
578 this_object = XRESIZEVAR (char, this_object, this_object_size);
580 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
581 /* obj might contain uninitialized bytes, e.g. in the trailing
582 padding of the object. Avoid warnings by making the memory
583 temporarily defined and then restoring previous state. */
584 int get_vbits = 0;
585 size_t valid_size = state.ptrs[i]->size;
586 if (__builtin_expect (RUNNING_ON_VALGRIND, 0))
588 if (vbits.length () < valid_size)
589 vbits.safe_grow (valid_size);
590 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
591 vbits.address (), valid_size);
592 if (get_vbits == 3)
594 /* We assume that first part of obj is addressable, and
595 the rest is unaddressable. Find out where the boundary is
596 using binary search. */
597 size_t lo = 0, hi = valid_size;
598 while (hi > lo)
600 size_t mid = (lo + hi) / 2;
601 get_vbits = VALGRIND_GET_VBITS ((char *) state.ptrs[i]->obj
602 + mid, vbits.address (),
604 if (get_vbits == 3)
605 hi = mid;
606 else if (get_vbits == 1)
607 lo = mid + 1;
608 else
609 break;
611 if (get_vbits == 1 || get_vbits == 3)
613 valid_size = lo;
614 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
615 vbits.address (),
616 valid_size);
619 if (get_vbits == 1)
620 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (state.ptrs[i]->obj,
621 state.ptrs[i]->size));
623 #endif
624 memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size);
625 if (state.ptrs[i]->reorder_fn != NULL)
626 state.ptrs[i]->reorder_fn (state.ptrs[i]->obj,
627 state.ptrs[i]->note_ptr_cookie,
628 relocate_ptrs, &state);
629 state.ptrs[i]->note_ptr_fn (state.ptrs[i]->obj,
630 state.ptrs[i]->note_ptr_cookie,
631 relocate_ptrs, &state);
632 ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj,
633 state.ptrs[i]->new_addr, state.ptrs[i]->size,
634 state.ptrs[i]->note_ptr_fn == gt_pch_p_S);
635 if (state.ptrs[i]->note_ptr_fn != gt_pch_p_S)
636 memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size);
637 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
638 if (__builtin_expect (get_vbits == 1, 0))
640 (void) VALGRIND_SET_VBITS (state.ptrs[i]->obj, vbits.address (),
641 valid_size);
642 if (valid_size != state.ptrs[i]->size)
643 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *)
644 state.ptrs[i]->obj
645 + valid_size,
646 state.ptrs[i]->size
647 - valid_size));
649 #endif
651 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
652 vbits.release ();
653 #endif
655 ggc_pch_finish (state.d, state.f);
656 gt_pch_fixup_stringpool ();
658 XDELETE (state.ptrs);
659 XDELETE (this_object);
660 delete saving_htab;
661 saving_htab = NULL;
664 /* Read the state of the compiler back in from F. */
666 void
667 gt_pch_restore (FILE *f)
669 const struct ggc_root_tab *const *rt;
670 const struct ggc_root_tab *rti;
671 size_t i;
672 struct mmap_info mmi;
673 int result;
675 /* Delete any deletable objects. This makes ggc_pch_read much
676 faster, as it can be sure that no GCable objects remain other
677 than the ones just read in. */
678 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
679 for (rti = *rt; rti->base != NULL; rti++)
680 memset (rti->base, 0, rti->stride);
682 /* Read in all the scalar variables. */
683 for (rt = gt_pch_scalar_rtab; *rt; rt++)
684 for (rti = *rt; rti->base != NULL; rti++)
685 if (fread (rti->base, rti->stride, 1, f) != 1)
686 fatal_error ("can%'t read PCH file: %m");
688 /* Read in all the global pointers, in 6 easy loops. */
689 for (rt = gt_ggc_rtab; *rt; rt++)
690 for (rti = *rt; rti->base != NULL; rti++)
691 for (i = 0; i < rti->nelt; i++)
692 if (fread ((char *)rti->base + rti->stride * i,
693 sizeof (void *), 1, f) != 1)
694 fatal_error ("can%'t read PCH file: %m");
696 for (rt = gt_pch_cache_rtab; *rt; rt++)
697 for (rti = *rt; rti->base != NULL; rti++)
698 for (i = 0; i < rti->nelt; i++)
699 if (fread ((char *)rti->base + rti->stride * i,
700 sizeof (void *), 1, f) != 1)
701 fatal_error ("can%'t read PCH file: %m");
703 if (fread (&mmi, sizeof (mmi), 1, f) != 1)
704 fatal_error ("can%'t read PCH file: %m");
706 result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size,
707 fileno (f), mmi.offset);
708 if (result < 0)
709 fatal_error ("had to relocate PCH");
710 if (result == 0)
712 if (fseek (f, mmi.offset, SEEK_SET) != 0
713 || fread (mmi.preferred_base, mmi.size, 1, f) != 1)
714 fatal_error ("can%'t read PCH file: %m");
716 else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0)
717 fatal_error ("can%'t read PCH file: %m");
719 ggc_pch_read (f, mmi.preferred_base);
721 gt_pch_restore_stringpool ();
724 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
725 Select no address whatsoever, and let gt_pch_save choose what it will with
726 malloc, presumably. */
728 void *
729 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED,
730 int fd ATTRIBUTE_UNUSED)
732 return NULL;
735 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
736 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
737 same as base, indicating that the memory has been allocated but needs to
738 be read in from the file. Return -1 if the address differs, to relocation
739 of the PCH file would be required. */
742 default_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED,
743 size_t offset ATTRIBUTE_UNUSED)
745 void *addr = xmalloc (size);
746 return (addr == base) - 1;
749 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the
750 alignment required for allocating virtual memory. Usually this is the
751 same as pagesize. */
753 size_t
754 default_gt_pch_alloc_granularity (void)
756 return getpagesize ();
759 #if HAVE_MMAP_FILE
760 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
761 We temporarily allocate SIZE bytes, and let the kernel place the data
762 wherever it will. If it worked, that's our spot, if not we're likely
763 to be in trouble. */
765 void *
766 mmap_gt_pch_get_address (size_t size, int fd)
768 void *ret;
770 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
771 if (ret == (void *) MAP_FAILED)
772 ret = NULL;
773 else
774 munmap ((caddr_t) ret, size);
776 return ret;
779 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
780 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
781 mapping the data at BASE, -1 if we couldn't.
783 This version assumes that the kernel honors the START operand of mmap
784 even without MAP_FIXED if START through START+SIZE are not currently
785 mapped with something. */
788 mmap_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
790 void *addr;
792 /* We're called with size == 0 if we're not planning to load a PCH
793 file at all. This allows the hook to free any static space that
794 we might have allocated at link time. */
795 if (size == 0)
796 return -1;
798 addr = mmap ((caddr_t) base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
799 fd, offset);
801 return addr == base ? 1 : -1;
803 #endif /* HAVE_MMAP_FILE */
805 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
807 /* Modify the bound based on rlimits. */
808 static double
809 ggc_rlimit_bound (double limit)
811 #if defined(HAVE_GETRLIMIT)
812 struct rlimit rlim;
813 # if defined (RLIMIT_AS)
814 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably
815 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */
816 if (getrlimit (RLIMIT_AS, &rlim) == 0
817 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
818 && rlim.rlim_cur < limit)
819 limit = rlim.rlim_cur;
820 # elif defined (RLIMIT_DATA)
821 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
822 might be on an OS that has a broken mmap. (Others don't bound
823 mmap at all, apparently.) */
824 if (getrlimit (RLIMIT_DATA, &rlim) == 0
825 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
826 && rlim.rlim_cur < limit
827 /* Darwin has this horribly bogus default setting of
828 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA
829 appears to be ignored. Ignore such silliness. If a limit
830 this small was actually effective for mmap, GCC wouldn't even
831 start up. */
832 && rlim.rlim_cur >= 8 * 1024 * 1024)
833 limit = rlim.rlim_cur;
834 # endif /* RLIMIT_AS or RLIMIT_DATA */
835 #endif /* HAVE_GETRLIMIT */
837 return limit;
840 /* Heuristic to set a default for GGC_MIN_EXPAND. */
841 static int
842 ggc_min_expand_heuristic (void)
844 double min_expand = physmem_total ();
846 /* Adjust for rlimits. */
847 min_expand = ggc_rlimit_bound (min_expand);
849 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
850 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
851 min_expand /= 1024*1024*1024;
852 min_expand *= 70;
853 min_expand = MIN (min_expand, 70);
854 min_expand += 30;
856 return min_expand;
859 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
860 static int
861 ggc_min_heapsize_heuristic (void)
863 double phys_kbytes = physmem_total ();
864 double limit_kbytes = ggc_rlimit_bound (phys_kbytes * 2);
866 phys_kbytes /= 1024; /* Convert to Kbytes. */
867 limit_kbytes /= 1024;
869 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
870 bound of 128M (when RAM >= 1GB). */
871 phys_kbytes /= 8;
873 #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
874 /* Try not to overrun the RSS limit while doing garbage collection.
875 The RSS limit is only advisory, so no margin is subtracted. */
877 struct rlimit rlim;
878 if (getrlimit (RLIMIT_RSS, &rlim) == 0
879 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
880 phys_kbytes = MIN (phys_kbytes, rlim.rlim_cur / 1024);
882 # endif
884 /* Don't blindly run over our data limit; do GC at least when the
885 *next* GC would be within 20Mb of the limit or within a quarter of
886 the limit, whichever is larger. If GCC does hit the data limit,
887 compilation will fail, so this tries to be conservative. */
888 limit_kbytes = MAX (0, limit_kbytes - MAX (limit_kbytes / 4, 20 * 1024));
889 limit_kbytes = (limit_kbytes * 100) / (110 + ggc_min_expand_heuristic ());
890 phys_kbytes = MIN (phys_kbytes, limit_kbytes);
892 phys_kbytes = MAX (phys_kbytes, 4 * 1024);
893 phys_kbytes = MIN (phys_kbytes, 128 * 1024);
895 return phys_kbytes;
897 #endif
899 void
900 init_ggc_heuristics (void)
902 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
903 set_default_param_value (GGC_MIN_EXPAND, ggc_min_expand_heuristic ());
904 set_default_param_value (GGC_MIN_HEAPSIZE, ggc_min_heapsize_heuristic ());
905 #endif
908 /* Datastructure used to store per-call-site statistics. */
909 struct ggc_loc_descriptor
911 const char *file;
912 int line;
913 const char *function;
914 int times;
915 size_t allocated;
916 size_t overhead;
917 size_t freed;
918 size_t collected;
921 /* Hash table helper. */
923 struct ggc_loc_desc_hasher : typed_noop_remove <ggc_loc_descriptor>
925 typedef ggc_loc_descriptor value_type;
926 typedef ggc_loc_descriptor compare_type;
927 static inline hashval_t hash (const value_type *);
928 static inline bool equal (const value_type *, const compare_type *);
931 inline hashval_t
932 ggc_loc_desc_hasher::hash (const value_type *d)
934 return htab_hash_pointer (d->function) | d->line;
937 inline bool
938 ggc_loc_desc_hasher::equal (const value_type *d, const compare_type *d2)
940 return (d->file == d2->file && d->line == d2->line
941 && d->function == d2->function);
944 /* Hashtable used for statistics. */
945 static hash_table<ggc_loc_desc_hasher> *loc_hash;
947 struct ggc_ptr_hash_entry
949 void *ptr;
950 struct ggc_loc_descriptor *loc;
951 size_t size;
954 /* Helper for ptr_hash table. */
956 struct ptr_hash_hasher : typed_noop_remove <ggc_ptr_hash_entry>
958 typedef ggc_ptr_hash_entry value_type;
959 typedef void compare_type;
960 static inline hashval_t hash (const value_type *);
961 static inline bool equal (const value_type *, const compare_type *);
964 inline hashval_t
965 ptr_hash_hasher::hash (const value_type *d)
967 return htab_hash_pointer (d->ptr);
970 inline bool
971 ptr_hash_hasher::equal (const value_type *p, const compare_type *p2)
973 return (p->ptr == p2);
976 /* Hashtable converting address of allocated field to loc descriptor. */
977 static hash_table<ptr_hash_hasher> *ptr_hash;
979 /* Return descriptor for given call site, create new one if needed. */
980 static struct ggc_loc_descriptor *
981 make_loc_descriptor (const char *name, int line, const char *function)
983 struct ggc_loc_descriptor loc;
984 struct ggc_loc_descriptor **slot;
986 loc.file = name;
987 loc.line = line;
988 loc.function = function;
989 if (!loc_hash)
990 loc_hash = new hash_table<ggc_loc_desc_hasher> (10);
992 slot = loc_hash->find_slot (&loc, INSERT);
993 if (*slot)
994 return *slot;
995 *slot = XCNEW (struct ggc_loc_descriptor);
996 (*slot)->file = name;
997 (*slot)->line = line;
998 (*slot)->function = function;
999 return *slot;
1002 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
1003 void
1004 ggc_record_overhead (size_t allocated, size_t overhead, void *ptr,
1005 const char *name, int line, const char *function)
1007 struct ggc_loc_descriptor *loc = make_loc_descriptor (name, line, function);
1008 struct ggc_ptr_hash_entry *p = XNEW (struct ggc_ptr_hash_entry);
1009 ggc_ptr_hash_entry **slot;
1011 p->ptr = ptr;
1012 p->loc = loc;
1013 p->size = allocated + overhead;
1014 if (!ptr_hash)
1015 ptr_hash = new hash_table<ptr_hash_hasher> (10);
1016 slot = ptr_hash->find_slot_with_hash (ptr, htab_hash_pointer (ptr), INSERT);
1017 gcc_assert (!*slot);
1018 *slot = p;
1020 loc->times++;
1021 loc->allocated+=allocated;
1022 loc->overhead+=overhead;
1025 /* Helper function for prune_overhead_list. See if SLOT is still marked and
1026 remove it from hashtable if it is not. */
1028 ggc_prune_ptr (ggc_ptr_hash_entry **slot, void *b ATTRIBUTE_UNUSED)
1030 struct ggc_ptr_hash_entry *p = *slot;
1031 if (!ggc_marked_p (p->ptr))
1033 p->loc->collected += p->size;
1034 ptr_hash->clear_slot (slot);
1035 free (p);
1037 return 1;
1040 /* After live values has been marked, walk all recorded pointers and see if
1041 they are still live. */
1042 void
1043 ggc_prune_overhead_list (void)
1045 ptr_hash->traverse <void *, ggc_prune_ptr> (NULL);
1048 /* Notice that the pointer has been freed. */
1049 void
1050 ggc_free_overhead (void *ptr)
1052 ggc_ptr_hash_entry **slot
1053 = ptr_hash->find_slot_with_hash (ptr, htab_hash_pointer (ptr), NO_INSERT);
1054 struct ggc_ptr_hash_entry *p;
1055 /* The pointer might be not found if a PCH read happened between allocation
1056 and ggc_free () call. FIXME: account memory properly in the presence of
1057 PCH. */
1058 if (!slot)
1059 return;
1060 p = (struct ggc_ptr_hash_entry *) *slot;
1061 p->loc->freed += p->size;
1062 ptr_hash->clear_slot (slot);
1063 free (p);
1066 /* Helper for qsort; sort descriptors by amount of memory consumed. */
1067 static int
1068 final_cmp_statistic (const void *loc1, const void *loc2)
1070 const struct ggc_loc_descriptor *const l1 =
1071 *(const struct ggc_loc_descriptor *const *) loc1;
1072 const struct ggc_loc_descriptor *const l2 =
1073 *(const struct ggc_loc_descriptor *const *) loc2;
1074 long diff;
1075 diff = ((long)(l1->allocated + l1->overhead - l1->freed) -
1076 (l2->allocated + l2->overhead - l2->freed));
1077 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
1080 /* Helper for qsort; sort descriptors by amount of memory consumed. */
1081 static int
1082 cmp_statistic (const void *loc1, const void *loc2)
1084 const struct ggc_loc_descriptor *const l1 =
1085 *(const struct ggc_loc_descriptor *const *) loc1;
1086 const struct ggc_loc_descriptor *const l2 =
1087 *(const struct ggc_loc_descriptor *const *) loc2;
1088 long diff;
1090 diff = ((long)(l1->allocated + l1->overhead - l1->freed - l1->collected) -
1091 (l2->allocated + l2->overhead - l2->freed - l2->collected));
1092 if (diff)
1093 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
1094 diff = ((long)(l1->allocated + l1->overhead - l1->freed) -
1095 (l2->allocated + l2->overhead - l2->freed));
1096 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
1099 /* Collect array of the descriptors from hashtable. */
1100 static struct ggc_loc_descriptor **loc_array;
1102 ggc_add_statistics (ggc_loc_descriptor **slot, int *n)
1104 loc_array[*n] = *slot;
1105 (*n)++;
1106 return 1;
1109 /* Dump per-site memory statistics. */
1111 void
1112 dump_ggc_loc_statistics (bool final)
1114 int nentries = 0;
1115 char s[4096];
1116 size_t collected = 0, freed = 0, allocated = 0, overhead = 0, times = 0;
1117 int i;
1119 if (! GATHER_STATISTICS)
1120 return;
1122 ggc_force_collect = true;
1123 ggc_collect ();
1125 loc_array = XCNEWVEC (struct ggc_loc_descriptor *,
1126 loc_hash->elements_with_deleted ());
1127 fprintf (stderr, "-------------------------------------------------------\n");
1128 fprintf (stderr, "\n%-48s %10s %10s %10s %10s %10s\n",
1129 "source location", "Garbage", "Freed", "Leak", "Overhead", "Times");
1130 fprintf (stderr, "-------------------------------------------------------\n");
1131 loc_hash->traverse <int *, ggc_add_statistics> (&nentries);
1132 qsort (loc_array, nentries, sizeof (*loc_array),
1133 final ? final_cmp_statistic : cmp_statistic);
1134 for (i = 0; i < nentries; i++)
1136 struct ggc_loc_descriptor *d = loc_array[i];
1137 allocated += d->allocated;
1138 times += d->times;
1139 freed += d->freed;
1140 collected += d->collected;
1141 overhead += d->overhead;
1143 for (i = 0; i < nentries; i++)
1145 struct ggc_loc_descriptor *d = loc_array[i];
1146 if (d->allocated)
1148 const char *s1 = d->file;
1149 const char *s2;
1150 while ((s2 = strstr (s1, "gcc/")))
1151 s1 = s2 + 4;
1152 sprintf (s, "%s:%i (%s)", s1, d->line, d->function);
1153 s[48] = 0;
1154 fprintf (stderr, "%-48s %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li\n", s,
1155 (long)d->collected,
1156 (d->collected) * 100.0 / collected,
1157 (long)d->freed,
1158 (d->freed) * 100.0 / freed,
1159 (long)(d->allocated + d->overhead - d->freed - d->collected),
1160 (d->allocated + d->overhead - d->freed - d->collected) * 100.0
1161 / (allocated + overhead - freed - collected),
1162 (long)d->overhead,
1163 d->overhead * 100.0 / overhead,
1164 (long)d->times);
1167 fprintf (stderr, "%-48s %10ld %10ld %10ld %10ld %10ld\n",
1168 "Total", (long)collected, (long)freed,
1169 (long)(allocated + overhead - freed - collected), (long)overhead,
1170 (long)times);
1171 fprintf (stderr, "%-48s %10s %10s %10s %10s %10s\n",
1172 "source location", "Garbage", "Freed", "Leak", "Overhead", "Times");
1173 fprintf (stderr, "-------------------------------------------------------\n");
1174 ggc_force_collect = false;