OSDN Git Service

hangeLog:
[pf3gnuchains/gcc-fork.git] / gcc / ggc-page.c
1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2    Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 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
32 #ifdef HAVE_MMAP_ANYWHERE
33 #include <sys/mman.h>
34 #endif
35
36 #ifndef MAP_FAILED
37 #define MAP_FAILED -1
38 #endif
39
40 #if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
41 #define MAP_ANONYMOUS MAP_ANON
42 #endif
43
44 /* Stategy: 
45
46    This garbage-collecting allocator allocates objects on one of a set
47    of pages.  Each page can allocate objects of a single size only;
48    available sizes are powers of two starting at four bytes.  The size
49    of an allocation request is rounded up to the next power of two
50    (`order'), and satisfied from the appropriate page.
51
52    Each page is recorded in a page-entry, which also maintains an
53    in-use bitmap of object positions on the page.  This allows the
54    allocation state of a particular object to be flipped without
55    touching the page itself.
56
57    Each page-entry also has a context depth, which is used to track
58    pushing and popping of allocation contexts.  Only objects allocated
59    in the current (highest-numbered) context may be collected.  
60
61    Page entries are arranged in an array of singly-linked lists.  The
62    array is indexed by the allocation size, in bits, of the pages on
63    it; i.e. all pages on a list allocate objects of the same size.
64    Pages are ordered on the list such that all non-full pages precede
65    all full pages, with non-full pages arranged in order of decreasing
66    context depth.
67
68    Empty pages (of all orders) are kept on a single page cache list,
69    and are considered first when new pages are required; they are
70    deallocated at the start of the next collection if they haven't
71    been recycled by then.  */
72
73
74 /* Define GGC_POISON to poison memory marked unused by the collector.  */
75 #undef GGC_POISON
76
77 /* Define GGC_ALWAYS_COLLECT to perform collection every time
78    ggc_collect is invoked.  Otherwise, collection is performed only
79    when a significant amount of memory has been allocated since the
80    last collection.  */
81 #undef GGC_ALWAYS_COLLECT
82
83 #ifdef ENABLE_GC_CHECKING
84 #define GGC_POISON
85 #endif
86 #ifdef ENABLE_GC_ALWAYS_COLLECT
87 #define GGC_ALWAYS_COLLECT
88 #endif
89
90 /* Define GGC_DEBUG_LEVEL to print debugging information.
91      0: No debugging output.
92      1: GC statistics only.
93      2: Page-entry allocations/deallocations as well.
94      3: Object allocations as well.
95      4: Object marks as well.   */
96 #define GGC_DEBUG_LEVEL (0)
97 \f
98 #ifndef HOST_BITS_PER_PTR
99 #define HOST_BITS_PER_PTR  HOST_BITS_PER_LONG
100 #endif
101
102 /* The "" allocated string.  */
103 char *empty_string;
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
139 /* A page_entry records the status of an allocation page.  This
140    structure is dynamically sized to fit the bitmap in_use_p.  */
141 typedef struct page_entry 
142 {
143   /* The next page-entry with objects of the same size, or NULL if
144      this is the last page-entry.  */
145   struct page_entry *next;
146
147   /* The number of bytes allocated.  (This will always be a multiple
148      of the host system page size.)  */
149   size_t bytes;
150
151   /* The address at which the memory is allocated.  */
152   char *page;
153
154   /* Saved in-use bit vector for pages that aren't in the topmost
155      context during collection.  */
156   unsigned long *save_in_use_p;
157
158   /* Context depth of this page.  */
159   unsigned short context_depth;
160
161   /* The number of free objects remaining on this page.  */
162   unsigned short num_free_objects;
163
164   /* A likely candidate for the bit position of a free object for the
165      next allocation from this page.  */
166   unsigned short next_bit_hint;
167
168   /* The lg of size of objects allocated from this page.  */
169   unsigned char order;
170
171   /* A bit vector indicating whether or not objects are in use.  The
172      Nth bit is one if the Nth object on this page is allocated.  This
173      array is dynamically sized.  */
174   unsigned long in_use_p[1];
175 } page_entry;
176
177
178 #if HOST_BITS_PER_PTR <= 32
179
180 /* On 32-bit hosts, we use a two level page table, as pictured above.  */
181 typedef page_entry **page_table[PAGE_L1_SIZE];
182
183 #else
184
185 /* On 64-bit hosts, we use the same two level page tables plus a linked
186    list that disambiguates the top 32-bits.  There will almost always be
187    exactly one entry in the list.  */
188 typedef struct page_table_chain
189 {
190   struct page_table_chain *next;
191   size_t high_bits;
192   page_entry **table[PAGE_L1_SIZE];
193 } *page_table;
194
195 #endif
196
197 /* The rest of the global variables.  */
198 static struct globals
199 {
200   /* The Nth element in this array is a page with objects of size 2^N.
201      If there are any pages with free objects, they will be at the
202      head of the list.  NULL if there are no page-entries for this
203      object size.  */
204   page_entry *pages[HOST_BITS_PER_PTR];
205
206   /* The Nth element in this array is the last page with objects of
207      size 2^N.  NULL if there are no page-entries for this object
208      size.  */
209   page_entry *page_tails[HOST_BITS_PER_PTR];
210
211   /* Lookup table for associating allocation pages with object addresses.  */
212   page_table lookup;
213
214   /* The system's page size.  */
215   size_t pagesize;
216   size_t lg_pagesize;
217
218   /* Bytes currently allocated.  */
219   size_t allocated;
220
221   /* Bytes currently allocated at the end of the last collection.  */
222   size_t allocated_last_gc;
223
224   /* Total amount of memory mapped.  */
225   size_t bytes_mapped;
226
227   /* The current depth in the context stack.  */
228   unsigned short context_depth;
229
230   /* A file descriptor open to /dev/zero for reading.  */
231 #if defined (HAVE_MMAP_ANYWHERE) && !defined(MAP_ANONYMOUS)
232   int dev_zero_fd;
233 #endif
234
235   /* A cache of free system pages.  */
236   page_entry *free_pages;
237
238   /* The file descriptor for debugging output.  */
239   FILE *debug_file;
240 } G;
241
242
243 /* Compute DIVIDEND / DIVISOR, rounded up.  */
244 #define DIV_ROUND_UP(Dividend, Divisor) \
245   (((Dividend) + (Divisor) - 1) / (Divisor))
246
247 /* The number of objects per allocation page, for objects of size
248    2^ORDER.  */
249 #define OBJECTS_PER_PAGE(Order) \
250   ((Order) >= G.lg_pagesize ? 1 : G.pagesize / ((size_t)1 << (Order)))
251
252 /* The size in bytes required to maintain a bitmap for the objects
253    on a page-entry.  */
254 #define BITMAP_SIZE(Num_objects) \
255   (DIV_ROUND_UP ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
256
257 /* Skip garbage collection if the current allocation is not at least
258    this factor times the allocation at the end of the last collection.
259    In other words, total allocation must expand by (this factor minus
260    one) before collection is performed.  */
261 #define GGC_MIN_EXPAND_FOR_GC (1.3)
262
263 /* Bound `allocated_last_gc' to 4MB, to prevent the memory expansion
264    test from triggering too often when the heap is small.  */
265 #define GGC_MIN_LAST_ALLOCATED (4 * 1024 * 1024)
266
267 \f
268 static int ggc_allocated_p PARAMS ((const void *));
269 static page_entry *lookup_page_table_entry PARAMS ((const void *));
270 static void set_page_table_entry PARAMS ((void *, page_entry *));
271 static char *alloc_anon PARAMS ((char *, size_t));
272 static struct page_entry * alloc_page PARAMS ((unsigned));
273 static void free_page PARAMS ((struct page_entry *));
274 static void release_pages PARAMS ((void));
275 static void clear_marks PARAMS ((void));
276 static void sweep_pages PARAMS ((void));
277 static void ggc_recalculate_in_use_p PARAMS ((page_entry *));
278
279 #ifdef GGC_POISON
280 static void poison_pages PARAMS ((void));
281 #endif
282
283 void debug_print_page_list PARAMS ((int));
284 \f
285 /* Returns non-zero if P was allocated in GC'able memory.  */
286
287 static inline int
288 ggc_allocated_p (p)
289      const void *p;
290 {
291   page_entry ***base;
292   size_t L1, L2;
293
294 #if HOST_BITS_PER_PTR <= 32
295   base = &G.lookup[0];
296 #else
297   page_table table = G.lookup;
298   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
299   while (1)
300     {
301       if (table == NULL)
302         return 0;
303       if (table->high_bits == high_bits)
304         break;
305       table = table->next;
306     }
307   base = &table->table[0];
308 #endif
309
310   /* Extract the level 1 and 2 indicies.  */
311   L1 = LOOKUP_L1 (p);
312   L2 = LOOKUP_L2 (p);
313
314   return base[L1] && base[L1][L2];
315 }
316
317 /* Traverse the page table and find the entry for a page. 
318    Die (probably) if the object wasn't allocated via GC.  */
319
320 static inline page_entry *
321 lookup_page_table_entry(p)
322      const void *p;
323 {
324   page_entry ***base;
325   size_t L1, L2;
326
327 #if HOST_BITS_PER_PTR <= 32
328   base = &G.lookup[0];
329 #else
330   page_table table = G.lookup;
331   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
332   while (table->high_bits != high_bits)
333     table = table->next;
334   base = &table->table[0];
335 #endif
336
337   /* Extract the level 1 and 2 indicies.  */
338   L1 = LOOKUP_L1 (p);
339   L2 = LOOKUP_L2 (p);
340
341   return base[L1][L2];
342 }
343
344 /* Set the page table entry for a page.  */
345
346 static void
347 set_page_table_entry(p, entry)
348      void *p;
349      page_entry *entry;
350 {
351   page_entry ***base;
352   size_t L1, L2;
353
354 #if HOST_BITS_PER_PTR <= 32
355   base = &G.lookup[0];
356 #else
357   page_table table;
358   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
359   for (table = G.lookup; table; table = table->next)
360     if (table->high_bits == high_bits)
361       goto found;
362
363   /* Not found -- allocate a new table.  */
364   table = (page_table) xcalloc (1, sizeof(*table));
365   table->next = G.lookup;
366   table->high_bits = high_bits;
367   G.lookup = table;
368 found:
369   base = &table->table[0];
370 #endif
371
372   /* Extract the level 1 and 2 indicies.  */
373   L1 = LOOKUP_L1 (p);
374   L2 = LOOKUP_L2 (p);
375
376   if (base[L1] == NULL)
377     base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
378
379   base[L1][L2] = entry;
380 }
381
382 /* Prints the page-entry for object size ORDER, for debugging.  */
383
384 void
385 debug_print_page_list (order)
386      int order;
387 {
388   page_entry *p;
389   printf ("Head=%p, Tail=%p:\n", G.pages[order], G.page_tails[order]);
390   p = G.pages[order];
391   while (p != NULL)
392     {
393       printf ("%p(%1d|%3d) -> ", p, p->context_depth, p->num_free_objects);
394       p = p->next;
395     }
396   printf ("NULL\n");
397   fflush (stdout);
398 }
399
400 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
401    (if non-null).  */
402
403 static inline char *
404 alloc_anon (pref, size)
405      char *pref ATTRIBUTE_UNUSED;
406      size_t size;
407 {
408   char *page;
409
410 #ifdef HAVE_MMAP_ANYWHERE
411 #ifdef MAP_ANONYMOUS
412   page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
413                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
414 #else
415   page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
416                         MAP_PRIVATE, G.dev_zero_fd, 0);
417 #endif
418   if (page == (char *) MAP_FAILED)
419     {
420       fputs ("Virtual memory exhausted!\n", stderr);
421       exit(1);
422     }
423 #else
424 #ifdef HAVE_VALLOC
425   page = (char *) valloc (size);
426   if (!page)
427     {
428       fputs ("Virtual memory exhausted!\n", stderr);
429       exit(1);
430     }
431 #endif /* HAVE_VALLOC */
432 #endif /* HAVE_MMAP_ANYWHERE */
433
434   /* Remember that we allocated this memory.  */
435   G.bytes_mapped += size;
436
437   return page;
438 }
439
440 /* Allocate a new page for allocating objects of size 2^ORDER,
441    and return an entry for it.  The entry is not added to the
442    appropriate page_table list.  */
443
444 static inline struct page_entry *
445 alloc_page (order)
446      unsigned order;
447 {
448   struct page_entry *entry, *p, **pp;
449   char *page;
450   size_t num_objects;
451   size_t bitmap_size;
452   size_t page_entry_size;
453   size_t entry_size;
454
455   num_objects = OBJECTS_PER_PAGE (order);
456   bitmap_size = BITMAP_SIZE (num_objects + 1);
457   page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
458   entry_size = num_objects * (1 << order);
459
460   entry = NULL;
461   page = NULL;
462
463   /* Check the list of free pages for one we can use.  */
464   for (pp = &G.free_pages, p = *pp; p ; pp = &p->next, p = *pp)
465     if (p->bytes == entry_size)
466       break;
467
468   if (p != NULL)
469     {
470       /* Recycle the allocated memory from this page ... */
471       *pp = p->next;
472       page = p->page;
473       /* ... and, if possible, the page entry itself.  */
474       if (p->order == order)
475         {
476           entry = p;
477           memset (entry, 0, page_entry_size);
478         }
479       else
480         free (p);
481     }
482   else
483     {
484       /* Actually allocate the memory.  */
485       page = alloc_anon (NULL, entry_size);
486     }
487
488   if (entry == NULL)
489     entry = (struct page_entry *) xcalloc (1, page_entry_size);
490
491   entry->bytes = entry_size;
492   entry->page = page;
493   entry->context_depth = G.context_depth;
494   entry->order = order;
495   entry->num_free_objects = num_objects;
496   entry->next_bit_hint = 1;
497
498   /* Set the one-past-the-end in-use bit.  This acts as a sentry as we
499      increment the hint.  */
500   entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
501     = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
502
503   set_page_table_entry (page, entry);
504
505   if (GGC_DEBUG_LEVEL >= 2)
506     fprintf (G.debug_file, 
507              "Allocating page at %p, object size=%d, data %p-%p\n", entry,
508              1 << order, page, page + entry_size - 1);
509
510   return entry;
511 }
512
513 /* For a page that is no longer needed, put it on the free page list.  */
514
515 static inline void
516 free_page (entry)
517      page_entry *entry;
518 {
519   if (GGC_DEBUG_LEVEL >= 2)
520     fprintf (G.debug_file, 
521              "Deallocating page at %p, data %p-%p\n", entry,
522              entry->page, entry->page + entry->bytes - 1);
523
524   set_page_table_entry (entry->page, NULL);
525
526   entry->next = G.free_pages;
527   G.free_pages = entry;
528 }
529
530 /* Release the free page cache to the system.  */
531
532 static void
533 release_pages ()
534 {
535 #ifdef HAVE_MMAP_ANYWHERE
536   page_entry *p, *next;
537   char *start;
538   size_t len;
539
540   p = G.free_pages;
541   if (p == NULL)
542     return;
543
544   next = p->next;
545   start = p->page;
546   len = p->bytes;
547   free (p);
548   p = next;
549
550   while (p)
551     {
552       next = p->next;
553       /* Gather up adjacent pages so they are unmapped together.  */
554       if (p->page == start + len)
555         len += p->bytes;
556       else
557         {
558           munmap (start, len);
559           G.bytes_mapped -= len;
560           start = p->page;
561           len = p->bytes;
562         }
563       free (p);
564       p = next;
565     }
566
567   munmap (start, len);
568   G.bytes_mapped -= len;
569 #else
570 #ifdef HAVE_VALLOC
571   page_entry *p, *next;
572
573   for (p = G.free_pages; p ; p = next)
574     {
575       next = p->next;
576       free (p->page);
577       G.bytes_mapped -= p->bytes;
578       free (p);
579     }
580 #endif /* HAVE_VALLOC */
581 #endif /* HAVE_MMAP_ANYWHERE */
582
583   G.free_pages = NULL;
584 }
585
586 /* This table provides a fast way to determine ceil(log_2(size)) for
587    allocation requests.  The minimum allocation size is four bytes.  */
588
589 static unsigned char const size_lookup[257] = 
590
591   2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 
592   4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
593   5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
594   6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
595   6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
596   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
597   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
598   7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 
599   7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
600   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
601   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
602   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
603   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
604   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
605   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
606   8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
607   8
608 };
609
610 /* Allocate a chunk of memory of SIZE bytes.  If ZERO is non-zero, the
611    memory is zeroed; otherwise, its contents are undefined.  */
612
613 void *
614 ggc_alloc_obj (size, zero)
615      size_t size;
616      int zero;
617 {
618   unsigned order, word, bit, object_offset;
619   struct page_entry *entry;
620   void *result;
621
622   if (size <= 256)
623     order = size_lookup[size];
624   else
625     {
626       order = 9;
627       while (size > ((size_t) 1 << order))
628         order++;
629     }
630
631   /* If there are non-full pages for this size allocation, they are at
632      the head of the list.  */
633   entry = G.pages[order];
634
635   /* If there is no page for this object size, or all pages in this
636      context are full, allocate a new page.  */
637   if (entry == NULL || entry->num_free_objects == 0)
638     {
639       struct page_entry *new_entry;
640       new_entry = alloc_page (order);
641       
642       /* If this is the only entry, it's also the tail.  */
643       if (entry == NULL)
644         G.page_tails[order] = new_entry;
645      
646       /* Put new pages at the head of the page list.  */
647       new_entry->next = entry;
648       entry = new_entry;
649       G.pages[order] = new_entry;
650
651       /* For a new page, we know the word and bit positions (in the
652          in_use bitmap) of the first available object -- they're zero.  */
653       new_entry->next_bit_hint = 1;
654       word = 0;
655       bit = 0;
656       object_offset = 0;
657     }
658   else
659     {
660       /* First try to use the hint left from the previous allocation
661          to locate a clear bit in the in-use bitmap.  We've made sure
662          that the one-past-the-end bit is always set, so if the hint
663          has run over, this test will fail.  */
664       unsigned hint = entry->next_bit_hint;
665       word = hint / HOST_BITS_PER_LONG;
666       bit = hint % HOST_BITS_PER_LONG;
667       
668       /* If the hint didn't work, scan the bitmap from the beginning.  */
669       if ((entry->in_use_p[word] >> bit) & 1)
670         {
671           word = bit = 0;
672           while (~entry->in_use_p[word] == 0)
673             ++word;
674           while ((entry->in_use_p[word] >> bit) & 1)
675             ++bit;
676           hint = word * HOST_BITS_PER_LONG + bit;
677         }
678
679       /* Next time, try the next bit.  */
680       entry->next_bit_hint = hint + 1;
681
682       object_offset = hint << order;
683     }
684
685   /* Set the in-use bit.  */
686   entry->in_use_p[word] |= ((unsigned long) 1 << bit);
687
688   /* Keep a running total of the number of free objects.  If this page
689      fills up, we may have to move it to the end of the list if the
690      next page isn't full.  If the next page is full, all subsequent
691      pages are full, so there's no need to move it.  */
692   if (--entry->num_free_objects == 0
693       && entry->next != NULL
694       && entry->next->num_free_objects > 0)
695     {
696       G.pages[order] = entry->next;
697       entry->next = NULL;
698       G.page_tails[order]->next = entry;
699       G.page_tails[order] = entry;
700     }
701
702   /* Calculate the object's address.  */
703   result = entry->page + object_offset;
704
705 #ifdef GGC_POISON
706   /* `Poison' the entire allocated object before zeroing the requested area,
707      so that bytes beyond the end, if any, will not necessarily be zero.  */
708   memset (result, 0xaf, 1 << order);
709 #endif
710
711   if (zero)
712     memset (result, 0, size);
713
714   /* Keep track of how many bytes are being allocated.  This
715      information is used in deciding when to collect.  */
716   G.allocated += (size_t) 1 << order;
717
718   if (GGC_DEBUG_LEVEL >= 3)
719     fprintf (G.debug_file, 
720              "Allocating object, requested size=%d, actual=%d at %p on %p\n",
721              (int) size, 1 << order, result, entry);
722
723   return result;
724 }
725
726 /* If P is not marked, marks it and return false.  Otherwise return true.
727    P must have been allocated by the GC allocator; it mustn't point to
728    static objects, stack variables, or memory allocated with malloc.  */
729
730 int
731 ggc_set_mark (p)
732      const void *p;
733 {
734   page_entry *entry;
735   unsigned bit, word;
736   unsigned long mask;
737
738   /* Look up the page on which the object is alloced.  If the object
739      wasn't allocated by the collector, we'll probably die.  */
740   entry = lookup_page_table_entry (p);
741 #ifdef ENABLE_CHECKING
742   if (entry == NULL)
743     abort ();
744 #endif
745
746   /* Calculate the index of the object on the page; this is its bit
747      position in the in_use_p bitmap.  */
748   bit = (((const char *) p) - entry->page) >> entry->order;
749   word = bit / HOST_BITS_PER_LONG;
750   mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
751   
752   /* If the bit was previously set, skip it. */
753   if (entry->in_use_p[word] & mask)
754     return 1;
755
756   /* Otherwise set it, and decrement the free object count.  */
757   entry->in_use_p[word] |= mask;
758   entry->num_free_objects -= 1;
759
760   G.allocated += (size_t) 1 << entry->order;
761
762   if (GGC_DEBUG_LEVEL >= 4)
763     fprintf (G.debug_file, "Marking %p\n", p);
764
765   return 0;
766 }
767
768 /* Mark P, but check first that it was allocated by the collector.  */
769
770 void
771 ggc_mark_if_gcable (p)
772      const void *p;
773 {
774   if (p && ggc_allocated_p (p))
775     ggc_set_mark (p);
776 }
777
778 /* Return the size of the gc-able object P.  */
779
780 size_t
781 ggc_get_size (p)
782      const void *p;
783 {
784   page_entry *pe = lookup_page_table_entry (p);
785   return 1 << pe->order;
786 }
787 \f
788 /* Initialize the ggc-mmap allocator.  */
789
790 void
791 init_ggc ()
792 {
793   G.pagesize = getpagesize();
794   G.lg_pagesize = exact_log2 (G.pagesize);
795
796 #if defined (HAVE_MMAP_ANYWHERE) && !defined(MAP_ANONYMOUS)
797   G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
798   if (G.dev_zero_fd == -1)
799     abort ();
800 #endif
801
802 #if 0
803   G.debug_file = fopen ("ggc-mmap.debug", "w");
804 #else
805   G.debug_file = stdout;
806 #endif
807
808   G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
809
810 #ifdef HAVE_MMAP_ANYWHERE
811   /* StunOS has an amazing off-by-one error for the first mmap allocation
812      after fiddling with RLIMIT_STACK.  The result, as hard as it is to
813      believe, is an unaligned page allocation, which would cause us to
814      hork badly if we tried to use it.  */
815   {
816     char *p = alloc_anon (NULL, G.pagesize);
817     if ((size_t)p & (G.pagesize - 1))
818       {
819         /* How losing.  Discard this one and try another.  If we still
820            can't get something useful, give up.  */
821
822         p = alloc_anon (NULL, G.pagesize);
823         if ((size_t)p & (G.pagesize - 1))
824           abort ();
825       }
826     munmap (p, G.pagesize);
827   }
828 #endif
829
830   empty_string = ggc_alloc_string ("", 0);
831   ggc_add_string_root (&empty_string, 1);
832 }
833
834 /* Increment the `GC context'.  Objects allocated in an outer context
835    are never freed, eliminating the need to register their roots.  */
836
837 void
838 ggc_push_context ()
839 {
840   ++G.context_depth;
841
842   /* Die on wrap.  */
843   if (G.context_depth == 0)
844     abort ();
845 }
846
847 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
848    reflects reality.  Recalculate NUM_FREE_OBJECTS as well.  */
849
850 static void
851 ggc_recalculate_in_use_p (p)
852      page_entry *p;
853 {
854   unsigned int i;
855   size_t num_objects;
856
857   /* Because the past-the-end bit in in_use_p is always set, we 
858      pretend there is one additional object.  */
859   num_objects = OBJECTS_PER_PAGE (p->order) + 1;
860
861   /* Reset the free object count.  */
862   p->num_free_objects = num_objects;
863
864   /* Combine the IN_USE_P and SAVE_IN_USE_P arrays.  */
865   for (i = 0; 
866        i < DIV_ROUND_UP (BITMAP_SIZE (num_objects),
867                          sizeof (*p->in_use_p));
868        ++i)
869     {
870       unsigned long j;
871
872       /* Something is in use if it is marked, or if it was in use in a
873          context further down the context stack.  */
874       p->in_use_p[i] |= p->save_in_use_p[i];
875
876       /* Decrement the free object count for every object allocated.  */
877       for (j = p->in_use_p[i]; j; j >>= 1)
878         p->num_free_objects -= (j & 1);
879     }
880
881   if (p->num_free_objects >= num_objects)
882     abort ();
883 }
884
885 /* Decrement the `GC context'.  All objects allocated since the 
886    previous ggc_push_context are migrated to the outer context.  */
887
888 void
889 ggc_pop_context ()
890 {
891   unsigned order, depth;
892
893   depth = --G.context_depth;
894
895   /* Any remaining pages in the popped context are lowered to the new
896      current context; i.e. objects allocated in the popped context and
897      left over are imported into the previous context.  */
898   for (order = 2; order < HOST_BITS_PER_PTR; order++)
899     {
900       page_entry *p;
901
902       for (p = G.pages[order]; p != NULL; p = p->next)
903         {
904           if (p->context_depth > depth)
905             p->context_depth = depth;
906
907           /* If this page is now in the topmost context, and we'd
908              saved its allocation state, restore it.  */
909           else if (p->context_depth == depth && p->save_in_use_p)
910             {
911               ggc_recalculate_in_use_p (p);
912               free (p->save_in_use_p);
913               p->save_in_use_p = 0;
914             }
915         }
916     }
917 }
918 \f
919 /* Unmark all objects.  */
920
921 static inline void
922 clear_marks ()
923 {
924   unsigned order;
925
926   for (order = 2; order < HOST_BITS_PER_PTR; order++)
927     {
928       size_t num_objects = OBJECTS_PER_PAGE (order);
929       size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
930       page_entry *p;
931
932       for (p = G.pages[order]; p != NULL; p = p->next)
933         {
934 #ifdef ENABLE_CHECKING
935           /* The data should be page-aligned.  */
936           if ((size_t) p->page & (G.pagesize - 1))
937             abort ();
938 #endif
939
940           /* Pages that aren't in the topmost context are not collected;
941              nevertheless, we need their in-use bit vectors to store GC
942              marks.  So, back them up first.  */
943           if (p->context_depth < G.context_depth)
944             {
945               if (! p->save_in_use_p)
946                 p->save_in_use_p = xmalloc (bitmap_size);
947               memcpy (p->save_in_use_p, p->in_use_p, bitmap_size);
948             }
949
950           /* Reset reset the number of free objects and clear the
951              in-use bits.  These will be adjusted by mark_obj.  */
952           p->num_free_objects = num_objects;
953           memset (p->in_use_p, 0, bitmap_size);
954
955           /* Make sure the one-past-the-end bit is always set.  */
956           p->in_use_p[num_objects / HOST_BITS_PER_LONG] 
957             = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
958         }
959     }
960 }
961
962 /* Free all empty pages.  Partially empty pages need no attention
963    because the `mark' bit doubles as an `unused' bit.  */
964
965 static inline void
966 sweep_pages ()
967 {
968   unsigned order;
969
970   for (order = 2; order < HOST_BITS_PER_PTR; order++)
971     {
972       /* The last page-entry to consider, regardless of entries
973          placed at the end of the list.  */
974       page_entry * const last = G.page_tails[order];
975
976       size_t num_objects = OBJECTS_PER_PAGE (order);
977       page_entry *p, *previous;
978       int done;
979         
980       p = G.pages[order];
981       if (p == NULL)
982         continue;
983
984       previous = NULL;
985       do
986         {
987           page_entry *next = p->next;
988
989           /* Loop until all entries have been examined.  */
990           done = (p == last);
991
992           /* Only objects on pages in the topmost context should get
993              collected.  */
994           if (p->context_depth < G.context_depth)
995             ;
996
997           /* Remove the page if it's empty.  */
998           else if (p->num_free_objects == num_objects)
999             {
1000               if (! previous)
1001                 G.pages[order] = next;
1002               else
1003                 previous->next = next;
1004
1005               /* Are we removing the last element?  */
1006               if (p == G.page_tails[order])
1007                 G.page_tails[order] = previous;
1008               free_page (p);
1009               p = previous;
1010             }
1011
1012           /* If the page is full, move it to the end.  */
1013           else if (p->num_free_objects == 0)
1014             {
1015               /* Don't move it if it's already at the end.  */
1016               if (p != G.page_tails[order])
1017                 {
1018                   /* Move p to the end of the list.  */
1019                   p->next = NULL;
1020                   G.page_tails[order]->next = p;
1021
1022                   /* Update the tail pointer...  */
1023                   G.page_tails[order] = p;
1024
1025                   /* ... and the head pointer, if necessary.  */
1026                   if (! previous)
1027                     G.pages[order] = next;
1028                   else
1029                     previous->next = next;
1030                   p = previous;
1031                 }
1032             }
1033
1034           /* If we've fallen through to here, it's a page in the
1035              topmost context that is neither full nor empty.  Such a
1036              page must precede pages at lesser context depth in the
1037              list, so move it to the head.  */
1038           else if (p != G.pages[order])
1039             {
1040               previous->next = p->next;
1041               p->next = G.pages[order];
1042               G.pages[order] = p;
1043               /* Are we moving the last element?  */
1044               if (G.page_tails[order] == p)
1045                 G.page_tails[order] = previous;
1046               p = previous;
1047             }
1048
1049           previous = p;
1050           p = next;
1051         } 
1052       while (! done);
1053
1054       /* Now, restore the in_use_p vectors for any pages from contexts
1055          other than the current one.  */
1056       for (p = G.pages[order]; p; p = p->next)
1057         if (p->context_depth != G.context_depth)
1058           ggc_recalculate_in_use_p (p);
1059     }
1060 }
1061
1062 #ifdef GGC_POISON
1063 /* Clobber all free objects.  */
1064
1065 static inline void
1066 poison_pages ()
1067 {
1068   unsigned order;
1069
1070   for (order = 2; order < HOST_BITS_PER_PTR; order++)
1071     {
1072       size_t num_objects = OBJECTS_PER_PAGE (order);
1073       size_t size = (size_t) 1 << order;
1074       page_entry *p;
1075
1076       for (p = G.pages[order]; p != NULL; p = p->next)
1077         {
1078           size_t i;
1079
1080           if (p->context_depth != G.context_depth)
1081             /* Since we don't do any collection for pages in pushed
1082                contexts, there's no need to do any poisoning.  And
1083                besides, the IN_USE_P array isn't valid until we pop
1084                contexts.  */
1085             continue;
1086
1087           for (i = 0; i < num_objects; i++)
1088             {
1089               size_t word, bit;
1090               word = i / HOST_BITS_PER_LONG;
1091               bit = i % HOST_BITS_PER_LONG;
1092               if (((p->in_use_p[word] >> bit) & 1) == 0)
1093                 memset (p->page + i * size, 0xa5, size);
1094             }
1095         }
1096     }
1097 }
1098 #endif
1099
1100 /* Top level mark-and-sweep routine.  */
1101
1102 void
1103 ggc_collect ()
1104 {
1105   /* Avoid frequent unnecessary work by skipping collection if the
1106      total allocations haven't expanded much since the last
1107      collection.  */
1108 #ifndef GGC_ALWAYS_COLLECT
1109   if (G.allocated < GGC_MIN_EXPAND_FOR_GC * G.allocated_last_gc)
1110     return;
1111 #endif
1112
1113   timevar_push (TV_GC);
1114   if (!quiet_flag)
1115     fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
1116
1117   /* Zero the total allocated bytes.  We'll reaccumulate this while
1118      marking.  */
1119   G.allocated = 0;
1120
1121   /* Release the pages we freed the last time we collected, but didn't 
1122      reuse in the interim.  */
1123   release_pages ();
1124
1125   clear_marks ();
1126   ggc_mark_roots ();
1127   
1128 #ifdef GGC_POISON
1129   poison_pages ();
1130 #endif
1131
1132   sweep_pages ();
1133
1134   G.allocated_last_gc = G.allocated;
1135   if (G.allocated_last_gc < GGC_MIN_LAST_ALLOCATED)
1136     G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
1137
1138   timevar_pop (TV_GC);
1139
1140   if (!quiet_flag)
1141     fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
1142 }
1143
1144 /* Print allocation statistics.  */
1145
1146 void
1147 ggc_page_print_statistics ()
1148 {
1149   struct ggc_statistics stats;
1150   unsigned int i;
1151
1152   /* Clear the statistics.  */
1153   memset (&stats, 0, sizeof (stats));
1154   
1155   /* Make sure collection will really occur.  */
1156   G.allocated_last_gc = 0;
1157
1158   /* Collect and print the statistics common across collectors.  */
1159   ggc_print_statistics (stderr, &stats);
1160
1161   /* Release free pages so that we will not count the bytes allocated
1162      there as part of the total allocated memory.  */
1163   release_pages ();
1164
1165   /* Collect some information about the various sizes of 
1166      allocation.  */
1167   fprintf (stderr, "\n%-4s%-16s%-16s\n", "Log", "Allocated", "Used");
1168   for (i = 0; i < HOST_BITS_PER_PTR; ++i)
1169     {
1170       page_entry *p;
1171       size_t allocated;
1172       size_t in_use;
1173
1174       /* Skip empty entries.  */
1175       if (!G.pages[i])
1176         continue;
1177
1178       allocated = in_use = 0;
1179
1180       /* Figure out the total number of bytes allocated for objects of
1181          this size, and how many of them are actually in use.  */
1182       for (p = G.pages[i]; p; p = p->next)
1183         {
1184           allocated += p->bytes;
1185           in_use += 
1186             (OBJECTS_PER_PAGE (i) - p->num_free_objects) * (1 << i);
1187         }
1188       fprintf (stderr, "%-3d %-15lu %-15lu\n", i, 
1189                (unsigned long) allocated, (unsigned long) in_use);
1190     }
1191
1192   /* Print out some global information.  */
1193   fprintf (stderr, "\nTotal bytes marked: %lu\n", 
1194            (unsigned long) G.allocated);
1195   fprintf (stderr, "Total bytes mapped: %lu\n", 
1196            (unsigned long) G.bytes_mapped);
1197 }