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