OSDN Git Service

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