xref: /dflybsd-src/contrib/gcc-8.0/gcc/ggc-common.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Simple garbage collection for the GNU compiler.
2*38fd1498Szrj    Copyright (C) 1999-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj 
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
7*38fd1498Szrj the terms of the GNU General Public License as published by the Free
8*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
9*38fd1498Szrj version.
10*38fd1498Szrj 
11*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*38fd1498Szrj for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj /* Generic garbage collection (GC) functions and data, not specific to
21*38fd1498Szrj    any particular GC implementation.  */
22*38fd1498Szrj 
23*38fd1498Szrj #include "config.h"
24*38fd1498Szrj #include "system.h"
25*38fd1498Szrj #include "coretypes.h"
26*38fd1498Szrj #include "timevar.h"
27*38fd1498Szrj #include "diagnostic-core.h"
28*38fd1498Szrj #include "ggc-internal.h"
29*38fd1498Szrj #include "params.h"
30*38fd1498Szrj #include "hosthooks.h"
31*38fd1498Szrj #include "plugin.h"
32*38fd1498Szrj 
33*38fd1498Szrj /* When set, ggc_collect will do collection.  */
34*38fd1498Szrj bool ggc_force_collect;
35*38fd1498Szrj 
36*38fd1498Szrj /* When true, protect the contents of the identifier hash table.  */
37*38fd1498Szrj bool ggc_protect_identifiers = true;
38*38fd1498Szrj 
39*38fd1498Szrj /* Statistics about the allocation.  */
40*38fd1498Szrj static ggc_statistics *ggc_stats;
41*38fd1498Szrj 
42*38fd1498Szrj struct traversal_state;
43*38fd1498Szrj 
44*38fd1498Szrj static int compare_ptr_data (const void *, const void *);
45*38fd1498Szrj static void relocate_ptrs (void *, void *);
46*38fd1498Szrj static void write_pch_globals (const struct ggc_root_tab * const *tab,
47*38fd1498Szrj 			       struct traversal_state *state);
48*38fd1498Szrj 
49*38fd1498Szrj /* Maintain global roots that are preserved during GC.  */
50*38fd1498Szrj 
51*38fd1498Szrj /* This extra vector of dynamically registered root_tab-s is used by
52*38fd1498Szrj    ggc_mark_roots and gives the ability to dynamically add new GGC root
53*38fd1498Szrj    tables, for instance from some plugins; this vector is on the heap
54*38fd1498Szrj    since it is used by GGC internally.  */
55*38fd1498Szrj typedef const struct ggc_root_tab *const_ggc_root_tab_t;
56*38fd1498Szrj static vec<const_ggc_root_tab_t> extra_root_vec;
57*38fd1498Szrj 
58*38fd1498Szrj /* Dynamically register a new GGC root table RT. This is useful for
59*38fd1498Szrj    plugins. */
60*38fd1498Szrj 
61*38fd1498Szrj void
ggc_register_root_tab(const struct ggc_root_tab * rt)62*38fd1498Szrj ggc_register_root_tab (const struct ggc_root_tab* rt)
63*38fd1498Szrj {
64*38fd1498Szrj   if (rt)
65*38fd1498Szrj     extra_root_vec.safe_push (rt);
66*38fd1498Szrj }
67*38fd1498Szrj 
68*38fd1498Szrj /* Mark all the roots in the table RT.  */
69*38fd1498Szrj 
70*38fd1498Szrj static void
ggc_mark_root_tab(const_ggc_root_tab_t rt)71*38fd1498Szrj ggc_mark_root_tab (const_ggc_root_tab_t rt)
72*38fd1498Szrj {
73*38fd1498Szrj   size_t i;
74*38fd1498Szrj 
75*38fd1498Szrj   for ( ; rt->base != NULL; rt++)
76*38fd1498Szrj     for (i = 0; i < rt->nelt; i++)
77*38fd1498Szrj       (*rt->cb) (*(void **) ((char *)rt->base + rt->stride * i));
78*38fd1498Szrj }
79*38fd1498Szrj 
80*38fd1498Szrj /* Iterate through all registered roots and mark each element.  */
81*38fd1498Szrj 
82*38fd1498Szrj void
ggc_mark_roots(void)83*38fd1498Szrj ggc_mark_roots (void)
84*38fd1498Szrj {
85*38fd1498Szrj   const struct ggc_root_tab *const *rt;
86*38fd1498Szrj   const_ggc_root_tab_t rtp, rti;
87*38fd1498Szrj   size_t i;
88*38fd1498Szrj 
89*38fd1498Szrj   for (rt = gt_ggc_deletable_rtab; *rt; rt++)
90*38fd1498Szrj     for (rti = *rt; rti->base != NULL; rti++)
91*38fd1498Szrj       memset (rti->base, 0, rti->stride);
92*38fd1498Szrj 
93*38fd1498Szrj   for (rt = gt_ggc_rtab; *rt; rt++)
94*38fd1498Szrj     ggc_mark_root_tab (*rt);
95*38fd1498Szrj 
96*38fd1498Szrj   FOR_EACH_VEC_ELT (extra_root_vec, i, rtp)
97*38fd1498Szrj     ggc_mark_root_tab (rtp);
98*38fd1498Szrj 
99*38fd1498Szrj   if (ggc_protect_identifiers)
100*38fd1498Szrj     ggc_mark_stringpool ();
101*38fd1498Szrj 
102*38fd1498Szrj   gt_clear_caches ();
103*38fd1498Szrj 
104*38fd1498Szrj   if (! ggc_protect_identifiers)
105*38fd1498Szrj     ggc_purge_stringpool ();
106*38fd1498Szrj 
107*38fd1498Szrj   /* Some plugins may call ggc_set_mark from here.  */
108*38fd1498Szrj   invoke_plugin_callbacks (PLUGIN_GGC_MARKING, NULL);
109*38fd1498Szrj }
110*38fd1498Szrj 
111*38fd1498Szrj /* Allocate a block of memory, then clear it.  */
112*38fd1498Szrj void *
ggc_internal_cleared_alloc(size_t size,void (* f)(void *),size_t s,size_t n MEM_STAT_DECL)113*38fd1498Szrj ggc_internal_cleared_alloc (size_t size, void (*f)(void *), size_t s, size_t n
114*38fd1498Szrj 			    MEM_STAT_DECL)
115*38fd1498Szrj {
116*38fd1498Szrj   void *buf = ggc_internal_alloc (size, f, s, n PASS_MEM_STAT);
117*38fd1498Szrj   memset (buf, 0, size);
118*38fd1498Szrj   return buf;
119*38fd1498Szrj }
120*38fd1498Szrj 
121*38fd1498Szrj /* Resize a block of memory, possibly re-allocating it.  */
122*38fd1498Szrj void *
ggc_realloc(void * x,size_t size MEM_STAT_DECL)123*38fd1498Szrj ggc_realloc (void *x, size_t size MEM_STAT_DECL)
124*38fd1498Szrj {
125*38fd1498Szrj   void *r;
126*38fd1498Szrj   size_t old_size;
127*38fd1498Szrj 
128*38fd1498Szrj   if (x == NULL)
129*38fd1498Szrj     return ggc_internal_alloc (size PASS_MEM_STAT);
130*38fd1498Szrj 
131*38fd1498Szrj   old_size = ggc_get_size (x);
132*38fd1498Szrj 
133*38fd1498Szrj   if (size <= old_size)
134*38fd1498Szrj     {
135*38fd1498Szrj       /* Mark the unwanted memory as unaccessible.  We also need to make
136*38fd1498Szrj 	 the "new" size accessible, since ggc_get_size returns the size of
137*38fd1498Szrj 	 the pool, not the size of the individually allocated object, the
138*38fd1498Szrj 	 size which was previously made accessible.  Unfortunately, we
139*38fd1498Szrj 	 don't know that previously allocated size.  Without that
140*38fd1498Szrj 	 knowledge we have to lose some initialization-tracking for the
141*38fd1498Szrj 	 old parts of the object.  An alternative is to mark the whole
142*38fd1498Szrj 	 old_size as reachable, but that would lose tracking of writes
143*38fd1498Szrj 	 after the end of the object (by small offsets).  Discard the
144*38fd1498Szrj 	 handle to avoid handle leak.  */
145*38fd1498Szrj       VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x + size,
146*38fd1498Szrj 						    old_size - size));
147*38fd1498Szrj       VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, size));
148*38fd1498Szrj       return x;
149*38fd1498Szrj     }
150*38fd1498Szrj 
151*38fd1498Szrj   r = ggc_internal_alloc (size PASS_MEM_STAT);
152*38fd1498Szrj 
153*38fd1498Szrj   /* Since ggc_get_size returns the size of the pool, not the size of the
154*38fd1498Szrj      individually allocated object, we'd access parts of the old object
155*38fd1498Szrj      that were marked invalid with the memcpy below.  We lose a bit of the
156*38fd1498Szrj      initialization-tracking since some of it may be uninitialized.  */
157*38fd1498Szrj   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, old_size));
158*38fd1498Szrj 
159*38fd1498Szrj   memcpy (r, x, old_size);
160*38fd1498Szrj 
161*38fd1498Szrj   /* The old object is not supposed to be used anymore.  */
162*38fd1498Szrj   ggc_free (x);
163*38fd1498Szrj 
164*38fd1498Szrj   return r;
165*38fd1498Szrj }
166*38fd1498Szrj 
167*38fd1498Szrj void *
ggc_cleared_alloc_htab_ignore_args(size_t c ATTRIBUTE_UNUSED,size_t n ATTRIBUTE_UNUSED)168*38fd1498Szrj ggc_cleared_alloc_htab_ignore_args (size_t c ATTRIBUTE_UNUSED,
169*38fd1498Szrj 				    size_t n ATTRIBUTE_UNUSED)
170*38fd1498Szrj {
171*38fd1498Szrj   gcc_assert (c * n == sizeof (struct htab));
172*38fd1498Szrj   return ggc_cleared_alloc<htab> ();
173*38fd1498Szrj }
174*38fd1498Szrj 
175*38fd1498Szrj /* TODO: once we actually use type information in GGC, create a new tag
176*38fd1498Szrj    gt_gcc_ptr_array and use it for pointer arrays.  */
177*38fd1498Szrj void *
ggc_cleared_alloc_ptr_array_two_args(size_t c,size_t n)178*38fd1498Szrj ggc_cleared_alloc_ptr_array_two_args (size_t c, size_t n)
179*38fd1498Szrj {
180*38fd1498Szrj   gcc_assert (sizeof (PTR *) == n);
181*38fd1498Szrj   return ggc_cleared_vec_alloc<PTR *> (c);
182*38fd1498Szrj }
183*38fd1498Szrj 
184*38fd1498Szrj /* These are for splay_tree_new_ggc.  */
185*38fd1498Szrj void *
ggc_splay_alloc(int sz,void * nl)186*38fd1498Szrj ggc_splay_alloc (int sz, void *nl)
187*38fd1498Szrj {
188*38fd1498Szrj   gcc_assert (!nl);
189*38fd1498Szrj   return ggc_internal_alloc (sz);
190*38fd1498Szrj }
191*38fd1498Szrj 
192*38fd1498Szrj void
ggc_splay_dont_free(void * x ATTRIBUTE_UNUSED,void * nl)193*38fd1498Szrj ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl)
194*38fd1498Szrj {
195*38fd1498Szrj   gcc_assert (!nl);
196*38fd1498Szrj }
197*38fd1498Szrj 
198*38fd1498Szrj /* Print statistics that are independent of the collector in use.  */
199*38fd1498Szrj #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
200*38fd1498Szrj 		  ? (x) \
201*38fd1498Szrj 		  : ((x) < 1024*1024*10 \
202*38fd1498Szrj 		     ? (x) / 1024 \
203*38fd1498Szrj 		     : (x) / (1024*1024))))
204*38fd1498Szrj #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
205*38fd1498Szrj 
206*38fd1498Szrj void
ggc_print_common_statistics(FILE * stream ATTRIBUTE_UNUSED,ggc_statistics * stats)207*38fd1498Szrj ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED,
208*38fd1498Szrj 			     ggc_statistics *stats)
209*38fd1498Szrj {
210*38fd1498Szrj   /* Set the pointer so that during collection we will actually gather
211*38fd1498Szrj      the statistics.  */
212*38fd1498Szrj   ggc_stats = stats;
213*38fd1498Szrj 
214*38fd1498Szrj   /* Then do one collection to fill in the statistics.  */
215*38fd1498Szrj   ggc_collect ();
216*38fd1498Szrj 
217*38fd1498Szrj   /* At present, we don't really gather any interesting statistics.  */
218*38fd1498Szrj 
219*38fd1498Szrj   /* Don't gather statistics any more.  */
220*38fd1498Szrj   ggc_stats = NULL;
221*38fd1498Szrj }
222*38fd1498Szrj 
223*38fd1498Szrj /* Functions for saving and restoring GCable memory to disk.  */
224*38fd1498Szrj 
225*38fd1498Szrj struct ptr_data
226*38fd1498Szrj {
227*38fd1498Szrj   void *obj;
228*38fd1498Szrj   void *note_ptr_cookie;
229*38fd1498Szrj   gt_note_pointers note_ptr_fn;
230*38fd1498Szrj   gt_handle_reorder reorder_fn;
231*38fd1498Szrj   size_t size;
232*38fd1498Szrj   void *new_addr;
233*38fd1498Szrj };
234*38fd1498Szrj 
235*38fd1498Szrj #define POINTER_HASH(x) (hashval_t)((intptr_t)x >> 3)
236*38fd1498Szrj 
237*38fd1498Szrj /* Helper for hashing saving_htab.  */
238*38fd1498Szrj 
239*38fd1498Szrj struct saving_hasher : free_ptr_hash <ptr_data>
240*38fd1498Szrj {
241*38fd1498Szrj   typedef void *compare_type;
242*38fd1498Szrj   static inline hashval_t hash (const ptr_data *);
243*38fd1498Szrj   static inline bool equal (const ptr_data *, const void *);
244*38fd1498Szrj };
245*38fd1498Szrj 
246*38fd1498Szrj inline hashval_t
hash(const ptr_data * p)247*38fd1498Szrj saving_hasher::hash (const ptr_data *p)
248*38fd1498Szrj {
249*38fd1498Szrj   return POINTER_HASH (p->obj);
250*38fd1498Szrj }
251*38fd1498Szrj 
252*38fd1498Szrj inline bool
equal(const ptr_data * p1,const void * p2)253*38fd1498Szrj saving_hasher::equal (const ptr_data *p1, const void *p2)
254*38fd1498Szrj {
255*38fd1498Szrj   return p1->obj == p2;
256*38fd1498Szrj }
257*38fd1498Szrj 
258*38fd1498Szrj static hash_table<saving_hasher> *saving_htab;
259*38fd1498Szrj 
260*38fd1498Szrj /* Register an object in the hash table.  */
261*38fd1498Szrj 
262*38fd1498Szrj int
gt_pch_note_object(void * obj,void * note_ptr_cookie,gt_note_pointers note_ptr_fn)263*38fd1498Szrj gt_pch_note_object (void *obj, void *note_ptr_cookie,
264*38fd1498Szrj 		    gt_note_pointers note_ptr_fn)
265*38fd1498Szrj {
266*38fd1498Szrj   struct ptr_data **slot;
267*38fd1498Szrj 
268*38fd1498Szrj   if (obj == NULL || obj == (void *) 1)
269*38fd1498Szrj     return 0;
270*38fd1498Szrj 
271*38fd1498Szrj   slot = (struct ptr_data **)
272*38fd1498Szrj     saving_htab->find_slot_with_hash (obj, POINTER_HASH (obj), INSERT);
273*38fd1498Szrj   if (*slot != NULL)
274*38fd1498Szrj     {
275*38fd1498Szrj       gcc_assert ((*slot)->note_ptr_fn == note_ptr_fn
276*38fd1498Szrj 		  && (*slot)->note_ptr_cookie == note_ptr_cookie);
277*38fd1498Szrj       return 0;
278*38fd1498Szrj     }
279*38fd1498Szrj 
280*38fd1498Szrj   *slot = XCNEW (struct ptr_data);
281*38fd1498Szrj   (*slot)->obj = obj;
282*38fd1498Szrj   (*slot)->note_ptr_fn = note_ptr_fn;
283*38fd1498Szrj   (*slot)->note_ptr_cookie = note_ptr_cookie;
284*38fd1498Szrj   if (note_ptr_fn == gt_pch_p_S)
285*38fd1498Szrj     (*slot)->size = strlen ((const char *)obj) + 1;
286*38fd1498Szrj   else
287*38fd1498Szrj     (*slot)->size = ggc_get_size (obj);
288*38fd1498Szrj   return 1;
289*38fd1498Szrj }
290*38fd1498Szrj 
291*38fd1498Szrj /* Register an object in the hash table.  */
292*38fd1498Szrj 
293*38fd1498Szrj void
gt_pch_note_reorder(void * obj,void * note_ptr_cookie,gt_handle_reorder reorder_fn)294*38fd1498Szrj gt_pch_note_reorder (void *obj, void *note_ptr_cookie,
295*38fd1498Szrj 		     gt_handle_reorder reorder_fn)
296*38fd1498Szrj {
297*38fd1498Szrj   struct ptr_data *data;
298*38fd1498Szrj 
299*38fd1498Szrj   if (obj == NULL || obj == (void *) 1)
300*38fd1498Szrj     return;
301*38fd1498Szrj 
302*38fd1498Szrj   data = (struct ptr_data *)
303*38fd1498Szrj     saving_htab->find_with_hash (obj, POINTER_HASH (obj));
304*38fd1498Szrj   gcc_assert (data && data->note_ptr_cookie == note_ptr_cookie);
305*38fd1498Szrj 
306*38fd1498Szrj   data->reorder_fn = reorder_fn;
307*38fd1498Szrj }
308*38fd1498Szrj 
309*38fd1498Szrj /* Handy state for the traversal functions.  */
310*38fd1498Szrj 
311*38fd1498Szrj struct traversal_state
312*38fd1498Szrj {
313*38fd1498Szrj   FILE *f;
314*38fd1498Szrj   struct ggc_pch_data *d;
315*38fd1498Szrj   size_t count;
316*38fd1498Szrj   struct ptr_data **ptrs;
317*38fd1498Szrj   size_t ptrs_i;
318*38fd1498Szrj };
319*38fd1498Szrj 
320*38fd1498Szrj /* Callbacks for htab_traverse.  */
321*38fd1498Szrj 
322*38fd1498Szrj int
ggc_call_count(ptr_data ** slot,traversal_state * state)323*38fd1498Szrj ggc_call_count (ptr_data **slot, traversal_state *state)
324*38fd1498Szrj {
325*38fd1498Szrj   struct ptr_data *d = *slot;
326*38fd1498Szrj 
327*38fd1498Szrj   ggc_pch_count_object (state->d, d->obj, d->size,
328*38fd1498Szrj 			d->note_ptr_fn == gt_pch_p_S);
329*38fd1498Szrj   state->count++;
330*38fd1498Szrj   return 1;
331*38fd1498Szrj }
332*38fd1498Szrj 
333*38fd1498Szrj int
ggc_call_alloc(ptr_data ** slot,traversal_state * state)334*38fd1498Szrj ggc_call_alloc (ptr_data **slot, traversal_state *state)
335*38fd1498Szrj {
336*38fd1498Szrj   struct ptr_data *d = *slot;
337*38fd1498Szrj 
338*38fd1498Szrj   d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size,
339*38fd1498Szrj 				      d->note_ptr_fn == gt_pch_p_S);
340*38fd1498Szrj   state->ptrs[state->ptrs_i++] = d;
341*38fd1498Szrj   return 1;
342*38fd1498Szrj }
343*38fd1498Szrj 
344*38fd1498Szrj /* Callback for qsort.  */
345*38fd1498Szrj 
346*38fd1498Szrj static int
compare_ptr_data(const void * p1_p,const void * p2_p)347*38fd1498Szrj compare_ptr_data (const void *p1_p, const void *p2_p)
348*38fd1498Szrj {
349*38fd1498Szrj   const struct ptr_data *const p1 = *(const struct ptr_data *const *)p1_p;
350*38fd1498Szrj   const struct ptr_data *const p2 = *(const struct ptr_data *const *)p2_p;
351*38fd1498Szrj   return (((size_t)p1->new_addr > (size_t)p2->new_addr)
352*38fd1498Szrj 	  - ((size_t)p1->new_addr < (size_t)p2->new_addr));
353*38fd1498Szrj }
354*38fd1498Szrj 
355*38fd1498Szrj /* Callbacks for note_ptr_fn.  */
356*38fd1498Szrj 
357*38fd1498Szrj static void
relocate_ptrs(void * ptr_p,void * state_p)358*38fd1498Szrj relocate_ptrs (void *ptr_p, void *state_p)
359*38fd1498Szrj {
360*38fd1498Szrj   void **ptr = (void **)ptr_p;
361*38fd1498Szrj   struct traversal_state *state ATTRIBUTE_UNUSED
362*38fd1498Szrj     = (struct traversal_state *)state_p;
363*38fd1498Szrj   struct ptr_data *result;
364*38fd1498Szrj 
365*38fd1498Szrj   if (*ptr == NULL || *ptr == (void *)1)
366*38fd1498Szrj     return;
367*38fd1498Szrj 
368*38fd1498Szrj   result = (struct ptr_data *)
369*38fd1498Szrj     saving_htab->find_with_hash (*ptr, POINTER_HASH (*ptr));
370*38fd1498Szrj   gcc_assert (result);
371*38fd1498Szrj   *ptr = result->new_addr;
372*38fd1498Szrj }
373*38fd1498Szrj 
374*38fd1498Szrj /* Write out, after relocation, the pointers in TAB.  */
375*38fd1498Szrj static void
write_pch_globals(const struct ggc_root_tab * const * tab,struct traversal_state * state)376*38fd1498Szrj write_pch_globals (const struct ggc_root_tab * const *tab,
377*38fd1498Szrj 		   struct traversal_state *state)
378*38fd1498Szrj {
379*38fd1498Szrj   const struct ggc_root_tab *const *rt;
380*38fd1498Szrj   const struct ggc_root_tab *rti;
381*38fd1498Szrj   size_t i;
382*38fd1498Szrj 
383*38fd1498Szrj   for (rt = tab; *rt; rt++)
384*38fd1498Szrj     for (rti = *rt; rti->base != NULL; rti++)
385*38fd1498Szrj       for (i = 0; i < rti->nelt; i++)
386*38fd1498Szrj 	{
387*38fd1498Szrj 	  void *ptr = *(void **)((char *)rti->base + rti->stride * i);
388*38fd1498Szrj 	  struct ptr_data *new_ptr;
389*38fd1498Szrj 	  if (ptr == NULL || ptr == (void *)1)
390*38fd1498Szrj 	    {
391*38fd1498Szrj 	      if (fwrite (&ptr, sizeof (void *), 1, state->f)
392*38fd1498Szrj 		  != 1)
393*38fd1498Szrj 		fatal_error (input_location, "can%'t write PCH file: %m");
394*38fd1498Szrj 	    }
395*38fd1498Szrj 	  else
396*38fd1498Szrj 	    {
397*38fd1498Szrj 	      new_ptr = (struct ptr_data *)
398*38fd1498Szrj 		saving_htab->find_with_hash (ptr, POINTER_HASH (ptr));
399*38fd1498Szrj 	      if (fwrite (&new_ptr->new_addr, sizeof (void *), 1, state->f)
400*38fd1498Szrj 		  != 1)
401*38fd1498Szrj 		fatal_error (input_location, "can%'t write PCH file: %m");
402*38fd1498Szrj 	    }
403*38fd1498Szrj 	}
404*38fd1498Szrj }
405*38fd1498Szrj 
406*38fd1498Szrj /* Hold the information we need to mmap the file back in.  */
407*38fd1498Szrj 
408*38fd1498Szrj struct mmap_info
409*38fd1498Szrj {
410*38fd1498Szrj   size_t offset;
411*38fd1498Szrj   size_t size;
412*38fd1498Szrj   void *preferred_base;
413*38fd1498Szrj };
414*38fd1498Szrj 
415*38fd1498Szrj /* Write out the state of the compiler to F.  */
416*38fd1498Szrj 
417*38fd1498Szrj void
gt_pch_save(FILE * f)418*38fd1498Szrj gt_pch_save (FILE *f)
419*38fd1498Szrj {
420*38fd1498Szrj   const struct ggc_root_tab *const *rt;
421*38fd1498Szrj   const struct ggc_root_tab *rti;
422*38fd1498Szrj   size_t i;
423*38fd1498Szrj   struct traversal_state state;
424*38fd1498Szrj   char *this_object = NULL;
425*38fd1498Szrj   size_t this_object_size = 0;
426*38fd1498Szrj   struct mmap_info mmi;
427*38fd1498Szrj   const size_t mmap_offset_alignment = host_hooks.gt_pch_alloc_granularity ();
428*38fd1498Szrj 
429*38fd1498Szrj   gt_pch_save_stringpool ();
430*38fd1498Szrj 
431*38fd1498Szrj   timevar_push (TV_PCH_PTR_REALLOC);
432*38fd1498Szrj   saving_htab = new hash_table<saving_hasher> (50000);
433*38fd1498Szrj 
434*38fd1498Szrj   for (rt = gt_ggc_rtab; *rt; rt++)
435*38fd1498Szrj     for (rti = *rt; rti->base != NULL; rti++)
436*38fd1498Szrj       for (i = 0; i < rti->nelt; i++)
437*38fd1498Szrj 	(*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
438*38fd1498Szrj 
439*38fd1498Szrj   /* Prepare the objects for writing, determine addresses and such.  */
440*38fd1498Szrj   state.f = f;
441*38fd1498Szrj   state.d = init_ggc_pch ();
442*38fd1498Szrj   state.count = 0;
443*38fd1498Szrj   saving_htab->traverse <traversal_state *, ggc_call_count> (&state);
444*38fd1498Szrj 
445*38fd1498Szrj   mmi.size = ggc_pch_total_size (state.d);
446*38fd1498Szrj 
447*38fd1498Szrj   /* Try to arrange things so that no relocation is necessary, but
448*38fd1498Szrj      don't try very hard.  On most platforms, this will always work,
449*38fd1498Szrj      and on the rest it's a lot of work to do better.
450*38fd1498Szrj      (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
451*38fd1498Szrj      HOST_HOOKS_GT_PCH_USE_ADDRESS.)  */
452*38fd1498Szrj   mmi.preferred_base = host_hooks.gt_pch_get_address (mmi.size, fileno (f));
453*38fd1498Szrj 
454*38fd1498Szrj   ggc_pch_this_base (state.d, mmi.preferred_base);
455*38fd1498Szrj 
456*38fd1498Szrj   state.ptrs = XNEWVEC (struct ptr_data *, state.count);
457*38fd1498Szrj   state.ptrs_i = 0;
458*38fd1498Szrj 
459*38fd1498Szrj   saving_htab->traverse <traversal_state *, ggc_call_alloc> (&state);
460*38fd1498Szrj   timevar_pop (TV_PCH_PTR_REALLOC);
461*38fd1498Szrj 
462*38fd1498Szrj   timevar_push (TV_PCH_PTR_SORT);
463*38fd1498Szrj   qsort (state.ptrs, state.count, sizeof (*state.ptrs), compare_ptr_data);
464*38fd1498Szrj   timevar_pop (TV_PCH_PTR_SORT);
465*38fd1498Szrj 
466*38fd1498Szrj   /* Write out all the scalar variables.  */
467*38fd1498Szrj   for (rt = gt_pch_scalar_rtab; *rt; rt++)
468*38fd1498Szrj     for (rti = *rt; rti->base != NULL; rti++)
469*38fd1498Szrj       if (fwrite (rti->base, rti->stride, 1, f) != 1)
470*38fd1498Szrj 	fatal_error (input_location, "can%'t write PCH file: %m");
471*38fd1498Szrj 
472*38fd1498Szrj   /* Write out all the global pointers, after translation.  */
473*38fd1498Szrj   write_pch_globals (gt_ggc_rtab, &state);
474*38fd1498Szrj 
475*38fd1498Szrj   /* Pad the PCH file so that the mmapped area starts on an allocation
476*38fd1498Szrj      granularity (usually page) boundary.  */
477*38fd1498Szrj   {
478*38fd1498Szrj     long o;
479*38fd1498Szrj     o = ftell (state.f) + sizeof (mmi);
480*38fd1498Szrj     if (o == -1)
481*38fd1498Szrj       fatal_error (input_location, "can%'t get position in PCH file: %m");
482*38fd1498Szrj     mmi.offset = mmap_offset_alignment - o % mmap_offset_alignment;
483*38fd1498Szrj     if (mmi.offset == mmap_offset_alignment)
484*38fd1498Szrj       mmi.offset = 0;
485*38fd1498Szrj     mmi.offset += o;
486*38fd1498Szrj   }
487*38fd1498Szrj   if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1)
488*38fd1498Szrj     fatal_error (input_location, "can%'t write PCH file: %m");
489*38fd1498Szrj   if (mmi.offset != 0
490*38fd1498Szrj       && fseek (state.f, mmi.offset, SEEK_SET) != 0)
491*38fd1498Szrj     fatal_error (input_location, "can%'t write padding to PCH file: %m");
492*38fd1498Szrj 
493*38fd1498Szrj   ggc_pch_prepare_write (state.d, state.f);
494*38fd1498Szrj 
495*38fd1498Szrj #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
496*38fd1498Szrj   vec<char> vbits = vNULL;
497*38fd1498Szrj #endif
498*38fd1498Szrj 
499*38fd1498Szrj   /* Actually write out the objects.  */
500*38fd1498Szrj   for (i = 0; i < state.count; i++)
501*38fd1498Szrj     {
502*38fd1498Szrj       if (this_object_size < state.ptrs[i]->size)
503*38fd1498Szrj 	{
504*38fd1498Szrj 	  this_object_size = state.ptrs[i]->size;
505*38fd1498Szrj 	  this_object = XRESIZEVAR (char, this_object, this_object_size);
506*38fd1498Szrj 	}
507*38fd1498Szrj #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
508*38fd1498Szrj       /* obj might contain uninitialized bytes, e.g. in the trailing
509*38fd1498Szrj 	 padding of the object.  Avoid warnings by making the memory
510*38fd1498Szrj 	 temporarily defined and then restoring previous state.  */
511*38fd1498Szrj       int get_vbits = 0;
512*38fd1498Szrj       size_t valid_size = state.ptrs[i]->size;
513*38fd1498Szrj       if (__builtin_expect (RUNNING_ON_VALGRIND, 0))
514*38fd1498Szrj 	{
515*38fd1498Szrj 	  if (vbits.length () < valid_size)
516*38fd1498Szrj 	    vbits.safe_grow (valid_size);
517*38fd1498Szrj 	  get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
518*38fd1498Szrj 					  vbits.address (), valid_size);
519*38fd1498Szrj 	  if (get_vbits == 3)
520*38fd1498Szrj 	    {
521*38fd1498Szrj 	      /* We assume that first part of obj is addressable, and
522*38fd1498Szrj 		 the rest is unaddressable.  Find out where the boundary is
523*38fd1498Szrj 		 using binary search.  */
524*38fd1498Szrj 	      size_t lo = 0, hi = valid_size;
525*38fd1498Szrj 	      while (hi > lo)
526*38fd1498Szrj 		{
527*38fd1498Szrj 		  size_t mid = (lo + hi) / 2;
528*38fd1498Szrj 		  get_vbits = VALGRIND_GET_VBITS ((char *) state.ptrs[i]->obj
529*38fd1498Szrj 						  + mid, vbits.address (),
530*38fd1498Szrj 						  1);
531*38fd1498Szrj 		  if (get_vbits == 3)
532*38fd1498Szrj 		    hi = mid;
533*38fd1498Szrj 		  else if (get_vbits == 1)
534*38fd1498Szrj 		    lo = mid + 1;
535*38fd1498Szrj 		  else
536*38fd1498Szrj 		    break;
537*38fd1498Szrj 		}
538*38fd1498Szrj 	      if (get_vbits == 1 || get_vbits == 3)
539*38fd1498Szrj 		{
540*38fd1498Szrj 		  valid_size = lo;
541*38fd1498Szrj 		  get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
542*38fd1498Szrj 						  vbits.address (),
543*38fd1498Szrj 						  valid_size);
544*38fd1498Szrj 		}
545*38fd1498Szrj 	    }
546*38fd1498Szrj 	  if (get_vbits == 1)
547*38fd1498Szrj 	    VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (state.ptrs[i]->obj,
548*38fd1498Szrj 							 state.ptrs[i]->size));
549*38fd1498Szrj 	}
550*38fd1498Szrj #endif
551*38fd1498Szrj       memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size);
552*38fd1498Szrj       if (state.ptrs[i]->reorder_fn != NULL)
553*38fd1498Szrj 	state.ptrs[i]->reorder_fn (state.ptrs[i]->obj,
554*38fd1498Szrj 				   state.ptrs[i]->note_ptr_cookie,
555*38fd1498Szrj 				   relocate_ptrs, &state);
556*38fd1498Szrj       state.ptrs[i]->note_ptr_fn (state.ptrs[i]->obj,
557*38fd1498Szrj 				  state.ptrs[i]->note_ptr_cookie,
558*38fd1498Szrj 				  relocate_ptrs, &state);
559*38fd1498Szrj       ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj,
560*38fd1498Szrj 			    state.ptrs[i]->new_addr, state.ptrs[i]->size,
561*38fd1498Szrj 			    state.ptrs[i]->note_ptr_fn == gt_pch_p_S);
562*38fd1498Szrj       if (state.ptrs[i]->note_ptr_fn != gt_pch_p_S)
563*38fd1498Szrj 	memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size);
564*38fd1498Szrj #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
565*38fd1498Szrj       if (__builtin_expect (get_vbits == 1, 0))
566*38fd1498Szrj 	{
567*38fd1498Szrj 	  (void) VALGRIND_SET_VBITS (state.ptrs[i]->obj, vbits.address (),
568*38fd1498Szrj 				     valid_size);
569*38fd1498Szrj 	  if (valid_size != state.ptrs[i]->size)
570*38fd1498Szrj 	    VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *)
571*38fd1498Szrj 							  state.ptrs[i]->obj
572*38fd1498Szrj 							  + valid_size,
573*38fd1498Szrj 							  state.ptrs[i]->size
574*38fd1498Szrj 							  - valid_size));
575*38fd1498Szrj 	}
576*38fd1498Szrj #endif
577*38fd1498Szrj     }
578*38fd1498Szrj #if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
579*38fd1498Szrj   vbits.release ();
580*38fd1498Szrj #endif
581*38fd1498Szrj 
582*38fd1498Szrj   ggc_pch_finish (state.d, state.f);
583*38fd1498Szrj   gt_pch_fixup_stringpool ();
584*38fd1498Szrj 
585*38fd1498Szrj   XDELETE (state.ptrs);
586*38fd1498Szrj   XDELETE (this_object);
587*38fd1498Szrj   delete saving_htab;
588*38fd1498Szrj   saving_htab = NULL;
589*38fd1498Szrj }
590*38fd1498Szrj 
591*38fd1498Szrj /* Read the state of the compiler back in from F.  */
592*38fd1498Szrj 
593*38fd1498Szrj void
gt_pch_restore(FILE * f)594*38fd1498Szrj gt_pch_restore (FILE *f)
595*38fd1498Szrj {
596*38fd1498Szrj   const struct ggc_root_tab *const *rt;
597*38fd1498Szrj   const struct ggc_root_tab *rti;
598*38fd1498Szrj   size_t i;
599*38fd1498Szrj   struct mmap_info mmi;
600*38fd1498Szrj   int result;
601*38fd1498Szrj 
602*38fd1498Szrj   /* Delete any deletable objects.  This makes ggc_pch_read much
603*38fd1498Szrj      faster, as it can be sure that no GCable objects remain other
604*38fd1498Szrj      than the ones just read in.  */
605*38fd1498Szrj   for (rt = gt_ggc_deletable_rtab; *rt; rt++)
606*38fd1498Szrj     for (rti = *rt; rti->base != NULL; rti++)
607*38fd1498Szrj       memset (rti->base, 0, rti->stride);
608*38fd1498Szrj 
609*38fd1498Szrj   /* Read in all the scalar variables.  */
610*38fd1498Szrj   for (rt = gt_pch_scalar_rtab; *rt; rt++)
611*38fd1498Szrj     for (rti = *rt; rti->base != NULL; rti++)
612*38fd1498Szrj       if (fread (rti->base, rti->stride, 1, f) != 1)
613*38fd1498Szrj 	fatal_error (input_location, "can%'t read PCH file: %m");
614*38fd1498Szrj 
615*38fd1498Szrj   /* Read in all the global pointers, in 6 easy loops.  */
616*38fd1498Szrj   for (rt = gt_ggc_rtab; *rt; rt++)
617*38fd1498Szrj     for (rti = *rt; rti->base != NULL; rti++)
618*38fd1498Szrj       for (i = 0; i < rti->nelt; i++)
619*38fd1498Szrj 	if (fread ((char *)rti->base + rti->stride * i,
620*38fd1498Szrj 		   sizeof (void *), 1, f) != 1)
621*38fd1498Szrj 	  fatal_error (input_location, "can%'t read PCH file: %m");
622*38fd1498Szrj 
623*38fd1498Szrj   if (fread (&mmi, sizeof (mmi), 1, f) != 1)
624*38fd1498Szrj     fatal_error (input_location, "can%'t read PCH file: %m");
625*38fd1498Szrj 
626*38fd1498Szrj   result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size,
627*38fd1498Szrj 					  fileno (f), mmi.offset);
628*38fd1498Szrj   if (result < 0)
629*38fd1498Szrj     fatal_error (input_location, "had to relocate PCH");
630*38fd1498Szrj   if (result == 0)
631*38fd1498Szrj     {
632*38fd1498Szrj       if (fseek (f, mmi.offset, SEEK_SET) != 0
633*38fd1498Szrj 	  || fread (mmi.preferred_base, mmi.size, 1, f) != 1)
634*38fd1498Szrj 	fatal_error (input_location, "can%'t read PCH file: %m");
635*38fd1498Szrj     }
636*38fd1498Szrj   else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0)
637*38fd1498Szrj     fatal_error (input_location, "can%'t read PCH file: %m");
638*38fd1498Szrj 
639*38fd1498Szrj   ggc_pch_read (f, mmi.preferred_base);
640*38fd1498Szrj 
641*38fd1498Szrj   gt_pch_restore_stringpool ();
642*38fd1498Szrj }
643*38fd1498Szrj 
644*38fd1498Szrj /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
645*38fd1498Szrj    Select no address whatsoever, and let gt_pch_save choose what it will with
646*38fd1498Szrj    malloc, presumably.  */
647*38fd1498Szrj 
648*38fd1498Szrj void *
default_gt_pch_get_address(size_t size ATTRIBUTE_UNUSED,int fd ATTRIBUTE_UNUSED)649*38fd1498Szrj default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED,
650*38fd1498Szrj 			    int fd ATTRIBUTE_UNUSED)
651*38fd1498Szrj {
652*38fd1498Szrj   return NULL;
653*38fd1498Szrj }
654*38fd1498Szrj 
655*38fd1498Szrj /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
656*38fd1498Szrj    Allocate SIZE bytes with malloc.  Return 0 if the address we got is the
657*38fd1498Szrj    same as base, indicating that the memory has been allocated but needs to
658*38fd1498Szrj    be read in from the file.  Return -1 if the address differs, to relocation
659*38fd1498Szrj    of the PCH file would be required.  */
660*38fd1498Szrj 
661*38fd1498Szrj int
default_gt_pch_use_address(void * base,size_t size,int fd ATTRIBUTE_UNUSED,size_t offset ATTRIBUTE_UNUSED)662*38fd1498Szrj default_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED,
663*38fd1498Szrj 			    size_t offset ATTRIBUTE_UNUSED)
664*38fd1498Szrj {
665*38fd1498Szrj   void *addr = xmalloc (size);
666*38fd1498Szrj   return (addr == base) - 1;
667*38fd1498Szrj }
668*38fd1498Szrj 
669*38fd1498Szrj /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS.   Return the
670*38fd1498Szrj    alignment required for allocating virtual memory. Usually this is the
671*38fd1498Szrj    same as pagesize.  */
672*38fd1498Szrj 
673*38fd1498Szrj size_t
default_gt_pch_alloc_granularity(void)674*38fd1498Szrj default_gt_pch_alloc_granularity (void)
675*38fd1498Szrj {
676*38fd1498Szrj   return getpagesize ();
677*38fd1498Szrj }
678*38fd1498Szrj 
679*38fd1498Szrj #if HAVE_MMAP_FILE
680*38fd1498Szrj /* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
681*38fd1498Szrj    We temporarily allocate SIZE bytes, and let the kernel place the data
682*38fd1498Szrj    wherever it will.  If it worked, that's our spot, if not we're likely
683*38fd1498Szrj    to be in trouble.  */
684*38fd1498Szrj 
685*38fd1498Szrj void *
mmap_gt_pch_get_address(size_t size,int fd)686*38fd1498Szrj mmap_gt_pch_get_address (size_t size, int fd)
687*38fd1498Szrj {
688*38fd1498Szrj   void *ret;
689*38fd1498Szrj 
690*38fd1498Szrj   ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
691*38fd1498Szrj   if (ret == (void *) MAP_FAILED)
692*38fd1498Szrj     ret = NULL;
693*38fd1498Szrj   else
694*38fd1498Szrj     munmap ((caddr_t) ret, size);
695*38fd1498Szrj 
696*38fd1498Szrj   return ret;
697*38fd1498Szrj }
698*38fd1498Szrj 
699*38fd1498Szrj /* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
700*38fd1498Szrj    Map SIZE bytes of FD+OFFSET at BASE.  Return 1 if we succeeded at
701*38fd1498Szrj    mapping the data at BASE, -1 if we couldn't.
702*38fd1498Szrj 
703*38fd1498Szrj    This version assumes that the kernel honors the START operand of mmap
704*38fd1498Szrj    even without MAP_FIXED if START through START+SIZE are not currently
705*38fd1498Szrj    mapped with something.  */
706*38fd1498Szrj 
707*38fd1498Szrj int
mmap_gt_pch_use_address(void * base,size_t size,int fd,size_t offset)708*38fd1498Szrj mmap_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
709*38fd1498Szrj {
710*38fd1498Szrj   void *addr;
711*38fd1498Szrj 
712*38fd1498Szrj   /* We're called with size == 0 if we're not planning to load a PCH
713*38fd1498Szrj      file at all.  This allows the hook to free any static space that
714*38fd1498Szrj      we might have allocated at link time.  */
715*38fd1498Szrj   if (size == 0)
716*38fd1498Szrj     return -1;
717*38fd1498Szrj 
718*38fd1498Szrj   addr = mmap ((caddr_t) base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
719*38fd1498Szrj 	       fd, offset);
720*38fd1498Szrj 
721*38fd1498Szrj   return addr == base ? 1 : -1;
722*38fd1498Szrj }
723*38fd1498Szrj #endif /* HAVE_MMAP_FILE */
724*38fd1498Szrj 
725*38fd1498Szrj #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
726*38fd1498Szrj 
727*38fd1498Szrj /* Modify the bound based on rlimits.  */
728*38fd1498Szrj static double
ggc_rlimit_bound(double limit)729*38fd1498Szrj ggc_rlimit_bound (double limit)
730*38fd1498Szrj {
731*38fd1498Szrj #if defined(HAVE_GETRLIMIT)
732*38fd1498Szrj   struct rlimit rlim;
733*38fd1498Szrj # if defined (RLIMIT_AS)
734*38fd1498Szrj   /* RLIMIT_AS is what POSIX says is the limit on mmap.  Presumably
735*38fd1498Szrj      any OS which has RLIMIT_AS also has a working mmap that GCC will use.  */
736*38fd1498Szrj   if (getrlimit (RLIMIT_AS, &rlim) == 0
737*38fd1498Szrj       && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
738*38fd1498Szrj       && rlim.rlim_cur < limit)
739*38fd1498Szrj     limit = rlim.rlim_cur;
740*38fd1498Szrj # elif defined (RLIMIT_DATA)
741*38fd1498Szrj   /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
742*38fd1498Szrj      might be on an OS that has a broken mmap.  (Others don't bound
743*38fd1498Szrj      mmap at all, apparently.)  */
744*38fd1498Szrj   if (getrlimit (RLIMIT_DATA, &rlim) == 0
745*38fd1498Szrj       && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
746*38fd1498Szrj       && rlim.rlim_cur < limit
747*38fd1498Szrj       /* Darwin has this horribly bogus default setting of
748*38fd1498Szrj 	 RLIMIT_DATA, to 6144Kb.  No-one notices because RLIMIT_DATA
749*38fd1498Szrj 	 appears to be ignored.  Ignore such silliness.  If a limit
750*38fd1498Szrj 	 this small was actually effective for mmap, GCC wouldn't even
751*38fd1498Szrj 	 start up.  */
752*38fd1498Szrj       && rlim.rlim_cur >= 8 * 1024 * 1024)
753*38fd1498Szrj     limit = rlim.rlim_cur;
754*38fd1498Szrj # endif /* RLIMIT_AS or RLIMIT_DATA */
755*38fd1498Szrj #endif /* HAVE_GETRLIMIT */
756*38fd1498Szrj 
757*38fd1498Szrj   return limit;
758*38fd1498Szrj }
759*38fd1498Szrj 
760*38fd1498Szrj /* Heuristic to set a default for GGC_MIN_EXPAND.  */
761*38fd1498Szrj static int
ggc_min_expand_heuristic(void)762*38fd1498Szrj ggc_min_expand_heuristic (void)
763*38fd1498Szrj {
764*38fd1498Szrj   double min_expand = physmem_total ();
765*38fd1498Szrj 
766*38fd1498Szrj   /* Adjust for rlimits.  */
767*38fd1498Szrj   min_expand = ggc_rlimit_bound (min_expand);
768*38fd1498Szrj 
769*38fd1498Szrj   /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
770*38fd1498Szrj      a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB).  */
771*38fd1498Szrj   min_expand /= 1024*1024*1024;
772*38fd1498Szrj   min_expand *= 70;
773*38fd1498Szrj   min_expand = MIN (min_expand, 70);
774*38fd1498Szrj   min_expand += 30;
775*38fd1498Szrj 
776*38fd1498Szrj   return min_expand;
777*38fd1498Szrj }
778*38fd1498Szrj 
779*38fd1498Szrj /* Heuristic to set a default for GGC_MIN_HEAPSIZE.  */
780*38fd1498Szrj static int
ggc_min_heapsize_heuristic(void)781*38fd1498Szrj ggc_min_heapsize_heuristic (void)
782*38fd1498Szrj {
783*38fd1498Szrj   double phys_kbytes = physmem_total ();
784*38fd1498Szrj   double limit_kbytes = ggc_rlimit_bound (phys_kbytes * 2);
785*38fd1498Szrj 
786*38fd1498Szrj   phys_kbytes /= 1024; /* Convert to Kbytes.  */
787*38fd1498Szrj   limit_kbytes /= 1024;
788*38fd1498Szrj 
789*38fd1498Szrj   /* The heuristic is RAM/8, with a lower bound of 4M and an upper
790*38fd1498Szrj      bound of 128M (when RAM >= 1GB).  */
791*38fd1498Szrj   phys_kbytes /= 8;
792*38fd1498Szrj 
793*38fd1498Szrj #if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
794*38fd1498Szrj   /* Try not to overrun the RSS limit while doing garbage collection.
795*38fd1498Szrj      The RSS limit is only advisory, so no margin is subtracted.  */
796*38fd1498Szrj  {
797*38fd1498Szrj    struct rlimit rlim;
798*38fd1498Szrj    if (getrlimit (RLIMIT_RSS, &rlim) == 0
799*38fd1498Szrj        && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
800*38fd1498Szrj      phys_kbytes = MIN (phys_kbytes, rlim.rlim_cur / 1024);
801*38fd1498Szrj  }
802*38fd1498Szrj # endif
803*38fd1498Szrj 
804*38fd1498Szrj   /* Don't blindly run over our data limit; do GC at least when the
805*38fd1498Szrj      *next* GC would be within 20Mb of the limit or within a quarter of
806*38fd1498Szrj      the limit, whichever is larger.  If GCC does hit the data limit,
807*38fd1498Szrj      compilation will fail, so this tries to be conservative.  */
808*38fd1498Szrj   limit_kbytes = MAX (0, limit_kbytes - MAX (limit_kbytes / 4, 20 * 1024));
809*38fd1498Szrj   limit_kbytes = (limit_kbytes * 100) / (110 + ggc_min_expand_heuristic ());
810*38fd1498Szrj   phys_kbytes = MIN (phys_kbytes, limit_kbytes);
811*38fd1498Szrj 
812*38fd1498Szrj   phys_kbytes = MAX (phys_kbytes, 4 * 1024);
813*38fd1498Szrj   phys_kbytes = MIN (phys_kbytes, 128 * 1024);
814*38fd1498Szrj 
815*38fd1498Szrj   return phys_kbytes;
816*38fd1498Szrj }
817*38fd1498Szrj #endif
818*38fd1498Szrj 
819*38fd1498Szrj void
init_ggc_heuristics(void)820*38fd1498Szrj init_ggc_heuristics (void)
821*38fd1498Szrj {
822*38fd1498Szrj #if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
823*38fd1498Szrj   set_default_param_value (GGC_MIN_EXPAND, ggc_min_expand_heuristic ());
824*38fd1498Szrj   set_default_param_value (GGC_MIN_HEAPSIZE, ggc_min_heapsize_heuristic ());
825*38fd1498Szrj #endif
826*38fd1498Szrj }
827*38fd1498Szrj 
828*38fd1498Szrj /* GGC memory usage.  */
829*38fd1498Szrj struct ggc_usage: public mem_usage
830*38fd1498Szrj {
831*38fd1498Szrj   /* Default constructor.  */
ggc_usageggc_usage832*38fd1498Szrj   ggc_usage (): m_freed (0), m_collected (0), m_overhead (0) {}
833*38fd1498Szrj   /* Constructor.  */
ggc_usageggc_usage834*38fd1498Szrj   ggc_usage (size_t allocated, size_t times, size_t peak,
835*38fd1498Szrj 	     size_t freed, size_t collected, size_t overhead)
836*38fd1498Szrj     : mem_usage (allocated, times, peak),
837*38fd1498Szrj     m_freed (freed), m_collected (collected), m_overhead (overhead) {}
838*38fd1498Szrj 
839*38fd1498Szrj   /* Equality operator.  */
840*38fd1498Szrj   inline bool
841*38fd1498Szrj   operator== (const ggc_usage &second) const
842*38fd1498Szrj   {
843*38fd1498Szrj     return (get_balance () == second.get_balance ()
844*38fd1498Szrj 	    && m_peak == second.m_peak
845*38fd1498Szrj 	    && m_times == second.m_times);
846*38fd1498Szrj   }
847*38fd1498Szrj 
848*38fd1498Szrj   /* Comparison operator.  */
849*38fd1498Szrj   inline bool
850*38fd1498Szrj   operator< (const ggc_usage &second) const
851*38fd1498Szrj   {
852*38fd1498Szrj     if (*this == second)
853*38fd1498Szrj       return false;
854*38fd1498Szrj 
855*38fd1498Szrj     return (get_balance () == second.get_balance () ?
856*38fd1498Szrj 	    (m_peak == second.m_peak ? m_times < second.m_times
857*38fd1498Szrj 	     : m_peak < second.m_peak)
858*38fd1498Szrj 	      : get_balance () < second.get_balance ());
859*38fd1498Szrj   }
860*38fd1498Szrj 
861*38fd1498Szrj   /* Register overhead of ALLOCATED and OVERHEAD bytes.  */
862*38fd1498Szrj   inline void
register_overheadggc_usage863*38fd1498Szrj   register_overhead (size_t allocated, size_t overhead)
864*38fd1498Szrj   {
865*38fd1498Szrj     m_allocated += allocated;
866*38fd1498Szrj     m_overhead += overhead;
867*38fd1498Szrj     m_times++;
868*38fd1498Szrj   }
869*38fd1498Szrj 
870*38fd1498Szrj   /* Release overhead of SIZE bytes.  */
871*38fd1498Szrj   inline void
release_overheadggc_usage872*38fd1498Szrj   release_overhead (size_t size)
873*38fd1498Szrj   {
874*38fd1498Szrj     m_freed += size;
875*38fd1498Szrj   }
876*38fd1498Szrj 
877*38fd1498Szrj   /* Sum the usage with SECOND usage.  */
878*38fd1498Szrj   ggc_usage
879*38fd1498Szrj   operator+ (const ggc_usage &second)
880*38fd1498Szrj   {
881*38fd1498Szrj     return ggc_usage (m_allocated + second.m_allocated,
882*38fd1498Szrj 		      m_times + second.m_times,
883*38fd1498Szrj 		      m_peak + second.m_peak,
884*38fd1498Szrj 		      m_freed + second.m_freed,
885*38fd1498Szrj 		      m_collected + second.m_collected,
886*38fd1498Szrj 		      m_overhead + second.m_overhead);
887*38fd1498Szrj   }
888*38fd1498Szrj 
889*38fd1498Szrj   /* Dump usage with PREFIX, where TOTAL is sum of all rows.  */
890*38fd1498Szrj   inline void
dumpggc_usage891*38fd1498Szrj   dump (const char *prefix, ggc_usage &total) const
892*38fd1498Szrj   {
893*38fd1498Szrj     long balance = get_balance ();
894*38fd1498Szrj     fprintf (stderr,
895*38fd1498Szrj 	     "%-48s %10li:%5.1f%%%10li:%5.1f%%"
896*38fd1498Szrj 	     "%10li:%5.1f%%%10li:%5.1f%%%10li\n",
897*38fd1498Szrj 	     prefix, (long)m_collected,
898*38fd1498Szrj 	     get_percent (m_collected, total.m_collected),
899*38fd1498Szrj 	     (long)m_freed, get_percent (m_freed, total.m_freed),
900*38fd1498Szrj 	     (long)balance, get_percent (balance, total.get_balance ()),
901*38fd1498Szrj 	     (long)m_overhead, get_percent (m_overhead, total.m_overhead),
902*38fd1498Szrj 	     (long)m_times);
903*38fd1498Szrj   }
904*38fd1498Szrj 
905*38fd1498Szrj   /* Dump usage coupled to LOC location, where TOTAL is sum of all rows.  */
906*38fd1498Szrj   inline void
dumpggc_usage907*38fd1498Szrj   dump (mem_location *loc, ggc_usage &total) const
908*38fd1498Szrj   {
909*38fd1498Szrj     char *location_string = loc->to_string ();
910*38fd1498Szrj 
911*38fd1498Szrj     dump (location_string, total);
912*38fd1498Szrj 
913*38fd1498Szrj     free (location_string);
914*38fd1498Szrj   }
915*38fd1498Szrj 
916*38fd1498Szrj   /* Dump footer.  */
917*38fd1498Szrj   inline void
dump_footerggc_usage918*38fd1498Szrj   dump_footer ()
919*38fd1498Szrj   {
920*38fd1498Szrj     print_dash_line ();
921*38fd1498Szrj     dump ("Total", *this);
922*38fd1498Szrj     print_dash_line ();
923*38fd1498Szrj   }
924*38fd1498Szrj 
925*38fd1498Szrj   /* Get balance which is GGC allocation leak.  */
926*38fd1498Szrj   inline long
get_balanceggc_usage927*38fd1498Szrj   get_balance () const
928*38fd1498Szrj   {
929*38fd1498Szrj     return m_allocated + m_overhead - m_collected - m_freed;
930*38fd1498Szrj   }
931*38fd1498Szrj 
932*38fd1498Szrj   typedef std::pair<mem_location *, ggc_usage *> mem_pair_t;
933*38fd1498Szrj 
934*38fd1498Szrj   /* Compare wrapper used by qsort method.  */
935*38fd1498Szrj   static int
compareggc_usage936*38fd1498Szrj   compare (const void *first, const void *second)
937*38fd1498Szrj   {
938*38fd1498Szrj     const mem_pair_t f = *(const mem_pair_t *)first;
939*38fd1498Szrj     const mem_pair_t s = *(const mem_pair_t *)second;
940*38fd1498Szrj 
941*38fd1498Szrj     if (*f.second == *s.second)
942*38fd1498Szrj       return 0;
943*38fd1498Szrj 
944*38fd1498Szrj     return *f.second < *s.second ? 1 : -1;
945*38fd1498Szrj   }
946*38fd1498Szrj 
947*38fd1498Szrj   /* Compare rows in final GGC summary dump.  */
948*38fd1498Szrj   static int
compare_finalggc_usage949*38fd1498Szrj   compare_final (const void *first, const void *second)
950*38fd1498Szrj   {
951*38fd1498Szrj     typedef std::pair<mem_location *, ggc_usage *> mem_pair_t;
952*38fd1498Szrj 
953*38fd1498Szrj     const ggc_usage *f = ((const mem_pair_t *)first)->second;
954*38fd1498Szrj     const ggc_usage *s = ((const mem_pair_t *)second)->second;
955*38fd1498Szrj 
956*38fd1498Szrj     size_t a = f->m_allocated + f->m_overhead - f->m_freed;
957*38fd1498Szrj     size_t b = s->m_allocated + s->m_overhead - s->m_freed;
958*38fd1498Szrj 
959*38fd1498Szrj     return a == b ? 0 : (a < b ? 1 : -1);
960*38fd1498Szrj   }
961*38fd1498Szrj 
962*38fd1498Szrj   /* Dump header with NAME.  */
963*38fd1498Szrj   static inline void
dump_headerggc_usage964*38fd1498Szrj   dump_header (const char *name)
965*38fd1498Szrj   {
966*38fd1498Szrj     fprintf (stderr, "%-48s %11s%17s%17s%16s%17s\n", name, "Garbage", "Freed",
967*38fd1498Szrj 	     "Leak", "Overhead", "Times");
968*38fd1498Szrj     print_dash_line ();
969*38fd1498Szrj   }
970*38fd1498Szrj 
971*38fd1498Szrj   /* Freed memory in bytes.  */
972*38fd1498Szrj   size_t m_freed;
973*38fd1498Szrj   /* Collected memory in bytes.  */
974*38fd1498Szrj   size_t m_collected;
975*38fd1498Szrj   /* Overhead memory in bytes.  */
976*38fd1498Szrj   size_t m_overhead;
977*38fd1498Szrj };
978*38fd1498Szrj 
979*38fd1498Szrj /* GCC memory description.  */
980*38fd1498Szrj static mem_alloc_description<ggc_usage> ggc_mem_desc;
981*38fd1498Szrj 
982*38fd1498Szrj /* Dump per-site memory statistics.  */
983*38fd1498Szrj 
984*38fd1498Szrj void
dump_ggc_loc_statistics(bool final)985*38fd1498Szrj dump_ggc_loc_statistics (bool final)
986*38fd1498Szrj {
987*38fd1498Szrj   if (! GATHER_STATISTICS)
988*38fd1498Szrj     return;
989*38fd1498Szrj 
990*38fd1498Szrj   ggc_force_collect = true;
991*38fd1498Szrj   ggc_collect ();
992*38fd1498Szrj 
993*38fd1498Szrj   ggc_mem_desc.dump (GGC_ORIGIN, final ? ggc_usage::compare_final : NULL);
994*38fd1498Szrj 
995*38fd1498Szrj   ggc_force_collect = false;
996*38fd1498Szrj }
997*38fd1498Szrj 
998*38fd1498Szrj /* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION).  */
999*38fd1498Szrj void
ggc_record_overhead(size_t allocated,size_t overhead,void * ptr MEM_STAT_DECL)1000*38fd1498Szrj ggc_record_overhead (size_t allocated, size_t overhead, void *ptr MEM_STAT_DECL)
1001*38fd1498Szrj {
1002*38fd1498Szrj   ggc_usage *usage = ggc_mem_desc.register_descriptor (ptr, GGC_ORIGIN, false
1003*38fd1498Szrj 						       FINAL_PASS_MEM_STAT);
1004*38fd1498Szrj 
1005*38fd1498Szrj   ggc_mem_desc.register_object_overhead (usage, allocated + overhead, ptr);
1006*38fd1498Szrj   usage->register_overhead (allocated, overhead);
1007*38fd1498Szrj }
1008*38fd1498Szrj 
1009*38fd1498Szrj /* Notice that the pointer has been freed.  */
1010*38fd1498Szrj void
ggc_free_overhead(void * ptr)1011*38fd1498Szrj ggc_free_overhead (void *ptr)
1012*38fd1498Szrj {
1013*38fd1498Szrj   ggc_mem_desc.release_object_overhead (ptr);
1014*38fd1498Szrj }
1015*38fd1498Szrj 
1016*38fd1498Szrj /* After live values has been marked, walk all recorded pointers and see if
1017*38fd1498Szrj    they are still live.  */
1018*38fd1498Szrj void
ggc_prune_overhead_list(void)1019*38fd1498Szrj ggc_prune_overhead_list (void)
1020*38fd1498Szrj {
1021*38fd1498Szrj   typedef hash_map<const void *, std::pair<ggc_usage *, size_t > > map_t;
1022*38fd1498Szrj 
1023*38fd1498Szrj   map_t::iterator it = ggc_mem_desc.m_reverse_object_map->begin ();
1024*38fd1498Szrj 
1025*38fd1498Szrj   for (; it != ggc_mem_desc.m_reverse_object_map->end (); ++it)
1026*38fd1498Szrj     if (!ggc_marked_p ((*it).first))
1027*38fd1498Szrj       (*it).second.first->m_collected += (*it).second.second;
1028*38fd1498Szrj 
1029*38fd1498Szrj   delete ggc_mem_desc.m_reverse_object_map;
1030*38fd1498Szrj   ggc_mem_desc.m_reverse_object_map = new map_t (13, false, false);
1031*38fd1498Szrj }
1032