* friend.c (make_friend_class): Robustify.
[official-gcc.git] / gcc / ggc-page.c
blob6b8e46846645780696805d60576d31c6d24593bd
1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999 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)
9 any later version.
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. */
21 #include "config.h"
22 #include "system.h"
23 #include "tree.h"
24 #include "rtl.h"
25 #include "tm_p.h"
26 #include "varray.h"
27 #include "flags.h"
28 #include "ggc.h"
30 #include <sys/mman.h>
33 /* Stategy:
35 This garbage-collecting allocator allocates objects on one of a set
36 of pages. Each page can allocate objects of a single size only;
37 available sizes are powers of two starting at four bytes. The size
38 of an allocation request is rounded up to the next power of two
39 (`order'), and satisfied from the appropriate page.
41 Each page is recorded in a page-entry, which also maintains an
42 in-use bitmap of object positions on the page. This allows the
43 allocation state of a particular object to be flipped without
44 touching the page itself.
46 Each page-entry also has a context depth, which is used to track
47 pushing and popping of allocation contexts. Only objects allocated
48 in the current (highest-numbered) context may be collected.
50 Page entries are arranged in an array of singly-linked lists. The
51 array is indexed by the allocation size, in bits, of the pages on
52 it; i.e. all pages on a list allocate objects of the same size.
53 Pages are ordered on the list such that all non-full pages precede
54 all full pages, with non-full pages arranged in order of decreasing
55 context depth.
57 Empty pages (of all orders) are kept on a single page cache list,
58 and are considered first when new pages are required; they are
59 deallocated at the start of the next collection if they haven't
60 been recycled by then. */
63 /* Define GGC_POISON to poison memory marked unused by the collector. */
64 #undef GGC_POISON
66 /* Define GGC_ALWAYS_COLLECT to perform collection every time
67 ggc_collect is invoked. Otherwise, collection is performed only
68 when a significant amount of memory has been allocated since the
69 last collection. */
70 #undef GGC_ALWAYS_COLLECT
72 /* If ENABLE_CHECKING is defined, enable GGC_POISON and
73 GGC_ALWAYS_COLLECT automatically. */
74 #ifdef ENABLE_CHECKING
75 #define GGC_POISON
76 #define GGC_ALWAYS_COLLECT
77 #endif
79 /* Define GGC_DEBUG_LEVEL to print debugging information.
80 0: No debugging output.
81 1: GC statistics only.
82 2: Page-entry allocations/deallocations as well.
83 3: Object allocations as well.
84 4: Object marks as well. */
85 #define GGC_DEBUG_LEVEL (0)
87 #ifndef HOST_BITS_PER_PTR
88 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
89 #endif
91 /* Timing information for collect execution goes into here. */
92 extern int gc_time;
94 /* The "" allocated string. */
95 char *empty_string;
97 /* A two-level tree is used to look up the page-entry for a given
98 pointer. Two chunks of the pointer's bits are extracted to index
99 the first and second levels of the tree, as follows:
101 HOST_PAGE_SIZE_BITS
102 32 | |
103 msb +----------------+----+------+------+ lsb
104 | | |
105 PAGE_L1_BITS |
107 PAGE_L2_BITS
109 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
110 pages are aligned on system page boundaries. The next most
111 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
112 index values in the lookup table, respectively.
114 The topmost leftover bits, if any, are ignored. For 32-bit
115 architectures and the settings below, there are no leftover bits.
116 For architectures with wider pointers, the lookup tree points to a
117 list of pages, which must be scanned to find the correct one. */
119 #define PAGE_L1_BITS (8)
120 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
121 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
122 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
124 #define LOOKUP_L1(p) \
125 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
127 #define LOOKUP_L2(p) \
128 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
131 /* A page_entry records the status of an allocation page. This
132 structure is dynamically sized to fit the bitmap in_use_p. */
133 typedef struct page_entry
135 /* The next page-entry with objects of the same size, or NULL if
136 this is the last page-entry. */
137 struct page_entry *next;
139 /* The number of bytes allocated. (This will always be a multiple
140 of the host system page size.) */
141 size_t bytes;
143 /* The address at which the memory is allocated. */
144 char *page;
146 /* Saved in-use bit vector for pages that aren't in the topmost
147 context during collection. */
148 unsigned long *save_in_use_p;
150 /* Context depth of this page. */
151 unsigned char context_depth;
153 /* The lg of size of objects allocated from this page. */
154 unsigned char order;
156 /* The number of free objects remaining on this page. */
157 unsigned short num_free_objects;
159 /* A likely candidate for the bit position of a free object for the
160 next allocation from this page. */
161 unsigned short next_bit_hint;
163 /* Saved number of free objects for pages that aren't in the topmost
164 context during colleciton. */
165 unsigned short save_num_free_objects;
167 /* A bit vector indicating whether or not objects are in use. The
168 Nth bit is one if the Nth object on this page is allocated. This
169 array is dynamically sized. */
170 unsigned long in_use_p[1];
171 } page_entry;
174 #if HOST_BITS_PER_PTR <= 32
176 /* On 32-bit hosts, we use a two level page table, as pictured above. */
177 typedef page_entry **page_table[PAGE_L1_SIZE];
179 #else
181 /* On 64-bit hosts, we use two level page tables plus a linked list
182 that disambiguates the top 32-bits. There will almost always be
183 exactly one entry in the list. */
184 typedef struct page_table_chain
186 struct page_table_chain *next;
187 size_t high_bits;
188 page_entry **table[PAGE_L1_SIZE];
189 } *page_table;
191 #endif
193 /* The rest of the global variables. */
194 static struct globals
196 /* The Nth element in this array is a page with objects of size 2^N.
197 If there are any pages with free objects, they will be at the
198 head of the list. NULL if there are no page-entries for this
199 object size. */
200 page_entry *pages[HOST_BITS_PER_PTR];
202 /* The Nth element in this array is the last page with objects of
203 size 2^N. NULL if there are no page-entries for this object
204 size. */
205 page_entry *page_tails[HOST_BITS_PER_PTR];
207 /* Lookup table for associating allocation pages with object addresses. */
208 page_table lookup;
210 /* The system's page size. */
211 size_t pagesize;
212 size_t lg_pagesize;
214 /* Bytes currently allocated. */
215 size_t allocated;
217 /* Bytes currently allocated at the end of the last collection. */
218 size_t allocated_last_gc;
220 /* The current depth in the context stack. */
221 unsigned char context_depth;
223 /* A file descriptor open to /dev/zero for reading. */
224 #ifndef MAP_ANONYMOUS
225 int dev_zero_fd;
226 #endif
228 /* A cache of free system pages. */
229 page_entry *free_pages;
231 /* The file descriptor for debugging output. */
232 FILE *debug_file;
233 } G;
236 /* Compute DIVIDEND / DIVISOR, rounded up. */
237 #define DIV_ROUND_UP(Dividend, Divisor) \
238 ((Dividend + Divisor - 1) / Divisor)
240 /* The number of objects per allocation page, for objects of size
241 2^ORDER. */
242 #define OBJECTS_PER_PAGE(Order) \
243 ((Order) >= G.lg_pagesize ? 1 : G.pagesize / ((size_t)1 << (Order)))
245 /* The size in bytes required to maintain a bitmap for the objects
246 on a page-entry. */
247 #define BITMAP_SIZE(Num_objects) \
248 (DIV_ROUND_UP ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
250 /* Skip garbage collection if the current allocation is not at least
251 this factor times the allocation at the end of the last collection.
252 In other words, total allocation must expand by (this factor minus
253 one) before collection is performed. */
254 #define GGC_MIN_EXPAND_FOR_GC (1.3)
256 /* Bound `allocated_last_gc' to 4MB, to prevent the memory expansion
257 test from triggering too often when the heap is small. */
258 #define GGC_MIN_LAST_ALLOCATED (4 * 1024 * 1024)
261 static page_entry *lookup_page_table_entry PROTO ((void *));
262 static void set_page_table_entry PROTO ((void *, page_entry *));
263 static char *alloc_anon PROTO ((char *, size_t));
264 static struct page_entry * alloc_page PROTO ((unsigned));
265 static void free_page PROTO ((struct page_entry *));
266 static void release_pages PROTO ((void));
267 static void *alloc_obj PROTO ((size_t, int));
268 static int mark_obj PROTO ((void *));
269 static void clear_marks PROTO ((void));
270 static void sweep_pages PROTO ((void));
272 #ifdef GGC_POISON
273 static void poison PROTO ((void *, size_t));
274 static void poison_pages PROTO ((void));
275 #endif
277 void debug_print_page_list PROTO ((int));
279 /* Traverse the page table and find the entry for a page.
280 Die (probably) if the object wasn't allocated via GC. */
282 static inline page_entry *
283 lookup_page_table_entry(p)
284 void *p;
286 page_entry ***base;
287 size_t L1, L2;
289 #if HOST_BITS_PER_PTR <= 32
290 base = &G.lookup[0];
291 #else
292 page_table table = G.lookup;
293 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
294 while (table->high_bits != high_bits)
295 table = table->next;
296 base = &table->table[0];
297 #endif
299 /* Extract the level 1 and 2 indicies. */
300 L1 = LOOKUP_L1 (p);
301 L2 = LOOKUP_L2 (p);
303 return base[L1][L2];
307 /* Set the page table entry for a page. */
308 static void
309 set_page_table_entry(p, entry)
310 void *p;
311 page_entry *entry;
313 page_entry ***base;
314 size_t L1, L2;
316 #if HOST_BITS_PER_PTR <= 32
317 base = &G.lookup[0];
318 #else
319 page_table table;
320 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
321 for (table = G.lookup; table; table = table->next)
322 if (table->high_bits == high_bits)
323 goto found;
325 /* Not found -- allocate a new table. */
326 table = (page_table) xcalloc (1, sizeof(*table));
327 table->next = G.lookup;
328 table->high_bits = high_bits;
329 G.lookup = table;
330 found:
331 base = &table->table[0];
332 #endif
334 /* Extract the level 1 and 2 indicies. */
335 L1 = LOOKUP_L1 (p);
336 L2 = LOOKUP_L2 (p);
338 if (base[L1] == NULL)
339 base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
341 base[L1][L2] = entry;
345 /* Prints the page-entry for object size ORDER, for debugging. */
346 void
347 debug_print_page_list (order)
348 int order;
350 page_entry *p;
351 printf ("Head=%p, Tail=%p:\n", G.pages[order], G.page_tails[order]);
352 p = G.pages[order];
353 while (p != NULL)
355 printf ("%p(%1d|%3d) -> ", p, p->context_depth, p->num_free_objects);
356 p = p->next;
358 printf ("NULL\n");
359 fflush (stdout);
362 #ifdef GGC_POISON
363 /* `Poisons' the region of memory starting at START and extending for
364 LEN bytes. */
365 static inline void
366 poison (start, len)
367 void *start;
368 size_t len;
370 memset (start, 0xa5, len);
372 #endif
374 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
375 (if non-null). */
376 static inline char *
377 alloc_anon (pref, size)
378 char *pref;
379 size_t size;
381 char *page;
383 #ifdef MAP_ANONYMOUS
384 page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
385 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
386 #else
387 page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
388 MAP_PRIVATE, G.dev_zero_fd, 0);
389 #endif
390 if (page == (char *) MAP_FAILED)
392 fputs ("Virtual memory exhausted!\n", stderr);
393 exit(1);
396 return page;
399 /* Allocate a new page for allocating objects of size 2^ORDER,
400 and return an entry for it. The entry is not added to the
401 appropriate page_table list. */
402 static inline struct page_entry *
403 alloc_page (order)
404 unsigned order;
406 struct page_entry *entry, *p, **pp;
407 char *page;
408 size_t num_objects;
409 size_t bitmap_size;
410 size_t page_entry_size;
411 size_t entry_size;
413 num_objects = OBJECTS_PER_PAGE (order);
414 bitmap_size = BITMAP_SIZE (num_objects + 1);
415 page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
416 entry_size = num_objects * (1 << order);
418 entry = NULL;
419 page = NULL;
421 /* Check the list of free pages for one we can use. */
422 for (pp = &G.free_pages, p = *pp; p ; pp = &p->next, p = *pp)
423 if (p->bytes == entry_size)
424 break;
426 if (p != NULL)
428 /* Recycle the allocated memory from this page ... */
429 *pp = p->next;
430 page = p->page;
431 /* ... and, if possible, the page entry itself. */
432 if (p->order == order)
434 entry = p;
435 memset (entry, 0, page_entry_size);
437 else
438 free (p);
440 else
442 /* Actually allocate the memory, using mmap. */
443 page = alloc_anon (NULL, entry_size);
446 if (entry == NULL)
447 entry = (struct page_entry *) xcalloc (1, page_entry_size);
449 entry->bytes = entry_size;
450 entry->page = page;
451 entry->context_depth = G.context_depth;
452 entry->order = order;
453 entry->num_free_objects = num_objects;
454 entry->next_bit_hint = 1;
456 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
457 increment the hint. */
458 entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
459 = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
461 set_page_table_entry (page, entry);
463 if (GGC_DEBUG_LEVEL >= 2)
464 fprintf (G.debug_file,
465 "Allocating page at %p, object size=%d, data %p-%p\n", entry,
466 1 << order, page, page + entry_size - 1);
468 return entry;
472 /* Free a page when it's no longer needed. */
473 static inline void
474 free_page (entry)
475 page_entry *entry;
477 if (GGC_DEBUG_LEVEL >= 2)
478 fprintf (G.debug_file,
479 "Deallocating page at %p, data %p-%p\n", entry,
480 entry->page, entry->page + entry->bytes - 1);
482 set_page_table_entry (entry->page, NULL);
484 entry->next = G.free_pages;
485 G.free_pages = entry;
489 /* Release the page cache to the system. */
490 static inline void
491 release_pages ()
493 page_entry *p, *next;
494 char *start;
495 size_t len;
497 p = G.free_pages;
498 if (p == NULL)
499 return;
501 next = p->next;
502 start = p->page;
503 len = p->bytes;
504 free (p);
505 p = next;
507 while (p)
509 next = p->next;
510 /* Gather up adjacent pages so they are unmapped together. */
511 if (p->page == start + len)
512 len += p->bytes;
513 else
515 munmap (start, len);
516 start = p->page;
517 len = p->bytes;
519 free (p);
520 p = next;
523 munmap (start, len);
524 G.free_pages = NULL;
528 /* This table provides a fast way to determine ceil(log_2(size)) for
529 allocation requests. The minimum allocation size is four bytes. */
530 static unsigned char const size_lookup[257] =
532 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
533 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
534 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
535 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
536 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
537 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
538 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
539 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
540 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
541 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
542 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
543 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
544 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
545 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
546 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
547 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
551 /* Allocate a chunk of memory of SIZE bytes. If ZERO is non-zero, the
552 memory is zeroed; otherwise, its contents are undefined. */
553 static void *
554 alloc_obj (size, zero)
555 size_t size;
556 int zero;
558 unsigned order, word, bit, object_offset;
559 struct page_entry *entry;
560 void *result;
562 if (size <= 256)
563 order = size_lookup[size];
564 else
566 order = 9;
567 while (size > ((size_t) 1 << order))
568 order++;
571 /* If there are non-full pages for this size allocation, they are at
572 the head of the list. */
573 entry = G.pages[order];
575 /* If there is no page for this object size, or all pages in this
576 context are full, allocate a new page. */
577 if (entry == NULL
578 || entry->num_free_objects == 0
579 || entry->context_depth != G.context_depth)
581 struct page_entry *new_entry;
582 new_entry = alloc_page (order);
584 /* If this is the only entry, it's also the tail. */
585 if (entry == NULL)
586 G.page_tails[order] = new_entry;
588 /* Put new pages at the head of the page list. */
589 new_entry->next = entry;
590 entry = new_entry;
591 G.pages[order] = new_entry;
593 /* For a new page, we know the word and bit positions (in the
594 in_use bitmap) of the first available object -- they're zero. */
595 new_entry->next_bit_hint = 1;
596 word = 0;
597 bit = 0;
598 object_offset = 0;
600 else
602 /* First try to use the hint left from the previous allocation
603 to locate a clear bit in the in-use bitmap. We've made sure
604 that the one-past-the-end bit is always set, so if the hint
605 has run over, this test will fail. */
606 unsigned hint = entry->next_bit_hint;
607 word = hint / HOST_BITS_PER_LONG;
608 bit = hint % HOST_BITS_PER_LONG;
610 /* If the hint didn't work, scan the bitmap from the beginning. */
611 if ((entry->in_use_p[word] >> bit) & 1)
613 word = bit = 0;
614 while (~entry->in_use_p[word] == 0)
615 ++word;
616 while ((entry->in_use_p[word] >> bit) & 1)
617 ++bit;
618 hint = word * HOST_BITS_PER_LONG + bit;
621 /* Next time, try the next bit. */
622 entry->next_bit_hint = hint + 1;
624 object_offset = hint << order;
627 /* Set the in-use bit. */
628 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
630 /* Keep a running total of the number of free objects. If this page
631 fills up, we may have to move it to the end of the list if the
632 next page isn't full. If the next page is full, all subsequent
633 pages are full, so there's no need to move it. */
634 if (--entry->num_free_objects == 0
635 && entry->next != NULL
636 && entry->next->num_free_objects > 0)
638 G.pages[order] = entry->next;
639 entry->next = NULL;
640 G.page_tails[order]->next = entry;
641 G.page_tails[order] = entry;
644 /* Calculate the object's address. */
645 result = entry->page + object_offset;
647 #ifdef GGC_POISON
648 /* `Poison' the entire allocated object before zeroing the requested area,
649 so that bytes beyond the end, if any, will not necessarily be zero. */
650 poison (result, 1 << order);
651 #endif
652 if (zero)
653 memset (result, 0, size);
655 /* Keep track of how many bytes are being allocated. This
656 information is used in deciding when to collect. */
657 G.allocated += (size_t) 1 << order;
659 if (GGC_DEBUG_LEVEL >= 3)
660 fprintf (G.debug_file,
661 "Allocating object, requested size=%d, actual=%d at %p on %p\n",
662 (int) size, 1 << order, result, entry);
664 return result;
668 /* If P is not marked, marks it and returns 0. Otherwise returns 1.
669 P must have been allocated by the GC allocator; it mustn't point to
670 static objects, stack variables, or memory allocated with malloc. */
671 static int
672 mark_obj (p)
673 void *p;
675 page_entry *entry;
676 unsigned bit, word;
677 unsigned long mask;
679 /* Look up the page on which the object is alloced. If the object
680 wasn't allocated by the collector, we'll probably die. */
681 entry = lookup_page_table_entry(p);
682 #ifdef ENABLE_CHECKING
683 if (entry == NULL)
684 abort ();
685 #endif
687 /* Calculate the index of the object on the page; this is its bit
688 position in the in_use_p bitmap. */
689 bit = (((char *) p) - entry->page) >> entry->order;
690 word = bit / HOST_BITS_PER_LONG;
691 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
693 /* If the bit was previously set, skip it. */
694 if (entry->in_use_p[word] & mask)
695 return 1;
697 /* Otherwise set it, and decrement the free object count. */
698 entry->in_use_p[word] |= mask;
699 entry->num_free_objects -= 1;
701 G.allocated += (size_t) 1 << entry->order;
703 if (GGC_DEBUG_LEVEL >= 4)
704 fprintf (G.debug_file, "Marking %p\n", p);
706 return 0;
710 /* Initialize the ggc-mmap allocator. */
711 void
712 init_ggc ()
714 G.pagesize = getpagesize();
715 G.lg_pagesize = exact_log2 (G.pagesize);
717 #ifndef MAP_ANONYMOUS
718 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
719 if (G.dev_zero_fd == -1)
720 abort ();
721 #endif
723 #if 0
724 G.debug_file = fopen ("ggc-mmap.debug", "w");
725 #else
726 G.debug_file = stdout;
727 #endif
729 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
731 empty_string = ggc_alloc_string ("", 0);
732 ggc_add_string_root (&empty_string, 1);
736 void
737 ggc_push_context ()
739 ++G.context_depth;
741 /* Die on wrap. */
742 if (G.context_depth == 0)
743 abort ();
747 void
748 ggc_pop_context ()
750 unsigned order, depth;
752 depth = --G.context_depth;
754 /* Any remaining pages in the popped context are lowered to the new
755 current context; i.e. objects allocated in the popped context and
756 left over are imported into the previous context. */
757 for (order = 2; order < HOST_BITS_PER_PTR; order++)
759 size_t num_objects = OBJECTS_PER_PAGE (order);
760 size_t bitmap_size = BITMAP_SIZE (num_objects);
762 page_entry *p;
764 for (p = G.pages[order]; p != NULL; p = p->next)
766 if (p->context_depth > depth)
768 p->context_depth = depth;
771 /* If this page is now in the topmost context, and we'd
772 saved its allocation state, restore it. */
773 else if (p->context_depth == depth && p->save_in_use_p)
775 memcpy (p->in_use_p, p->save_in_use_p, bitmap_size);
776 free (p->save_in_use_p);
777 p->save_in_use_p = 0;
778 p->num_free_objects = p->save_num_free_objects;
785 struct rtx_def *
786 ggc_alloc_rtx (nslots)
787 int nslots;
789 return (struct rtx_def *)
790 alloc_obj (sizeof (struct rtx_def) + (nslots - 1) * sizeof (rtunion), 1);
794 struct rtvec_def *
795 ggc_alloc_rtvec (nelt)
796 int nelt;
798 return (struct rtvec_def *)
799 alloc_obj (sizeof (struct rtvec_def) + (nelt - 1) * sizeof (rtx), 1);
803 union tree_node *
804 ggc_alloc_tree (length)
805 int length;
807 return (union tree_node *) alloc_obj (length, 1);
811 char *
812 ggc_alloc_string (contents, length)
813 const char *contents;
814 int length;
816 char *string;
818 if (length < 0)
820 if (contents == NULL)
821 return NULL;
822 length = strlen (contents);
825 string = (char *) alloc_obj (length + 1, 0);
826 if (contents != NULL)
827 memcpy (string, contents, length);
828 string[length] = 0;
830 return string;
834 void *
835 ggc_alloc (size)
836 size_t size;
838 return alloc_obj (size, 0);
842 static inline void
843 clear_marks ()
845 unsigned order;
847 for (order = 2; order < HOST_BITS_PER_PTR; order++)
849 size_t num_objects = OBJECTS_PER_PAGE (order);
850 size_t bitmap_size = BITMAP_SIZE (num_objects);
851 page_entry *p;
853 for (p = G.pages[order]; p != NULL; p = p->next)
855 #ifdef ENABLE_CHECKING
856 /* The data should be page-aligned. */
857 if ((size_t) p->page & (G.pagesize - 1))
858 abort ();
859 #endif
861 /* Pages that aren't in the topmost context are not collected;
862 nevertheless, we need their in-use bit vectors to store GC
863 marks. So, back them up first. */
864 if (p->context_depth < G.context_depth
865 && ! p->save_in_use_p)
867 p->save_in_use_p = xmalloc (bitmap_size);
868 memcpy (p->save_in_use_p, p->in_use_p, bitmap_size);
869 p->save_num_free_objects = p->num_free_objects;
872 /* Reset reset the number of free objects and clear the
873 in-use bits. These will be adjusted by mark_obj. */
874 p->num_free_objects = num_objects;
875 memset (p->in_use_p, 0, bitmap_size);
877 /* Make sure the one-past-the-end bit is always set. */
878 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
879 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
884 static inline void
885 sweep_pages ()
887 unsigned order;
889 for (order = 2; order < HOST_BITS_PER_PTR; order++)
891 /* The last page-entry to consider, regardless of entries
892 placed at the end of the list. */
893 page_entry * const last = G.page_tails[order];
895 size_t num_objects = OBJECTS_PER_PAGE (order);
896 page_entry *p, *previous;
897 int done;
899 p = G.pages[order];
900 if (p == NULL)
901 continue;
903 previous = NULL;
906 page_entry *next = p->next;
908 /* Loop until all entries have been examined. */
909 done = (p == last);
911 /* Only objects on pages in the topmost context should get
912 collected. */
913 if (p->context_depth < G.context_depth)
916 /* Remove the page if it's empty. */
917 else if (p->num_free_objects == num_objects)
919 if (! previous)
920 G.pages[order] = next;
921 else
922 previous->next = next;
924 /* Are we removing the last element? */
925 if (p == G.page_tails[order])
926 G.page_tails[order] = previous;
927 free_page (p);
928 p = previous;
931 /* If the page is full, move it to the end. */
932 else if (p->num_free_objects == 0)
934 /* Don't move it if it's already at the end. */
935 if (p != G.page_tails[order])
937 /* Move p to the end of the list. */
938 p->next = NULL;
939 G.page_tails[order]->next = p;
941 /* Update the tail pointer... */
942 G.page_tails[order] = p;
944 /* ... and the head pointer, if necessary. */
945 if (! previous)
946 G.pages[order] = next;
947 else
948 previous->next = next;
949 p = previous;
953 /* If we've fallen through to here, it's a page in the
954 topmost context that is neither full nor empty. Such a
955 page must precede pages at lesser context depth in the
956 list, so move it to the head. */
957 else if (p != G.pages[order])
959 previous->next = p->next;
960 p->next = G.pages[order];
961 G.pages[order] = p;
962 /* Are we moving the last element? */
963 if (G.page_tails[order] == p)
964 G.page_tails[order] = previous;
965 p = previous;
968 previous = p;
969 p = next;
971 while (! done);
975 #ifdef GGC_POISON
976 static inline void
977 poison_pages ()
979 unsigned order;
981 for (order = 2; order < HOST_BITS_PER_PTR; order++)
983 size_t num_objects = OBJECTS_PER_PAGE (order);
984 size_t size = (size_t) 1 << order;
985 page_entry *p;
987 for (p = G.pages[order]; p != NULL; p = p->next)
989 size_t i;
990 for (i = 0; i < num_objects; i++)
992 size_t word, bit;
993 word = i / HOST_BITS_PER_LONG;
994 bit = i % HOST_BITS_PER_LONG;
995 if (((p->in_use_p[word] >> bit) & 1) == 0)
996 poison (p->page + i * size, size);
1001 #endif
1003 void
1004 ggc_collect ()
1006 int time;
1008 /* Avoid frequent unnecessary work by skipping collection if the
1009 total allocations haven't expanded much since the last
1010 collection. */
1011 #ifndef GGC_ALWAYS_COLLECT
1012 if (G.allocated < GGC_MIN_EXPAND_FOR_GC * G.allocated_last_gc)
1013 return;
1014 #endif
1016 time = get_run_time ();
1017 if (!quiet_flag)
1018 fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
1020 /* Zero the total allocated bytes. We'll reaccumulate this while
1021 marking. */
1022 G.allocated = 0;
1024 /* Release the pages we freed the last time we collected, but didn't
1025 reuse in the interim. */
1026 release_pages ();
1028 clear_marks ();
1029 ggc_mark_roots ();
1030 sweep_pages ();
1032 #ifdef GGC_POISON
1033 poison_pages ();
1034 #endif
1036 G.allocated_last_gc = G.allocated;
1037 if (G.allocated_last_gc < GGC_MIN_LAST_ALLOCATED)
1038 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
1040 time = get_run_time () - time;
1041 gc_time += time;
1043 time = (time + 500) / 1000;
1044 if (!quiet_flag)
1045 fprintf (stderr, "%luk in %d.%03d}",
1046 (unsigned long) G.allocated / 1024, time / 1000, time % 1000);
1051 ggc_set_mark_rtx (r)
1052 rtx r;
1054 return mark_obj (r);
1058 ggc_set_mark_rtvec (v)
1059 rtvec v;
1061 return mark_obj (v);
1065 ggc_set_mark_tree (t)
1066 tree t;
1068 return mark_obj (t);
1071 void
1072 ggc_mark_string (s)
1073 char *s;
1075 if (s)
1076 mark_obj (s);
1079 void
1080 ggc_mark (p)
1081 void *p;
1083 if (p)
1084 mark_obj (p);