* genattrtab.c (simplify_cond): Make TESTS an array of rtxs, instead
[official-gcc.git] / gcc / ggc-page.c
blob226db4b7466d1eb47f7dfd8c8377c49b4844117c
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 "varray.h"
26 #include "flags.h"
27 #include "ggc.h"
29 #include <sys/mman.h>
32 /* Stategy:
34 This garbage-collecting allocator allocates objects on one of a set
35 of pages. Each page can allocate objects of a single size only;
36 available sizes are powers of two starting at four bytes. The size
37 of an allocation request is rounded up to the next power of two
38 (`order'), and satisfied from the appropriate page.
40 Each page is recorded in a page-entry, which also maintains an
41 in-use bitmap of object positions on the page. This allows the
42 allocation state of a particular object to be flipped without
43 touching the page itself.
45 Each page-entry also has a context depth, which is used to track
46 pushing and popping of allocation contexts. Only objects allocated
47 in the current (highest-numbered) context may be collected.
49 Page entries are arranged in an array of singly-linked lists. The
50 array is indexed by the allocation size, in bits, of the pages on
51 it; i.e. all pages on a list allocate objects of the same size.
52 Pages are ordered on the list such that all non-full pages precede
53 all full pages, with non-full pages arranged in order of decreasing
54 context depth.
56 Empty pages (of all orders) are kept on a single page cache list,
57 and are considered first when new pages are required; they are
58 deallocated at the start of the next collection if they haven't
59 been recycled by then. */
62 /* Define GGC_POISON to poison memory marked unused by the collector. */
63 #undef GGC_POISON
65 /* Define GGC_ALWAYS_COLLECT to perform collection every time
66 ggc_collect is invoked. Otherwise, collection is performed only
67 when a significant amount of memory has been allocated since the
68 last collection. */
69 #undef GGC_ALWAYS_COLLECT.
71 /* If ENABLE_CHECKING is defined, enable GGC_POISON and
72 GGC_ALWAYS_COLLECT automatically. */
73 #ifdef ENABLE_CHECKING
74 #define GGC_POISON
75 #define GGC_ALWAYS_COLLECT
76 #endif
78 /* Define GGC_DEBUG_LEVEL to print debugging information.
79 0: No debugging output.
80 1: GC statistics only.
81 2: Page-entry allocations/deallocations as well.
82 3: Object allocations as well.
83 4: Object marks as well. */
84 #define GGC_DEBUG_LEVEL (0)
86 #ifndef HOST_BITS_PER_PTR
87 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
88 #endif
90 /* Timing information for collect execution goes into here. */
91 extern int gc_time;
93 /* The "" allocated string. */
94 char *empty_string;
96 /* A two-level tree is used to look up the page-entry for a given
97 pointer. Two chunks of the pointer's bits are extracted to index
98 the first and second levels of the tree, as follows:
100 HOST_PAGE_SIZE_BITS
101 32 | |
102 msb +----------------+----+------+------+ lsb
103 | | |
104 PAGE_L1_BITS |
106 PAGE_L2_BITS
108 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
109 pages are aligned on system page boundaries. The next most
110 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
111 index values in the lookup table, respectively.
113 The topmost leftover bits, if any, are ignored. For 32-bit
114 architectures and the settings below, there are no leftover bits.
115 For architectures with wider pointers, the lookup tree points to a
116 list of pages, which must be scanned to find the correct one. */
118 #define PAGE_L1_BITS (8)
119 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
120 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
121 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
123 #define LOOKUP_L1(p) \
124 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
126 #define LOOKUP_L2(p) \
127 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
130 /* A page_entry records the status of an allocation page. This
131 structure is dynamically sized to fit the bitmap in_use_p. */
132 typedef struct page_entry
134 /* The next page-entry with objects of the same size, or NULL if
135 this is the last page-entry. */
136 struct page_entry *next;
138 /* The number of bytes allocated. (This will always be a multiple
139 of the host system page size.) */
140 size_t bytes;
142 /* The address at which the memory is allocated. */
143 char *page;
145 /* Saved in-use bit vector for pages that aren't in the topmost
146 context during collection. */
147 unsigned long *save_in_use_p;
149 /* Context depth of this page. */
150 unsigned char context_depth;
152 /* The lg of size of objects allocated from this page. */
153 unsigned char order;
155 /* The number of free objects remaining on this page. */
156 unsigned short num_free_objects;
158 /* A likely candidate for the bit position of a free object for the
159 next allocation from this page. */
160 unsigned short next_bit_hint;
162 /* Saved number of free objects for pages that aren't in the topmost
163 context during colleciton. */
164 unsigned short save_num_free_objects;
166 /* A bit vector indicating whether or not objects are in use. The
167 Nth bit is one if the Nth object on this page is allocated. This
168 array is dynamically sized. */
169 unsigned long in_use_p[1];
170 } page_entry;
173 #if HOST_BITS_PER_PTR <= 32
175 /* On 32-bit hosts, we use a two level page table, as pictured above. */
176 typedef page_entry **page_table[PAGE_L1_SIZE];
178 #else
180 /* On 64-bit hosts, we use two level page tables plus a linked list
181 that disambiguates the top 32-bits. There will almost always be
182 exactly one entry in the list. */
183 typedef struct page_table_chain
185 struct page_table_chain *next;
186 size_t high_bits;
187 page_entry **table[PAGE_L1_SIZE];
188 } *page_table;
190 #endif
192 /* The rest of the global variables. */
193 static struct globals
195 /* The Nth element in this array is a page with objects of size 2^N.
196 If there are any pages with free objects, they will be at the
197 head of the list. NULL if there are no page-entries for this
198 object size. */
199 page_entry *pages[HOST_BITS_PER_PTR];
201 /* The Nth element in this array is the last page with objects of
202 size 2^N. NULL if there are no page-entries for this object
203 size. */
204 page_entry *page_tails[HOST_BITS_PER_PTR];
206 /* Lookup table for associating allocation pages with object addresses. */
207 page_table lookup;
209 /* The system's page size. */
210 size_t pagesize;
211 size_t lg_pagesize;
213 /* Bytes currently allocated. */
214 size_t allocated;
216 /* Bytes currently allocated at the end of the last collection. */
217 size_t allocated_last_gc;
219 /* The current depth in the context stack. */
220 unsigned char context_depth;
222 /* A file descriptor open to /dev/zero for reading. */
223 #ifndef MAP_ANONYMOUS
224 int dev_zero_fd;
225 #endif
227 /* A cache of free system pages. */
228 page_entry *free_pages;
230 /* The file descriptor for debugging output. */
231 FILE *debug_file;
232 } G;
235 /* Compute DIVIDEND / DIVISOR, rounded up. */
236 #define DIV_ROUND_UP(Dividend, Divisor) \
237 ((Dividend + Divisor - 1) / Divisor)
239 /* The number of objects per allocation page, for objects of size
240 2^ORDER. */
241 #define OBJECTS_PER_PAGE(Order) \
242 ((Order) >= G.lg_pagesize ? 1 : G.pagesize / ((size_t)1 << (Order)))
244 /* The size in bytes required to maintain a bitmap for the objects
245 on a page-entry. */
246 #define BITMAP_SIZE(Num_objects) \
247 (DIV_ROUND_UP ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
249 /* Skip garbage collection if the current allocation is not at least
250 this factor times the allocation at the end of the last collection.
251 In other words, total allocation must expand by (this factor minus
252 one) before collection is performed. */
253 #define GGC_MIN_EXPAND_FOR_GC (1.3)
255 /* Bound `allocated_last_gc' to 4MB, to prevent the memory expansion
256 test from triggering too often when the heap is small. */
257 #define GGC_MIN_LAST_ALLOCATED (4 * 1024 * 1024)
260 static page_entry *lookup_page_table_entry PROTO ((void *));
261 static void set_page_table_entry PROTO ((void *, page_entry *));
262 static char *alloc_anon PROTO ((char *, size_t));
263 static struct page_entry * alloc_page PROTO ((unsigned));
264 static void free_page PROTO ((struct page_entry *));
265 static void release_pages PROTO ((void));
266 static void *alloc_obj PROTO ((size_t, int));
267 static int mark_obj PROTO ((void *));
268 static void clear_marks PROTO ((void));
269 static void sweep_pages PROTO ((void));
271 #ifdef GGC_POISON
272 static void poison PROTO ((void *, size_t));
273 static void poison_pages PROTO ((void));
274 #endif
276 void debug_print_page_list PROTO ((int));
278 /* Traverse the page table and find the entry for a page.
279 Die (probably) if the object wasn't allocated via GC. */
281 static inline page_entry *
282 lookup_page_table_entry(p)
283 void *p;
285 page_entry ***base;
286 size_t L1, L2;
288 #if HOST_BITS_PER_PTR <= 32
289 base = &G.lookup[0];
290 #else
291 page_table table = G.lookup;
292 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
293 while (table->high_bits != high_bits)
294 table = table->next;
295 base = &table->table[0];
296 #endif
298 /* Extract the level 1 and 2 indicies. */
299 L1 = LOOKUP_L1 (p);
300 L2 = LOOKUP_L2 (p);
302 return base[L1][L2];
306 /* Set the page table entry for a page. */
307 static void
308 set_page_table_entry(p, entry)
309 void *p;
310 page_entry *entry;
312 page_entry ***base;
313 size_t L1, L2;
315 #if HOST_BITS_PER_PTR <= 32
316 base = &G.lookup[0];
317 #else
318 page_table table;
319 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
320 for (table = G.lookup; table; table = table->next)
321 if (table->high_bits == high_bits)
322 goto found;
324 /* Not found -- allocate a new table. */
325 table = (page_table) xcalloc (1, sizeof(*table));
326 table->next = G.lookup;
327 table->high_bits = high_bits;
328 G.lookup = table;
329 found:
330 base = &table->table[0];
331 #endif
333 /* Extract the level 1 and 2 indicies. */
334 L1 = LOOKUP_L1 (p);
335 L2 = LOOKUP_L2 (p);
337 if (base[L1] == NULL)
338 base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
340 base[L1][L2] = entry;
344 /* Prints the page-entry for object size ORDER, for debugging. */
345 void
346 debug_print_page_list (order)
347 int order;
349 page_entry *p;
350 printf ("Head=%p, Tail=%p:\n", G.pages[order], G.page_tails[order]);
351 p = G.pages[order];
352 while (p != NULL)
354 printf ("%p(%1d|%3d) -> ", p, p->context_depth, p->num_free_objects);
355 p = p->next;
357 printf ("NULL\n");
358 fflush (stdout);
361 #ifdef GGC_POISON
362 /* `Poisons' the region of memory starting at START and extending for
363 LEN bytes. */
364 static inline void
365 poison (start, len)
366 void *start;
367 size_t len;
369 memset (start, 0xa5, len);
371 #endif
373 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
374 (if non-null). */
375 static inline char *
376 alloc_anon (pref, size)
377 char *pref;
378 size_t size;
380 char *page;
382 #ifdef MAP_ANONYMOUS
383 page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
384 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
385 #else
386 page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
387 MAP_PRIVATE, G.dev_zero_fd, 0);
388 #endif
389 if (page == (char *) MAP_FAILED)
391 fputs ("Virtual memory exhausted!\n", stderr);
392 exit(1);
395 return page;
398 /* Allocate a new page for allocating objects of size 2^ORDER,
399 and return an entry for it. The entry is not added to the
400 appropriate page_table list. */
401 static inline struct page_entry *
402 alloc_page (order)
403 unsigned order;
405 struct page_entry *entry, *p, **pp;
406 char *page;
407 size_t num_objects;
408 size_t bitmap_size;
409 size_t page_entry_size;
410 size_t entry_size;
412 num_objects = OBJECTS_PER_PAGE (order);
413 bitmap_size = BITMAP_SIZE (num_objects + 1);
414 page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
415 entry_size = num_objects * (1 << order);
417 entry = NULL;
418 page = NULL;
420 /* Check the list of free pages for one we can use. */
421 for (pp = &G.free_pages, p = *pp; p ; pp = &p->next, p = *pp)
422 if (p->bytes == entry_size)
423 break;
425 if (p != NULL)
427 /* Recycle the allocated memory from this page ... */
428 *pp = p->next;
429 page = p->page;
430 /* ... and, if possible, the page entry itself. */
431 if (p->order == order)
433 entry = p;
434 memset (entry, 0, page_entry_size);
436 else
437 free (p);
439 else
441 /* Actually allocate the memory, using mmap. */
442 page = alloc_anon (NULL, entry_size);
445 if (entry == NULL)
446 entry = (struct page_entry *) xcalloc (1, page_entry_size);
448 entry->bytes = entry_size;
449 entry->page = page;
450 entry->context_depth = G.context_depth;
451 entry->order = order;
452 entry->num_free_objects = num_objects;
453 entry->next_bit_hint = 1;
455 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
456 increment the hint. */
457 entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
458 = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
460 set_page_table_entry (page, entry);
462 if (GGC_DEBUG_LEVEL >= 2)
463 fprintf (G.debug_file,
464 "Allocating page at %p, object size=%d, data %p-%p\n", entry,
465 1 << order, page, page + entry_size - 1);
467 return entry;
471 /* Free a page when it's no longer needed. */
472 static inline void
473 free_page (entry)
474 page_entry *entry;
476 if (GGC_DEBUG_LEVEL >= 2)
477 fprintf (G.debug_file,
478 "Deallocating page at %p, data %p-%p\n", entry,
479 entry->page, entry->page + entry->bytes - 1);
481 set_page_table_entry (entry->page, NULL);
483 entry->next = G.free_pages;
484 G.free_pages = entry;
488 /* Release the page cache to the system. */
489 static inline void
490 release_pages ()
492 page_entry *p, *next;
493 char *start;
494 size_t len;
496 p = G.free_pages;
497 if (p == NULL)
498 return;
500 next = p->next;
501 start = p->page;
502 len = p->bytes;
503 free (p);
504 p = next;
506 while (p)
508 next = p->next;
509 /* Gather up adjacent pages so they are unmapped together. */
510 if (p->page == start + len)
511 len += p->bytes;
512 else
514 munmap (start, len);
515 start = p->page;
516 len = p->bytes;
518 free (p);
519 p = next;
522 munmap (start, len);
523 G.free_pages = NULL;
527 /* This table provides a fast way to determine ceil(log_2(size)) for
528 allocation requests. The minimum allocation size is four bytes. */
529 static unsigned char const size_lookup[257] =
531 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
532 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
533 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
534 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
535 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
536 7, 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, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
540 8, 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,
550 /* Allocate a chunk of memory of SIZE bytes. If ZERO is non-zero, the
551 memory is zeroed; otherwise, its contents are undefined. */
552 static void *
553 alloc_obj (size, zero)
554 size_t size;
555 int zero;
557 unsigned order, word, bit, object_offset;
558 struct page_entry *entry;
559 void *result;
561 if (size <= 256)
562 order = size_lookup[size];
563 else
565 order = 9;
566 while (size > ((size_t) 1 << order))
567 order++;
570 /* If there are non-full pages for this size allocation, they are at
571 the head of the list. */
572 entry = G.pages[order];
574 /* If there is no page for this object size, or all pages in this
575 context are full, allocate a new page. */
576 if (entry == NULL
577 || entry->num_free_objects == 0
578 || entry->context_depth != G.context_depth)
580 struct page_entry *new_entry;
581 new_entry = alloc_page (order);
583 /* If this is the only entry, it's also the tail. */
584 if (entry == NULL)
585 G.page_tails[order] = new_entry;
587 /* Put new pages at the head of the page list. */
588 new_entry->next = entry;
589 entry = new_entry;
590 G.pages[order] = new_entry;
592 /* For a new page, we know the word and bit positions (in the
593 in_use bitmap) of the first available object -- they're zero. */
594 new_entry->next_bit_hint = 1;
595 word = 0;
596 bit = 0;
597 object_offset = 0;
599 else
601 /* First try to use the hint left from the previous allocation
602 to locate a clear bit in the in-use bitmap. We've made sure
603 that the one-past-the-end bit is always set, so if the hint
604 has run over, this test will fail. */
605 unsigned hint = entry->next_bit_hint;
606 word = hint / HOST_BITS_PER_LONG;
607 bit = hint % HOST_BITS_PER_LONG;
609 /* If the hint didn't work, scan the bitmap from the beginning. */
610 if ((entry->in_use_p[word] >> bit) & 1)
612 word = bit = 0;
613 while (~entry->in_use_p[word] == 0)
614 ++word;
615 while ((entry->in_use_p[word] >> bit) & 1)
616 ++bit;
617 hint = word * HOST_BITS_PER_LONG + bit;
620 /* Next time, try the next bit. */
621 entry->next_bit_hint = hint + 1;
623 object_offset = hint << order;
626 /* Set the in-use bit. */
627 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
629 /* Keep a running total of the number of free objects. If this page
630 fills up, we may have to move it to the end of the list if the
631 next page isn't full. If the next page is full, all subsequent
632 pages are full, so there's no need to move it. */
633 if (--entry->num_free_objects == 0
634 && entry->next != NULL
635 && entry->next->num_free_objects > 0)
637 G.pages[order] = entry->next;
638 entry->next = NULL;
639 G.page_tails[order]->next = entry;
640 G.page_tails[order] = entry;
643 /* Calculate the object's address. */
644 result = entry->page + object_offset;
646 #ifdef GGC_POISON
647 /* `Poison' the entire allocated object before zeroing the requested area,
648 so that bytes beyond the end, if any, will not necessarily be zero. */
649 poison (result, 1 << order);
650 #endif
651 if (zero)
652 memset (result, 0, size);
654 /* Keep track of how many bytes are being allocated. This
655 information is used in deciding when to collect. */
656 G.allocated += (size_t) 1 << order;
658 if (GGC_DEBUG_LEVEL >= 3)
659 fprintf (G.debug_file,
660 "Allocating object, requested size=%d, actual=%d at %p on %p\n",
661 (int) size, 1 << order, result, entry);
663 return result;
667 /* If P is not marked, marks it and returns 0. Otherwise returns 1.
668 P must have been allocated by the GC allocator; it mustn't point to
669 static objects, stack variables, or memory allocated with malloc. */
670 static int
671 mark_obj (p)
672 void *p;
674 page_entry *entry;
675 unsigned bit, word;
676 unsigned long mask;
678 /* Look up the page on which the object is alloced. If the object
679 wasn't allocated by the collector, we'll probably die. */
680 entry = lookup_page_table_entry(p);
681 #ifdef ENABLE_CHECKING
682 if (entry == NULL)
683 abort ();
684 #endif
686 /* Calculate the index of the object on the page; this is its bit
687 position in the in_use_p bitmap. */
688 bit = (((char *) p) - entry->page) >> entry->order;
689 word = bit / HOST_BITS_PER_LONG;
690 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
692 /* If the bit was previously set, skip it. */
693 if (entry->in_use_p[word] & mask)
694 return 1;
696 /* Otherwise set it, and decrement the free object count. */
697 entry->in_use_p[word] |= mask;
698 entry->num_free_objects -= 1;
700 G.allocated += (size_t) 1 << entry->order;
702 if (GGC_DEBUG_LEVEL >= 4)
703 fprintf (G.debug_file, "Marking %p\n", p);
705 return 0;
709 /* Initialize the ggc-mmap allocator. */
710 void
711 init_ggc ()
713 G.pagesize = getpagesize();
714 G.lg_pagesize = exact_log2 (G.pagesize);
716 #ifndef MAP_ANONYMOUS
717 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
718 if (G.dev_zero_fd == -1)
719 abort ();
720 #endif
722 #if 0
723 G.debug_file = fopen ("ggc-mmap.debug", "w");
724 #else
725 G.debug_file = stdout;
726 #endif
728 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
730 empty_string = ggc_alloc_string ("", 0);
731 ggc_add_string_root (&empty_string, 1);
735 void
736 ggc_push_context ()
738 ++G.context_depth;
740 /* Die on wrap. */
741 if (G.context_depth == 0)
742 abort ();
746 void
747 ggc_pop_context ()
749 unsigned order, depth;
751 depth = --G.context_depth;
753 /* Any remaining pages in the popped context are lowered to the new
754 current context; i.e. objects allocated in the popped context and
755 left over are imported into the previous context. */
756 for (order = 2; order < HOST_BITS_PER_PTR; order++)
758 size_t num_objects = OBJECTS_PER_PAGE (order);
759 size_t bitmap_size = BITMAP_SIZE (num_objects);
761 page_entry *p;
763 for (p = G.pages[order]; p != NULL; p = p->next)
765 if (p->context_depth > depth)
767 p->context_depth = depth;
770 /* If this page is now in the topmost context, and we'd
771 saved its allocation state, restore it. */
772 else if (p->context_depth == depth && p->save_in_use_p)
774 memcpy (p->in_use_p, p->save_in_use_p, bitmap_size);
775 free (p->save_in_use_p);
776 p->save_in_use_p = 0;
777 p->num_free_objects = p->save_num_free_objects;
784 struct rtx_def *
785 ggc_alloc_rtx (nslots)
786 int nslots;
788 return (struct rtx_def *)
789 alloc_obj (sizeof (struct rtx_def) + (nslots - 1) * sizeof (rtunion), 1);
793 struct rtvec_def *
794 ggc_alloc_rtvec (nelt)
795 int nelt;
797 return (struct rtvec_def *)
798 alloc_obj (sizeof (struct rtvec_def) + (nelt - 1) * sizeof (rtunion), 1);
802 union tree_node *
803 ggc_alloc_tree (length)
804 int length;
806 return (union tree_node *) alloc_obj (length, 1);
810 char *
811 ggc_alloc_string (contents, length)
812 const char *contents;
813 int length;
815 char *string;
817 if (length < 0)
819 if (contents == NULL)
820 return NULL;
821 length = strlen (contents);
824 string = (char *) alloc_obj (length + 1, 0);
825 if (contents != NULL)
826 memcpy (string, contents, length);
827 string[length] = 0;
829 return string;
833 void *
834 ggc_alloc (size)
835 size_t size;
837 return alloc_obj (size, 0);
841 static inline void
842 clear_marks ()
844 unsigned order;
846 for (order = 2; order < HOST_BITS_PER_PTR; order++)
848 size_t num_objects = OBJECTS_PER_PAGE (order);
849 size_t bitmap_size = BITMAP_SIZE (num_objects);
850 page_entry *p;
852 for (p = G.pages[order]; p != NULL; p = p->next)
854 #ifdef ENABLE_CHECKING
855 /* The data should be page-aligned. */
856 if ((size_t) p->page & (G.pagesize - 1))
857 abort ();
858 #endif
860 /* Pages that aren't in the topmost context are not collected;
861 nevertheless, we need their in-use bit vectors to store GC
862 marks. So, back them up first. */
863 if (p->context_depth < G.context_depth
864 && ! p->save_in_use_p)
866 p->save_in_use_p = xmalloc (bitmap_size);
867 memcpy (p->save_in_use_p, p->in_use_p, bitmap_size);
868 p->save_num_free_objects = p->num_free_objects;
871 /* Reset reset the number of free objects and clear the
872 in-use bits. These will be adjusted by mark_obj. */
873 p->num_free_objects = num_objects;
874 memset (p->in_use_p, 0, bitmap_size);
876 /* Make sure the one-past-the-end bit is always set. */
877 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
878 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
883 static inline void
884 sweep_pages ()
886 unsigned order;
888 for (order = 2; order < HOST_BITS_PER_PTR; order++)
890 /* The last page-entry to consider, regardless of entries
891 placed at the end of the list. */
892 page_entry * const last = G.page_tails[order];
894 size_t num_objects = OBJECTS_PER_PAGE (order);
895 page_entry *p, *previous;
896 int done;
898 p = G.pages[order];
899 if (p == NULL)
900 continue;
902 previous = NULL;
905 page_entry *next = p->next;
907 /* Loop until all entries have been examined. */
908 done = (p == last);
910 /* Only objects on pages in the topmost context should get
911 collected. */
912 if (p->context_depth < G.context_depth)
915 /* Remove the page if it's empty. */
916 else if (p->num_free_objects == num_objects)
918 if (! previous)
919 G.pages[order] = next;
920 else
921 previous->next = next;
923 /* Are we removing the last element? */
924 if (p == G.page_tails[order])
925 G.page_tails[order] = previous;
926 free_page (p);
927 p = previous;
930 /* If the page is full, move it to the end. */
931 else if (p->num_free_objects == 0)
933 /* Don't move it if it's already at the end. */
934 if (p != G.page_tails[order])
936 /* Move p to the end of the list. */
937 p->next = NULL;
938 G.page_tails[order]->next = p;
940 /* Update the tail pointer... */
941 G.page_tails[order] = p;
943 /* ... and the head pointer, if necessary. */
944 if (! previous)
945 G.pages[order] = next;
946 else
947 previous->next = next;
948 p = previous;
952 /* If we've fallen through to here, it's a page in the
953 topmost context that is neither full nor empty. Such a
954 page must precede pages at lesser context depth in the
955 list, so move it to the head. */
956 else if (p != G.pages[order])
958 previous->next = p->next;
959 p->next = G.pages[order];
960 G.pages[order] = p;
961 /* Are we moving the last element? */
962 if (G.page_tails[order] == p)
963 G.page_tails[order] = previous;
964 p = previous;
967 previous = p;
968 p = next;
970 while (! done);
974 #ifdef GGC_POISON
975 static inline void
976 poison_pages ()
978 unsigned order;
980 for (order = 2; order < HOST_BITS_PER_PTR; order++)
982 size_t num_objects = OBJECTS_PER_PAGE (order);
983 size_t size = (size_t) 1 << order;
984 page_entry *p;
986 for (p = G.pages[order]; p != NULL; p = p->next)
988 size_t i;
989 for (i = 0; i < num_objects; i++)
991 size_t word, bit;
992 word = i / HOST_BITS_PER_LONG;
993 bit = i % HOST_BITS_PER_LONG;
994 if (((p->in_use_p[word] >> bit) & 1) == 0)
995 poison (p->page + i * size, size);
1000 #endif
1002 void
1003 ggc_collect ()
1005 int time;
1007 /* Avoid frequent unnecessary work by skipping collection if the
1008 total allocations haven't expanded much since the last
1009 collection. */
1010 #ifndef GGC_ALWAYS_COLLECT
1011 if (G.allocated < GGC_MIN_EXPAND_FOR_GC * G.allocated_last_gc)
1012 return;
1013 #endif
1015 time = get_run_time ();
1016 if (!quiet_flag)
1017 fprintf (stderr, " {GC %luk -> ", (unsigned long)G.allocated / 1024);
1019 /* Zero the total allocated bytes. We'll reaccumulate this while
1020 marking. */
1021 G.allocated = 0;
1023 /* Release the pages we freed the last time we collected, but didn't
1024 reuse in the interim. */
1025 release_pages ();
1027 clear_marks ();
1028 ggc_mark_roots ();
1029 sweep_pages ();
1031 #ifdef GGC_POISON
1032 poison_pages ();
1033 #endif
1035 G.allocated_last_gc = G.allocated;
1036 if (G.allocated_last_gc < GGC_MIN_LAST_ALLOCATED)
1037 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
1039 time = get_run_time () - time;
1040 gc_time += time;
1042 time = (time + 500) / 1000;
1043 if (!quiet_flag)
1044 fprintf (stderr, "%luk in %d.%03d}",
1045 (unsigned long) G.allocated / 1024, time / 1000, time % 1000);
1050 ggc_set_mark_rtx (r)
1051 rtx r;
1053 return mark_obj (r);
1057 ggc_set_mark_rtvec (v)
1058 rtvec v;
1060 return mark_obj (v);
1064 ggc_set_mark_tree (t)
1065 tree t;
1067 return mark_obj (t);
1070 void
1071 ggc_mark_string (s)
1072 char *s;
1074 if (s)
1075 mark_obj (s);
1078 void
1079 ggc_mark (p)
1080 void *p;
1082 if (p)
1083 mark_obj (p);