PR middle-end/66633
[official-gcc.git] / gcc / ggc-page.c
blob0f07000e3e109034abb5346fd2b97cc5238f0f9a
1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999-2015 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 3, 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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "diagnostic-core.h"
30 #include "flags.h"
31 #include "ggc-internal.h"
32 #include "timevar.h"
33 #include "params.h"
34 #include "hard-reg-set.h"
35 #include "function.h"
36 #include "cgraph.h"
37 #include "cfgloop.h"
38 #include "plugin.h"
39 #include "predict.h"
40 #include "basic-block.h"
42 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
43 file open. Prefer either to valloc. */
44 #ifdef HAVE_MMAP_ANON
45 # undef HAVE_MMAP_DEV_ZERO
46 # define USING_MMAP
47 #endif
49 #ifdef HAVE_MMAP_DEV_ZERO
50 # define USING_MMAP
51 #endif
53 #ifndef USING_MMAP
54 #define USING_MALLOC_PAGE_GROUPS
55 #endif
57 #if defined(HAVE_MADVISE) && HAVE_DECL_MADVISE && defined(MADV_DONTNEED) \
58 && defined(USING_MMAP)
59 # define USING_MADVISE
60 #endif
62 /* Strategy:
64 This garbage-collecting allocator allocates objects on one of a set
65 of pages. Each page can allocate objects of a single size only;
66 available sizes are powers of two starting at four bytes. The size
67 of an allocation request is rounded up to the next power of two
68 (`order'), and satisfied from the appropriate page.
70 Each page is recorded in a page-entry, which also maintains an
71 in-use bitmap of object positions on the page. This allows the
72 allocation state of a particular object to be flipped without
73 touching the page itself.
75 Each page-entry also has a context depth, which is used to track
76 pushing and popping of allocation contexts. Only objects allocated
77 in the current (highest-numbered) context may be collected.
79 Page entries are arranged in an array of singly-linked lists. The
80 array is indexed by the allocation size, in bits, of the pages on
81 it; i.e. all pages on a list allocate objects of the same size.
82 Pages are ordered on the list such that all non-full pages precede
83 all full pages, with non-full pages arranged in order of decreasing
84 context depth.
86 Empty pages (of all orders) are kept on a single page cache list,
87 and are considered first when new pages are required; they are
88 deallocated at the start of the next collection if they haven't
89 been recycled by then. */
91 /* Define GGC_DEBUG_LEVEL to print debugging information.
92 0: No debugging output.
93 1: GC statistics only.
94 2: Page-entry allocations/deallocations as well.
95 3: Object allocations as well.
96 4: Object marks as well. */
97 #define GGC_DEBUG_LEVEL (0)
99 #ifndef HOST_BITS_PER_PTR
100 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
101 #endif
104 /* A two-level tree is used to look up the page-entry for a given
105 pointer. Two chunks of the pointer's bits are extracted to index
106 the first and second levels of the tree, as follows:
108 HOST_PAGE_SIZE_BITS
109 32 | |
110 msb +----------------+----+------+------+ lsb
111 | | |
112 PAGE_L1_BITS |
114 PAGE_L2_BITS
116 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
117 pages are aligned on system page boundaries. The next most
118 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
119 index values in the lookup table, respectively.
121 For 32-bit architectures and the settings below, there are no
122 leftover bits. For architectures with wider pointers, the lookup
123 tree points to a list of pages, which must be scanned to find the
124 correct one. */
126 #define PAGE_L1_BITS (8)
127 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
128 #define PAGE_L1_SIZE ((uintptr_t) 1 << PAGE_L1_BITS)
129 #define PAGE_L2_SIZE ((uintptr_t) 1 << PAGE_L2_BITS)
131 #define LOOKUP_L1(p) \
132 (((uintptr_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
134 #define LOOKUP_L2(p) \
135 (((uintptr_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
137 /* The number of objects per allocation page, for objects on a page of
138 the indicated ORDER. */
139 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
141 /* The number of objects in P. */
142 #define OBJECTS_IN_PAGE(P) ((P)->bytes / OBJECT_SIZE ((P)->order))
144 /* The size of an object on a page of the indicated ORDER. */
145 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
147 /* For speed, we avoid doing a general integer divide to locate the
148 offset in the allocation bitmap, by precalculating numbers M, S
149 such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
150 within the page which is evenly divisible by the object size Z. */
151 #define DIV_MULT(ORDER) inverse_table[ORDER].mult
152 #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
153 #define OFFSET_TO_BIT(OFFSET, ORDER) \
154 (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
156 /* We use this structure to determine the alignment required for
157 allocations. For power-of-two sized allocations, that's not a
158 problem, but it does matter for odd-sized allocations.
159 We do not care about alignment for floating-point types. */
161 struct max_alignment {
162 char c;
163 union {
164 int64_t i;
165 void *p;
166 } u;
169 /* The biggest alignment required. */
171 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
174 /* The number of extra orders, not corresponding to power-of-two sized
175 objects. */
177 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
179 #define RTL_SIZE(NSLOTS) \
180 (RTX_HDR_SIZE + (NSLOTS) * sizeof (rtunion))
182 #define TREE_EXP_SIZE(OPS) \
183 (sizeof (struct tree_exp) + ((OPS) - 1) * sizeof (tree))
185 /* The Ith entry is the maximum size of an object to be stored in the
186 Ith extra order. Adding a new entry to this array is the *only*
187 thing you need to do to add a new special allocation size. */
189 static const size_t extra_order_size_table[] = {
190 /* Extra orders for small non-power-of-two multiples of MAX_ALIGNMENT.
191 There are a lot of structures with these sizes and explicitly
192 listing them risks orders being dropped because they changed size. */
193 MAX_ALIGNMENT * 3,
194 MAX_ALIGNMENT * 5,
195 MAX_ALIGNMENT * 6,
196 MAX_ALIGNMENT * 7,
197 MAX_ALIGNMENT * 9,
198 MAX_ALIGNMENT * 10,
199 MAX_ALIGNMENT * 11,
200 MAX_ALIGNMENT * 12,
201 MAX_ALIGNMENT * 13,
202 MAX_ALIGNMENT * 14,
203 MAX_ALIGNMENT * 15,
204 sizeof (struct tree_decl_non_common),
205 sizeof (struct tree_field_decl),
206 sizeof (struct tree_parm_decl),
207 sizeof (struct tree_var_decl),
208 sizeof (struct tree_type_non_common),
209 sizeof (struct function),
210 sizeof (struct basic_block_def),
211 sizeof (struct cgraph_node),
212 sizeof (struct loop),
215 /* The total number of orders. */
217 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
219 /* Compute the smallest nonnegative number which when added to X gives
220 a multiple of F. */
222 #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
224 /* Compute the smallest multiple of F that is >= X. */
226 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
228 /* Round X to next multiple of the page size */
230 #define PAGE_ALIGN(x) (((x) + G.pagesize - 1) & ~(G.pagesize - 1))
232 /* The Ith entry is the number of objects on a page or order I. */
234 static unsigned objects_per_page_table[NUM_ORDERS];
236 /* The Ith entry is the size of an object on a page of order I. */
238 static size_t object_size_table[NUM_ORDERS];
240 /* The Ith entry is a pair of numbers (mult, shift) such that
241 ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
242 for all k evenly divisible by OBJECT_SIZE(I). */
244 static struct
246 size_t mult;
247 unsigned int shift;
249 inverse_table[NUM_ORDERS];
251 /* A page_entry records the status of an allocation page. This
252 structure is dynamically sized to fit the bitmap in_use_p. */
253 typedef struct page_entry
255 /* The next page-entry with objects of the same size, or NULL if
256 this is the last page-entry. */
257 struct page_entry *next;
259 /* The previous page-entry with objects of the same size, or NULL if
260 this is the first page-entry. The PREV pointer exists solely to
261 keep the cost of ggc_free manageable. */
262 struct page_entry *prev;
264 /* The number of bytes allocated. (This will always be a multiple
265 of the host system page size.) */
266 size_t bytes;
268 /* The address at which the memory is allocated. */
269 char *page;
271 #ifdef USING_MALLOC_PAGE_GROUPS
272 /* Back pointer to the page group this page came from. */
273 struct page_group *group;
274 #endif
276 /* This is the index in the by_depth varray where this page table
277 can be found. */
278 unsigned long index_by_depth;
280 /* Context depth of this page. */
281 unsigned short context_depth;
283 /* The number of free objects remaining on this page. */
284 unsigned short num_free_objects;
286 /* A likely candidate for the bit position of a free object for the
287 next allocation from this page. */
288 unsigned short next_bit_hint;
290 /* The lg of size of objects allocated from this page. */
291 unsigned char order;
293 /* Discarded page? */
294 bool discarded;
296 /* A bit vector indicating whether or not objects are in use. The
297 Nth bit is one if the Nth object on this page is allocated. This
298 array is dynamically sized. */
299 unsigned long in_use_p[1];
300 } page_entry;
302 #ifdef USING_MALLOC_PAGE_GROUPS
303 /* A page_group describes a large allocation from malloc, from which
304 we parcel out aligned pages. */
305 typedef struct page_group
307 /* A linked list of all extant page groups. */
308 struct page_group *next;
310 /* The address we received from malloc. */
311 char *allocation;
313 /* The size of the block. */
314 size_t alloc_size;
316 /* A bitmask of pages in use. */
317 unsigned int in_use;
318 } page_group;
319 #endif
321 #if HOST_BITS_PER_PTR <= 32
323 /* On 32-bit hosts, we use a two level page table, as pictured above. */
324 typedef page_entry **page_table[PAGE_L1_SIZE];
326 #else
328 /* On 64-bit hosts, we use the same two level page tables plus a linked
329 list that disambiguates the top 32-bits. There will almost always be
330 exactly one entry in the list. */
331 typedef struct page_table_chain
333 struct page_table_chain *next;
334 size_t high_bits;
335 page_entry **table[PAGE_L1_SIZE];
336 } *page_table;
338 #endif
340 class finalizer
342 public:
343 finalizer (void *addr, void (*f)(void *)) : m_addr (addr), m_function (f) {}
345 void *addr () const { return m_addr; }
347 void call () const { m_function (m_addr); }
349 private:
350 void *m_addr;
351 void (*m_function)(void *);
354 class vec_finalizer
356 public:
357 vec_finalizer (uintptr_t addr, void (*f)(void *), size_t s, size_t n) :
358 m_addr (addr), m_function (f), m_object_size (s), m_n_objects (n) {}
360 void call () const
362 for (size_t i = 0; i < m_n_objects; i++)
363 m_function (reinterpret_cast<void *> (m_addr + (i * m_object_size)));
366 void *addr () const { return reinterpret_cast<void *> (m_addr); }
368 private:
369 uintptr_t m_addr;
370 void (*m_function)(void *);
371 size_t m_object_size;
372 size_t m_n_objects;
375 #ifdef ENABLE_GC_ALWAYS_COLLECT
376 /* List of free objects to be verified as actually free on the
377 next collection. */
378 struct free_object
380 void *object;
381 struct free_object *next;
383 #endif
385 /* The rest of the global variables. */
386 static struct ggc_globals
388 /* The Nth element in this array is a page with objects of size 2^N.
389 If there are any pages with free objects, they will be at the
390 head of the list. NULL if there are no page-entries for this
391 object size. */
392 page_entry *pages[NUM_ORDERS];
394 /* The Nth element in this array is the last page with objects of
395 size 2^N. NULL if there are no page-entries for this object
396 size. */
397 page_entry *page_tails[NUM_ORDERS];
399 /* Lookup table for associating allocation pages with object addresses. */
400 page_table lookup;
402 /* The system's page size. */
403 size_t pagesize;
404 size_t lg_pagesize;
406 /* Bytes currently allocated. */
407 size_t allocated;
409 /* Bytes currently allocated at the end of the last collection. */
410 size_t allocated_last_gc;
412 /* Total amount of memory mapped. */
413 size_t bytes_mapped;
415 /* Bit N set if any allocations have been done at context depth N. */
416 unsigned long context_depth_allocations;
418 /* Bit N set if any collections have been done at context depth N. */
419 unsigned long context_depth_collections;
421 /* The current depth in the context stack. */
422 unsigned short context_depth;
424 /* A file descriptor open to /dev/zero for reading. */
425 #if defined (HAVE_MMAP_DEV_ZERO)
426 int dev_zero_fd;
427 #endif
429 /* A cache of free system pages. */
430 page_entry *free_pages;
432 #ifdef USING_MALLOC_PAGE_GROUPS
433 page_group *page_groups;
434 #endif
436 /* The file descriptor for debugging output. */
437 FILE *debug_file;
439 /* Current number of elements in use in depth below. */
440 unsigned int depth_in_use;
442 /* Maximum number of elements that can be used before resizing. */
443 unsigned int depth_max;
445 /* Each element of this array is an index in by_depth where the given
446 depth starts. This structure is indexed by that given depth we
447 are interested in. */
448 unsigned int *depth;
450 /* Current number of elements in use in by_depth below. */
451 unsigned int by_depth_in_use;
453 /* Maximum number of elements that can be used before resizing. */
454 unsigned int by_depth_max;
456 /* Each element of this array is a pointer to a page_entry, all
457 page_entries can be found in here by increasing depth.
458 index_by_depth in the page_entry is the index into this data
459 structure where that page_entry can be found. This is used to
460 speed up finding all page_entries at a particular depth. */
461 page_entry **by_depth;
463 /* Each element is a pointer to the saved in_use_p bits, if any,
464 zero otherwise. We allocate them all together, to enable a
465 better runtime data access pattern. */
466 unsigned long **save_in_use;
468 /* Finalizers for single objects. */
469 vec<finalizer> finalizers;
471 /* Finalizers for vectors of objects. */
472 vec<vec_finalizer> vec_finalizers;
474 #ifdef ENABLE_GC_ALWAYS_COLLECT
475 /* List of free objects to be verified as actually free on the
476 next collection. */
477 struct free_object *free_object_list;
478 #endif
480 struct
482 /* Total GC-allocated memory. */
483 unsigned long long total_allocated;
484 /* Total overhead for GC-allocated memory. */
485 unsigned long long total_overhead;
487 /* Total allocations and overhead for sizes less than 32, 64 and 128.
488 These sizes are interesting because they are typical cache line
489 sizes. */
491 unsigned long long total_allocated_under32;
492 unsigned long long total_overhead_under32;
494 unsigned long long total_allocated_under64;
495 unsigned long long total_overhead_under64;
497 unsigned long long total_allocated_under128;
498 unsigned long long total_overhead_under128;
500 /* The allocations for each of the allocation orders. */
501 unsigned long long total_allocated_per_order[NUM_ORDERS];
503 /* The overhead for each of the allocation orders. */
504 unsigned long long total_overhead_per_order[NUM_ORDERS];
505 } stats;
506 } G;
508 /* True if a gc is currently taking place. */
510 static bool in_gc = false;
512 /* The size in bytes required to maintain a bitmap for the objects
513 on a page-entry. */
514 #define BITMAP_SIZE(Num_objects) \
515 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof (long))
517 /* Allocate pages in chunks of this size, to throttle calls to memory
518 allocation routines. The first page is used, the rest go onto the
519 free list. This cannot be larger than HOST_BITS_PER_INT for the
520 in_use bitmask for page_group. Hosts that need a different value
521 can override this by defining GGC_QUIRE_SIZE explicitly. */
522 #ifndef GGC_QUIRE_SIZE
523 # ifdef USING_MMAP
524 # define GGC_QUIRE_SIZE 512 /* 2MB for 4K pages */
525 # else
526 # define GGC_QUIRE_SIZE 16
527 # endif
528 #endif
530 /* Initial guess as to how many page table entries we might need. */
531 #define INITIAL_PTE_COUNT 128
533 static int ggc_allocated_p (const void *);
534 static page_entry *lookup_page_table_entry (const void *);
535 static void set_page_table_entry (void *, page_entry *);
536 #ifdef USING_MMAP
537 static char *alloc_anon (char *, size_t, bool check);
538 #endif
539 #ifdef USING_MALLOC_PAGE_GROUPS
540 static size_t page_group_index (char *, char *);
541 static void set_page_group_in_use (page_group *, char *);
542 static void clear_page_group_in_use (page_group *, char *);
543 #endif
544 static struct page_entry * alloc_page (unsigned);
545 static void free_page (struct page_entry *);
546 static void release_pages (void);
547 static void clear_marks (void);
548 static void sweep_pages (void);
549 static void ggc_recalculate_in_use_p (page_entry *);
550 static void compute_inverse (unsigned);
551 static inline void adjust_depth (void);
552 static void move_ptes_to_front (int, int);
554 void debug_print_page_list (int);
555 static void push_depth (unsigned int);
556 static void push_by_depth (page_entry *, unsigned long *);
558 /* Push an entry onto G.depth. */
560 inline static void
561 push_depth (unsigned int i)
563 if (G.depth_in_use >= G.depth_max)
565 G.depth_max *= 2;
566 G.depth = XRESIZEVEC (unsigned int, G.depth, G.depth_max);
568 G.depth[G.depth_in_use++] = i;
571 /* Push an entry onto G.by_depth and G.save_in_use. */
573 inline static void
574 push_by_depth (page_entry *p, unsigned long *s)
576 if (G.by_depth_in_use >= G.by_depth_max)
578 G.by_depth_max *= 2;
579 G.by_depth = XRESIZEVEC (page_entry *, G.by_depth, G.by_depth_max);
580 G.save_in_use = XRESIZEVEC (unsigned long *, G.save_in_use,
581 G.by_depth_max);
583 G.by_depth[G.by_depth_in_use] = p;
584 G.save_in_use[G.by_depth_in_use++] = s;
587 #if (GCC_VERSION < 3001)
588 #define prefetch(X) ((void) X)
589 #else
590 #define prefetch(X) __builtin_prefetch (X)
591 #endif
593 #define save_in_use_p_i(__i) \
594 (G.save_in_use[__i])
595 #define save_in_use_p(__p) \
596 (save_in_use_p_i (__p->index_by_depth))
598 /* Returns nonzero if P was allocated in GC'able memory. */
600 static inline int
601 ggc_allocated_p (const void *p)
603 page_entry ***base;
604 size_t L1, L2;
606 #if HOST_BITS_PER_PTR <= 32
607 base = &G.lookup[0];
608 #else
609 page_table table = G.lookup;
610 uintptr_t high_bits = (uintptr_t) p & ~ (uintptr_t) 0xffffffff;
611 while (1)
613 if (table == NULL)
614 return 0;
615 if (table->high_bits == high_bits)
616 break;
617 table = table->next;
619 base = &table->table[0];
620 #endif
622 /* Extract the level 1 and 2 indices. */
623 L1 = LOOKUP_L1 (p);
624 L2 = LOOKUP_L2 (p);
626 return base[L1] && base[L1][L2];
629 /* Traverse the page table and find the entry for a page.
630 Die (probably) if the object wasn't allocated via GC. */
632 static inline page_entry *
633 lookup_page_table_entry (const void *p)
635 page_entry ***base;
636 size_t L1, L2;
638 #if HOST_BITS_PER_PTR <= 32
639 base = &G.lookup[0];
640 #else
641 page_table table = G.lookup;
642 uintptr_t high_bits = (uintptr_t) p & ~ (uintptr_t) 0xffffffff;
643 while (table->high_bits != high_bits)
644 table = table->next;
645 base = &table->table[0];
646 #endif
648 /* Extract the level 1 and 2 indices. */
649 L1 = LOOKUP_L1 (p);
650 L2 = LOOKUP_L2 (p);
652 return base[L1][L2];
655 /* Set the page table entry for a page. */
657 static void
658 set_page_table_entry (void *p, page_entry *entry)
660 page_entry ***base;
661 size_t L1, L2;
663 #if HOST_BITS_PER_PTR <= 32
664 base = &G.lookup[0];
665 #else
666 page_table table;
667 uintptr_t high_bits = (uintptr_t) p & ~ (uintptr_t) 0xffffffff;
668 for (table = G.lookup; table; table = table->next)
669 if (table->high_bits == high_bits)
670 goto found;
672 /* Not found -- allocate a new table. */
673 table = XCNEW (struct page_table_chain);
674 table->next = G.lookup;
675 table->high_bits = high_bits;
676 G.lookup = table;
677 found:
678 base = &table->table[0];
679 #endif
681 /* Extract the level 1 and 2 indices. */
682 L1 = LOOKUP_L1 (p);
683 L2 = LOOKUP_L2 (p);
685 if (base[L1] == NULL)
686 base[L1] = XCNEWVEC (page_entry *, PAGE_L2_SIZE);
688 base[L1][L2] = entry;
691 /* Prints the page-entry for object size ORDER, for debugging. */
693 DEBUG_FUNCTION void
694 debug_print_page_list (int order)
696 page_entry *p;
697 printf ("Head=%p, Tail=%p:\n", (void *) G.pages[order],
698 (void *) G.page_tails[order]);
699 p = G.pages[order];
700 while (p != NULL)
702 printf ("%p(%1d|%3d) -> ", (void *) p, p->context_depth,
703 p->num_free_objects);
704 p = p->next;
706 printf ("NULL\n");
707 fflush (stdout);
710 #ifdef USING_MMAP
711 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
712 (if non-null). The ifdef structure here is intended to cause a
713 compile error unless exactly one of the HAVE_* is defined. */
715 static inline char *
716 alloc_anon (char *pref ATTRIBUTE_UNUSED, size_t size, bool check)
718 #ifdef HAVE_MMAP_ANON
719 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
720 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
721 #endif
722 #ifdef HAVE_MMAP_DEV_ZERO
723 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
724 MAP_PRIVATE, G.dev_zero_fd, 0);
725 #endif
727 if (page == (char *) MAP_FAILED)
729 if (!check)
730 return NULL;
731 perror ("virtual memory exhausted");
732 exit (FATAL_EXIT_CODE);
735 /* Remember that we allocated this memory. */
736 G.bytes_mapped += size;
738 /* Pretend we don't have access to the allocated pages. We'll enable
739 access to smaller pieces of the area in ggc_internal_alloc. Discard the
740 handle to avoid handle leak. */
741 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (page, size));
743 return page;
745 #endif
746 #ifdef USING_MALLOC_PAGE_GROUPS
747 /* Compute the index for this page into the page group. */
749 static inline size_t
750 page_group_index (char *allocation, char *page)
752 return (size_t) (page - allocation) >> G.lg_pagesize;
755 /* Set and clear the in_use bit for this page in the page group. */
757 static inline void
758 set_page_group_in_use (page_group *group, char *page)
760 group->in_use |= 1 << page_group_index (group->allocation, page);
763 static inline void
764 clear_page_group_in_use (page_group *group, char *page)
766 group->in_use &= ~(1 << page_group_index (group->allocation, page));
768 #endif
770 /* Allocate a new page for allocating objects of size 2^ORDER,
771 and return an entry for it. The entry is not added to the
772 appropriate page_table list. */
774 static inline struct page_entry *
775 alloc_page (unsigned order)
777 struct page_entry *entry, *p, **pp;
778 char *page;
779 size_t num_objects;
780 size_t bitmap_size;
781 size_t page_entry_size;
782 size_t entry_size;
783 #ifdef USING_MALLOC_PAGE_GROUPS
784 page_group *group;
785 #endif
787 num_objects = OBJECTS_PER_PAGE (order);
788 bitmap_size = BITMAP_SIZE (num_objects + 1);
789 page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
790 entry_size = num_objects * OBJECT_SIZE (order);
791 if (entry_size < G.pagesize)
792 entry_size = G.pagesize;
793 entry_size = PAGE_ALIGN (entry_size);
795 entry = NULL;
796 page = NULL;
798 /* Check the list of free pages for one we can use. */
799 for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
800 if (p->bytes == entry_size)
801 break;
803 if (p != NULL)
805 if (p->discarded)
806 G.bytes_mapped += p->bytes;
807 p->discarded = false;
809 /* Recycle the allocated memory from this page ... */
810 *pp = p->next;
811 page = p->page;
813 #ifdef USING_MALLOC_PAGE_GROUPS
814 group = p->group;
815 #endif
817 /* ... and, if possible, the page entry itself. */
818 if (p->order == order)
820 entry = p;
821 memset (entry, 0, page_entry_size);
823 else
824 free (p);
826 #ifdef USING_MMAP
827 else if (entry_size == G.pagesize)
829 /* We want just one page. Allocate a bunch of them and put the
830 extras on the freelist. (Can only do this optimization with
831 mmap for backing store.) */
832 struct page_entry *e, *f = G.free_pages;
833 int i, entries = GGC_QUIRE_SIZE;
835 page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE, false);
836 if (page == NULL)
838 page = alloc_anon (NULL, G.pagesize, true);
839 entries = 1;
842 /* This loop counts down so that the chain will be in ascending
843 memory order. */
844 for (i = entries - 1; i >= 1; i--)
846 e = XCNEWVAR (struct page_entry, page_entry_size);
847 e->order = order;
848 e->bytes = G.pagesize;
849 e->page = page + (i << G.lg_pagesize);
850 e->next = f;
851 f = e;
854 G.free_pages = f;
856 else
857 page = alloc_anon (NULL, entry_size, true);
858 #endif
859 #ifdef USING_MALLOC_PAGE_GROUPS
860 else
862 /* Allocate a large block of memory and serve out the aligned
863 pages therein. This results in much less memory wastage
864 than the traditional implementation of valloc. */
866 char *allocation, *a, *enda;
867 size_t alloc_size, head_slop, tail_slop;
868 int multiple_pages = (entry_size == G.pagesize);
870 if (multiple_pages)
871 alloc_size = GGC_QUIRE_SIZE * G.pagesize;
872 else
873 alloc_size = entry_size + G.pagesize - 1;
874 allocation = XNEWVEC (char, alloc_size);
876 page = (char *) (((uintptr_t) allocation + G.pagesize - 1) & -G.pagesize);
877 head_slop = page - allocation;
878 if (multiple_pages)
879 tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
880 else
881 tail_slop = alloc_size - entry_size - head_slop;
882 enda = allocation + alloc_size - tail_slop;
884 /* We allocated N pages, which are likely not aligned, leaving
885 us with N-1 usable pages. We plan to place the page_group
886 structure somewhere in the slop. */
887 if (head_slop >= sizeof (page_group))
888 group = (page_group *)page - 1;
889 else
891 /* We magically got an aligned allocation. Too bad, we have
892 to waste a page anyway. */
893 if (tail_slop == 0)
895 enda -= G.pagesize;
896 tail_slop += G.pagesize;
898 gcc_assert (tail_slop >= sizeof (page_group));
899 group = (page_group *)enda;
900 tail_slop -= sizeof (page_group);
903 /* Remember that we allocated this memory. */
904 group->next = G.page_groups;
905 group->allocation = allocation;
906 group->alloc_size = alloc_size;
907 group->in_use = 0;
908 G.page_groups = group;
909 G.bytes_mapped += alloc_size;
911 /* If we allocated multiple pages, put the rest on the free list. */
912 if (multiple_pages)
914 struct page_entry *e, *f = G.free_pages;
915 for (a = enda - G.pagesize; a != page; a -= G.pagesize)
917 e = XCNEWVAR (struct page_entry, page_entry_size);
918 e->order = order;
919 e->bytes = G.pagesize;
920 e->page = a;
921 e->group = group;
922 e->next = f;
923 f = e;
925 G.free_pages = f;
928 #endif
930 if (entry == NULL)
931 entry = XCNEWVAR (struct page_entry, page_entry_size);
933 entry->bytes = entry_size;
934 entry->page = page;
935 entry->context_depth = G.context_depth;
936 entry->order = order;
937 entry->num_free_objects = num_objects;
938 entry->next_bit_hint = 1;
940 G.context_depth_allocations |= (unsigned long)1 << G.context_depth;
942 #ifdef USING_MALLOC_PAGE_GROUPS
943 entry->group = group;
944 set_page_group_in_use (group, page);
945 #endif
947 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
948 increment the hint. */
949 entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
950 = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
952 set_page_table_entry (page, entry);
954 if (GGC_DEBUG_LEVEL >= 2)
955 fprintf (G.debug_file,
956 "Allocating page at %p, object size=%lu, data %p-%p\n",
957 (void *) entry, (unsigned long) OBJECT_SIZE (order), page,
958 page + entry_size - 1);
960 return entry;
963 /* Adjust the size of G.depth so that no index greater than the one
964 used by the top of the G.by_depth is used. */
966 static inline void
967 adjust_depth (void)
969 page_entry *top;
971 if (G.by_depth_in_use)
973 top = G.by_depth[G.by_depth_in_use-1];
975 /* Peel back indices in depth that index into by_depth, so that
976 as new elements are added to by_depth, we note the indices
977 of those elements, if they are for new context depths. */
978 while (G.depth_in_use > (size_t)top->context_depth+1)
979 --G.depth_in_use;
983 /* For a page that is no longer needed, put it on the free page list. */
985 static void
986 free_page (page_entry *entry)
988 if (GGC_DEBUG_LEVEL >= 2)
989 fprintf (G.debug_file,
990 "Deallocating page at %p, data %p-%p\n", (void *) entry,
991 entry->page, entry->page + entry->bytes - 1);
993 /* Mark the page as inaccessible. Discard the handle to avoid handle
994 leak. */
995 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (entry->page, entry->bytes));
997 set_page_table_entry (entry->page, NULL);
999 #ifdef USING_MALLOC_PAGE_GROUPS
1000 clear_page_group_in_use (entry->group, entry->page);
1001 #endif
1003 if (G.by_depth_in_use > 1)
1005 page_entry *top = G.by_depth[G.by_depth_in_use-1];
1006 int i = entry->index_by_depth;
1008 /* We cannot free a page from a context deeper than the current
1009 one. */
1010 gcc_assert (entry->context_depth == top->context_depth);
1012 /* Put top element into freed slot. */
1013 G.by_depth[i] = top;
1014 G.save_in_use[i] = G.save_in_use[G.by_depth_in_use-1];
1015 top->index_by_depth = i;
1017 --G.by_depth_in_use;
1019 adjust_depth ();
1021 entry->next = G.free_pages;
1022 G.free_pages = entry;
1025 /* Release the free page cache to the system. */
1027 static void
1028 release_pages (void)
1030 #ifdef USING_MADVISE
1031 page_entry *p, *start_p;
1032 char *start;
1033 size_t len;
1034 size_t mapped_len;
1035 page_entry *next, *prev, *newprev;
1036 size_t free_unit = (GGC_QUIRE_SIZE/2) * G.pagesize;
1038 /* First free larger continuous areas to the OS.
1039 This allows other allocators to grab these areas if needed.
1040 This is only done on larger chunks to avoid fragmentation.
1041 This does not always work because the free_pages list is only
1042 approximately sorted. */
1044 p = G.free_pages;
1045 prev = NULL;
1046 while (p)
1048 start = p->page;
1049 start_p = p;
1050 len = 0;
1051 mapped_len = 0;
1052 newprev = prev;
1053 while (p && p->page == start + len)
1055 len += p->bytes;
1056 if (!p->discarded)
1057 mapped_len += p->bytes;
1058 newprev = p;
1059 p = p->next;
1061 if (len >= free_unit)
1063 while (start_p != p)
1065 next = start_p->next;
1066 free (start_p);
1067 start_p = next;
1069 munmap (start, len);
1070 if (prev)
1071 prev->next = p;
1072 else
1073 G.free_pages = p;
1074 G.bytes_mapped -= mapped_len;
1075 continue;
1077 prev = newprev;
1080 /* Now give back the fragmented pages to the OS, but keep the address
1081 space to reuse it next time. */
1083 for (p = G.free_pages; p; )
1085 if (p->discarded)
1087 p = p->next;
1088 continue;
1090 start = p->page;
1091 len = p->bytes;
1092 start_p = p;
1093 p = p->next;
1094 while (p && p->page == start + len)
1096 len += p->bytes;
1097 p = p->next;
1099 /* Give the page back to the kernel, but don't free the mapping.
1100 This avoids fragmentation in the virtual memory map of the
1101 process. Next time we can reuse it by just touching it. */
1102 madvise (start, len, MADV_DONTNEED);
1103 /* Don't count those pages as mapped to not touch the garbage collector
1104 unnecessarily. */
1105 G.bytes_mapped -= len;
1106 while (start_p != p)
1108 start_p->discarded = true;
1109 start_p = start_p->next;
1112 #endif
1113 #if defined(USING_MMAP) && !defined(USING_MADVISE)
1114 page_entry *p, *next;
1115 char *start;
1116 size_t len;
1118 /* Gather up adjacent pages so they are unmapped together. */
1119 p = G.free_pages;
1121 while (p)
1123 start = p->page;
1124 next = p->next;
1125 len = p->bytes;
1126 free (p);
1127 p = next;
1129 while (p && p->page == start + len)
1131 next = p->next;
1132 len += p->bytes;
1133 free (p);
1134 p = next;
1137 munmap (start, len);
1138 G.bytes_mapped -= len;
1141 G.free_pages = NULL;
1142 #endif
1143 #ifdef USING_MALLOC_PAGE_GROUPS
1144 page_entry **pp, *p;
1145 page_group **gp, *g;
1147 /* Remove all pages from free page groups from the list. */
1148 pp = &G.free_pages;
1149 while ((p = *pp) != NULL)
1150 if (p->group->in_use == 0)
1152 *pp = p->next;
1153 free (p);
1155 else
1156 pp = &p->next;
1158 /* Remove all free page groups, and release the storage. */
1159 gp = &G.page_groups;
1160 while ((g = *gp) != NULL)
1161 if (g->in_use == 0)
1163 *gp = g->next;
1164 G.bytes_mapped -= g->alloc_size;
1165 free (g->allocation);
1167 else
1168 gp = &g->next;
1169 #endif
1172 /* This table provides a fast way to determine ceil(log_2(size)) for
1173 allocation requests. The minimum allocation size is eight bytes. */
1174 #define NUM_SIZE_LOOKUP 512
1175 static unsigned char size_lookup[NUM_SIZE_LOOKUP] =
1177 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
1178 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1179 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1180 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1181 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1182 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1183 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1184 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1185 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1186 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1187 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1188 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1189 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1190 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1191 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1192 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1193 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1194 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1195 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1196 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1197 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1198 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1199 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1200 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1201 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1202 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1203 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1204 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1205 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1206 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1207 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1208 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9
1211 /* For a given size of memory requested for allocation, return the
1212 actual size that is going to be allocated, as well as the size
1213 order. */
1215 static void
1216 ggc_round_alloc_size_1 (size_t requested_size,
1217 size_t *size_order,
1218 size_t *alloced_size)
1220 size_t order, object_size;
1222 if (requested_size < NUM_SIZE_LOOKUP)
1224 order = size_lookup[requested_size];
1225 object_size = OBJECT_SIZE (order);
1227 else
1229 order = 10;
1230 while (requested_size > (object_size = OBJECT_SIZE (order)))
1231 order++;
1234 if (size_order)
1235 *size_order = order;
1236 if (alloced_size)
1237 *alloced_size = object_size;
1240 /* For a given size of memory requested for allocation, return the
1241 actual size that is going to be allocated. */
1243 size_t
1244 ggc_round_alloc_size (size_t requested_size)
1246 size_t size = 0;
1248 ggc_round_alloc_size_1 (requested_size, NULL, &size);
1249 return size;
1252 /* Allocate a chunk of memory of SIZE bytes. Its contents are undefined. */
1254 void *
1255 ggc_internal_alloc (size_t size, void (*f)(void *), size_t s, size_t n
1256 MEM_STAT_DECL)
1258 size_t order, word, bit, object_offset, object_size;
1259 struct page_entry *entry;
1260 void *result;
1262 ggc_round_alloc_size_1 (size, &order, &object_size);
1264 /* If there are non-full pages for this size allocation, they are at
1265 the head of the list. */
1266 entry = G.pages[order];
1268 /* If there is no page for this object size, or all pages in this
1269 context are full, allocate a new page. */
1270 if (entry == NULL || entry->num_free_objects == 0)
1272 struct page_entry *new_entry;
1273 new_entry = alloc_page (order);
1275 new_entry->index_by_depth = G.by_depth_in_use;
1276 push_by_depth (new_entry, 0);
1278 /* We can skip context depths, if we do, make sure we go all the
1279 way to the new depth. */
1280 while (new_entry->context_depth >= G.depth_in_use)
1281 push_depth (G.by_depth_in_use-1);
1283 /* If this is the only entry, it's also the tail. If it is not
1284 the only entry, then we must update the PREV pointer of the
1285 ENTRY (G.pages[order]) to point to our new page entry. */
1286 if (entry == NULL)
1287 G.page_tails[order] = new_entry;
1288 else
1289 entry->prev = new_entry;
1291 /* Put new pages at the head of the page list. By definition the
1292 entry at the head of the list always has a NULL pointer. */
1293 new_entry->next = entry;
1294 new_entry->prev = NULL;
1295 entry = new_entry;
1296 G.pages[order] = new_entry;
1298 /* For a new page, we know the word and bit positions (in the
1299 in_use bitmap) of the first available object -- they're zero. */
1300 new_entry->next_bit_hint = 1;
1301 word = 0;
1302 bit = 0;
1303 object_offset = 0;
1305 else
1307 /* First try to use the hint left from the previous allocation
1308 to locate a clear bit in the in-use bitmap. We've made sure
1309 that the one-past-the-end bit is always set, so if the hint
1310 has run over, this test will fail. */
1311 unsigned hint = entry->next_bit_hint;
1312 word = hint / HOST_BITS_PER_LONG;
1313 bit = hint % HOST_BITS_PER_LONG;
1315 /* If the hint didn't work, scan the bitmap from the beginning. */
1316 if ((entry->in_use_p[word] >> bit) & 1)
1318 word = bit = 0;
1319 while (~entry->in_use_p[word] == 0)
1320 ++word;
1322 #if GCC_VERSION >= 3004
1323 bit = __builtin_ctzl (~entry->in_use_p[word]);
1324 #else
1325 while ((entry->in_use_p[word] >> bit) & 1)
1326 ++bit;
1327 #endif
1329 hint = word * HOST_BITS_PER_LONG + bit;
1332 /* Next time, try the next bit. */
1333 entry->next_bit_hint = hint + 1;
1335 object_offset = hint * object_size;
1338 /* Set the in-use bit. */
1339 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
1341 /* Keep a running total of the number of free objects. If this page
1342 fills up, we may have to move it to the end of the list if the
1343 next page isn't full. If the next page is full, all subsequent
1344 pages are full, so there's no need to move it. */
1345 if (--entry->num_free_objects == 0
1346 && entry->next != NULL
1347 && entry->next->num_free_objects > 0)
1349 /* We have a new head for the list. */
1350 G.pages[order] = entry->next;
1352 /* We are moving ENTRY to the end of the page table list.
1353 The new page at the head of the list will have NULL in
1354 its PREV field and ENTRY will have NULL in its NEXT field. */
1355 entry->next->prev = NULL;
1356 entry->next = NULL;
1358 /* Append ENTRY to the tail of the list. */
1359 entry->prev = G.page_tails[order];
1360 G.page_tails[order]->next = entry;
1361 G.page_tails[order] = entry;
1364 /* Calculate the object's address. */
1365 result = entry->page + object_offset;
1366 if (GATHER_STATISTICS)
1367 ggc_record_overhead (OBJECT_SIZE (order), OBJECT_SIZE (order) - size,
1368 result FINAL_PASS_MEM_STAT);
1370 #ifdef ENABLE_GC_CHECKING
1371 /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
1372 exact same semantics in presence of memory bugs, regardless of
1373 ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
1374 handle to avoid handle leak. */
1375 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, object_size));
1377 /* `Poison' the entire allocated object, including any padding at
1378 the end. */
1379 memset (result, 0xaf, object_size);
1381 /* Make the bytes after the end of the object unaccessible. Discard the
1382 handle to avoid handle leak. */
1383 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) result + size,
1384 object_size - size));
1385 #endif
1387 /* Tell Valgrind that the memory is there, but its content isn't
1388 defined. The bytes at the end of the object are still marked
1389 unaccessible. */
1390 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, size));
1392 /* Keep track of how many bytes are being allocated. This
1393 information is used in deciding when to collect. */
1394 G.allocated += object_size;
1396 /* For timevar statistics. */
1397 timevar_ggc_mem_total += object_size;
1399 if (f && n == 1)
1400 G.finalizers.safe_push (finalizer (result, f));
1401 else if (f)
1402 G.vec_finalizers.safe_push
1403 (vec_finalizer (reinterpret_cast<uintptr_t> (result), f, s, n));
1405 if (GATHER_STATISTICS)
1407 size_t overhead = object_size - size;
1409 G.stats.total_overhead += overhead;
1410 G.stats.total_allocated += object_size;
1411 G.stats.total_overhead_per_order[order] += overhead;
1412 G.stats.total_allocated_per_order[order] += object_size;
1414 if (size <= 32)
1416 G.stats.total_overhead_under32 += overhead;
1417 G.stats.total_allocated_under32 += object_size;
1419 if (size <= 64)
1421 G.stats.total_overhead_under64 += overhead;
1422 G.stats.total_allocated_under64 += object_size;
1424 if (size <= 128)
1426 G.stats.total_overhead_under128 += overhead;
1427 G.stats.total_allocated_under128 += object_size;
1431 if (GGC_DEBUG_LEVEL >= 3)
1432 fprintf (G.debug_file,
1433 "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
1434 (unsigned long) size, (unsigned long) object_size, result,
1435 (void *) entry);
1437 return result;
1440 /* Mark function for strings. */
1442 void
1443 gt_ggc_m_S (const void *p)
1445 page_entry *entry;
1446 unsigned bit, word;
1447 unsigned long mask;
1448 unsigned long offset;
1450 if (!p || !ggc_allocated_p (p))
1451 return;
1453 /* Look up the page on which the object is alloced. . */
1454 entry = lookup_page_table_entry (p);
1455 gcc_assert (entry);
1457 /* Calculate the index of the object on the page; this is its bit
1458 position in the in_use_p bitmap. Note that because a char* might
1459 point to the middle of an object, we need special code here to
1460 make sure P points to the start of an object. */
1461 offset = ((const char *) p - entry->page) % object_size_table[entry->order];
1462 if (offset)
1464 /* Here we've seen a char* which does not point to the beginning
1465 of an allocated object. We assume it points to the middle of
1466 a STRING_CST. */
1467 gcc_assert (offset == offsetof (struct tree_string, str));
1468 p = ((const char *) p) - offset;
1469 gt_ggc_mx_lang_tree_node (CONST_CAST (void *, p));
1470 return;
1473 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1474 word = bit / HOST_BITS_PER_LONG;
1475 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1477 /* If the bit was previously set, skip it. */
1478 if (entry->in_use_p[word] & mask)
1479 return;
1481 /* Otherwise set it, and decrement the free object count. */
1482 entry->in_use_p[word] |= mask;
1483 entry->num_free_objects -= 1;
1485 if (GGC_DEBUG_LEVEL >= 4)
1486 fprintf (G.debug_file, "Marking %p\n", p);
1488 return;
1492 /* User-callable entry points for marking string X. */
1494 void
1495 gt_ggc_mx (const char *& x)
1497 gt_ggc_m_S (x);
1500 void
1501 gt_ggc_mx (unsigned char *& x)
1503 gt_ggc_m_S (x);
1506 void
1507 gt_ggc_mx (unsigned char& x ATTRIBUTE_UNUSED)
1511 /* If P is not marked, marks it and return false. Otherwise return true.
1512 P must have been allocated by the GC allocator; it mustn't point to
1513 static objects, stack variables, or memory allocated with malloc. */
1516 ggc_set_mark (const void *p)
1518 page_entry *entry;
1519 unsigned bit, word;
1520 unsigned long mask;
1522 /* Look up the page on which the object is alloced. If the object
1523 wasn't allocated by the collector, we'll probably die. */
1524 entry = lookup_page_table_entry (p);
1525 gcc_assert (entry);
1527 /* Calculate the index of the object on the page; this is its bit
1528 position in the in_use_p bitmap. */
1529 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1530 word = bit / HOST_BITS_PER_LONG;
1531 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1533 /* If the bit was previously set, skip it. */
1534 if (entry->in_use_p[word] & mask)
1535 return 1;
1537 /* Otherwise set it, and decrement the free object count. */
1538 entry->in_use_p[word] |= mask;
1539 entry->num_free_objects -= 1;
1541 if (GGC_DEBUG_LEVEL >= 4)
1542 fprintf (G.debug_file, "Marking %p\n", p);
1544 return 0;
1547 /* Return 1 if P has been marked, zero otherwise.
1548 P must have been allocated by the GC allocator; it mustn't point to
1549 static objects, stack variables, or memory allocated with malloc. */
1552 ggc_marked_p (const void *p)
1554 page_entry *entry;
1555 unsigned bit, word;
1556 unsigned long mask;
1558 /* Look up the page on which the object is alloced. If the object
1559 wasn't allocated by the collector, we'll probably die. */
1560 entry = lookup_page_table_entry (p);
1561 gcc_assert (entry);
1563 /* Calculate the index of the object on the page; this is its bit
1564 position in the in_use_p bitmap. */
1565 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1566 word = bit / HOST_BITS_PER_LONG;
1567 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1569 return (entry->in_use_p[word] & mask) != 0;
1572 /* Return the size of the gc-able object P. */
1574 size_t
1575 ggc_get_size (const void *p)
1577 page_entry *pe = lookup_page_table_entry (p);
1578 return OBJECT_SIZE (pe->order);
1581 /* Release the memory for object P. */
1583 void
1584 ggc_free (void *p)
1586 if (in_gc)
1587 return;
1589 page_entry *pe = lookup_page_table_entry (p);
1590 size_t order = pe->order;
1591 size_t size = OBJECT_SIZE (order);
1593 if (GATHER_STATISTICS)
1594 ggc_free_overhead (p);
1596 if (GGC_DEBUG_LEVEL >= 3)
1597 fprintf (G.debug_file,
1598 "Freeing object, actual size=%lu, at %p on %p\n",
1599 (unsigned long) size, p, (void *) pe);
1601 #ifdef ENABLE_GC_CHECKING
1602 /* Poison the data, to indicate the data is garbage. */
1603 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
1604 memset (p, 0xa5, size);
1605 #endif
1606 /* Let valgrind know the object is free. */
1607 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
1609 #ifdef ENABLE_GC_ALWAYS_COLLECT
1610 /* In the completely-anal-checking mode, we do *not* immediately free
1611 the data, but instead verify that the data is *actually* not
1612 reachable the next time we collect. */
1614 struct free_object *fo = XNEW (struct free_object);
1615 fo->object = p;
1616 fo->next = G.free_object_list;
1617 G.free_object_list = fo;
1619 #else
1621 unsigned int bit_offset, word, bit;
1623 G.allocated -= size;
1625 /* Mark the object not-in-use. */
1626 bit_offset = OFFSET_TO_BIT (((const char *) p) - pe->page, order);
1627 word = bit_offset / HOST_BITS_PER_LONG;
1628 bit = bit_offset % HOST_BITS_PER_LONG;
1629 pe->in_use_p[word] &= ~(1UL << bit);
1631 if (pe->num_free_objects++ == 0)
1633 page_entry *p, *q;
1635 /* If the page is completely full, then it's supposed to
1636 be after all pages that aren't. Since we've freed one
1637 object from a page that was full, we need to move the
1638 page to the head of the list.
1640 PE is the node we want to move. Q is the previous node
1641 and P is the next node in the list. */
1642 q = pe->prev;
1643 if (q && q->num_free_objects == 0)
1645 p = pe->next;
1647 q->next = p;
1649 /* If PE was at the end of the list, then Q becomes the
1650 new end of the list. If PE was not the end of the
1651 list, then we need to update the PREV field for P. */
1652 if (!p)
1653 G.page_tails[order] = q;
1654 else
1655 p->prev = q;
1657 /* Move PE to the head of the list. */
1658 pe->next = G.pages[order];
1659 pe->prev = NULL;
1660 G.pages[order]->prev = pe;
1661 G.pages[order] = pe;
1664 /* Reset the hint bit to point to the only free object. */
1665 pe->next_bit_hint = bit_offset;
1668 #endif
1671 /* Subroutine of init_ggc which computes the pair of numbers used to
1672 perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1674 This algorithm is taken from Granlund and Montgomery's paper
1675 "Division by Invariant Integers using Multiplication"
1676 (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1677 constants). */
1679 static void
1680 compute_inverse (unsigned order)
1682 size_t size, inv;
1683 unsigned int e;
1685 size = OBJECT_SIZE (order);
1686 e = 0;
1687 while (size % 2 == 0)
1689 e++;
1690 size >>= 1;
1693 inv = size;
1694 while (inv * size != 1)
1695 inv = inv * (2 - inv*size);
1697 DIV_MULT (order) = inv;
1698 DIV_SHIFT (order) = e;
1701 /* Initialize the ggc-mmap allocator. */
1702 void
1703 init_ggc (void)
1705 static bool init_p = false;
1706 unsigned order;
1708 if (init_p)
1709 return;
1710 init_p = true;
1712 G.pagesize = getpagesize ();
1713 G.lg_pagesize = exact_log2 (G.pagesize);
1715 #ifdef HAVE_MMAP_DEV_ZERO
1716 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1717 if (G.dev_zero_fd == -1)
1718 internal_error ("open /dev/zero: %m");
1719 #endif
1721 #if 0
1722 G.debug_file = fopen ("ggc-mmap.debug", "w");
1723 #else
1724 G.debug_file = stdout;
1725 #endif
1727 #ifdef USING_MMAP
1728 /* StunOS has an amazing off-by-one error for the first mmap allocation
1729 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1730 believe, is an unaligned page allocation, which would cause us to
1731 hork badly if we tried to use it. */
1733 char *p = alloc_anon (NULL, G.pagesize, true);
1734 struct page_entry *e;
1735 if ((uintptr_t)p & (G.pagesize - 1))
1737 /* How losing. Discard this one and try another. If we still
1738 can't get something useful, give up. */
1740 p = alloc_anon (NULL, G.pagesize, true);
1741 gcc_assert (!((uintptr_t)p & (G.pagesize - 1)));
1744 /* We have a good page, might as well hold onto it... */
1745 e = XCNEW (struct page_entry);
1746 e->bytes = G.pagesize;
1747 e->page = p;
1748 e->next = G.free_pages;
1749 G.free_pages = e;
1751 #endif
1753 /* Initialize the object size table. */
1754 for (order = 0; order < HOST_BITS_PER_PTR; ++order)
1755 object_size_table[order] = (size_t) 1 << order;
1756 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1758 size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
1760 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1761 so that we're sure of getting aligned memory. */
1762 s = ROUND_UP (s, MAX_ALIGNMENT);
1763 object_size_table[order] = s;
1766 /* Initialize the objects-per-page and inverse tables. */
1767 for (order = 0; order < NUM_ORDERS; ++order)
1769 objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
1770 if (objects_per_page_table[order] == 0)
1771 objects_per_page_table[order] = 1;
1772 compute_inverse (order);
1775 /* Reset the size_lookup array to put appropriately sized objects in
1776 the special orders. All objects bigger than the previous power
1777 of two, but no greater than the special size, should go in the
1778 new order. */
1779 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1781 int o;
1782 int i;
1784 i = OBJECT_SIZE (order);
1785 if (i >= NUM_SIZE_LOOKUP)
1786 continue;
1788 for (o = size_lookup[i]; o == size_lookup [i]; --i)
1789 size_lookup[i] = order;
1792 G.depth_in_use = 0;
1793 G.depth_max = 10;
1794 G.depth = XNEWVEC (unsigned int, G.depth_max);
1796 G.by_depth_in_use = 0;
1797 G.by_depth_max = INITIAL_PTE_COUNT;
1798 G.by_depth = XNEWVEC (page_entry *, G.by_depth_max);
1799 G.save_in_use = XNEWVEC (unsigned long *, G.by_depth_max);
1802 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1803 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1805 static void
1806 ggc_recalculate_in_use_p (page_entry *p)
1808 unsigned int i;
1809 size_t num_objects;
1811 /* Because the past-the-end bit in in_use_p is always set, we
1812 pretend there is one additional object. */
1813 num_objects = OBJECTS_IN_PAGE (p) + 1;
1815 /* Reset the free object count. */
1816 p->num_free_objects = num_objects;
1818 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1819 for (i = 0;
1820 i < CEIL (BITMAP_SIZE (num_objects),
1821 sizeof (*p->in_use_p));
1822 ++i)
1824 unsigned long j;
1826 /* Something is in use if it is marked, or if it was in use in a
1827 context further down the context stack. */
1828 p->in_use_p[i] |= save_in_use_p (p)[i];
1830 /* Decrement the free object count for every object allocated. */
1831 for (j = p->in_use_p[i]; j; j >>= 1)
1832 p->num_free_objects -= (j & 1);
1835 gcc_assert (p->num_free_objects < num_objects);
1838 /* Unmark all objects. */
1840 static void
1841 clear_marks (void)
1843 unsigned order;
1845 for (order = 2; order < NUM_ORDERS; order++)
1847 page_entry *p;
1849 for (p = G.pages[order]; p != NULL; p = p->next)
1851 size_t num_objects = OBJECTS_IN_PAGE (p);
1852 size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
1854 /* The data should be page-aligned. */
1855 gcc_assert (!((uintptr_t) p->page & (G.pagesize - 1)));
1857 /* Pages that aren't in the topmost context are not collected;
1858 nevertheless, we need their in-use bit vectors to store GC
1859 marks. So, back them up first. */
1860 if (p->context_depth < G.context_depth)
1862 if (! save_in_use_p (p))
1863 save_in_use_p (p) = XNEWVAR (unsigned long, bitmap_size);
1864 memcpy (save_in_use_p (p), p->in_use_p, bitmap_size);
1867 /* Reset reset the number of free objects and clear the
1868 in-use bits. These will be adjusted by mark_obj. */
1869 p->num_free_objects = num_objects;
1870 memset (p->in_use_p, 0, bitmap_size);
1872 /* Make sure the one-past-the-end bit is always set. */
1873 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
1874 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
1879 /* Check if any blocks with a registered finalizer have become unmarked. If so
1880 run the finalizer and unregister it because the block is about to be freed.
1881 Note that no garantee is made about what order finalizers will run in so
1882 touching other objects in gc memory is extremely unwise. */
1884 static void
1885 ggc_handle_finalizers ()
1887 if (G.context_depth != 0)
1888 return;
1890 unsigned length = G.finalizers.length ();
1891 for (unsigned int i = 0; i < length;)
1893 finalizer &f = G.finalizers[i];
1894 if (!ggc_marked_p (f.addr ()))
1896 f.call ();
1897 G.finalizers.unordered_remove (i);
1898 length--;
1900 else
1901 i++;
1905 length = G.vec_finalizers.length ();
1906 for (unsigned int i = 0; i < length;)
1908 vec_finalizer &f = G.vec_finalizers[i];
1909 if (!ggc_marked_p (f.addr ()))
1911 f.call ();
1912 G.vec_finalizers.unordered_remove (i);
1913 length--;
1915 else
1916 i++;
1920 /* Free all empty pages. Partially empty pages need no attention
1921 because the `mark' bit doubles as an `unused' bit. */
1923 static void
1924 sweep_pages (void)
1926 unsigned order;
1928 for (order = 2; order < NUM_ORDERS; order++)
1930 /* The last page-entry to consider, regardless of entries
1931 placed at the end of the list. */
1932 page_entry * const last = G.page_tails[order];
1934 size_t num_objects;
1935 size_t live_objects;
1936 page_entry *p, *previous;
1937 int done;
1939 p = G.pages[order];
1940 if (p == NULL)
1941 continue;
1943 previous = NULL;
1946 page_entry *next = p->next;
1948 /* Loop until all entries have been examined. */
1949 done = (p == last);
1951 num_objects = OBJECTS_IN_PAGE (p);
1953 /* Add all live objects on this page to the count of
1954 allocated memory. */
1955 live_objects = num_objects - p->num_free_objects;
1957 G.allocated += OBJECT_SIZE (order) * live_objects;
1959 /* Only objects on pages in the topmost context should get
1960 collected. */
1961 if (p->context_depth < G.context_depth)
1964 /* Remove the page if it's empty. */
1965 else if (live_objects == 0)
1967 /* If P was the first page in the list, then NEXT
1968 becomes the new first page in the list, otherwise
1969 splice P out of the forward pointers. */
1970 if (! previous)
1971 G.pages[order] = next;
1972 else
1973 previous->next = next;
1975 /* Splice P out of the back pointers too. */
1976 if (next)
1977 next->prev = previous;
1979 /* Are we removing the last element? */
1980 if (p == G.page_tails[order])
1981 G.page_tails[order] = previous;
1982 free_page (p);
1983 p = previous;
1986 /* If the page is full, move it to the end. */
1987 else if (p->num_free_objects == 0)
1989 /* Don't move it if it's already at the end. */
1990 if (p != G.page_tails[order])
1992 /* Move p to the end of the list. */
1993 p->next = NULL;
1994 p->prev = G.page_tails[order];
1995 G.page_tails[order]->next = p;
1997 /* Update the tail pointer... */
1998 G.page_tails[order] = p;
2000 /* ... and the head pointer, if necessary. */
2001 if (! previous)
2002 G.pages[order] = next;
2003 else
2004 previous->next = next;
2006 /* And update the backpointer in NEXT if necessary. */
2007 if (next)
2008 next->prev = previous;
2010 p = previous;
2014 /* If we've fallen through to here, it's a page in the
2015 topmost context that is neither full nor empty. Such a
2016 page must precede pages at lesser context depth in the
2017 list, so move it to the head. */
2018 else if (p != G.pages[order])
2020 previous->next = p->next;
2022 /* Update the backchain in the next node if it exists. */
2023 if (p->next)
2024 p->next->prev = previous;
2026 /* Move P to the head of the list. */
2027 p->next = G.pages[order];
2028 p->prev = NULL;
2029 G.pages[order]->prev = p;
2031 /* Update the head pointer. */
2032 G.pages[order] = p;
2034 /* Are we moving the last element? */
2035 if (G.page_tails[order] == p)
2036 G.page_tails[order] = previous;
2037 p = previous;
2040 previous = p;
2041 p = next;
2043 while (! done);
2045 /* Now, restore the in_use_p vectors for any pages from contexts
2046 other than the current one. */
2047 for (p = G.pages[order]; p; p = p->next)
2048 if (p->context_depth != G.context_depth)
2049 ggc_recalculate_in_use_p (p);
2053 #ifdef ENABLE_GC_CHECKING
2054 /* Clobber all free objects. */
2056 static void
2057 poison_pages (void)
2059 unsigned order;
2061 for (order = 2; order < NUM_ORDERS; order++)
2063 size_t size = OBJECT_SIZE (order);
2064 page_entry *p;
2066 for (p = G.pages[order]; p != NULL; p = p->next)
2068 size_t num_objects;
2069 size_t i;
2071 if (p->context_depth != G.context_depth)
2072 /* Since we don't do any collection for pages in pushed
2073 contexts, there's no need to do any poisoning. And
2074 besides, the IN_USE_P array isn't valid until we pop
2075 contexts. */
2076 continue;
2078 num_objects = OBJECTS_IN_PAGE (p);
2079 for (i = 0; i < num_objects; i++)
2081 size_t word, bit;
2082 word = i / HOST_BITS_PER_LONG;
2083 bit = i % HOST_BITS_PER_LONG;
2084 if (((p->in_use_p[word] >> bit) & 1) == 0)
2086 char *object = p->page + i * size;
2088 /* Keep poison-by-write when we expect to use Valgrind,
2089 so the exact same memory semantics is kept, in case
2090 there are memory errors. We override this request
2091 below. */
2092 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (object,
2093 size));
2094 memset (object, 0xa5, size);
2096 /* Drop the handle to avoid handle leak. */
2097 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (object, size));
2103 #else
2104 #define poison_pages()
2105 #endif
2107 #ifdef ENABLE_GC_ALWAYS_COLLECT
2108 /* Validate that the reportedly free objects actually are. */
2110 static void
2111 validate_free_objects (void)
2113 struct free_object *f, *next, *still_free = NULL;
2115 for (f = G.free_object_list; f ; f = next)
2117 page_entry *pe = lookup_page_table_entry (f->object);
2118 size_t bit, word;
2120 bit = OFFSET_TO_BIT ((char *)f->object - pe->page, pe->order);
2121 word = bit / HOST_BITS_PER_LONG;
2122 bit = bit % HOST_BITS_PER_LONG;
2123 next = f->next;
2125 /* Make certain it isn't visible from any root. Notice that we
2126 do this check before sweep_pages merges save_in_use_p. */
2127 gcc_assert (!(pe->in_use_p[word] & (1UL << bit)));
2129 /* If the object comes from an outer context, then retain the
2130 free_object entry, so that we can verify that the address
2131 isn't live on the stack in some outer context. */
2132 if (pe->context_depth != G.context_depth)
2134 f->next = still_free;
2135 still_free = f;
2137 else
2138 free (f);
2141 G.free_object_list = still_free;
2143 #else
2144 #define validate_free_objects()
2145 #endif
2147 /* Top level mark-and-sweep routine. */
2149 void
2150 ggc_collect (void)
2152 /* Avoid frequent unnecessary work by skipping collection if the
2153 total allocations haven't expanded much since the last
2154 collection. */
2155 float allocated_last_gc =
2156 MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
2158 float min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
2159 if (G.allocated < allocated_last_gc + min_expand && !ggc_force_collect)
2160 return;
2162 timevar_push (TV_GC);
2163 if (!quiet_flag)
2164 fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
2165 if (GGC_DEBUG_LEVEL >= 2)
2166 fprintf (G.debug_file, "BEGIN COLLECTING\n");
2168 /* Zero the total allocated bytes. This will be recalculated in the
2169 sweep phase. */
2170 G.allocated = 0;
2172 /* Release the pages we freed the last time we collected, but didn't
2173 reuse in the interim. */
2174 release_pages ();
2176 /* Indicate that we've seen collections at this context depth. */
2177 G.context_depth_collections = ((unsigned long)1 << (G.context_depth + 1)) - 1;
2179 invoke_plugin_callbacks (PLUGIN_GGC_START, NULL);
2181 in_gc = true;
2182 clear_marks ();
2183 ggc_mark_roots ();
2184 ggc_handle_finalizers ();
2186 if (GATHER_STATISTICS)
2187 ggc_prune_overhead_list ();
2189 poison_pages ();
2190 validate_free_objects ();
2191 sweep_pages ();
2193 in_gc = false;
2194 G.allocated_last_gc = G.allocated;
2196 invoke_plugin_callbacks (PLUGIN_GGC_END, NULL);
2198 timevar_pop (TV_GC);
2200 if (!quiet_flag)
2201 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
2202 if (GGC_DEBUG_LEVEL >= 2)
2203 fprintf (G.debug_file, "END COLLECTING\n");
2206 /* Assume that all GGC memory is reachable and grow the limits for next collection.
2207 With checking, trigger GGC so -Q compilation outputs how much of memory really is
2208 reachable. */
2210 void
2211 ggc_grow (void)
2213 #ifndef ENABLE_CHECKING
2214 G.allocated_last_gc = MAX (G.allocated_last_gc,
2215 G.allocated);
2216 #else
2217 ggc_collect ();
2218 #endif
2219 if (!quiet_flag)
2220 fprintf (stderr, " {GC start %luk} ", (unsigned long) G.allocated / 1024);
2223 /* Print allocation statistics. */
2224 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
2225 ? (x) \
2226 : ((x) < 1024*1024*10 \
2227 ? (x) / 1024 \
2228 : (x) / (1024*1024))))
2229 #define STAT_LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
2231 void
2232 ggc_print_statistics (void)
2234 struct ggc_statistics stats;
2235 unsigned int i;
2236 size_t total_overhead = 0;
2238 /* Clear the statistics. */
2239 memset (&stats, 0, sizeof (stats));
2241 /* Make sure collection will really occur. */
2242 G.allocated_last_gc = 0;
2244 /* Collect and print the statistics common across collectors. */
2245 ggc_print_common_statistics (stderr, &stats);
2247 /* Release free pages so that we will not count the bytes allocated
2248 there as part of the total allocated memory. */
2249 release_pages ();
2251 /* Collect some information about the various sizes of
2252 allocation. */
2253 fprintf (stderr,
2254 "Memory still allocated at the end of the compilation process\n");
2255 fprintf (stderr, "%-8s %10s %10s %10s\n",
2256 "Size", "Allocated", "Used", "Overhead");
2257 for (i = 0; i < NUM_ORDERS; ++i)
2259 page_entry *p;
2260 size_t allocated;
2261 size_t in_use;
2262 size_t overhead;
2264 /* Skip empty entries. */
2265 if (!G.pages[i])
2266 continue;
2268 overhead = allocated = in_use = 0;
2270 /* Figure out the total number of bytes allocated for objects of
2271 this size, and how many of them are actually in use. Also figure
2272 out how much memory the page table is using. */
2273 for (p = G.pages[i]; p; p = p->next)
2275 allocated += p->bytes;
2276 in_use +=
2277 (OBJECTS_IN_PAGE (p) - p->num_free_objects) * OBJECT_SIZE (i);
2279 overhead += (sizeof (page_entry) - sizeof (long)
2280 + BITMAP_SIZE (OBJECTS_IN_PAGE (p) + 1));
2282 fprintf (stderr, "%-8lu %10lu%c %10lu%c %10lu%c\n",
2283 (unsigned long) OBJECT_SIZE (i),
2284 SCALE (allocated), STAT_LABEL (allocated),
2285 SCALE (in_use), STAT_LABEL (in_use),
2286 SCALE (overhead), STAT_LABEL (overhead));
2287 total_overhead += overhead;
2289 fprintf (stderr, "%-8s %10lu%c %10lu%c %10lu%c\n", "Total",
2290 SCALE (G.bytes_mapped), STAT_LABEL (G.bytes_mapped),
2291 SCALE (G.allocated), STAT_LABEL (G.allocated),
2292 SCALE (total_overhead), STAT_LABEL (total_overhead));
2294 if (GATHER_STATISTICS)
2296 fprintf (stderr, "\nTotal allocations and overheads during "
2297 "the compilation process\n");
2299 fprintf (stderr, "Total Overhead: %10"
2300 HOST_LONG_LONG_FORMAT "d\n", G.stats.total_overhead);
2301 fprintf (stderr, "Total Allocated: %10"
2302 HOST_LONG_LONG_FORMAT "d\n",
2303 G.stats.total_allocated);
2305 fprintf (stderr, "Total Overhead under 32B: %10"
2306 HOST_LONG_LONG_FORMAT "d\n", G.stats.total_overhead_under32);
2307 fprintf (stderr, "Total Allocated under 32B: %10"
2308 HOST_LONG_LONG_FORMAT "d\n", G.stats.total_allocated_under32);
2309 fprintf (stderr, "Total Overhead under 64B: %10"
2310 HOST_LONG_LONG_FORMAT "d\n", G.stats.total_overhead_under64);
2311 fprintf (stderr, "Total Allocated under 64B: %10"
2312 HOST_LONG_LONG_FORMAT "d\n", G.stats.total_allocated_under64);
2313 fprintf (stderr, "Total Overhead under 128B: %10"
2314 HOST_LONG_LONG_FORMAT "d\n", G.stats.total_overhead_under128);
2315 fprintf (stderr, "Total Allocated under 128B: %10"
2316 HOST_LONG_LONG_FORMAT "d\n", G.stats.total_allocated_under128);
2318 for (i = 0; i < NUM_ORDERS; i++)
2319 if (G.stats.total_allocated_per_order[i])
2321 fprintf (stderr, "Total Overhead page size %9lu: %10"
2322 HOST_LONG_LONG_FORMAT "d\n",
2323 (unsigned long) OBJECT_SIZE (i),
2324 G.stats.total_overhead_per_order[i]);
2325 fprintf (stderr, "Total Allocated page size %9lu: %10"
2326 HOST_LONG_LONG_FORMAT "d\n",
2327 (unsigned long) OBJECT_SIZE (i),
2328 G.stats.total_allocated_per_order[i]);
2333 struct ggc_pch_ondisk
2335 unsigned totals[NUM_ORDERS];
2338 struct ggc_pch_data
2340 struct ggc_pch_ondisk d;
2341 uintptr_t base[NUM_ORDERS];
2342 size_t written[NUM_ORDERS];
2345 struct ggc_pch_data *
2346 init_ggc_pch (void)
2348 return XCNEW (struct ggc_pch_data);
2351 void
2352 ggc_pch_count_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
2353 size_t size, bool is_string ATTRIBUTE_UNUSED)
2355 unsigned order;
2357 if (size < NUM_SIZE_LOOKUP)
2358 order = size_lookup[size];
2359 else
2361 order = 10;
2362 while (size > OBJECT_SIZE (order))
2363 order++;
2366 d->d.totals[order]++;
2369 size_t
2370 ggc_pch_total_size (struct ggc_pch_data *d)
2372 size_t a = 0;
2373 unsigned i;
2375 for (i = 0; i < NUM_ORDERS; i++)
2376 a += PAGE_ALIGN (d->d.totals[i] * OBJECT_SIZE (i));
2377 return a;
2380 void
2381 ggc_pch_this_base (struct ggc_pch_data *d, void *base)
2383 uintptr_t a = (uintptr_t) base;
2384 unsigned i;
2386 for (i = 0; i < NUM_ORDERS; i++)
2388 d->base[i] = a;
2389 a += PAGE_ALIGN (d->d.totals[i] * OBJECT_SIZE (i));
2394 char *
2395 ggc_pch_alloc_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
2396 size_t size, bool is_string ATTRIBUTE_UNUSED)
2398 unsigned order;
2399 char *result;
2401 if (size < NUM_SIZE_LOOKUP)
2402 order = size_lookup[size];
2403 else
2405 order = 10;
2406 while (size > OBJECT_SIZE (order))
2407 order++;
2410 result = (char *) d->base[order];
2411 d->base[order] += OBJECT_SIZE (order);
2412 return result;
2415 void
2416 ggc_pch_prepare_write (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
2417 FILE *f ATTRIBUTE_UNUSED)
2419 /* Nothing to do. */
2422 void
2423 ggc_pch_write_object (struct ggc_pch_data *d,
2424 FILE *f, void *x, void *newx ATTRIBUTE_UNUSED,
2425 size_t size, bool is_string ATTRIBUTE_UNUSED)
2427 unsigned order;
2428 static const char emptyBytes[256] = { 0 };
2430 if (size < NUM_SIZE_LOOKUP)
2431 order = size_lookup[size];
2432 else
2434 order = 10;
2435 while (size > OBJECT_SIZE (order))
2436 order++;
2439 if (fwrite (x, size, 1, f) != 1)
2440 fatal_error (input_location, "can%'t write PCH file: %m");
2442 /* If SIZE is not the same as OBJECT_SIZE(order), then we need to pad the
2443 object out to OBJECT_SIZE(order). This happens for strings. */
2445 if (size != OBJECT_SIZE (order))
2447 unsigned padding = OBJECT_SIZE (order) - size;
2449 /* To speed small writes, we use a nulled-out array that's larger
2450 than most padding requests as the source for our null bytes. This
2451 permits us to do the padding with fwrite() rather than fseek(), and
2452 limits the chance the OS may try to flush any outstanding writes. */
2453 if (padding <= sizeof (emptyBytes))
2455 if (fwrite (emptyBytes, 1, padding, f) != padding)
2456 fatal_error (input_location, "can%'t write PCH file");
2458 else
2460 /* Larger than our buffer? Just default to fseek. */
2461 if (fseek (f, padding, SEEK_CUR) != 0)
2462 fatal_error (input_location, "can%'t write PCH file");
2466 d->written[order]++;
2467 if (d->written[order] == d->d.totals[order]
2468 && fseek (f, ROUND_UP_VALUE (d->d.totals[order] * OBJECT_SIZE (order),
2469 G.pagesize),
2470 SEEK_CUR) != 0)
2471 fatal_error (input_location, "can%'t write PCH file: %m");
2474 void
2475 ggc_pch_finish (struct ggc_pch_data *d, FILE *f)
2477 if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
2478 fatal_error (input_location, "can%'t write PCH file: %m");
2479 free (d);
2482 /* Move the PCH PTE entries just added to the end of by_depth, to the
2483 front. */
2485 static void
2486 move_ptes_to_front (int count_old_page_tables, int count_new_page_tables)
2488 unsigned i;
2490 /* First, we swap the new entries to the front of the varrays. */
2491 page_entry **new_by_depth;
2492 unsigned long **new_save_in_use;
2494 new_by_depth = XNEWVEC (page_entry *, G.by_depth_max);
2495 new_save_in_use = XNEWVEC (unsigned long *, G.by_depth_max);
2497 memcpy (&new_by_depth[0],
2498 &G.by_depth[count_old_page_tables],
2499 count_new_page_tables * sizeof (void *));
2500 memcpy (&new_by_depth[count_new_page_tables],
2501 &G.by_depth[0],
2502 count_old_page_tables * sizeof (void *));
2503 memcpy (&new_save_in_use[0],
2504 &G.save_in_use[count_old_page_tables],
2505 count_new_page_tables * sizeof (void *));
2506 memcpy (&new_save_in_use[count_new_page_tables],
2507 &G.save_in_use[0],
2508 count_old_page_tables * sizeof (void *));
2510 free (G.by_depth);
2511 free (G.save_in_use);
2513 G.by_depth = new_by_depth;
2514 G.save_in_use = new_save_in_use;
2516 /* Now update all the index_by_depth fields. */
2517 for (i = G.by_depth_in_use; i > 0; --i)
2519 page_entry *p = G.by_depth[i-1];
2520 p->index_by_depth = i-1;
2523 /* And last, we update the depth pointers in G.depth. The first
2524 entry is already 0, and context 0 entries always start at index
2525 0, so there is nothing to update in the first slot. We need a
2526 second slot, only if we have old ptes, and if we do, they start
2527 at index count_new_page_tables. */
2528 if (count_old_page_tables)
2529 push_depth (count_new_page_tables);
2532 void
2533 ggc_pch_read (FILE *f, void *addr)
2535 struct ggc_pch_ondisk d;
2536 unsigned i;
2537 char *offs = (char *) addr;
2538 unsigned long count_old_page_tables;
2539 unsigned long count_new_page_tables;
2541 count_old_page_tables = G.by_depth_in_use;
2543 /* We've just read in a PCH file. So, every object that used to be
2544 allocated is now free. */
2545 clear_marks ();
2546 #ifdef ENABLE_GC_CHECKING
2547 poison_pages ();
2548 #endif
2549 /* Since we free all the allocated objects, the free list becomes
2550 useless. Validate it now, which will also clear it. */
2551 validate_free_objects ();
2553 /* No object read from a PCH file should ever be freed. So, set the
2554 context depth to 1, and set the depth of all the currently-allocated
2555 pages to be 1 too. PCH pages will have depth 0. */
2556 gcc_assert (!G.context_depth);
2557 G.context_depth = 1;
2558 for (i = 0; i < NUM_ORDERS; i++)
2560 page_entry *p;
2561 for (p = G.pages[i]; p != NULL; p = p->next)
2562 p->context_depth = G.context_depth;
2565 /* Allocate the appropriate page-table entries for the pages read from
2566 the PCH file. */
2567 if (fread (&d, sizeof (d), 1, f) != 1)
2568 fatal_error (input_location, "can%'t read PCH file: %m");
2570 for (i = 0; i < NUM_ORDERS; i++)
2572 struct page_entry *entry;
2573 char *pte;
2574 size_t bytes;
2575 size_t num_objs;
2576 size_t j;
2578 if (d.totals[i] == 0)
2579 continue;
2581 bytes = PAGE_ALIGN (d.totals[i] * OBJECT_SIZE (i));
2582 num_objs = bytes / OBJECT_SIZE (i);
2583 entry = XCNEWVAR (struct page_entry, (sizeof (struct page_entry)
2584 - sizeof (long)
2585 + BITMAP_SIZE (num_objs + 1)));
2586 entry->bytes = bytes;
2587 entry->page = offs;
2588 entry->context_depth = 0;
2589 offs += bytes;
2590 entry->num_free_objects = 0;
2591 entry->order = i;
2593 for (j = 0;
2594 j + HOST_BITS_PER_LONG <= num_objs + 1;
2595 j += HOST_BITS_PER_LONG)
2596 entry->in_use_p[j / HOST_BITS_PER_LONG] = -1;
2597 for (; j < num_objs + 1; j++)
2598 entry->in_use_p[j / HOST_BITS_PER_LONG]
2599 |= 1L << (j % HOST_BITS_PER_LONG);
2601 for (pte = entry->page;
2602 pte < entry->page + entry->bytes;
2603 pte += G.pagesize)
2604 set_page_table_entry (pte, entry);
2606 if (G.page_tails[i] != NULL)
2607 G.page_tails[i]->next = entry;
2608 else
2609 G.pages[i] = entry;
2610 G.page_tails[i] = entry;
2612 /* We start off by just adding all the new information to the
2613 end of the varrays, later, we will move the new information
2614 to the front of the varrays, as the PCH page tables are at
2615 context 0. */
2616 push_by_depth (entry, 0);
2619 /* Now, we update the various data structures that speed page table
2620 handling. */
2621 count_new_page_tables = G.by_depth_in_use - count_old_page_tables;
2623 move_ptes_to_front (count_old_page_tables, count_new_page_tables);
2625 /* Update the statistics. */
2626 G.allocated = G.allocated_last_gc = offs - (char *)addr;