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