* config/sh/sh.c (push_regs): Emit movml for interrupt handler
[official-gcc.git] / gcc / ggc-common.c
blob5b66bb4fa5978d2a81a1b8abe90e0094d455867a
1 /* Simple garbage collection for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* Generic garbage collection (GC) functions and data, not specific to
22 any particular GC implementation. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "hashtab.h"
28 #include "ggc.h"
29 #include "ggc-internal.h"
30 #include "diagnostic-core.h"
31 #include "toplev.h"
32 #include "params.h"
33 #include "hosthooks.h"
34 #include "hosthooks-def.h"
35 #include "plugin.h"
36 #include "vec.h"
37 #include "timevar.h"
39 #ifdef HAVE_SYS_RESOURCE_H
40 # include <sys/resource.h>
41 #endif
43 #ifdef HAVE_MMAP_FILE
44 # include <sys/mman.h>
45 # ifdef HAVE_MINCORE
46 /* This is on Solaris. */
47 # include <sys/types.h>
48 # endif
49 #endif
51 #ifndef MAP_FAILED
52 # define MAP_FAILED ((void *)-1)
53 #endif
55 /* When set, ggc_collect will do collection. */
56 bool ggc_force_collect;
58 /* When true, protect the contents of the identifier hash table. */
59 bool ggc_protect_identifiers = true;
61 /* Statistics about the allocation. */
62 static ggc_statistics *ggc_stats;
64 struct traversal_state;
66 static int ggc_htab_delete (void **, void *);
67 static hashval_t saving_htab_hash (const void *);
68 static int saving_htab_eq (const void *, const void *);
69 static int call_count (void **, void *);
70 static int call_alloc (void **, void *);
71 static int compare_ptr_data (const void *, const void *);
72 static void relocate_ptrs (void *, void *);
73 static void write_pch_globals (const struct ggc_root_tab * const *tab,
74 struct traversal_state *state);
76 /* Maintain global roots that are preserved during GC. */
78 /* Process a slot of an htab by deleting it if it has not been marked. */
80 static int
81 ggc_htab_delete (void **slot, void *info)
83 const struct ggc_cache_tab *r = (const struct ggc_cache_tab *) info;
85 if (! (*r->marked_p) (*slot))
86 htab_clear_slot (*r->base, slot);
87 else
88 (*r->cb) (*slot);
90 return 1;
94 /* This extra vector of dynamically registered root_tab-s is used by
95 ggc_mark_roots and gives the ability to dynamically add new GGC root
96 tables, for instance from some plugins; this vector is on the heap
97 since it is used by GGC internally. */
98 typedef const struct ggc_root_tab *const_ggc_root_tab_t;
99 DEF_VEC_P(const_ggc_root_tab_t);
100 DEF_VEC_ALLOC_P(const_ggc_root_tab_t, heap);
101 static VEC(const_ggc_root_tab_t, heap) *extra_root_vec;
103 /* Dynamically register a new GGC root table RT. This is useful for
104 plugins. */
106 void
107 ggc_register_root_tab (const struct ggc_root_tab* rt)
109 if (rt)
110 VEC_safe_push (const_ggc_root_tab_t, heap, extra_root_vec, rt);
113 /* This extra vector of dynamically registered cache_tab-s is used by
114 ggc_mark_roots and gives the ability to dynamically add new GGC cache
115 tables, for instance from some plugins; this vector is on the heap
116 since it is used by GGC internally. */
117 typedef const struct ggc_cache_tab *const_ggc_cache_tab_t;
118 DEF_VEC_P(const_ggc_cache_tab_t);
119 DEF_VEC_ALLOC_P(const_ggc_cache_tab_t, heap);
120 static VEC(const_ggc_cache_tab_t, heap) *extra_cache_vec;
122 /* Dynamically register a new GGC cache table CT. This is useful for
123 plugins. */
125 void
126 ggc_register_cache_tab (const struct ggc_cache_tab* ct)
128 if (ct)
129 VEC_safe_push (const_ggc_cache_tab_t, heap, extra_cache_vec, ct);
132 /* Scan a hash table that has objects which are to be deleted if they are not
133 already marked. */
135 static void
136 ggc_scan_cache_tab (const_ggc_cache_tab_t ctp)
138 const struct ggc_cache_tab *cti;
140 for (cti = ctp; cti->base != NULL; cti++)
141 if (*cti->base)
143 ggc_set_mark (*cti->base);
144 htab_traverse_noresize (*cti->base, ggc_htab_delete,
145 CONST_CAST (void *, (const void *)cti));
146 ggc_set_mark ((*cti->base)->entries);
150 /* Mark all the roots in the table RT. */
152 static void
153 ggc_mark_root_tab (const_ggc_root_tab_t rt)
155 size_t i;
157 for ( ; rt->base != NULL; rt++)
158 for (i = 0; i < rt->nelt; i++)
159 (*rt->cb) (*(void **) ((char *)rt->base + rt->stride * i));
162 /* Iterate through all registered roots and mark each element. */
164 void
165 ggc_mark_roots (void)
167 const struct ggc_root_tab *const *rt;
168 const_ggc_root_tab_t rtp, rti;
169 const struct ggc_cache_tab *const *ct;
170 const_ggc_cache_tab_t ctp;
171 size_t i;
173 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
174 for (rti = *rt; rti->base != NULL; rti++)
175 memset (rti->base, 0, rti->stride);
177 for (rt = gt_ggc_rtab; *rt; rt++)
178 ggc_mark_root_tab (*rt);
180 FOR_EACH_VEC_ELT (const_ggc_root_tab_t, extra_root_vec, i, rtp)
181 ggc_mark_root_tab (rtp);
183 if (ggc_protect_identifiers)
184 ggc_mark_stringpool ();
186 /* Now scan all hash tables that have objects which are to be deleted if
187 they are not already marked. */
188 for (ct = gt_ggc_cache_rtab; *ct; ct++)
189 ggc_scan_cache_tab (*ct);
191 FOR_EACH_VEC_ELT (const_ggc_cache_tab_t, extra_cache_vec, i, ctp)
192 ggc_scan_cache_tab (ctp);
194 if (! ggc_protect_identifiers)
195 ggc_purge_stringpool ();
197 /* Some plugins may call ggc_set_mark from here. */
198 invoke_plugin_callbacks (PLUGIN_GGC_MARKING, NULL);
201 /* Allocate a block of memory, then clear it. */
202 void *
203 ggc_internal_cleared_alloc_stat (size_t size MEM_STAT_DECL)
205 void *buf = ggc_internal_alloc_stat (size PASS_MEM_STAT);
206 memset (buf, 0, size);
207 return buf;
210 /* Resize a block of memory, possibly re-allocating it. */
211 void *
212 ggc_realloc_stat (void *x, size_t size MEM_STAT_DECL)
214 void *r;
215 size_t old_size;
217 if (x == NULL)
218 return ggc_internal_alloc_stat (size PASS_MEM_STAT);
220 old_size = ggc_get_size (x);
222 if (size <= old_size)
224 /* Mark the unwanted memory as unaccessible. We also need to make
225 the "new" size accessible, since ggc_get_size returns the size of
226 the pool, not the size of the individually allocated object, the
227 size which was previously made accessible. Unfortunately, we
228 don't know that previously allocated size. Without that
229 knowledge we have to lose some initialization-tracking for the
230 old parts of the object. An alternative is to mark the whole
231 old_size as reachable, but that would lose tracking of writes
232 after the end of the object (by small offsets). Discard the
233 handle to avoid handle leak. */
234 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x + size,
235 old_size - size));
236 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, size));
237 return x;
240 r = ggc_internal_alloc_stat (size PASS_MEM_STAT);
242 /* Since ggc_get_size returns the size of the pool, not the size of the
243 individually allocated object, we'd access parts of the old object
244 that were marked invalid with the memcpy below. We lose a bit of the
245 initialization-tracking since some of it may be uninitialized. */
246 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, old_size));
248 memcpy (r, x, old_size);
250 /* The old object is not supposed to be used anymore. */
251 ggc_free (x);
253 return r;
256 void *
257 ggc_cleared_alloc_htab_ignore_args (size_t c ATTRIBUTE_UNUSED,
258 size_t n ATTRIBUTE_UNUSED)
260 gcc_assert (c * n == sizeof (struct htab));
261 return ggc_alloc_cleared_htab ();
264 /* TODO: once we actually use type information in GGC, create a new tag
265 gt_gcc_ptr_array and use it for pointer arrays. */
266 void *
267 ggc_cleared_alloc_ptr_array_two_args (size_t c, size_t n)
269 gcc_assert (sizeof (PTR *) == n);
270 return ggc_internal_cleared_vec_alloc (sizeof (PTR *), c);
273 /* These are for splay_tree_new_ggc. */
274 void *
275 ggc_splay_alloc (enum gt_types_enum obj_type ATTRIBUTE_UNUSED, int sz,
276 void *nl)
278 gcc_assert (!nl);
279 return ggc_internal_alloc (sz);
282 void
283 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl)
285 gcc_assert (!nl);
288 /* Print statistics that are independent of the collector in use. */
289 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
290 ? (x) \
291 : ((x) < 1024*1024*10 \
292 ? (x) / 1024 \
293 : (x) / (1024*1024))))
294 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
296 void
297 ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED,
298 ggc_statistics *stats)
300 /* Set the pointer so that during collection we will actually gather
301 the statistics. */
302 ggc_stats = stats;
304 /* Then do one collection to fill in the statistics. */
305 ggc_collect ();
307 /* At present, we don't really gather any interesting statistics. */
309 /* Don't gather statistics any more. */
310 ggc_stats = NULL;
313 /* Functions for saving and restoring GCable memory to disk. */
315 static htab_t saving_htab;
317 struct ptr_data
319 void *obj;
320 void *note_ptr_cookie;
321 gt_note_pointers note_ptr_fn;
322 gt_handle_reorder reorder_fn;
323 size_t size;
324 void *new_addr;
325 enum gt_types_enum type;
328 #define POINTER_HASH(x) (hashval_t)((long)x >> 3)
330 /* Register an object in the hash table. */
333 gt_pch_note_object (void *obj, void *note_ptr_cookie,
334 gt_note_pointers note_ptr_fn,
335 enum gt_types_enum type)
337 struct ptr_data **slot;
339 if (obj == NULL || obj == (void *) 1)
340 return 0;
342 slot = (struct ptr_data **)
343 htab_find_slot_with_hash (saving_htab, obj, POINTER_HASH (obj),
344 INSERT);
345 if (*slot != NULL)
347 gcc_assert ((*slot)->note_ptr_fn == note_ptr_fn
348 && (*slot)->note_ptr_cookie == note_ptr_cookie);
349 return 0;
352 *slot = XCNEW (struct ptr_data);
353 (*slot)->obj = obj;
354 (*slot)->note_ptr_fn = note_ptr_fn;
355 (*slot)->note_ptr_cookie = note_ptr_cookie;
356 if (note_ptr_fn == gt_pch_p_S)
357 (*slot)->size = strlen ((const char *)obj) + 1;
358 else
359 (*slot)->size = ggc_get_size (obj);
360 (*slot)->type = type;
361 return 1;
364 /* Register an object in the hash table. */
366 void
367 gt_pch_note_reorder (void *obj, void *note_ptr_cookie,
368 gt_handle_reorder reorder_fn)
370 struct ptr_data *data;
372 if (obj == NULL || obj == (void *) 1)
373 return;
375 data = (struct ptr_data *)
376 htab_find_with_hash (saving_htab, obj, POINTER_HASH (obj));
377 gcc_assert (data && data->note_ptr_cookie == note_ptr_cookie);
379 data->reorder_fn = reorder_fn;
382 /* Hash and equality functions for saving_htab, callbacks for htab_create. */
384 static hashval_t
385 saving_htab_hash (const void *p)
387 return POINTER_HASH (((const struct ptr_data *)p)->obj);
390 static int
391 saving_htab_eq (const void *p1, const void *p2)
393 return ((const struct ptr_data *)p1)->obj == p2;
396 /* Handy state for the traversal functions. */
398 struct traversal_state
400 FILE *f;
401 struct ggc_pch_data *d;
402 size_t count;
403 struct ptr_data **ptrs;
404 size_t ptrs_i;
407 /* Callbacks for htab_traverse. */
409 static int
410 call_count (void **slot, void *state_p)
412 struct ptr_data *d = (struct ptr_data *)*slot;
413 struct traversal_state *state = (struct traversal_state *)state_p;
415 ggc_pch_count_object (state->d, d->obj, d->size,
416 d->note_ptr_fn == gt_pch_p_S,
417 d->type);
418 state->count++;
419 return 1;
422 static int
423 call_alloc (void **slot, void *state_p)
425 struct ptr_data *d = (struct ptr_data *)*slot;
426 struct traversal_state *state = (struct traversal_state *)state_p;
428 d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size,
429 d->note_ptr_fn == gt_pch_p_S,
430 d->type);
431 state->ptrs[state->ptrs_i++] = d;
432 return 1;
435 /* Callback for qsort. */
437 static int
438 compare_ptr_data (const void *p1_p, const void *p2_p)
440 const struct ptr_data *const p1 = *(const struct ptr_data *const *)p1_p;
441 const struct ptr_data *const p2 = *(const struct ptr_data *const *)p2_p;
442 return (((size_t)p1->new_addr > (size_t)p2->new_addr)
443 - ((size_t)p1->new_addr < (size_t)p2->new_addr));
446 /* Callbacks for note_ptr_fn. */
448 static void
449 relocate_ptrs (void *ptr_p, void *state_p)
451 void **ptr = (void **)ptr_p;
452 struct traversal_state *state ATTRIBUTE_UNUSED
453 = (struct traversal_state *)state_p;
454 struct ptr_data *result;
456 if (*ptr == NULL || *ptr == (void *)1)
457 return;
459 result = (struct ptr_data *)
460 htab_find_with_hash (saving_htab, *ptr, POINTER_HASH (*ptr));
461 gcc_assert (result);
462 *ptr = result->new_addr;
465 /* Write out, after relocation, the pointers in TAB. */
466 static void
467 write_pch_globals (const struct ggc_root_tab * const *tab,
468 struct traversal_state *state)
470 const struct ggc_root_tab *const *rt;
471 const struct ggc_root_tab *rti;
472 size_t i;
474 for (rt = tab; *rt; rt++)
475 for (rti = *rt; rti->base != NULL; rti++)
476 for (i = 0; i < rti->nelt; i++)
478 void *ptr = *(void **)((char *)rti->base + rti->stride * i);
479 struct ptr_data *new_ptr;
480 if (ptr == NULL || ptr == (void *)1)
482 if (fwrite (&ptr, sizeof (void *), 1, state->f)
483 != 1)
484 fatal_error ("can't write PCH file: %m");
486 else
488 new_ptr = (struct ptr_data *)
489 htab_find_with_hash (saving_htab, ptr, POINTER_HASH (ptr));
490 if (fwrite (&new_ptr->new_addr, sizeof (void *), 1, state->f)
491 != 1)
492 fatal_error ("can't write PCH file: %m");
497 /* Hold the information we need to mmap the file back in. */
499 struct mmap_info
501 size_t offset;
502 size_t size;
503 void *preferred_base;
506 /* Write out the state of the compiler to F. */
508 void
509 gt_pch_save (FILE *f)
511 const struct ggc_root_tab *const *rt;
512 const struct ggc_root_tab *rti;
513 size_t i;
514 struct traversal_state state;
515 char *this_object = NULL;
516 size_t this_object_size = 0;
517 struct mmap_info mmi;
518 const size_t mmap_offset_alignment = host_hooks.gt_pch_alloc_granularity();
520 gt_pch_save_stringpool ();
522 timevar_push (TV_PCH_PTR_REALLOC);
523 saving_htab = htab_create (50000, saving_htab_hash, saving_htab_eq, free);
525 for (rt = gt_ggc_rtab; *rt; rt++)
526 for (rti = *rt; rti->base != NULL; rti++)
527 for (i = 0; i < rti->nelt; i++)
528 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
530 for (rt = gt_pch_cache_rtab; *rt; rt++)
531 for (rti = *rt; rti->base != NULL; rti++)
532 for (i = 0; i < rti->nelt; i++)
533 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
535 /* Prepare the objects for writing, determine addresses and such. */
536 state.f = f;
537 state.d = init_ggc_pch ();
538 state.count = 0;
539 htab_traverse (saving_htab, call_count, &state);
541 mmi.size = ggc_pch_total_size (state.d);
543 /* Try to arrange things so that no relocation is necessary, but
544 don't try very hard. On most platforms, this will always work,
545 and on the rest it's a lot of work to do better.
546 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
547 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */
548 mmi.preferred_base = host_hooks.gt_pch_get_address (mmi.size, fileno (f));
550 ggc_pch_this_base (state.d, mmi.preferred_base);
552 state.ptrs = XNEWVEC (struct ptr_data *, state.count);
553 state.ptrs_i = 0;
555 htab_traverse (saving_htab, call_alloc, &state);
556 timevar_pop (TV_PCH_PTR_REALLOC);
558 timevar_push (TV_PCH_PTR_SORT);
559 qsort (state.ptrs, state.count, sizeof (*state.ptrs), compare_ptr_data);
560 timevar_pop (TV_PCH_PTR_SORT);
562 /* Write out all the scalar variables. */
563 for (rt = gt_pch_scalar_rtab; *rt; rt++)
564 for (rti = *rt; rti->base != NULL; rti++)
565 if (fwrite (rti->base, rti->stride, 1, f) != 1)
566 fatal_error ("can't write PCH file: %m");
568 /* Write out all the global pointers, after translation. */
569 write_pch_globals (gt_ggc_rtab, &state);
570 write_pch_globals (gt_pch_cache_rtab, &state);
572 /* Pad the PCH file so that the mmapped area starts on an allocation
573 granularity (usually page) boundary. */
575 long o;
576 o = ftell (state.f) + sizeof (mmi);
577 if (o == -1)
578 fatal_error ("can't get position in PCH file: %m");
579 mmi.offset = mmap_offset_alignment - o % mmap_offset_alignment;
580 if (mmi.offset == mmap_offset_alignment)
581 mmi.offset = 0;
582 mmi.offset += o;
584 if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1)
585 fatal_error ("can't write PCH file: %m");
586 if (mmi.offset != 0
587 && fseek (state.f, mmi.offset, SEEK_SET) != 0)
588 fatal_error ("can't write padding to PCH file: %m");
590 ggc_pch_prepare_write (state.d, state.f);
592 /* Actually write out the objects. */
593 for (i = 0; i < state.count; i++)
595 if (this_object_size < state.ptrs[i]->size)
597 this_object_size = state.ptrs[i]->size;
598 this_object = XRESIZEVAR (char, this_object, this_object_size);
600 memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size);
601 if (state.ptrs[i]->reorder_fn != NULL)
602 state.ptrs[i]->reorder_fn (state.ptrs[i]->obj,
603 state.ptrs[i]->note_ptr_cookie,
604 relocate_ptrs, &state);
605 state.ptrs[i]->note_ptr_fn (state.ptrs[i]->obj,
606 state.ptrs[i]->note_ptr_cookie,
607 relocate_ptrs, &state);
608 ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj,
609 state.ptrs[i]->new_addr, state.ptrs[i]->size,
610 state.ptrs[i]->note_ptr_fn == gt_pch_p_S);
611 if (state.ptrs[i]->note_ptr_fn != gt_pch_p_S)
612 memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size);
614 ggc_pch_finish (state.d, state.f);
615 gt_pch_fixup_stringpool ();
617 free (state.ptrs);
618 htab_delete (saving_htab);
621 /* Read the state of the compiler back in from F. */
623 void
624 gt_pch_restore (FILE *f)
626 const struct ggc_root_tab *const *rt;
627 const struct ggc_root_tab *rti;
628 size_t i;
629 struct mmap_info mmi;
630 int result;
632 /* Delete any deletable objects. This makes ggc_pch_read much
633 faster, as it can be sure that no GCable objects remain other
634 than the ones just read in. */
635 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
636 for (rti = *rt; rti->base != NULL; rti++)
637 memset (rti->base, 0, rti->stride);
639 /* Read in all the scalar variables. */
640 for (rt = gt_pch_scalar_rtab; *rt; rt++)
641 for (rti = *rt; rti->base != NULL; rti++)
642 if (fread (rti->base, rti->stride, 1, f) != 1)
643 fatal_error ("can't read PCH file: %m");
645 /* Read in all the global pointers, in 6 easy loops. */
646 for (rt = gt_ggc_rtab; *rt; rt++)
647 for (rti = *rt; rti->base != NULL; rti++)
648 for (i = 0; i < rti->nelt; i++)
649 if (fread ((char *)rti->base + rti->stride * i,
650 sizeof (void *), 1, f) != 1)
651 fatal_error ("can't read PCH file: %m");
653 for (rt = gt_pch_cache_rtab; *rt; rt++)
654 for (rti = *rt; rti->base != NULL; rti++)
655 for (i = 0; i < rti->nelt; i++)
656 if (fread ((char *)rti->base + rti->stride * i,
657 sizeof (void *), 1, f) != 1)
658 fatal_error ("can't read PCH file: %m");
660 if (fread (&mmi, sizeof (mmi), 1, f) != 1)
661 fatal_error ("can't read PCH file: %m");
663 result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size,
664 fileno (f), mmi.offset);
665 if (result < 0)
666 fatal_error ("had to relocate PCH");
667 if (result == 0)
669 if (fseek (f, mmi.offset, SEEK_SET) != 0
670 || fread (mmi.preferred_base, mmi.size, 1, f) != 1)
671 fatal_error ("can't read PCH file: %m");
673 else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0)
674 fatal_error ("can't read PCH file: %m");
676 ggc_pch_read (f, mmi.preferred_base);
678 gt_pch_restore_stringpool ();
681 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
682 Select no address whatsoever, and let gt_pch_save choose what it will with
683 malloc, presumably. */
685 void *
686 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED,
687 int fd ATTRIBUTE_UNUSED)
689 return NULL;
692 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
693 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
694 same as base, indicating that the memory has been allocated but needs to
695 be read in from the file. Return -1 if the address differs, to relocation
696 of the PCH file would be required. */
699 default_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED,
700 size_t offset ATTRIBUTE_UNUSED)
702 void *addr = xmalloc (size);
703 return (addr == base) - 1;
706 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the
707 alignment required for allocating virtual memory. Usually this is the
708 same as pagesize. */
710 size_t
711 default_gt_pch_alloc_granularity (void)
713 return getpagesize();
716 #if HAVE_MMAP_FILE
717 /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
718 We temporarily allocate SIZE bytes, and let the kernel place the data
719 wherever it will. If it worked, that's our spot, if not we're likely
720 to be in trouble. */
722 void *
723 mmap_gt_pch_get_address (size_t size, int fd)
725 void *ret;
727 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
728 if (ret == (void *) MAP_FAILED)
729 ret = NULL;
730 else
731 munmap ((caddr_t) ret, size);
733 return ret;
736 /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
737 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
738 mapping the data at BASE, -1 if we couldn't.
740 This version assumes that the kernel honors the START operand of mmap
741 even without MAP_FIXED if START through START+SIZE are not currently
742 mapped with something. */
745 mmap_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
747 void *addr;
749 /* We're called with size == 0 if we're not planning to load a PCH
750 file at all. This allows the hook to free any static space that
751 we might have allocated at link time. */
752 if (size == 0)
753 return -1;
755 addr = mmap ((caddr_t) base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
756 fd, offset);
758 return addr == base ? 1 : -1;
760 #endif /* HAVE_MMAP_FILE */
762 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
764 /* Modify the bound based on rlimits. */
765 static double
766 ggc_rlimit_bound (double limit)
768 #if defined(HAVE_GETRLIMIT)
769 struct rlimit rlim;
770 # if defined (RLIMIT_AS)
771 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably
772 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */
773 if (getrlimit (RLIMIT_AS, &rlim) == 0
774 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
775 && rlim.rlim_cur < limit)
776 limit = rlim.rlim_cur;
777 # elif defined (RLIMIT_DATA)
778 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
779 might be on an OS that has a broken mmap. (Others don't bound
780 mmap at all, apparently.) */
781 if (getrlimit (RLIMIT_DATA, &rlim) == 0
782 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
783 && rlim.rlim_cur < limit
784 /* Darwin has this horribly bogus default setting of
785 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA
786 appears to be ignored. Ignore such silliness. If a limit
787 this small was actually effective for mmap, GCC wouldn't even
788 start up. */
789 && rlim.rlim_cur >= 8 * 1024 * 1024)
790 limit = rlim.rlim_cur;
791 # endif /* RLIMIT_AS or RLIMIT_DATA */
792 #endif /* HAVE_GETRLIMIT */
794 return limit;
797 /* Heuristic to set a default for GGC_MIN_EXPAND. */
798 static int
799 ggc_min_expand_heuristic (void)
801 double min_expand = physmem_total();
803 /* Adjust for rlimits. */
804 min_expand = ggc_rlimit_bound (min_expand);
806 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
807 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
808 min_expand /= 1024*1024*1024;
809 min_expand *= 70;
810 min_expand = MIN (min_expand, 70);
811 min_expand += 30;
813 return min_expand;
816 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
817 static int
818 ggc_min_heapsize_heuristic (void)
820 double phys_kbytes = physmem_total();
821 double limit_kbytes = ggc_rlimit_bound (phys_kbytes * 2);
823 phys_kbytes /= 1024; /* Convert to Kbytes. */
824 limit_kbytes /= 1024;
826 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
827 bound of 128M (when RAM >= 1GB). */
828 phys_kbytes /= 8;
830 #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
831 /* Try not to overrun the RSS limit while doing garbage collection.
832 The RSS limit is only advisory, so no margin is subtracted. */
834 struct rlimit rlim;
835 if (getrlimit (RLIMIT_RSS, &rlim) == 0
836 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
837 phys_kbytes = MIN (phys_kbytes, rlim.rlim_cur / 1024);
839 # endif
841 /* Don't blindly run over our data limit; do GC at least when the
842 *next* GC would be within 20Mb of the limit or within a quarter of
843 the limit, whichever is larger. If GCC does hit the data limit,
844 compilation will fail, so this tries to be conservative. */
845 limit_kbytes = MAX (0, limit_kbytes - MAX (limit_kbytes / 4, 20 * 1024));
846 limit_kbytes = (limit_kbytes * 100) / (110 + ggc_min_expand_heuristic ());
847 phys_kbytes = MIN (phys_kbytes, limit_kbytes);
849 phys_kbytes = MAX (phys_kbytes, 4 * 1024);
850 phys_kbytes = MIN (phys_kbytes, 128 * 1024);
852 return phys_kbytes;
854 #endif
856 void
857 init_ggc_heuristics (void)
859 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
860 set_param_value ("ggc-min-expand", ggc_min_expand_heuristic ());
861 set_param_value ("ggc-min-heapsize", ggc_min_heapsize_heuristic ());
862 #endif
865 #ifdef GATHER_STATISTICS
867 /* Datastructure used to store per-call-site statistics. */
868 struct loc_descriptor
870 const char *file;
871 int line;
872 const char *function;
873 int times;
874 size_t allocated;
875 size_t overhead;
876 size_t freed;
877 size_t collected;
880 /* Hashtable used for statistics. */
881 static htab_t loc_hash;
883 /* Hash table helpers functions. */
884 static hashval_t
885 hash_descriptor (const void *p)
887 const struct loc_descriptor *const d = (const struct loc_descriptor *) p;
889 return htab_hash_pointer (d->function) | d->line;
892 static int
893 eq_descriptor (const void *p1, const void *p2)
895 const struct loc_descriptor *const d = (const struct loc_descriptor *) p1;
896 const struct loc_descriptor *const d2 = (const struct loc_descriptor *) p2;
898 return (d->file == d2->file && d->line == d2->line
899 && d->function == d2->function);
902 /* Hashtable converting address of allocated field to loc descriptor. */
903 static htab_t ptr_hash;
904 struct ptr_hash_entry
906 void *ptr;
907 struct loc_descriptor *loc;
908 size_t size;
911 /* Hash table helpers functions. */
912 static hashval_t
913 hash_ptr (const void *p)
915 const struct ptr_hash_entry *const d = (const struct ptr_hash_entry *) p;
917 return htab_hash_pointer (d->ptr);
920 static int
921 eq_ptr (const void *p1, const void *p2)
923 const struct ptr_hash_entry *const p = (const struct ptr_hash_entry *) p1;
925 return (p->ptr == p2);
928 /* Return descriptor for given call site, create new one if needed. */
929 static struct loc_descriptor *
930 loc_descriptor (const char *name, int line, const char *function)
932 struct loc_descriptor loc;
933 struct loc_descriptor **slot;
935 loc.file = name;
936 loc.line = line;
937 loc.function = function;
938 if (!loc_hash)
939 loc_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
941 slot = (struct loc_descriptor **) htab_find_slot (loc_hash, &loc, INSERT);
942 if (*slot)
943 return *slot;
944 *slot = XCNEW (struct loc_descriptor);
945 (*slot)->file = name;
946 (*slot)->line = line;
947 (*slot)->function = function;
948 return *slot;
951 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
952 void
953 ggc_record_overhead (size_t allocated, size_t overhead, void *ptr,
954 const char *name, int line, const char *function)
956 struct loc_descriptor *loc = loc_descriptor (name, line, function);
957 struct ptr_hash_entry *p = XNEW (struct ptr_hash_entry);
958 PTR *slot;
960 p->ptr = ptr;
961 p->loc = loc;
962 p->size = allocated + overhead;
963 if (!ptr_hash)
964 ptr_hash = htab_create (10, hash_ptr, eq_ptr, NULL);
965 slot = htab_find_slot_with_hash (ptr_hash, ptr, htab_hash_pointer (ptr), INSERT);
966 gcc_assert (!*slot);
967 *slot = p;
969 loc->times++;
970 loc->allocated+=allocated;
971 loc->overhead+=overhead;
974 /* Helper function for prune_overhead_list. See if SLOT is still marked and
975 remove it from hashtable if it is not. */
976 static int
977 ggc_prune_ptr (void **slot, void *b ATTRIBUTE_UNUSED)
979 struct ptr_hash_entry *p = (struct ptr_hash_entry *) *slot;
980 if (!ggc_marked_p (p->ptr))
982 p->loc->collected += p->size;
983 htab_clear_slot (ptr_hash, slot);
984 free (p);
986 return 1;
989 /* After live values has been marked, walk all recorded pointers and see if
990 they are still live. */
991 void
992 ggc_prune_overhead_list (void)
994 htab_traverse (ptr_hash, ggc_prune_ptr, NULL);
997 /* Notice that the pointer has been freed. */
998 void
999 ggc_free_overhead (void *ptr)
1001 PTR *slot = htab_find_slot_with_hash (ptr_hash, ptr, htab_hash_pointer (ptr),
1002 NO_INSERT);
1003 struct ptr_hash_entry *p;
1004 /* The pointer might be not found if a PCH read happened between allocation
1005 and ggc_free () call. FIXME: account memory properly in the presence of
1006 PCH. */
1007 if (!slot)
1008 return;
1009 p = (struct ptr_hash_entry *) *slot;
1010 p->loc->freed += p->size;
1011 htab_clear_slot (ptr_hash, slot);
1012 free (p);
1015 /* Helper for qsort; sort descriptors by amount of memory consumed. */
1016 static int
1017 final_cmp_statistic (const void *loc1, const void *loc2)
1019 const struct loc_descriptor *const l1 =
1020 *(const struct loc_descriptor *const *) loc1;
1021 const struct loc_descriptor *const l2 =
1022 *(const struct loc_descriptor *const *) loc2;
1023 long diff;
1024 diff = ((long)(l1->allocated + l1->overhead - l1->freed) -
1025 (l2->allocated + l2->overhead - l2->freed));
1026 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
1029 /* Helper for qsort; sort descriptors by amount of memory consumed. */
1030 static int
1031 cmp_statistic (const void *loc1, const void *loc2)
1033 const struct loc_descriptor *const l1 =
1034 *(const struct loc_descriptor *const *) loc1;
1035 const struct loc_descriptor *const l2 =
1036 *(const struct loc_descriptor *const *) loc2;
1037 long diff;
1039 diff = ((long)(l1->allocated + l1->overhead - l1->freed - l1->collected) -
1040 (l2->allocated + l2->overhead - l2->freed - l2->collected));
1041 if (diff)
1042 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
1043 diff = ((long)(l1->allocated + l1->overhead - l1->freed) -
1044 (l2->allocated + l2->overhead - l2->freed));
1045 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
1048 /* Collect array of the descriptors from hashtable. */
1049 static struct loc_descriptor **loc_array;
1050 static int
1051 add_statistics (void **slot, void *b)
1053 int *n = (int *)b;
1054 loc_array[*n] = (struct loc_descriptor *) *slot;
1055 (*n)++;
1056 return 1;
1059 /* Dump per-site memory statistics. */
1060 #endif
1061 void
1062 dump_ggc_loc_statistics (bool final ATTRIBUTE_UNUSED)
1064 #ifdef GATHER_STATISTICS
1065 int nentries = 0;
1066 char s[4096];
1067 size_t collected = 0, freed = 0, allocated = 0, overhead = 0, times = 0;
1068 int i;
1070 ggc_force_collect = true;
1071 ggc_collect ();
1073 loc_array = XCNEWVEC (struct loc_descriptor *, loc_hash->n_elements);
1074 fprintf (stderr, "-------------------------------------------------------\n");
1075 fprintf (stderr, "\n%-48s %10s %10s %10s %10s %10s\n",
1076 "source location", "Garbage", "Freed", "Leak", "Overhead", "Times");
1077 fprintf (stderr, "-------------------------------------------------------\n");
1078 htab_traverse (loc_hash, add_statistics, &nentries);
1079 qsort (loc_array, nentries, sizeof (*loc_array),
1080 final ? final_cmp_statistic : cmp_statistic);
1081 for (i = 0; i < nentries; i++)
1083 struct loc_descriptor *d = loc_array[i];
1084 allocated += d->allocated;
1085 times += d->times;
1086 freed += d->freed;
1087 collected += d->collected;
1088 overhead += d->overhead;
1090 for (i = 0; i < nentries; i++)
1092 struct loc_descriptor *d = loc_array[i];
1093 if (d->allocated)
1095 const char *s1 = d->file;
1096 const char *s2;
1097 while ((s2 = strstr (s1, "gcc/")))
1098 s1 = s2 + 4;
1099 sprintf (s, "%s:%i (%s)", s1, d->line, d->function);
1100 s[48] = 0;
1101 fprintf (stderr, "%-48s %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li\n", s,
1102 (long)d->collected,
1103 (d->collected) * 100.0 / collected,
1104 (long)d->freed,
1105 (d->freed) * 100.0 / freed,
1106 (long)(d->allocated + d->overhead - d->freed - d->collected),
1107 (d->allocated + d->overhead - d->freed - d->collected) * 100.0
1108 / (allocated + overhead - freed - collected),
1109 (long)d->overhead,
1110 d->overhead * 100.0 / overhead,
1111 (long)d->times);
1114 fprintf (stderr, "%-48s %10ld %10ld %10ld %10ld %10ld\n",
1115 "Total", (long)collected, (long)freed,
1116 (long)(allocated + overhead - freed - collected), (long)overhead,
1117 (long)times);
1118 fprintf (stderr, "%-48s %10s %10s %10s %10s %10s\n",
1119 "source location", "Garbage", "Freed", "Leak", "Overhead", "Times");
1120 fprintf (stderr, "-------------------------------------------------------\n");
1121 ggc_force_collect = false;
1122 #endif