Check int_size_in_bytes in ix86_return_in_memory
[official-gcc.git] / gcc / ggc-common.c
blob5096837df272defb2a10b721e9186853eb6e87a4
1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 "ggc-internal.h"
27 #include "diagnostic-core.h"
28 #include "params.h"
29 #include "hosthooks.h"
30 #include "hosthooks-def.h"
31 #include "plugin.h"
32 #include "timevar.h"
34 /* When set, ggc_collect will do collection. */
35 bool ggc_force_collect;
37 /* When true, protect the contents of the identifier hash table. */
38 bool ggc_protect_identifiers = true;
40 /* Statistics about the allocation. */
41 static ggc_statistics *ggc_stats;
43 struct traversal_state;
45 static int compare_ptr_data (const void *, const void *);
46 static void relocate_ptrs (void *, void *);
47 static void write_pch_globals (const struct ggc_root_tab * const *tab,
48 struct traversal_state *state);
50 /* Maintain global roots that are preserved during GC. */
52 /* This extra vector of dynamically registered root_tab-s is used by
53 ggc_mark_roots and gives the ability to dynamically add new GGC root
54 tables, for instance from some plugins; this vector is on the heap
55 since it is used by GGC internally. */
56 typedef const struct ggc_root_tab *const_ggc_root_tab_t;
57 static vec<const_ggc_root_tab_t> extra_root_vec;
59 /* Dynamically register a new GGC root table RT. This is useful for
60 plugins. */
62 void
63 ggc_register_root_tab (const struct ggc_root_tab* rt)
65 if (rt)
66 extra_root_vec.safe_push (rt);
69 /* Mark all the roots in the table RT. */
71 static void
72 ggc_mark_root_tab (const_ggc_root_tab_t rt)
74 size_t i;
76 for ( ; rt->base != NULL; rt++)
77 for (i = 0; i < rt->nelt; i++)
78 (*rt->cb) (*(void **) ((char *)rt->base + rt->stride * i));
81 /* Iterate through all registered roots and mark each element. */
83 void
84 ggc_mark_roots (void)
86 const struct ggc_root_tab *const *rt;
87 const_ggc_root_tab_t rtp, rti;
88 size_t i;
90 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
91 for (rti = *rt; rti->base != NULL; rti++)
92 memset (rti->base, 0, rti->stride);
94 for (rt = gt_ggc_rtab; *rt; rt++)
95 ggc_mark_root_tab (*rt);
97 FOR_EACH_VEC_ELT (extra_root_vec, i, rtp)
98 ggc_mark_root_tab (rtp);
100 if (ggc_protect_identifiers)
101 ggc_mark_stringpool ();
103 gt_clear_caches ();
105 if (! ggc_protect_identifiers)
106 ggc_purge_stringpool ();
108 /* Some plugins may call ggc_set_mark from here. */
109 invoke_plugin_callbacks (PLUGIN_GGC_MARKING, NULL);
112 /* Allocate a block of memory, then clear it. */
113 void *
114 ggc_internal_cleared_alloc (size_t size, void (*f)(void *), size_t s, size_t n
115 MEM_STAT_DECL)
117 void *buf = ggc_internal_alloc (size, f, s, n PASS_MEM_STAT);
118 memset (buf, 0, size);
119 return buf;
122 /* Resize a block of memory, possibly re-allocating it. */
123 void *
124 ggc_realloc (void *x, size_t size MEM_STAT_DECL)
126 void *r;
127 size_t old_size;
129 if (x == NULL)
130 return ggc_internal_alloc (size PASS_MEM_STAT);
132 old_size = ggc_get_size (x);
134 if (size <= old_size)
136 /* Mark the unwanted memory as unaccessible. We also need to make
137 the "new" size accessible, since ggc_get_size returns the size of
138 the pool, not the size of the individually allocated object, the
139 size which was previously made accessible. Unfortunately, we
140 don't know that previously allocated size. Without that
141 knowledge we have to lose some initialization-tracking for the
142 old parts of the object. An alternative is to mark the whole
143 old_size as reachable, but that would lose tracking of writes
144 after the end of the object (by small offsets). Discard the
145 handle to avoid handle leak. */
146 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x + size,
147 old_size - size));
148 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, size));
149 return x;
152 r = ggc_internal_alloc (size PASS_MEM_STAT);
154 /* Since ggc_get_size returns the size of the pool, not the size of the
155 individually allocated object, we'd access parts of the old object
156 that were marked invalid with the memcpy below. We lose a bit of the
157 initialization-tracking since some of it may be uninitialized. */
158 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, old_size));
160 memcpy (r, x, old_size);
162 /* The old object is not supposed to be used anymore. */
163 ggc_free (x);
165 return r;
168 void *
169 ggc_cleared_alloc_htab_ignore_args (size_t c ATTRIBUTE_UNUSED,
170 size_t n ATTRIBUTE_UNUSED)
172 gcc_assert (c * n == sizeof (struct htab));
173 return ggc_cleared_alloc<htab> ();
176 /* TODO: once we actually use type information in GGC, create a new tag
177 gt_gcc_ptr_array and use it for pointer arrays. */
178 void *
179 ggc_cleared_alloc_ptr_array_two_args (size_t c, size_t n)
181 gcc_assert (sizeof (PTR *) == n);
182 return ggc_cleared_vec_alloc<PTR *> (c);
185 /* These are for splay_tree_new_ggc. */
186 void *
187 ggc_splay_alloc (int sz, void *nl)
189 gcc_assert (!nl);
190 return ggc_internal_alloc (sz);
193 void
194 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl)
196 gcc_assert (!nl);
199 /* Print statistics that are independent of the collector in use. */
200 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
201 ? (x) \
202 : ((x) < 1024*1024*10 \
203 ? (x) / 1024 \
204 : (x) / (1024*1024))))
205 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
207 void
208 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED,
209 ggc_statistics *stats)
211 /* Set the pointer so that during collection we will actually gather
212 the statistics. */
213 ggc_stats = stats;
215 /* Then do one collection to fill in the statistics. */
216 ggc_collect ();
218 /* At present, we don't really gather any interesting statistics. */
220 /* Don't gather statistics any more. */
221 ggc_stats = NULL;
224 /* Functions for saving and restoring GCable memory to disk. */
226 struct ptr_data
228 void *obj;
229 void *note_ptr_cookie;
230 gt_note_pointers note_ptr_fn;
231 gt_handle_reorder reorder_fn;
232 size_t size;
233 void *new_addr;
236 #define POINTER_HASH(x) (hashval_t)((intptr_t)x >> 3)
238 /* Helper for hashing saving_htab. */
240 struct saving_hasher : free_ptr_hash <ptr_data>
242 typedef void *compare_type;
243 static inline hashval_t hash (const ptr_data *);
244 static inline bool equal (const ptr_data *, const void *);
247 inline hashval_t
248 saving_hasher::hash (const ptr_data *p)
250 return POINTER_HASH (p->obj);
253 inline bool
254 saving_hasher::equal (const ptr_data *p1, const void *p2)
256 return p1->obj == p2;
259 static hash_table<saving_hasher> *saving_htab;
261 /* Register an object in the hash table. */
264 gt_pch_note_object (void *obj, void *note_ptr_cookie,
265 gt_note_pointers note_ptr_fn)
267 struct ptr_data **slot;
269 if (obj == NULL || obj == (void *) 1)
270 return 0;
272 slot = (struct ptr_data **)
273 saving_htab->find_slot_with_hash (obj, POINTER_HASH (obj), INSERT);
274 if (*slot != NULL)
276 gcc_assert ((*slot)->note_ptr_fn == note_ptr_fn
277 && (*slot)->note_ptr_cookie == note_ptr_cookie);
278 return 0;
281 *slot = XCNEW (struct ptr_data);
282 (*slot)->obj = obj;
283 (*slot)->note_ptr_fn = note_ptr_fn;
284 (*slot)->note_ptr_cookie = note_ptr_cookie;
285 if (note_ptr_fn == gt_pch_p_S)
286 (*slot)->size = strlen ((const char *)obj) + 1;
287 else
288 (*slot)->size = ggc_get_size (obj);
289 return 1;
292 /* Register an object in the hash table. */
294 void
295 gt_pch_note_reorder (void *obj, void *note_ptr_cookie,
296 gt_handle_reorder reorder_fn)
298 struct ptr_data *data;
300 if (obj == NULL || obj == (void *) 1)
301 return;
303 data = (struct ptr_data *)
304 saving_htab->find_with_hash (obj, POINTER_HASH (obj));
305 gcc_assert (data && data->note_ptr_cookie == note_ptr_cookie);
307 data->reorder_fn = reorder_fn;
310 /* Handy state for the traversal functions. */
312 struct traversal_state
314 FILE *f;
315 struct ggc_pch_data *d;
316 size_t count;
317 struct ptr_data **ptrs;
318 size_t ptrs_i;
321 /* Callbacks for htab_traverse. */
324 ggc_call_count (ptr_data **slot, traversal_state *state)
326 struct ptr_data *d = *slot;
328 ggc_pch_count_object (state->d, d->obj, d->size,
329 d->note_ptr_fn == gt_pch_p_S);
330 state->count++;
331 return 1;
335 ggc_call_alloc (ptr_data **slot, traversal_state *state)
337 struct ptr_data *d = *slot;
339 d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size,
340 d->note_ptr_fn == gt_pch_p_S);
341 state->ptrs[state->ptrs_i++] = d;
342 return 1;
345 /* Callback for qsort. */
347 static int
348 compare_ptr_data (const void *p1_p, const void *p2_p)
350 const struct ptr_data *const p1 = *(const struct ptr_data *const *)p1_p;
351 const struct ptr_data *const p2 = *(const struct ptr_data *const *)p2_p;
352 return (((size_t)p1->new_addr > (size_t)p2->new_addr)
353 - ((size_t)p1->new_addr < (size_t)p2->new_addr));
356 /* Callbacks for note_ptr_fn. */
358 static void
359 relocate_ptrs (void *ptr_p, void *state_p)
361 void **ptr = (void **)ptr_p;
362 struct traversal_state *state ATTRIBUTE_UNUSED
363 = (struct traversal_state *)state_p;
364 struct ptr_data *result;
366 if (*ptr == NULL || *ptr == (void *)1)
367 return;
369 result = (struct ptr_data *)
370 saving_htab->find_with_hash (*ptr, POINTER_HASH (*ptr));
371 gcc_assert (result);
372 *ptr = result->new_addr;
375 /* Write out, after relocation, the pointers in TAB. */
376 static void
377 write_pch_globals (const struct ggc_root_tab * const *tab,
378 struct traversal_state *state)
380 const struct ggc_root_tab *const *rt;
381 const struct ggc_root_tab *rti;
382 size_t i;
384 for (rt = tab; *rt; rt++)
385 for (rti = *rt; rti->base != NULL; rti++)
386 for (i = 0; i < rti->nelt; i++)
388 void *ptr = *(void **)((char *)rti->base + rti->stride * i);
389 struct ptr_data *new_ptr;
390 if (ptr == NULL || ptr == (void *)1)
392 if (fwrite (&ptr, sizeof (void *), 1, state->f)
393 != 1)
394 fatal_error (input_location, "can%'t write PCH file: %m");
396 else
398 new_ptr = (struct ptr_data *)
399 saving_htab->find_with_hash (ptr, POINTER_HASH (ptr));
400 if (fwrite (&new_ptr->new_addr, sizeof (void *), 1, state->f)
401 != 1)
402 fatal_error (input_location, "can%'t write PCH file: %m");
407 /* Hold the information we need to mmap the file back in. */
409 struct mmap_info
411 size_t offset;
412 size_t size;
413 void *preferred_base;
416 /* Write out the state of the compiler to F. */
418 void
419 gt_pch_save (FILE *f)
421 const struct ggc_root_tab *const *rt;
422 const struct ggc_root_tab *rti;
423 size_t i;
424 struct traversal_state state;
425 char *this_object = NULL;
426 size_t this_object_size = 0;
427 struct mmap_info mmi;
428 const size_t mmap_offset_alignment = host_hooks.gt_pch_alloc_granularity ();
430 gt_pch_save_stringpool ();
432 timevar_push (TV_PCH_PTR_REALLOC);
433 saving_htab = new hash_table<saving_hasher> (50000);
435 for (rt = gt_ggc_rtab; *rt; rt++)
436 for (rti = *rt; rti->base != NULL; rti++)
437 for (i = 0; i < rti->nelt; i++)
438 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
440 /* Prepare the objects for writing, determine addresses and such. */
441 state.f = f;
442 state.d = init_ggc_pch ();
443 state.count = 0;
444 saving_htab->traverse <traversal_state *, ggc_call_count> (&state);
446 mmi.size = ggc_pch_total_size (state.d);
448 /* Try to arrange things so that no relocation is necessary, but
449 don't try very hard. On most platforms, this will always work,
450 and on the rest it's a lot of work to do better.
451 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
452 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */
453 mmi.preferred_base = host_hooks.gt_pch_get_address (mmi.size, fileno (f));
455 ggc_pch_this_base (state.d, mmi.preferred_base);
457 state.ptrs = XNEWVEC (struct ptr_data *, state.count);
458 state.ptrs_i = 0;
460 saving_htab->traverse <traversal_state *, ggc_call_alloc> (&state);
461 timevar_pop (TV_PCH_PTR_REALLOC);
463 timevar_push (TV_PCH_PTR_SORT);
464 qsort (state.ptrs, state.count, sizeof (*state.ptrs), compare_ptr_data);
465 timevar_pop (TV_PCH_PTR_SORT);
467 /* Write out all the scalar variables. */
468 for (rt = gt_pch_scalar_rtab; *rt; rt++)
469 for (rti = *rt; rti->base != NULL; rti++)
470 if (fwrite (rti->base, rti->stride, 1, f) != 1)
471 fatal_error (input_location, "can%'t write PCH file: %m");
473 /* Write out all the global pointers, after translation. */
474 write_pch_globals (gt_ggc_rtab, &state);
476 /* Pad the PCH file so that the mmapped area starts on an allocation
477 granularity (usually page) boundary. */
479 long o;
480 o = ftell (state.f) + sizeof (mmi);
481 if (o == -1)
482 fatal_error (input_location, "can%'t get position in PCH file: %m");
483 mmi.offset = mmap_offset_alignment - o % mmap_offset_alignment;
484 if (mmi.offset == mmap_offset_alignment)
485 mmi.offset = 0;
486 mmi.offset += o;
488 if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1)
489 fatal_error (input_location, "can%'t write PCH file: %m");
490 if (mmi.offset != 0
491 && fseek (state.f, mmi.offset, SEEK_SET) != 0)
492 fatal_error (input_location, "can%'t write padding to PCH file: %m");
494 ggc_pch_prepare_write (state.d, state.f);
496 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
497 vec<char> vbits = vNULL;
498 #endif
500 /* Actually write out the objects. */
501 for (i = 0; i < state.count; i++)
503 if (this_object_size < state.ptrs[i]->size)
505 this_object_size = state.ptrs[i]->size;
506 this_object = XRESIZEVAR (char, this_object, this_object_size);
508 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
509 /* obj might contain uninitialized bytes, e.g. in the trailing
510 padding of the object. Avoid warnings by making the memory
511 temporarily defined and then restoring previous state. */
512 int get_vbits = 0;
513 size_t valid_size = state.ptrs[i]->size;
514 if (__builtin_expect (RUNNING_ON_VALGRIND, 0))
516 if (vbits.length () < valid_size)
517 vbits.safe_grow (valid_size);
518 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
519 vbits.address (), valid_size);
520 if (get_vbits == 3)
522 /* We assume that first part of obj is addressable, and
523 the rest is unaddressable. Find out where the boundary is
524 using binary search. */
525 size_t lo = 0, hi = valid_size;
526 while (hi > lo)
528 size_t mid = (lo + hi) / 2;
529 get_vbits = VALGRIND_GET_VBITS ((char *) state.ptrs[i]->obj
530 + mid, vbits.address (),
532 if (get_vbits == 3)
533 hi = mid;
534 else if (get_vbits == 1)
535 lo = mid + 1;
536 else
537 break;
539 if (get_vbits == 1 || get_vbits == 3)
541 valid_size = lo;
542 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
543 vbits.address (),
544 valid_size);
547 if (get_vbits == 1)
548 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (state.ptrs[i]->obj,
549 state.ptrs[i]->size));
551 #endif
552 memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size);
553 if (state.ptrs[i]->reorder_fn != NULL)
554 state.ptrs[i]->reorder_fn (state.ptrs[i]->obj,
555 state.ptrs[i]->note_ptr_cookie,
556 relocate_ptrs, &state);
557 state.ptrs[i]->note_ptr_fn (state.ptrs[i]->obj,
558 state.ptrs[i]->note_ptr_cookie,
559 relocate_ptrs, &state);
560 ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj,
561 state.ptrs[i]->new_addr, state.ptrs[i]->size,
562 state.ptrs[i]->note_ptr_fn == gt_pch_p_S);
563 if (state.ptrs[i]->note_ptr_fn != gt_pch_p_S)
564 memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size);
565 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
566 if (__builtin_expect (get_vbits == 1, 0))
568 (void) VALGRIND_SET_VBITS (state.ptrs[i]->obj, vbits.address (),
569 valid_size);
570 if (valid_size != state.ptrs[i]->size)
571 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *)
572 state.ptrs[i]->obj
573 + valid_size,
574 state.ptrs[i]->size
575 - valid_size));
577 #endif
579 #if defined ENABLE_VALGRIND_CHECKING && defined VALGRIND_GET_VBITS
580 vbits.release ();
581 #endif
583 ggc_pch_finish (state.d, state.f);
584 gt_pch_fixup_stringpool ();
586 XDELETE (state.ptrs);
587 XDELETE (this_object);
588 delete saving_htab;
589 saving_htab = NULL;
592 /* Read the state of the compiler back in from F. */
594 void
595 gt_pch_restore (FILE *f)
597 const struct ggc_root_tab *const *rt;
598 const struct ggc_root_tab *rti;
599 size_t i;
600 struct mmap_info mmi;
601 int result;
603 /* Delete any deletable objects. This makes ggc_pch_read much
604 faster, as it can be sure that no GCable objects remain other
605 than the ones just read in. */
606 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
607 for (rti = *rt; rti->base != NULL; rti++)
608 memset (rti->base, 0, rti->stride);
610 /* Read in all the scalar variables. */
611 for (rt = gt_pch_scalar_rtab; *rt; rt++)
612 for (rti = *rt; rti->base != NULL; rti++)
613 if (fread (rti->base, rti->stride, 1, f) != 1)
614 fatal_error (input_location, "can%'t read PCH file: %m");
616 /* Read in all the global pointers, in 6 easy loops. */
617 for (rt = gt_ggc_rtab; *rt; rt++)
618 for (rti = *rt; rti->base != NULL; rti++)
619 for (i = 0; i < rti->nelt; i++)
620 if (fread ((char *)rti->base + rti->stride * i,
621 sizeof (void *), 1, f) != 1)
622 fatal_error (input_location, "can%'t read PCH file: %m");
624 if (fread (&mmi, sizeof (mmi), 1, f) != 1)
625 fatal_error (input_location, "can%'t read PCH file: %m");
627 result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size,
628 fileno (f), mmi.offset);
629 if (result < 0)
630 fatal_error (input_location, "had to relocate PCH");
631 if (result == 0)
633 if (fseek (f, mmi.offset, SEEK_SET) != 0
634 || fread (mmi.preferred_base, mmi.size, 1, f) != 1)
635 fatal_error (input_location, "can%'t read PCH file: %m");
637 else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0)
638 fatal_error (input_location, "can%'t read PCH file: %m");
640 ggc_pch_read (f, mmi.preferred_base);
642 gt_pch_restore_stringpool ();
645 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
646 Select no address whatsoever, and let gt_pch_save choose what it will with
647 malloc, presumably. */
649 void *
650 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED,
651 int fd ATTRIBUTE_UNUSED)
653 return NULL;
656 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
657 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
658 same as base, indicating that the memory has been allocated but needs to
659 be read in from the file. Return -1 if the address differs, to relocation
660 of the PCH file would be required. */
663 default_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED,
664 size_t offset ATTRIBUTE_UNUSED)
666 void *addr = xmalloc (size);
667 return (addr == base) - 1;
670 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the
671 alignment required for allocating virtual memory. Usually this is the
672 same as pagesize. */
674 size_t
675 default_gt_pch_alloc_granularity (void)
677 return getpagesize ();
680 #if HAVE_MMAP_FILE
681 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
682 We temporarily allocate SIZE bytes, and let the kernel place the data
683 wherever it will. If it worked, that's our spot, if not we're likely
684 to be in trouble. */
686 void *
687 mmap_gt_pch_get_address (size_t size, int fd)
689 void *ret;
691 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
692 if (ret == (void *) MAP_FAILED)
693 ret = NULL;
694 else
695 munmap ((caddr_t) ret, size);
697 return ret;
700 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
701 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
702 mapping the data at BASE, -1 if we couldn't.
704 This version assumes that the kernel honors the START operand of mmap
705 even without MAP_FIXED if START through START+SIZE are not currently
706 mapped with something. */
709 mmap_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
711 void *addr;
713 /* We're called with size == 0 if we're not planning to load a PCH
714 file at all. This allows the hook to free any static space that
715 we might have allocated at link time. */
716 if (size == 0)
717 return -1;
719 addr = mmap ((caddr_t) base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
720 fd, offset);
722 return addr == base ? 1 : -1;
724 #endif /* HAVE_MMAP_FILE */
726 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
728 /* Modify the bound based on rlimits. */
729 static double
730 ggc_rlimit_bound (double limit)
732 #if defined(HAVE_GETRLIMIT)
733 struct rlimit rlim;
734 # if defined (RLIMIT_AS)
735 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably
736 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */
737 if (getrlimit (RLIMIT_AS, &rlim) == 0
738 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
739 && rlim.rlim_cur < limit)
740 limit = rlim.rlim_cur;
741 # elif defined (RLIMIT_DATA)
742 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
743 might be on an OS that has a broken mmap. (Others don't bound
744 mmap at all, apparently.) */
745 if (getrlimit (RLIMIT_DATA, &rlim) == 0
746 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
747 && rlim.rlim_cur < limit
748 /* Darwin has this horribly bogus default setting of
749 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA
750 appears to be ignored. Ignore such silliness. If a limit
751 this small was actually effective for mmap, GCC wouldn't even
752 start up. */
753 && rlim.rlim_cur >= 8 * 1024 * 1024)
754 limit = rlim.rlim_cur;
755 # endif /* RLIMIT_AS or RLIMIT_DATA */
756 #endif /* HAVE_GETRLIMIT */
758 return limit;
761 /* Heuristic to set a default for GGC_MIN_EXPAND. */
762 static int
763 ggc_min_expand_heuristic (void)
765 double min_expand = physmem_total ();
767 /* Adjust for rlimits. */
768 min_expand = ggc_rlimit_bound (min_expand);
770 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
771 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
772 min_expand /= 1024*1024*1024;
773 min_expand *= 70;
774 min_expand = MIN (min_expand, 70);
775 min_expand += 30;
777 return min_expand;
780 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
781 static int
782 ggc_min_heapsize_heuristic (void)
784 double phys_kbytes = physmem_total ();
785 double limit_kbytes = ggc_rlimit_bound (phys_kbytes * 2);
787 phys_kbytes /= 1024; /* Convert to Kbytes. */
788 limit_kbytes /= 1024;
790 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
791 bound of 128M (when RAM >= 1GB). */
792 phys_kbytes /= 8;
794 #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
795 /* Try not to overrun the RSS limit while doing garbage collection.
796 The RSS limit is only advisory, so no margin is subtracted. */
798 struct rlimit rlim;
799 if (getrlimit (RLIMIT_RSS, &rlim) == 0
800 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
801 phys_kbytes = MIN (phys_kbytes, rlim.rlim_cur / 1024);
803 # endif
805 /* Don't blindly run over our data limit; do GC at least when the
806 *next* GC would be within 20Mb of the limit or within a quarter of
807 the limit, whichever is larger. If GCC does hit the data limit,
808 compilation will fail, so this tries to be conservative. */
809 limit_kbytes = MAX (0, limit_kbytes - MAX (limit_kbytes / 4, 20 * 1024));
810 limit_kbytes = (limit_kbytes * 100) / (110 + ggc_min_expand_heuristic ());
811 phys_kbytes = MIN (phys_kbytes, limit_kbytes);
813 phys_kbytes = MAX (phys_kbytes, 4 * 1024);
814 phys_kbytes = MIN (phys_kbytes, 128 * 1024);
816 return phys_kbytes;
818 #endif
820 void
821 init_ggc_heuristics (void)
823 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
824 set_default_param_value (GGC_MIN_EXPAND, ggc_min_expand_heuristic ());
825 set_default_param_value (GGC_MIN_HEAPSIZE, ggc_min_heapsize_heuristic ());
826 #endif
829 /* GGC memory usage. */
830 struct ggc_usage: public mem_usage
832 /* Default constructor. */
833 ggc_usage (): m_freed (0), m_collected (0), m_overhead (0) {}
834 /* Constructor. */
835 ggc_usage (size_t allocated, size_t times, size_t peak,
836 size_t freed, size_t collected, size_t overhead)
837 : mem_usage (allocated, times, peak),
838 m_freed (freed), m_collected (collected), m_overhead (overhead) {}
840 /* Comparison operator. */
841 inline bool
842 operator< (const ggc_usage &second) const
844 return (get_balance () == second.get_balance () ?
845 (m_peak == second.m_peak ? m_times < second.m_times
846 : m_peak < second.m_peak)
847 : get_balance () < second.get_balance ());
850 /* Register overhead of ALLOCATED and OVERHEAD bytes. */
851 inline void
852 register_overhead (size_t allocated, size_t overhead)
854 m_allocated += allocated;
855 m_overhead += overhead;
856 m_times++;
859 /* Release overhead of SIZE bytes. */
860 inline void
861 release_overhead (size_t size)
863 m_freed += size;
866 /* Sum the usage with SECOND usage. */
867 ggc_usage
868 operator+ (const ggc_usage &second)
870 return ggc_usage (m_allocated + second.m_allocated,
871 m_times + second.m_times,
872 m_peak + second.m_peak,
873 m_freed + second.m_freed,
874 m_collected + second.m_collected,
875 m_overhead + second.m_overhead);
878 /* Dump usage with PREFIX, where TOTAL is sum of all rows. */
879 inline void
880 dump (const char *prefix, ggc_usage &total) const
882 long balance = get_balance ();
883 fprintf (stderr,
884 "%-48s %10li:%5.1f%%%10li:%5.1f%%"
885 "%10li:%5.1f%%%10li:%5.1f%%%10li\n",
886 prefix, (long)m_collected,
887 get_percent (m_collected, total.m_collected),
888 (long)m_freed, get_percent (m_freed, total.m_freed),
889 (long)balance, get_percent (balance, total.get_balance ()),
890 (long)m_overhead, get_percent (m_overhead, total.m_overhead),
891 (long)m_times);
894 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
895 inline void
896 dump (mem_location *loc, ggc_usage &total) const
898 char *location_string = loc->to_string ();
900 dump (location_string, total);
902 free (location_string);
905 /* Dump footer. */
906 inline void
907 dump_footer ()
909 print_dash_line ();
910 dump ("Total", *this);
911 print_dash_line ();
914 /* Get balance which is GGC allocation leak. */
915 inline long
916 get_balance () const
918 return m_allocated + m_overhead - m_collected - m_freed;
921 typedef std::pair<mem_location *, ggc_usage *> mem_pair_t;
923 /* Compare wrapper used by qsort method. */
924 static int
925 compare (const void *first, const void *second)
927 const mem_pair_t f = *(const mem_pair_t *)first;
928 const mem_pair_t s = *(const mem_pair_t *)second;
930 return (*f.second) < (*s.second);
933 /* Compare rows in final GGC summary dump. */
934 static int
935 compare_final (const void *first, const void *second)
937 typedef std::pair<mem_location *, ggc_usage *> mem_pair_t;
939 const ggc_usage *f = ((const mem_pair_t *)first)->second;
940 const ggc_usage *s = ((const mem_pair_t *)second)->second;
942 size_t a = f->m_allocated + f->m_overhead - f->m_freed;
943 size_t b = s->m_allocated + s->m_overhead - s->m_freed;
945 return a == b ? 0 : (a < b ? 1 : -1);
948 /* Dump header with NAME. */
949 static inline void
950 dump_header (const char *name)
952 fprintf (stderr, "%-48s %11s%17s%17s%16s%17s\n", name, "Garbage", "Freed",
953 "Leak", "Overhead", "Times");
954 print_dash_line ();
957 /* Freed memory in bytes. */
958 size_t m_freed;
959 /* Collected memory in bytes. */
960 size_t m_collected;
961 /* Overhead memory in bytes. */
962 size_t m_overhead;
965 /* GCC memory description. */
966 static mem_alloc_description<ggc_usage> ggc_mem_desc;
968 /* Dump per-site memory statistics. */
970 void
971 dump_ggc_loc_statistics (bool final)
973 if (! GATHER_STATISTICS)
974 return;
976 ggc_force_collect = true;
977 ggc_collect ();
979 ggc_mem_desc.dump (GGC_ORIGIN, final ? ggc_usage::compare_final : NULL);
981 ggc_force_collect = false;
984 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
985 void
986 ggc_record_overhead (size_t allocated, size_t overhead, void *ptr MEM_STAT_DECL)
988 ggc_usage *usage = ggc_mem_desc.register_descriptor (ptr, GGC_ORIGIN, false
989 FINAL_PASS_MEM_STAT);
991 ggc_mem_desc.register_object_overhead (usage, allocated + overhead, ptr);
992 usage->register_overhead (allocated, overhead);
995 /* Notice that the pointer has been freed. */
996 void
997 ggc_free_overhead (void *ptr)
999 ggc_mem_desc.release_object_overhead (ptr);
1002 /* After live values has been marked, walk all recorded pointers and see if
1003 they are still live. */
1004 void
1005 ggc_prune_overhead_list (void)
1007 typedef hash_map<const void *, std::pair<ggc_usage *, size_t > > map_t;
1009 map_t::iterator it = ggc_mem_desc.m_reverse_object_map->begin ();
1011 for (; it != ggc_mem_desc.m_reverse_object_map->end (); ++it)
1012 if (!ggc_marked_p ((*it).first))
1013 (*it).second.first->m_collected += (*it).second.second;
1015 delete ggc_mem_desc.m_reverse_object_map;
1016 ggc_mem_desc.m_reverse_object_map = new map_t (13, false, false);