OSDN Git Service

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