]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ggc-common.cc
build: Use of cargo not yet supported here in Canadian cross configurations
[thirdparty/gcc.git] / gcc / ggc-common.cc
CommitLineData
b49a6a90 1/* Simple garbage collection for the GNU compiler.
a945c346 2 Copyright (C) 1999-2024 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 42static int compare_ptr_data (const void *, const void *);
747380f4 43static void relocate_ptrs (void *, void *, void *);
20c1dc5e
AJ
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
7525707c
ST
78/* Zero out all the roots in the table RT. */
79
80static void
81ggc_zero_rtab_roots (const_ggc_root_tab_t rt)
82{
83 size_t i;
84
85 for ( ; rt->base != NULL; rt++)
86 for (i = 0; i < rt->nelt; i++)
87 (*(void **) ((char *)rt->base + rt->stride * i)) = (void*)0;
88}
89
cb2ec151
RH
90/* Iterate through all registered roots and mark each element. */
91
b49a6a90 92void
20c1dc5e 93ggc_mark_roots (void)
96df4529 94{
e2500fed 95 const struct ggc_root_tab *const *rt;
71bb2d86 96 const_ggc_root_tab_t rtp, rti;
e2500fed 97 size_t i;
589005ff 98
e2500fed
GK
99 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
100 for (rti = *rt; rti->base != NULL; rti++)
eaa8e854 101 memset (rti->base, 0, rti->stride * rti->nelt);
e2500fed
GK
102
103 for (rt = gt_ggc_rtab; *rt; rt++)
71bb2d86 104 ggc_mark_root_tab (*rt);
ae2392a9 105
9771b263 106 FOR_EACH_VEC_ELT (extra_root_vec, i, rtp)
71bb2d86 107 ggc_mark_root_tab (rtp);
bedda2da 108
dae4174e
TT
109 if (ggc_protect_identifiers)
110 ggc_mark_stringpool ();
bedda2da 111
aebf76a2
TS
112 gt_clear_caches ();
113
dae4174e
TT
114 if (! ggc_protect_identifiers)
115 ggc_purge_stringpool ();
ae2392a9
BS
116
117 /* Some plugins may call ggc_set_mark from here. */
118 invoke_plugin_callbacks (PLUGIN_GGC_MARKING, NULL);
96df4529
AS
119}
120
e2500fed
GK
121/* Allocate a block of memory, then clear it. */
122void *
de49ce19
TS
123ggc_internal_cleared_alloc (size_t size, void (*f)(void *), size_t s, size_t n
124 MEM_STAT_DECL)
ef8288f7 125{
de49ce19 126 void *buf = ggc_internal_alloc (size, f, s, n PASS_MEM_STAT);
e2500fed
GK
127 memset (buf, 0, size);
128 return buf;
ef8288f7
RH
129}
130
e2500fed
GK
131/* Resize a block of memory, possibly re-allocating it. */
132void *
231120e5 133ggc_realloc (void *x, size_t size MEM_STAT_DECL)
ef8288f7 134{
e2500fed
GK
135 void *r;
136 size_t old_size;
ef8288f7 137
e2500fed 138 if (x == NULL)
231120e5 139 return ggc_internal_alloc (size PASS_MEM_STAT);
ef8288f7 140
e2500fed 141 old_size = ggc_get_size (x);
685fe032 142
e2500fed 143 if (size <= old_size)
9a0a7d5d
HPN
144 {
145 /* Mark the unwanted memory as unaccessible. We also need to make
146 the "new" size accessible, since ggc_get_size returns the size of
147 the pool, not the size of the individually allocated object, the
148 size which was previously made accessible. Unfortunately, we
149 don't know that previously allocated size. Without that
150 knowledge we have to lose some initialization-tracking for the
151 old parts of the object. An alternative is to mark the whole
20c1dc5e 152 old_size as reachable, but that would lose tracking of writes
9a0a7d5d
HPN
153 after the end of the object (by small offsets). Discard the
154 handle to avoid handle leak. */
35dee980
HPN
155 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) x + size,
156 old_size - size));
157 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, size));
9a0a7d5d
HPN
158 return x;
159 }
ef8288f7 160
231120e5 161 r = ggc_internal_alloc (size PASS_MEM_STAT);
9a0a7d5d
HPN
162
163 /* Since ggc_get_size returns the size of the pool, not the size of the
164 individually allocated object, we'd access parts of the old object
165 that were marked invalid with the memcpy below. We lose a bit of the
166 initialization-tracking since some of it may be uninitialized. */
35dee980 167 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (x, old_size));
9a0a7d5d 168
e2500fed 169 memcpy (r, x, old_size);
9a0a7d5d
HPN
170
171 /* The old object is not supposed to be used anymore. */
685fe032 172 ggc_free (x);
9a0a7d5d 173
e2500fed 174 return r;
ef8288f7
RH
175}
176
f8a83ee3 177void *
a9429e29
LB
178ggc_cleared_alloc_htab_ignore_args (size_t c ATTRIBUTE_UNUSED,
179 size_t n ATTRIBUTE_UNUSED)
f8a83ee3 180{
a9429e29 181 gcc_assert (c * n == sizeof (struct htab));
766090c2 182 return ggc_cleared_alloc<htab> ();
a9429e29
LB
183}
184
185/* TODO: once we actually use type information in GGC, create a new tag
186 gt_gcc_ptr_array and use it for pointer arrays. */
187void *
188ggc_cleared_alloc_ptr_array_two_args (size_t c, size_t n)
189{
4b865081
ML
190 gcc_assert (sizeof (void **) == n);
191 return ggc_cleared_vec_alloc<void **> (c);
f8a83ee3
ZW
192}
193
17211ab5 194/* These are for splay_tree_new_ggc. */
20c1dc5e 195void *
cd030c07 196ggc_splay_alloc (int sz, void *nl)
17211ab5 197{
282899df 198 gcc_assert (!nl);
a9429e29 199 return ggc_internal_alloc (sz);
17211ab5
GK
200}
201
202void
20c1dc5e 203ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl)
17211ab5 204{
282899df 205 gcc_assert (!nl);
17211ab5
GK
206}
207
3277221c 208void
20c1dc5e
AJ
209ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED,
210 ggc_statistics *stats)
3277221c 211{
3277221c
MM
212 /* Set the pointer so that during collection we will actually gather
213 the statistics. */
214 ggc_stats = stats;
215
216 /* Then do one collection to fill in the statistics. */
217 ggc_collect ();
218
17211ab5
GK
219 /* At present, we don't really gather any interesting statistics. */
220
221 /* Don't gather statistics any more. */
222 ggc_stats = NULL;
223}
224\f
225/* Functions for saving and restoring GCable memory to disk. */
226
20c1dc5e 227struct ptr_data
17211ab5
GK
228{
229 void *obj;
230 void *note_ptr_cookie;
231 gt_note_pointers note_ptr_fn;
232 gt_handle_reorder reorder_fn;
233 size_t size;
234 void *new_addr;
235};
236
9204da15 237#define POINTER_HASH(x) (hashval_t)((intptr_t)x >> 3)
17211ab5 238
4a8fb1a1
LC
239/* Helper for hashing saving_htab. */
240
95fbe13e 241struct saving_hasher : free_ptr_hash <ptr_data>
4a8fb1a1 242{
67f58944
TS
243 typedef void *compare_type;
244 static inline hashval_t hash (const ptr_data *);
245 static inline bool equal (const ptr_data *, const void *);
4a8fb1a1
LC
246};
247
248inline hashval_t
67f58944 249saving_hasher::hash (const ptr_data *p)
4a8fb1a1
LC
250{
251 return POINTER_HASH (p->obj);
252}
253
254inline bool
67f58944 255saving_hasher::equal (const ptr_data *p1, const void *p2)
4a8fb1a1
LC
256{
257 return p1->obj == p2;
258}
259
c203e8a7 260static hash_table<saving_hasher> *saving_htab;
fe7c3ecf 261static vec<void *> callback_vec;
747380f4 262static vec<void *> reloc_addrs_vec;
4a8fb1a1 263
17211ab5
GK
264/* Register an object in the hash table. */
265
266int
20c1dc5e 267gt_pch_note_object (void *obj, void *note_ptr_cookie,
f3b957ea
LH
268 gt_note_pointers note_ptr_fn,
269 size_t length_override)
17211ab5
GK
270{
271 struct ptr_data **slot;
20c1dc5e 272
17211ab5
GK
273 if (obj == NULL || obj == (void *) 1)
274 return 0;
275
276 slot = (struct ptr_data **)
c203e8a7 277 saving_htab->find_slot_with_hash (obj, POINTER_HASH (obj), INSERT);
17211ab5
GK
278 if (*slot != NULL)
279 {
282899df
NS
280 gcc_assert ((*slot)->note_ptr_fn == note_ptr_fn
281 && (*slot)->note_ptr_cookie == note_ptr_cookie);
17211ab5
GK
282 return 0;
283 }
20c1dc5e 284
d3bfe4de 285 *slot = XCNEW (struct ptr_data);
17211ab5
GK
286 (*slot)->obj = obj;
287 (*slot)->note_ptr_fn = note_ptr_fn;
288 (*slot)->note_ptr_cookie = note_ptr_cookie;
f3b957ea
LH
289 if (length_override != (size_t)-1)
290 (*slot)->size = length_override;
291 else if (note_ptr_fn == gt_pch_p_S)
d3bfe4de 292 (*slot)->size = strlen ((const char *)obj) + 1;
17211ab5
GK
293 else
294 (*slot)->size = ggc_get_size (obj);
295 return 1;
296}
297
fe7c3ecf
JJ
298/* Register address of a callback pointer. */
299void
300gt_pch_note_callback (void *obj, void *base)
301{
302 void *ptr;
303 memcpy (&ptr, obj, sizeof (void *));
304 if (ptr != NULL)
305 {
306 struct ptr_data *data
307 = (struct ptr_data *)
308 saving_htab->find_with_hash (base, POINTER_HASH (base));
309 gcc_assert (data);
310 callback_vec.safe_push ((char *) data->new_addr
311 + ((char *) obj - (char *) base));
312 }
313}
314
17211ab5
GK
315/* Register an object in the hash table. */
316
317void
20c1dc5e
AJ
318gt_pch_note_reorder (void *obj, void *note_ptr_cookie,
319 gt_handle_reorder reorder_fn)
17211ab5
GK
320{
321 struct ptr_data *data;
20c1dc5e 322
17211ab5
GK
323 if (obj == NULL || obj == (void *) 1)
324 return;
325
d3bfe4de 326 data = (struct ptr_data *)
c203e8a7 327 saving_htab->find_with_hash (obj, POINTER_HASH (obj));
282899df 328 gcc_assert (data && data->note_ptr_cookie == note_ptr_cookie);
79a90e69
TS
329 /* The GTY 'reorder' option doesn't make sense if we don't walk pointers,
330 such as for strings. */
331 gcc_checking_assert (data->note_ptr_fn != gt_pch_p_S);
20c1dc5e 332
17211ab5
GK
333 data->reorder_fn = reorder_fn;
334}
335
17211ab5
GK
336/* Handy state for the traversal functions. */
337
20c1dc5e 338struct traversal_state
17211ab5
GK
339{
340 FILE *f;
341 struct ggc_pch_data *d;
342 size_t count;
343 struct ptr_data **ptrs;
344 size_t ptrs_i;
345};
346
347/* Callbacks for htab_traverse. */
348
4a8fb1a1
LC
349int
350ggc_call_count (ptr_data **slot, traversal_state *state)
17211ab5 351{
4a8fb1a1 352 struct ptr_data *d = *slot;
20c1dc5e 353
bbbe2dc1 354 ggc_pch_count_object (state->d, d->obj, d->size);
17211ab5
GK
355 state->count++;
356 return 1;
357}
358
4a8fb1a1
LC
359int
360ggc_call_alloc (ptr_data **slot, traversal_state *state)
17211ab5 361{
4a8fb1a1 362 struct ptr_data *d = *slot;
20c1dc5e 363
bbbe2dc1 364 d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size);
17211ab5
GK
365 state->ptrs[state->ptrs_i++] = d;
366 return 1;
367}
368
369/* Callback for qsort. */
370
371static int
20c1dc5e 372compare_ptr_data (const void *p1_p, const void *p2_p)
17211ab5 373{
58f9752a
KG
374 const struct ptr_data *const p1 = *(const struct ptr_data *const *)p1_p;
375 const struct ptr_data *const p2 = *(const struct ptr_data *const *)p2_p;
17211ab5
GK
376 return (((size_t)p1->new_addr > (size_t)p2->new_addr)
377 - ((size_t)p1->new_addr < (size_t)p2->new_addr));
378}
379
380/* Callbacks for note_ptr_fn. */
381
382static void
747380f4 383relocate_ptrs (void *ptr_p, void *real_ptr_p, void *state_p)
17211ab5
GK
384{
385 void **ptr = (void **)ptr_p;
747380f4 386 struct traversal_state *state
17211ab5
GK
387 = (struct traversal_state *)state_p;
388 struct ptr_data *result;
389
390 if (*ptr == NULL || *ptr == (void *)1)
391 return;
20c1dc5e 392
d3bfe4de 393 result = (struct ptr_data *)
c203e8a7 394 saving_htab->find_with_hash (*ptr, POINTER_HASH (*ptr));
282899df 395 gcc_assert (result);
17211ab5 396 *ptr = result->new_addr;
747380f4
JJ
397 if (ptr_p == real_ptr_p)
398 return;
399 if (real_ptr_p == NULL)
400 real_ptr_p = ptr_p;
401 gcc_assert (real_ptr_p >= state->ptrs[state->ptrs_i]->obj
402 && ((char *) real_ptr_p + sizeof (void *)
403 <= ((char *) state->ptrs[state->ptrs_i]->obj
404 + state->ptrs[state->ptrs_i]->size)));
405 void *addr
406 = (void *) ((char *) state->ptrs[state->ptrs_i]->new_addr
407 + ((char *) real_ptr_p
408 - (char *) state->ptrs[state->ptrs_i]->obj));
409 reloc_addrs_vec.safe_push (addr);
17211ab5
GK
410}
411
412/* Write out, after relocation, the pointers in TAB. */
413static void
20c1dc5e
AJ
414write_pch_globals (const struct ggc_root_tab * const *tab,
415 struct traversal_state *state)
17211ab5
GK
416{
417 const struct ggc_root_tab *const *rt;
418 const struct ggc_root_tab *rti;
419 size_t i;
420
421 for (rt = tab; *rt; rt++)
422 for (rti = *rt; rti->base != NULL; rti++)
423 for (i = 0; i < rti->nelt; i++)
424 {
425 void *ptr = *(void **)((char *)rti->base + rti->stride * i);
426 struct ptr_data *new_ptr;
427 if (ptr == NULL || ptr == (void *)1)
428 {
20c1dc5e 429 if (fwrite (&ptr, sizeof (void *), 1, state->f)
17211ab5 430 != 1)
a9c697b8 431 fatal_error (input_location, "cannot write PCH file: %m");
17211ab5
GK
432 }
433 else
434 {
d3bfe4de 435 new_ptr = (struct ptr_data *)
c203e8a7 436 saving_htab->find_with_hash (ptr, POINTER_HASH (ptr));
20c1dc5e 437 if (fwrite (&new_ptr->new_addr, sizeof (void *), 1, state->f)
17211ab5 438 != 1)
a9c697b8 439 fatal_error (input_location, "cannot write PCH file: %m");
17211ab5
GK
440 }
441 }
442}
443
747380f4
JJ
444/* Callback for qsort. */
445
446static int
447compare_ptr (const void *p1_p, const void *p2_p)
448{
449 void *p1 = *(void *const *)p1_p;
450 void *p2 = *(void *const *)p2_p;
451 return (((uintptr_t)p1 > (uintptr_t)p2)
452 - ((uintptr_t)p1 < (uintptr_t)p2));
453}
454
455/* Decode one uleb128 from P, return first byte after it, store
456 decoded value into *VAL. */
457
458static unsigned char *
459read_uleb128 (unsigned char *p, size_t *val)
460{
461 unsigned int shift = 0;
462 unsigned char byte;
463 size_t result;
464
465 result = 0;
466 do
467 {
468 byte = *p++;
469 result |= ((size_t) byte & 0x7f) << shift;
470 shift += 7;
471 }
472 while (byte & 0x80);
473
474 *val = result;
475 return p;
476}
477
478/* Store VAL as uleb128 at P, return length in bytes. */
479
480static size_t
481write_uleb128 (unsigned char *p, size_t val)
482{
483 size_t len = 0;
484 do
485 {
486 unsigned char byte = (val & 0x7f);
487 val >>= 7;
488 if (val != 0)
489 /* More bytes to follow. */
490 byte |= 0x80;
491
492 *p++ = byte;
493 ++len;
494 }
495 while (val != 0);
496 return len;
497}
498
17211ab5
GK
499/* Hold the information we need to mmap the file back in. */
500
20c1dc5e 501struct mmap_info
17211ab5
GK
502{
503 size_t offset;
504 size_t size;
505 void *preferred_base;
506};
507
508/* Write out the state of the compiler to F. */
509
510void
20c1dc5e 511gt_pch_save (FILE *f)
17211ab5
GK
512{
513 const struct ggc_root_tab *const *rt;
514 const struct ggc_root_tab *rti;
515 size_t i;
516 struct traversal_state state;
517 char *this_object = NULL;
518 size_t this_object_size = 0;
519 struct mmap_info mmi;
c3284718 520 const size_t mmap_offset_alignment = host_hooks.gt_pch_alloc_granularity ();
17211ab5
GK
521
522 gt_pch_save_stringpool ();
523
10d43c2d 524 timevar_push (TV_PCH_PTR_REALLOC);
c203e8a7 525 saving_htab = new hash_table<saving_hasher> (50000);
17211ab5
GK
526
527 for (rt = gt_ggc_rtab; *rt; rt++)
528 for (rti = *rt; rti->base != NULL; rti++)
529 for (i = 0; i < rti->nelt; i++)
530 (*rti->pchw)(*(void **)((char *)rti->base + rti->stride * i));
531
17211ab5
GK
532 /* Prepare the objects for writing, determine addresses and such. */
533 state.f = f;
a9429e29 534 state.d = init_ggc_pch ();
17211ab5 535 state.count = 0;
c203e8a7 536 saving_htab->traverse <traversal_state *, ggc_call_count> (&state);
17211ab5
GK
537
538 mmi.size = ggc_pch_total_size (state.d);
539
18c81520
GK
540 /* Try to arrange things so that no relocation is necessary, but
541 don't try very hard. On most platforms, this will always work,
b8698a0f 542 and on the rest it's a lot of work to do better.
18c81520
GK
543 (The extra work goes in HOST_HOOKS_GT_PCH_GET_ADDRESS and
544 HOST_HOOKS_GT_PCH_USE_ADDRESS.) */
4d0c31e6 545 mmi.preferred_base = host_hooks.gt_pch_get_address (mmi.size, fileno (f));
e4641191
IS
546 /* If the host cannot supply any suitable address for this, we are stuck. */
547 if (mmi.preferred_base == NULL)
548 fatal_error (input_location,
549 "cannot write PCH file: required memory segment unavailable");
b8698a0f 550
17211ab5
GK
551 ggc_pch_this_base (state.d, mmi.preferred_base);
552
5ed6ace5 553 state.ptrs = XNEWVEC (struct ptr_data *, state.count);
17211ab5 554 state.ptrs_i = 0;
10d43c2d 555
c203e8a7 556 saving_htab->traverse <traversal_state *, ggc_call_alloc> (&state);
10d43c2d
DN
557 timevar_pop (TV_PCH_PTR_REALLOC);
558
559 timevar_push (TV_PCH_PTR_SORT);
17211ab5 560 qsort (state.ptrs, state.count, sizeof (*state.ptrs), compare_ptr_data);
10d43c2d 561 timevar_pop (TV_PCH_PTR_SORT);
17211ab5
GK
562
563 /* Write out all the scalar variables. */
564 for (rt = gt_pch_scalar_rtab; *rt; rt++)
565 for (rti = *rt; rti->base != NULL; rti++)
566 if (fwrite (rti->base, rti->stride, 1, f) != 1)
a9c697b8 567 fatal_error (input_location, "cannot write PCH file: %m");
17211ab5
GK
568
569 /* Write out all the global pointers, after translation. */
570 write_pch_globals (gt_ggc_rtab, &state);
17211ab5 571
90aa6719
DS
572 /* Pad the PCH file so that the mmapped area starts on an allocation
573 granularity (usually page) boundary. */
17211ab5 574 {
70f8b89f
KG
575 long o;
576 o = ftell (state.f) + sizeof (mmi);
577 if (o == -1)
a9c697b8 578 fatal_error (input_location, "cannot get position in PCH file: %m");
90aa6719
DS
579 mmi.offset = mmap_offset_alignment - o % mmap_offset_alignment;
580 if (mmi.offset == mmap_offset_alignment)
17211ab5
GK
581 mmi.offset = 0;
582 mmi.offset += o;
583 }
584 if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1)
a9c697b8 585 fatal_error (input_location, "cannot write PCH file: %m");
17211ab5
GK
586 if (mmi.offset != 0
587 && fseek (state.f, mmi.offset, SEEK_SET) != 0)
a9c697b8 588 fatal_error (input_location, "cannot write padding to PCH file: %m");
17211ab5 589
08cee789
DJ
590 ggc_pch_prepare_write (state.d, state.f);
591
c132770e 592#if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
0b50e654
JJ
593 vec<char> vbits = vNULL;
594#endif
595
17211ab5
GK
596 /* Actually write out the objects. */
597 for (i = 0; i < state.count; i++)
3277221c 598 {
747380f4 599 state.ptrs_i = i;
17211ab5
GK
600 if (this_object_size < state.ptrs[i]->size)
601 {
602 this_object_size = state.ptrs[i]->size;
d3bfe4de 603 this_object = XRESIZEVAR (char, this_object, this_object_size);
17211ab5 604 }
c132770e 605#if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
0b50e654
JJ
606 /* obj might contain uninitialized bytes, e.g. in the trailing
607 padding of the object. Avoid warnings by making the memory
608 temporarily defined and then restoring previous state. */
609 int get_vbits = 0;
610 size_t valid_size = state.ptrs[i]->size;
22d9c880 611 if (UNLIKELY (RUNNING_ON_VALGRIND))
0b50e654
JJ
612 {
613 if (vbits.length () < valid_size)
1ad755dc 614 vbits.safe_grow (valid_size, true);
0b50e654
JJ
615 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
616 vbits.address (), valid_size);
617 if (get_vbits == 3)
618 {
619 /* We assume that first part of obj is addressable, and
620 the rest is unaddressable. Find out where the boundary is
621 using binary search. */
622 size_t lo = 0, hi = valid_size;
623 while (hi > lo)
624 {
625 size_t mid = (lo + hi) / 2;
626 get_vbits = VALGRIND_GET_VBITS ((char *) state.ptrs[i]->obj
627 + mid, vbits.address (),
628 1);
629 if (get_vbits == 3)
630 hi = mid;
631 else if (get_vbits == 1)
632 lo = mid + 1;
633 else
634 break;
635 }
636 if (get_vbits == 1 || get_vbits == 3)
637 {
638 valid_size = lo;
639 get_vbits = VALGRIND_GET_VBITS (state.ptrs[i]->obj,
640 vbits.address (),
641 valid_size);
642 }
643 }
644 if (get_vbits == 1)
645 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (state.ptrs[i]->obj,
646 state.ptrs[i]->size));
647 }
648#endif
17211ab5
GK
649 memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size);
650 if (state.ptrs[i]->reorder_fn != NULL)
20c1dc5e 651 state.ptrs[i]->reorder_fn (state.ptrs[i]->obj,
17211ab5
GK
652 state.ptrs[i]->note_ptr_cookie,
653 relocate_ptrs, &state);
79a90e69
TS
654 gt_note_pointers note_ptr_fn = state.ptrs[i]->note_ptr_fn;
655 gcc_checking_assert (note_ptr_fn != NULL);
656 /* 'gt_pch_p_S' enables certain special handling, but otherwise
657 corresponds to no 'note_ptr_fn'. */
658 if (note_ptr_fn == gt_pch_p_S)
659 note_ptr_fn = NULL;
660 if (note_ptr_fn != NULL)
661 note_ptr_fn (state.ptrs[i]->obj, state.ptrs[i]->note_ptr_cookie,
662 relocate_ptrs, &state);
17211ab5 663 ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj,
bbbe2dc1 664 state.ptrs[i]->new_addr, state.ptrs[i]->size);
79a90e69
TS
665 if (state.ptrs[i]->reorder_fn != NULL
666 || note_ptr_fn != NULL)
17211ab5 667 memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size);
c132770e 668#if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
22d9c880 669 if (UNLIKELY (get_vbits == 1))
0b50e654
JJ
670 {
671 (void) VALGRIND_SET_VBITS (state.ptrs[i]->obj, vbits.address (),
672 valid_size);
673 if (valid_size != state.ptrs[i]->size)
674 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *)
675 state.ptrs[i]->obj
676 + valid_size,
677 state.ptrs[i]->size
678 - valid_size));
679 }
680#endif
3277221c 681 }
c132770e 682#if defined ENABLE_VALGRIND_ANNOTATIONS && defined VALGRIND_GET_VBITS
0b50e654
JJ
683 vbits.release ();
684#endif
685
747380f4
JJ
686 reloc_addrs_vec.qsort (compare_ptr);
687
688 size_t reloc_addrs_size = 0;
689 void *last_addr = NULL;
690 unsigned char uleb128_buf[sizeof (size_t) * 2];
691 for (void *addr : reloc_addrs_vec)
692 {
693 gcc_assert ((uintptr_t) addr >= (uintptr_t) mmi.preferred_base
694 && ((uintptr_t) addr + sizeof (void *)
a4e24064 695 <= (uintptr_t) mmi.preferred_base + mmi.size));
747380f4
JJ
696 if (addr == last_addr)
697 continue;
698 if (last_addr == NULL)
699 last_addr = mmi.preferred_base;
700 size_t diff = (uintptr_t) addr - (uintptr_t) last_addr;
701 reloc_addrs_size += write_uleb128 (uleb128_buf, diff);
702 last_addr = addr;
703 }
704 if (fwrite (&reloc_addrs_size, sizeof (reloc_addrs_size), 1, f) != 1)
705 fatal_error (input_location, "cannot write PCH file: %m");
706 last_addr = NULL;
707 for (void *addr : reloc_addrs_vec)
708 {
709 if (addr == last_addr)
710 continue;
711 if (last_addr == NULL)
712 last_addr = mmi.preferred_base;
713 size_t diff = (uintptr_t) addr - (uintptr_t) last_addr;
714 reloc_addrs_size = write_uleb128 (uleb128_buf, diff);
715 if (fwrite (uleb128_buf, 1, reloc_addrs_size, f) != reloc_addrs_size)
716 fatal_error (input_location, "cannot write PCH file: %m");
717 last_addr = addr;
718 }
719
17211ab5 720 ggc_pch_finish (state.d, state.f);
747380f4 721
d24ecd21 722 gt_pch_fixup_stringpool ();
17211ab5 723
fe7c3ecf
JJ
724 unsigned num_callbacks = callback_vec.length ();
725 void (*pch_save) (FILE *) = &gt_pch_save;
726 if (fwrite (&pch_save, sizeof (pch_save), 1, f) != 1
727 || fwrite (&num_callbacks, sizeof (num_callbacks), 1, f) != 1
728 || (num_callbacks
729 && fwrite (callback_vec.address (), sizeof (void *), num_callbacks,
730 f) != num_callbacks))
731 fatal_error (input_location, "cannot write PCH file: %m");
732
0b50e654
JJ
733 XDELETE (state.ptrs);
734 XDELETE (this_object);
c203e8a7
TS
735 delete saving_htab;
736 saving_htab = NULL;
fe7c3ecf 737 callback_vec.release ();
747380f4 738 reloc_addrs_vec.release ();
17211ab5
GK
739}
740
741/* Read the state of the compiler back in from F. */
742
743void
20c1dc5e 744gt_pch_restore (FILE *f)
17211ab5
GK
745{
746 const struct ggc_root_tab *const *rt;
747 const struct ggc_root_tab *rti;
748 size_t i;
749 struct mmap_info mmi;
4d0c31e6 750 int result;
17211ab5 751
e4641191
IS
752 /* We are about to reload the line maps along with the rest of the PCH
753 data, which means that the (loaded) ones cannot be guaranteed to be
754 in any valid state for reporting diagnostics that happen during the
755 load. Save the current table (and use it during the loading process
756 below). */
757 class line_maps *save_line_table = line_table;
758
17211ab5
GK
759 /* Delete any deletable objects. This makes ggc_pch_read much
760 faster, as it can be sure that no GCable objects remain other
761 than the ones just read in. */
762 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
763 for (rti = *rt; rti->base != NULL; rti++)
764 memset (rti->base, 0, rti->stride);
765
766 /* Read in all the scalar variables. */
767 for (rt = gt_pch_scalar_rtab; *rt; rt++)
768 for (rti = *rt; rti->base != NULL; rti++)
769 if (fread (rti->base, rti->stride, 1, f) != 1)
a9c697b8 770 fatal_error (input_location, "cannot read PCH file: %m");
17211ab5
GK
771
772 /* Read in all the global pointers, in 6 easy loops. */
e4641191 773 bool error_reading_pointers = false;
17211ab5
GK
774 for (rt = gt_ggc_rtab; *rt; rt++)
775 for (rti = *rt; rti->base != NULL; rti++)
776 for (i = 0; i < rti->nelt; i++)
777 if (fread ((char *)rti->base + rti->stride * i,
778 sizeof (void *), 1, f) != 1)
e4641191
IS
779 error_reading_pointers = true;
780
781 /* Stash the newly read-in line table pointer - it does not point to
782 anything meaningful yet, so swap the old one back in. */
783 class line_maps *new_line_table = line_table;
784 line_table = save_line_table;
785 if (error_reading_pointers)
786 fatal_error (input_location, "cannot read PCH file: %m");
17211ab5 787
17211ab5 788 if (fread (&mmi, sizeof (mmi), 1, f) != 1)
a9c697b8 789 fatal_error (input_location, "cannot read PCH file: %m");
20c1dc5e 790
747380f4 791 void *orig_preferred_base = mmi.preferred_base;
4d0c31e6
RH
792 result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size,
793 fileno (f), mmi.offset);
e4641191
IS
794
795 /* We could not mmap or otherwise allocate the required memory at the
796 address needed. */
4d0c31e6 797 if (result < 0)
e4641191 798 {
747380f4 799 sorry_at (input_location, "PCH allocation failure");
e4641191
IS
800 /* There is no point in continuing from here, we will only end up
801 with a crashed (most likely hanging) compiler. */
802 exit (-1);
803 }
804
805 /* (0) We allocated memory, but did not mmap the file, so we need to read
806 the data in manually. (>0) Otherwise the mmap succeed for the address
807 we wanted. */
4d0c31e6 808 if (result == 0)
18c81520 809 {
4d0c31e6
RH
810 if (fseek (f, mmi.offset, SEEK_SET) != 0
811 || fread (mmi.preferred_base, mmi.size, 1, f) != 1)
a9c697b8 812 fatal_error (input_location, "cannot read PCH file: %m");
4d0c31e6
RH
813 }
814 else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0)
a9c697b8 815 fatal_error (input_location, "cannot read PCH file: %m");
8eb6a092 816
747380f4
JJ
817 size_t reloc_addrs_size;
818 if (fread (&reloc_addrs_size, sizeof (reloc_addrs_size), 1, f) != 1)
819 fatal_error (input_location, "cannot read PCH file: %m");
18c81520 820
747380f4
JJ
821 if (orig_preferred_base != mmi.preferred_base)
822 {
823 uintptr_t bias
824 = (uintptr_t) mmi.preferred_base - (uintptr_t) orig_preferred_base;
825
826 /* Adjust all the global pointers by bias. */
827 line_table = new_line_table;
828 for (rt = gt_ggc_rtab; *rt; rt++)
829 for (rti = *rt; rti->base != NULL; rti++)
830 for (i = 0; i < rti->nelt; i++)
831 {
832 char *addr = (char *)rti->base + rti->stride * i;
833 char *p;
834 memcpy (&p, addr, sizeof (void *));
835 if ((uintptr_t) p >= (uintptr_t) orig_preferred_base
836 && (uintptr_t) p < (uintptr_t) orig_preferred_base + mmi.size)
837 {
838 p = (char *) ((uintptr_t) p + bias);
839 memcpy (addr, &p, sizeof (void *));
840 }
841 }
842 new_line_table = line_table;
843 line_table = save_line_table;
844
845 /* And adjust all the pointers in the image by bias too. */
846 char *addr = (char *) mmi.preferred_base;
847 unsigned char uleb128_buf[4096], *uleb128_ptr = uleb128_buf;
848 while (reloc_addrs_size != 0)
849 {
850 size_t this_size
851 = MIN (reloc_addrs_size,
852 (size_t) (4096 - (uleb128_ptr - uleb128_buf)));
853 if (fread (uleb128_ptr, 1, this_size, f) != this_size)
854 fatal_error (input_location, "cannot read PCH file: %m");
855 unsigned char *uleb128_end = uleb128_ptr + this_size;
856 if (this_size != reloc_addrs_size)
857 uleb128_end -= 2 * sizeof (size_t);
858 uleb128_ptr = uleb128_buf;
859 while (uleb128_ptr < uleb128_end)
860 {
861 size_t diff;
862 uleb128_ptr = read_uleb128 (uleb128_ptr, &diff);
863 addr = (char *) ((uintptr_t) addr + diff);
864
865 char *p;
866 memcpy (&p, addr, sizeof (void *));
867 gcc_assert ((uintptr_t) p >= (uintptr_t) orig_preferred_base
868 && ((uintptr_t) p
869 < (uintptr_t) orig_preferred_base + mmi.size));
870 p = (char *) ((uintptr_t) p + bias);
871 memcpy (addr, &p, sizeof (void *));
872 }
873 reloc_addrs_size -= this_size;
874 if (reloc_addrs_size == 0)
875 break;
876 this_size = uleb128_end + 2 * sizeof (size_t) - uleb128_ptr;
877 memcpy (uleb128_buf, uleb128_ptr, this_size);
878 uleb128_ptr = uleb128_buf + this_size;
879 }
880 }
881 else if (fseek (f, (mmi.offset + mmi.size + sizeof (reloc_addrs_size)
882 + reloc_addrs_size), SEEK_SET) != 0)
883 fatal_error (input_location, "cannot read PCH file: %m");
884
885 ggc_pch_read (f, mmi.preferred_base);
e4641191 886
fe7c3ecf
JJ
887 void (*pch_save) (FILE *);
888 unsigned num_callbacks;
889 if (fread (&pch_save, sizeof (pch_save), 1, f) != 1
890 || fread (&num_callbacks, sizeof (num_callbacks), 1, f) != 1)
891 fatal_error (input_location, "cannot read PCH file: %m");
892 if (pch_save != &gt_pch_save)
893 {
747380f4 894 uintptr_t binbias = (uintptr_t) &gt_pch_save - (uintptr_t) pch_save;
fe7c3ecf
JJ
895 void **ptrs = XNEWVEC (void *, num_callbacks);
896 unsigned i;
747380f4
JJ
897 uintptr_t bias
898 = (uintptr_t) mmi.preferred_base - (uintptr_t) orig_preferred_base;
fe7c3ecf
JJ
899
900 if (fread (ptrs, sizeof (void *), num_callbacks, f) != num_callbacks)
901 fatal_error (input_location, "cannot read PCH file: %m");
902 for (i = 0; i < num_callbacks; ++i)
903 {
747380f4
JJ
904 void *ptr = (void *) ((uintptr_t) ptrs[i] + bias);
905 memcpy (&pch_save, ptr, sizeof (pch_save));
906 pch_save = (void (*) (FILE *)) ((uintptr_t) pch_save + binbias);
907 memcpy (ptr, &pch_save, sizeof (pch_save));
fe7c3ecf
JJ
908 }
909 XDELETE (ptrs);
910 }
911 else if (fseek (f, num_callbacks * sizeof (void *), SEEK_CUR) != 0)
912 fatal_error (input_location, "cannot read PCH file: %m");
913
747380f4
JJ
914 gt_pch_restore_stringpool ();
915
e4641191
IS
916 /* Barring corruption of the PCH file, the restored line table should be
917 complete and usable. */
918 line_table = new_line_table;
4d0c31e6 919}
18c81520 920
4d0c31e6
RH
921/* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
922 Select no address whatsoever, and let gt_pch_save choose what it will with
923 malloc, presumably. */
ee0d75ef 924
4d0c31e6
RH
925void *
926default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED,
927 int fd ATTRIBUTE_UNUSED)
928{
929 return NULL;
930}
ee0d75ef 931
4d0c31e6
RH
932/* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
933 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
934 same as base, indicating that the memory has been allocated but needs to
935 be read in from the file. Return -1 if the address differs, to relocation
936 of the PCH file would be required. */
937
938int
747380f4 939default_gt_pch_use_address (void *&base, size_t size, int fd ATTRIBUTE_UNUSED,
4d0c31e6
RH
940 size_t offset ATTRIBUTE_UNUSED)
941{
942 void *addr = xmalloc (size);
943 return (addr == base) - 1;
944}
ee0d75ef 945
90aa6719
DS
946/* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS. Return the
947 alignment required for allocating virtual memory. Usually this is the
948 same as pagesize. */
949
950size_t
951default_gt_pch_alloc_granularity (void)
952{
c3284718 953 return getpagesize ();
90aa6719
DS
954}
955
4d0c31e6
RH
956#if HAVE_MMAP_FILE
957/* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
958 We temporarily allocate SIZE bytes, and let the kernel place the data
d1a6adeb 959 wherever it will. If it worked, that's our spot, if not we're likely
4d0c31e6 960 to be in trouble. */
8eb6a092 961
4d0c31e6
RH
962void *
963mmap_gt_pch_get_address (size_t size, int fd)
964{
965 void *ret;
18c81520 966
4d0c31e6
RH
967 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
968 if (ret == (void *) MAP_FAILED)
969 ret = NULL;
970 else
bba09b5a 971 munmap ((caddr_t) ret, size);
3277221c 972
4d0c31e6
RH
973 return ret;
974}
3277221c 975
4d0c31e6 976/* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
b8698a0f 977 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
4d0c31e6 978 mapping the data at BASE, -1 if we couldn't.
20c1dc5e 979
4d0c31e6
RH
980 This version assumes that the kernel honors the START operand of mmap
981 even without MAP_FIXED if START through START+SIZE are not currently
982 mapped with something. */
17211ab5 983
4d0c31e6 984int
747380f4 985mmap_gt_pch_use_address (void *&base, size_t size, int fd, size_t offset)
4d0c31e6
RH
986{
987 void *addr;
17211ab5 988
4d0c31e6
RH
989 /* We're called with size == 0 if we're not planning to load a PCH
990 file at all. This allows the hook to free any static space that
991 we might have allocated at link time. */
992 if (size == 0)
993 return -1;
994
bba09b5a 995 addr = mmap ((caddr_t) base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
4d0c31e6
RH
996 fd, offset);
997
998 return addr == base ? 1 : -1;
3277221c 999}
4d0c31e6 1000#endif /* HAVE_MMAP_FILE */
9ac121af 1001
e4dfaf72
LB
1002#if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
1003
d37e6b50 1004/* Modify the bound based on rlimits. */
16226f1e 1005static double
20c1dc5e 1006ggc_rlimit_bound (double limit)
16226f1e
KG
1007{
1008#if defined(HAVE_GETRLIMIT)
1009 struct rlimit rlim;
d37e6b50
GK
1010# if defined (RLIMIT_AS)
1011 /* RLIMIT_AS is what POSIX says is the limit on mmap. Presumably
1012 any OS which has RLIMIT_AS also has a working mmap that GCC will use. */
1013 if (getrlimit (RLIMIT_AS, &rlim) == 0
a2581175 1014 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
16226f1e
KG
1015 && rlim.rlim_cur < limit)
1016 limit = rlim.rlim_cur;
d37e6b50
GK
1017# elif defined (RLIMIT_DATA)
1018 /* ... but some older OSs bound mmap based on RLIMIT_DATA, or we
1019 might be on an OS that has a broken mmap. (Others don't bound
1020 mmap at all, apparently.) */
16226f1e 1021 if (getrlimit (RLIMIT_DATA, &rlim) == 0
a2581175 1022 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
d37e6b50
GK
1023 && rlim.rlim_cur < limit
1024 /* Darwin has this horribly bogus default setting of
1025 RLIMIT_DATA, to 6144Kb. No-one notices because RLIMIT_DATA
1026 appears to be ignored. Ignore such silliness. If a limit
1027 this small was actually effective for mmap, GCC wouldn't even
1028 start up. */
da871904 1029 && rlim.rlim_cur >= 8 * ONE_M)
16226f1e 1030 limit = rlim.rlim_cur;
d37e6b50 1031# endif /* RLIMIT_AS or RLIMIT_DATA */
16226f1e
KG
1032#endif /* HAVE_GETRLIMIT */
1033
1034 return limit;
1035}
1036
9ac121af 1037/* Heuristic to set a default for GGC_MIN_EXPAND. */
e4dfaf72 1038static int
20c1dc5e 1039ggc_min_expand_heuristic (void)
9ac121af 1040{
c3284718 1041 double min_expand = physmem_total ();
16226f1e
KG
1042
1043 /* Adjust for rlimits. */
1044 min_expand = ggc_rlimit_bound (min_expand);
20c1dc5e 1045
9ac121af
KG
1046 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
1047 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
da871904 1048 min_expand /= ONE_G;
9ac121af
KG
1049 min_expand *= 70;
1050 min_expand = MIN (min_expand, 70);
1051 min_expand += 30;
1052
1053 return min_expand;
1054}
1055
1056/* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
e4dfaf72 1057static int
20c1dc5e 1058ggc_min_heapsize_heuristic (void)
9ac121af 1059{
c3284718 1060 double phys_kbytes = physmem_total ();
d37e6b50 1061 double limit_kbytes = ggc_rlimit_bound (phys_kbytes * 2);
16226f1e 1062
da871904
ML
1063 phys_kbytes /= ONE_K; /* Convert to Kbytes. */
1064 limit_kbytes /= ONE_K;
20c1dc5e 1065
9ac121af
KG
1066 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
1067 bound of 128M (when RAM >= 1GB). */
d37e6b50
GK
1068 phys_kbytes /= 8;
1069
1070#if defined(HAVE_GETRLIMIT) && defined (RLIMIT_RSS)
b8698a0f 1071 /* Try not to overrun the RSS limit while doing garbage collection.
d37e6b50
GK
1072 The RSS limit is only advisory, so no margin is subtracted. */
1073 {
1074 struct rlimit rlim;
1075 if (getrlimit (RLIMIT_RSS, &rlim) == 0
1076 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
da871904 1077 phys_kbytes = MIN (phys_kbytes, rlim.rlim_cur / ONE_K);
d37e6b50
GK
1078 }
1079# endif
1080
1081 /* Don't blindly run over our data limit; do GC at least when the
ded5f8f4
NF
1082 *next* GC would be within 20Mb of the limit or within a quarter of
1083 the limit, whichever is larger. If GCC does hit the data limit,
1084 compilation will fail, so this tries to be conservative. */
da871904 1085 limit_kbytes = MAX (0, limit_kbytes - MAX (limit_kbytes / 4, 20 * ONE_K));
a9429e29 1086 limit_kbytes = (limit_kbytes * 100) / (110 + ggc_min_expand_heuristic ());
d37e6b50
GK
1087 phys_kbytes = MIN (phys_kbytes, limit_kbytes);
1088
da871904
ML
1089 phys_kbytes = MAX (phys_kbytes, 4 * ONE_K);
1090 phys_kbytes = MIN (phys_kbytes, 128 * ONE_K);
9ac121af 1091
d37e6b50 1092 return phys_kbytes;
9ac121af 1093}
e4dfaf72 1094#endif
9ac121af
KG
1095
1096void
20c1dc5e 1097init_ggc_heuristics (void)
9ac121af 1098{
d85a0aae 1099#if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
028d4092
ML
1100 param_ggc_min_expand = ggc_min_expand_heuristic ();
1101 param_ggc_min_heapsize = ggc_min_heapsize_heuristic ();
9ac121af
KG
1102#endif
1103}
b9dcdee4 1104
2d44c7de 1105/* GGC memory usage. */
6c1dae73 1106class ggc_usage: public mem_usage
b9dcdee4 1107{
6c1dae73 1108public:
2d44c7de
ML
1109 /* Default constructor. */
1110 ggc_usage (): m_freed (0), m_collected (0), m_overhead (0) {}
1111 /* Constructor. */
1112 ggc_usage (size_t allocated, size_t times, size_t peak,
1113 size_t freed, size_t collected, size_t overhead)
1114 : mem_usage (allocated, times, peak),
1115 m_freed (freed), m_collected (collected), m_overhead (overhead) {}
b9dcdee4 1116
b27b31dc
ML
1117 /* Equality operator. */
1118 inline bool
1119 operator== (const ggc_usage &second) const
1120 {
1121 return (get_balance () == second.get_balance ()
1122 && m_peak == second.m_peak
1123 && m_times == second.m_times);
1124 }
1125
2d44c7de 1126 /* Comparison operator. */
80a4fe78
ML
1127 inline bool
1128 operator< (const ggc_usage &second) const
2d44c7de 1129 {
b27b31dc
ML
1130 if (*this == second)
1131 return false;
1132
2d44c7de
ML
1133 return (get_balance () == second.get_balance () ?
1134 (m_peak == second.m_peak ? m_times < second.m_times
1135 : m_peak < second.m_peak)
1136 : get_balance () < second.get_balance ());
1137 }
b9dcdee4 1138
2d44c7de 1139 /* Register overhead of ALLOCATED and OVERHEAD bytes. */
80a4fe78
ML
1140 inline void
1141 register_overhead (size_t allocated, size_t overhead)
2d44c7de
ML
1142 {
1143 m_allocated += allocated;
1144 m_overhead += overhead;
1145 m_times++;
1146 }
b9dcdee4 1147
2d44c7de 1148 /* Release overhead of SIZE bytes. */
80a4fe78
ML
1149 inline void
1150 release_overhead (size_t size)
2d44c7de
ML
1151 {
1152 m_freed += size;
1153 }
b9dcdee4 1154
2d44c7de 1155 /* Sum the usage with SECOND usage. */
80a4fe78
ML
1156 ggc_usage
1157 operator+ (const ggc_usage &second)
2d44c7de
ML
1158 {
1159 return ggc_usage (m_allocated + second.m_allocated,
1160 m_times + second.m_times,
1161 m_peak + second.m_peak,
1162 m_freed + second.m_freed,
1163 m_collected + second.m_collected,
1164 m_overhead + second.m_overhead);
1165 }
b9dcdee4 1166
2d44c7de 1167 /* Dump usage with PREFIX, where TOTAL is sum of all rows. */
80a4fe78
ML
1168 inline void
1169 dump (const char *prefix, ggc_usage &total) const
2d44c7de 1170 {
40ce7fa6 1171 size_t balance = get_balance ();
2d44c7de 1172 fprintf (stderr,
a0b48080
MM
1173 "%-48s " PRsa (9) ":%5.1f%%" PRsa (9) ":%5.1f%%"
1174 PRsa (9) ":%5.1f%%" PRsa (9) ":%5.1f%%" PRsa (9) "\n",
58cc99af
ML
1175 prefix,
1176 SIZE_AMOUNT (balance), get_percent (balance, total.get_balance ()),
1177 SIZE_AMOUNT (m_collected),
2d44c7de 1178 get_percent (m_collected, total.m_collected),
40ce7fa6 1179 SIZE_AMOUNT (m_freed), get_percent (m_freed, total.m_freed),
40ce7fa6
ML
1180 SIZE_AMOUNT (m_overhead),
1181 get_percent (m_overhead, total.m_overhead),
1182 SIZE_AMOUNT (m_times));
2d44c7de 1183 }
4a8fb1a1 1184
2d44c7de 1185 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
80a4fe78
ML
1186 inline void
1187 dump (mem_location *loc, ggc_usage &total) const
2d44c7de 1188 {
ac059261 1189 char *location_string = loc->to_string ();
07724022 1190
ac059261
ML
1191 dump (location_string, total);
1192
1193 free (location_string);
2d44c7de 1194 }
4a8fb1a1 1195
2d44c7de 1196 /* Dump footer. */
80a4fe78
ML
1197 inline void
1198 dump_footer ()
2d44c7de 1199 {
2d44c7de 1200 dump ("Total", *this);
2d44c7de 1201 }
07724022 1202
2d44c7de 1203 /* Get balance which is GGC allocation leak. */
40ce7fa6 1204 inline size_t
80a4fe78 1205 get_balance () const
2d44c7de
ML
1206 {
1207 return m_allocated + m_overhead - m_collected - m_freed;
1208 }
07724022 1209
2d44c7de 1210 typedef std::pair<mem_location *, ggc_usage *> mem_pair_t;
07724022 1211
2d44c7de 1212 /* Compare wrapper used by qsort method. */
80a4fe78
ML
1213 static int
1214 compare (const void *first, const void *second)
2d44c7de 1215 {
50a2d3be
ML
1216 const mem_pair_t mem1 = *(const mem_pair_t *) first;
1217 const mem_pair_t mem2 = *(const mem_pair_t *) second;
4a8fb1a1 1218
50a2d3be
ML
1219 size_t balance1 = mem1.second->get_balance ();
1220 size_t balance2 = mem2.second->get_balance ();
1221
1222 return balance1 == balance2 ? 0 : (balance1 < balance2 ? 1 : -1);
2d44c7de
ML
1223 }
1224
2d44c7de 1225 /* Dump header with NAME. */
80a4fe78
ML
1226 static inline void
1227 dump_header (const char *name)
2d44c7de 1228 {
58cc99af
ML
1229 fprintf (stderr, "%-48s %11s%17s%17s%16s%17s\n", name, "Leak", "Garbage",
1230 "Freed", "Overhead", "Times");
2d44c7de
ML
1231 }
1232
1233 /* Freed memory in bytes. */
1234 size_t m_freed;
1235 /* Collected memory in bytes. */
1236 size_t m_collected;
1237 /* Overhead memory in bytes. */
1238 size_t m_overhead;
1239};
1240
1241/* GCC memory description. */
1242static mem_alloc_description<ggc_usage> ggc_mem_desc;
1243
1244/* Dump per-site memory statistics. */
b9dcdee4 1245
d1a6adeb 1246void
c5281d50 1247dump_ggc_loc_statistics ()
b9dcdee4 1248{
2d44c7de
ML
1249 if (! GATHER_STATISTICS)
1250 return;
b9dcdee4 1251
602fca42 1252 ggc_collect (GGC_COLLECT_FORCE);
2d44c7de 1253
c5281d50 1254 ggc_mem_desc.dump (GGC_ORIGIN);
07724022
JH
1255}
1256
2d44c7de 1257/* Record ALLOCATED and OVERHEAD bytes to descriptor NAME:LINE (FUNCTION). */
07724022 1258void
2d44c7de 1259ggc_record_overhead (size_t allocated, size_t overhead, void *ptr MEM_STAT_DECL)
07724022 1260{
643e0a30 1261 ggc_usage *usage = ggc_mem_desc.register_descriptor (ptr, GGC_ORIGIN, false
2d44c7de
ML
1262 FINAL_PASS_MEM_STAT);
1263
1264 ggc_mem_desc.register_object_overhead (usage, allocated + overhead, ptr);
1265 usage->register_overhead (allocated, overhead);
07724022
JH
1266}
1267
1268/* Notice that the pointer has been freed. */
83f676b3
RS
1269void
1270ggc_free_overhead (void *ptr)
07724022 1271{
2d44c7de 1272 ggc_mem_desc.release_object_overhead (ptr);
a5573239
JH
1273}
1274
2d44c7de
ML
1275/* After live values has been marked, walk all recorded pointers and see if
1276 they are still live. */
83f676b3 1277void
2d44c7de 1278ggc_prune_overhead_list (void)
b9dcdee4 1279{
2d44c7de 1280 typedef hash_map<const void *, std::pair<ggc_usage *, size_t > > map_t;
b9dcdee4 1281
2d44c7de 1282 map_t::iterator it = ggc_mem_desc.m_reverse_object_map->begin ();
7aa6d18a 1283
2d44c7de
ML
1284 for (; it != ggc_mem_desc.m_reverse_object_map->end (); ++it)
1285 if (!ggc_marked_p ((*it).first))
5d24b4f2
JH
1286 {
1287 (*it).second.first->m_collected += (*it).second.second;
1288 ggc_mem_desc.m_reverse_object_map->remove ((*it).first);
1289 }
b9dcdee4 1290}
c9ef0409 1291
79f4e20d 1292/* Print memory used by heap if this info is available. */
c9ef0409
JH
1293
1294void
1295report_heap_memory_use ()
1296{
432c551b
ML
1297#if defined(HAVE_MALLINFO) || defined(HAVE_MALLINFO2)
1298#ifdef HAVE_MALLINFO2
1299 #define MALLINFO_FN mallinfo2
1300#else
1301 #define MALLINFO_FN mallinfo
1302#endif
c9ef0409 1303 if (!quiet_flag)
79f4e20d
ML
1304 fprintf (stderr, " {heap " PRsa (0) "}",
1305 SIZE_AMOUNT (MALLINFO_FN ().arena));
c9ef0409
JH
1306#endif
1307}
eaa8e854
DM
1308
1309/* Forcibly clear all GTY roots. */
1310
1311void
1312ggc_common_finalize ()
1313{
1314 const struct ggc_root_tab *const *rt;
1315 const_ggc_root_tab_t rti;
1316
1317 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
1318 for (rti = *rt; rti->base != NULL; rti++)
1319 memset (rti->base, 0, rti->stride * rti->nelt);
1320
1321 for (rt = gt_ggc_rtab; *rt; rt++)
7525707c 1322 ggc_zero_rtab_roots (*rt);
eaa8e854
DM
1323
1324 for (rt = gt_pch_scalar_rtab; *rt; rt++)
1325 for (rti = *rt; rti->base != NULL; rti++)
1326 memset (rti->base, 0, rti->stride * rti->nelt);
1327}