PR libstdc++/9527, PR libstdc++/8713
[official-gcc.git] / gcc / ggc-page.c
blobe0064e7fdb2a5e9e35e0190af860e7f7f8810cf9
1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "toplev.h"
29 #include "varray.h"
30 #include "flags.h"
31 #include "ggc.h"
32 #include "timevar.h"
33 #include "params.h"
34 #ifdef ENABLE_VALGRIND_CHECKING
35 #include <valgrind.h>
36 #else
37 /* Avoid #ifdef:s when we can help it. */
38 #define VALGRIND_DISCARD(x)
39 #endif
41 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
42 file open. Prefer either to valloc. */
43 #ifdef HAVE_MMAP_ANON
44 # undef HAVE_MMAP_DEV_ZERO
46 # include <sys/mman.h>
47 # ifndef MAP_FAILED
48 # define MAP_FAILED -1
49 # endif
50 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
51 # define MAP_ANONYMOUS MAP_ANON
52 # endif
53 # define USING_MMAP
55 #endif
57 #ifdef HAVE_MMAP_DEV_ZERO
59 # include <sys/mman.h>
60 # ifndef MAP_FAILED
61 # define MAP_FAILED -1
62 # endif
63 # define USING_MMAP
65 #endif
67 #ifndef USING_MMAP
68 #define USING_MALLOC_PAGE_GROUPS
69 #endif
71 /* Stategy:
73 This garbage-collecting allocator allocates objects on one of a set
74 of pages. Each page can allocate objects of a single size only;
75 available sizes are powers of two starting at four bytes. The size
76 of an allocation request is rounded up to the next power of two
77 (`order'), and satisfied from the appropriate page.
79 Each page is recorded in a page-entry, which also maintains an
80 in-use bitmap of object positions on the page. This allows the
81 allocation state of a particular object to be flipped without
82 touching the page itself.
84 Each page-entry also has a context depth, which is used to track
85 pushing and popping of allocation contexts. Only objects allocated
86 in the current (highest-numbered) context may be collected.
88 Page entries are arranged in an array of singly-linked lists. The
89 array is indexed by the allocation size, in bits, of the pages on
90 it; i.e. all pages on a list allocate objects of the same size.
91 Pages are ordered on the list such that all non-full pages precede
92 all full pages, with non-full pages arranged in order of decreasing
93 context depth.
95 Empty pages (of all orders) are kept on a single page cache list,
96 and are considered first when new pages are required; they are
97 deallocated at the start of the next collection if they haven't
98 been recycled by then. */
100 /* Define GGC_DEBUG_LEVEL to print debugging information.
101 0: No debugging output.
102 1: GC statistics only.
103 2: Page-entry allocations/deallocations as well.
104 3: Object allocations as well.
105 4: Object marks as well. */
106 #define GGC_DEBUG_LEVEL (0)
108 #ifndef HOST_BITS_PER_PTR
109 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
110 #endif
113 /* A two-level tree is used to look up the page-entry for a given
114 pointer. Two chunks of the pointer's bits are extracted to index
115 the first and second levels of the tree, as follows:
117 HOST_PAGE_SIZE_BITS
118 32 | |
119 msb +----------------+----+------+------+ lsb
120 | | |
121 PAGE_L1_BITS |
123 PAGE_L2_BITS
125 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
126 pages are aligned on system page boundaries. The next most
127 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
128 index values in the lookup table, respectively.
130 For 32-bit architectures and the settings below, there are no
131 leftover bits. For architectures with wider pointers, the lookup
132 tree points to a list of pages, which must be scanned to find the
133 correct one. */
135 #define PAGE_L1_BITS (8)
136 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
137 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
138 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
140 #define LOOKUP_L1(p) \
141 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
143 #define LOOKUP_L2(p) \
144 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
146 /* The number of objects per allocation page, for objects on a page of
147 the indicated ORDER. */
148 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
150 /* The number of objects in P. */
151 #define OBJECTS_IN_PAGE(P) ((P)->bytes / OBJECT_SIZE ((P)->order))
153 /* The size of an object on a page of the indicated ORDER. */
154 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
156 /* For speed, we avoid doing a general integer divide to locate the
157 offset in the allocation bitmap, by precalculating numbers M, S
158 such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
159 within the page which is evenly divisible by the object size Z. */
160 #define DIV_MULT(ORDER) inverse_table[ORDER].mult
161 #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
162 #define OFFSET_TO_BIT(OFFSET, ORDER) \
163 (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
165 /* The number of extra orders, not corresponding to power-of-two sized
166 objects. */
168 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
170 #define RTL_SIZE(NSLOTS) \
171 (sizeof (struct rtx_def) + ((NSLOTS) - 1) * sizeof (rtunion))
173 /* The Ith entry is the maximum size of an object to be stored in the
174 Ith extra order. Adding a new entry to this array is the *only*
175 thing you need to do to add a new special allocation size. */
177 static const size_t extra_order_size_table[] = {
178 sizeof (struct tree_decl),
179 sizeof (struct tree_list),
180 RTL_SIZE (2), /* REG, MEM, PLUS, etc. */
181 RTL_SIZE (10), /* INSN, CALL_INSN, JUMP_INSN */
184 /* The total number of orders. */
186 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
188 /* We use this structure to determine the alignment required for
189 allocations. For power-of-two sized allocations, that's not a
190 problem, but it does matter for odd-sized allocations. */
192 struct max_alignment {
193 char c;
194 union {
195 HOST_WIDEST_INT i;
196 #ifdef HAVE_LONG_DOUBLE
197 long double d;
198 #else
199 double d;
200 #endif
201 } u;
204 /* The biggest alignment required. */
206 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
208 /* Compute the smallest nonnegative number which when added to X gives
209 a multiple of F. */
211 #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
213 /* Compute the smallest multiple of F that is >= X. */
215 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
217 /* The Ith entry is the number of objects on a page or order I. */
219 static unsigned objects_per_page_table[NUM_ORDERS];
221 /* The Ith entry is the size of an object on a page of order I. */
223 static size_t object_size_table[NUM_ORDERS];
225 /* The Ith entry is a pair of numbers (mult, shift) such that
226 ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
227 for all k evenly divisible by OBJECT_SIZE(I). */
229 static struct
231 unsigned int mult;
232 unsigned int shift;
234 inverse_table[NUM_ORDERS];
236 /* A page_entry records the status of an allocation page. This
237 structure is dynamically sized to fit the bitmap in_use_p. */
238 typedef struct page_entry
240 /* The next page-entry with objects of the same size, or NULL if
241 this is the last page-entry. */
242 struct page_entry *next;
244 /* The number of bytes allocated. (This will always be a multiple
245 of the host system page size.) */
246 size_t bytes;
248 /* The address at which the memory is allocated. */
249 char *page;
251 #ifdef USING_MALLOC_PAGE_GROUPS
252 /* Back pointer to the page group this page came from. */
253 struct page_group *group;
254 #endif
256 /* Saved in-use bit vector for pages that aren't in the topmost
257 context during collection. */
258 unsigned long *save_in_use_p;
260 /* Context depth of this page. */
261 unsigned short context_depth;
263 /* The number of free objects remaining on this page. */
264 unsigned short num_free_objects;
266 /* A likely candidate for the bit position of a free object for the
267 next allocation from this page. */
268 unsigned short next_bit_hint;
270 /* The lg of size of objects allocated from this page. */
271 unsigned char order;
273 /* A bit vector indicating whether or not objects are in use. The
274 Nth bit is one if the Nth object on this page is allocated. This
275 array is dynamically sized. */
276 unsigned long in_use_p[1];
277 } page_entry;
279 #ifdef USING_MALLOC_PAGE_GROUPS
280 /* A page_group describes a large allocation from malloc, from which
281 we parcel out aligned pages. */
282 typedef struct page_group
284 /* A linked list of all extant page groups. */
285 struct page_group *next;
287 /* The address we received from malloc. */
288 char *allocation;
290 /* The size of the block. */
291 size_t alloc_size;
293 /* A bitmask of pages in use. */
294 unsigned int in_use;
295 } page_group;
296 #endif
298 #if HOST_BITS_PER_PTR <= 32
300 /* On 32-bit hosts, we use a two level page table, as pictured above. */
301 typedef page_entry **page_table[PAGE_L1_SIZE];
303 #else
305 /* On 64-bit hosts, we use the same two level page tables plus a linked
306 list that disambiguates the top 32-bits. There will almost always be
307 exactly one entry in the list. */
308 typedef struct page_table_chain
310 struct page_table_chain *next;
311 size_t high_bits;
312 page_entry **table[PAGE_L1_SIZE];
313 } *page_table;
315 #endif
317 /* The rest of the global variables. */
318 static struct globals
320 /* The Nth element in this array is a page with objects of size 2^N.
321 If there are any pages with free objects, they will be at the
322 head of the list. NULL if there are no page-entries for this
323 object size. */
324 page_entry *pages[NUM_ORDERS];
326 /* The Nth element in this array is the last page with objects of
327 size 2^N. NULL if there are no page-entries for this object
328 size. */
329 page_entry *page_tails[NUM_ORDERS];
331 /* Lookup table for associating allocation pages with object addresses. */
332 page_table lookup;
334 /* The system's page size. */
335 size_t pagesize;
336 size_t lg_pagesize;
338 /* Bytes currently allocated. */
339 size_t allocated;
341 /* Bytes currently allocated at the end of the last collection. */
342 size_t allocated_last_gc;
344 /* Total amount of memory mapped. */
345 size_t bytes_mapped;
347 /* Bit N set if any allocations have been done at context depth N. */
348 unsigned long context_depth_allocations;
350 /* Bit N set if any collections have been done at context depth N. */
351 unsigned long context_depth_collections;
353 /* The current depth in the context stack. */
354 unsigned short context_depth;
356 /* A file descriptor open to /dev/zero for reading. */
357 #if defined (HAVE_MMAP_DEV_ZERO)
358 int dev_zero_fd;
359 #endif
361 /* A cache of free system pages. */
362 page_entry *free_pages;
364 #ifdef USING_MALLOC_PAGE_GROUPS
365 page_group *page_groups;
366 #endif
368 /* The file descriptor for debugging output. */
369 FILE *debug_file;
370 } G;
372 /* The size in bytes required to maintain a bitmap for the objects
373 on a page-entry. */
374 #define BITMAP_SIZE(Num_objects) \
375 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
377 /* Allocate pages in chunks of this size, to throttle calls to memory
378 allocation routines. The first page is used, the rest go onto the
379 free list. This cannot be larger than HOST_BITS_PER_INT for the
380 in_use bitmask for page_group. */
381 #define GGC_QUIRE_SIZE 16
383 static int ggc_allocated_p PARAMS ((const void *));
384 static page_entry *lookup_page_table_entry PARAMS ((const void *));
385 static void set_page_table_entry PARAMS ((void *, page_entry *));
386 #ifdef USING_MMAP
387 static char *alloc_anon PARAMS ((char *, size_t));
388 #endif
389 #ifdef USING_MALLOC_PAGE_GROUPS
390 static size_t page_group_index PARAMS ((char *, char *));
391 static void set_page_group_in_use PARAMS ((page_group *, char *));
392 static void clear_page_group_in_use PARAMS ((page_group *, char *));
393 #endif
394 static struct page_entry * alloc_page PARAMS ((unsigned));
395 static void free_page PARAMS ((struct page_entry *));
396 static void release_pages PARAMS ((void));
397 static void clear_marks PARAMS ((void));
398 static void sweep_pages PARAMS ((void));
399 static void ggc_recalculate_in_use_p PARAMS ((page_entry *));
400 static void compute_inverse PARAMS ((unsigned));
402 #ifdef ENABLE_GC_CHECKING
403 static void poison_pages PARAMS ((void));
404 #endif
406 void debug_print_page_list PARAMS ((int));
408 /* Returns nonzero if P was allocated in GC'able memory. */
410 static inline int
411 ggc_allocated_p (p)
412 const void *p;
414 page_entry ***base;
415 size_t L1, L2;
417 #if HOST_BITS_PER_PTR <= 32
418 base = &G.lookup[0];
419 #else
420 page_table table = G.lookup;
421 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
422 while (1)
424 if (table == NULL)
425 return 0;
426 if (table->high_bits == high_bits)
427 break;
428 table = table->next;
430 base = &table->table[0];
431 #endif
433 /* Extract the level 1 and 2 indices. */
434 L1 = LOOKUP_L1 (p);
435 L2 = LOOKUP_L2 (p);
437 return base[L1] && base[L1][L2];
440 /* Traverse the page table and find the entry for a page.
441 Die (probably) if the object wasn't allocated via GC. */
443 static inline page_entry *
444 lookup_page_table_entry(p)
445 const void *p;
447 page_entry ***base;
448 size_t L1, L2;
450 #if HOST_BITS_PER_PTR <= 32
451 base = &G.lookup[0];
452 #else
453 page_table table = G.lookup;
454 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
455 while (table->high_bits != high_bits)
456 table = table->next;
457 base = &table->table[0];
458 #endif
460 /* Extract the level 1 and 2 indices. */
461 L1 = LOOKUP_L1 (p);
462 L2 = LOOKUP_L2 (p);
464 return base[L1][L2];
467 /* Set the page table entry for a page. */
469 static void
470 set_page_table_entry(p, entry)
471 void *p;
472 page_entry *entry;
474 page_entry ***base;
475 size_t L1, L2;
477 #if HOST_BITS_PER_PTR <= 32
478 base = &G.lookup[0];
479 #else
480 page_table table;
481 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
482 for (table = G.lookup; table; table = table->next)
483 if (table->high_bits == high_bits)
484 goto found;
486 /* Not found -- allocate a new table. */
487 table = (page_table) xcalloc (1, sizeof(*table));
488 table->next = G.lookup;
489 table->high_bits = high_bits;
490 G.lookup = table;
491 found:
492 base = &table->table[0];
493 #endif
495 /* Extract the level 1 and 2 indices. */
496 L1 = LOOKUP_L1 (p);
497 L2 = LOOKUP_L2 (p);
499 if (base[L1] == NULL)
500 base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
502 base[L1][L2] = entry;
505 /* Prints the page-entry for object size ORDER, for debugging. */
507 void
508 debug_print_page_list (order)
509 int order;
511 page_entry *p;
512 printf ("Head=%p, Tail=%p:\n", (PTR) G.pages[order],
513 (PTR) G.page_tails[order]);
514 p = G.pages[order];
515 while (p != NULL)
517 printf ("%p(%1d|%3d) -> ", (PTR) p, p->context_depth,
518 p->num_free_objects);
519 p = p->next;
521 printf ("NULL\n");
522 fflush (stdout);
525 #ifdef USING_MMAP
526 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
527 (if non-null). The ifdef structure here is intended to cause a
528 compile error unless exactly one of the HAVE_* is defined. */
530 static inline char *
531 alloc_anon (pref, size)
532 char *pref ATTRIBUTE_UNUSED;
533 size_t size;
535 #ifdef HAVE_MMAP_ANON
536 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
537 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
538 #endif
539 #ifdef HAVE_MMAP_DEV_ZERO
540 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
541 MAP_PRIVATE, G.dev_zero_fd, 0);
542 #endif
544 if (page == (char *) MAP_FAILED)
546 perror ("virtual memory exhausted");
547 exit (FATAL_EXIT_CODE);
550 /* Remember that we allocated this memory. */
551 G.bytes_mapped += size;
553 /* Pretend we don't have access to the allocated pages. We'll enable
554 access to smaller pieces of the area in ggc_alloc. Discard the
555 handle to avoid handle leak. */
556 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (page, size));
558 return page;
560 #endif
561 #ifdef USING_MALLOC_PAGE_GROUPS
562 /* Compute the index for this page into the page group. */
564 static inline size_t
565 page_group_index (allocation, page)
566 char *allocation, *page;
568 return (size_t) (page - allocation) >> G.lg_pagesize;
571 /* Set and clear the in_use bit for this page in the page group. */
573 static inline void
574 set_page_group_in_use (group, page)
575 page_group *group;
576 char *page;
578 group->in_use |= 1 << page_group_index (group->allocation, page);
581 static inline void
582 clear_page_group_in_use (group, page)
583 page_group *group;
584 char *page;
586 group->in_use &= ~(1 << page_group_index (group->allocation, page));
588 #endif
590 /* Allocate a new page for allocating objects of size 2^ORDER,
591 and return an entry for it. The entry is not added to the
592 appropriate page_table list. */
594 static inline struct page_entry *
595 alloc_page (order)
596 unsigned order;
598 struct page_entry *entry, *p, **pp;
599 char *page;
600 size_t num_objects;
601 size_t bitmap_size;
602 size_t page_entry_size;
603 size_t entry_size;
604 #ifdef USING_MALLOC_PAGE_GROUPS
605 page_group *group;
606 #endif
608 num_objects = OBJECTS_PER_PAGE (order);
609 bitmap_size = BITMAP_SIZE (num_objects + 1);
610 page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
611 entry_size = num_objects * OBJECT_SIZE (order);
612 if (entry_size < G.pagesize)
613 entry_size = G.pagesize;
615 entry = NULL;
616 page = NULL;
618 /* Check the list of free pages for one we can use. */
619 for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
620 if (p->bytes == entry_size)
621 break;
623 if (p != NULL)
625 /* Recycle the allocated memory from this page ... */
626 *pp = p->next;
627 page = p->page;
629 #ifdef USING_MALLOC_PAGE_GROUPS
630 group = p->group;
631 #endif
633 /* ... and, if possible, the page entry itself. */
634 if (p->order == order)
636 entry = p;
637 memset (entry, 0, page_entry_size);
639 else
640 free (p);
642 #ifdef USING_MMAP
643 else if (entry_size == G.pagesize)
645 /* We want just one page. Allocate a bunch of them and put the
646 extras on the freelist. (Can only do this optimization with
647 mmap for backing store.) */
648 struct page_entry *e, *f = G.free_pages;
649 int i;
651 page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE);
653 /* This loop counts down so that the chain will be in ascending
654 memory order. */
655 for (i = GGC_QUIRE_SIZE - 1; i >= 1; i--)
657 e = (struct page_entry *) xcalloc (1, page_entry_size);
658 e->order = order;
659 e->bytes = G.pagesize;
660 e->page = page + (i << G.lg_pagesize);
661 e->next = f;
662 f = e;
665 G.free_pages = f;
667 else
668 page = alloc_anon (NULL, entry_size);
669 #endif
670 #ifdef USING_MALLOC_PAGE_GROUPS
671 else
673 /* Allocate a large block of memory and serve out the aligned
674 pages therein. This results in much less memory wastage
675 than the traditional implementation of valloc. */
677 char *allocation, *a, *enda;
678 size_t alloc_size, head_slop, tail_slop;
679 int multiple_pages = (entry_size == G.pagesize);
681 if (multiple_pages)
682 alloc_size = GGC_QUIRE_SIZE * G.pagesize;
683 else
684 alloc_size = entry_size + G.pagesize - 1;
685 allocation = xmalloc (alloc_size);
687 page = (char *) (((size_t) allocation + G.pagesize - 1) & -G.pagesize);
688 head_slop = page - allocation;
689 if (multiple_pages)
690 tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
691 else
692 tail_slop = alloc_size - entry_size - head_slop;
693 enda = allocation + alloc_size - tail_slop;
695 /* We allocated N pages, which are likely not aligned, leaving
696 us with N-1 usable pages. We plan to place the page_group
697 structure somewhere in the slop. */
698 if (head_slop >= sizeof (page_group))
699 group = (page_group *)page - 1;
700 else
702 /* We magically got an aligned allocation. Too bad, we have
703 to waste a page anyway. */
704 if (tail_slop == 0)
706 enda -= G.pagesize;
707 tail_slop += G.pagesize;
709 if (tail_slop < sizeof (page_group))
710 abort ();
711 group = (page_group *)enda;
712 tail_slop -= sizeof (page_group);
715 /* Remember that we allocated this memory. */
716 group->next = G.page_groups;
717 group->allocation = allocation;
718 group->alloc_size = alloc_size;
719 group->in_use = 0;
720 G.page_groups = group;
721 G.bytes_mapped += alloc_size;
723 /* If we allocated multiple pages, put the rest on the free list. */
724 if (multiple_pages)
726 struct page_entry *e, *f = G.free_pages;
727 for (a = enda - G.pagesize; a != page; a -= G.pagesize)
729 e = (struct page_entry *) xcalloc (1, page_entry_size);
730 e->order = order;
731 e->bytes = G.pagesize;
732 e->page = a;
733 e->group = group;
734 e->next = f;
735 f = e;
737 G.free_pages = f;
740 #endif
742 if (entry == NULL)
743 entry = (struct page_entry *) xcalloc (1, page_entry_size);
745 entry->bytes = entry_size;
746 entry->page = page;
747 entry->context_depth = G.context_depth;
748 entry->order = order;
749 entry->num_free_objects = num_objects;
750 entry->next_bit_hint = 1;
752 G.context_depth_allocations |= (unsigned long)1 << G.context_depth;
754 #ifdef USING_MALLOC_PAGE_GROUPS
755 entry->group = group;
756 set_page_group_in_use (group, page);
757 #endif
759 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
760 increment the hint. */
761 entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
762 = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
764 set_page_table_entry (page, entry);
766 if (GGC_DEBUG_LEVEL >= 2)
767 fprintf (G.debug_file,
768 "Allocating page at %p, object size=%lu, data %p-%p\n",
769 (PTR) entry, (unsigned long) OBJECT_SIZE (order), page,
770 page + entry_size - 1);
772 return entry;
775 /* For a page that is no longer needed, put it on the free page list. */
777 static inline void
778 free_page (entry)
779 page_entry *entry;
781 if (GGC_DEBUG_LEVEL >= 2)
782 fprintf (G.debug_file,
783 "Deallocating page at %p, data %p-%p\n", (PTR) entry,
784 entry->page, entry->page + entry->bytes - 1);
786 /* Mark the page as inaccessible. Discard the handle to avoid handle
787 leak. */
788 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (entry->page, entry->bytes));
790 set_page_table_entry (entry->page, NULL);
792 #ifdef USING_MALLOC_PAGE_GROUPS
793 clear_page_group_in_use (entry->group, entry->page);
794 #endif
796 entry->next = G.free_pages;
797 G.free_pages = entry;
800 /* Release the free page cache to the system. */
802 static void
803 release_pages ()
805 #ifdef USING_MMAP
806 page_entry *p, *next;
807 char *start;
808 size_t len;
810 /* Gather up adjacent pages so they are unmapped together. */
811 p = G.free_pages;
813 while (p)
815 start = p->page;
816 next = p->next;
817 len = p->bytes;
818 free (p);
819 p = next;
821 while (p && p->page == start + len)
823 next = p->next;
824 len += p->bytes;
825 free (p);
826 p = next;
829 munmap (start, len);
830 G.bytes_mapped -= len;
833 G.free_pages = NULL;
834 #endif
835 #ifdef USING_MALLOC_PAGE_GROUPS
836 page_entry **pp, *p;
837 page_group **gp, *g;
839 /* Remove all pages from free page groups from the list. */
840 pp = &G.free_pages;
841 while ((p = *pp) != NULL)
842 if (p->group->in_use == 0)
844 *pp = p->next;
845 free (p);
847 else
848 pp = &p->next;
850 /* Remove all free page groups, and release the storage. */
851 gp = &G.page_groups;
852 while ((g = *gp) != NULL)
853 if (g->in_use == 0)
855 *gp = g->next;
856 G.bytes_mapped -= g->alloc_size;
857 free (g->allocation);
859 else
860 gp = &g->next;
861 #endif
864 /* This table provides a fast way to determine ceil(log_2(size)) for
865 allocation requests. The minimum allocation size is eight bytes. */
867 static unsigned char size_lookup[257] =
869 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
870 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
871 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
872 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
873 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
874 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
875 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
876 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
877 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
878 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
879 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
880 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
881 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
882 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
883 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
884 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
888 /* Allocate a chunk of memory of SIZE bytes. If ZERO is nonzero, the
889 memory is zeroed; otherwise, its contents are undefined. */
891 void *
892 ggc_alloc (size)
893 size_t size;
895 unsigned order, word, bit, object_offset;
896 struct page_entry *entry;
897 void *result;
899 if (size <= 256)
900 order = size_lookup[size];
901 else
903 order = 9;
904 while (size > OBJECT_SIZE (order))
905 order++;
908 /* If there are non-full pages for this size allocation, they are at
909 the head of the list. */
910 entry = G.pages[order];
912 /* If there is no page for this object size, or all pages in this
913 context are full, allocate a new page. */
914 if (entry == NULL || entry->num_free_objects == 0)
916 struct page_entry *new_entry;
917 new_entry = alloc_page (order);
919 /* If this is the only entry, it's also the tail. */
920 if (entry == NULL)
921 G.page_tails[order] = new_entry;
923 /* Put new pages at the head of the page list. */
924 new_entry->next = entry;
925 entry = new_entry;
926 G.pages[order] = new_entry;
928 /* For a new page, we know the word and bit positions (in the
929 in_use bitmap) of the first available object -- they're zero. */
930 new_entry->next_bit_hint = 1;
931 word = 0;
932 bit = 0;
933 object_offset = 0;
935 else
937 /* First try to use the hint left from the previous allocation
938 to locate a clear bit in the in-use bitmap. We've made sure
939 that the one-past-the-end bit is always set, so if the hint
940 has run over, this test will fail. */
941 unsigned hint = entry->next_bit_hint;
942 word = hint / HOST_BITS_PER_LONG;
943 bit = hint % HOST_BITS_PER_LONG;
945 /* If the hint didn't work, scan the bitmap from the beginning. */
946 if ((entry->in_use_p[word] >> bit) & 1)
948 word = bit = 0;
949 while (~entry->in_use_p[word] == 0)
950 ++word;
951 while ((entry->in_use_p[word] >> bit) & 1)
952 ++bit;
953 hint = word * HOST_BITS_PER_LONG + bit;
956 /* Next time, try the next bit. */
957 entry->next_bit_hint = hint + 1;
959 object_offset = hint * OBJECT_SIZE (order);
962 /* Set the in-use bit. */
963 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
965 /* Keep a running total of the number of free objects. If this page
966 fills up, we may have to move it to the end of the list if the
967 next page isn't full. If the next page is full, all subsequent
968 pages are full, so there's no need to move it. */
969 if (--entry->num_free_objects == 0
970 && entry->next != NULL
971 && entry->next->num_free_objects > 0)
973 G.pages[order] = entry->next;
974 entry->next = NULL;
975 G.page_tails[order]->next = entry;
976 G.page_tails[order] = entry;
979 /* Calculate the object's address. */
980 result = entry->page + object_offset;
982 #ifdef ENABLE_GC_CHECKING
983 /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
984 exact same semantics in presence of memory bugs, regardless of
985 ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
986 handle to avoid handle leak. */
987 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, OBJECT_SIZE (order)));
989 /* `Poison' the entire allocated object, including any padding at
990 the end. */
991 memset (result, 0xaf, OBJECT_SIZE (order));
993 /* Make the bytes after the end of the object unaccessible. Discard the
994 handle to avoid handle leak. */
995 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) result + size,
996 OBJECT_SIZE (order) - size));
997 #endif
999 /* Tell Valgrind that the memory is there, but its content isn't
1000 defined. The bytes at the end of the object are still marked
1001 unaccessible. */
1002 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, size));
1004 /* Keep track of how many bytes are being allocated. This
1005 information is used in deciding when to collect. */
1006 G.allocated += OBJECT_SIZE (order);
1008 if (GGC_DEBUG_LEVEL >= 3)
1009 fprintf (G.debug_file,
1010 "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
1011 (unsigned long) size, (unsigned long) OBJECT_SIZE (order), result,
1012 (PTR) entry);
1014 return result;
1017 /* If P is not marked, marks it and return false. Otherwise return true.
1018 P must have been allocated by the GC allocator; it mustn't point to
1019 static objects, stack variables, or memory allocated with malloc. */
1022 ggc_set_mark (p)
1023 const void *p;
1025 page_entry *entry;
1026 unsigned bit, word;
1027 unsigned long mask;
1029 /* Look up the page on which the object is alloced. If the object
1030 wasn't allocated by the collector, we'll probably die. */
1031 entry = lookup_page_table_entry (p);
1032 #ifdef ENABLE_CHECKING
1033 if (entry == NULL)
1034 abort ();
1035 #endif
1037 /* Calculate the index of the object on the page; this is its bit
1038 position in the in_use_p bitmap. */
1039 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1040 word = bit / HOST_BITS_PER_LONG;
1041 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1043 /* If the bit was previously set, skip it. */
1044 if (entry->in_use_p[word] & mask)
1045 return 1;
1047 /* Otherwise set it, and decrement the free object count. */
1048 entry->in_use_p[word] |= mask;
1049 entry->num_free_objects -= 1;
1051 if (GGC_DEBUG_LEVEL >= 4)
1052 fprintf (G.debug_file, "Marking %p\n", p);
1054 return 0;
1057 /* Return 1 if P has been marked, zero otherwise.
1058 P must have been allocated by the GC allocator; it mustn't point to
1059 static objects, stack variables, or memory allocated with malloc. */
1062 ggc_marked_p (p)
1063 const void *p;
1065 page_entry *entry;
1066 unsigned bit, word;
1067 unsigned long mask;
1069 /* Look up the page on which the object is alloced. If the object
1070 wasn't allocated by the collector, we'll probably die. */
1071 entry = lookup_page_table_entry (p);
1072 #ifdef ENABLE_CHECKING
1073 if (entry == NULL)
1074 abort ();
1075 #endif
1077 /* Calculate the index of the object on the page; this is its bit
1078 position in the in_use_p bitmap. */
1079 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1080 word = bit / HOST_BITS_PER_LONG;
1081 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1083 return (entry->in_use_p[word] & mask) != 0;
1086 /* Return the size of the gc-able object P. */
1088 size_t
1089 ggc_get_size (p)
1090 const void *p;
1092 page_entry *pe = lookup_page_table_entry (p);
1093 return OBJECT_SIZE (pe->order);
1096 /* Subroutine of init_ggc which computes the pair of numbers used to
1097 perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1099 This algorithm is taken from Granlund and Montgomery's paper
1100 "Division by Invariant Integers using Multiplication"
1101 (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1102 constants). */
1104 static void
1105 compute_inverse (order)
1106 unsigned order;
1108 unsigned size, inv, e;
1110 /* There can be only one object per "page" in a bucket for sizes
1111 larger than half a machine page; it will always have offset zero. */
1112 if (OBJECT_SIZE (order) > G.pagesize/2)
1114 if (OBJECTS_PER_PAGE (order) != 1)
1115 abort ();
1117 DIV_MULT (order) = 1;
1118 DIV_SHIFT (order) = 0;
1119 return;
1122 size = OBJECT_SIZE (order);
1123 e = 0;
1124 while (size % 2 == 0)
1126 e++;
1127 size >>= 1;
1130 inv = size;
1131 while (inv * size != 1)
1132 inv = inv * (2 - inv*size);
1134 DIV_MULT (order) = inv;
1135 DIV_SHIFT (order) = e;
1138 /* Initialize the ggc-mmap allocator. */
1139 void
1140 init_ggc ()
1142 unsigned order;
1144 G.pagesize = getpagesize();
1145 G.lg_pagesize = exact_log2 (G.pagesize);
1147 #ifdef HAVE_MMAP_DEV_ZERO
1148 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1149 if (G.dev_zero_fd == -1)
1150 abort ();
1151 #endif
1153 #if 0
1154 G.debug_file = fopen ("ggc-mmap.debug", "w");
1155 #else
1156 G.debug_file = stdout;
1157 #endif
1159 #ifdef USING_MMAP
1160 /* StunOS has an amazing off-by-one error for the first mmap allocation
1161 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1162 believe, is an unaligned page allocation, which would cause us to
1163 hork badly if we tried to use it. */
1165 char *p = alloc_anon (NULL, G.pagesize);
1166 struct page_entry *e;
1167 if ((size_t)p & (G.pagesize - 1))
1169 /* How losing. Discard this one and try another. If we still
1170 can't get something useful, give up. */
1172 p = alloc_anon (NULL, G.pagesize);
1173 if ((size_t)p & (G.pagesize - 1))
1174 abort ();
1177 /* We have a good page, might as well hold onto it... */
1178 e = (struct page_entry *) xcalloc (1, sizeof (struct page_entry));
1179 e->bytes = G.pagesize;
1180 e->page = p;
1181 e->next = G.free_pages;
1182 G.free_pages = e;
1184 #endif
1186 /* Initialize the object size table. */
1187 for (order = 0; order < HOST_BITS_PER_PTR; ++order)
1188 object_size_table[order] = (size_t) 1 << order;
1189 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1191 size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
1193 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1194 so that we're sure of getting aligned memory. */
1195 s = ROUND_UP (s, MAX_ALIGNMENT);
1196 object_size_table[order] = s;
1199 /* Initialize the objects-per-page and inverse tables. */
1200 for (order = 0; order < NUM_ORDERS; ++order)
1202 objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
1203 if (objects_per_page_table[order] == 0)
1204 objects_per_page_table[order] = 1;
1205 compute_inverse (order);
1208 /* Reset the size_lookup array to put appropriately sized objects in
1209 the special orders. All objects bigger than the previous power
1210 of two, but no greater than the special size, should go in the
1211 new order. */
1212 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1214 int o;
1215 int i;
1217 o = size_lookup[OBJECT_SIZE (order)];
1218 for (i = OBJECT_SIZE (order); size_lookup [i] == o; --i)
1219 size_lookup[i] = order;
1223 /* Increment the `GC context'. Objects allocated in an outer context
1224 are never freed, eliminating the need to register their roots. */
1226 void
1227 ggc_push_context ()
1229 ++G.context_depth;
1231 /* Die on wrap. */
1232 if (G.context_depth >= HOST_BITS_PER_LONG)
1233 abort ();
1236 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1237 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1239 static void
1240 ggc_recalculate_in_use_p (p)
1241 page_entry *p;
1243 unsigned int i;
1244 size_t num_objects;
1246 /* Because the past-the-end bit in in_use_p is always set, we
1247 pretend there is one additional object. */
1248 num_objects = OBJECTS_IN_PAGE (p) + 1;
1250 /* Reset the free object count. */
1251 p->num_free_objects = num_objects;
1253 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1254 for (i = 0;
1255 i < CEIL (BITMAP_SIZE (num_objects),
1256 sizeof (*p->in_use_p));
1257 ++i)
1259 unsigned long j;
1261 /* Something is in use if it is marked, or if it was in use in a
1262 context further down the context stack. */
1263 p->in_use_p[i] |= p->save_in_use_p[i];
1265 /* Decrement the free object count for every object allocated. */
1266 for (j = p->in_use_p[i]; j; j >>= 1)
1267 p->num_free_objects -= (j & 1);
1270 if (p->num_free_objects >= num_objects)
1271 abort ();
1274 /* Decrement the `GC context'. All objects allocated since the
1275 previous ggc_push_context are migrated to the outer context. */
1277 void
1278 ggc_pop_context ()
1280 unsigned long omask;
1281 unsigned order, depth;
1283 depth = --G.context_depth;
1284 omask = (unsigned long)1 << (depth + 1);
1286 if (!((G.context_depth_allocations | G.context_depth_collections) & omask))
1287 return;
1289 G.context_depth_allocations |= (G.context_depth_allocations & omask) >> 1;
1290 G.context_depth_allocations &= omask - 1;
1291 G.context_depth_collections &= omask - 1;
1293 /* Any remaining pages in the popped context are lowered to the new
1294 current context; i.e. objects allocated in the popped context and
1295 left over are imported into the previous context. */
1296 for (order = 2; order < NUM_ORDERS; order++)
1298 page_entry *p;
1300 for (p = G.pages[order]; p != NULL; p = p->next)
1302 if (p->context_depth > depth)
1303 p->context_depth = depth;
1305 /* If this page is now in the topmost context, and we'd
1306 saved its allocation state, restore it. */
1307 else if (p->context_depth == depth && p->save_in_use_p)
1309 ggc_recalculate_in_use_p (p);
1310 free (p->save_in_use_p);
1311 p->save_in_use_p = 0;
1317 /* Unmark all objects. */
1319 static inline void
1320 clear_marks ()
1322 unsigned order;
1324 for (order = 2; order < NUM_ORDERS; order++)
1326 page_entry *p;
1328 for (p = G.pages[order]; p != NULL; p = p->next)
1330 size_t num_objects = OBJECTS_IN_PAGE (p);
1331 size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
1333 #ifdef ENABLE_CHECKING
1334 /* The data should be page-aligned. */
1335 if ((size_t) p->page & (G.pagesize - 1))
1336 abort ();
1337 #endif
1339 /* Pages that aren't in the topmost context are not collected;
1340 nevertheless, we need their in-use bit vectors to store GC
1341 marks. So, back them up first. */
1342 if (p->context_depth < G.context_depth)
1344 if (! p->save_in_use_p)
1345 p->save_in_use_p = xmalloc (bitmap_size);
1346 memcpy (p->save_in_use_p, p->in_use_p, bitmap_size);
1349 /* Reset reset the number of free objects and clear the
1350 in-use bits. These will be adjusted by mark_obj. */
1351 p->num_free_objects = num_objects;
1352 memset (p->in_use_p, 0, bitmap_size);
1354 /* Make sure the one-past-the-end bit is always set. */
1355 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
1356 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
1361 /* Free all empty pages. Partially empty pages need no attention
1362 because the `mark' bit doubles as an `unused' bit. */
1364 static inline void
1365 sweep_pages ()
1367 unsigned order;
1369 for (order = 2; order < NUM_ORDERS; order++)
1371 /* The last page-entry to consider, regardless of entries
1372 placed at the end of the list. */
1373 page_entry * const last = G.page_tails[order];
1375 size_t num_objects;
1376 size_t live_objects;
1377 page_entry *p, *previous;
1378 int done;
1380 p = G.pages[order];
1381 if (p == NULL)
1382 continue;
1384 previous = NULL;
1387 page_entry *next = p->next;
1389 /* Loop until all entries have been examined. */
1390 done = (p == last);
1392 num_objects = OBJECTS_IN_PAGE (p);
1394 /* Add all live objects on this page to the count of
1395 allocated memory. */
1396 live_objects = num_objects - p->num_free_objects;
1398 G.allocated += OBJECT_SIZE (order) * live_objects;
1400 /* Only objects on pages in the topmost context should get
1401 collected. */
1402 if (p->context_depth < G.context_depth)
1405 /* Remove the page if it's empty. */
1406 else if (live_objects == 0)
1408 if (! previous)
1409 G.pages[order] = next;
1410 else
1411 previous->next = next;
1413 /* Are we removing the last element? */
1414 if (p == G.page_tails[order])
1415 G.page_tails[order] = previous;
1416 free_page (p);
1417 p = previous;
1420 /* If the page is full, move it to the end. */
1421 else if (p->num_free_objects == 0)
1423 /* Don't move it if it's already at the end. */
1424 if (p != G.page_tails[order])
1426 /* Move p to the end of the list. */
1427 p->next = NULL;
1428 G.page_tails[order]->next = p;
1430 /* Update the tail pointer... */
1431 G.page_tails[order] = p;
1433 /* ... and the head pointer, if necessary. */
1434 if (! previous)
1435 G.pages[order] = next;
1436 else
1437 previous->next = next;
1438 p = previous;
1442 /* If we've fallen through to here, it's a page in the
1443 topmost context that is neither full nor empty. Such a
1444 page must precede pages at lesser context depth in the
1445 list, so move it to the head. */
1446 else if (p != G.pages[order])
1448 previous->next = p->next;
1449 p->next = G.pages[order];
1450 G.pages[order] = p;
1451 /* Are we moving the last element? */
1452 if (G.page_tails[order] == p)
1453 G.page_tails[order] = previous;
1454 p = previous;
1457 previous = p;
1458 p = next;
1460 while (! done);
1462 /* Now, restore the in_use_p vectors for any pages from contexts
1463 other than the current one. */
1464 for (p = G.pages[order]; p; p = p->next)
1465 if (p->context_depth != G.context_depth)
1466 ggc_recalculate_in_use_p (p);
1470 #ifdef ENABLE_GC_CHECKING
1471 /* Clobber all free objects. */
1473 static inline void
1474 poison_pages ()
1476 unsigned order;
1478 for (order = 2; order < NUM_ORDERS; order++)
1480 size_t size = OBJECT_SIZE (order);
1481 page_entry *p;
1483 for (p = G.pages[order]; p != NULL; p = p->next)
1485 size_t num_objects;
1486 size_t i;
1488 if (p->context_depth != G.context_depth)
1489 /* Since we don't do any collection for pages in pushed
1490 contexts, there's no need to do any poisoning. And
1491 besides, the IN_USE_P array isn't valid until we pop
1492 contexts. */
1493 continue;
1495 num_objects = OBJECTS_IN_PAGE (p);
1496 for (i = 0; i < num_objects; i++)
1498 size_t word, bit;
1499 word = i / HOST_BITS_PER_LONG;
1500 bit = i % HOST_BITS_PER_LONG;
1501 if (((p->in_use_p[word] >> bit) & 1) == 0)
1503 char *object = p->page + i * size;
1505 /* Keep poison-by-write when we expect to use Valgrind,
1506 so the exact same memory semantics is kept, in case
1507 there are memory errors. We override this request
1508 below. */
1509 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (object, size));
1510 memset (object, 0xa5, size);
1512 /* Drop the handle to avoid handle leak. */
1513 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (object, size));
1519 #endif
1521 /* Top level mark-and-sweep routine. */
1523 void
1524 ggc_collect ()
1526 /* Avoid frequent unnecessary work by skipping collection if the
1527 total allocations haven't expanded much since the last
1528 collection. */
1529 float allocated_last_gc =
1530 MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
1532 float min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
1534 if (G.allocated < allocated_last_gc + min_expand)
1535 return;
1537 timevar_push (TV_GC);
1538 if (!quiet_flag)
1539 fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
1541 /* Zero the total allocated bytes. This will be recalculated in the
1542 sweep phase. */
1543 G.allocated = 0;
1545 /* Release the pages we freed the last time we collected, but didn't
1546 reuse in the interim. */
1547 release_pages ();
1549 /* Indicate that we've seen collections at this context depth. */
1550 G.context_depth_collections = ((unsigned long)1 << (G.context_depth + 1)) - 1;
1552 clear_marks ();
1553 ggc_mark_roots ();
1555 #ifdef ENABLE_GC_CHECKING
1556 poison_pages ();
1557 #endif
1559 sweep_pages ();
1561 G.allocated_last_gc = G.allocated;
1563 timevar_pop (TV_GC);
1565 if (!quiet_flag)
1566 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
1569 /* Print allocation statistics. */
1570 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1571 ? (x) \
1572 : ((x) < 1024*1024*10 \
1573 ? (x) / 1024 \
1574 : (x) / (1024*1024))))
1575 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1577 void
1578 ggc_print_statistics ()
1580 struct ggc_statistics stats;
1581 unsigned int i;
1582 size_t total_overhead = 0;
1584 /* Clear the statistics. */
1585 memset (&stats, 0, sizeof (stats));
1587 /* Make sure collection will really occur. */
1588 G.allocated_last_gc = 0;
1590 /* Collect and print the statistics common across collectors. */
1591 ggc_print_common_statistics (stderr, &stats);
1593 /* Release free pages so that we will not count the bytes allocated
1594 there as part of the total allocated memory. */
1595 release_pages ();
1597 /* Collect some information about the various sizes of
1598 allocation. */
1599 fprintf (stderr, "\n%-5s %10s %10s %10s\n",
1600 "Size", "Allocated", "Used", "Overhead");
1601 for (i = 0; i < NUM_ORDERS; ++i)
1603 page_entry *p;
1604 size_t allocated;
1605 size_t in_use;
1606 size_t overhead;
1608 /* Skip empty entries. */
1609 if (!G.pages[i])
1610 continue;
1612 overhead = allocated = in_use = 0;
1614 /* Figure out the total number of bytes allocated for objects of
1615 this size, and how many of them are actually in use. Also figure
1616 out how much memory the page table is using. */
1617 for (p = G.pages[i]; p; p = p->next)
1619 allocated += p->bytes;
1620 in_use +=
1621 (OBJECTS_IN_PAGE (p) - p->num_free_objects) * OBJECT_SIZE (i);
1623 overhead += (sizeof (page_entry) - sizeof (long)
1624 + BITMAP_SIZE (OBJECTS_IN_PAGE (p) + 1));
1626 fprintf (stderr, "%-5lu %10lu%c %10lu%c %10lu%c\n",
1627 (unsigned long) OBJECT_SIZE (i),
1628 SCALE (allocated), LABEL (allocated),
1629 SCALE (in_use), LABEL (in_use),
1630 SCALE (overhead), LABEL (overhead));
1631 total_overhead += overhead;
1633 fprintf (stderr, "%-5s %10lu%c %10lu%c %10lu%c\n", "Total",
1634 SCALE (G.bytes_mapped), LABEL (G.bytes_mapped),
1635 SCALE (G.allocated), LABEL(G.allocated),
1636 SCALE (total_overhead), LABEL (total_overhead));
1639 struct ggc_pch_data
1641 struct ggc_pch_ondisk
1643 unsigned totals[NUM_ORDERS];
1644 } d;
1645 size_t base[NUM_ORDERS];
1646 size_t written[NUM_ORDERS];
1649 struct ggc_pch_data *
1650 init_ggc_pch ()
1652 return xcalloc (sizeof (struct ggc_pch_data), 1);
1655 void
1656 ggc_pch_count_object (d, x, size)
1657 struct ggc_pch_data *d;
1658 void *x ATTRIBUTE_UNUSED;
1659 size_t size;
1661 unsigned order;
1663 if (size <= 256)
1664 order = size_lookup[size];
1665 else
1667 order = 9;
1668 while (size > OBJECT_SIZE (order))
1669 order++;
1672 d->d.totals[order]++;
1675 size_t
1676 ggc_pch_total_size (d)
1677 struct ggc_pch_data *d;
1679 size_t a = 0;
1680 unsigned i;
1682 for (i = 0; i < NUM_ORDERS; i++)
1683 a += ROUND_UP (d->d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1684 return a;
1687 void
1688 ggc_pch_this_base (d, base)
1689 struct ggc_pch_data *d;
1690 void *base;
1692 size_t a = (size_t) base;
1693 unsigned i;
1695 for (i = 0; i < NUM_ORDERS; i++)
1697 d->base[i] = a;
1698 a += ROUND_UP (d->d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1703 char *
1704 ggc_pch_alloc_object (d, x, size)
1705 struct ggc_pch_data *d;
1706 void *x ATTRIBUTE_UNUSED;
1707 size_t size;
1709 unsigned order;
1710 char *result;
1712 if (size <= 256)
1713 order = size_lookup[size];
1714 else
1716 order = 9;
1717 while (size > OBJECT_SIZE (order))
1718 order++;
1721 result = (char *) d->base[order];
1722 d->base[order] += OBJECT_SIZE (order);
1723 return result;
1726 void
1727 ggc_pch_prepare_write (d, f)
1728 struct ggc_pch_data * d ATTRIBUTE_UNUSED;
1729 FILE * f ATTRIBUTE_UNUSED;
1731 /* Nothing to do. */
1734 void
1735 ggc_pch_write_object (d, f, x, newx, size)
1736 struct ggc_pch_data * d ATTRIBUTE_UNUSED;
1737 FILE *f;
1738 void *x;
1739 void *newx ATTRIBUTE_UNUSED;
1740 size_t size;
1742 unsigned order;
1744 if (size <= 256)
1745 order = size_lookup[size];
1746 else
1748 order = 9;
1749 while (size > OBJECT_SIZE (order))
1750 order++;
1753 if (fwrite (x, size, 1, f) != 1)
1754 fatal_io_error ("can't write PCH file");
1756 /* In the current implementation, SIZE is always equal to
1757 OBJECT_SIZE (order) and so the fseek is never executed. */
1758 if (size != OBJECT_SIZE (order)
1759 && fseek (f, OBJECT_SIZE (order) - size, SEEK_CUR) != 0)
1760 fatal_io_error ("can't write PCH file");
1762 d->written[order]++;
1763 if (d->written[order] == d->d.totals[order]
1764 && fseek (f, ROUND_UP_VALUE (d->d.totals[order] * OBJECT_SIZE (order),
1765 G.pagesize),
1766 SEEK_CUR) != 0)
1767 fatal_io_error ("can't write PCH file");
1770 void
1771 ggc_pch_finish (d, f)
1772 struct ggc_pch_data * d;
1773 FILE *f;
1775 if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
1776 fatal_io_error ("can't write PCH file");
1777 free (d);
1780 void
1781 ggc_pch_read (f, addr)
1782 FILE *f;
1783 void *addr;
1785 struct ggc_pch_ondisk d;
1786 unsigned i;
1787 char *offs = addr;
1789 /* We've just read in a PCH file. So, every object that used to be allocated
1790 is now free. */
1791 clear_marks ();
1792 #ifdef GGC_POISON
1793 poison_pages ();
1794 #endif
1796 /* No object read from a PCH file should ever be freed. So, set the
1797 context depth to 1, and set the depth of all the currently-allocated
1798 pages to be 1 too. PCH pages will have depth 0. */
1799 if (G.context_depth != 0)
1800 abort ();
1801 G.context_depth = 1;
1802 for (i = 0; i < NUM_ORDERS; i++)
1804 page_entry *p;
1805 for (p = G.pages[i]; p != NULL; p = p->next)
1806 p->context_depth = G.context_depth;
1809 /* Allocate the appropriate page-table entries for the pages read from
1810 the PCH file. */
1811 if (fread (&d, sizeof (d), 1, f) != 1)
1812 fatal_io_error ("can't read PCH file");
1814 for (i = 0; i < NUM_ORDERS; i++)
1816 struct page_entry *entry;
1817 char *pte;
1818 size_t bytes;
1819 size_t num_objs;
1820 size_t j;
1822 if (d.totals[i] == 0)
1823 continue;
1825 bytes = ROUND_UP (d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1826 num_objs = bytes / OBJECT_SIZE (i);
1827 entry = xcalloc (1, (sizeof (struct page_entry)
1828 - sizeof (long)
1829 + BITMAP_SIZE (num_objs + 1)));
1830 entry->bytes = bytes;
1831 entry->page = offs;
1832 entry->context_depth = 0;
1833 offs += bytes;
1834 entry->num_free_objects = 0;
1835 entry->order = i;
1837 for (j = 0;
1838 j + HOST_BITS_PER_LONG <= num_objs + 1;
1839 j += HOST_BITS_PER_LONG)
1840 entry->in_use_p[j / HOST_BITS_PER_LONG] = -1;
1841 for (; j < num_objs + 1; j++)
1842 entry->in_use_p[j / HOST_BITS_PER_LONG]
1843 |= 1L << (j % HOST_BITS_PER_LONG);
1845 for (pte = entry->page;
1846 pte < entry->page + entry->bytes;
1847 pte += G.pagesize)
1848 set_page_table_entry (pte, entry);
1850 if (G.page_tails[i] != NULL)
1851 G.page_tails[i]->next = entry;
1852 else
1853 G.pages[i] = entry;
1854 G.page_tails[i] = entry;
1857 /* Update the statistics. */
1858 G.allocated = G.allocated_last_gc = offs - (char *)addr;