OSDN Git Service

31c646349f38e3dd91a35f8198b3b1e7d7ca65b1
[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 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 "tree.h"
24 #include "rtl.h"
25 #include "tm_p.h"
26 #include "toplev.h"
27 #include "varray.h"
28 #include "flags.h"
29 #include "ggc.h"
30 #include "timevar.h"
31 #include "params.h"
32
33 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
34    file open.  Prefer either to valloc.  */
35 #ifdef HAVE_MMAP_ANON
36 # undef HAVE_MMAP_DEV_ZERO
37
38 # include <sys/mman.h>
39 # ifndef MAP_FAILED
40 #  define MAP_FAILED -1
41 # endif
42 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
43 #  define MAP_ANONYMOUS MAP_ANON
44 # endif
45 # define USING_MMAP
46
47 #endif
48
49 #ifdef HAVE_MMAP_DEV_ZERO
50
51 # include <sys/mman.h>
52 # ifndef MAP_FAILED
53 #  define MAP_FAILED -1
54 # endif
55 # define USING_MMAP
56
57 #endif
58
59 #ifndef USING_MMAP
60 #define USING_MALLOC_PAGE_GROUPS
61 #endif
62
63 /* Stategy:
64
65    This garbage-collecting allocator allocates objects on one of a set
66    of pages.  Each page can allocate objects of a single size only;
67    available sizes are powers of two starting at four bytes.  The size
68    of an allocation request is rounded up to the next power of two
69    (`order'), and satisfied from the appropriate page.
70
71    Each page is recorded in a page-entry, which also maintains an
72    in-use bitmap of object positions on the page.  This allows the
73    allocation state of a particular object to be flipped without
74    touching the page itself.
75
76    Each page-entry also has a context depth, which is used to track
77    pushing and popping of allocation contexts.  Only objects allocated
78    in the current (highest-numbered) context may be collected.
79
80    Page entries are arranged in an array of singly-linked lists.  The
81    array is indexed by the allocation size, in bits, of the pages on
82    it; i.e. all pages on a list allocate objects of the same size.
83    Pages are ordered on the list such that all non-full pages precede
84    all full pages, with non-full pages arranged in order of decreasing
85    context depth.
86
87    Empty pages (of all orders) are kept on a single page cache list,
88    and are considered first when new pages are required; they are
89    deallocated at the start of the next collection if they haven't
90    been recycled by then.  */
91
92 /* Define GGC_DEBUG_LEVEL to print debugging information.
93      0: No debugging output.
94      1: GC statistics only.
95      2: Page-entry allocations/deallocations as well.
96      3: Object allocations as well.
97      4: Object marks as well.  */
98 #define GGC_DEBUG_LEVEL (0)
99 \f
100 #ifndef HOST_BITS_PER_PTR
101 #define HOST_BITS_PER_PTR  HOST_BITS_PER_LONG
102 #endif
103
104 \f
105 /* A two-level tree is used to look up the page-entry for a given
106    pointer.  Two chunks of the pointer's bits are extracted to index
107    the first and second levels of the tree, as follows:
108
109                                    HOST_PAGE_SIZE_BITS
110                            32           |      |
111        msb +----------------+----+------+------+ lsb
112                             |    |      |
113                          PAGE_L1_BITS   |
114                                  |      |
115                                PAGE_L2_BITS
116
117    The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
118    pages are aligned on system page boundaries.  The next most
119    significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
120    index values in the lookup table, respectively.
121
122    For 32-bit architectures and the settings below, there are no
123    leftover bits.  For architectures with wider pointers, the lookup
124    tree points to a list of pages, which must be scanned to find the
125    correct one.  */
126
127 #define PAGE_L1_BITS    (8)
128 #define PAGE_L2_BITS    (32 - PAGE_L1_BITS - G.lg_pagesize)
129 #define PAGE_L1_SIZE    ((size_t) 1 << PAGE_L1_BITS)
130 #define PAGE_L2_SIZE    ((size_t) 1 << PAGE_L2_BITS)
131
132 #define LOOKUP_L1(p) \
133   (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
134
135 #define LOOKUP_L2(p) \
136   (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
137
138 /* The number of objects per allocation page, for objects on a page of
139    the indicated ORDER.  */
140 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
141
142 /* The size of an object on a page of the indicated ORDER.  */
143 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
144
145 /* For speed, we avoid doing a general integer divide to locate the
146    offset in the allocation bitmap, by precalculating numbers M, S
147    such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
148    within the page which is evenly divisible by the object size Z.  */
149 #define DIV_MULT(ORDER) inverse_table[ORDER].mult
150 #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
151 #define OFFSET_TO_BIT(OFFSET, ORDER) \
152   (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
153
154 /* The number of extra orders, not corresponding to power-of-two sized
155    objects.  */
156
157 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
158
159 #define RTL_SIZE(NSLOTS) \
160   (sizeof (struct rtx_def) + ((NSLOTS) - 1) * sizeof (rtunion))
161
162 /* The Ith entry is the maximum size of an object to be stored in the
163    Ith extra order.  Adding a new entry to this array is the *only*
164    thing you need to do to add a new special allocation size.  */
165
166 static const size_t extra_order_size_table[] = {
167   sizeof (struct tree_decl),
168   sizeof (struct tree_list),
169   RTL_SIZE (2),                 /* REG, MEM, PLUS, etc.  */
170   RTL_SIZE (10),                /* INSN, CALL_INSN, JUMP_INSN */
171 };
172
173 /* The total number of orders.  */
174
175 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
176
177 /* We use this structure to determine the alignment required for
178    allocations.  For power-of-two sized allocations, that's not a
179    problem, but it does matter for odd-sized allocations.  */
180
181 struct max_alignment {
182   char c;
183   union {
184     HOST_WIDEST_INT i;
185 #ifdef HAVE_LONG_DOUBLE
186     long double d;
187 #else
188     double d;
189 #endif
190   } u;
191 };
192
193 /* The biggest alignment required.  */
194
195 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
196
197 /* The Ith entry is the number of objects on a page or order I.  */
198
199 static unsigned objects_per_page_table[NUM_ORDERS];
200
201 /* The Ith entry is the size of an object on a page of order I.  */
202
203 static size_t object_size_table[NUM_ORDERS];
204
205 /* The Ith entry is a pair of numbers (mult, shift) such that
206    ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
207    for all k evenly divisible by OBJECT_SIZE(I).  */
208
209 static struct
210 {
211   unsigned int mult;
212   unsigned int shift;
213 }
214 inverse_table[NUM_ORDERS];
215
216 /* A page_entry records the status of an allocation page.  This
217    structure is dynamically sized to fit the bitmap in_use_p.  */
218 typedef struct page_entry
219 {
220   /* The next page-entry with objects of the same size, or NULL if
221      this is the last page-entry.  */
222   struct page_entry *next;
223
224   /* The number of bytes allocated.  (This will always be a multiple
225      of the host system page size.)  */
226   size_t bytes;
227
228   /* The address at which the memory is allocated.  */
229   char *page;
230
231 #ifdef USING_MALLOC_PAGE_GROUPS
232   /* Back pointer to the page group this page came from.  */
233   struct page_group *group;
234 #endif
235
236   /* Saved in-use bit vector for pages that aren't in the topmost
237      context during collection.  */
238   unsigned long *save_in_use_p;
239
240   /* Context depth of this page.  */
241   unsigned short context_depth;
242
243   /* The number of free objects remaining on this page.  */
244   unsigned short num_free_objects;
245
246   /* A likely candidate for the bit position of a free object for the
247      next allocation from this page.  */
248   unsigned short next_bit_hint;
249
250   /* The lg of size of objects allocated from this page.  */
251   unsigned char order;
252
253   /* A bit vector indicating whether or not objects are in use.  The
254      Nth bit is one if the Nth object on this page is allocated.  This
255      array is dynamically sized.  */
256   unsigned long in_use_p[1];
257 } page_entry;
258
259 #ifdef USING_MALLOC_PAGE_GROUPS
260 /* A page_group describes a large allocation from malloc, from which
261    we parcel out aligned pages.  */
262 typedef struct page_group
263 {
264   /* A linked list of all extant page groups.  */
265   struct page_group *next;
266
267   /* The address we received from malloc.  */
268   char *allocation;
269
270   /* The size of the block.  */
271   size_t alloc_size;
272
273   /* A bitmask of pages in use.  */
274   unsigned int in_use;
275 } page_group;
276 #endif
277
278 #if HOST_BITS_PER_PTR <= 32
279
280 /* On 32-bit hosts, we use a two level page table, as pictured above.  */
281 typedef page_entry **page_table[PAGE_L1_SIZE];
282
283 #else
284
285 /* On 64-bit hosts, we use the same two level page tables plus a linked
286    list that disambiguates the top 32-bits.  There will almost always be
287    exactly one entry in the list.  */
288 typedef struct page_table_chain
289 {
290   struct page_table_chain *next;
291   size_t high_bits;
292   page_entry **table[PAGE_L1_SIZE];
293 } *page_table;
294
295 #endif
296
297 /* The rest of the global variables.  */
298 static struct globals
299 {
300   /* The Nth element in this array is a page with objects of size 2^N.
301      If there are any pages with free objects, they will be at the
302      head of the list.  NULL if there are no page-entries for this
303      object size.  */
304   page_entry *pages[NUM_ORDERS];
305
306   /* The Nth element in this array is the last page with objects of
307      size 2^N.  NULL if there are no page-entries for this object
308      size.  */
309   page_entry *page_tails[NUM_ORDERS];
310
311   /* Lookup table for associating allocation pages with object addresses.  */
312   page_table lookup;
313
314   /* The system's page size.  */
315   size_t pagesize;
316   size_t lg_pagesize;
317
318   /* Bytes currently allocated.  */
319   size_t allocated;
320
321   /* Bytes currently allocated at the end of the last collection.  */
322   size_t allocated_last_gc;
323
324   /* Total amount of memory mapped.  */
325   size_t bytes_mapped;
326
327   /* The current depth in the context stack.  */
328   unsigned short context_depth;
329
330   /* A file descriptor open to /dev/zero for reading.  */
331 #if defined (HAVE_MMAP_DEV_ZERO)
332   int dev_zero_fd;
333 #endif
334
335   /* A cache of free system pages.  */
336   page_entry *free_pages;
337
338 #ifdef USING_MALLOC_PAGE_GROUPS
339   page_group *page_groups;
340 #endif
341
342   /* The file descriptor for debugging output.  */
343   FILE *debug_file;
344 } G;
345
346 /* The size in bytes required to maintain a bitmap for the objects
347    on a page-entry.  */
348 #define BITMAP_SIZE(Num_objects) \
349   (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
350
351 /* Allocate pages in chunks of this size, to throttle calls to memory
352    allocation routines.  The first page is used, the rest go onto the
353    free list.  This cannot be larger than HOST_BITS_PER_INT for the
354    in_use bitmask for page_group.  */
355 #define GGC_QUIRE_SIZE 16
356 \f
357 static int ggc_allocated_p PARAMS ((const void *));
358 static page_entry *lookup_page_table_entry PARAMS ((const void *));
359 static void set_page_table_entry PARAMS ((void *, page_entry *));
360 #ifdef USING_MMAP
361 static char *alloc_anon PARAMS ((char *, size_t));
362 #endif
363 #ifdef USING_MALLOC_PAGE_GROUPS
364 static size_t page_group_index PARAMS ((char *, char *));
365 static void set_page_group_in_use PARAMS ((page_group *, char *));
366 static void clear_page_group_in_use PARAMS ((page_group *, char *));
367 #endif
368 static struct page_entry * alloc_page PARAMS ((unsigned));
369 static void free_page PARAMS ((struct page_entry *));
370 static void release_pages PARAMS ((void));
371 static void clear_marks PARAMS ((void));
372 static void sweep_pages PARAMS ((void));
373 static void ggc_recalculate_in_use_p PARAMS ((page_entry *));
374 static void compute_inverse PARAMS ((unsigned));
375
376 #ifdef ENABLE_GC_CHECKING
377 static void poison_pages PARAMS ((void));
378 #endif
379
380 void debug_print_page_list PARAMS ((int));
381 \f
382 /* Returns nonzero if P was allocated in GC'able memory.  */
383
384 static inline int
385 ggc_allocated_p (p)
386      const void *p;
387 {
388   page_entry ***base;
389   size_t L1, L2;
390
391 #if HOST_BITS_PER_PTR <= 32
392   base = &G.lookup[0];
393 #else
394   page_table table = G.lookup;
395   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
396   while (1)
397     {
398       if (table == NULL)
399         return 0;
400       if (table->high_bits == high_bits)
401         break;
402       table = table->next;
403     }
404   base = &table->table[0];
405 #endif
406
407   /* Extract the level 1 and 2 indices.  */
408   L1 = LOOKUP_L1 (p);
409   L2 = LOOKUP_L2 (p);
410
411   return base[L1] && base[L1][L2];
412 }
413
414 /* Traverse the page table and find the entry for a page.
415    Die (probably) if the object wasn't allocated via GC.  */
416
417 static inline page_entry *
418 lookup_page_table_entry(p)
419      const void *p;
420 {
421   page_entry ***base;
422   size_t L1, L2;
423
424 #if HOST_BITS_PER_PTR <= 32
425   base = &G.lookup[0];
426 #else
427   page_table table = G.lookup;
428   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
429   while (table->high_bits != high_bits)
430     table = table->next;
431   base = &table->table[0];
432 #endif
433
434   /* Extract the level 1 and 2 indices.  */
435   L1 = LOOKUP_L1 (p);
436   L2 = LOOKUP_L2 (p);
437
438   return base[L1][L2];
439 }
440
441 /* Set the page table entry for a page.  */
442
443 static void
444 set_page_table_entry(p, entry)
445      void *p;
446      page_entry *entry;
447 {
448   page_entry ***base;
449   size_t L1, L2;
450
451 #if HOST_BITS_PER_PTR <= 32
452   base = &G.lookup[0];
453 #else
454   page_table table;
455   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
456   for (table = G.lookup; table; table = table->next)
457     if (table->high_bits == high_bits)
458       goto found;
459
460   /* Not found -- allocate a new table.  */
461   table = (page_table) xcalloc (1, sizeof(*table));
462   table->next = G.lookup;
463   table->high_bits = high_bits;
464   G.lookup = table;
465 found:
466   base = &table->table[0];
467 #endif
468
469   /* Extract the level 1 and 2 indices.  */
470   L1 = LOOKUP_L1 (p);
471   L2 = LOOKUP_L2 (p);
472
473   if (base[L1] == NULL)
474     base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
475
476   base[L1][L2] = entry;
477 }
478
479 /* Prints the page-entry for object size ORDER, for debugging.  */
480
481 void
482 debug_print_page_list (order)
483      int order;
484 {
485   page_entry *p;
486   printf ("Head=%p, Tail=%p:\n", (PTR) G.pages[order],
487           (PTR) G.page_tails[order]);
488   p = G.pages[order];
489   while (p != NULL)
490     {
491       printf ("%p(%1d|%3d) -> ", (PTR) p, p->context_depth,
492               p->num_free_objects);
493       p = p->next;
494     }
495   printf ("NULL\n");
496   fflush (stdout);
497 }
498
499 #ifdef USING_MMAP
500 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
501    (if non-null).  The ifdef structure here is intended to cause a
502    compile error unless exactly one of the HAVE_* is defined.  */
503
504 static inline char *
505 alloc_anon (pref, size)
506      char *pref ATTRIBUTE_UNUSED;
507      size_t size;
508 {
509 #ifdef HAVE_MMAP_ANON
510   char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
511                               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
512 #endif
513 #ifdef HAVE_MMAP_DEV_ZERO
514   char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
515                               MAP_PRIVATE, G.dev_zero_fd, 0);
516 #endif
517
518   if (page == (char *) MAP_FAILED)
519     {
520       perror ("virtual memory exhausted");
521       exit (FATAL_EXIT_CODE);
522     }
523
524   /* Remember that we allocated this memory.  */
525   G.bytes_mapped += size;
526
527   return page;
528 }
529 #endif
530 #ifdef USING_MALLOC_PAGE_GROUPS
531 /* Compute the index for this page into the page group.  */
532
533 static inline size_t
534 page_group_index (allocation, page)
535      char *allocation, *page;
536 {
537   return (size_t) (page - allocation) >> G.lg_pagesize;
538 }
539
540 /* Set and clear the in_use bit for this page in the page group.  */
541
542 static inline void
543 set_page_group_in_use (group, page)
544      page_group *group;
545      char *page;
546 {
547   group->in_use |= 1 << page_group_index (group->allocation, page);
548 }
549
550 static inline void
551 clear_page_group_in_use (group, page)
552      page_group *group;
553      char *page;
554 {
555   group->in_use &= ~(1 << page_group_index (group->allocation, page));
556 }
557 #endif
558
559 /* Allocate a new page for allocating objects of size 2^ORDER,
560    and return an entry for it.  The entry is not added to the
561    appropriate page_table list.  */
562
563 static inline struct page_entry *
564 alloc_page (order)
565      unsigned order;
566 {
567   struct page_entry *entry, *p, **pp;
568   char *page;
569   size_t num_objects;
570   size_t bitmap_size;
571   size_t page_entry_size;
572   size_t entry_size;
573 #ifdef USING_MALLOC_PAGE_GROUPS
574   page_group *group;
575 #endif
576
577   num_objects = OBJECTS_PER_PAGE (order);
578   bitmap_size = BITMAP_SIZE (num_objects + 1);
579   page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
580   entry_size = num_objects * OBJECT_SIZE (order);
581   if (entry_size < G.pagesize)
582     entry_size = G.pagesize;
583
584   entry = NULL;
585   page = NULL;
586
587   /* Check the list of free pages for one we can use.  */
588   for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
589     if (p->bytes == entry_size)
590       break;
591
592   if (p != NULL)
593     {
594       /* Recycle the allocated memory from this page ...  */
595       *pp = p->next;
596       page = p->page;
597
598 #ifdef USING_MALLOC_PAGE_GROUPS
599       group = p->group;
600 #endif
601
602       /* ... and, if possible, the page entry itself.  */
603       if (p->order == order)
604         {
605           entry = p;
606           memset (entry, 0, page_entry_size);
607         }
608       else
609         free (p);
610     }
611 #ifdef USING_MMAP
612   else if (entry_size == G.pagesize)
613     {
614       /* We want just one page.  Allocate a bunch of them and put the
615          extras on the freelist.  (Can only do this optimization with
616          mmap for backing store.)  */
617       struct page_entry *e, *f = G.free_pages;
618       int i;
619
620       page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE);
621
622       /* This loop counts down so that the chain will be in ascending
623          memory order.  */
624       for (i = GGC_QUIRE_SIZE - 1; i >= 1; i--)
625         {
626           e = (struct page_entry *) xcalloc (1, page_entry_size);
627           e->order = order;
628           e->bytes = G.pagesize;
629           e->page = page + (i << G.lg_pagesize);
630           e->next = f;
631           f = e;
632         }
633
634       G.free_pages = f;
635     }
636   else
637     page = alloc_anon (NULL, entry_size);
638 #endif
639 #ifdef USING_MALLOC_PAGE_GROUPS
640   else
641     {
642       /* Allocate a large block of memory and serve out the aligned
643          pages therein.  This results in much less memory wastage
644          than the traditional implementation of valloc.  */
645
646       char *allocation, *a, *enda;
647       size_t alloc_size, head_slop, tail_slop;
648       int multiple_pages = (entry_size == G.pagesize);
649
650       if (multiple_pages)
651         alloc_size = GGC_QUIRE_SIZE * G.pagesize;
652       else
653         alloc_size = entry_size + G.pagesize - 1;
654       allocation = xmalloc (alloc_size);
655
656       page = (char *) (((size_t) allocation + G.pagesize - 1) & -G.pagesize);
657       head_slop = page - allocation;
658       if (multiple_pages)
659         tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
660       else
661         tail_slop = alloc_size - entry_size - head_slop;
662       enda = allocation + alloc_size - tail_slop;
663
664       /* We allocated N pages, which are likely not aligned, leaving
665          us with N-1 usable pages.  We plan to place the page_group
666          structure somewhere in the slop.  */
667       if (head_slop >= sizeof (page_group))
668         group = (page_group *)page - 1;
669       else
670         {
671           /* We magically got an aligned allocation.  Too bad, we have
672              to waste a page anyway.  */
673           if (tail_slop == 0)
674             {
675               enda -= G.pagesize;
676               tail_slop += G.pagesize;
677             }
678           if (tail_slop < sizeof (page_group))
679             abort ();
680           group = (page_group *)enda;
681           tail_slop -= sizeof (page_group);
682         }
683
684       /* Remember that we allocated this memory.  */
685       group->next = G.page_groups;
686       group->allocation = allocation;
687       group->alloc_size = alloc_size;
688       group->in_use = 0;
689       G.page_groups = group;
690       G.bytes_mapped += alloc_size;
691
692       /* If we allocated multiple pages, put the rest on the free list.  */
693       if (multiple_pages)
694         {
695           struct page_entry *e, *f = G.free_pages;
696           for (a = enda - G.pagesize; a != page; a -= G.pagesize)
697             {
698               e = (struct page_entry *) xcalloc (1, page_entry_size);
699               e->order = order;
700               e->bytes = G.pagesize;
701               e->page = a;
702               e->group = group;
703               e->next = f;
704               f = e;
705             }
706           G.free_pages = f;
707         }
708     }
709 #endif
710
711   if (entry == NULL)
712     entry = (struct page_entry *) xcalloc (1, page_entry_size);
713
714   entry->bytes = entry_size;
715   entry->page = page;
716   entry->context_depth = G.context_depth;
717   entry->order = order;
718   entry->num_free_objects = num_objects;
719   entry->next_bit_hint = 1;
720
721 #ifdef USING_MALLOC_PAGE_GROUPS
722   entry->group = group;
723   set_page_group_in_use (group, page);
724 #endif
725
726   /* Set the one-past-the-end in-use bit.  This acts as a sentry as we
727      increment the hint.  */
728   entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
729     = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
730
731   set_page_table_entry (page, entry);
732
733   if (GGC_DEBUG_LEVEL >= 2)
734     fprintf (G.debug_file,
735              "Allocating page at %p, object size=%lu, data %p-%p\n",
736              (PTR) entry, (unsigned long) OBJECT_SIZE (order), page,
737              page + entry_size - 1);
738
739   return entry;
740 }
741
742 /* For a page that is no longer needed, put it on the free page list.  */
743
744 static inline void
745 free_page (entry)
746      page_entry *entry;
747 {
748   if (GGC_DEBUG_LEVEL >= 2)
749     fprintf (G.debug_file,
750              "Deallocating page at %p, data %p-%p\n", (PTR) entry,
751              entry->page, entry->page + entry->bytes - 1);
752
753   set_page_table_entry (entry->page, NULL);
754
755 #ifdef USING_MALLOC_PAGE_GROUPS
756   clear_page_group_in_use (entry->group, entry->page);
757 #endif
758
759   entry->next = G.free_pages;
760   G.free_pages = entry;
761 }
762
763 /* Release the free page cache to the system.  */
764
765 static void
766 release_pages ()
767 {
768 #ifdef USING_MMAP
769   page_entry *p, *next;
770   char *start;
771   size_t len;
772
773   /* Gather up adjacent pages so they are unmapped together.  */
774   p = G.free_pages;
775
776   while (p)
777     {
778       start = p->page;
779       next = p->next;
780       len = p->bytes;
781       free (p);
782       p = next;
783
784       while (p && p->page == start + len)
785         {
786           next = p->next;
787           len += p->bytes;
788           free (p);
789           p = next;
790         }
791
792       munmap (start, len);
793       G.bytes_mapped -= len;
794     }
795
796   G.free_pages = NULL;
797 #endif
798 #ifdef USING_MALLOC_PAGE_GROUPS
799   page_entry **pp, *p;
800   page_group **gp, *g;
801
802   /* Remove all pages from free page groups from the list.  */
803   pp = &G.free_pages;
804   while ((p = *pp) != NULL)
805     if (p->group->in_use == 0)
806       {
807         *pp = p->next;
808         free (p);
809       }
810     else
811       pp = &p->next;
812
813   /* Remove all free page groups, and release the storage.  */
814   gp = &G.page_groups;
815   while ((g = *gp) != NULL)
816     if (g->in_use == 0)
817       {
818         *gp = g->next;
819         G.bytes_mapped -= g->alloc_size;
820         free (g->allocation);
821       }
822     else
823       gp = &g->next;
824 #endif
825 }
826
827 /* This table provides a fast way to determine ceil(log_2(size)) for
828    allocation requests.  The minimum allocation size is eight bytes.  */
829
830 static unsigned char size_lookup[257] =
831 {
832   3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
833   4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
834   5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
835   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
836   6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
837   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
838   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
839   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
840   7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
841   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
842   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
843   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
844   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
845   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
846   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
847   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
848   8
849 };
850
851 /* Allocate a chunk of memory of SIZE bytes.  If ZERO is nonzero, the
852    memory is zeroed; otherwise, its contents are undefined.  */
853
854 void *
855 ggc_alloc (size)
856      size_t size;
857 {
858   unsigned order, word, bit, object_offset;
859   struct page_entry *entry;
860   void *result;
861
862   if (size <= 256)
863     order = size_lookup[size];
864   else
865     {
866       order = 9;
867       while (size > OBJECT_SIZE (order))
868         order++;
869     }
870
871   /* If there are non-full pages for this size allocation, they are at
872      the head of the list.  */
873   entry = G.pages[order];
874
875   /* If there is no page for this object size, or all pages in this
876      context are full, allocate a new page.  */
877   if (entry == NULL || entry->num_free_objects == 0)
878     {
879       struct page_entry *new_entry;
880       new_entry = alloc_page (order);
881
882       /* If this is the only entry, it's also the tail.  */
883       if (entry == NULL)
884         G.page_tails[order] = new_entry;
885
886       /* Put new pages at the head of the page list.  */
887       new_entry->next = entry;
888       entry = new_entry;
889       G.pages[order] = new_entry;
890
891       /* For a new page, we know the word and bit positions (in the
892          in_use bitmap) of the first available object -- they're zero.  */
893       new_entry->next_bit_hint = 1;
894       word = 0;
895       bit = 0;
896       object_offset = 0;
897     }
898   else
899     {
900       /* First try to use the hint left from the previous allocation
901          to locate a clear bit in the in-use bitmap.  We've made sure
902          that the one-past-the-end bit is always set, so if the hint
903          has run over, this test will fail.  */
904       unsigned hint = entry->next_bit_hint;
905       word = hint / HOST_BITS_PER_LONG;
906       bit = hint % HOST_BITS_PER_LONG;
907
908       /* If the hint didn't work, scan the bitmap from the beginning.  */
909       if ((entry->in_use_p[word] >> bit) & 1)
910         {
911           word = bit = 0;
912           while (~entry->in_use_p[word] == 0)
913             ++word;
914           while ((entry->in_use_p[word] >> bit) & 1)
915             ++bit;
916           hint = word * HOST_BITS_PER_LONG + bit;
917         }
918
919       /* Next time, try the next bit.  */
920       entry->next_bit_hint = hint + 1;
921
922       object_offset = hint * OBJECT_SIZE (order);
923     }
924
925   /* Set the in-use bit.  */
926   entry->in_use_p[word] |= ((unsigned long) 1 << bit);
927
928   /* Keep a running total of the number of free objects.  If this page
929      fills up, we may have to move it to the end of the list if the
930      next page isn't full.  If the next page is full, all subsequent
931      pages are full, so there's no need to move it.  */
932   if (--entry->num_free_objects == 0
933       && entry->next != NULL
934       && entry->next->num_free_objects > 0)
935     {
936       G.pages[order] = entry->next;
937       entry->next = NULL;
938       G.page_tails[order]->next = entry;
939       G.page_tails[order] = entry;
940     }
941
942   /* Calculate the object's address.  */
943   result = entry->page + object_offset;
944
945 #ifdef ENABLE_GC_CHECKING
946   /* `Poison' the entire allocated object, including any padding at
947      the end.  */
948   memset (result, 0xaf, OBJECT_SIZE (order));
949 #endif
950
951   /* Keep track of how many bytes are being allocated.  This
952      information is used in deciding when to collect.  */
953   G.allocated += OBJECT_SIZE (order);
954
955   if (GGC_DEBUG_LEVEL >= 3)
956     fprintf (G.debug_file,
957              "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
958              (unsigned long) size, (unsigned long) OBJECT_SIZE (order), result,
959              (PTR) entry);
960
961   return result;
962 }
963
964 /* If P is not marked, marks it and return false.  Otherwise return true.
965    P must have been allocated by the GC allocator; it mustn't point to
966    static objects, stack variables, or memory allocated with malloc.  */
967
968 int
969 ggc_set_mark (p)
970      const void *p;
971 {
972   page_entry *entry;
973   unsigned bit, word;
974   unsigned long mask;
975
976   /* Look up the page on which the object is alloced.  If the object
977      wasn't allocated by the collector, we'll probably die.  */
978   entry = lookup_page_table_entry (p);
979 #ifdef ENABLE_CHECKING
980   if (entry == NULL)
981     abort ();
982 #endif
983
984   /* Calculate the index of the object on the page; this is its bit
985      position in the in_use_p bitmap.  */
986   bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
987   word = bit / HOST_BITS_PER_LONG;
988   mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
989
990   /* If the bit was previously set, skip it.  */
991   if (entry->in_use_p[word] & mask)
992     return 1;
993
994   /* Otherwise set it, and decrement the free object count.  */
995   entry->in_use_p[word] |= mask;
996   entry->num_free_objects -= 1;
997
998   if (GGC_DEBUG_LEVEL >= 4)
999     fprintf (G.debug_file, "Marking %p\n", p);
1000
1001   return 0;
1002 }
1003
1004 /* Return 1 if P has been marked, zero otherwise.
1005    P must have been allocated by the GC allocator; it mustn't point to
1006    static objects, stack variables, or memory allocated with malloc.  */
1007
1008 int
1009 ggc_marked_p (p)
1010      const void *p;
1011 {
1012   page_entry *entry;
1013   unsigned bit, word;
1014   unsigned long mask;
1015
1016   /* Look up the page on which the object is alloced.  If the object
1017      wasn't allocated by the collector, we'll probably die.  */
1018   entry = lookup_page_table_entry (p);
1019 #ifdef ENABLE_CHECKING
1020   if (entry == NULL)
1021     abort ();
1022 #endif
1023
1024   /* Calculate the index of the object on the page; this is its bit
1025      position in the in_use_p bitmap.  */
1026   bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1027   word = bit / HOST_BITS_PER_LONG;
1028   mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1029
1030   return (entry->in_use_p[word] & mask) != 0;
1031 }
1032
1033 /* Return the size of the gc-able object P.  */
1034
1035 size_t
1036 ggc_get_size (p)
1037      const void *p;
1038 {
1039   page_entry *pe = lookup_page_table_entry (p);
1040   return OBJECT_SIZE (pe->order);
1041 }
1042 \f
1043 /* Subroutine of init_ggc which computes the pair of numbers used to
1044    perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1045
1046    This algorithm is taken from Granlund and Montgomery's paper
1047    "Division by Invariant Integers using Multiplication"
1048    (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1049    constants).  */
1050
1051 static void
1052 compute_inverse (order)
1053      unsigned order;
1054 {
1055   unsigned size, inv, e;
1056
1057   /* There can be only one object per "page" in a bucket for sizes
1058      larger than half a machine page; it will always have offset zero.  */
1059   if (OBJECT_SIZE (order) > G.pagesize/2)
1060     {
1061       if (OBJECTS_PER_PAGE (order) != 1)
1062         abort ();
1063
1064       DIV_MULT (order) = 1;
1065       DIV_SHIFT (order) = 0;
1066       return;
1067     }
1068
1069   size = OBJECT_SIZE (order);
1070   e = 0;
1071   while (size % 2 == 0)
1072     {
1073       e++;
1074       size >>= 1;
1075     }
1076
1077   inv = size;
1078   while (inv * size != 1)
1079     inv = inv * (2 - inv*size);
1080
1081   DIV_MULT (order) = inv;
1082   DIV_SHIFT (order) = e;
1083 }
1084
1085 /* Initialize the ggc-mmap allocator.  */
1086 void
1087 init_ggc ()
1088 {
1089   unsigned order;
1090
1091   G.pagesize = getpagesize();
1092   G.lg_pagesize = exact_log2 (G.pagesize);
1093
1094 #ifdef HAVE_MMAP_DEV_ZERO
1095   G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1096   if (G.dev_zero_fd == -1)
1097     abort ();
1098 #endif
1099
1100 #if 0
1101   G.debug_file = fopen ("ggc-mmap.debug", "w");
1102 #else
1103   G.debug_file = stdout;
1104 #endif
1105
1106 #ifdef USING_MMAP
1107   /* StunOS has an amazing off-by-one error for the first mmap allocation
1108      after fiddling with RLIMIT_STACK.  The result, as hard as it is to
1109      believe, is an unaligned page allocation, which would cause us to
1110      hork badly if we tried to use it.  */
1111   {
1112     char *p = alloc_anon (NULL, G.pagesize);
1113     struct page_entry *e;
1114     if ((size_t)p & (G.pagesize - 1))
1115       {
1116         /* How losing.  Discard this one and try another.  If we still
1117            can't get something useful, give up.  */
1118
1119         p = alloc_anon (NULL, G.pagesize);
1120         if ((size_t)p & (G.pagesize - 1))
1121           abort ();
1122       }
1123
1124     /* We have a good page, might as well hold onto it...  */
1125     e = (struct page_entry *) xcalloc (1, sizeof (struct page_entry));
1126     e->bytes = G.pagesize;
1127     e->page = p;
1128     e->next = G.free_pages;
1129     G.free_pages = e;
1130   }
1131 #endif
1132
1133   /* Initialize the object size table.  */
1134   for (order = 0; order < HOST_BITS_PER_PTR; ++order)
1135     object_size_table[order] = (size_t) 1 << order;
1136   for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1137     {
1138       size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
1139
1140       /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1141          so that we're sure of getting aligned memory.  */
1142       s = CEIL (s, MAX_ALIGNMENT) * MAX_ALIGNMENT;
1143       object_size_table[order] = s;
1144     }
1145
1146   /* Initialize the objects-per-page and inverse tables.  */
1147   for (order = 0; order < NUM_ORDERS; ++order)
1148     {
1149       objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
1150       if (objects_per_page_table[order] == 0)
1151         objects_per_page_table[order] = 1;
1152       compute_inverse (order);
1153     }
1154
1155   /* Reset the size_lookup array to put appropriately sized objects in
1156      the special orders.  All objects bigger than the previous power
1157      of two, but no greater than the special size, should go in the
1158      new order.  */
1159   for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1160     {
1161       int o;
1162       int i;
1163
1164       o = size_lookup[OBJECT_SIZE (order)];
1165       for (i = OBJECT_SIZE (order); size_lookup [i] == o; --i)
1166         size_lookup[i] = order;
1167     }
1168 }
1169
1170 /* Increment the `GC context'.  Objects allocated in an outer context
1171    are never freed, eliminating the need to register their roots.  */
1172
1173 void
1174 ggc_push_context ()
1175 {
1176   ++G.context_depth;
1177
1178   /* Die on wrap.  */
1179   if (G.context_depth == 0)
1180     abort ();
1181 }
1182
1183 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1184    reflects reality.  Recalculate NUM_FREE_OBJECTS as well.  */
1185
1186 static void
1187 ggc_recalculate_in_use_p (p)
1188      page_entry *p;
1189 {
1190   unsigned int i;
1191   size_t num_objects;
1192
1193   /* Because the past-the-end bit in in_use_p is always set, we
1194      pretend there is one additional object.  */
1195   num_objects = OBJECTS_PER_PAGE (p->order) + 1;
1196
1197   /* Reset the free object count.  */
1198   p->num_free_objects = num_objects;
1199
1200   /* Combine the IN_USE_P and SAVE_IN_USE_P arrays.  */
1201   for (i = 0;
1202        i < CEIL (BITMAP_SIZE (num_objects),
1203                  sizeof (*p->in_use_p));
1204        ++i)
1205     {
1206       unsigned long j;
1207
1208       /* Something is in use if it is marked, or if it was in use in a
1209          context further down the context stack.  */
1210       p->in_use_p[i] |= p->save_in_use_p[i];
1211
1212       /* Decrement the free object count for every object allocated.  */
1213       for (j = p->in_use_p[i]; j; j >>= 1)
1214         p->num_free_objects -= (j & 1);
1215     }
1216
1217   if (p->num_free_objects >= num_objects)
1218     abort ();
1219 }
1220
1221 /* Decrement the `GC context'.  All objects allocated since the
1222    previous ggc_push_context are migrated to the outer context.  */
1223
1224 void
1225 ggc_pop_context ()
1226 {
1227   unsigned order, depth;
1228
1229   depth = --G.context_depth;
1230
1231   /* Any remaining pages in the popped context are lowered to the new
1232      current context; i.e. objects allocated in the popped context and
1233      left over are imported into the previous context.  */
1234   for (order = 2; order < NUM_ORDERS; order++)
1235     {
1236       page_entry *p;
1237
1238       for (p = G.pages[order]; p != NULL; p = p->next)
1239         {
1240           if (p->context_depth > depth)
1241             p->context_depth = depth;
1242
1243           /* If this page is now in the topmost context, and we'd
1244              saved its allocation state, restore it.  */
1245           else if (p->context_depth == depth && p->save_in_use_p)
1246             {
1247               ggc_recalculate_in_use_p (p);
1248               free (p->save_in_use_p);
1249               p->save_in_use_p = 0;
1250             }
1251         }
1252     }
1253 }
1254 \f
1255 /* Unmark all objects.  */
1256
1257 static inline void
1258 clear_marks ()
1259 {
1260   unsigned order;
1261
1262   for (order = 2; order < NUM_ORDERS; order++)
1263     {
1264       size_t num_objects = OBJECTS_PER_PAGE (order);
1265       size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
1266       page_entry *p;
1267
1268       for (p = G.pages[order]; p != NULL; p = p->next)
1269         {
1270 #ifdef ENABLE_CHECKING
1271           /* The data should be page-aligned.  */
1272           if ((size_t) p->page & (G.pagesize - 1))
1273             abort ();
1274 #endif
1275
1276           /* Pages that aren't in the topmost context are not collected;
1277              nevertheless, we need their in-use bit vectors to store GC
1278              marks.  So, back them up first.  */
1279           if (p->context_depth < G.context_depth)
1280             {
1281               if (! p->save_in_use_p)
1282                 p->save_in_use_p = xmalloc (bitmap_size);
1283               memcpy (p->save_in_use_p, p->in_use_p, bitmap_size);
1284             }
1285
1286           /* Reset reset the number of free objects and clear the
1287              in-use bits.  These will be adjusted by mark_obj.  */
1288           p->num_free_objects = num_objects;
1289           memset (p->in_use_p, 0, bitmap_size);
1290
1291           /* Make sure the one-past-the-end bit is always set.  */
1292           p->in_use_p[num_objects / HOST_BITS_PER_LONG]
1293             = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
1294         }
1295     }
1296 }
1297
1298 /* Free all empty pages.  Partially empty pages need no attention
1299    because the `mark' bit doubles as an `unused' bit.  */
1300
1301 static inline void
1302 sweep_pages ()
1303 {
1304   unsigned order;
1305
1306   for (order = 2; order < NUM_ORDERS; order++)
1307     {
1308       /* The last page-entry to consider, regardless of entries
1309          placed at the end of the list.  */
1310       page_entry * const last = G.page_tails[order];
1311
1312       size_t num_objects = OBJECTS_PER_PAGE (order);
1313       size_t live_objects;
1314       page_entry *p, *previous;
1315       int done;
1316
1317       p = G.pages[order];
1318       if (p == NULL)
1319         continue;
1320
1321       previous = NULL;
1322       do
1323         {
1324           page_entry *next = p->next;
1325
1326           /* Loop until all entries have been examined.  */
1327           done = (p == last);
1328
1329           /* Add all live objects on this page to the count of
1330              allocated memory.  */
1331           live_objects = num_objects - p->num_free_objects;
1332
1333           G.allocated += OBJECT_SIZE (order) * live_objects;
1334
1335           /* Only objects on pages in the topmost context should get
1336              collected.  */
1337           if (p->context_depth < G.context_depth)
1338             ;
1339
1340           /* Remove the page if it's empty.  */
1341           else if (live_objects == 0)
1342             {
1343               if (! previous)
1344                 G.pages[order] = next;
1345               else
1346                 previous->next = next;
1347
1348               /* Are we removing the last element?  */
1349               if (p == G.page_tails[order])
1350                 G.page_tails[order] = previous;
1351               free_page (p);
1352               p = previous;
1353             }
1354
1355           /* If the page is full, move it to the end.  */
1356           else if (p->num_free_objects == 0)
1357             {
1358               /* Don't move it if it's already at the end.  */
1359               if (p != G.page_tails[order])
1360                 {
1361                   /* Move p to the end of the list.  */
1362                   p->next = NULL;
1363                   G.page_tails[order]->next = p;
1364
1365                   /* Update the tail pointer...  */
1366                   G.page_tails[order] = p;
1367
1368                   /* ... and the head pointer, if necessary.  */
1369                   if (! previous)
1370                     G.pages[order] = next;
1371                   else
1372                     previous->next = next;
1373                   p = previous;
1374                 }
1375             }
1376
1377           /* If we've fallen through to here, it's a page in the
1378              topmost context that is neither full nor empty.  Such a
1379              page must precede pages at lesser context depth in the
1380              list, so move it to the head.  */
1381           else if (p != G.pages[order])
1382             {
1383               previous->next = p->next;
1384               p->next = G.pages[order];
1385               G.pages[order] = p;
1386               /* Are we moving the last element?  */
1387               if (G.page_tails[order] == p)
1388                 G.page_tails[order] = previous;
1389               p = previous;
1390             }
1391
1392           previous = p;
1393           p = next;
1394         }
1395       while (! done);
1396
1397       /* Now, restore the in_use_p vectors for any pages from contexts
1398          other than the current one.  */
1399       for (p = G.pages[order]; p; p = p->next)
1400         if (p->context_depth != G.context_depth)
1401           ggc_recalculate_in_use_p (p);
1402     }
1403 }
1404
1405 #ifdef ENABLE_GC_CHECKING
1406 /* Clobber all free objects.  */
1407
1408 static inline void
1409 poison_pages ()
1410 {
1411   unsigned order;
1412
1413   for (order = 2; order < NUM_ORDERS; order++)
1414     {
1415       size_t num_objects = OBJECTS_PER_PAGE (order);
1416       size_t size = OBJECT_SIZE (order);
1417       page_entry *p;
1418
1419       for (p = G.pages[order]; p != NULL; p = p->next)
1420         {
1421           size_t i;
1422
1423           if (p->context_depth != G.context_depth)
1424             /* Since we don't do any collection for pages in pushed
1425                contexts, there's no need to do any poisoning.  And
1426                besides, the IN_USE_P array isn't valid until we pop
1427                contexts.  */
1428             continue;
1429
1430           for (i = 0; i < num_objects; i++)
1431             {
1432               size_t word, bit;
1433               word = i / HOST_BITS_PER_LONG;
1434               bit = i % HOST_BITS_PER_LONG;
1435               if (((p->in_use_p[word] >> bit) & 1) == 0)
1436                 memset (p->page + i * size, 0xa5, size);
1437             }
1438         }
1439     }
1440 }
1441 #endif
1442
1443 /* Top level mark-and-sweep routine.  */
1444
1445 void
1446 ggc_collect ()
1447 {
1448   /* Avoid frequent unnecessary work by skipping collection if the
1449      total allocations haven't expanded much since the last
1450      collection.  */
1451   size_t allocated_last_gc =
1452     MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
1453
1454   size_t min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
1455
1456   if (G.allocated < allocated_last_gc + min_expand)
1457     return;
1458
1459   timevar_push (TV_GC);
1460   if (!quiet_flag)
1461     fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
1462
1463   /* Zero the total allocated bytes.  This will be recalculated in the
1464      sweep phase.  */
1465   G.allocated = 0;
1466
1467   /* Release the pages we freed the last time we collected, but didn't
1468      reuse in the interim.  */
1469   release_pages ();
1470
1471   clear_marks ();
1472   ggc_mark_roots ();
1473
1474 #ifdef ENABLE_GC_CHECKING
1475   poison_pages ();
1476 #endif
1477
1478   sweep_pages ();
1479
1480   G.allocated_last_gc = G.allocated;
1481
1482   timevar_pop (TV_GC);
1483
1484   if (!quiet_flag)
1485     fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
1486 }
1487
1488 /* Print allocation statistics.  */
1489 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1490                   ? (x) \
1491                   : ((x) < 1024*1024*10 \
1492                      ? (x) / 1024 \
1493                      : (x) / (1024*1024))))
1494 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1495
1496 void
1497 ggc_print_statistics ()
1498 {
1499   struct ggc_statistics stats;
1500   unsigned int i;
1501   size_t total_overhead = 0;
1502
1503   /* Clear the statistics.  */
1504   memset (&stats, 0, sizeof (stats));
1505
1506   /* Make sure collection will really occur.  */
1507   G.allocated_last_gc = 0;
1508
1509   /* Collect and print the statistics common across collectors.  */
1510   ggc_print_common_statistics (stderr, &stats);
1511
1512   /* Release free pages so that we will not count the bytes allocated
1513      there as part of the total allocated memory.  */
1514   release_pages ();
1515
1516   /* Collect some information about the various sizes of
1517      allocation.  */
1518   fprintf (stderr, "\n%-5s %10s  %10s  %10s\n",
1519            "Size", "Allocated", "Used", "Overhead");
1520   for (i = 0; i < NUM_ORDERS; ++i)
1521     {
1522       page_entry *p;
1523       size_t allocated;
1524       size_t in_use;
1525       size_t overhead;
1526
1527       /* Skip empty entries.  */
1528       if (!G.pages[i])
1529         continue;
1530
1531       overhead = allocated = in_use = 0;
1532
1533       /* Figure out the total number of bytes allocated for objects of
1534          this size, and how many of them are actually in use.  Also figure
1535          out how much memory the page table is using.  */
1536       for (p = G.pages[i]; p; p = p->next)
1537         {
1538           allocated += p->bytes;
1539           in_use +=
1540             (OBJECTS_PER_PAGE (i) - p->num_free_objects) * OBJECT_SIZE (i);
1541
1542           overhead += (sizeof (page_entry) - sizeof (long)
1543                        + BITMAP_SIZE (OBJECTS_PER_PAGE (i) + 1));
1544         }
1545       fprintf (stderr, "%-5lu %10lu%c %10lu%c %10lu%c\n",
1546                (unsigned long) OBJECT_SIZE (i),
1547                SCALE (allocated), LABEL (allocated),
1548                SCALE (in_use), LABEL (in_use),
1549                SCALE (overhead), LABEL (overhead));
1550       total_overhead += overhead;
1551     }
1552   fprintf (stderr, "%-5s %10lu%c %10lu%c %10lu%c\n", "Total",
1553            SCALE (G.bytes_mapped), LABEL (G.bytes_mapped),
1554            SCALE (G.allocated), LABEL(G.allocated),
1555            SCALE (total_overhead), LABEL (total_overhead));
1556 }