]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ggc-common.c
gcc_release (announce_snapshot): Use changedir instead of plain cd.
[thirdparty/gcc.git] / gcc / ggc-common.c
CommitLineData
b49a6a90 1/* Simple garbage collection for the GNU compiler.
283334f0 2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004
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
9Software Foundation; either version 2, or (at your option) any later
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
1322177d 18along with GCC; see the file COPYING. If not, write to the Free
14a774a9
RK
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
b49a6a90
AS
21
22/* Generic garbage collection (GC) functions and data, not specific to
23 any particular GC implementation. */
24
25#include "config.h"
26#include "system.h"
4977bab6 27#include "coretypes.h"
4c160717 28#include "hashtab.h"
1b42a6a9 29#include "ggc.h"
17211ab5 30#include "toplev.h"
9ac121af 31#include "params.h"
18c81520 32#include "hosthooks.h"
4d0c31e6 33#include "hosthooks-def.h"
17211ab5 34
16226f1e
KG
35#ifdef HAVE_SYS_RESOURCE_H
36# include <sys/resource.h>
37#endif
38
17211ab5
GK
39#ifdef HAVE_MMAP_FILE
40# include <sys/mman.h>
8eb6a092
EB
41# ifdef HAVE_MINCORE
42/* This is on Solaris. */
43# include <sys/types.h>
44# endif
45#endif
46
47#ifndef MAP_FAILED
48# define MAP_FAILED ((void *)-1)
17211ab5
GK
49#endif
50
9a0a7d5d 51#ifdef ENABLE_VALGRIND_CHECKING
a207b594
HPN
52# ifdef HAVE_VALGRIND_MEMCHECK_H
53# include <valgrind/memcheck.h>
54# elif defined HAVE_MEMCHECK_H
55# include <memcheck.h>
14011ca4 56# else
a207b594 57# include <valgrind.h>
14011ca4 58# endif
9a0a7d5d
HPN
59#else
60/* Avoid #ifdef:s when we can help it. */
61#define VALGRIND_DISCARD(x)
62#endif
b49a6a90 63
3277221c
MM
64/* Statistics about the allocation. */
65static ggc_statistics *ggc_stats;
66
17211ab5
GK
67struct traversal_state;
68
20c1dc5e
AJ
69static int ggc_htab_delete (void **, void *);
70static hashval_t saving_htab_hash (const void *);
71static int saving_htab_eq (const void *, const void *);
72static int call_count (void **, void *);
73static int call_alloc (void **, void *);
74static int compare_ptr_data (const void *, const void *);
75static void relocate_ptrs (void *, void *);
76static void write_pch_globals (const struct ggc_root_tab * const *tab,
77 struct traversal_state *state);
78static double ggc_rlimit_bound (double);
b49a6a90
AS
79
80/* Maintain global roots that are preserved during GC. */
81
4c160717
RK
82/* Process a slot of an htab by deleting it if it has not been marked. */
83
84static int
20c1dc5e 85ggc_htab_delete (void **slot, void *info)
4c160717 86{
e2500fed 87 const struct ggc_cache_tab *r = (const struct ggc_cache_tab *) info;
4c160717
RK
88
89 if (! (*r->marked_p) (*slot))
e2500fed
GK
90 htab_clear_slot (*r->base, slot);
91 else
92 (*r->cb) (*slot);
4c160717
RK
93
94 return 1;
95}
96
cb2ec151
RH
97/* Iterate through all registered roots and mark each element. */
98
b49a6a90 99void
20c1dc5e 100ggc_mark_roots (void)
96df4529 101{
e2500fed
GK
102 const struct ggc_root_tab *const *rt;
103 const struct ggc_root_tab *rti;
104 const struct ggc_cache_tab *const *ct;
105 const struct ggc_cache_tab *cti;
106 size_t i;
589005ff 107
e2500fed
GK
108 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
109 for (rti = *rt; rti->base != NULL; rti++)
110 memset (rti->base, 0, rti->stride);
111
112 for (rt = gt_ggc_rtab; *rt; rt++)
113 for (rti = *rt; rti->base != NULL; rti++)
114 for (i = 0; i < rti->nelt; i++)
115 (*rti->cb)(*(void **)((char *)rti->base + rti->stride * i));
bedda2da 116
17211ab5 117 ggc_mark_stringpool ();
bedda2da 118
4c160717 119 /* Now scan all hash tables that have objects which are to be deleted if
e2500fed
GK
120 they are not already marked. */
121 for (ct = gt_ggc_cache_rtab; *ct; ct++)
122 for (cti = *ct; cti->base != NULL; cti++)
690eed2c 123 if (*cti->base)
17211ab5
GK
124 {
125 ggc_set_mark (*cti->base);
20c1dc5e 126 htab_traverse_noresize (*cti->base, ggc_htab_delete, (void *) cti);
17211ab5
GK
127 ggc_set_mark ((*cti->base)->entries);
128 }
96df4529
AS
129}
130
e2500fed
GK
131/* Allocate a block of memory, then clear it. */
132void *
b9dcdee4 133ggc_alloc_cleared_stat (size_t size MEM_STAT_DECL)
ef8288f7 134{
b9dcdee4 135 void *buf = ggc_alloc_stat (size PASS_MEM_STAT);
e2500fed
GK
136 memset (buf, 0, size);
137 return buf;
ef8288f7
RH
138}
139
e2500fed
GK
140/* Resize a block of memory, possibly re-allocating it. */
141void *
b9dcdee4 142ggc_realloc_stat (void *x, size_t size MEM_STAT_DECL)
ef8288f7 143{
e2500fed
GK
144 void *r;
145 size_t old_size;
ef8288f7 146
e2500fed 147 if (x == NULL)
b9dcdee4 148 return ggc_alloc_stat (size PASS_MEM_STAT);
ef8288f7 149
e2500fed 150 old_size = ggc_get_size (x);
685fe032 151
e2500fed 152 if (size <= old_size)
9a0a7d5d
HPN
153 {
154 /* Mark the unwanted memory as unaccessible. We also need to make
155 the "new" size accessible, since ggc_get_size returns the size of
156 the pool, not the size of the individually allocated object, the
157 size which was previously made accessible. Unfortunately, we
158 don't know that previously allocated size. Without that
159 knowledge we have to lose some initialization-tracking for the
160 old parts of the object. An alternative is to mark the whole
20c1dc5e 161 old_size as reachable, but that would lose tracking of writes
9a0a7d5d
HPN
162 after the end of the object (by small offsets). Discard the
163 handle to avoid handle leak. */
164 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) x + size,
165 old_size - size));
166 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x, size));
167 return x;
168 }
ef8288f7 169
b9dcdee4 170 r = ggc_alloc_stat (size PASS_MEM_STAT);
9a0a7d5d
HPN
171
172 /* Since ggc_get_size returns the size of the pool, not the size of the
173 individually allocated object, we'd access parts of the old object
174 that were marked invalid with the memcpy below. We lose a bit of the
175 initialization-tracking since some of it may be uninitialized. */
176 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (x, old_size));
177
e2500fed 178 memcpy (r, x, old_size);
9a0a7d5d
HPN
179
180 /* The old object is not supposed to be used anymore. */
685fe032 181 ggc_free (x);
9a0a7d5d 182
e2500fed 183 return r;
ef8288f7
RH
184}
185
e2500fed 186/* Like ggc_alloc_cleared, but performs a multiplication. */
f8a83ee3 187void *
20c1dc5e 188ggc_calloc (size_t s1, size_t s2)
f8a83ee3 189{
e2500fed 190 return ggc_alloc_cleared (s1 * s2);
f8a83ee3
ZW
191}
192
17211ab5 193/* These are for splay_tree_new_ggc. */
20c1dc5e
AJ
194void *
195ggc_splay_alloc (int sz, void *nl)
17211ab5
GK
196{
197 if (nl != NULL)
198 abort ();
199 return ggc_alloc (sz);
200}
201
202void
20c1dc5e 203ggc_splay_dont_free (void * x ATTRIBUTE_UNUSED, void *nl)
17211ab5
GK
204{
205 if (nl != NULL)
206 abort ();
207}
208
3277221c 209/* Print statistics that are independent of the collector in use. */
fba0bfd4
ZW
210#define SCALE(x) ((unsigned long) ((x) < 1024*10 \
211 ? (x) \
212 : ((x) < 1024*1024*10 \
213 ? (x) / 1024 \
214 : (x) / (1024*1024))))
215#define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
3277221c
MM
216
217void
20c1dc5e
AJ
218ggc_print_common_statistics (FILE *stream ATTRIBUTE_UNUSED,
219 ggc_statistics *stats)
3277221c 220{
3277221c
MM
221 /* Set the pointer so that during collection we will actually gather
222 the statistics. */
223 ggc_stats = stats;
224
225 /* Then do one collection to fill in the statistics. */
226 ggc_collect ();
227
17211ab5
GK
228 /* At present, we don't really gather any interesting statistics. */
229
230 /* Don't gather statistics any more. */
231 ggc_stats = NULL;
232}
233\f
234/* Functions for saving and restoring GCable memory to disk. */
235
236static htab_t saving_htab;
237
20c1dc5e 238struct ptr_data
17211ab5
GK
239{
240 void *obj;
241 void *note_ptr_cookie;
242 gt_note_pointers note_ptr_fn;
243 gt_handle_reorder reorder_fn;
244 size_t size;
245 void *new_addr;
246};
247
248#define POINTER_HASH(x) (hashval_t)((long)x >> 3)
249
250/* Register an object in the hash table. */
251
252int
20c1dc5e
AJ
253gt_pch_note_object (void *obj, void *note_ptr_cookie,
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 **)
262 htab_find_slot_with_hash (saving_htab, obj, POINTER_HASH (obj),
263 INSERT);
264 if (*slot != NULL)
265 {
266 if ((*slot)->note_ptr_fn != note_ptr_fn
267 || (*slot)->note_ptr_cookie != note_ptr_cookie)
268 abort ();
269 return 0;
270 }
20c1dc5e 271
17211ab5
GK
272 *slot = xcalloc (sizeof (struct ptr_data), 1);
273 (*slot)->obj = obj;
274 (*slot)->note_ptr_fn = note_ptr_fn;
275 (*slot)->note_ptr_cookie = note_ptr_cookie;
276 if (note_ptr_fn == gt_pch_p_S)
277 (*slot)->size = strlen (obj) + 1;
278 else
279 (*slot)->size = ggc_get_size (obj);
280 return 1;
281}
282
283/* Register an object in the hash table. */
284
285void
20c1dc5e
AJ
286gt_pch_note_reorder (void *obj, void *note_ptr_cookie,
287 gt_handle_reorder reorder_fn)
17211ab5
GK
288{
289 struct ptr_data *data;
20c1dc5e 290
17211ab5
GK
291 if (obj == NULL || obj == (void *) 1)
292 return;
293
294 data = htab_find_with_hash (saving_htab, obj, POINTER_HASH (obj));
295 if (data == NULL
296 || data->note_ptr_cookie != note_ptr_cookie)
297 abort ();
20c1dc5e 298
17211ab5
GK
299 data->reorder_fn = reorder_fn;
300}
301
302/* Hash and equality functions for saving_htab, callbacks for htab_create. */
303
304static hashval_t
20c1dc5e 305saving_htab_hash (const void *p)
17211ab5
GK
306{
307 return POINTER_HASH (((struct ptr_data *)p)->obj);
308}
309
310static int
20c1dc5e 311saving_htab_eq (const void *p1, const void *p2)
17211ab5
GK
312{
313 return ((struct ptr_data *)p1)->obj == p2;
314}
315
316/* Handy state for the traversal functions. */
317
20c1dc5e 318struct traversal_state
17211ab5
GK
319{
320 FILE *f;
321 struct ggc_pch_data *d;
322 size_t count;
323 struct ptr_data **ptrs;
324 size_t ptrs_i;
325};
326
327/* Callbacks for htab_traverse. */
328
329static int
20c1dc5e 330call_count (void **slot, void *state_p)
17211ab5
GK
331{
332 struct ptr_data *d = (struct ptr_data *)*slot;
333 struct traversal_state *state = (struct traversal_state *)state_p;
20c1dc5e 334
b6f61163 335 ggc_pch_count_object (state->d, d->obj, d->size, d->note_ptr_fn == gt_pch_p_S);
17211ab5
GK
336 state->count++;
337 return 1;
338}
339
340static int
20c1dc5e 341call_alloc (void **slot, void *state_p)
17211ab5
GK
342{
343 struct ptr_data *d = (struct ptr_data *)*slot;
344 struct traversal_state *state = (struct traversal_state *)state_p;
20c1dc5e 345
b6f61163 346 d->new_addr = ggc_pch_alloc_object (state->d, d->obj, d->size, d->note_ptr_fn == gt_pch_p_S);
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
GK
355{
356 struct ptr_data *p1 = *(struct ptr_data *const *)p1_p;
357 struct ptr_data *p2 = *(struct ptr_data *const *)p2_p;
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
17211ab5
GK
375 result = htab_find_with_hash (saving_htab, *ptr, POINTER_HASH (*ptr));
376 if (result == NULL)
377 abort ();
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 {
20c1dc5e 404 new_ptr = htab_find_with_hash (saving_htab, ptr,
17211ab5 405 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;
434 size_t page_size = getpagesize();
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
467 state.ptrs = xmalloc (state.count * sizeof (*state.ptrs));
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
482 ggc_pch_prepare_write (state.d, state.f);
20c1dc5e 483
e0bb17a8 484 /* Pad the PCH file so that the mmapped area starts on a page boundary. */
17211ab5 485 {
70f8b89f
KG
486 long o;
487 o = ftell (state.f) + sizeof (mmi);
488 if (o == -1)
fa6ef813 489 fatal_error ("can't get position in PCH file: %m");
17211ab5
GK
490 mmi.offset = page_size - o % page_size;
491 if (mmi.offset == page_size)
492 mmi.offset = 0;
493 mmi.offset += o;
494 }
495 if (fwrite (&mmi, sizeof (mmi), 1, state.f) != 1)
fa6ef813 496 fatal_error ("can't write PCH file: %m");
17211ab5
GK
497 if (mmi.offset != 0
498 && fseek (state.f, mmi.offset, SEEK_SET) != 0)
fa6ef813 499 fatal_error ("can't write padding to PCH file: %m");
17211ab5
GK
500
501 /* Actually write out the objects. */
502 for (i = 0; i < state.count; i++)
3277221c 503 {
17211ab5
GK
504 if (this_object_size < state.ptrs[i]->size)
505 {
506 this_object_size = state.ptrs[i]->size;
507 this_object = xrealloc (this_object, this_object_size);
508 }
509 memcpy (this_object, state.ptrs[i]->obj, state.ptrs[i]->size);
510 if (state.ptrs[i]->reorder_fn != NULL)
20c1dc5e 511 state.ptrs[i]->reorder_fn (state.ptrs[i]->obj,
17211ab5
GK
512 state.ptrs[i]->note_ptr_cookie,
513 relocate_ptrs, &state);
20c1dc5e 514 state.ptrs[i]->note_ptr_fn (state.ptrs[i]->obj,
17211ab5
GK
515 state.ptrs[i]->note_ptr_cookie,
516 relocate_ptrs, &state);
517 ggc_pch_write_object (state.d, state.f, state.ptrs[i]->obj,
4d0c31e6
RH
518 state.ptrs[i]->new_addr, state.ptrs[i]->size,
519 state.ptrs[i]->note_ptr_fn == gt_pch_p_S);
17211ab5
GK
520 if (state.ptrs[i]->note_ptr_fn != gt_pch_p_S)
521 memcpy (state.ptrs[i]->obj, this_object, state.ptrs[i]->size);
3277221c 522 }
17211ab5 523 ggc_pch_finish (state.d, state.f);
d24ecd21 524 gt_pch_fixup_stringpool ();
17211ab5
GK
525
526 free (state.ptrs);
527 htab_delete (saving_htab);
528}
529
530/* Read the state of the compiler back in from F. */
531
532void
20c1dc5e 533gt_pch_restore (FILE *f)
17211ab5
GK
534{
535 const struct ggc_root_tab *const *rt;
536 const struct ggc_root_tab *rti;
537 size_t i;
538 struct mmap_info mmi;
4d0c31e6 539 int result;
17211ab5
GK
540
541 /* Delete any deletable objects. This makes ggc_pch_read much
542 faster, as it can be sure that no GCable objects remain other
543 than the ones just read in. */
544 for (rt = gt_ggc_deletable_rtab; *rt; rt++)
545 for (rti = *rt; rti->base != NULL; rti++)
546 memset (rti->base, 0, rti->stride);
547
548 /* Read in all the scalar variables. */
549 for (rt = gt_pch_scalar_rtab; *rt; rt++)
550 for (rti = *rt; rti->base != NULL; rti++)
551 if (fread (rti->base, rti->stride, 1, f) != 1)
fa6ef813 552 fatal_error ("can't read PCH file: %m");
17211ab5
GK
553
554 /* Read in all the global pointers, in 6 easy loops. */
555 for (rt = gt_ggc_rtab; *rt; rt++)
556 for (rti = *rt; rti->base != NULL; rti++)
557 for (i = 0; i < rti->nelt; i++)
558 if (fread ((char *)rti->base + rti->stride * i,
559 sizeof (void *), 1, f) != 1)
fa6ef813 560 fatal_error ("can't read PCH file: %m");
17211ab5
GK
561
562 for (rt = gt_pch_cache_rtab; *rt; rt++)
563 for (rti = *rt; rti->base != NULL; rti++)
564 for (i = 0; i < rti->nelt; i++)
565 if (fread ((char *)rti->base + rti->stride * i,
566 sizeof (void *), 1, f) != 1)
fa6ef813 567 fatal_error ("can't read PCH file: %m");
17211ab5
GK
568
569 if (fread (&mmi, sizeof (mmi), 1, f) != 1)
fa6ef813 570 fatal_error ("can't read PCH file: %m");
20c1dc5e 571
4d0c31e6
RH
572 result = host_hooks.gt_pch_use_address (mmi.preferred_base, mmi.size,
573 fileno (f), mmi.offset);
574 if (result < 0)
575 fatal_error ("had to relocate PCH");
576 if (result == 0)
18c81520 577 {
4d0c31e6
RH
578 if (fseek (f, mmi.offset, SEEK_SET) != 0
579 || fread (mmi.preferred_base, mmi.size, 1, f) != 1)
580 fatal_error ("can't read PCH file: %m");
581 }
582 else if (fseek (f, mmi.offset + mmi.size, SEEK_SET) != 0)
583 fatal_error ("can't read PCH file: %m");
8eb6a092 584
4d0c31e6 585 ggc_pch_read (f, mmi.preferred_base);
18c81520 586
4d0c31e6
RH
587 gt_pch_restore_stringpool ();
588}
18c81520 589
4d0c31e6
RH
590/* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is not present.
591 Select no address whatsoever, and let gt_pch_save choose what it will with
592 malloc, presumably. */
ee0d75ef 593
4d0c31e6
RH
594void *
595default_gt_pch_get_address (size_t size ATTRIBUTE_UNUSED,
596 int fd ATTRIBUTE_UNUSED)
597{
598 return NULL;
599}
ee0d75ef 600
4d0c31e6
RH
601/* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is not present.
602 Allocate SIZE bytes with malloc. Return 0 if the address we got is the
603 same as base, indicating that the memory has been allocated but needs to
604 be read in from the file. Return -1 if the address differs, to relocation
605 of the PCH file would be required. */
606
607int
608default_gt_pch_use_address (void *base, size_t size, int fd ATTRIBUTE_UNUSED,
609 size_t offset ATTRIBUTE_UNUSED)
610{
611 void *addr = xmalloc (size);
612 return (addr == base) - 1;
613}
ee0d75ef 614
4d0c31e6
RH
615#if HAVE_MMAP_FILE
616/* Default version of HOST_HOOKS_GT_PCH_GET_ADDRESS when mmap is present.
617 We temporarily allocate SIZE bytes, and let the kernel place the data
618 whereever it will. If it worked, that's our spot, if not we're likely
619 to be in trouble. */
8eb6a092 620
4d0c31e6
RH
621void *
622mmap_gt_pch_get_address (size_t size, int fd)
623{
624 void *ret;
18c81520 625
4d0c31e6
RH
626 ret = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
627 if (ret == (void *) MAP_FAILED)
628 ret = NULL;
629 else
630 munmap (ret, size);
3277221c 631
4d0c31e6
RH
632 return ret;
633}
3277221c 634
4d0c31e6
RH
635/* Default version of HOST_HOOKS_GT_PCH_USE_ADDRESS when mmap is present.
636 Map SIZE bytes of FD+OFFSET at BASE. Return 1 if we succeeded at
637 mapping the data at BASE, -1 if we couldn't.
20c1dc5e 638
4d0c31e6
RH
639 This version assumes that the kernel honors the START operand of mmap
640 even without MAP_FIXED if START through START+SIZE are not currently
641 mapped with something. */
17211ab5 642
4d0c31e6
RH
643int
644mmap_gt_pch_use_address (void *base, size_t size, int fd, size_t offset)
645{
646 void *addr;
17211ab5 647
4d0c31e6
RH
648 /* We're called with size == 0 if we're not planning to load a PCH
649 file at all. This allows the hook to free any static space that
650 we might have allocated at link time. */
651 if (size == 0)
652 return -1;
653
654 addr = mmap (base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
655 fd, offset);
656
657 return addr == base ? 1 : -1;
3277221c 658}
4d0c31e6 659#endif /* HAVE_MMAP_FILE */
9ac121af 660
16226f1e
KG
661/* Modify the bound based on rlimits. Keep the smallest number found. */
662static double
20c1dc5e 663ggc_rlimit_bound (double limit)
16226f1e
KG
664{
665#if defined(HAVE_GETRLIMIT)
666 struct rlimit rlim;
667# ifdef RLIMIT_RSS
668 if (getrlimit (RLIMIT_RSS, &rlim) == 0
a2581175 669 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
16226f1e
KG
670 && rlim.rlim_cur < limit)
671 limit = rlim.rlim_cur;
672# endif
673# ifdef RLIMIT_DATA
674 if (getrlimit (RLIMIT_DATA, &rlim) == 0
a2581175 675 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
16226f1e
KG
676 && rlim.rlim_cur < limit)
677 limit = rlim.rlim_cur;
678# endif
679# ifdef RLIMIT_AS
680 if (getrlimit (RLIMIT_AS, &rlim) == 0
a2581175 681 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY
16226f1e
KG
682 && rlim.rlim_cur < limit)
683 limit = rlim.rlim_cur;
684# endif
685#endif /* HAVE_GETRLIMIT */
686
687 return limit;
688}
689
9ac121af
KG
690/* Heuristic to set a default for GGC_MIN_EXPAND. */
691int
20c1dc5e 692ggc_min_expand_heuristic (void)
9ac121af
KG
693{
694 double min_expand = physmem_total();
16226f1e
KG
695
696 /* Adjust for rlimits. */
697 min_expand = ggc_rlimit_bound (min_expand);
20c1dc5e 698
9ac121af
KG
699 /* The heuristic is a percentage equal to 30% + 70%*(RAM/1GB), yielding
700 a lower bound of 30% and an upper bound of 100% (when RAM >= 1GB). */
701 min_expand /= 1024*1024*1024;
702 min_expand *= 70;
703 min_expand = MIN (min_expand, 70);
704 min_expand += 30;
705
706 return min_expand;
707}
708
709/* Heuristic to set a default for GGC_MIN_HEAPSIZE. */
710int
20c1dc5e 711ggc_min_heapsize_heuristic (void)
9ac121af 712{
16226f1e
KG
713 double min_heap_kbytes = physmem_total();
714
715 /* Adjust for rlimits. */
716 min_heap_kbytes = ggc_rlimit_bound (min_heap_kbytes);
717
beb235f8 718 min_heap_kbytes /= 1024; /* Convert to Kbytes. */
20c1dc5e 719
9ac121af
KG
720 /* The heuristic is RAM/8, with a lower bound of 4M and an upper
721 bound of 128M (when RAM >= 1GB). */
722 min_heap_kbytes /= 8;
723 min_heap_kbytes = MAX (min_heap_kbytes, 4 * 1024);
724 min_heap_kbytes = MIN (min_heap_kbytes, 128 * 1024);
725
726 return min_heap_kbytes;
727}
728
729void
20c1dc5e 730init_ggc_heuristics (void)
9ac121af 731{
d85a0aae 732#if !defined ENABLE_GC_CHECKING && !defined ENABLE_GC_ALWAYS_COLLECT
9ac121af
KG
733 set_param_value ("ggc-min-expand", ggc_min_expand_heuristic());
734 set_param_value ("ggc-min-heapsize", ggc_min_heapsize_heuristic());
735#endif
736}
b9dcdee4
JH
737
738#ifdef GATHER_STATISTICS
739
740/* Datastructure used to store per-call-site statistics. */
741struct loc_descriptor
742{
743 const char *file;
744 int line;
745 const char *function;
746 int times;
747 size_t allocated;
748 size_t overhead;
749};
750
751/* Hashtable used for statistics. */
752static htab_t loc_hash;
753
754/* Hash table helpers functions. */
755static hashval_t
756hash_descriptor (const void *p)
757{
758 const struct loc_descriptor *d = p;
759
760 return htab_hash_pointer (d->function) | d->line;
761}
762
763static int
764eq_descriptor (const void *p1, const void *p2)
765{
766 const struct loc_descriptor *d = p1;
767 const struct loc_descriptor *d2 = p2;
768
769 return (d->file == d2->file && d->line == d2->line
770 && d->function == d2->function);
771}
772
773/* Return descriptor for given call site, create new one if needed. */
774static struct loc_descriptor *
775loc_descriptor (const char *name, int line, const char *function)
776{
777 struct loc_descriptor loc;
778 struct loc_descriptor **slot;
779
780 loc.file = name;
781 loc.line = line;
782 loc.function = function;
783 if (!loc_hash)
784 loc_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
785
786 slot = (struct loc_descriptor **) htab_find_slot (loc_hash, &loc, 1);
787 if (*slot)
788 return *slot;
789 *slot = xcalloc (sizeof (**slot), 1);
790 (*slot)->file = name;
791 (*slot)->line = line;
792 (*slot)->function = function;
793 return *slot;
794}
795
796/* Record ALLOCATED and OVERHEAD bytes to descritor NAME:LINE (FUNCTION). */
797void ggc_record_overhead (size_t allocated, size_t overhead,
798 const char *name, int line, const char *function)
799{
800 struct loc_descriptor *loc = loc_descriptor (name, line, function);
801
802 loc->times++;
803 loc->allocated+=allocated;
804 loc->overhead+=overhead;
805}
806
807/* Helper for qsort; sort descriptors by amount of memory consumed. */
808static int
809cmp_statistic (const void *loc1, const void *loc2)
810{
811 struct loc_descriptor *l1 = *(struct loc_descriptor **) loc1;
812 struct loc_descriptor *l2 = *(struct loc_descriptor **) loc2;
813 return (l1->allocated + l1->overhead) - (l2->allocated + l2->overhead);
814}
815
816/* Collect array of the descriptors from hashtable. */
817struct loc_descriptor **loc_array;
818static int
819add_statistics (void **slot, void *b)
820{
821 int *n = (int *)b;
822 loc_array[*n] = (struct loc_descriptor *) *slot;
823 (*n)++;
824 return 1;
825}
826
827/* Dump per-site memory statistics. */
828#endif
829void dump_ggc_loc_statistics (void)
830{
831#ifdef GATHER_STATISTICS
832 int nentries = 0;
833 char s[4096];
834 size_t count, size, overhead;
835 int i;
836
837 loc_array = xcalloc (sizeof (*loc_array), loc_hash->n_elements);
838 fprintf (stderr, "-------------------------------------------------------\n");
839 fprintf (stderr, "\n%-60s %10s %10s %10s\n",
840 "source location", "Times", "Allocated", "Overhead");
841 fprintf (stderr, "-------------------------------------------------------\n");
842 count = 0;
843 size = 0;
844 overhead = 0;
845 htab_traverse (loc_hash, add_statistics, &nentries);
846 qsort (loc_array, nentries, sizeof (*loc_array), cmp_statistic);
847 for (i = 0; i < nentries; i++)
848 {
849 struct loc_descriptor *d = loc_array[i];
850 size += d->allocated;
851 count += d->times;
852 overhead += d->overhead;
853 }
854 for (i = 0; i < nentries; i++)
855 {
856 struct loc_descriptor *d = loc_array[i];
857 if (d->allocated)
858 {
859 const char *s1 = d->file;
860 const char *s2;
861 while ((s2 = strstr (s1, "gcc/")))
862 s1 = s2 + 4;
863 sprintf (s, "%s:%i (%s)", s1, d->line, d->function);
864 fprintf (stderr, "%-60s %10i %10li %10li:%.3f%%\n", s,
865 d->times, (long)d->allocated, (long)d->overhead,
866 (d->allocated + d->overhead) *100.0 / (size + overhead));
867 }
868 }
869 fprintf (stderr, "%-60s %10ld %10ld %10ld\n",
870 "Total", (long)count, (long)size, (long)overhead);
871 fprintf (stderr, "-------------------------------------------------------\n");
872#endif
873}