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
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
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
23 #include "coretypes.h"
33 #ifdef ENABLE_VALGRIND_CHECKING
34 # ifdef HAVE_MEMCHECK_H
35 # include <memcheck.h>
37 # include <valgrind.h>
40 /* Avoid #ifdef:s when we can help it. */
41 #define VALGRIND_DISCARD(x)
44 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
45 file open. Prefer either to valloc. */
47 # undef HAVE_MMAP_DEV_ZERO
49 # include <sys/mman.h>
51 # define MAP_FAILED -1
53 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
54 # define MAP_ANONYMOUS MAP_ANON
60 #ifdef HAVE_MMAP_DEV_ZERO
62 # include <sys/mman.h>
64 # define MAP_FAILED -1
71 #define USING_MALLOC_PAGE_GROUPS
76 This garbage-collecting allocator allocates objects on one of a set
77 of pages. Each page can allocate objects of a single size only;
78 available sizes are powers of two starting at four bytes. The size
79 of an allocation request is rounded up to the next power of two
80 (`order'), and satisfied from the appropriate page.
82 Each page is recorded in a page-entry, which also maintains an
83 in-use bitmap of object positions on the page. This allows the
84 allocation state of a particular object to be flipped without
85 touching the page itself.
87 Each page-entry also has a context depth, which is used to track
88 pushing and popping of allocation contexts. Only objects allocated
89 in the current (highest-numbered) context may be collected.
91 Page entries are arranged in an array of singly-linked lists. The
92 array is indexed by the allocation size, in bits, of the pages on
93 it; i.e. all pages on a list allocate objects of the same size.
94 Pages are ordered on the list such that all non-full pages precede
95 all full pages, with non-full pages arranged in order of decreasing
98 Empty pages (of all orders) are kept on a single page cache list,
99 and are considered first when new pages are required; they are
100 deallocated at the start of the next collection if they haven't
101 been recycled by then. */
103 /* Define GGC_DEBUG_LEVEL to print debugging information.
104 0: No debugging output.
105 1: GC statistics only.
106 2: Page-entry allocations/deallocations as well.
107 3: Object allocations as well.
108 4: Object marks as well. */
109 #define GGC_DEBUG_LEVEL (0)
111 #ifndef HOST_BITS_PER_PTR
112 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
116 /* A two-level tree is used to look up the page-entry for a given
117 pointer. Two chunks of the pointer's bits are extracted to index
118 the first and second levels of the tree, as follows:
122 msb +----------------+----+------+------+ lsb
128 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
129 pages are aligned on system page boundaries. The next most
130 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
131 index values in the lookup table, respectively.
133 For 32-bit architectures and the settings below, there are no
134 leftover bits. For architectures with wider pointers, the lookup
135 tree points to a list of pages, which must be scanned to find the
138 #define PAGE_L1_BITS (8)
139 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
140 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
141 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
143 #define LOOKUP_L1(p) \
144 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
146 #define LOOKUP_L2(p) \
147 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
149 /* The number of objects per allocation page, for objects on a page of
150 the indicated ORDER. */
151 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
153 /* The number of objects in P. */
154 #define OBJECTS_IN_PAGE(P) ((P)->bytes / OBJECT_SIZE ((P)->order))
156 /* The size of an object on a page of the indicated ORDER. */
157 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
159 /* For speed, we avoid doing a general integer divide to locate the
160 offset in the allocation bitmap, by precalculating numbers M, S
161 such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
162 within the page which is evenly divisible by the object size Z. */
163 #define DIV_MULT(ORDER) inverse_table[ORDER].mult
164 #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
165 #define OFFSET_TO_BIT(OFFSET, ORDER) \
166 (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
168 /* The number of extra orders, not corresponding to power-of-two sized
171 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
173 #define RTL_SIZE(NSLOTS) \
174 (sizeof (struct rtx_def) + ((NSLOTS) - 1) * sizeof (rtunion))
176 #define TREE_EXP_SIZE(OPS) \
177 (sizeof (struct tree_exp) + ((OPS) - 1) * sizeof (tree))
179 /* The Ith entry is the maximum size of an object to be stored in the
180 Ith extra order. Adding a new entry to this array is the *only*
181 thing you need to do to add a new special allocation size. */
183 static const size_t extra_order_size_table[] = {
184 sizeof (struct tree_decl),
185 sizeof (struct tree_list),
187 RTL_SIZE (2), /* MEM, PLUS, etc. */
188 RTL_SIZE (9), /* INSN, CALL_INSN, JUMP_INSN */
191 /* The total number of orders. */
193 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
195 /* We use this structure to determine the alignment required for
196 allocations. For power-of-two sized allocations, that's not a
197 problem, but it does matter for odd-sized allocations. */
199 struct max_alignment {
207 /* The biggest alignment required. */
209 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
211 /* Compute the smallest nonnegative number which when added to X gives
214 #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
216 /* Compute the smallest multiple of F that is >= X. */
218 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
220 /* The Ith entry is the number of objects on a page or order I. */
222 static unsigned objects_per_page_table[NUM_ORDERS];
224 /* The Ith entry is the size of an object on a page of order I. */
226 static size_t object_size_table[NUM_ORDERS];
228 /* The Ith entry is a pair of numbers (mult, shift) such that
229 ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
230 for all k evenly divisible by OBJECT_SIZE(I). */
237 inverse_table[NUM_ORDERS];
239 /* A page_entry records the status of an allocation page. This
240 structure is dynamically sized to fit the bitmap in_use_p. */
241 typedef struct page_entry
243 /* The next page-entry with objects of the same size, or NULL if
244 this is the last page-entry. */
245 struct page_entry *next;
247 /* The number of bytes allocated. (This will always be a multiple
248 of the host system page size.) */
251 /* The address at which the memory is allocated. */
254 #ifdef USING_MALLOC_PAGE_GROUPS
255 /* Back pointer to the page group this page came from. */
256 struct page_group *group;
259 /* This is the index in the by_depth varray where this page table
261 unsigned long index_by_depth;
263 /* Context depth of this page. */
264 unsigned short context_depth;
266 /* The number of free objects remaining on this page. */
267 unsigned short num_free_objects;
269 /* A likely candidate for the bit position of a free object for the
270 next allocation from this page. */
271 unsigned short next_bit_hint;
273 /* The lg of size of objects allocated from this page. */
276 /* A bit vector indicating whether or not objects are in use. The
277 Nth bit is one if the Nth object on this page is allocated. This
278 array is dynamically sized. */
279 unsigned long in_use_p[1];
282 #ifdef USING_MALLOC_PAGE_GROUPS
283 /* A page_group describes a large allocation from malloc, from which
284 we parcel out aligned pages. */
285 typedef struct page_group
287 /* A linked list of all extant page groups. */
288 struct page_group *next;
290 /* The address we received from malloc. */
293 /* The size of the block. */
296 /* A bitmask of pages in use. */
301 #if HOST_BITS_PER_PTR <= 32
303 /* On 32-bit hosts, we use a two level page table, as pictured above. */
304 typedef page_entry **page_table[PAGE_L1_SIZE];
308 /* On 64-bit hosts, we use the same two level page tables plus a linked
309 list that disambiguates the top 32-bits. There will almost always be
310 exactly one entry in the list. */
311 typedef struct page_table_chain
313 struct page_table_chain *next;
315 page_entry **table[PAGE_L1_SIZE];
320 /* The rest of the global variables. */
321 static struct globals
323 /* The Nth element in this array is a page with objects of size 2^N.
324 If there are any pages with free objects, they will be at the
325 head of the list. NULL if there are no page-entries for this
327 page_entry *pages[NUM_ORDERS];
329 /* The Nth element in this array is the last page with objects of
330 size 2^N. NULL if there are no page-entries for this object
332 page_entry *page_tails[NUM_ORDERS];
334 /* Lookup table for associating allocation pages with object addresses. */
337 /* The system's page size. */
341 /* Bytes currently allocated. */
344 /* Bytes currently allocated at the end of the last collection. */
345 size_t allocated_last_gc;
347 /* Total amount of memory mapped. */
350 /* Bit N set if any allocations have been done at context depth N. */
351 unsigned long context_depth_allocations;
353 /* Bit N set if any collections have been done at context depth N. */
354 unsigned long context_depth_collections;
356 /* The current depth in the context stack. */
357 unsigned short context_depth;
359 /* A file descriptor open to /dev/zero for reading. */
360 #if defined (HAVE_MMAP_DEV_ZERO)
364 /* A cache of free system pages. */
365 page_entry *free_pages;
367 #ifdef USING_MALLOC_PAGE_GROUPS
368 page_group *page_groups;
371 /* The file descriptor for debugging output. */
374 /* Current number of elements in use in depth below. */
375 unsigned int depth_in_use;
377 /* Maximum number of elements that can be used before resizing. */
378 unsigned int depth_max;
380 /* Each element of this arry is an index in by_depth where the given
381 depth starts. This structure is indexed by that given depth we
382 are interested in. */
385 /* Current number of elements in use in by_depth below. */
386 unsigned int by_depth_in_use;
388 /* Maximum number of elements that can be used before resizing. */
389 unsigned int by_depth_max;
391 /* Each element of this array is a pointer to a page_entry, all
392 page_entries can be found in here by increasing depth.
393 index_by_depth in the page_entry is the index into this data
394 structure where that page_entry can be found. This is used to
395 speed up finding all page_entries at a particular depth. */
396 page_entry **by_depth;
398 /* Each element is a pointer to the saved in_use_p bits, if any,
399 zero otherwise. We allocate them all together, to enable a
400 better runtime data access pattern. */
401 unsigned long **save_in_use;
403 #ifdef GATHER_STATISTICS
406 /* Total memory allocated with ggc_alloc */
407 unsigned long long total_allocated;
408 /* Total overhead for memory to be allocated with ggc_alloc */
409 unsigned long long total_overhead;
411 /* Total allocations and overhead for sizes less than 32, 64 and 128.
412 These sizes are interesting because they are typical cache line
415 unsigned long long total_allocated_under32;
416 unsigned long long total_overhead_under32;
418 unsigned long long total_allocated_under64;
419 unsigned long long total_overhead_under64;
421 unsigned long long total_allocated_under128;
422 unsigned long long total_overhead_under128;
424 /* The overhead for each of the allocation orders. */
425 unsigned long long total_overhead_per_order[NUM_ORDERS];
430 /* The size in bytes required to maintain a bitmap for the objects
432 #define BITMAP_SIZE(Num_objects) \
433 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
435 /* Allocate pages in chunks of this size, to throttle calls to memory
436 allocation routines. The first page is used, the rest go onto the
437 free list. This cannot be larger than HOST_BITS_PER_INT for the
438 in_use bitmask for page_group. */
439 #define GGC_QUIRE_SIZE 16
441 /* Initial guess as to how many page table entries we might need. */
442 #define INITIAL_PTE_COUNT 128
444 static int ggc_allocated_p (const void *);
445 static page_entry *lookup_page_table_entry (const void *);
446 static void set_page_table_entry (void *, page_entry *);
448 static char *alloc_anon (char *, size_t);
450 #ifdef USING_MALLOC_PAGE_GROUPS
451 static size_t page_group_index (char *, char *);
452 static void set_page_group_in_use (page_group *, char *);
453 static void clear_page_group_in_use (page_group *, char *);
455 static struct page_entry * alloc_page (unsigned);
456 static void free_page (struct page_entry *);
457 static void release_pages (void);
458 static void clear_marks (void);
459 static void sweep_pages (void);
460 static void ggc_recalculate_in_use_p (page_entry *);
461 static void compute_inverse (unsigned);
462 static inline void adjust_depth (void);
463 static void move_ptes_to_front (int, int);
465 #ifdef ENABLE_GC_CHECKING
466 static void poison_pages (void);
469 void debug_print_page_list (int);
470 static void push_depth (unsigned int);
471 static void push_by_depth (page_entry *, unsigned long *);
473 /* Push an entry onto G.depth. */
476 push_depth (unsigned int i)
478 if (G.depth_in_use >= G.depth_max)
481 G.depth = (unsigned int *) xrealloc ((char *) G.depth,
482 G.depth_max * sizeof (unsigned int));
484 G.depth[G.depth_in_use++] = i;
487 /* Push an entry onto G.by_depth and G.save_in_use. */
490 push_by_depth (page_entry *p, unsigned long *s)
492 if (G.by_depth_in_use >= G.by_depth_max)
495 G.by_depth = (page_entry **) xrealloc ((char *) G.by_depth,
496 G.by_depth_max * sizeof (page_entry *));
497 G.save_in_use = (unsigned long **) xrealloc ((char *) G.save_in_use,
498 G.by_depth_max * sizeof (unsigned long *));
500 G.by_depth[G.by_depth_in_use] = p;
501 G.save_in_use[G.by_depth_in_use++] = s;
504 #if (GCC_VERSION < 3001)
505 #define prefetch(X) ((void) X)
507 #define prefetch(X) __builtin_prefetch (X)
510 #define save_in_use_p_i(__i) \
512 #define save_in_use_p(__p) \
513 (save_in_use_p_i (__p->index_by_depth))
515 /* Returns nonzero if P was allocated in GC'able memory. */
518 ggc_allocated_p (const void *p)
523 #if HOST_BITS_PER_PTR <= 32
526 page_table table = G.lookup;
527 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
532 if (table->high_bits == high_bits)
536 base = &table->table[0];
539 /* Extract the level 1 and 2 indices. */
543 return base[L1] && base[L1][L2];
546 /* Traverse the page table and find the entry for a page.
547 Die (probably) if the object wasn't allocated via GC. */
549 static inline page_entry *
550 lookup_page_table_entry (const void *p)
555 #if HOST_BITS_PER_PTR <= 32
558 page_table table = G.lookup;
559 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
560 while (table->high_bits != high_bits)
562 base = &table->table[0];
565 /* Extract the level 1 and 2 indices. */
572 /* Set the page table entry for a page. */
575 set_page_table_entry (void *p, page_entry *entry)
580 #if HOST_BITS_PER_PTR <= 32
584 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
585 for (table = G.lookup; table; table = table->next)
586 if (table->high_bits == high_bits)
589 /* Not found -- allocate a new table. */
590 table = (page_table) xcalloc (1, sizeof(*table));
591 table->next = G.lookup;
592 table->high_bits = high_bits;
595 base = &table->table[0];
598 /* Extract the level 1 and 2 indices. */
602 if (base[L1] == NULL)
603 base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
605 base[L1][L2] = entry;
608 /* Prints the page-entry for object size ORDER, for debugging. */
611 debug_print_page_list (int order)
614 printf ("Head=%p, Tail=%p:\n", (void *) G.pages[order],
615 (void *) G.page_tails[order]);
619 printf ("%p(%1d|%3d) -> ", (void *) p, p->context_depth,
620 p->num_free_objects);
628 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
629 (if non-null). The ifdef structure here is intended to cause a
630 compile error unless exactly one of the HAVE_* is defined. */
633 alloc_anon (char *pref ATTRIBUTE_UNUSED, size_t size)
635 #ifdef HAVE_MMAP_ANON
636 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
637 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
639 #ifdef HAVE_MMAP_DEV_ZERO
640 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
641 MAP_PRIVATE, G.dev_zero_fd, 0);
644 if (page == (char *) MAP_FAILED)
646 perror ("virtual memory exhausted");
647 exit (FATAL_EXIT_CODE);
650 /* Remember that we allocated this memory. */
651 G.bytes_mapped += size;
653 /* Pretend we don't have access to the allocated pages. We'll enable
654 access to smaller pieces of the area in ggc_alloc. Discard the
655 handle to avoid handle leak. */
656 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (page, size));
661 #ifdef USING_MALLOC_PAGE_GROUPS
662 /* Compute the index for this page into the page group. */
665 page_group_index (char *allocation, char *page)
667 return (size_t) (page - allocation) >> G.lg_pagesize;
670 /* Set and clear the in_use bit for this page in the page group. */
673 set_page_group_in_use (page_group *group, char *page)
675 group->in_use |= 1 << page_group_index (group->allocation, page);
679 clear_page_group_in_use (page_group *group, char *page)
681 group->in_use &= ~(1 << page_group_index (group->allocation, page));
685 /* Allocate a new page for allocating objects of size 2^ORDER,
686 and return an entry for it. The entry is not added to the
687 appropriate page_table list. */
689 static inline struct page_entry *
690 alloc_page (unsigned order)
692 struct page_entry *entry, *p, **pp;
696 size_t page_entry_size;
698 #ifdef USING_MALLOC_PAGE_GROUPS
702 num_objects = OBJECTS_PER_PAGE (order);
703 bitmap_size = BITMAP_SIZE (num_objects + 1);
704 page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
705 entry_size = num_objects * OBJECT_SIZE (order);
706 if (entry_size < G.pagesize)
707 entry_size = G.pagesize;
712 /* Check the list of free pages for one we can use. */
713 for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
714 if (p->bytes == entry_size)
719 /* Recycle the allocated memory from this page ... */
723 #ifdef USING_MALLOC_PAGE_GROUPS
727 /* ... and, if possible, the page entry itself. */
728 if (p->order == order)
731 memset (entry, 0, page_entry_size);
737 else if (entry_size == G.pagesize)
739 /* We want just one page. Allocate a bunch of them and put the
740 extras on the freelist. (Can only do this optimization with
741 mmap for backing store.) */
742 struct page_entry *e, *f = G.free_pages;
745 page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE);
747 /* This loop counts down so that the chain will be in ascending
749 for (i = GGC_QUIRE_SIZE - 1; i >= 1; i--)
751 e = (struct page_entry *) xcalloc (1, page_entry_size);
753 e->bytes = G.pagesize;
754 e->page = page + (i << G.lg_pagesize);
762 page = alloc_anon (NULL, entry_size);
764 #ifdef USING_MALLOC_PAGE_GROUPS
767 /* Allocate a large block of memory and serve out the aligned
768 pages therein. This results in much less memory wastage
769 than the traditional implementation of valloc. */
771 char *allocation, *a, *enda;
772 size_t alloc_size, head_slop, tail_slop;
773 int multiple_pages = (entry_size == G.pagesize);
776 alloc_size = GGC_QUIRE_SIZE * G.pagesize;
778 alloc_size = entry_size + G.pagesize - 1;
779 allocation = xmalloc (alloc_size);
781 page = (char *) (((size_t) allocation + G.pagesize - 1) & -G.pagesize);
782 head_slop = page - allocation;
784 tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
786 tail_slop = alloc_size - entry_size - head_slop;
787 enda = allocation + alloc_size - tail_slop;
789 /* We allocated N pages, which are likely not aligned, leaving
790 us with N-1 usable pages. We plan to place the page_group
791 structure somewhere in the slop. */
792 if (head_slop >= sizeof (page_group))
793 group = (page_group *)page - 1;
796 /* We magically got an aligned allocation. Too bad, we have
797 to waste a page anyway. */
801 tail_slop += G.pagesize;
803 if (tail_slop < sizeof (page_group))
805 group = (page_group *)enda;
806 tail_slop -= sizeof (page_group);
809 /* Remember that we allocated this memory. */
810 group->next = G.page_groups;
811 group->allocation = allocation;
812 group->alloc_size = alloc_size;
814 G.page_groups = group;
815 G.bytes_mapped += alloc_size;
817 /* If we allocated multiple pages, put the rest on the free list. */
820 struct page_entry *e, *f = G.free_pages;
821 for (a = enda - G.pagesize; a != page; a -= G.pagesize)
823 e = (struct page_entry *) xcalloc (1, page_entry_size);
825 e->bytes = G.pagesize;
837 entry = (struct page_entry *) xcalloc (1, page_entry_size);
839 entry->bytes = entry_size;
841 entry->context_depth = G.context_depth;
842 entry->order = order;
843 entry->num_free_objects = num_objects;
844 entry->next_bit_hint = 1;
846 G.context_depth_allocations |= (unsigned long)1 << G.context_depth;
848 #ifdef USING_MALLOC_PAGE_GROUPS
849 entry->group = group;
850 set_page_group_in_use (group, page);
853 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
854 increment the hint. */
855 entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
856 = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
858 set_page_table_entry (page, entry);
860 if (GGC_DEBUG_LEVEL >= 2)
861 fprintf (G.debug_file,
862 "Allocating page at %p, object size=%lu, data %p-%p\n",
863 (void *) entry, (unsigned long) OBJECT_SIZE (order), page,
864 page + entry_size - 1);
869 /* Adjust the size of G.depth so that no index greater than the one
870 used by the top of the G.by_depth is used. */
877 if (G.by_depth_in_use)
879 top = G.by_depth[G.by_depth_in_use-1];
881 /* Peel back indices in depth that index into by_depth, so that
882 as new elements are added to by_depth, we note the indices
883 of those elements, if they are for new context depths. */
884 while (G.depth_in_use > (size_t)top->context_depth+1)
889 /* For a page that is no longer needed, put it on the free page list. */
892 free_page (page_entry *entry)
894 if (GGC_DEBUG_LEVEL >= 2)
895 fprintf (G.debug_file,
896 "Deallocating page at %p, data %p-%p\n", (void *) entry,
897 entry->page, entry->page + entry->bytes - 1);
899 /* Mark the page as inaccessible. Discard the handle to avoid handle
901 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (entry->page, entry->bytes));
903 set_page_table_entry (entry->page, NULL);
905 #ifdef USING_MALLOC_PAGE_GROUPS
906 clear_page_group_in_use (entry->group, entry->page);
909 if (G.by_depth_in_use > 1)
911 page_entry *top = G.by_depth[G.by_depth_in_use-1];
913 /* If they are at the same depth, put top element into freed
915 if (entry->context_depth == top->context_depth)
917 int i = entry->index_by_depth;
919 G.save_in_use[i] = G.save_in_use[G.by_depth_in_use-1];
920 top->index_by_depth = i;
924 /* We cannot free a page from a context deeper than the
933 entry->next = G.free_pages;
934 G.free_pages = entry;
937 /* Release the free page cache to the system. */
943 page_entry *p, *next;
947 /* Gather up adjacent pages so they are unmapped together. */
958 while (p && p->page == start + len)
967 G.bytes_mapped -= len;
972 #ifdef USING_MALLOC_PAGE_GROUPS
976 /* Remove all pages from free page groups from the list. */
978 while ((p = *pp) != NULL)
979 if (p->group->in_use == 0)
987 /* Remove all free page groups, and release the storage. */
989 while ((g = *gp) != NULL)
993 G.bytes_mapped -= g->alloc_size;
994 free (g->allocation);
1001 /* This table provides a fast way to determine ceil(log_2(size)) for
1002 allocation requests. The minimum allocation size is eight bytes. */
1004 static unsigned char size_lookup[257] =
1006 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
1007 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1008 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1009 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1010 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1011 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1012 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1013 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1014 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1015 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1016 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1017 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1018 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1019 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1020 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1021 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1025 /* Allocate a chunk of memory of SIZE bytes. Its contents are undefined. */
1028 ggc_alloc (size_t size)
1030 unsigned order, word, bit, object_offset;
1031 struct page_entry *entry;
1035 order = size_lookup[size];
1039 while (size > OBJECT_SIZE (order))
1043 /* If there are non-full pages for this size allocation, they are at
1044 the head of the list. */
1045 entry = G.pages[order];
1047 /* If there is no page for this object size, or all pages in this
1048 context are full, allocate a new page. */
1049 if (entry == NULL || entry->num_free_objects == 0)
1051 struct page_entry *new_entry;
1052 new_entry = alloc_page (order);
1054 new_entry->index_by_depth = G.by_depth_in_use;
1055 push_by_depth (new_entry, 0);
1057 /* We can skip context depths, if we do, make sure we go all the
1058 way to the new depth. */
1059 while (new_entry->context_depth >= G.depth_in_use)
1060 push_depth (G.by_depth_in_use-1);
1062 /* If this is the only entry, it's also the tail. */
1064 G.page_tails[order] = new_entry;
1066 /* Put new pages at the head of the page list. */
1067 new_entry->next = entry;
1069 G.pages[order] = new_entry;
1071 /* For a new page, we know the word and bit positions (in the
1072 in_use bitmap) of the first available object -- they're zero. */
1073 new_entry->next_bit_hint = 1;
1080 /* First try to use the hint left from the previous allocation
1081 to locate a clear bit in the in-use bitmap. We've made sure
1082 that the one-past-the-end bit is always set, so if the hint
1083 has run over, this test will fail. */
1084 unsigned hint = entry->next_bit_hint;
1085 word = hint / HOST_BITS_PER_LONG;
1086 bit = hint % HOST_BITS_PER_LONG;
1088 /* If the hint didn't work, scan the bitmap from the beginning. */
1089 if ((entry->in_use_p[word] >> bit) & 1)
1092 while (~entry->in_use_p[word] == 0)
1094 while ((entry->in_use_p[word] >> bit) & 1)
1096 hint = word * HOST_BITS_PER_LONG + bit;
1099 /* Next time, try the next bit. */
1100 entry->next_bit_hint = hint + 1;
1102 object_offset = hint * OBJECT_SIZE (order);
1105 /* Set the in-use bit. */
1106 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
1108 /* Keep a running total of the number of free objects. If this page
1109 fills up, we may have to move it to the end of the list if the
1110 next page isn't full. If the next page is full, all subsequent
1111 pages are full, so there's no need to move it. */
1112 if (--entry->num_free_objects == 0
1113 && entry->next != NULL
1114 && entry->next->num_free_objects > 0)
1116 G.pages[order] = entry->next;
1118 G.page_tails[order]->next = entry;
1119 G.page_tails[order] = entry;
1122 /* Calculate the object's address. */
1123 result = entry->page + object_offset;
1125 #ifdef ENABLE_GC_CHECKING
1126 /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
1127 exact same semantics in presence of memory bugs, regardless of
1128 ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
1129 handle to avoid handle leak. */
1130 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, OBJECT_SIZE (order)));
1132 /* `Poison' the entire allocated object, including any padding at
1134 memset (result, 0xaf, OBJECT_SIZE (order));
1136 /* Make the bytes after the end of the object unaccessible. Discard the
1137 handle to avoid handle leak. */
1138 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) result + size,
1139 OBJECT_SIZE (order) - size));
1142 /* Tell Valgrind that the memory is there, but its content isn't
1143 defined. The bytes at the end of the object are still marked
1145 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, size));
1147 /* Keep track of how many bytes are being allocated. This
1148 information is used in deciding when to collect. */
1149 G.allocated += OBJECT_SIZE (order);
1151 #ifdef GATHER_STATISTICS
1153 G.stats.total_overhead += OBJECT_SIZE (order) - size;
1154 G.stats.total_overhead_per_order[order] += OBJECT_SIZE (order) - size;
1155 G.stats.total_allocated += OBJECT_SIZE(order);
1158 G.stats.total_overhead_under32 += OBJECT_SIZE (order) - size;
1159 G.stats.total_allocated_under32 += OBJECT_SIZE(order);
1163 G.stats.total_overhead_under64 += OBJECT_SIZE (order) - size;
1164 G.stats.total_allocated_under64 += OBJECT_SIZE(order);
1168 G.stats.total_overhead_under128 += OBJECT_SIZE (order) - size;
1169 G.stats.total_allocated_under128 += OBJECT_SIZE(order);
1175 if (GGC_DEBUG_LEVEL >= 3)
1176 fprintf (G.debug_file,
1177 "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
1178 (unsigned long) size, (unsigned long) OBJECT_SIZE (order), result,
1184 /* If P is not marked, marks it and return false. Otherwise return true.
1185 P must have been allocated by the GC allocator; it mustn't point to
1186 static objects, stack variables, or memory allocated with malloc. */
1189 ggc_set_mark (const void *p)
1195 /* Look up the page on which the object is alloced. If the object
1196 wasn't allocated by the collector, we'll probably die. */
1197 entry = lookup_page_table_entry (p);
1198 #ifdef ENABLE_CHECKING
1203 /* Calculate the index of the object on the page; this is its bit
1204 position in the in_use_p bitmap. */
1205 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1206 word = bit / HOST_BITS_PER_LONG;
1207 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1209 /* If the bit was previously set, skip it. */
1210 if (entry->in_use_p[word] & mask)
1213 /* Otherwise set it, and decrement the free object count. */
1214 entry->in_use_p[word] |= mask;
1215 entry->num_free_objects -= 1;
1217 if (GGC_DEBUG_LEVEL >= 4)
1218 fprintf (G.debug_file, "Marking %p\n", p);
1223 /* Return 1 if P has been marked, zero otherwise.
1224 P must have been allocated by the GC allocator; it mustn't point to
1225 static objects, stack variables, or memory allocated with malloc. */
1228 ggc_marked_p (const void *p)
1234 /* Look up the page on which the object is alloced. If the object
1235 wasn't allocated by the collector, we'll probably die. */
1236 entry = lookup_page_table_entry (p);
1237 #ifdef ENABLE_CHECKING
1242 /* Calculate the index of the object on the page; this is its bit
1243 position in the in_use_p bitmap. */
1244 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1245 word = bit / HOST_BITS_PER_LONG;
1246 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1248 return (entry->in_use_p[word] & mask) != 0;
1251 /* Return the size of the gc-able object P. */
1254 ggc_get_size (const void *p)
1256 page_entry *pe = lookup_page_table_entry (p);
1257 return OBJECT_SIZE (pe->order);
1260 /* Subroutine of init_ggc which computes the pair of numbers used to
1261 perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1263 This algorithm is taken from Granlund and Montgomery's paper
1264 "Division by Invariant Integers using Multiplication"
1265 (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1269 compute_inverse (unsigned order)
1274 size = OBJECT_SIZE (order);
1276 while (size % 2 == 0)
1283 while (inv * size != 1)
1284 inv = inv * (2 - inv*size);
1286 DIV_MULT (order) = inv;
1287 DIV_SHIFT (order) = e;
1290 /* Initialize the ggc-mmap allocator. */
1296 G.pagesize = getpagesize();
1297 G.lg_pagesize = exact_log2 (G.pagesize);
1299 #ifdef HAVE_MMAP_DEV_ZERO
1300 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1301 if (G.dev_zero_fd == -1)
1302 internal_error ("open /dev/zero: %m");
1306 G.debug_file = fopen ("ggc-mmap.debug", "w");
1308 G.debug_file = stdout;
1312 /* StunOS has an amazing off-by-one error for the first mmap allocation
1313 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1314 believe, is an unaligned page allocation, which would cause us to
1315 hork badly if we tried to use it. */
1317 char *p = alloc_anon (NULL, G.pagesize);
1318 struct page_entry *e;
1319 if ((size_t)p & (G.pagesize - 1))
1321 /* How losing. Discard this one and try another. If we still
1322 can't get something useful, give up. */
1324 p = alloc_anon (NULL, G.pagesize);
1325 if ((size_t)p & (G.pagesize - 1))
1329 /* We have a good page, might as well hold onto it... */
1330 e = (struct page_entry *) xcalloc (1, sizeof (struct page_entry));
1331 e->bytes = G.pagesize;
1333 e->next = G.free_pages;
1338 /* Initialize the object size table. */
1339 for (order = 0; order < HOST_BITS_PER_PTR; ++order)
1340 object_size_table[order] = (size_t) 1 << order;
1341 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1343 size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
1345 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1346 so that we're sure of getting aligned memory. */
1347 s = ROUND_UP (s, MAX_ALIGNMENT);
1348 object_size_table[order] = s;
1351 /* Initialize the objects-per-page and inverse tables. */
1352 for (order = 0; order < NUM_ORDERS; ++order)
1354 objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
1355 if (objects_per_page_table[order] == 0)
1356 objects_per_page_table[order] = 1;
1357 compute_inverse (order);
1360 /* Reset the size_lookup array to put appropriately sized objects in
1361 the special orders. All objects bigger than the previous power
1362 of two, but no greater than the special size, should go in the
1364 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1369 o = size_lookup[OBJECT_SIZE (order)];
1370 for (i = OBJECT_SIZE (order); size_lookup [i] == o; --i)
1371 size_lookup[i] = order;
1376 G.depth = (unsigned int *) xmalloc (G.depth_max * sizeof (unsigned int));
1378 G.by_depth_in_use = 0;
1379 G.by_depth_max = INITIAL_PTE_COUNT;
1380 G.by_depth = (page_entry **) xmalloc (G.by_depth_max * sizeof (page_entry *));
1381 G.save_in_use = (unsigned long **) xmalloc (G.by_depth_max * sizeof (unsigned long *));
1384 /* Increment the `GC context'. Objects allocated in an outer context
1385 are never freed, eliminating the need to register their roots. */
1388 ggc_push_context (void)
1393 if (G.context_depth >= HOST_BITS_PER_LONG)
1397 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1398 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1401 ggc_recalculate_in_use_p (page_entry *p)
1406 /* Because the past-the-end bit in in_use_p is always set, we
1407 pretend there is one additional object. */
1408 num_objects = OBJECTS_IN_PAGE (p) + 1;
1410 /* Reset the free object count. */
1411 p->num_free_objects = num_objects;
1413 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1415 i < CEIL (BITMAP_SIZE (num_objects),
1416 sizeof (*p->in_use_p));
1421 /* Something is in use if it is marked, or if it was in use in a
1422 context further down the context stack. */
1423 p->in_use_p[i] |= save_in_use_p (p)[i];
1425 /* Decrement the free object count for every object allocated. */
1426 for (j = p->in_use_p[i]; j; j >>= 1)
1427 p->num_free_objects -= (j & 1);
1430 if (p->num_free_objects >= num_objects)
1434 /* Decrement the `GC context'. All objects allocated since the
1435 previous ggc_push_context are migrated to the outer context. */
1438 ggc_pop_context (void)
1440 unsigned long omask;
1441 unsigned int depth, i, e;
1442 #ifdef ENABLE_CHECKING
1446 depth = --G.context_depth;
1447 omask = (unsigned long)1 << (depth + 1);
1449 if (!((G.context_depth_allocations | G.context_depth_collections) & omask))
1452 G.context_depth_allocations |= (G.context_depth_allocations & omask) >> 1;
1453 G.context_depth_allocations &= omask - 1;
1454 G.context_depth_collections &= omask - 1;
1456 /* The G.depth array is shortend so that the last index is the
1457 context_depth of the top element of by_depth. */
1458 if (depth+1 < G.depth_in_use)
1459 e = G.depth[depth+1];
1461 e = G.by_depth_in_use;
1463 /* We might not have any PTEs of depth depth. */
1464 if (depth < G.depth_in_use)
1467 /* First we go through all the pages at depth depth to
1468 recalculate the in use bits. */
1469 for (i = G.depth[depth]; i < e; ++i)
1473 #ifdef ENABLE_CHECKING
1476 /* Check that all of the pages really are at the depth that
1478 if (p->context_depth != depth)
1480 if (p->index_by_depth != i)
1484 prefetch (&save_in_use_p_i (i+8));
1485 prefetch (&save_in_use_p_i (i+16));
1486 if (save_in_use_p_i (i))
1489 ggc_recalculate_in_use_p (p);
1490 free (save_in_use_p_i (i));
1491 save_in_use_p_i (i) = 0;
1496 /* Then, we reset all page_entries with a depth greater than depth
1498 for (i = e; i < G.by_depth_in_use; ++i)
1500 page_entry *p = G.by_depth[i];
1502 /* Check that all of the pages really are at the depth we
1504 #ifdef ENABLE_CHECKING
1505 if (p->context_depth <= depth)
1507 if (p->index_by_depth != i)
1510 p->context_depth = depth;
1515 #ifdef ENABLE_CHECKING
1516 for (order = 2; order < NUM_ORDERS; order++)
1520 for (p = G.pages[order]; p != NULL; p = p->next)
1522 if (p->context_depth > depth)
1524 else if (p->context_depth == depth && save_in_use_p (p))
1531 /* Unmark all objects. */
1538 for (order = 2; order < NUM_ORDERS; order++)
1542 for (p = G.pages[order]; p != NULL; p = p->next)
1544 size_t num_objects = OBJECTS_IN_PAGE (p);
1545 size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
1547 #ifdef ENABLE_CHECKING
1548 /* The data should be page-aligned. */
1549 if ((size_t) p->page & (G.pagesize - 1))
1553 /* Pages that aren't in the topmost context are not collected;
1554 nevertheless, we need their in-use bit vectors to store GC
1555 marks. So, back them up first. */
1556 if (p->context_depth < G.context_depth)
1558 if (! save_in_use_p (p))
1559 save_in_use_p (p) = xmalloc (bitmap_size);
1560 memcpy (save_in_use_p (p), p->in_use_p, bitmap_size);
1563 /* Reset reset the number of free objects and clear the
1564 in-use bits. These will be adjusted by mark_obj. */
1565 p->num_free_objects = num_objects;
1566 memset (p->in_use_p, 0, bitmap_size);
1568 /* Make sure the one-past-the-end bit is always set. */
1569 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
1570 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
1575 /* Free all empty pages. Partially empty pages need no attention
1576 because the `mark' bit doubles as an `unused' bit. */
1583 for (order = 2; order < NUM_ORDERS; order++)
1585 /* The last page-entry to consider, regardless of entries
1586 placed at the end of the list. */
1587 page_entry * const last = G.page_tails[order];
1590 size_t live_objects;
1591 page_entry *p, *previous;
1601 page_entry *next = p->next;
1603 /* Loop until all entries have been examined. */
1606 num_objects = OBJECTS_IN_PAGE (p);
1608 /* Add all live objects on this page to the count of
1609 allocated memory. */
1610 live_objects = num_objects - p->num_free_objects;
1612 G.allocated += OBJECT_SIZE (order) * live_objects;
1614 /* Only objects on pages in the topmost context should get
1616 if (p->context_depth < G.context_depth)
1619 /* Remove the page if it's empty. */
1620 else if (live_objects == 0)
1623 G.pages[order] = next;
1625 previous->next = next;
1627 /* Are we removing the last element? */
1628 if (p == G.page_tails[order])
1629 G.page_tails[order] = previous;
1634 /* If the page is full, move it to the end. */
1635 else if (p->num_free_objects == 0)
1637 /* Don't move it if it's already at the end. */
1638 if (p != G.page_tails[order])
1640 /* Move p to the end of the list. */
1642 G.page_tails[order]->next = p;
1644 /* Update the tail pointer... */
1645 G.page_tails[order] = p;
1647 /* ... and the head pointer, if necessary. */
1649 G.pages[order] = next;
1651 previous->next = next;
1656 /* If we've fallen through to here, it's a page in the
1657 topmost context that is neither full nor empty. Such a
1658 page must precede pages at lesser context depth in the
1659 list, so move it to the head. */
1660 else if (p != G.pages[order])
1662 previous->next = p->next;
1663 p->next = G.pages[order];
1665 /* Are we moving the last element? */
1666 if (G.page_tails[order] == p)
1667 G.page_tails[order] = previous;
1676 /* Now, restore the in_use_p vectors for any pages from contexts
1677 other than the current one. */
1678 for (p = G.pages[order]; p; p = p->next)
1679 if (p->context_depth != G.context_depth)
1680 ggc_recalculate_in_use_p (p);
1684 #ifdef ENABLE_GC_CHECKING
1685 /* Clobber all free objects. */
1692 for (order = 2; order < NUM_ORDERS; order++)
1694 size_t size = OBJECT_SIZE (order);
1697 for (p = G.pages[order]; p != NULL; p = p->next)
1702 if (p->context_depth != G.context_depth)
1703 /* Since we don't do any collection for pages in pushed
1704 contexts, there's no need to do any poisoning. And
1705 besides, the IN_USE_P array isn't valid until we pop
1709 num_objects = OBJECTS_IN_PAGE (p);
1710 for (i = 0; i < num_objects; i++)
1713 word = i / HOST_BITS_PER_LONG;
1714 bit = i % HOST_BITS_PER_LONG;
1715 if (((p->in_use_p[word] >> bit) & 1) == 0)
1717 char *object = p->page + i * size;
1719 /* Keep poison-by-write when we expect to use Valgrind,
1720 so the exact same memory semantics is kept, in case
1721 there are memory errors. We override this request
1723 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (object, size));
1724 memset (object, 0xa5, size);
1726 /* Drop the handle to avoid handle leak. */
1727 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (object, size));
1735 /* Top level mark-and-sweep routine. */
1740 /* Avoid frequent unnecessary work by skipping collection if the
1741 total allocations haven't expanded much since the last
1743 float allocated_last_gc =
1744 MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
1746 float min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
1748 if (G.allocated < allocated_last_gc + min_expand)
1751 timevar_push (TV_GC);
1753 fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
1755 /* Zero the total allocated bytes. This will be recalculated in the
1759 /* Release the pages we freed the last time we collected, but didn't
1760 reuse in the interim. */
1763 /* Indicate that we've seen collections at this context depth. */
1764 G.context_depth_collections = ((unsigned long)1 << (G.context_depth + 1)) - 1;
1769 #ifdef ENABLE_GC_CHECKING
1775 G.allocated_last_gc = G.allocated;
1777 timevar_pop (TV_GC);
1780 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
1783 /* Print allocation statistics. */
1784 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1786 : ((x) < 1024*1024*10 \
1788 : (x) / (1024*1024))))
1789 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1792 ggc_print_statistics (void)
1794 struct ggc_statistics stats;
1796 size_t total_overhead = 0;
1798 /* Clear the statistics. */
1799 memset (&stats, 0, sizeof (stats));
1801 /* Make sure collection will really occur. */
1802 G.allocated_last_gc = 0;
1804 /* Collect and print the statistics common across collectors. */
1805 ggc_print_common_statistics (stderr, &stats);
1807 /* Release free pages so that we will not count the bytes allocated
1808 there as part of the total allocated memory. */
1811 /* Collect some information about the various sizes of
1813 fprintf (stderr, "%-5s %10s %10s %10s\n",
1814 "Size", "Allocated", "Used", "Overhead");
1815 for (i = 0; i < NUM_ORDERS; ++i)
1822 /* Skip empty entries. */
1826 overhead = allocated = in_use = 0;
1828 /* Figure out the total number of bytes allocated for objects of
1829 this size, and how many of them are actually in use. Also figure
1830 out how much memory the page table is using. */
1831 for (p = G.pages[i]; p; p = p->next)
1833 allocated += p->bytes;
1835 (OBJECTS_IN_PAGE (p) - p->num_free_objects) * OBJECT_SIZE (i);
1837 overhead += (sizeof (page_entry) - sizeof (long)
1838 + BITMAP_SIZE (OBJECTS_IN_PAGE (p) + 1));
1840 fprintf (stderr, "%-5lu %10lu%c %10lu%c %10lu%c\n",
1841 (unsigned long) OBJECT_SIZE (i),
1842 SCALE (allocated), LABEL (allocated),
1843 SCALE (in_use), LABEL (in_use),
1844 SCALE (overhead), LABEL (overhead));
1845 total_overhead += overhead;
1847 fprintf (stderr, "%-5s %10lu%c %10lu%c %10lu%c\n", "Total",
1848 SCALE (G.bytes_mapped), LABEL (G.bytes_mapped),
1849 SCALE (G.allocated), LABEL(G.allocated),
1850 SCALE (total_overhead), LABEL (total_overhead));
1852 #ifdef GATHER_STATISTICS
1854 fprintf (stderr, "Total Overhead: %10lld\n",
1855 G.stats.total_overhead);
1856 fprintf (stderr, "Total Allocated: %10lld\n",
1857 G.stats.total_allocated);
1859 fprintf (stderr, "Total Overhead under 32B: %10lld\n",
1860 G.stats.total_overhead_under32);
1861 fprintf (stderr, "Total Allocated under 32B: %10lld\n",
1862 G.stats.total_allocated_under32);
1863 fprintf (stderr, "Total Overhead under 64B: %10lld\n",
1864 G.stats.total_overhead_under64);
1865 fprintf (stderr, "Total Allocated under 64B: %10lld\n",
1866 G.stats.total_allocated_under64);
1867 fprintf (stderr, "Total Overhead under 128B: %10lld\n",
1868 G.stats.total_overhead_under128);
1869 fprintf (stderr, "Total Allocated under 128B: %10lld\n",
1870 G.stats.total_allocated_under128);
1872 for (i = 0; i < NUM_ORDERS; i++)
1873 if (G.stats.total_overhead_per_order[i])
1874 fprintf (stderr, "Total Overhead page size %7d: %10lld\n",
1875 OBJECT_SIZE (i), G.stats.total_overhead_per_order[i]);
1882 struct ggc_pch_ondisk
1884 unsigned totals[NUM_ORDERS];
1886 size_t base[NUM_ORDERS];
1887 size_t written[NUM_ORDERS];
1890 struct ggc_pch_data *
1893 return xcalloc (sizeof (struct ggc_pch_data), 1);
1897 ggc_pch_count_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
1903 order = size_lookup[size];
1907 while (size > OBJECT_SIZE (order))
1911 d->d.totals[order]++;
1915 ggc_pch_total_size (struct ggc_pch_data *d)
1920 for (i = 0; i < NUM_ORDERS; i++)
1921 a += ROUND_UP (d->d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1926 ggc_pch_this_base (struct ggc_pch_data *d, void *base)
1928 size_t a = (size_t) base;
1931 for (i = 0; i < NUM_ORDERS; i++)
1934 a += ROUND_UP (d->d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1940 ggc_pch_alloc_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
1947 order = size_lookup[size];
1951 while (size > OBJECT_SIZE (order))
1955 result = (char *) d->base[order];
1956 d->base[order] += OBJECT_SIZE (order);
1961 ggc_pch_prepare_write (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
1962 FILE *f ATTRIBUTE_UNUSED)
1964 /* Nothing to do. */
1968 ggc_pch_write_object (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
1969 FILE *f, void *x, void *newx ATTRIBUTE_UNUSED,
1975 order = size_lookup[size];
1979 while (size > OBJECT_SIZE (order))
1983 if (fwrite (x, size, 1, f) != 1)
1984 fatal_error ("can't write PCH file: %m");
1986 /* In the current implementation, SIZE is always equal to
1987 OBJECT_SIZE (order) and so the fseek is never executed. */
1988 if (size != OBJECT_SIZE (order)
1989 && fseek (f, OBJECT_SIZE (order) - size, SEEK_CUR) != 0)
1990 fatal_error ("can't write PCH file: %m");
1992 d->written[order]++;
1993 if (d->written[order] == d->d.totals[order]
1994 && fseek (f, ROUND_UP_VALUE (d->d.totals[order] * OBJECT_SIZE (order),
1997 fatal_error ("can't write PCH file: %m");
2001 ggc_pch_finish (struct ggc_pch_data *d, FILE *f)
2003 if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
2004 fatal_error ("can't write PCH file: %m");
2008 /* Move the PCH PTE entries just added to the end of by_depth, to the
2012 move_ptes_to_front (int count_old_page_tables, int count_new_page_tables)
2016 /* First, we swap the new entries to the front of the varrays. */
2017 page_entry **new_by_depth;
2018 unsigned long **new_save_in_use;
2020 new_by_depth = (page_entry **) xmalloc (G.by_depth_max * sizeof (page_entry *));
2021 new_save_in_use = (unsigned long **) xmalloc (G.by_depth_max * sizeof (unsigned long *));
2023 memcpy (&new_by_depth[0],
2024 &G.by_depth[count_old_page_tables],
2025 count_new_page_tables * sizeof (void *));
2026 memcpy (&new_by_depth[count_new_page_tables],
2028 count_old_page_tables * sizeof (void *));
2029 memcpy (&new_save_in_use[0],
2030 &G.save_in_use[count_old_page_tables],
2031 count_new_page_tables * sizeof (void *));
2032 memcpy (&new_save_in_use[count_new_page_tables],
2034 count_old_page_tables * sizeof (void *));
2037 free (G.save_in_use);
2039 G.by_depth = new_by_depth;
2040 G.save_in_use = new_save_in_use;
2042 /* Now update all the index_by_depth fields. */
2043 for (i = G.by_depth_in_use; i > 0; --i)
2045 page_entry *p = G.by_depth[i-1];
2046 p->index_by_depth = i-1;
2049 /* And last, we update the depth pointers in G.depth. The first
2050 entry is already 0, and context 0 entries always start at index
2051 0, so there is nothing to update in the first slot. We need a
2052 second slot, only if we have old ptes, and if we do, they start
2053 at index count_new_page_tables. */
2054 if (count_old_page_tables)
2055 push_depth (count_new_page_tables);
2059 ggc_pch_read (FILE *f, void *addr)
2061 struct ggc_pch_ondisk d;
2064 unsigned long count_old_page_tables;
2065 unsigned long count_new_page_tables;
2067 count_old_page_tables = G.by_depth_in_use;
2069 /* We've just read in a PCH file. So, every object that used to be
2070 allocated is now free. */
2076 /* No object read from a PCH file should ever be freed. So, set the
2077 context depth to 1, and set the depth of all the currently-allocated
2078 pages to be 1 too. PCH pages will have depth 0. */
2079 if (G.context_depth != 0)
2081 G.context_depth = 1;
2082 for (i = 0; i < NUM_ORDERS; i++)
2085 for (p = G.pages[i]; p != NULL; p = p->next)
2086 p->context_depth = G.context_depth;
2089 /* Allocate the appropriate page-table entries for the pages read from
2091 if (fread (&d, sizeof (d), 1, f) != 1)
2092 fatal_error ("can't read PCH file: %m");
2094 for (i = 0; i < NUM_ORDERS; i++)
2096 struct page_entry *entry;
2102 if (d.totals[i] == 0)
2105 bytes = ROUND_UP (d.totals[i] * OBJECT_SIZE (i), G.pagesize);
2106 num_objs = bytes / OBJECT_SIZE (i);
2107 entry = xcalloc (1, (sizeof (struct page_entry)
2109 + BITMAP_SIZE (num_objs + 1)));
2110 entry->bytes = bytes;
2112 entry->context_depth = 0;
2114 entry->num_free_objects = 0;
2118 j + HOST_BITS_PER_LONG <= num_objs + 1;
2119 j += HOST_BITS_PER_LONG)
2120 entry->in_use_p[j / HOST_BITS_PER_LONG] = -1;
2121 for (; j < num_objs + 1; j++)
2122 entry->in_use_p[j / HOST_BITS_PER_LONG]
2123 |= 1L << (j % HOST_BITS_PER_LONG);
2125 for (pte = entry->page;
2126 pte < entry->page + entry->bytes;
2128 set_page_table_entry (pte, entry);
2130 if (G.page_tails[i] != NULL)
2131 G.page_tails[i]->next = entry;
2134 G.page_tails[i] = entry;
2136 /* We start off by just adding all the new information to the
2137 end of the varrays, later, we will move the new information
2138 to the front of the varrays, as the PCH page tables are at
2140 push_by_depth (entry, 0);
2143 /* Now, we update the various data structures that speed page table
2145 count_new_page_tables = G.by_depth_in_use - count_old_page_tables;
2147 move_ptes_to_front (count_old_page_tables, count_new_page_tables);
2149 /* Update the statistics. */
2150 G.allocated = G.allocated_last_gc = offs - (char *)addr;