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