1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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)
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.
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. */
32 #ifdef HAVE_MMAP_ANYWHERE
40 #if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
41 #define MAP_ANONYMOUS MAP_ANON
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.
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.
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.
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
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. */
74 /* Define GGC_POISON to poison memory marked unused by the collector. */
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
81 #undef GGC_ALWAYS_COLLECT
83 #ifdef ENABLE_GC_CHECKING
86 #ifdef ENABLE_GC_ALWAYS_COLLECT
87 #define GGC_ALWAYS_COLLECT
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)
98 #ifndef HOST_BITS_PER_PTR
99 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
102 /* The "" allocated string. */
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:
111 msb +----------------+----+------+------+ lsb
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.
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
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)
132 #define LOOKUP_L1(p) \
133 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
135 #define LOOKUP_L2(p) \
136 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
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
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;
147 /* The number of bytes allocated. (This will always be a multiple
148 of the host system page size.) */
151 /* The address at which the memory is allocated. */
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;
158 /* Context depth of this page. */
159 unsigned short context_depth;
161 /* The number of free objects remaining on this page. */
162 unsigned short num_free_objects;
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;
168 /* The lg of size of objects allocated from this page. */
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];
178 #if HOST_BITS_PER_PTR <= 32
180 /* On 32-bit hosts, we use a two level page table, as pictured above. */
181 typedef page_entry **page_table[PAGE_L1_SIZE];
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
190 struct page_table_chain *next;
192 page_entry **table[PAGE_L1_SIZE];
197 /* The rest of the global variables. */
198 static struct globals
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
204 page_entry *pages[HOST_BITS_PER_PTR];
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
209 page_entry *page_tails[HOST_BITS_PER_PTR];
211 /* Lookup table for associating allocation pages with object addresses. */
214 /* The system's page size. */
218 /* Bytes currently allocated. */
221 /* Bytes currently allocated at the end of the last collection. */
222 size_t allocated_last_gc;
224 /* Total amount of memory mapped. */
227 /* The current depth in the context stack. */
228 unsigned short context_depth;
230 /* A file descriptor open to /dev/zero for reading. */
231 #if defined (HAVE_MMAP_ANYWHERE) && !defined(MAP_ANONYMOUS)
235 /* A cache of free system pages. */
236 page_entry *free_pages;
238 /* The file descriptor for debugging output. */
243 /* Compute DIVIDEND / DIVISOR, rounded up. */
244 #define DIV_ROUND_UP(Dividend, Divisor) \
245 (((Dividend) + (Divisor) - 1) / (Divisor))
247 /* The number of objects per allocation page, for objects of size
249 #define OBJECTS_PER_PAGE(Order) \
250 ((Order) >= G.lg_pagesize ? 1 : G.pagesize / ((size_t)1 << (Order)))
252 /* The size in bytes required to maintain a bitmap for the objects
254 #define BITMAP_SIZE(Num_objects) \
255 (DIV_ROUND_UP ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
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)
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)
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 *));
280 static void poison_pages PARAMS ((void));
283 void debug_print_page_list PARAMS ((int));
285 /* Returns non-zero if P was allocated in GC'able memory. */
294 #if HOST_BITS_PER_PTR <= 32
297 page_table table = G.lookup;
298 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
303 if (table->high_bits == high_bits)
307 base = &table->table[0];
310 /* Extract the level 1 and 2 indicies. */
314 return base[L1] && base[L1][L2];
317 /* Traverse the page table and find the entry for a page.
318 Die (probably) if the object wasn't allocated via GC. */
320 static inline page_entry *
321 lookup_page_table_entry(p)
327 #if HOST_BITS_PER_PTR <= 32
330 page_table table = G.lookup;
331 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
332 while (table->high_bits != high_bits)
334 base = &table->table[0];
337 /* Extract the level 1 and 2 indicies. */
344 /* Set the page table entry for a page. */
347 set_page_table_entry(p, entry)
354 #if HOST_BITS_PER_PTR <= 32
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)
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;
369 base = &table->table[0];
372 /* Extract the level 1 and 2 indicies. */
376 if (base[L1] == NULL)
377 base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
379 base[L1][L2] = entry;
382 /* Prints the page-entry for object size ORDER, for debugging. */
385 debug_print_page_list (order)
389 printf ("Head=%p, Tail=%p:\n", (PTR) G.pages[order],
390 (PTR) G.page_tails[order]);
394 printf ("%p(%1d|%3d) -> ", (PTR) p, p->context_depth,
395 p->num_free_objects);
402 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
406 alloc_anon (pref, size)
407 char *pref ATTRIBUTE_UNUSED;
412 #ifdef HAVE_MMAP_ANYWHERE
414 page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
415 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
417 page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
418 MAP_PRIVATE, G.dev_zero_fd, 0);
420 if (page == (char *) MAP_FAILED)
422 fputs ("Virtual memory exhausted!\n", stderr);
427 page = (char *) valloc (size);
430 fputs ("Virtual memory exhausted!\n", stderr);
433 #endif /* HAVE_VALLOC */
434 #endif /* HAVE_MMAP_ANYWHERE */
436 /* Remember that we allocated this memory. */
437 G.bytes_mapped += size;
442 /* Allocate a new page for allocating objects of size 2^ORDER,
443 and return an entry for it. The entry is not added to the
444 appropriate page_table list. */
446 static inline struct page_entry *
450 struct page_entry *entry, *p, **pp;
454 size_t page_entry_size;
457 num_objects = OBJECTS_PER_PAGE (order);
458 bitmap_size = BITMAP_SIZE (num_objects + 1);
459 page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
460 entry_size = num_objects * (1 << order);
465 /* Check the list of free pages for one we can use. */
466 for (pp = &G.free_pages, p = *pp; p ; pp = &p->next, p = *pp)
467 if (p->bytes == entry_size)
472 /* Recycle the allocated memory from this page ... */
475 /* ... and, if possible, the page entry itself. */
476 if (p->order == order)
479 memset (entry, 0, page_entry_size);
486 /* Actually allocate the memory. */
487 page = alloc_anon (NULL, entry_size);
491 entry = (struct page_entry *) xcalloc (1, page_entry_size);
493 entry->bytes = entry_size;
495 entry->context_depth = G.context_depth;
496 entry->order = order;
497 entry->num_free_objects = num_objects;
498 entry->next_bit_hint = 1;
500 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
501 increment the hint. */
502 entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
503 = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
505 set_page_table_entry (page, entry);
507 if (GGC_DEBUG_LEVEL >= 2)
508 fprintf (G.debug_file,
509 "Allocating page at %p, object size=%d, data %p-%p\n",
510 (PTR) entry, 1 << order, page, page + entry_size - 1);
515 /* For a page that is no longer needed, put it on the free page list. */
521 if (GGC_DEBUG_LEVEL >= 2)
522 fprintf (G.debug_file,
523 "Deallocating page at %p, data %p-%p\n", (PTR) entry,
524 entry->page, entry->page + entry->bytes - 1);
526 set_page_table_entry (entry->page, NULL);
528 entry->next = G.free_pages;
529 G.free_pages = entry;
532 /* Release the free page cache to the system. */
537 #ifdef HAVE_MMAP_ANYWHERE
538 page_entry *p, *next;
555 /* Gather up adjacent pages so they are unmapped together. */
556 if (p->page == start + len)
561 G.bytes_mapped -= len;
570 G.bytes_mapped -= len;
573 page_entry *p, *next;
575 for (p = G.free_pages; p ; p = next)
579 G.bytes_mapped -= p->bytes;
582 #endif /* HAVE_VALLOC */
583 #endif /* HAVE_MMAP_ANYWHERE */
588 /* This table provides a fast way to determine ceil(log_2(size)) for
589 allocation requests. The minimum allocation size is four bytes. */
591 static unsigned char const size_lookup[257] =
593 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
594 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
595 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
596 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
597 6, 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, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
600 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
601 7, 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, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
608 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
612 /* Allocate a chunk of memory of SIZE bytes. If ZERO is non-zero, the
613 memory is zeroed; otherwise, its contents are undefined. */
619 unsigned order, word, bit, object_offset;
620 struct page_entry *entry;
624 order = size_lookup[size];
628 while (size > ((size_t) 1 << order))
632 /* If there are non-full pages for this size allocation, they are at
633 the head of the list. */
634 entry = G.pages[order];
636 /* If there is no page for this object size, or all pages in this
637 context are full, allocate a new page. */
638 if (entry == NULL || entry->num_free_objects == 0)
640 struct page_entry *new_entry;
641 new_entry = alloc_page (order);
643 /* If this is the only entry, it's also the tail. */
645 G.page_tails[order] = new_entry;
647 /* Put new pages at the head of the page list. */
648 new_entry->next = entry;
650 G.pages[order] = new_entry;
652 /* For a new page, we know the word and bit positions (in the
653 in_use bitmap) of the first available object -- they're zero. */
654 new_entry->next_bit_hint = 1;
661 /* First try to use the hint left from the previous allocation
662 to locate a clear bit in the in-use bitmap. We've made sure
663 that the one-past-the-end bit is always set, so if the hint
664 has run over, this test will fail. */
665 unsigned hint = entry->next_bit_hint;
666 word = hint / HOST_BITS_PER_LONG;
667 bit = hint % HOST_BITS_PER_LONG;
669 /* If the hint didn't work, scan the bitmap from the beginning. */
670 if ((entry->in_use_p[word] >> bit) & 1)
673 while (~entry->in_use_p[word] == 0)
675 while ((entry->in_use_p[word] >> bit) & 1)
677 hint = word * HOST_BITS_PER_LONG + bit;
680 /* Next time, try the next bit. */
681 entry->next_bit_hint = hint + 1;
683 object_offset = hint << order;
686 /* Set the in-use bit. */
687 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
689 /* Keep a running total of the number of free objects. If this page
690 fills up, we may have to move it to the end of the list if the
691 next page isn't full. If the next page is full, all subsequent
692 pages are full, so there's no need to move it. */
693 if (--entry->num_free_objects == 0
694 && entry->next != NULL
695 && entry->next->num_free_objects > 0)
697 G.pages[order] = entry->next;
699 G.page_tails[order]->next = entry;
700 G.page_tails[order] = entry;
703 /* Calculate the object's address. */
704 result = entry->page + object_offset;
707 /* `Poison' the entire allocated object, including any padding at
709 memset (result, 0xaf, 1 << order);
712 /* Keep track of how many bytes are being allocated. This
713 information is used in deciding when to collect. */
714 G.allocated += (size_t) 1 << order;
716 if (GGC_DEBUG_LEVEL >= 3)
717 fprintf (G.debug_file,
718 "Allocating object, requested size=%d, actual=%d at %p on %p\n",
719 (int) size, 1 << order, result, (PTR) entry);
724 /* If P is not marked, marks it and return false. Otherwise return true.
725 P must have been allocated by the GC allocator; it mustn't point to
726 static objects, stack variables, or memory allocated with malloc. */
736 /* Look up the page on which the object is alloced. If the object
737 wasn't allocated by the collector, we'll probably die. */
738 entry = lookup_page_table_entry (p);
739 #ifdef ENABLE_CHECKING
744 /* Calculate the index of the object on the page; this is its bit
745 position in the in_use_p bitmap. */
746 bit = (((const char *) p) - entry->page) >> entry->order;
747 word = bit / HOST_BITS_PER_LONG;
748 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
750 /* If the bit was previously set, skip it. */
751 if (entry->in_use_p[word] & mask)
754 /* Otherwise set it, and decrement the free object count. */
755 entry->in_use_p[word] |= mask;
756 entry->num_free_objects -= 1;
758 G.allocated += (size_t) 1 << entry->order;
760 if (GGC_DEBUG_LEVEL >= 4)
761 fprintf (G.debug_file, "Marking %p\n", p);
766 /* Mark P, but check first that it was allocated by the collector. */
769 ggc_mark_if_gcable (p)
772 if (p && ggc_allocated_p (p))
776 /* Return the size of the gc-able object P. */
782 page_entry *pe = lookup_page_table_entry (p);
783 return 1 << pe->order;
786 /* Initialize the ggc-mmap allocator. */
791 G.pagesize = getpagesize();
792 G.lg_pagesize = exact_log2 (G.pagesize);
794 #if defined (HAVE_MMAP_ANYWHERE) && !defined(MAP_ANONYMOUS)
795 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
796 if (G.dev_zero_fd == -1)
801 G.debug_file = fopen ("ggc-mmap.debug", "w");
803 G.debug_file = stdout;
806 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
808 #ifdef HAVE_MMAP_ANYWHERE
809 /* StunOS has an amazing off-by-one error for the first mmap allocation
810 after fiddling with RLIMIT_STACK. The result, as hard as it is to
811 believe, is an unaligned page allocation, which would cause us to
812 hork badly if we tried to use it. */
814 char *p = alloc_anon (NULL, G.pagesize);
815 if ((size_t)p & (G.pagesize - 1))
817 /* How losing. Discard this one and try another. If we still
818 can't get something useful, give up. */
820 p = alloc_anon (NULL, G.pagesize);
821 if ((size_t)p & (G.pagesize - 1))
824 munmap (p, G.pagesize);
828 empty_string = ggc_alloc_string ("", 0);
829 ggc_add_string_root (&empty_string, 1);
832 /* Increment the `GC context'. Objects allocated in an outer context
833 are never freed, eliminating the need to register their roots. */
841 if (G.context_depth == 0)
845 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
846 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
849 ggc_recalculate_in_use_p (p)
855 /* Because the past-the-end bit in in_use_p is always set, we
856 pretend there is one additional object. */
857 num_objects = OBJECTS_PER_PAGE (p->order) + 1;
859 /* Reset the free object count. */
860 p->num_free_objects = num_objects;
862 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
864 i < DIV_ROUND_UP (BITMAP_SIZE (num_objects),
865 sizeof (*p->in_use_p));
870 /* Something is in use if it is marked, or if it was in use in a
871 context further down the context stack. */
872 p->in_use_p[i] |= p->save_in_use_p[i];
874 /* Decrement the free object count for every object allocated. */
875 for (j = p->in_use_p[i]; j; j >>= 1)
876 p->num_free_objects -= (j & 1);
879 if (p->num_free_objects >= num_objects)
883 /* Decrement the `GC context'. All objects allocated since the
884 previous ggc_push_context are migrated to the outer context. */
889 unsigned order, depth;
891 depth = --G.context_depth;
893 /* Any remaining pages in the popped context are lowered to the new
894 current context; i.e. objects allocated in the popped context and
895 left over are imported into the previous context. */
896 for (order = 2; order < HOST_BITS_PER_PTR; order++)
900 for (p = G.pages[order]; p != NULL; p = p->next)
902 if (p->context_depth > depth)
903 p->context_depth = depth;
905 /* If this page is now in the topmost context, and we'd
906 saved its allocation state, restore it. */
907 else if (p->context_depth == depth && p->save_in_use_p)
909 ggc_recalculate_in_use_p (p);
910 free (p->save_in_use_p);
911 p->save_in_use_p = 0;
917 /* Unmark all objects. */
924 for (order = 2; order < HOST_BITS_PER_PTR; order++)
926 size_t num_objects = OBJECTS_PER_PAGE (order);
927 size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
930 for (p = G.pages[order]; p != NULL; p = p->next)
932 #ifdef ENABLE_CHECKING
933 /* The data should be page-aligned. */
934 if ((size_t) p->page & (G.pagesize - 1))
938 /* Pages that aren't in the topmost context are not collected;
939 nevertheless, we need their in-use bit vectors to store GC
940 marks. So, back them up first. */
941 if (p->context_depth < G.context_depth)
943 if (! p->save_in_use_p)
944 p->save_in_use_p = xmalloc (bitmap_size);
945 memcpy (p->save_in_use_p, p->in_use_p, bitmap_size);
948 /* Reset reset the number of free objects and clear the
949 in-use bits. These will be adjusted by mark_obj. */
950 p->num_free_objects = num_objects;
951 memset (p->in_use_p, 0, bitmap_size);
953 /* Make sure the one-past-the-end bit is always set. */
954 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
955 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
960 /* Free all empty pages. Partially empty pages need no attention
961 because the `mark' bit doubles as an `unused' bit. */
968 for (order = 2; order < HOST_BITS_PER_PTR; order++)
970 /* The last page-entry to consider, regardless of entries
971 placed at the end of the list. */
972 page_entry * const last = G.page_tails[order];
974 size_t num_objects = OBJECTS_PER_PAGE (order);
975 page_entry *p, *previous;
985 page_entry *next = p->next;
987 /* Loop until all entries have been examined. */
990 /* Only objects on pages in the topmost context should get
992 if (p->context_depth < G.context_depth)
995 /* Remove the page if it's empty. */
996 else if (p->num_free_objects == num_objects)
999 G.pages[order] = next;
1001 previous->next = next;
1003 /* Are we removing the last element? */
1004 if (p == G.page_tails[order])
1005 G.page_tails[order] = previous;
1010 /* If the page is full, move it to the end. */
1011 else if (p->num_free_objects == 0)
1013 /* Don't move it if it's already at the end. */
1014 if (p != G.page_tails[order])
1016 /* Move p to the end of the list. */
1018 G.page_tails[order]->next = p;
1020 /* Update the tail pointer... */
1021 G.page_tails[order] = p;
1023 /* ... and the head pointer, if necessary. */
1025 G.pages[order] = next;
1027 previous->next = next;
1032 /* If we've fallen through to here, it's a page in the
1033 topmost context that is neither full nor empty. Such a
1034 page must precede pages at lesser context depth in the
1035 list, so move it to the head. */
1036 else if (p != G.pages[order])
1038 previous->next = p->next;
1039 p->next = G.pages[order];
1041 /* Are we moving the last element? */
1042 if (G.page_tails[order] == p)
1043 G.page_tails[order] = previous;
1052 /* Now, restore the in_use_p vectors for any pages from contexts
1053 other than the current one. */
1054 for (p = G.pages[order]; p; p = p->next)
1055 if (p->context_depth != G.context_depth)
1056 ggc_recalculate_in_use_p (p);
1061 /* Clobber all free objects. */
1068 for (order = 2; order < HOST_BITS_PER_PTR; order++)
1070 size_t num_objects = OBJECTS_PER_PAGE (order);
1071 size_t size = (size_t) 1 << order;
1074 for (p = G.pages[order]; p != NULL; p = p->next)
1078 if (p->context_depth != G.context_depth)
1079 /* Since we don't do any collection for pages in pushed
1080 contexts, there's no need to do any poisoning. And
1081 besides, the IN_USE_P array isn't valid until we pop
1085 for (i = 0; i < num_objects; i++)
1088 word = i / HOST_BITS_PER_LONG;
1089 bit = i % HOST_BITS_PER_LONG;
1090 if (((p->in_use_p[word] >> bit) & 1) == 0)
1091 memset (p->page + i * size, 0xa5, size);
1098 /* Top level mark-and-sweep routine. */
1103 /* Avoid frequent unnecessary work by skipping collection if the
1104 total allocations haven't expanded much since the last
1106 #ifndef GGC_ALWAYS_COLLECT
1107 if (G.allocated < GGC_MIN_EXPAND_FOR_GC * G.allocated_last_gc)
1111 timevar_push (TV_GC);
1113 fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
1115 /* Zero the total allocated bytes. We'll reaccumulate this while
1119 /* Release the pages we freed the last time we collected, but didn't
1120 reuse in the interim. */
1132 G.allocated_last_gc = G.allocated;
1133 if (G.allocated_last_gc < GGC_MIN_LAST_ALLOCATED)
1134 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
1136 timevar_pop (TV_GC);
1139 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
1142 /* Print allocation statistics. */
1145 ggc_page_print_statistics ()
1147 struct ggc_statistics stats;
1150 /* Clear the statistics. */
1151 memset (&stats, 0, sizeof (stats));
1153 /* Make sure collection will really occur. */
1154 G.allocated_last_gc = 0;
1156 /* Collect and print the statistics common across collectors. */
1157 ggc_print_statistics (stderr, &stats);
1159 /* Release free pages so that we will not count the bytes allocated
1160 there as part of the total allocated memory. */
1163 /* Collect some information about the various sizes of
1165 fprintf (stderr, "\n%-4s%-16s%-16s\n", "Log", "Allocated", "Used");
1166 for (i = 0; i < HOST_BITS_PER_PTR; ++i)
1172 /* Skip empty entries. */
1176 allocated = in_use = 0;
1178 /* Figure out the total number of bytes allocated for objects of
1179 this size, and how many of them are actually in use. */
1180 for (p = G.pages[i]; p; p = p->next)
1182 allocated += p->bytes;
1184 (OBJECTS_PER_PAGE (i) - p->num_free_objects) * (1 << i);
1186 fprintf (stderr, "%-3d %-15lu %-15lu\n", i,
1187 (unsigned long) allocated, (unsigned long) in_use);
1190 /* Print out some global information. */
1191 fprintf (stderr, "\nTotal bytes marked: %lu\n",
1192 (unsigned long) G.allocated);
1193 fprintf (stderr, "Total bytes mapped: %lu\n",
1194 (unsigned long) G.bytes_mapped);