PR target/11183
[official-gcc.git] / gcc / ggc-page.c
blob929ef8e41cc6d19e59c862bdc7c2fbf06b408744
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 "flags.h"
30 #include "ggc.h"
31 #include "timevar.h"
32 #include "params.h"
33 #ifdef ENABLE_VALGRIND_CHECKING
34 # ifdef HAVE_MEMCHECK_H
35 # include <memcheck.h>
36 # else
37 # include <valgrind.h>
38 # endif
39 #else
40 /* Avoid #ifdef:s when we can help it. */
41 #define VALGRIND_DISCARD(x)
42 #endif
44 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
45 file open. Prefer either to valloc. */
46 #ifdef HAVE_MMAP_ANON
47 # undef HAVE_MMAP_DEV_ZERO
49 # include <sys/mman.h>
50 # ifndef MAP_FAILED
51 # define MAP_FAILED -1
52 # endif
53 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
54 # define MAP_ANONYMOUS MAP_ANON
55 # endif
56 # define USING_MMAP
58 #endif
60 #ifdef HAVE_MMAP_DEV_ZERO
62 # include <sys/mman.h>
63 # ifndef MAP_FAILED
64 # define MAP_FAILED -1
65 # endif
66 # define USING_MMAP
68 #endif
70 #ifndef USING_MMAP
71 #define USING_MALLOC_PAGE_GROUPS
72 #endif
74 /* Stategy:
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
96 context depth.
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
113 #endif
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:
120 HOST_PAGE_SIZE_BITS
121 32 | |
122 msb +----------------+----+------+------+ lsb
123 | | |
124 PAGE_L1_BITS |
126 PAGE_L2_BITS
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
136 correct one. */
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
169 objects. */
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),
186 TREE_EXP_SIZE (2),
187 RTL_SIZE (2), /* REG, MEM, PLUS, etc. */
188 RTL_SIZE (10), /* 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 {
200 char c;
201 union {
202 HOST_WIDEST_INT i;
203 #ifdef HAVE_LONG_DOUBLE
204 long double d;
205 #else
206 double d;
207 #endif
208 } u;
211 /* The biggest alignment required. */
213 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
215 /* Compute the smallest nonnegative number which when added to X gives
216 a multiple of F. */
218 #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
220 /* Compute the smallest multiple of F that is >= X. */
222 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
224 /* The Ith entry is the number of objects on a page or order I. */
226 static unsigned objects_per_page_table[NUM_ORDERS];
228 /* The Ith entry is the size of an object on a page of order I. */
230 static size_t object_size_table[NUM_ORDERS];
232 /* The Ith entry is a pair of numbers (mult, shift) such that
233 ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
234 for all k evenly divisible by OBJECT_SIZE(I). */
236 static struct
238 unsigned int mult;
239 unsigned int shift;
241 inverse_table[NUM_ORDERS];
243 /* A page_entry records the status of an allocation page. This
244 structure is dynamically sized to fit the bitmap in_use_p. */
245 typedef struct page_entry
247 /* The next page-entry with objects of the same size, or NULL if
248 this is the last page-entry. */
249 struct page_entry *next;
251 /* The number of bytes allocated. (This will always be a multiple
252 of the host system page size.) */
253 size_t bytes;
255 /* The address at which the memory is allocated. */
256 char *page;
258 #ifdef USING_MALLOC_PAGE_GROUPS
259 /* Back pointer to the page group this page came from. */
260 struct page_group *group;
261 #endif
263 /* This is the index in the by_depth varray where this page table
264 can be found. */
265 unsigned long index_by_depth;
267 /* Context depth of this page. */
268 unsigned short context_depth;
270 /* The number of free objects remaining on this page. */
271 unsigned short num_free_objects;
273 /* A likely candidate for the bit position of a free object for the
274 next allocation from this page. */
275 unsigned short next_bit_hint;
277 /* The lg of size of objects allocated from this page. */
278 unsigned char order;
280 /* A bit vector indicating whether or not objects are in use. The
281 Nth bit is one if the Nth object on this page is allocated. This
282 array is dynamically sized. */
283 unsigned long in_use_p[1];
284 } page_entry;
286 #ifdef USING_MALLOC_PAGE_GROUPS
287 /* A page_group describes a large allocation from malloc, from which
288 we parcel out aligned pages. */
289 typedef struct page_group
291 /* A linked list of all extant page groups. */
292 struct page_group *next;
294 /* The address we received from malloc. */
295 char *allocation;
297 /* The size of the block. */
298 size_t alloc_size;
300 /* A bitmask of pages in use. */
301 unsigned int in_use;
302 } page_group;
303 #endif
305 #if HOST_BITS_PER_PTR <= 32
307 /* On 32-bit hosts, we use a two level page table, as pictured above. */
308 typedef page_entry **page_table[PAGE_L1_SIZE];
310 #else
312 /* On 64-bit hosts, we use the same two level page tables plus a linked
313 list that disambiguates the top 32-bits. There will almost always be
314 exactly one entry in the list. */
315 typedef struct page_table_chain
317 struct page_table_chain *next;
318 size_t high_bits;
319 page_entry **table[PAGE_L1_SIZE];
320 } *page_table;
322 #endif
324 /* The rest of the global variables. */
325 static struct globals
327 /* The Nth element in this array is a page with objects of size 2^N.
328 If there are any pages with free objects, they will be at the
329 head of the list. NULL if there are no page-entries for this
330 object size. */
331 page_entry *pages[NUM_ORDERS];
333 /* The Nth element in this array is the last page with objects of
334 size 2^N. NULL if there are no page-entries for this object
335 size. */
336 page_entry *page_tails[NUM_ORDERS];
338 /* Lookup table for associating allocation pages with object addresses. */
339 page_table lookup;
341 /* The system's page size. */
342 size_t pagesize;
343 size_t lg_pagesize;
345 /* Bytes currently allocated. */
346 size_t allocated;
348 /* Bytes currently allocated at the end of the last collection. */
349 size_t allocated_last_gc;
351 /* Total amount of memory mapped. */
352 size_t bytes_mapped;
354 /* Bit N set if any allocations have been done at context depth N. */
355 unsigned long context_depth_allocations;
357 /* Bit N set if any collections have been done at context depth N. */
358 unsigned long context_depth_collections;
360 /* The current depth in the context stack. */
361 unsigned short context_depth;
363 /* A file descriptor open to /dev/zero for reading. */
364 #if defined (HAVE_MMAP_DEV_ZERO)
365 int dev_zero_fd;
366 #endif
368 /* A cache of free system pages. */
369 page_entry *free_pages;
371 #ifdef USING_MALLOC_PAGE_GROUPS
372 page_group *page_groups;
373 #endif
375 /* The file descriptor for debugging output. */
376 FILE *debug_file;
378 /* Current number of elements in use in depth below. */
379 unsigned int depth_in_use;
381 /* Maximum number of elements that can be used before resizing. */
382 unsigned int depth_max;
384 /* Each element of this arry is an index in by_depth where the given
385 depth starts. This structure is indexed by that given depth we
386 are interested in. */
387 unsigned int *depth;
389 /* Current number of elements in use in by_depth below. */
390 unsigned int by_depth_in_use;
392 /* Maximum number of elements that can be used before resizing. */
393 unsigned int by_depth_max;
395 /* Each element of this array is a pointer to a page_entry, all
396 page_entries can be found in here by increasing depth.
397 index_by_depth in the page_entry is the index into this data
398 structure where that page_entry can be found. This is used to
399 speed up finding all page_entries at a particular depth. */
400 page_entry **by_depth;
402 /* Each element is a pointer to the saved in_use_p bits, if any,
403 zero otherwise. We allocate them all together, to enable a
404 better runtime data access pattern. */
405 unsigned long **save_in_use;
407 } G;
409 /* The size in bytes required to maintain a bitmap for the objects
410 on a page-entry. */
411 #define BITMAP_SIZE(Num_objects) \
412 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
414 /* Allocate pages in chunks of this size, to throttle calls to memory
415 allocation routines. The first page is used, the rest go onto the
416 free list. This cannot be larger than HOST_BITS_PER_INT for the
417 in_use bitmask for page_group. */
418 #define GGC_QUIRE_SIZE 16
420 /* Initial guess as to how many page table entries we might need. */
421 #define INITIAL_PTE_COUNT 128
423 static int ggc_allocated_p (const void *);
424 static page_entry *lookup_page_table_entry (const void *);
425 static void set_page_table_entry (void *, page_entry *);
426 #ifdef USING_MMAP
427 static char *alloc_anon (char *, size_t);
428 #endif
429 #ifdef USING_MALLOC_PAGE_GROUPS
430 static size_t page_group_index (char *, char *);
431 static void set_page_group_in_use (page_group *, char *);
432 static void clear_page_group_in_use (page_group *, char *);
433 #endif
434 static struct page_entry * alloc_page (unsigned);
435 static void free_page (struct page_entry *);
436 static void release_pages (void);
437 static void clear_marks (void);
438 static void sweep_pages (void);
439 static void ggc_recalculate_in_use_p (page_entry *);
440 static void compute_inverse (unsigned);
441 static inline void adjust_depth (void);
442 static void move_ptes_to_front (int, int);
444 #ifdef ENABLE_GC_CHECKING
445 static void poison_pages (void);
446 #endif
448 void debug_print_page_list (int);
449 static void push_depth (unsigned int);
450 static void push_by_depth (page_entry *, unsigned long *);
452 /* Push an entry onto G.depth. */
454 inline static void
455 push_depth (unsigned int i)
457 if (G.depth_in_use >= G.depth_max)
459 G.depth_max *= 2;
460 G.depth = (unsigned int *) xrealloc ((char *) G.depth,
461 G.depth_max * sizeof (unsigned int));
463 G.depth[G.depth_in_use++] = i;
466 /* Push an entry onto G.by_depth and G.save_in_use. */
468 inline static void
469 push_by_depth (page_entry *p, unsigned long *s)
471 if (G.by_depth_in_use >= G.by_depth_max)
473 G.by_depth_max *= 2;
474 G.by_depth = (page_entry **) xrealloc ((char *) G.by_depth,
475 G.by_depth_max * sizeof (page_entry *));
476 G.save_in_use = (unsigned long **) xrealloc ((char *) G.save_in_use,
477 G.by_depth_max * sizeof (unsigned long *));
479 G.by_depth[G.by_depth_in_use] = p;
480 G.save_in_use[G.by_depth_in_use++] = s;
483 #if (GCC_VERSION < 3001)
484 #define prefetch(X) ((void) X)
485 #else
486 #define prefetch(X) __builtin_prefetch (X)
487 #endif
489 #define save_in_use_p_i(__i) \
490 (G.save_in_use[__i])
491 #define save_in_use_p(__p) \
492 (save_in_use_p_i (__p->index_by_depth))
494 /* Returns nonzero if P was allocated in GC'able memory. */
496 static inline int
497 ggc_allocated_p (const void *p)
499 page_entry ***base;
500 size_t L1, L2;
502 #if HOST_BITS_PER_PTR <= 32
503 base = &G.lookup[0];
504 #else
505 page_table table = G.lookup;
506 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
507 while (1)
509 if (table == NULL)
510 return 0;
511 if (table->high_bits == high_bits)
512 break;
513 table = table->next;
515 base = &table->table[0];
516 #endif
518 /* Extract the level 1 and 2 indices. */
519 L1 = LOOKUP_L1 (p);
520 L2 = LOOKUP_L2 (p);
522 return base[L1] && base[L1][L2];
525 /* Traverse the page table and find the entry for a page.
526 Die (probably) if the object wasn't allocated via GC. */
528 static inline page_entry *
529 lookup_page_table_entry (const void *p)
531 page_entry ***base;
532 size_t L1, L2;
534 #if HOST_BITS_PER_PTR <= 32
535 base = &G.lookup[0];
536 #else
537 page_table table = G.lookup;
538 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
539 while (table->high_bits != high_bits)
540 table = table->next;
541 base = &table->table[0];
542 #endif
544 /* Extract the level 1 and 2 indices. */
545 L1 = LOOKUP_L1 (p);
546 L2 = LOOKUP_L2 (p);
548 return base[L1][L2];
551 /* Set the page table entry for a page. */
553 static void
554 set_page_table_entry (void *p, page_entry *entry)
556 page_entry ***base;
557 size_t L1, L2;
559 #if HOST_BITS_PER_PTR <= 32
560 base = &G.lookup[0];
561 #else
562 page_table table;
563 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
564 for (table = G.lookup; table; table = table->next)
565 if (table->high_bits == high_bits)
566 goto found;
568 /* Not found -- allocate a new table. */
569 table = (page_table) xcalloc (1, sizeof(*table));
570 table->next = G.lookup;
571 table->high_bits = high_bits;
572 G.lookup = table;
573 found:
574 base = &table->table[0];
575 #endif
577 /* Extract the level 1 and 2 indices. */
578 L1 = LOOKUP_L1 (p);
579 L2 = LOOKUP_L2 (p);
581 if (base[L1] == NULL)
582 base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
584 base[L1][L2] = entry;
587 /* Prints the page-entry for object size ORDER, for debugging. */
589 void
590 debug_print_page_list (int order)
592 page_entry *p;
593 printf ("Head=%p, Tail=%p:\n", (void *) G.pages[order],
594 (void *) G.page_tails[order]);
595 p = G.pages[order];
596 while (p != NULL)
598 printf ("%p(%1d|%3d) -> ", (void *) p, p->context_depth,
599 p->num_free_objects);
600 p = p->next;
602 printf ("NULL\n");
603 fflush (stdout);
606 #ifdef USING_MMAP
607 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
608 (if non-null). The ifdef structure here is intended to cause a
609 compile error unless exactly one of the HAVE_* is defined. */
611 static inline char *
612 alloc_anon (char *pref ATTRIBUTE_UNUSED, size_t size)
614 #ifdef HAVE_MMAP_ANON
615 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
616 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
617 #endif
618 #ifdef HAVE_MMAP_DEV_ZERO
619 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
620 MAP_PRIVATE, G.dev_zero_fd, 0);
621 #endif
623 if (page == (char *) MAP_FAILED)
625 perror ("virtual memory exhausted");
626 exit (FATAL_EXIT_CODE);
629 /* Remember that we allocated this memory. */
630 G.bytes_mapped += size;
632 /* Pretend we don't have access to the allocated pages. We'll enable
633 access to smaller pieces of the area in ggc_alloc. Discard the
634 handle to avoid handle leak. */
635 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (page, size));
637 return page;
639 #endif
640 #ifdef USING_MALLOC_PAGE_GROUPS
641 /* Compute the index for this page into the page group. */
643 static inline size_t
644 page_group_index (char *allocation, char *page)
646 return (size_t) (page - allocation) >> G.lg_pagesize;
649 /* Set and clear the in_use bit for this page in the page group. */
651 static inline void
652 set_page_group_in_use (page_group *group, char *page)
654 group->in_use |= 1 << page_group_index (group->allocation, page);
657 static inline void
658 clear_page_group_in_use (page_group *group, char *page)
660 group->in_use &= ~(1 << page_group_index (group->allocation, page));
662 #endif
664 /* Allocate a new page for allocating objects of size 2^ORDER,
665 and return an entry for it. The entry is not added to the
666 appropriate page_table list. */
668 static inline struct page_entry *
669 alloc_page (unsigned order)
671 struct page_entry *entry, *p, **pp;
672 char *page;
673 size_t num_objects;
674 size_t bitmap_size;
675 size_t page_entry_size;
676 size_t entry_size;
677 #ifdef USING_MALLOC_PAGE_GROUPS
678 page_group *group;
679 #endif
681 num_objects = OBJECTS_PER_PAGE (order);
682 bitmap_size = BITMAP_SIZE (num_objects + 1);
683 page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
684 entry_size = num_objects * OBJECT_SIZE (order);
685 if (entry_size < G.pagesize)
686 entry_size = G.pagesize;
688 entry = NULL;
689 page = NULL;
691 /* Check the list of free pages for one we can use. */
692 for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
693 if (p->bytes == entry_size)
694 break;
696 if (p != NULL)
698 /* Recycle the allocated memory from this page ... */
699 *pp = p->next;
700 page = p->page;
702 #ifdef USING_MALLOC_PAGE_GROUPS
703 group = p->group;
704 #endif
706 /* ... and, if possible, the page entry itself. */
707 if (p->order == order)
709 entry = p;
710 memset (entry, 0, page_entry_size);
712 else
713 free (p);
715 #ifdef USING_MMAP
716 else if (entry_size == G.pagesize)
718 /* We want just one page. Allocate a bunch of them and put the
719 extras on the freelist. (Can only do this optimization with
720 mmap for backing store.) */
721 struct page_entry *e, *f = G.free_pages;
722 int i;
724 page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE);
726 /* This loop counts down so that the chain will be in ascending
727 memory order. */
728 for (i = GGC_QUIRE_SIZE - 1; i >= 1; i--)
730 e = (struct page_entry *) xcalloc (1, page_entry_size);
731 e->order = order;
732 e->bytes = G.pagesize;
733 e->page = page + (i << G.lg_pagesize);
734 e->next = f;
735 f = e;
738 G.free_pages = f;
740 else
741 page = alloc_anon (NULL, entry_size);
742 #endif
743 #ifdef USING_MALLOC_PAGE_GROUPS
744 else
746 /* Allocate a large block of memory and serve out the aligned
747 pages therein. This results in much less memory wastage
748 than the traditional implementation of valloc. */
750 char *allocation, *a, *enda;
751 size_t alloc_size, head_slop, tail_slop;
752 int multiple_pages = (entry_size == G.pagesize);
754 if (multiple_pages)
755 alloc_size = GGC_QUIRE_SIZE * G.pagesize;
756 else
757 alloc_size = entry_size + G.pagesize - 1;
758 allocation = xmalloc (alloc_size);
760 page = (char *) (((size_t) allocation + G.pagesize - 1) & -G.pagesize);
761 head_slop = page - allocation;
762 if (multiple_pages)
763 tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
764 else
765 tail_slop = alloc_size - entry_size - head_slop;
766 enda = allocation + alloc_size - tail_slop;
768 /* We allocated N pages, which are likely not aligned, leaving
769 us with N-1 usable pages. We plan to place the page_group
770 structure somewhere in the slop. */
771 if (head_slop >= sizeof (page_group))
772 group = (page_group *)page - 1;
773 else
775 /* We magically got an aligned allocation. Too bad, we have
776 to waste a page anyway. */
777 if (tail_slop == 0)
779 enda -= G.pagesize;
780 tail_slop += G.pagesize;
782 if (tail_slop < sizeof (page_group))
783 abort ();
784 group = (page_group *)enda;
785 tail_slop -= sizeof (page_group);
788 /* Remember that we allocated this memory. */
789 group->next = G.page_groups;
790 group->allocation = allocation;
791 group->alloc_size = alloc_size;
792 group->in_use = 0;
793 G.page_groups = group;
794 G.bytes_mapped += alloc_size;
796 /* If we allocated multiple pages, put the rest on the free list. */
797 if (multiple_pages)
799 struct page_entry *e, *f = G.free_pages;
800 for (a = enda - G.pagesize; a != page; a -= G.pagesize)
802 e = (struct page_entry *) xcalloc (1, page_entry_size);
803 e->order = order;
804 e->bytes = G.pagesize;
805 e->page = a;
806 e->group = group;
807 e->next = f;
808 f = e;
810 G.free_pages = f;
813 #endif
815 if (entry == NULL)
816 entry = (struct page_entry *) xcalloc (1, page_entry_size);
818 entry->bytes = entry_size;
819 entry->page = page;
820 entry->context_depth = G.context_depth;
821 entry->order = order;
822 entry->num_free_objects = num_objects;
823 entry->next_bit_hint = 1;
825 G.context_depth_allocations |= (unsigned long)1 << G.context_depth;
827 #ifdef USING_MALLOC_PAGE_GROUPS
828 entry->group = group;
829 set_page_group_in_use (group, page);
830 #endif
832 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
833 increment the hint. */
834 entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
835 = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
837 set_page_table_entry (page, entry);
839 if (GGC_DEBUG_LEVEL >= 2)
840 fprintf (G.debug_file,
841 "Allocating page at %p, object size=%lu, data %p-%p\n",
842 (void *) entry, (unsigned long) OBJECT_SIZE (order), page,
843 page + entry_size - 1);
845 return entry;
848 /* Adjust the size of G.depth so that no index greater than the one
849 used by the top of the G.by_depth is used. */
851 static inline void
852 adjust_depth (void)
854 page_entry *top;
856 if (G.by_depth_in_use)
858 top = G.by_depth[G.by_depth_in_use-1];
860 /* Peel back indicies in depth that index into by_depth, so that
861 as new elements are added to by_depth, we note the indicies
862 of those elements, if they are for new context depths. */
863 while (G.depth_in_use > (size_t)top->context_depth+1)
864 --G.depth_in_use;
868 /* For a page that is no longer needed, put it on the free page list. */
870 static inline void
871 free_page (page_entry *entry)
873 if (GGC_DEBUG_LEVEL >= 2)
874 fprintf (G.debug_file,
875 "Deallocating page at %p, data %p-%p\n", (void *) entry,
876 entry->page, entry->page + entry->bytes - 1);
878 /* Mark the page as inaccessible. Discard the handle to avoid handle
879 leak. */
880 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (entry->page, entry->bytes));
882 set_page_table_entry (entry->page, NULL);
884 #ifdef USING_MALLOC_PAGE_GROUPS
885 clear_page_group_in_use (entry->group, entry->page);
886 #endif
888 if (G.by_depth_in_use > 1)
890 page_entry *top = G.by_depth[G.by_depth_in_use-1];
892 /* If they are at the same depth, put top element into freed
893 slot. */
894 if (entry->context_depth == top->context_depth)
896 int i = entry->index_by_depth;
897 G.by_depth[i] = top;
898 G.save_in_use[i] = G.save_in_use[G.by_depth_in_use-1];
899 top->index_by_depth = i;
901 else
903 /* We cannot free a page from a context deeper than the
904 current one. */
905 abort ();
908 --G.by_depth_in_use;
910 adjust_depth ();
912 entry->next = G.free_pages;
913 G.free_pages = entry;
916 /* Release the free page cache to the system. */
918 static void
919 release_pages (void)
921 #ifdef USING_MMAP
922 page_entry *p, *next;
923 char *start;
924 size_t len;
926 /* Gather up adjacent pages so they are unmapped together. */
927 p = G.free_pages;
929 while (p)
931 start = p->page;
932 next = p->next;
933 len = p->bytes;
934 free (p);
935 p = next;
937 while (p && p->page == start + len)
939 next = p->next;
940 len += p->bytes;
941 free (p);
942 p = next;
945 munmap (start, len);
946 G.bytes_mapped -= len;
949 G.free_pages = NULL;
950 #endif
951 #ifdef USING_MALLOC_PAGE_GROUPS
952 page_entry **pp, *p;
953 page_group **gp, *g;
955 /* Remove all pages from free page groups from the list. */
956 pp = &G.free_pages;
957 while ((p = *pp) != NULL)
958 if (p->group->in_use == 0)
960 *pp = p->next;
961 free (p);
963 else
964 pp = &p->next;
966 /* Remove all free page groups, and release the storage. */
967 gp = &G.page_groups;
968 while ((g = *gp) != NULL)
969 if (g->in_use == 0)
971 *gp = g->next;
972 G.bytes_mapped -= g->alloc_size;
973 free (g->allocation);
975 else
976 gp = &g->next;
977 #endif
980 /* This table provides a fast way to determine ceil(log_2(size)) for
981 allocation requests. The minimum allocation size is eight bytes. */
983 static unsigned char size_lookup[257] =
985 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
986 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
987 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
988 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
989 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
990 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
991 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
992 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
993 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
994 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
995 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
996 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
997 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
998 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
999 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1000 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1004 /* Allocate a chunk of memory of SIZE bytes. Its contents are undefined. */
1006 void *
1007 ggc_alloc (size_t size)
1009 unsigned order, word, bit, object_offset;
1010 struct page_entry *entry;
1011 void *result;
1013 if (size <= 256)
1014 order = size_lookup[size];
1015 else
1017 order = 9;
1018 while (size > OBJECT_SIZE (order))
1019 order++;
1022 /* If there are non-full pages for this size allocation, they are at
1023 the head of the list. */
1024 entry = G.pages[order];
1026 /* If there is no page for this object size, or all pages in this
1027 context are full, allocate a new page. */
1028 if (entry == NULL || entry->num_free_objects == 0)
1030 struct page_entry *new_entry;
1031 new_entry = alloc_page (order);
1033 new_entry->index_by_depth = G.by_depth_in_use;
1034 push_by_depth (new_entry, 0);
1036 /* We can skip context depths, if we do, make sure we go all the
1037 way to the new depth. */
1038 while (new_entry->context_depth >= G.depth_in_use)
1039 push_depth (G.by_depth_in_use-1);
1041 /* If this is the only entry, it's also the tail. */
1042 if (entry == NULL)
1043 G.page_tails[order] = new_entry;
1045 /* Put new pages at the head of the page list. */
1046 new_entry->next = entry;
1047 entry = new_entry;
1048 G.pages[order] = new_entry;
1050 /* For a new page, we know the word and bit positions (in the
1051 in_use bitmap) of the first available object -- they're zero. */
1052 new_entry->next_bit_hint = 1;
1053 word = 0;
1054 bit = 0;
1055 object_offset = 0;
1057 else
1059 /* First try to use the hint left from the previous allocation
1060 to locate a clear bit in the in-use bitmap. We've made sure
1061 that the one-past-the-end bit is always set, so if the hint
1062 has run over, this test will fail. */
1063 unsigned hint = entry->next_bit_hint;
1064 word = hint / HOST_BITS_PER_LONG;
1065 bit = hint % HOST_BITS_PER_LONG;
1067 /* If the hint didn't work, scan the bitmap from the beginning. */
1068 if ((entry->in_use_p[word] >> bit) & 1)
1070 word = bit = 0;
1071 while (~entry->in_use_p[word] == 0)
1072 ++word;
1073 while ((entry->in_use_p[word] >> bit) & 1)
1074 ++bit;
1075 hint = word * HOST_BITS_PER_LONG + bit;
1078 /* Next time, try the next bit. */
1079 entry->next_bit_hint = hint + 1;
1081 object_offset = hint * OBJECT_SIZE (order);
1084 /* Set the in-use bit. */
1085 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
1087 /* Keep a running total of the number of free objects. If this page
1088 fills up, we may have to move it to the end of the list if the
1089 next page isn't full. If the next page is full, all subsequent
1090 pages are full, so there's no need to move it. */
1091 if (--entry->num_free_objects == 0
1092 && entry->next != NULL
1093 && entry->next->num_free_objects > 0)
1095 G.pages[order] = entry->next;
1096 entry->next = NULL;
1097 G.page_tails[order]->next = entry;
1098 G.page_tails[order] = entry;
1101 /* Calculate the object's address. */
1102 result = entry->page + object_offset;
1104 #ifdef ENABLE_GC_CHECKING
1105 /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
1106 exact same semantics in presence of memory bugs, regardless of
1107 ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
1108 handle to avoid handle leak. */
1109 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, OBJECT_SIZE (order)));
1111 /* `Poison' the entire allocated object, including any padding at
1112 the end. */
1113 memset (result, 0xaf, OBJECT_SIZE (order));
1115 /* Make the bytes after the end of the object unaccessible. Discard the
1116 handle to avoid handle leak. */
1117 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) result + size,
1118 OBJECT_SIZE (order) - size));
1119 #endif
1121 /* Tell Valgrind that the memory is there, but its content isn't
1122 defined. The bytes at the end of the object are still marked
1123 unaccessible. */
1124 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, size));
1126 /* Keep track of how many bytes are being allocated. This
1127 information is used in deciding when to collect. */
1128 G.allocated += OBJECT_SIZE (order);
1130 if (GGC_DEBUG_LEVEL >= 3)
1131 fprintf (G.debug_file,
1132 "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
1133 (unsigned long) size, (unsigned long) OBJECT_SIZE (order), result,
1134 (void *) entry);
1136 return result;
1139 /* If P is not marked, marks it and return false. Otherwise return true.
1140 P must have been allocated by the GC allocator; it mustn't point to
1141 static objects, stack variables, or memory allocated with malloc. */
1144 ggc_set_mark (const void *p)
1146 page_entry *entry;
1147 unsigned bit, word;
1148 unsigned long mask;
1150 /* Look up the page on which the object is alloced. If the object
1151 wasn't allocated by the collector, we'll probably die. */
1152 entry = lookup_page_table_entry (p);
1153 #ifdef ENABLE_CHECKING
1154 if (entry == NULL)
1155 abort ();
1156 #endif
1158 /* Calculate the index of the object on the page; this is its bit
1159 position in the in_use_p bitmap. */
1160 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1161 word = bit / HOST_BITS_PER_LONG;
1162 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1164 /* If the bit was previously set, skip it. */
1165 if (entry->in_use_p[word] & mask)
1166 return 1;
1168 /* Otherwise set it, and decrement the free object count. */
1169 entry->in_use_p[word] |= mask;
1170 entry->num_free_objects -= 1;
1172 if (GGC_DEBUG_LEVEL >= 4)
1173 fprintf (G.debug_file, "Marking %p\n", p);
1175 return 0;
1178 /* Return 1 if P has been marked, zero otherwise.
1179 P must have been allocated by the GC allocator; it mustn't point to
1180 static objects, stack variables, or memory allocated with malloc. */
1183 ggc_marked_p (const void *p)
1185 page_entry *entry;
1186 unsigned bit, word;
1187 unsigned long mask;
1189 /* Look up the page on which the object is alloced. If the object
1190 wasn't allocated by the collector, we'll probably die. */
1191 entry = lookup_page_table_entry (p);
1192 #ifdef ENABLE_CHECKING
1193 if (entry == NULL)
1194 abort ();
1195 #endif
1197 /* Calculate the index of the object on the page; this is its bit
1198 position in the in_use_p bitmap. */
1199 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1200 word = bit / HOST_BITS_PER_LONG;
1201 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1203 return (entry->in_use_p[word] & mask) != 0;
1206 /* Return the size of the gc-able object P. */
1208 size_t
1209 ggc_get_size (const void *p)
1211 page_entry *pe = lookup_page_table_entry (p);
1212 return OBJECT_SIZE (pe->order);
1215 /* Subroutine of init_ggc which computes the pair of numbers used to
1216 perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1218 This algorithm is taken from Granlund and Montgomery's paper
1219 "Division by Invariant Integers using Multiplication"
1220 (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1221 constants). */
1223 static void
1224 compute_inverse (unsigned order)
1226 unsigned size, inv, e;
1228 /* There can be only one object per "page" in a bucket for sizes
1229 larger than half a machine page; it will always have offset zero. */
1230 if (OBJECT_SIZE (order) > G.pagesize/2)
1232 if (OBJECTS_PER_PAGE (order) != 1)
1233 abort ();
1235 DIV_MULT (order) = 1;
1236 DIV_SHIFT (order) = 0;
1237 return;
1240 size = OBJECT_SIZE (order);
1241 e = 0;
1242 while (size % 2 == 0)
1244 e++;
1245 size >>= 1;
1248 inv = size;
1249 while (inv * size != 1)
1250 inv = inv * (2 - inv*size);
1252 DIV_MULT (order) = inv;
1253 DIV_SHIFT (order) = e;
1256 /* Initialize the ggc-mmap allocator. */
1257 void
1258 init_ggc (void)
1260 unsigned order;
1262 G.pagesize = getpagesize();
1263 G.lg_pagesize = exact_log2 (G.pagesize);
1265 #ifdef HAVE_MMAP_DEV_ZERO
1266 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1267 if (G.dev_zero_fd == -1)
1268 internal_error ("open /dev/zero: %m");
1269 #endif
1271 #if 0
1272 G.debug_file = fopen ("ggc-mmap.debug", "w");
1273 #else
1274 G.debug_file = stdout;
1275 #endif
1277 #ifdef USING_MMAP
1278 /* StunOS has an amazing off-by-one error for the first mmap allocation
1279 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1280 believe, is an unaligned page allocation, which would cause us to
1281 hork badly if we tried to use it. */
1283 char *p = alloc_anon (NULL, G.pagesize);
1284 struct page_entry *e;
1285 if ((size_t)p & (G.pagesize - 1))
1287 /* How losing. Discard this one and try another. If we still
1288 can't get something useful, give up. */
1290 p = alloc_anon (NULL, G.pagesize);
1291 if ((size_t)p & (G.pagesize - 1))
1292 abort ();
1295 /* We have a good page, might as well hold onto it... */
1296 e = (struct page_entry *) xcalloc (1, sizeof (struct page_entry));
1297 e->bytes = G.pagesize;
1298 e->page = p;
1299 e->next = G.free_pages;
1300 G.free_pages = e;
1302 #endif
1304 /* Initialize the object size table. */
1305 for (order = 0; order < HOST_BITS_PER_PTR; ++order)
1306 object_size_table[order] = (size_t) 1 << order;
1307 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1309 size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
1311 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1312 so that we're sure of getting aligned memory. */
1313 s = ROUND_UP (s, MAX_ALIGNMENT);
1314 object_size_table[order] = s;
1317 /* Initialize the objects-per-page and inverse tables. */
1318 for (order = 0; order < NUM_ORDERS; ++order)
1320 objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
1321 if (objects_per_page_table[order] == 0)
1322 objects_per_page_table[order] = 1;
1323 compute_inverse (order);
1326 /* Reset the size_lookup array to put appropriately sized objects in
1327 the special orders. All objects bigger than the previous power
1328 of two, but no greater than the special size, should go in the
1329 new order. */
1330 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1332 int o;
1333 int i;
1335 o = size_lookup[OBJECT_SIZE (order)];
1336 for (i = OBJECT_SIZE (order); size_lookup [i] == o; --i)
1337 size_lookup[i] = order;
1340 G.depth_in_use = 0;
1341 G.depth_max = 10;
1342 G.depth = (unsigned int *) xmalloc (G.depth_max * sizeof (unsigned int));
1344 G.by_depth_in_use = 0;
1345 G.by_depth_max = INITIAL_PTE_COUNT;
1346 G.by_depth = (page_entry **) xmalloc (G.by_depth_max * sizeof (page_entry *));
1347 G.save_in_use = (unsigned long **) xmalloc (G.by_depth_max * sizeof (unsigned long *));
1350 /* Increment the `GC context'. Objects allocated in an outer context
1351 are never freed, eliminating the need to register their roots. */
1353 void
1354 ggc_push_context (void)
1356 ++G.context_depth;
1358 /* Die on wrap. */
1359 if (G.context_depth >= HOST_BITS_PER_LONG)
1360 abort ();
1363 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1364 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1366 static void
1367 ggc_recalculate_in_use_p (page_entry *p)
1369 unsigned int i;
1370 size_t num_objects;
1372 /* Because the past-the-end bit in in_use_p is always set, we
1373 pretend there is one additional object. */
1374 num_objects = OBJECTS_IN_PAGE (p) + 1;
1376 /* Reset the free object count. */
1377 p->num_free_objects = num_objects;
1379 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1380 for (i = 0;
1381 i < CEIL (BITMAP_SIZE (num_objects),
1382 sizeof (*p->in_use_p));
1383 ++i)
1385 unsigned long j;
1387 /* Something is in use if it is marked, or if it was in use in a
1388 context further down the context stack. */
1389 p->in_use_p[i] |= save_in_use_p (p)[i];
1391 /* Decrement the free object count for every object allocated. */
1392 for (j = p->in_use_p[i]; j; j >>= 1)
1393 p->num_free_objects -= (j & 1);
1396 if (p->num_free_objects >= num_objects)
1397 abort ();
1400 /* Decrement the `GC context'. All objects allocated since the
1401 previous ggc_push_context are migrated to the outer context. */
1403 void
1404 ggc_pop_context (void)
1406 unsigned long omask;
1407 unsigned int depth, i, e;
1408 #ifdef ENABLE_CHECKING
1409 unsigned int order;
1410 #endif
1412 depth = --G.context_depth;
1413 omask = (unsigned long)1 << (depth + 1);
1415 if (!((G.context_depth_allocations | G.context_depth_collections) & omask))
1416 return;
1418 G.context_depth_allocations |= (G.context_depth_allocations & omask) >> 1;
1419 G.context_depth_allocations &= omask - 1;
1420 G.context_depth_collections &= omask - 1;
1422 /* The G.depth array is shortend so that the last index is the
1423 context_depth of the top element of by_depth. */
1424 if (depth+1 < G.depth_in_use)
1425 e = G.depth[depth+1];
1426 else
1427 e = G.by_depth_in_use;
1429 /* We might not have any PTEs of depth depth. */
1430 if (depth < G.depth_in_use)
1433 /* First we go through all the pages at depth depth to
1434 recalculate the in use bits. */
1435 for (i = G.depth[depth]; i < e; ++i)
1437 page_entry *p;
1439 #ifdef ENABLE_CHECKING
1440 p = G.by_depth[i];
1442 /* Check that all of the pages really are at the depth that
1443 we expect. */
1444 if (p->context_depth != depth)
1445 abort ();
1446 if (p->index_by_depth != i)
1447 abort ();
1448 #endif
1450 prefetch (&save_in_use_p_i (i+8));
1451 prefetch (&save_in_use_p_i (i+16));
1452 if (save_in_use_p_i (i))
1454 p = G.by_depth[i];
1455 ggc_recalculate_in_use_p (p);
1456 free (save_in_use_p_i (i));
1457 save_in_use_p_i (i) = 0;
1462 /* Then, we reset all page_entries with a depth greater than depth
1463 to be at depth. */
1464 for (i = e; i < G.by_depth_in_use; ++i)
1466 page_entry *p = G.by_depth[i];
1468 /* Check that all of the pages really are at the depth we
1469 expect. */
1470 #ifdef ENABLE_CHECKING
1471 if (p->context_depth <= depth)
1472 abort ();
1473 if (p->index_by_depth != i)
1474 abort ();
1475 #endif
1476 p->context_depth = depth;
1479 adjust_depth ();
1481 #ifdef ENABLE_CHECKING
1482 for (order = 2; order < NUM_ORDERS; order++)
1484 page_entry *p;
1486 for (p = G.pages[order]; p != NULL; p = p->next)
1488 if (p->context_depth > depth)
1489 abort ();
1490 else if (p->context_depth == depth && save_in_use_p (p))
1491 abort ();
1494 #endif
1497 /* Unmark all objects. */
1499 static inline void
1500 clear_marks (void)
1502 unsigned order;
1504 for (order = 2; order < NUM_ORDERS; order++)
1506 page_entry *p;
1508 for (p = G.pages[order]; p != NULL; p = p->next)
1510 size_t num_objects = OBJECTS_IN_PAGE (p);
1511 size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
1513 #ifdef ENABLE_CHECKING
1514 /* The data should be page-aligned. */
1515 if ((size_t) p->page & (G.pagesize - 1))
1516 abort ();
1517 #endif
1519 /* Pages that aren't in the topmost context are not collected;
1520 nevertheless, we need their in-use bit vectors to store GC
1521 marks. So, back them up first. */
1522 if (p->context_depth < G.context_depth)
1524 if (! save_in_use_p (p))
1525 save_in_use_p (p) = xmalloc (bitmap_size);
1526 memcpy (save_in_use_p (p), p->in_use_p, bitmap_size);
1529 /* Reset reset the number of free objects and clear the
1530 in-use bits. These will be adjusted by mark_obj. */
1531 p->num_free_objects = num_objects;
1532 memset (p->in_use_p, 0, bitmap_size);
1534 /* Make sure the one-past-the-end bit is always set. */
1535 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
1536 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
1541 /* Free all empty pages. Partially empty pages need no attention
1542 because the `mark' bit doubles as an `unused' bit. */
1544 static inline void
1545 sweep_pages (void)
1547 unsigned order;
1549 for (order = 2; order < NUM_ORDERS; order++)
1551 /* The last page-entry to consider, regardless of entries
1552 placed at the end of the list. */
1553 page_entry * const last = G.page_tails[order];
1555 size_t num_objects;
1556 size_t live_objects;
1557 page_entry *p, *previous;
1558 int done;
1560 p = G.pages[order];
1561 if (p == NULL)
1562 continue;
1564 previous = NULL;
1567 page_entry *next = p->next;
1569 /* Loop until all entries have been examined. */
1570 done = (p == last);
1572 num_objects = OBJECTS_IN_PAGE (p);
1574 /* Add all live objects on this page to the count of
1575 allocated memory. */
1576 live_objects = num_objects - p->num_free_objects;
1578 G.allocated += OBJECT_SIZE (order) * live_objects;
1580 /* Only objects on pages in the topmost context should get
1581 collected. */
1582 if (p->context_depth < G.context_depth)
1585 /* Remove the page if it's empty. */
1586 else if (live_objects == 0)
1588 if (! previous)
1589 G.pages[order] = next;
1590 else
1591 previous->next = next;
1593 /* Are we removing the last element? */
1594 if (p == G.page_tails[order])
1595 G.page_tails[order] = previous;
1596 free_page (p);
1597 p = previous;
1600 /* If the page is full, move it to the end. */
1601 else if (p->num_free_objects == 0)
1603 /* Don't move it if it's already at the end. */
1604 if (p != G.page_tails[order])
1606 /* Move p to the end of the list. */
1607 p->next = NULL;
1608 G.page_tails[order]->next = p;
1610 /* Update the tail pointer... */
1611 G.page_tails[order] = p;
1613 /* ... and the head pointer, if necessary. */
1614 if (! previous)
1615 G.pages[order] = next;
1616 else
1617 previous->next = next;
1618 p = previous;
1622 /* If we've fallen through to here, it's a page in the
1623 topmost context that is neither full nor empty. Such a
1624 page must precede pages at lesser context depth in the
1625 list, so move it to the head. */
1626 else if (p != G.pages[order])
1628 previous->next = p->next;
1629 p->next = G.pages[order];
1630 G.pages[order] = p;
1631 /* Are we moving the last element? */
1632 if (G.page_tails[order] == p)
1633 G.page_tails[order] = previous;
1634 p = previous;
1637 previous = p;
1638 p = next;
1640 while (! done);
1642 /* Now, restore the in_use_p vectors for any pages from contexts
1643 other than the current one. */
1644 for (p = G.pages[order]; p; p = p->next)
1645 if (p->context_depth != G.context_depth)
1646 ggc_recalculate_in_use_p (p);
1650 #ifdef ENABLE_GC_CHECKING
1651 /* Clobber all free objects. */
1653 static inline void
1654 poison_pages (void)
1656 unsigned order;
1658 for (order = 2; order < NUM_ORDERS; order++)
1660 size_t size = OBJECT_SIZE (order);
1661 page_entry *p;
1663 for (p = G.pages[order]; p != NULL; p = p->next)
1665 size_t num_objects;
1666 size_t i;
1668 if (p->context_depth != G.context_depth)
1669 /* Since we don't do any collection for pages in pushed
1670 contexts, there's no need to do any poisoning. And
1671 besides, the IN_USE_P array isn't valid until we pop
1672 contexts. */
1673 continue;
1675 num_objects = OBJECTS_IN_PAGE (p);
1676 for (i = 0; i < num_objects; i++)
1678 size_t word, bit;
1679 word = i / HOST_BITS_PER_LONG;
1680 bit = i % HOST_BITS_PER_LONG;
1681 if (((p->in_use_p[word] >> bit) & 1) == 0)
1683 char *object = p->page + i * size;
1685 /* Keep poison-by-write when we expect to use Valgrind,
1686 so the exact same memory semantics is kept, in case
1687 there are memory errors. We override this request
1688 below. */
1689 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (object, size));
1690 memset (object, 0xa5, size);
1692 /* Drop the handle to avoid handle leak. */
1693 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (object, size));
1699 #endif
1701 /* Top level mark-and-sweep routine. */
1703 void
1704 ggc_collect (void)
1706 /* Avoid frequent unnecessary work by skipping collection if the
1707 total allocations haven't expanded much since the last
1708 collection. */
1709 float allocated_last_gc =
1710 MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
1712 float min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
1714 if (G.allocated < allocated_last_gc + min_expand)
1715 return;
1717 timevar_push (TV_GC);
1718 if (!quiet_flag)
1719 fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
1721 /* Zero the total allocated bytes. This will be recalculated in the
1722 sweep phase. */
1723 G.allocated = 0;
1725 /* Release the pages we freed the last time we collected, but didn't
1726 reuse in the interim. */
1727 release_pages ();
1729 /* Indicate that we've seen collections at this context depth. */
1730 G.context_depth_collections = ((unsigned long)1 << (G.context_depth + 1)) - 1;
1732 clear_marks ();
1733 ggc_mark_roots ();
1735 #ifdef ENABLE_GC_CHECKING
1736 poison_pages ();
1737 #endif
1739 sweep_pages ();
1741 G.allocated_last_gc = G.allocated;
1743 timevar_pop (TV_GC);
1745 if (!quiet_flag)
1746 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
1749 /* Print allocation statistics. */
1750 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1751 ? (x) \
1752 : ((x) < 1024*1024*10 \
1753 ? (x) / 1024 \
1754 : (x) / (1024*1024))))
1755 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1757 void
1758 ggc_print_statistics (void)
1760 struct ggc_statistics stats;
1761 unsigned int i;
1762 size_t total_overhead = 0;
1764 /* Clear the statistics. */
1765 memset (&stats, 0, sizeof (stats));
1767 /* Make sure collection will really occur. */
1768 G.allocated_last_gc = 0;
1770 /* Collect and print the statistics common across collectors. */
1771 ggc_print_common_statistics (stderr, &stats);
1773 /* Release free pages so that we will not count the bytes allocated
1774 there as part of the total allocated memory. */
1775 release_pages ();
1777 /* Collect some information about the various sizes of
1778 allocation. */
1779 fprintf (stderr, "\n%-5s %10s %10s %10s\n",
1780 "Size", "Allocated", "Used", "Overhead");
1781 for (i = 0; i < NUM_ORDERS; ++i)
1783 page_entry *p;
1784 size_t allocated;
1785 size_t in_use;
1786 size_t overhead;
1788 /* Skip empty entries. */
1789 if (!G.pages[i])
1790 continue;
1792 overhead = allocated = in_use = 0;
1794 /* Figure out the total number of bytes allocated for objects of
1795 this size, and how many of them are actually in use. Also figure
1796 out how much memory the page table is using. */
1797 for (p = G.pages[i]; p; p = p->next)
1799 allocated += p->bytes;
1800 in_use +=
1801 (OBJECTS_IN_PAGE (p) - p->num_free_objects) * OBJECT_SIZE (i);
1803 overhead += (sizeof (page_entry) - sizeof (long)
1804 + BITMAP_SIZE (OBJECTS_IN_PAGE (p) + 1));
1806 fprintf (stderr, "%-5lu %10lu%c %10lu%c %10lu%c\n",
1807 (unsigned long) OBJECT_SIZE (i),
1808 SCALE (allocated), LABEL (allocated),
1809 SCALE (in_use), LABEL (in_use),
1810 SCALE (overhead), LABEL (overhead));
1811 total_overhead += overhead;
1813 fprintf (stderr, "%-5s %10lu%c %10lu%c %10lu%c\n", "Total",
1814 SCALE (G.bytes_mapped), LABEL (G.bytes_mapped),
1815 SCALE (G.allocated), LABEL(G.allocated),
1816 SCALE (total_overhead), LABEL (total_overhead));
1819 struct ggc_pch_data
1821 struct ggc_pch_ondisk
1823 unsigned totals[NUM_ORDERS];
1824 } d;
1825 size_t base[NUM_ORDERS];
1826 size_t written[NUM_ORDERS];
1829 struct ggc_pch_data *
1830 init_ggc_pch (void)
1832 return xcalloc (sizeof (struct ggc_pch_data), 1);
1835 void
1836 ggc_pch_count_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
1837 size_t size)
1839 unsigned order;
1841 if (size <= 256)
1842 order = size_lookup[size];
1843 else
1845 order = 9;
1846 while (size > OBJECT_SIZE (order))
1847 order++;
1850 d->d.totals[order]++;
1853 size_t
1854 ggc_pch_total_size (struct ggc_pch_data *d)
1856 size_t a = 0;
1857 unsigned i;
1859 for (i = 0; i < NUM_ORDERS; i++)
1860 a += ROUND_UP (d->d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1861 return a;
1864 void
1865 ggc_pch_this_base (struct ggc_pch_data *d, void *base)
1867 size_t a = (size_t) base;
1868 unsigned i;
1870 for (i = 0; i < NUM_ORDERS; i++)
1872 d->base[i] = a;
1873 a += ROUND_UP (d->d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1878 char *
1879 ggc_pch_alloc_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
1880 size_t size)
1882 unsigned order;
1883 char *result;
1885 if (size <= 256)
1886 order = size_lookup[size];
1887 else
1889 order = 9;
1890 while (size > OBJECT_SIZE (order))
1891 order++;
1894 result = (char *) d->base[order];
1895 d->base[order] += OBJECT_SIZE (order);
1896 return result;
1899 void
1900 ggc_pch_prepare_write (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
1901 FILE *f ATTRIBUTE_UNUSED)
1903 /* Nothing to do. */
1906 void
1907 ggc_pch_write_object (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
1908 FILE *f, void *x, void *newx ATTRIBUTE_UNUSED,
1909 size_t size)
1911 unsigned order;
1913 if (size <= 256)
1914 order = size_lookup[size];
1915 else
1917 order = 9;
1918 while (size > OBJECT_SIZE (order))
1919 order++;
1922 if (fwrite (x, size, 1, f) != 1)
1923 fatal_error ("can't write PCH file: %m");
1925 /* In the current implementation, SIZE is always equal to
1926 OBJECT_SIZE (order) and so the fseek is never executed. */
1927 if (size != OBJECT_SIZE (order)
1928 && fseek (f, OBJECT_SIZE (order) - size, SEEK_CUR) != 0)
1929 fatal_error ("can't write PCH file: %m");
1931 d->written[order]++;
1932 if (d->written[order] == d->d.totals[order]
1933 && fseek (f, ROUND_UP_VALUE (d->d.totals[order] * OBJECT_SIZE (order),
1934 G.pagesize),
1935 SEEK_CUR) != 0)
1936 fatal_error ("can't write PCH file: %m");
1939 void
1940 ggc_pch_finish (struct ggc_pch_data *d, FILE *f)
1942 if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
1943 fatal_error ("can't write PCH file: %m");
1944 free (d);
1947 /* Move the PCH PTE entries just added to the end of by_depth, to the
1948 front. */
1950 static void
1951 move_ptes_to_front (int count_old_page_tables, int count_new_page_tables)
1953 unsigned i;
1955 /* First, we swap the new entries to the front of the varrays. */
1956 page_entry **new_by_depth;
1957 unsigned long **new_save_in_use;
1959 new_by_depth = (page_entry **) xmalloc (G.by_depth_max * sizeof (page_entry *));
1960 new_save_in_use = (unsigned long **) xmalloc (G.by_depth_max * sizeof (unsigned long *));
1962 memcpy (&new_by_depth[0],
1963 &G.by_depth[count_old_page_tables],
1964 count_new_page_tables * sizeof (void *));
1965 memcpy (&new_by_depth[count_new_page_tables],
1966 &G.by_depth[0],
1967 count_old_page_tables * sizeof (void *));
1968 memcpy (&new_save_in_use[0],
1969 &G.save_in_use[count_old_page_tables],
1970 count_new_page_tables * sizeof (void *));
1971 memcpy (&new_save_in_use[count_new_page_tables],
1972 &G.save_in_use[0],
1973 count_old_page_tables * sizeof (void *));
1975 free (G.by_depth);
1976 free (G.save_in_use);
1978 G.by_depth = new_by_depth;
1979 G.save_in_use = new_save_in_use;
1981 /* Now update all the index_by_depth fields. */
1982 for (i = G.by_depth_in_use; i > 0; --i)
1984 page_entry *p = G.by_depth[i-1];
1985 p->index_by_depth = i-1;
1988 /* And last, we update the depth pointers in G.depth. The first
1989 entry is already 0, and context 0 entries always start at index
1990 0, so there is nothing to update in the first slot. We need a
1991 second slot, only if we have old ptes, and if we do, they start
1992 at index count_new_page_tables. */
1993 if (count_old_page_tables)
1994 push_depth (count_new_page_tables);
1997 void
1998 ggc_pch_read (FILE *f, void *addr)
2000 struct ggc_pch_ondisk d;
2001 unsigned i;
2002 char *offs = addr;
2003 unsigned long count_old_page_tables;
2004 unsigned long count_new_page_tables;
2006 count_old_page_tables = G.by_depth_in_use;
2008 /* We've just read in a PCH file. So, every object that used to be
2009 allocated is now free. */
2010 clear_marks ();
2011 #ifdef GGC_POISON
2012 poison_pages ();
2013 #endif
2015 /* No object read from a PCH file should ever be freed. So, set the
2016 context depth to 1, and set the depth of all the currently-allocated
2017 pages to be 1 too. PCH pages will have depth 0. */
2018 if (G.context_depth != 0)
2019 abort ();
2020 G.context_depth = 1;
2021 for (i = 0; i < NUM_ORDERS; i++)
2023 page_entry *p;
2024 for (p = G.pages[i]; p != NULL; p = p->next)
2025 p->context_depth = G.context_depth;
2028 /* Allocate the appropriate page-table entries for the pages read from
2029 the PCH file. */
2030 if (fread (&d, sizeof (d), 1, f) != 1)
2031 fatal_error ("can't read PCH file: %m");
2033 for (i = 0; i < NUM_ORDERS; i++)
2035 struct page_entry *entry;
2036 char *pte;
2037 size_t bytes;
2038 size_t num_objs;
2039 size_t j;
2041 if (d.totals[i] == 0)
2042 continue;
2044 bytes = ROUND_UP (d.totals[i] * OBJECT_SIZE (i), G.pagesize);
2045 num_objs = bytes / OBJECT_SIZE (i);
2046 entry = xcalloc (1, (sizeof (struct page_entry)
2047 - sizeof (long)
2048 + BITMAP_SIZE (num_objs + 1)));
2049 entry->bytes = bytes;
2050 entry->page = offs;
2051 entry->context_depth = 0;
2052 offs += bytes;
2053 entry->num_free_objects = 0;
2054 entry->order = i;
2056 for (j = 0;
2057 j + HOST_BITS_PER_LONG <= num_objs + 1;
2058 j += HOST_BITS_PER_LONG)
2059 entry->in_use_p[j / HOST_BITS_PER_LONG] = -1;
2060 for (; j < num_objs + 1; j++)
2061 entry->in_use_p[j / HOST_BITS_PER_LONG]
2062 |= 1L << (j % HOST_BITS_PER_LONG);
2064 for (pte = entry->page;
2065 pte < entry->page + entry->bytes;
2066 pte += G.pagesize)
2067 set_page_table_entry (pte, entry);
2069 if (G.page_tails[i] != NULL)
2070 G.page_tails[i]->next = entry;
2071 else
2072 G.pages[i] = entry;
2073 G.page_tails[i] = entry;
2075 /* We start off by just adding all the new information to the
2076 end of the varrays, later, we will move the new information
2077 to the front of the varrays, as the PCH page tables are at
2078 context 0. */
2079 push_by_depth (entry, 0);
2082 /* Now, we update the various data structures that speed page table
2083 handling. */
2084 count_new_page_tables = G.by_depth_in_use - count_old_page_tables;
2086 move_ptes_to_front (count_old_page_tables, count_new_page_tables);
2088 /* Update the statistics. */
2089 G.allocated = G.allocated_last_gc = offs - (char *)addr;