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
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
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. */
26 #include "coretypes.h"
29 #include "ggc-internal.h"
30 #include "diagnostic-core.h"
33 #include "hosthooks.h"
34 #include "hosthooks-def.h"
39 #ifdef HAVE_SYS_RESOURCE_H
40 # include <sys/resource.h>
44 # include <sys/mman.h>
46 /* This is on Solaris. */
47 # include <sys/types.h>
52 # define MAP_FAILED ((void *)-1)
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. */
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
);
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
107 ggc_register_root_tab (const struct ggc_root_tab
* 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
126 ggc_register_cache_tab (const struct ggc_cache_tab
* 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
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
++)
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. */
153 ggc_mark_root_tab (const_ggc_root_tab_t rt
)
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. */
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
;
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. */
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
);
210 /* Resize a block of memory, possibly re-allocating it. */
212 ggc_realloc_stat (void *x
, size_t size MEM_STAT_DECL
)
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
,
236 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x
, size
));
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. */
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. */
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. */
275 ggc_splay_alloc (enum gt_types_enum obj_type ATTRIBUTE_UNUSED
, int sz
,
279 return ggc_internal_alloc (sz
);
283 ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED
, void *nl
)
288 /* Print statistics that are independent of the collector in use. */
289 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
291 : ((x) < 1024*1024*10 \
293 : (x) / (1024*1024))))
294 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
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
304 /* Then do one collection to fill in the statistics. */
307 /* At present, we don't really gather any interesting statistics. */
309 /* Don't gather statistics any more. */
313 /* Functions for saving and restoring GCable memory to disk. */
315 static htab_t saving_htab
;
320 void *note_ptr_cookie
;
321 gt_note_pointers note_ptr_fn
;
322 gt_handle_reorder reorder_fn
;
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)
342 slot
= (struct ptr_data
**)
343 htab_find_slot_with_hash (saving_htab
, obj
, POINTER_HASH (obj
),
347 gcc_assert ((*slot
)->note_ptr_fn
== note_ptr_fn
348 && (*slot
)->note_ptr_cookie
== note_ptr_cookie
);
352 *slot
= XCNEW (struct ptr_data
);
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;
359 (*slot
)->size
= ggc_get_size (obj
);
360 (*slot
)->type
= type
;
364 /* Register an object in the hash table. */
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)
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. */
385 saving_htab_hash (const void *p
)
387 return POINTER_HASH (((const struct ptr_data
*)p
)->obj
);
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
401 struct ggc_pch_data
*d
;
403 struct ptr_data
**ptrs
;
407 /* Callbacks for htab_traverse. */
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
,
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
,
431 state
->ptrs
[state
->ptrs_i
++] = d
;
435 /* Callback for qsort. */
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. */
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)
459 result
= (struct ptr_data
*)
460 htab_find_with_hash (saving_htab
, *ptr
, POINTER_HASH (*ptr
));
462 *ptr
= result
->new_addr
;
465 /* Write out, after relocation, the pointers in TAB. */
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
;
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
)
484 fatal_error ("can't write PCH file: %m");
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
)
492 fatal_error ("can't write PCH file: %m");
497 /* Hold the information we need to mmap the file back in. */
503 void *preferred_base
;
506 /* Write out the state of the compiler to F. */
509 gt_pch_save (FILE *f
)
511 const struct ggc_root_tab
*const *rt
;
512 const struct ggc_root_tab
*rti
;
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. */
537 state
.d
= init_ggc_pch ();
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
);
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. */
576 o
= ftell (state
.f
) + sizeof (mmi
);
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
)
584 if (fwrite (&mmi
, sizeof (mmi
), 1, state
.f
) != 1)
585 fatal_error ("can't write PCH file: %m");
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 ();
618 htab_delete (saving_htab
);
621 /* Read the state of the compiler back in from F. */
624 gt_pch_restore (FILE *f
)
626 const struct ggc_root_tab
*const *rt
;
627 const struct ggc_root_tab
*rti
;
629 struct mmap_info mmi
;
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
);
666 fatal_error ("had to relocate PCH");
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. */
686 default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED
,
687 int fd ATTRIBUTE_UNUSED
)
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
711 default_gt_pch_alloc_granularity (void)
713 return getpagesize();
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
723 mmap_gt_pch_get_address (size_t size
, int fd
)
727 ret
= mmap (NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
728 if (ret
== (void *) MAP_FAILED
)
731 munmap ((caddr_t
) ret
, size
);
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
)
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. */
755 addr
= mmap ((caddr_t
) base
, size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
,
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. */
766 ggc_rlimit_bound (double limit
)
768 #if defined(HAVE_GETRLIMIT)
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
789 && rlim
.rlim_cur
>= 8 * 1024 * 1024)
790 limit
= rlim
.rlim_cur
;
791 # endif /* RLIMIT_AS or RLIMIT_DATA */
792 #endif /* HAVE_GETRLIMIT */
797 /* Heuristic to set a default for GGC_MIN_EXPAND. */
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;
810 min_expand
= MIN (min_expand
, 70);
816 /* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
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). */
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. */
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);
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);
857 init_ggc_heuristics (void)
859 #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
860 set_default_param_value (GGC_MIN_EXPAND
, ggc_min_expand_heuristic ());
861 set_default_param_value (GGC_MIN_HEAPSIZE
, ggc_min_heapsize_heuristic ());
865 #ifdef GATHER_STATISTICS
867 /* Datastructure used to store per-call-site statistics. */
868 struct loc_descriptor
872 const char *function
;
880 /* Hashtable used for statistics. */
881 static htab_t loc_hash
;
883 /* Hash table helpers functions. */
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
;
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
907 struct loc_descriptor
*loc
;
911 /* Hash table helpers functions. */
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
);
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
;
937 loc
.function
= function
;
939 loc_hash
= htab_create (10, hash_descriptor
, eq_descriptor
, NULL
);
941 slot
= (struct loc_descriptor
**) htab_find_slot (loc_hash
, &loc
, INSERT
);
944 *slot
= XCNEW (struct loc_descriptor
);
945 (*slot
)->file
= name
;
946 (*slot
)->line
= line
;
947 (*slot
)->function
= function
;
951 /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
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
);
962 p
->size
= allocated
+ overhead
;
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
);
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. */
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
);
989 /* After live values has been marked, walk all recorded pointers and see if
990 they are still live. */
992 ggc_prune_overhead_list (void)
994 htab_traverse (ptr_hash
, ggc_prune_ptr
, NULL
);
997 /* Notice that the pointer has been freed. */
999 ggc_free_overhead (void *ptr
)
1001 PTR
*slot
= htab_find_slot_with_hash (ptr_hash
, ptr
, htab_hash_pointer (ptr
),
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
1009 p
= (struct ptr_hash_entry
*) *slot
;
1010 p
->loc
->freed
+= p
->size
;
1011 htab_clear_slot (ptr_hash
, slot
);
1015 /* Helper for qsort; sort descriptors by amount of memory consumed. */
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
;
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. */
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
;
1039 diff
= ((long)(l1
->allocated
+ l1
->overhead
- l1
->freed
- l1
->collected
) -
1040 (l2
->allocated
+ l2
->overhead
- l2
->freed
- l2
->collected
));
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
;
1051 add_statistics (void **slot
, void *b
)
1054 loc_array
[*n
] = (struct loc_descriptor
*) *slot
;
1059 /* Dump per-site memory statistics. */
1062 dump_ggc_loc_statistics (bool final ATTRIBUTE_UNUSED
)
1064 #ifdef GATHER_STATISTICS
1067 size_t collected
= 0, freed
= 0, allocated
= 0, overhead
= 0, times
= 0;
1070 ggc_force_collect
= true;
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
;
1087 collected
+= d
->collected
;
1088 overhead
+= d
->overhead
;
1090 for (i
= 0; i
< nentries
; i
++)
1092 struct loc_descriptor
*d
= loc_array
[i
];
1095 const char *s1
= d
->file
;
1097 while ((s2
= strstr (s1
, "gcc/")))
1099 sprintf (s
, "%s:%i (%s)", s1
, d
->line
, d
->function
);
1101 fprintf (stderr
, "%-48s %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li:%4.1f%% %10li\n", s
,
1103 (d
->collected
) * 100.0 / collected
,
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
),
1110 d
->overhead
* 100.0 / overhead
,
1114 fprintf (stderr
, "%-48s %10ld %10ld %10ld %10ld %10ld\n",
1115 "Total", (long)collected
, (long)freed
,
1116 (long)(allocated
+ overhead
- freed
- collected
), (long)overhead
,
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;