OSDN Git Service

Added missing semicolon at end of union.
[pf3gnuchains/gcc-fork.git] / gcc / ggc-page.c
1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2    Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING.  If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "toplev.h"
29 #include "flags.h"
30 #include "ggc.h"
31 #include "timevar.h"
32 #include "params.h"
33 #ifdef ENABLE_VALGRIND_CHECKING
34 # ifdef HAVE_MEMCHECK_H
35 # include <memcheck.h>
36 # else
37 # include <valgrind.h>
38 # endif
39 #else
40 /* Avoid #ifdef:s when we can help it.  */
41 #define VALGRIND_DISCARD(x)
42 #endif
43
44 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
45    file open.  Prefer either to valloc.  */
46 #ifdef HAVE_MMAP_ANON
47 # undef HAVE_MMAP_DEV_ZERO
48
49 # include <sys/mman.h>
50 # ifndef MAP_FAILED
51 #  define MAP_FAILED -1
52 # endif
53 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
54 #  define MAP_ANONYMOUS MAP_ANON
55 # endif
56 # define USING_MMAP
57
58 #endif
59
60 #ifdef HAVE_MMAP_DEV_ZERO
61
62 # include <sys/mman.h>
63 # ifndef MAP_FAILED
64 #  define MAP_FAILED -1
65 # endif
66 # define USING_MMAP
67
68 #endif
69
70 #ifndef USING_MMAP
71 #define USING_MALLOC_PAGE_GROUPS
72 #endif
73
74 /* Stategy:
75
76    This garbage-collecting allocator allocates objects on one of a set
77    of pages.  Each page can allocate objects of a single size only;
78    available sizes are powers of two starting at four bytes.  The size
79    of an allocation request is rounded up to the next power of two
80    (`order'), and satisfied from the appropriate page.
81
82    Each page is recorded in a page-entry, which also maintains an
83    in-use bitmap of object positions on the page.  This allows the
84    allocation state of a particular object to be flipped without
85    touching the page itself.
86
87    Each page-entry also has a context depth, which is used to track
88    pushing and popping of allocation contexts.  Only objects allocated
89    in the current (highest-numbered) context may be collected.
90
91    Page entries are arranged in an array of singly-linked lists.  The
92    array is indexed by the allocation size, in bits, of the pages on
93    it; i.e. all pages on a list allocate objects of the same size.
94    Pages are ordered on the list such that all non-full pages precede
95    all full pages, with non-full pages arranged in order of decreasing
96    context depth.
97
98    Empty pages (of all orders) are kept on a single page cache list,
99    and are considered first when new pages are required; they are
100    deallocated at the start of the next collection if they haven't
101    been recycled by then.  */
102
103 /* Define GGC_DEBUG_LEVEL to print debugging information.
104      0: No debugging output.
105      1: GC statistics only.
106      2: Page-entry allocations/deallocations as well.
107      3: Object allocations as well.
108      4: Object marks as well.  */
109 #define GGC_DEBUG_LEVEL (0)
110 \f
111 #ifndef HOST_BITS_PER_PTR
112 #define HOST_BITS_PER_PTR  HOST_BITS_PER_LONG
113 #endif
114
115 \f
116 /* A two-level tree is used to look up the page-entry for a given
117    pointer.  Two chunks of the pointer's bits are extracted to index
118    the first and second levels of the tree, as follows:
119
120                                    HOST_PAGE_SIZE_BITS
121                            32           |      |
122        msb +----------------+----+------+------+ lsb
123                             |    |      |
124                          PAGE_L1_BITS   |
125                                  |      |
126                                PAGE_L2_BITS
127
128    The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
129    pages are aligned on system page boundaries.  The next most
130    significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
131    index values in the lookup table, respectively.
132
133    For 32-bit architectures and the settings below, there are no
134    leftover bits.  For architectures with wider pointers, the lookup
135    tree points to a list of pages, which must be scanned to find the
136    correct one.  */
137
138 #define PAGE_L1_BITS    (8)
139 #define PAGE_L2_BITS    (32 - PAGE_L1_BITS - G.lg_pagesize)
140 #define PAGE_L1_SIZE    ((size_t) 1 << PAGE_L1_BITS)
141 #define PAGE_L2_SIZE    ((size_t) 1 << PAGE_L2_BITS)
142
143 #define LOOKUP_L1(p) \
144   (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
145
146 #define LOOKUP_L2(p) \
147   (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
148
149 /* The number of objects per allocation page, for objects on a page of
150    the indicated ORDER.  */
151 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
152
153 /* The number of objects in P.  */
154 #define OBJECTS_IN_PAGE(P) ((P)->bytes / OBJECT_SIZE ((P)->order))
155
156 /* The size of an object on a page of the indicated ORDER.  */
157 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
158
159 /* For speed, we avoid doing a general integer divide to locate the
160    offset in the allocation bitmap, by precalculating numbers M, S
161    such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
162    within the page which is evenly divisible by the object size Z.  */
163 #define DIV_MULT(ORDER) inverse_table[ORDER].mult
164 #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
165 #define OFFSET_TO_BIT(OFFSET, ORDER) \
166   (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
167
168 /* The number of extra orders, not corresponding to power-of-two sized
169    objects.  */
170
171 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
172
173 #define RTL_SIZE(NSLOTS) \
174   (sizeof (struct rtx_def) + ((NSLOTS) - 1) * sizeof (rtunion))
175
176 #define TREE_EXP_SIZE(OPS) \
177   (sizeof (struct tree_exp) + ((OPS) - 1) * sizeof (tree))
178
179 /* The Ith entry is the maximum size of an object to be stored in the
180    Ith extra order.  Adding a new entry to this array is the *only*
181    thing you need to do to add a new special allocation size.  */
182
183 static const size_t extra_order_size_table[] = {
184   sizeof (struct tree_decl),
185   sizeof (struct tree_list),
186   TREE_EXP_SIZE (2),
187   RTL_SIZE (2),                 /* REG, MEM, PLUS, etc.  */
188   RTL_SIZE (10),                /* INSN, CALL_INSN, JUMP_INSN */
189 };
190
191 /* The total number of orders.  */
192
193 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
194
195 /* We use this structure to determine the alignment required for
196    allocations.  For power-of-two sized allocations, that's not a
197    problem, but it does matter for odd-sized allocations.  */
198
199 struct max_alignment {
200   char c;
201   union {
202     HOST_WIDEST_INT i;
203     long double d;
204   } u;
205 };
206
207 /* The biggest alignment required.  */
208
209 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
210
211 /* Compute the smallest nonnegative number which when added to X gives
212    a multiple of F.  */
213
214 #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
215
216 /* Compute the smallest multiple of F that is >= X.  */
217
218 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
219
220 /* The Ith entry is the number of objects on a page or order I.  */
221
222 static unsigned objects_per_page_table[NUM_ORDERS];
223
224 /* The Ith entry is the size of an object on a page of order I.  */
225
226 static size_t object_size_table[NUM_ORDERS];
227
228 /* The Ith entry is a pair of numbers (mult, shift) such that
229    ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
230    for all k evenly divisible by OBJECT_SIZE(I).  */
231
232 static struct
233 {
234   unsigned int mult;
235   unsigned int shift;
236 }
237 inverse_table[NUM_ORDERS];
238
239 /* A page_entry records the status of an allocation page.  This
240    structure is dynamically sized to fit the bitmap in_use_p.  */
241 typedef struct page_entry
242 {
243   /* The next page-entry with objects of the same size, or NULL if
244      this is the last page-entry.  */
245   struct page_entry *next;
246
247   /* The number of bytes allocated.  (This will always be a multiple
248      of the host system page size.)  */
249   size_t bytes;
250
251   /* The address at which the memory is allocated.  */
252   char *page;
253
254 #ifdef USING_MALLOC_PAGE_GROUPS
255   /* Back pointer to the page group this page came from.  */
256   struct page_group *group;
257 #endif
258
259   /* This is the index in the by_depth varray where this page table
260      can be found.  */
261   unsigned long index_by_depth;
262
263   /* Context depth of this page.  */
264   unsigned short context_depth;
265
266   /* The number of free objects remaining on this page.  */
267   unsigned short num_free_objects;
268
269   /* A likely candidate for the bit position of a free object for the
270      next allocation from this page.  */
271   unsigned short next_bit_hint;
272
273   /* The lg of size of objects allocated from this page.  */
274   unsigned char order;
275
276   /* A bit vector indicating whether or not objects are in use.  The
277      Nth bit is one if the Nth object on this page is allocated.  This
278      array is dynamically sized.  */
279   unsigned long in_use_p[1];
280 } page_entry;
281
282 #ifdef USING_MALLOC_PAGE_GROUPS
283 /* A page_group describes a large allocation from malloc, from which
284    we parcel out aligned pages.  */
285 typedef struct page_group
286 {
287   /* A linked list of all extant page groups.  */
288   struct page_group *next;
289
290   /* The address we received from malloc.  */
291   char *allocation;
292
293   /* The size of the block.  */
294   size_t alloc_size;
295
296   /* A bitmask of pages in use.  */
297   unsigned int in_use;
298 } page_group;
299 #endif
300
301 #if HOST_BITS_PER_PTR <= 32
302
303 /* On 32-bit hosts, we use a two level page table, as pictured above.  */
304 typedef page_entry **page_table[PAGE_L1_SIZE];
305
306 #else
307
308 /* On 64-bit hosts, we use the same two level page tables plus a linked
309    list that disambiguates the top 32-bits.  There will almost always be
310    exactly one entry in the list.  */
311 typedef struct page_table_chain
312 {
313   struct page_table_chain *next;
314   size_t high_bits;
315   page_entry **table[PAGE_L1_SIZE];
316 } *page_table;
317
318 #endif
319
320 /* The rest of the global variables.  */
321 static struct globals
322 {
323   /* The Nth element in this array is a page with objects of size 2^N.
324      If there are any pages with free objects, they will be at the
325      head of the list.  NULL if there are no page-entries for this
326      object size.  */
327   page_entry *pages[NUM_ORDERS];
328
329   /* The Nth element in this array is the last page with objects of
330      size 2^N.  NULL if there are no page-entries for this object
331      size.  */
332   page_entry *page_tails[NUM_ORDERS];
333
334   /* Lookup table for associating allocation pages with object addresses.  */
335   page_table lookup;
336
337   /* The system's page size.  */
338   size_t pagesize;
339   size_t lg_pagesize;
340
341   /* Bytes currently allocated.  */
342   size_t allocated;
343
344   /* Bytes currently allocated at the end of the last collection.  */
345   size_t allocated_last_gc;
346
347   /* Total amount of memory mapped.  */
348   size_t bytes_mapped;
349
350   /* Bit N set if any allocations have been done at context depth N.  */
351   unsigned long context_depth_allocations;
352
353   /* Bit N set if any collections have been done at context depth N.  */
354   unsigned long context_depth_collections;
355
356   /* The current depth in the context stack.  */
357   unsigned short context_depth;
358
359   /* A file descriptor open to /dev/zero for reading.  */
360 #if defined (HAVE_MMAP_DEV_ZERO)
361   int dev_zero_fd;
362 #endif
363
364   /* A cache of free system pages.  */
365   page_entry *free_pages;
366
367 #ifdef USING_MALLOC_PAGE_GROUPS
368   page_group *page_groups;
369 #endif
370
371   /* The file descriptor for debugging output.  */
372   FILE *debug_file;
373
374   /* Current number of elements in use in depth below.  */
375   unsigned int depth_in_use;
376
377   /* Maximum number of elements that can be used before resizing.  */
378   unsigned int depth_max;
379
380   /* Each element of this arry is an index in by_depth where the given
381      depth starts.  This structure is indexed by that given depth we
382      are interested in.  */
383   unsigned int *depth;
384
385   /* Current number of elements in use in by_depth below.  */
386   unsigned int by_depth_in_use;
387
388   /* Maximum number of elements that can be used before resizing.  */
389   unsigned int by_depth_max;
390
391   /* Each element of this array is a pointer to a page_entry, all
392      page_entries can be found in here by increasing depth.
393      index_by_depth in the page_entry is the index into this data
394      structure where that page_entry can be found.  This is used to
395      speed up finding all page_entries at a particular depth.  */
396   page_entry **by_depth;
397
398   /* Each element is a pointer to the saved in_use_p bits, if any,
399      zero otherwise.  We allocate them all together, to enable a
400      better runtime data access pattern.  */
401   unsigned long **save_in_use;
402
403 } G;
404
405 /* The size in bytes required to maintain a bitmap for the objects
406    on a page-entry.  */
407 #define BITMAP_SIZE(Num_objects) \
408   (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
409
410 /* Allocate pages in chunks of this size, to throttle calls to memory
411    allocation routines.  The first page is used, the rest go onto the
412    free list.  This cannot be larger than HOST_BITS_PER_INT for the
413    in_use bitmask for page_group.  */
414 #define GGC_QUIRE_SIZE 16
415
416 /* Initial guess as to how many page table entries we might need.  */
417 #define INITIAL_PTE_COUNT 128
418 \f
419 static int ggc_allocated_p (const void *);
420 static page_entry *lookup_page_table_entry (const void *);
421 static void set_page_table_entry (void *, page_entry *);
422 #ifdef USING_MMAP
423 static char *alloc_anon (char *, size_t);
424 #endif
425 #ifdef USING_MALLOC_PAGE_GROUPS
426 static size_t page_group_index (char *, char *);
427 static void set_page_group_in_use (page_group *, char *);
428 static void clear_page_group_in_use (page_group *, char *);
429 #endif
430 static struct page_entry * alloc_page (unsigned);
431 static void free_page (struct page_entry *);
432 static void release_pages (void);
433 static void clear_marks (void);
434 static void sweep_pages (void);
435 static void ggc_recalculate_in_use_p (page_entry *);
436 static void compute_inverse (unsigned);
437 static inline void adjust_depth (void);
438 static void move_ptes_to_front (int, int);
439
440 #ifdef ENABLE_GC_CHECKING
441 static void poison_pages (void);
442 #endif
443
444 void debug_print_page_list (int);
445 static void push_depth (unsigned int);
446 static void push_by_depth (page_entry *, unsigned long *);
447 \f
448 /* Push an entry onto G.depth.  */
449
450 inline static void
451 push_depth (unsigned int i)
452 {
453   if (G.depth_in_use >= G.depth_max)
454     {
455       G.depth_max *= 2;
456       G.depth = (unsigned int *) xrealloc ((char *) G.depth,
457                                            G.depth_max * sizeof (unsigned int));
458     }
459   G.depth[G.depth_in_use++] = i;
460 }
461
462 /* Push an entry onto G.by_depth and G.save_in_use.  */
463
464 inline static void
465 push_by_depth (page_entry *p, unsigned long *s)
466 {
467   if (G.by_depth_in_use >= G.by_depth_max)
468     {
469       G.by_depth_max *= 2;
470       G.by_depth = (page_entry **) xrealloc ((char *) G.by_depth,
471                                              G.by_depth_max * sizeof (page_entry *));
472       G.save_in_use = (unsigned long **) xrealloc ((char *) G.save_in_use,
473                                                    G.by_depth_max * sizeof (unsigned long *));
474     }
475   G.by_depth[G.by_depth_in_use] = p;
476   G.save_in_use[G.by_depth_in_use++] = s;
477 }
478
479 #if (GCC_VERSION < 3001)
480 #define prefetch(X) ((void) X)
481 #else
482 #define prefetch(X) __builtin_prefetch (X)
483 #endif
484
485 #define save_in_use_p_i(__i) \
486   (G.save_in_use[__i])
487 #define save_in_use_p(__p) \
488   (save_in_use_p_i (__p->index_by_depth))
489
490 /* Returns nonzero if P was allocated in GC'able memory.  */
491
492 static inline int
493 ggc_allocated_p (const void *p)
494 {
495   page_entry ***base;
496   size_t L1, L2;
497
498 #if HOST_BITS_PER_PTR <= 32
499   base = &G.lookup[0];
500 #else
501   page_table table = G.lookup;
502   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
503   while (1)
504     {
505       if (table == NULL)
506         return 0;
507       if (table->high_bits == high_bits)
508         break;
509       table = table->next;
510     }
511   base = &table->table[0];
512 #endif
513
514   /* Extract the level 1 and 2 indices.  */
515   L1 = LOOKUP_L1 (p);
516   L2 = LOOKUP_L2 (p);
517
518   return base[L1] && base[L1][L2];
519 }
520
521 /* Traverse the page table and find the entry for a page.
522    Die (probably) if the object wasn't allocated via GC.  */
523
524 static inline page_entry *
525 lookup_page_table_entry (const void *p)
526 {
527   page_entry ***base;
528   size_t L1, L2;
529
530 #if HOST_BITS_PER_PTR <= 32
531   base = &G.lookup[0];
532 #else
533   page_table table = G.lookup;
534   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
535   while (table->high_bits != high_bits)
536     table = table->next;
537   base = &table->table[0];
538 #endif
539
540   /* Extract the level 1 and 2 indices.  */
541   L1 = LOOKUP_L1 (p);
542   L2 = LOOKUP_L2 (p);
543
544   return base[L1][L2];
545 }
546
547 /* Set the page table entry for a page.  */
548
549 static void
550 set_page_table_entry (void *p, page_entry *entry)
551 {
552   page_entry ***base;
553   size_t L1, L2;
554
555 #if HOST_BITS_PER_PTR <= 32
556   base = &G.lookup[0];
557 #else
558   page_table table;
559   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
560   for (table = G.lookup; table; table = table->next)
561     if (table->high_bits == high_bits)
562       goto found;
563
564   /* Not found -- allocate a new table.  */
565   table = (page_table) xcalloc (1, sizeof(*table));
566   table->next = G.lookup;
567   table->high_bits = high_bits;
568   G.lookup = table;
569 found:
570   base = &table->table[0];
571 #endif
572
573   /* Extract the level 1 and 2 indices.  */
574   L1 = LOOKUP_L1 (p);
575   L2 = LOOKUP_L2 (p);
576
577   if (base[L1] == NULL)
578     base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
579
580   base[L1][L2] = entry;
581 }
582
583 /* Prints the page-entry for object size ORDER, for debugging.  */
584
585 void
586 debug_print_page_list (int order)
587 {
588   page_entry *p;
589   printf ("Head=%p, Tail=%p:\n", (void *) G.pages[order],
590           (void *) G.page_tails[order]);
591   p = G.pages[order];
592   while (p != NULL)
593     {
594       printf ("%p(%1d|%3d) -> ", (void *) p, p->context_depth,
595               p->num_free_objects);
596       p = p->next;
597     }
598   printf ("NULL\n");
599   fflush (stdout);
600 }
601
602 #ifdef USING_MMAP
603 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
604    (if non-null).  The ifdef structure here is intended to cause a
605    compile error unless exactly one of the HAVE_* is defined.  */
606
607 static inline char *
608 alloc_anon (char *pref ATTRIBUTE_UNUSED, size_t size)
609 {
610 #ifdef HAVE_MMAP_ANON
611   char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
612                               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
613 #endif
614 #ifdef HAVE_MMAP_DEV_ZERO
615   char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
616                               MAP_PRIVATE, G.dev_zero_fd, 0);
617 #endif
618
619   if (page == (char *) MAP_FAILED)
620     {
621       perror ("virtual memory exhausted");
622       exit (FATAL_EXIT_CODE);
623     }
624
625   /* Remember that we allocated this memory.  */
626   G.bytes_mapped += size;
627
628   /* Pretend we don't have access to the allocated pages.  We'll enable
629      access to smaller pieces of the area in ggc_alloc.  Discard the
630      handle to avoid handle leak.  */
631   VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (page, size));
632
633   return page;
634 }
635 #endif
636 #ifdef USING_MALLOC_PAGE_GROUPS
637 /* Compute the index for this page into the page group.  */
638
639 static inline size_t
640 page_group_index (char *allocation, char *page)
641 {
642   return (size_t) (page - allocation) >> G.lg_pagesize;
643 }
644
645 /* Set and clear the in_use bit for this page in the page group.  */
646
647 static inline void
648 set_page_group_in_use (page_group *group, char *page)
649 {
650   group->in_use |= 1 << page_group_index (group->allocation, page);
651 }
652
653 static inline void
654 clear_page_group_in_use (page_group *group, char *page)
655 {
656   group->in_use &= ~(1 << page_group_index (group->allocation, page));
657 }
658 #endif
659
660 /* Allocate a new page for allocating objects of size 2^ORDER,
661    and return an entry for it.  The entry is not added to the
662    appropriate page_table list.  */
663
664 static inline struct page_entry *
665 alloc_page (unsigned order)
666 {
667   struct page_entry *entry, *p, **pp;
668   char *page;
669   size_t num_objects;
670   size_t bitmap_size;
671   size_t page_entry_size;
672   size_t entry_size;
673 #ifdef USING_MALLOC_PAGE_GROUPS
674   page_group *group;
675 #endif
676
677   num_objects = OBJECTS_PER_PAGE (order);
678   bitmap_size = BITMAP_SIZE (num_objects + 1);
679   page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
680   entry_size = num_objects * OBJECT_SIZE (order);
681   if (entry_size < G.pagesize)
682     entry_size = G.pagesize;
683
684   entry = NULL;
685   page = NULL;
686
687   /* Check the list of free pages for one we can use.  */
688   for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
689     if (p->bytes == entry_size)
690       break;
691
692   if (p != NULL)
693     {
694       /* Recycle the allocated memory from this page ...  */
695       *pp = p->next;
696       page = p->page;
697
698 #ifdef USING_MALLOC_PAGE_GROUPS
699       group = p->group;
700 #endif
701
702       /* ... and, if possible, the page entry itself.  */
703       if (p->order == order)
704         {
705           entry = p;
706           memset (entry, 0, page_entry_size);
707         }
708       else
709         free (p);
710     }
711 #ifdef USING_MMAP
712   else if (entry_size == G.pagesize)
713     {
714       /* We want just one page.  Allocate a bunch of them and put the
715          extras on the freelist.  (Can only do this optimization with
716          mmap for backing store.)  */
717       struct page_entry *e, *f = G.free_pages;
718       int i;
719
720       page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE);
721
722       /* This loop counts down so that the chain will be in ascending
723          memory order.  */
724       for (i = GGC_QUIRE_SIZE - 1; i >= 1; i--)
725         {
726           e = (struct page_entry *) xcalloc (1, page_entry_size);
727           e->order = order;
728           e->bytes = G.pagesize;
729           e->page = page + (i << G.lg_pagesize);
730           e->next = f;
731           f = e;
732         }
733
734       G.free_pages = f;
735     }
736   else
737     page = alloc_anon (NULL, entry_size);
738 #endif
739 #ifdef USING_MALLOC_PAGE_GROUPS
740   else
741     {
742       /* Allocate a large block of memory and serve out the aligned
743          pages therein.  This results in much less memory wastage
744          than the traditional implementation of valloc.  */
745
746       char *allocation, *a, *enda;
747       size_t alloc_size, head_slop, tail_slop;
748       int multiple_pages = (entry_size == G.pagesize);
749
750       if (multiple_pages)
751         alloc_size = GGC_QUIRE_SIZE * G.pagesize;
752       else
753         alloc_size = entry_size + G.pagesize - 1;
754       allocation = xmalloc (alloc_size);
755
756       page = (char *) (((size_t) allocation + G.pagesize - 1) & -G.pagesize);
757       head_slop = page - allocation;
758       if (multiple_pages)
759         tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
760       else
761         tail_slop = alloc_size - entry_size - head_slop;
762       enda = allocation + alloc_size - tail_slop;
763
764       /* We allocated N pages, which are likely not aligned, leaving
765          us with N-1 usable pages.  We plan to place the page_group
766          structure somewhere in the slop.  */
767       if (head_slop >= sizeof (page_group))
768         group = (page_group *)page - 1;
769       else
770         {
771           /* We magically got an aligned allocation.  Too bad, we have
772              to waste a page anyway.  */
773           if (tail_slop == 0)
774             {
775               enda -= G.pagesize;
776               tail_slop += G.pagesize;
777             }
778           if (tail_slop < sizeof (page_group))
779             abort ();
780           group = (page_group *)enda;
781           tail_slop -= sizeof (page_group);
782         }
783
784       /* Remember that we allocated this memory.  */
785       group->next = G.page_groups;
786       group->allocation = allocation;
787       group->alloc_size = alloc_size;
788       group->in_use = 0;
789       G.page_groups = group;
790       G.bytes_mapped += alloc_size;
791
792       /* If we allocated multiple pages, put the rest on the free list.  */
793       if (multiple_pages)
794         {
795           struct page_entry *e, *f = G.free_pages;
796           for (a = enda - G.pagesize; a != page; a -= G.pagesize)
797             {
798               e = (struct page_entry *) xcalloc (1, page_entry_size);
799               e->order = order;
800               e->bytes = G.pagesize;
801               e->page = a;
802               e->group = group;
803               e->next = f;
804               f = e;
805             }
806           G.free_pages = f;
807         }
808     }
809 #endif
810
811   if (entry == NULL)
812     entry = (struct page_entry *) xcalloc (1, page_entry_size);
813
814   entry->bytes = entry_size;
815   entry->page = page;
816   entry->context_depth = G.context_depth;
817   entry->order = order;
818   entry->num_free_objects = num_objects;
819   entry->next_bit_hint = 1;
820
821   G.context_depth_allocations |= (unsigned long)1 << G.context_depth;
822
823 #ifdef USING_MALLOC_PAGE_GROUPS
824   entry->group = group;
825   set_page_group_in_use (group, page);
826 #endif
827
828   /* Set the one-past-the-end in-use bit.  This acts as a sentry as we
829      increment the hint.  */
830   entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
831     = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
832
833   set_page_table_entry (page, entry);
834
835   if (GGC_DEBUG_LEVEL >= 2)
836     fprintf (G.debug_file,
837              "Allocating page at %p, object size=%lu, data %p-%p\n",
838              (void *) entry, (unsigned long) OBJECT_SIZE (order), page,
839              page + entry_size - 1);
840
841   return entry;
842 }
843
844 /* Adjust the size of G.depth so that no index greater than the one
845    used by the top of the G.by_depth is used.  */
846
847 static inline void
848 adjust_depth (void)
849 {
850   page_entry *top;
851
852   if (G.by_depth_in_use)
853     {
854       top = G.by_depth[G.by_depth_in_use-1];
855
856       /* Peel back indicies in depth that index into by_depth, so that
857          as new elements are added to by_depth, we note the indicies
858          of those elements, if they are for new context depths.  */
859       while (G.depth_in_use > (size_t)top->context_depth+1)
860         --G.depth_in_use;
861     }
862 }
863
864 /* For a page that is no longer needed, put it on the free page list.  */
865
866 static inline void
867 free_page (page_entry *entry)
868 {
869   if (GGC_DEBUG_LEVEL >= 2)
870     fprintf (G.debug_file,
871              "Deallocating page at %p, data %p-%p\n", (void *) entry,
872              entry->page, entry->page + entry->bytes - 1);
873
874   /* Mark the page as inaccessible.  Discard the handle to avoid handle
875      leak.  */
876   VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (entry->page, entry->bytes));
877
878   set_page_table_entry (entry->page, NULL);
879
880 #ifdef USING_MALLOC_PAGE_GROUPS
881   clear_page_group_in_use (entry->group, entry->page);
882 #endif
883
884   if (G.by_depth_in_use > 1)
885     {
886       page_entry *top = G.by_depth[G.by_depth_in_use-1];
887
888       /* If they are at the same depth, put top element into freed
889          slot.  */
890       if (entry->context_depth == top->context_depth)
891         {
892           int i = entry->index_by_depth;
893           G.by_depth[i] = top;
894           G.save_in_use[i] = G.save_in_use[G.by_depth_in_use-1];
895           top->index_by_depth = i;
896         }
897       else
898         {
899           /* We cannot free a page from a context deeper than the
900              current one.  */
901           abort ();
902         }
903     }
904   --G.by_depth_in_use;
905
906   adjust_depth ();
907
908   entry->next = G.free_pages;
909   G.free_pages = entry;
910 }
911
912 /* Release the free page cache to the system.  */
913
914 static void
915 release_pages (void)
916 {
917 #ifdef USING_MMAP
918   page_entry *p, *next;
919   char *start;
920   size_t len;
921
922   /* Gather up adjacent pages so they are unmapped together.  */
923   p = G.free_pages;
924
925   while (p)
926     {
927       start = p->page;
928       next = p->next;
929       len = p->bytes;
930       free (p);
931       p = next;
932
933       while (p && p->page == start + len)
934         {
935           next = p->next;
936           len += p->bytes;
937           free (p);
938           p = next;
939         }
940
941       munmap (start, len);
942       G.bytes_mapped -= len;
943     }
944
945   G.free_pages = NULL;
946 #endif
947 #ifdef USING_MALLOC_PAGE_GROUPS
948   page_entry **pp, *p;
949   page_group **gp, *g;
950
951   /* Remove all pages from free page groups from the list.  */
952   pp = &G.free_pages;
953   while ((p = *pp) != NULL)
954     if (p->group->in_use == 0)
955       {
956         *pp = p->next;
957         free (p);
958       }
959     else
960       pp = &p->next;
961
962   /* Remove all free page groups, and release the storage.  */
963   gp = &G.page_groups;
964   while ((g = *gp) != NULL)
965     if (g->in_use == 0)
966       {
967         *gp = g->next;
968         G.bytes_mapped -= g->alloc_size;
969         free (g->allocation);
970       }
971     else
972       gp = &g->next;
973 #endif
974 }
975
976 /* This table provides a fast way to determine ceil(log_2(size)) for
977    allocation requests.  The minimum allocation size is eight bytes.  */
978
979 static unsigned char size_lookup[257] =
980 {
981   3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
982   4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
983   5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
984   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
985   6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
986   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
987   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
988   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
989   7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
990   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
991   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
992   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
993   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
994   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
995   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
996   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
997   8
998 };
999
1000 /* Allocate a chunk of memory of SIZE bytes.  Its contents are undefined.  */
1001
1002 void *
1003 ggc_alloc (size_t size)
1004 {
1005   unsigned order, word, bit, object_offset;
1006   struct page_entry *entry;
1007   void *result;
1008
1009   if (size <= 256)
1010     order = size_lookup[size];
1011   else
1012     {
1013       order = 9;
1014       while (size > OBJECT_SIZE (order))
1015         order++;
1016     }
1017
1018   /* If there are non-full pages for this size allocation, they are at
1019      the head of the list.  */
1020   entry = G.pages[order];
1021
1022   /* If there is no page for this object size, or all pages in this
1023      context are full, allocate a new page.  */
1024   if (entry == NULL || entry->num_free_objects == 0)
1025     {
1026       struct page_entry *new_entry;
1027       new_entry = alloc_page (order);
1028
1029       new_entry->index_by_depth = G.by_depth_in_use;
1030       push_by_depth (new_entry, 0);
1031
1032       /* We can skip context depths, if we do, make sure we go all the
1033          way to the new depth.  */
1034       while (new_entry->context_depth >= G.depth_in_use)
1035         push_depth (G.by_depth_in_use-1);
1036
1037       /* If this is the only entry, it's also the tail.  */
1038       if (entry == NULL)
1039         G.page_tails[order] = new_entry;
1040
1041       /* Put new pages at the head of the page list.  */
1042       new_entry->next = entry;
1043       entry = new_entry;
1044       G.pages[order] = new_entry;
1045
1046       /* For a new page, we know the word and bit positions (in the
1047          in_use bitmap) of the first available object -- they're zero.  */
1048       new_entry->next_bit_hint = 1;
1049       word = 0;
1050       bit = 0;
1051       object_offset = 0;
1052     }
1053   else
1054     {
1055       /* First try to use the hint left from the previous allocation
1056          to locate a clear bit in the in-use bitmap.  We've made sure
1057          that the one-past-the-end bit is always set, so if the hint
1058          has run over, this test will fail.  */
1059       unsigned hint = entry->next_bit_hint;
1060       word = hint / HOST_BITS_PER_LONG;
1061       bit = hint % HOST_BITS_PER_LONG;
1062
1063       /* If the hint didn't work, scan the bitmap from the beginning.  */
1064       if ((entry->in_use_p[word] >> bit) & 1)
1065         {
1066           word = bit = 0;
1067           while (~entry->in_use_p[word] == 0)
1068             ++word;
1069           while ((entry->in_use_p[word] >> bit) & 1)
1070             ++bit;
1071           hint = word * HOST_BITS_PER_LONG + bit;
1072         }
1073
1074       /* Next time, try the next bit.  */
1075       entry->next_bit_hint = hint + 1;
1076
1077       object_offset = hint * OBJECT_SIZE (order);
1078     }
1079
1080   /* Set the in-use bit.  */
1081   entry->in_use_p[word] |= ((unsigned long) 1 << bit);
1082
1083   /* Keep a running total of the number of free objects.  If this page
1084      fills up, we may have to move it to the end of the list if the
1085      next page isn't full.  If the next page is full, all subsequent
1086      pages are full, so there's no need to move it.  */
1087   if (--entry->num_free_objects == 0
1088       && entry->next != NULL
1089       && entry->next->num_free_objects > 0)
1090     {
1091       G.pages[order] = entry->next;
1092       entry->next = NULL;
1093       G.page_tails[order]->next = entry;
1094       G.page_tails[order] = entry;
1095     }
1096
1097   /* Calculate the object's address.  */
1098   result = entry->page + object_offset;
1099
1100 #ifdef ENABLE_GC_CHECKING
1101   /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
1102      exact same semantics in presence of memory bugs, regardless of
1103      ENABLE_VALGRIND_CHECKING.  We override this request below.  Drop the
1104      handle to avoid handle leak.  */
1105   VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, OBJECT_SIZE (order)));
1106
1107   /* `Poison' the entire allocated object, including any padding at
1108      the end.  */
1109   memset (result, 0xaf, OBJECT_SIZE (order));
1110
1111   /* Make the bytes after the end of the object unaccessible.  Discard the
1112      handle to avoid handle leak.  */
1113   VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) result + size,
1114                                             OBJECT_SIZE (order) - size));
1115 #endif
1116
1117   /* Tell Valgrind that the memory is there, but its content isn't
1118      defined.  The bytes at the end of the object are still marked
1119      unaccessible.  */
1120   VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, size));
1121
1122   /* Keep track of how many bytes are being allocated.  This
1123      information is used in deciding when to collect.  */
1124   G.allocated += OBJECT_SIZE (order);
1125
1126   if (GGC_DEBUG_LEVEL >= 3)
1127     fprintf (G.debug_file,
1128              "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
1129              (unsigned long) size, (unsigned long) OBJECT_SIZE (order), result,
1130              (void *) entry);
1131
1132   return result;
1133 }
1134
1135 /* If P is not marked, marks it and return false.  Otherwise return true.
1136    P must have been allocated by the GC allocator; it mustn't point to
1137    static objects, stack variables, or memory allocated with malloc.  */
1138
1139 int
1140 ggc_set_mark (const void *p)
1141 {
1142   page_entry *entry;
1143   unsigned bit, word;
1144   unsigned long mask;
1145
1146   /* Look up the page on which the object is alloced.  If the object
1147      wasn't allocated by the collector, we'll probably die.  */
1148   entry = lookup_page_table_entry (p);
1149 #ifdef ENABLE_CHECKING
1150   if (entry == NULL)
1151     abort ();
1152 #endif
1153
1154   /* Calculate the index of the object on the page; this is its bit
1155      position in the in_use_p bitmap.  */
1156   bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1157   word = bit / HOST_BITS_PER_LONG;
1158   mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1159
1160   /* If the bit was previously set, skip it.  */
1161   if (entry->in_use_p[word] & mask)
1162     return 1;
1163
1164   /* Otherwise set it, and decrement the free object count.  */
1165   entry->in_use_p[word] |= mask;
1166   entry->num_free_objects -= 1;
1167
1168   if (GGC_DEBUG_LEVEL >= 4)
1169     fprintf (G.debug_file, "Marking %p\n", p);
1170
1171   return 0;
1172 }
1173
1174 /* Return 1 if P has been marked, zero otherwise.
1175    P must have been allocated by the GC allocator; it mustn't point to
1176    static objects, stack variables, or memory allocated with malloc.  */
1177
1178 int
1179 ggc_marked_p (const void *p)
1180 {
1181   page_entry *entry;
1182   unsigned bit, word;
1183   unsigned long mask;
1184
1185   /* Look up the page on which the object is alloced.  If the object
1186      wasn't allocated by the collector, we'll probably die.  */
1187   entry = lookup_page_table_entry (p);
1188 #ifdef ENABLE_CHECKING
1189   if (entry == NULL)
1190     abort ();
1191 #endif
1192
1193   /* Calculate the index of the object on the page; this is its bit
1194      position in the in_use_p bitmap.  */
1195   bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1196   word = bit / HOST_BITS_PER_LONG;
1197   mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1198
1199   return (entry->in_use_p[word] & mask) != 0;
1200 }
1201
1202 /* Return the size of the gc-able object P.  */
1203
1204 size_t
1205 ggc_get_size (const void *p)
1206 {
1207   page_entry *pe = lookup_page_table_entry (p);
1208   return OBJECT_SIZE (pe->order);
1209 }
1210 \f
1211 /* Subroutine of init_ggc which computes the pair of numbers used to
1212    perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1213
1214    This algorithm is taken from Granlund and Montgomery's paper
1215    "Division by Invariant Integers using Multiplication"
1216    (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1217    constants).  */
1218
1219 static void
1220 compute_inverse (unsigned order)
1221 {
1222   unsigned size, inv, e;
1223
1224   /* There can be only one object per "page" in a bucket for sizes
1225      larger than half a machine page; it will always have offset zero.  */
1226   if (OBJECT_SIZE (order) > G.pagesize/2)
1227     {
1228       if (OBJECTS_PER_PAGE (order) != 1)
1229         abort ();
1230
1231       DIV_MULT (order) = 1;
1232       DIV_SHIFT (order) = 0;
1233       return;
1234     }
1235
1236   size = OBJECT_SIZE (order);
1237   e = 0;
1238   while (size % 2 == 0)
1239     {
1240       e++;
1241       size >>= 1;
1242     }
1243
1244   inv = size;
1245   while (inv * size != 1)
1246     inv = inv * (2 - inv*size);
1247
1248   DIV_MULT (order) = inv;
1249   DIV_SHIFT (order) = e;
1250 }
1251
1252 /* Initialize the ggc-mmap allocator.  */
1253 void
1254 init_ggc (void)
1255 {
1256   unsigned order;
1257
1258   G.pagesize = getpagesize();
1259   G.lg_pagesize = exact_log2 (G.pagesize);
1260
1261 #ifdef HAVE_MMAP_DEV_ZERO
1262   G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1263   if (G.dev_zero_fd == -1)
1264     internal_error ("open /dev/zero: %m");
1265 #endif
1266
1267 #if 0
1268   G.debug_file = fopen ("ggc-mmap.debug", "w");
1269 #else
1270   G.debug_file = stdout;
1271 #endif
1272
1273 #ifdef USING_MMAP
1274   /* StunOS has an amazing off-by-one error for the first mmap allocation
1275      after fiddling with RLIMIT_STACK.  The result, as hard as it is to
1276      believe, is an unaligned page allocation, which would cause us to
1277      hork badly if we tried to use it.  */
1278   {
1279     char *p = alloc_anon (NULL, G.pagesize);
1280     struct page_entry *e;
1281     if ((size_t)p & (G.pagesize - 1))
1282       {
1283         /* How losing.  Discard this one and try another.  If we still
1284            can't get something useful, give up.  */
1285
1286         p = alloc_anon (NULL, G.pagesize);
1287         if ((size_t)p & (G.pagesize - 1))
1288           abort ();
1289       }
1290
1291     /* We have a good page, might as well hold onto it...  */
1292     e = (struct page_entry *) xcalloc (1, sizeof (struct page_entry));
1293     e->bytes = G.pagesize;
1294     e->page = p;
1295     e->next = G.free_pages;
1296     G.free_pages = e;
1297   }
1298 #endif
1299
1300   /* Initialize the object size table.  */
1301   for (order = 0; order < HOST_BITS_PER_PTR; ++order)
1302     object_size_table[order] = (size_t) 1 << order;
1303   for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1304     {
1305       size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
1306
1307       /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1308          so that we're sure of getting aligned memory.  */
1309       s = ROUND_UP (s, MAX_ALIGNMENT);
1310       object_size_table[order] = s;
1311     }
1312
1313   /* Initialize the objects-per-page and inverse tables.  */
1314   for (order = 0; order < NUM_ORDERS; ++order)
1315     {
1316       objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
1317       if (objects_per_page_table[order] == 0)
1318         objects_per_page_table[order] = 1;
1319       compute_inverse (order);
1320     }
1321
1322   /* Reset the size_lookup array to put appropriately sized objects in
1323      the special orders.  All objects bigger than the previous power
1324      of two, but no greater than the special size, should go in the
1325      new order.  */
1326   for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1327     {
1328       int o;
1329       int i;
1330
1331       o = size_lookup[OBJECT_SIZE (order)];
1332       for (i = OBJECT_SIZE (order); size_lookup [i] == o; --i)
1333         size_lookup[i] = order;
1334     }
1335
1336   G.depth_in_use = 0;
1337   G.depth_max = 10;
1338   G.depth = (unsigned int *) xmalloc (G.depth_max * sizeof (unsigned int));
1339
1340   G.by_depth_in_use = 0;
1341   G.by_depth_max = INITIAL_PTE_COUNT;
1342   G.by_depth = (page_entry **) xmalloc (G.by_depth_max * sizeof (page_entry *));
1343   G.save_in_use = (unsigned long **) xmalloc (G.by_depth_max * sizeof (unsigned long *));
1344 }
1345
1346 /* Increment the `GC context'.  Objects allocated in an outer context
1347    are never freed, eliminating the need to register their roots.  */
1348
1349 void
1350 ggc_push_context (void)
1351 {
1352   ++G.context_depth;
1353
1354   /* Die on wrap.  */
1355   if (G.context_depth >= HOST_BITS_PER_LONG)
1356     abort ();
1357 }
1358
1359 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1360    reflects reality.  Recalculate NUM_FREE_OBJECTS as well.  */
1361
1362 static void
1363 ggc_recalculate_in_use_p (page_entry *p)
1364 {
1365   unsigned int i;
1366   size_t num_objects;
1367
1368   /* Because the past-the-end bit in in_use_p is always set, we
1369      pretend there is one additional object.  */
1370   num_objects = OBJECTS_IN_PAGE (p) + 1;
1371
1372   /* Reset the free object count.  */
1373   p->num_free_objects = num_objects;
1374
1375   /* Combine the IN_USE_P and SAVE_IN_USE_P arrays.  */
1376   for (i = 0;
1377        i < CEIL (BITMAP_SIZE (num_objects),
1378                  sizeof (*p->in_use_p));
1379        ++i)
1380     {
1381       unsigned long j;
1382
1383       /* Something is in use if it is marked, or if it was in use in a
1384          context further down the context stack.  */
1385       p->in_use_p[i] |= save_in_use_p (p)[i];
1386
1387       /* Decrement the free object count for every object allocated.  */
1388       for (j = p->in_use_p[i]; j; j >>= 1)
1389         p->num_free_objects -= (j & 1);
1390     }
1391
1392   if (p->num_free_objects >= num_objects)
1393     abort ();
1394 }
1395
1396 /* Decrement the `GC context'.  All objects allocated since the
1397    previous ggc_push_context are migrated to the outer context.  */
1398
1399 void
1400 ggc_pop_context (void)
1401 {
1402   unsigned long omask;
1403   unsigned int depth, i, e;
1404 #ifdef ENABLE_CHECKING
1405   unsigned int order;
1406 #endif
1407
1408   depth = --G.context_depth;
1409   omask = (unsigned long)1 << (depth + 1);
1410
1411   if (!((G.context_depth_allocations | G.context_depth_collections) & omask))
1412     return;
1413
1414   G.context_depth_allocations |= (G.context_depth_allocations & omask) >> 1;
1415   G.context_depth_allocations &= omask - 1;
1416   G.context_depth_collections &= omask - 1;
1417
1418   /* The G.depth array is shortend so that the last index is the
1419      context_depth of the top element of by_depth.  */
1420   if (depth+1 < G.depth_in_use)
1421     e = G.depth[depth+1];
1422   else
1423     e = G.by_depth_in_use;
1424
1425   /* We might not have any PTEs of depth depth.  */
1426   if (depth < G.depth_in_use)
1427     {
1428
1429       /* First we go through all the pages at depth depth to
1430          recalculate the in use bits.  */
1431       for (i = G.depth[depth]; i < e; ++i)
1432         {
1433           page_entry *p;
1434
1435 #ifdef ENABLE_CHECKING
1436           p = G.by_depth[i];
1437
1438           /* Check that all of the pages really are at the depth that
1439              we expect.  */
1440           if (p->context_depth != depth)
1441             abort ();
1442           if (p->index_by_depth != i)
1443             abort ();
1444 #endif
1445
1446           prefetch (&save_in_use_p_i (i+8));
1447           prefetch (&save_in_use_p_i (i+16));
1448           if (save_in_use_p_i (i))
1449             {
1450               p = G.by_depth[i];
1451               ggc_recalculate_in_use_p (p);
1452               free (save_in_use_p_i (i));
1453               save_in_use_p_i (i) = 0;
1454             }
1455         }
1456     }
1457
1458   /* Then, we reset all page_entries with a depth greater than depth
1459      to be at depth.  */
1460   for (i = e; i < G.by_depth_in_use; ++i)
1461     {
1462       page_entry *p = G.by_depth[i];
1463
1464       /* Check that all of the pages really are at the depth we
1465          expect.  */
1466 #ifdef ENABLE_CHECKING
1467       if (p->context_depth <= depth)
1468         abort ();
1469       if (p->index_by_depth != i)
1470         abort ();
1471 #endif
1472       p->context_depth = depth;
1473     }
1474
1475   adjust_depth ();
1476
1477 #ifdef ENABLE_CHECKING
1478   for (order = 2; order < NUM_ORDERS; order++)
1479     {
1480       page_entry *p;
1481
1482       for (p = G.pages[order]; p != NULL; p = p->next)
1483         {
1484           if (p->context_depth > depth)
1485             abort ();
1486           else if (p->context_depth == depth && save_in_use_p (p))
1487             abort ();
1488         }
1489     }
1490 #endif
1491 }
1492 \f
1493 /* Unmark all objects.  */
1494
1495 static inline void
1496 clear_marks (void)
1497 {
1498   unsigned order;
1499
1500   for (order = 2; order < NUM_ORDERS; order++)
1501     {
1502       page_entry *p;
1503
1504       for (p = G.pages[order]; p != NULL; p = p->next)
1505         {
1506           size_t num_objects = OBJECTS_IN_PAGE (p);
1507           size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
1508
1509 #ifdef ENABLE_CHECKING
1510           /* The data should be page-aligned.  */
1511           if ((size_t) p->page & (G.pagesize - 1))
1512             abort ();
1513 #endif
1514
1515           /* Pages that aren't in the topmost context are not collected;
1516              nevertheless, we need their in-use bit vectors to store GC
1517              marks.  So, back them up first.  */
1518           if (p->context_depth < G.context_depth)
1519             {
1520               if (! save_in_use_p (p))
1521                 save_in_use_p (p) = xmalloc (bitmap_size);
1522               memcpy (save_in_use_p (p), p->in_use_p, bitmap_size);
1523             }
1524
1525           /* Reset reset the number of free objects and clear the
1526              in-use bits.  These will be adjusted by mark_obj.  */
1527           p->num_free_objects = num_objects;
1528           memset (p->in_use_p, 0, bitmap_size);
1529
1530           /* Make sure the one-past-the-end bit is always set.  */
1531           p->in_use_p[num_objects / HOST_BITS_PER_LONG]
1532             = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
1533         }
1534     }
1535 }
1536
1537 /* Free all empty pages.  Partially empty pages need no attention
1538    because the `mark' bit doubles as an `unused' bit.  */
1539
1540 static inline void
1541 sweep_pages (void)
1542 {
1543   unsigned order;
1544
1545   for (order = 2; order < NUM_ORDERS; order++)
1546     {
1547       /* The last page-entry to consider, regardless of entries
1548          placed at the end of the list.  */
1549       page_entry * const last = G.page_tails[order];
1550
1551       size_t num_objects;
1552       size_t live_objects;
1553       page_entry *p, *previous;
1554       int done;
1555
1556       p = G.pages[order];
1557       if (p == NULL)
1558         continue;
1559
1560       previous = NULL;
1561       do
1562         {
1563           page_entry *next = p->next;
1564
1565           /* Loop until all entries have been examined.  */
1566           done = (p == last);
1567
1568           num_objects = OBJECTS_IN_PAGE (p);
1569
1570           /* Add all live objects on this page to the count of
1571              allocated memory.  */
1572           live_objects = num_objects - p->num_free_objects;
1573
1574           G.allocated += OBJECT_SIZE (order) * live_objects;
1575
1576           /* Only objects on pages in the topmost context should get
1577              collected.  */
1578           if (p->context_depth < G.context_depth)
1579             ;
1580
1581           /* Remove the page if it's empty.  */
1582           else if (live_objects == 0)
1583             {
1584               if (! previous)
1585                 G.pages[order] = next;
1586               else
1587                 previous->next = next;
1588
1589               /* Are we removing the last element?  */
1590               if (p == G.page_tails[order])
1591                 G.page_tails[order] = previous;
1592               free_page (p);
1593               p = previous;
1594             }
1595
1596           /* If the page is full, move it to the end.  */
1597           else if (p->num_free_objects == 0)
1598             {
1599               /* Don't move it if it's already at the end.  */
1600               if (p != G.page_tails[order])
1601                 {
1602                   /* Move p to the end of the list.  */
1603                   p->next = NULL;
1604                   G.page_tails[order]->next = p;
1605
1606                   /* Update the tail pointer...  */
1607                   G.page_tails[order] = p;
1608
1609                   /* ... and the head pointer, if necessary.  */
1610                   if (! previous)
1611                     G.pages[order] = next;
1612                   else
1613                     previous->next = next;
1614                   p = previous;
1615                 }
1616             }
1617
1618           /* If we've fallen through to here, it's a page in the
1619              topmost context that is neither full nor empty.  Such a
1620              page must precede pages at lesser context depth in the
1621              list, so move it to the head.  */
1622           else if (p != G.pages[order])
1623             {
1624               previous->next = p->next;
1625               p->next = G.pages[order];
1626               G.pages[order] = p;
1627               /* Are we moving the last element?  */
1628               if (G.page_tails[order] == p)
1629                 G.page_tails[order] = previous;
1630               p = previous;
1631             }
1632
1633           previous = p;
1634           p = next;
1635         }
1636       while (! done);
1637
1638       /* Now, restore the in_use_p vectors for any pages from contexts
1639          other than the current one.  */
1640       for (p = G.pages[order]; p; p = p->next)
1641         if (p->context_depth != G.context_depth)
1642           ggc_recalculate_in_use_p (p);
1643     }
1644 }
1645
1646 #ifdef ENABLE_GC_CHECKING
1647 /* Clobber all free objects.  */
1648
1649 static inline void
1650 poison_pages (void)
1651 {
1652   unsigned order;
1653
1654   for (order = 2; order < NUM_ORDERS; order++)
1655     {
1656       size_t size = OBJECT_SIZE (order);
1657       page_entry *p;
1658
1659       for (p = G.pages[order]; p != NULL; p = p->next)
1660         {
1661           size_t num_objects;
1662           size_t i;
1663
1664           if (p->context_depth != G.context_depth)
1665             /* Since we don't do any collection for pages in pushed
1666                contexts, there's no need to do any poisoning.  And
1667                besides, the IN_USE_P array isn't valid until we pop
1668                contexts.  */
1669             continue;
1670
1671           num_objects = OBJECTS_IN_PAGE (p);
1672           for (i = 0; i < num_objects; i++)
1673             {
1674               size_t word, bit;
1675               word = i / HOST_BITS_PER_LONG;
1676               bit = i % HOST_BITS_PER_LONG;
1677               if (((p->in_use_p[word] >> bit) & 1) == 0)
1678                 {
1679                   char *object = p->page + i * size;
1680
1681                   /* Keep poison-by-write when we expect to use Valgrind,
1682                      so the exact same memory semantics is kept, in case
1683                      there are memory errors.  We override this request
1684                      below.  */
1685                   VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (object, size));
1686                   memset (object, 0xa5, size);
1687
1688                   /* Drop the handle to avoid handle leak.  */
1689                   VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (object, size));
1690                 }
1691             }
1692         }
1693     }
1694 }
1695 #endif
1696
1697 /* Top level mark-and-sweep routine.  */
1698
1699 void
1700 ggc_collect (void)
1701 {
1702   /* Avoid frequent unnecessary work by skipping collection if the
1703      total allocations haven't expanded much since the last
1704      collection.  */
1705   float allocated_last_gc =
1706     MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
1707
1708   float min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
1709
1710   if (G.allocated < allocated_last_gc + min_expand)
1711     return;
1712
1713   timevar_push (TV_GC);
1714   if (!quiet_flag)
1715     fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
1716
1717   /* Zero the total allocated bytes.  This will be recalculated in the
1718      sweep phase.  */
1719   G.allocated = 0;
1720
1721   /* Release the pages we freed the last time we collected, but didn't
1722      reuse in the interim.  */
1723   release_pages ();
1724
1725   /* Indicate that we've seen collections at this context depth.  */
1726   G.context_depth_collections = ((unsigned long)1 << (G.context_depth + 1)) - 1;
1727
1728   clear_marks ();
1729   ggc_mark_roots ();
1730
1731 #ifdef ENABLE_GC_CHECKING
1732   poison_pages ();
1733 #endif
1734
1735   sweep_pages ();
1736
1737   G.allocated_last_gc = G.allocated;
1738
1739   timevar_pop (TV_GC);
1740
1741   if (!quiet_flag)
1742     fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
1743 }
1744
1745 /* Print allocation statistics.  */
1746 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1747                   ? (x) \
1748                   : ((x) < 1024*1024*10 \
1749                      ? (x) / 1024 \
1750                      : (x) / (1024*1024))))
1751 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1752
1753 void
1754 ggc_print_statistics (void)
1755 {
1756   struct ggc_statistics stats;
1757   unsigned int i;
1758   size_t total_overhead = 0;
1759
1760   /* Clear the statistics.  */
1761   memset (&stats, 0, sizeof (stats));
1762
1763   /* Make sure collection will really occur.  */
1764   G.allocated_last_gc = 0;
1765
1766   /* Collect and print the statistics common across collectors.  */
1767   ggc_print_common_statistics (stderr, &stats);
1768
1769   /* Release free pages so that we will not count the bytes allocated
1770      there as part of the total allocated memory.  */
1771   release_pages ();
1772
1773   /* Collect some information about the various sizes of
1774      allocation.  */
1775   fprintf (stderr, "\n%-5s %10s  %10s  %10s\n",
1776            "Size", "Allocated", "Used", "Overhead");
1777   for (i = 0; i < NUM_ORDERS; ++i)
1778     {
1779       page_entry *p;
1780       size_t allocated;
1781       size_t in_use;
1782       size_t overhead;
1783
1784       /* Skip empty entries.  */
1785       if (!G.pages[i])
1786         continue;
1787
1788       overhead = allocated = in_use = 0;
1789
1790       /* Figure out the total number of bytes allocated for objects of
1791          this size, and how many of them are actually in use.  Also figure
1792          out how much memory the page table is using.  */
1793       for (p = G.pages[i]; p; p = p->next)
1794         {
1795           allocated += p->bytes;
1796           in_use +=
1797             (OBJECTS_IN_PAGE (p) - p->num_free_objects) * OBJECT_SIZE (i);
1798
1799           overhead += (sizeof (page_entry) - sizeof (long)
1800                        + BITMAP_SIZE (OBJECTS_IN_PAGE (p) + 1));
1801         }
1802       fprintf (stderr, "%-5lu %10lu%c %10lu%c %10lu%c\n",
1803                (unsigned long) OBJECT_SIZE (i),
1804                SCALE (allocated), LABEL (allocated),
1805                SCALE (in_use), LABEL (in_use),
1806                SCALE (overhead), LABEL (overhead));
1807       total_overhead += overhead;
1808     }
1809   fprintf (stderr, "%-5s %10lu%c %10lu%c %10lu%c\n", "Total",
1810            SCALE (G.bytes_mapped), LABEL (G.bytes_mapped),
1811            SCALE (G.allocated), LABEL(G.allocated),
1812            SCALE (total_overhead), LABEL (total_overhead));
1813 }
1814 \f
1815 struct ggc_pch_data
1816 {
1817   struct ggc_pch_ondisk
1818   {
1819     unsigned totals[NUM_ORDERS];
1820   } d;
1821   size_t base[NUM_ORDERS];
1822   size_t written[NUM_ORDERS];
1823 };
1824
1825 struct ggc_pch_data *
1826 init_ggc_pch (void)
1827 {
1828   return xcalloc (sizeof (struct ggc_pch_data), 1);
1829 }
1830
1831 void
1832 ggc_pch_count_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
1833                       size_t size)
1834 {
1835   unsigned order;
1836
1837   if (size <= 256)
1838     order = size_lookup[size];
1839   else
1840     {
1841       order = 9;
1842       while (size > OBJECT_SIZE (order))
1843         order++;
1844     }
1845
1846   d->d.totals[order]++;
1847 }
1848
1849 size_t
1850 ggc_pch_total_size (struct ggc_pch_data *d)
1851 {
1852   size_t a = 0;
1853   unsigned i;
1854
1855   for (i = 0; i < NUM_ORDERS; i++)
1856     a += ROUND_UP (d->d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1857   return a;
1858 }
1859
1860 void
1861 ggc_pch_this_base (struct ggc_pch_data *d, void *base)
1862 {
1863   size_t a = (size_t) base;
1864   unsigned i;
1865
1866   for (i = 0; i < NUM_ORDERS; i++)
1867     {
1868       d->base[i] = a;
1869       a += ROUND_UP (d->d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1870     }
1871 }
1872
1873
1874 char *
1875 ggc_pch_alloc_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
1876                       size_t size)
1877 {
1878   unsigned order;
1879   char *result;
1880
1881   if (size <= 256)
1882     order = size_lookup[size];
1883   else
1884     {
1885       order = 9;
1886       while (size > OBJECT_SIZE (order))
1887         order++;
1888     }
1889
1890   result = (char *) d->base[order];
1891   d->base[order] += OBJECT_SIZE (order);
1892   return result;
1893 }
1894
1895 void
1896 ggc_pch_prepare_write (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
1897                        FILE *f ATTRIBUTE_UNUSED)
1898 {
1899   /* Nothing to do.  */
1900 }
1901
1902 void
1903 ggc_pch_write_object (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
1904                       FILE *f, void *x, void *newx ATTRIBUTE_UNUSED,
1905                       size_t size)
1906 {
1907   unsigned order;
1908
1909   if (size <= 256)
1910     order = size_lookup[size];
1911   else
1912     {
1913       order = 9;
1914       while (size > OBJECT_SIZE (order))
1915         order++;
1916     }
1917
1918   if (fwrite (x, size, 1, f) != 1)
1919     fatal_error ("can't write PCH file: %m");
1920
1921   /* In the current implementation, SIZE is always equal to
1922      OBJECT_SIZE (order) and so the fseek is never executed.  */
1923   if (size != OBJECT_SIZE (order)
1924       && fseek (f, OBJECT_SIZE (order) - size, SEEK_CUR) != 0)
1925     fatal_error ("can't write PCH file: %m");
1926
1927   d->written[order]++;
1928   if (d->written[order] == d->d.totals[order]
1929       && fseek (f, ROUND_UP_VALUE (d->d.totals[order] * OBJECT_SIZE (order),
1930                                    G.pagesize),
1931                 SEEK_CUR) != 0)
1932     fatal_error ("can't write PCH file: %m");
1933 }
1934
1935 void
1936 ggc_pch_finish (struct ggc_pch_data *d, FILE *f)
1937 {
1938   if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
1939     fatal_error ("can't write PCH file: %m");
1940   free (d);
1941 }
1942
1943 /* Move the PCH PTE entries just added to the end of by_depth, to the
1944    front.  */
1945
1946 static void
1947 move_ptes_to_front (int count_old_page_tables, int count_new_page_tables)
1948 {
1949   unsigned i;
1950
1951   /* First, we swap the new entries to the front of the varrays.  */
1952   page_entry **new_by_depth;
1953   unsigned long **new_save_in_use;
1954
1955   new_by_depth = (page_entry **) xmalloc (G.by_depth_max * sizeof (page_entry *));
1956   new_save_in_use = (unsigned long **) xmalloc (G.by_depth_max * sizeof (unsigned long *));
1957
1958   memcpy (&new_by_depth[0],
1959           &G.by_depth[count_old_page_tables],
1960           count_new_page_tables * sizeof (void *));
1961   memcpy (&new_by_depth[count_new_page_tables],
1962           &G.by_depth[0],
1963           count_old_page_tables * sizeof (void *));
1964   memcpy (&new_save_in_use[0],
1965           &G.save_in_use[count_old_page_tables],
1966           count_new_page_tables * sizeof (void *));
1967   memcpy (&new_save_in_use[count_new_page_tables],
1968           &G.save_in_use[0],
1969           count_old_page_tables * sizeof (void *));
1970
1971   free (G.by_depth);
1972   free (G.save_in_use);
1973
1974   G.by_depth = new_by_depth;
1975   G.save_in_use = new_save_in_use;
1976
1977   /* Now update all the index_by_depth fields.  */
1978   for (i = G.by_depth_in_use; i > 0; --i)
1979     {
1980       page_entry *p = G.by_depth[i-1];
1981       p->index_by_depth = i-1;
1982     }
1983
1984   /* And last, we update the depth pointers in G.depth.  The first
1985      entry is already 0, and context 0 entries always start at index
1986      0, so there is nothing to update in the first slot.  We need a
1987      second slot, only if we have old ptes, and if we do, they start
1988      at index count_new_page_tables.  */
1989   if (count_old_page_tables)
1990     push_depth (count_new_page_tables);
1991 }
1992
1993 void
1994 ggc_pch_read (FILE *f, void *addr)
1995 {
1996   struct ggc_pch_ondisk d;
1997   unsigned i;
1998   char *offs = addr;
1999   unsigned long count_old_page_tables;
2000   unsigned long count_new_page_tables;
2001
2002   count_old_page_tables = G.by_depth_in_use;
2003
2004   /* We've just read in a PCH file.  So, every object that used to be
2005      allocated is now free.  */
2006   clear_marks ();
2007 #ifdef GGC_POISON
2008   poison_pages ();
2009 #endif
2010
2011   /* No object read from a PCH file should ever be freed.  So, set the
2012      context depth to 1, and set the depth of all the currently-allocated
2013      pages to be 1 too.  PCH pages will have depth 0.  */
2014   if (G.context_depth != 0)
2015     abort ();
2016   G.context_depth = 1;
2017   for (i = 0; i < NUM_ORDERS; i++)
2018     {
2019       page_entry *p;
2020       for (p = G.pages[i]; p != NULL; p = p->next)
2021         p->context_depth = G.context_depth;
2022     }
2023
2024   /* Allocate the appropriate page-table entries for the pages read from
2025      the PCH file.  */
2026   if (fread (&d, sizeof (d), 1, f) != 1)
2027     fatal_error ("can't read PCH file: %m");
2028
2029   for (i = 0; i < NUM_ORDERS; i++)
2030     {
2031       struct page_entry *entry;
2032       char *pte;
2033       size_t bytes;
2034       size_t num_objs;
2035       size_t j;
2036
2037       if (d.totals[i] == 0)
2038         continue;
2039
2040       bytes = ROUND_UP (d.totals[i] * OBJECT_SIZE (i), G.pagesize);
2041       num_objs = bytes / OBJECT_SIZE (i);
2042       entry = xcalloc (1, (sizeof (struct page_entry)
2043                            - sizeof (long)
2044                            + BITMAP_SIZE (num_objs + 1)));
2045       entry->bytes = bytes;
2046       entry->page = offs;
2047       entry->context_depth = 0;
2048       offs += bytes;
2049       entry->num_free_objects = 0;
2050       entry->order = i;
2051
2052       for (j = 0;
2053            j + HOST_BITS_PER_LONG <= num_objs + 1;
2054            j += HOST_BITS_PER_LONG)
2055         entry->in_use_p[j / HOST_BITS_PER_LONG] = -1;
2056       for (; j < num_objs + 1; j++)
2057         entry->in_use_p[j / HOST_BITS_PER_LONG]
2058           |= 1L << (j % HOST_BITS_PER_LONG);
2059
2060       for (pte = entry->page;
2061            pte < entry->page + entry->bytes;
2062            pte += G.pagesize)
2063         set_page_table_entry (pte, entry);
2064
2065       if (G.page_tails[i] != NULL)
2066         G.page_tails[i]->next = entry;
2067       else
2068         G.pages[i] = entry;
2069       G.page_tails[i] = entry;
2070
2071       /* We start off by just adding all the new information to the
2072          end of the varrays, later, we will move the new information
2073          to the front of the varrays, as the PCH page tables are at
2074          context 0.  */
2075       push_by_depth (entry, 0);
2076     }
2077
2078   /* Now, we update the various data structures that speed page table
2079      handling.  */
2080   count_new_page_tables = G.by_depth_in_use - count_old_page_tables;
2081
2082   move_ptes_to_front (count_old_page_tables, count_new_page_tables);
2083
2084   /* Update the statistics.  */
2085   G.allocated = G.allocated_last_gc = offs - (char *)addr;
2086 }