struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / cpp / gcc / ggc-page.cc
blobb85597d81ba07f070e5c11197d727ecc925a419c
1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999-2022 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 "backend.h"
24 #include "alias.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "memmodel.h"
28 #include "function.h" // sdcpp
29 // sdcpp #include "tm_p.h"
30 #include "diagnostic-core.h"
31 #include "flags.h"
32 #include "ggc-internal.h"
33 #include "timevar.h"
34 #include "cgraph.h"
35 #include "cfgloop.h"
36 #include "plugin.h"
38 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
39 file open. Prefer either to valloc. */
40 #ifdef HAVE_MMAP_ANON
41 # undef HAVE_MMAP_DEV_ZERO
42 # define USING_MMAP
43 #endif
45 #ifdef HAVE_MMAP_DEV_ZERO
46 # define USING_MMAP
47 #endif
49 #ifndef USING_MMAP
50 #define USING_MALLOC_PAGE_GROUPS
51 #endif
53 #if defined(HAVE_MADVISE) && HAVE_DECL_MADVISE && defined(MADV_DONTNEED) \
54 && defined(USING_MMAP)
55 # define USING_MADVISE
56 #endif
58 /* Strategy:
60 This garbage-collecting allocator allocates objects on one of a set
61 of pages. Each page can allocate objects of a single size only;
62 available sizes are powers of two starting at four bytes. The size
63 of an allocation request is rounded up to the next power of two
64 (`order'), and satisfied from the appropriate page.
66 Each page is recorded in a page-entry, which also maintains an
67 in-use bitmap of object positions on the page. This allows the
68 allocation state of a particular object to be flipped without
69 touching the page itself.
71 Each page-entry also has a context depth, which is used to track
72 pushing and popping of allocation contexts. Only objects allocated
73 in the current (highest-numbered) context may be collected.
75 Page entries are arranged in an array of singly-linked lists. The
76 array is indexed by the allocation size, in bits, of the pages on
77 it; i.e. all pages on a list allocate objects of the same size.
78 Pages are ordered on the list such that all non-full pages precede
79 all full pages, with non-full pages arranged in order of decreasing
80 context depth.
82 Empty pages (of all orders) are kept on a single page cache list,
83 and are considered first when new pages are required; they are
84 deallocated at the start of the next collection if they haven't
85 been recycled by then. */
87 /* Define GGC_DEBUG_LEVEL to print debugging information.
88 0: No debugging output.
89 1: GC statistics only.
90 2: Page-entry allocations/deallocations as well.
91 3: Object allocations as well.
92 4: Object marks as well. */
93 #define GGC_DEBUG_LEVEL (0)
95 /* A two-level tree is used to look up the page-entry for a given
96 pointer. Two chunks of the pointer's bits are extracted to index
97 the first and second levels of the tree, as follows:
99 HOST_PAGE_SIZE_BITS
100 32 | |
101 msb +----------------+----+------+------+ lsb
102 | | |
103 PAGE_L1_BITS |
105 PAGE_L2_BITS
107 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
108 pages are aligned on system page boundaries. The next most
109 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
110 index values in the lookup table, respectively.
112 For 32-bit architectures and the settings below, there are no
113 leftover bits. For architectures with wider pointers, the lookup
114 tree points to a list of pages, which must be scanned to find the
115 correct one. */
117 #define PAGE_L1_BITS (8)
118 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
119 #define PAGE_L1_SIZE ((uintptr_t) 1 << PAGE_L1_BITS)
120 #define PAGE_L2_SIZE ((uintptr_t) 1 << PAGE_L2_BITS)
122 #define LOOKUP_L1(p) \
123 (((uintptr_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
125 #define LOOKUP_L2(p) \
126 (((uintptr_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
128 /* The number of objects per allocation page, for objects on a page of
129 the indicated ORDER. */
130 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
132 /* The number of objects in P. */
133 #define OBJECTS_IN_PAGE(P) ((P)->bytes / OBJECT_SIZE ((P)->order))
135 /* The size of an object on a page of the indicated ORDER. */
136 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
138 /* For speed, we avoid doing a general integer divide to locate the
139 offset in the allocation bitmap, by precalculating numbers M, S
140 such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
141 within the page which is evenly divisible by the object size Z. */
142 #define DIV_MULT(ORDER) inverse_table[ORDER].mult
143 #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
144 #define OFFSET_TO_BIT(OFFSET, ORDER) \
145 (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
147 /* We use this structure to determine the alignment required for
148 allocations. For power-of-two sized allocations, that's not a
149 problem, but it does matter for odd-sized allocations.
150 We do not care about alignment for floating-point types. */
152 struct max_alignment {
153 char c;
154 union {
155 int64_t i;
156 void *p;
157 } u;
160 /* The biggest alignment required. */
162 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
165 /* The number of extra orders, not corresponding to power-of-two sized
166 objects. */
168 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
170 #define RTL_SIZE(NSLOTS) \
171 (RTX_HDR_SIZE + (NSLOTS) * sizeof (rtunion))
173 #define TREE_EXP_SIZE(OPS) \
174 (sizeof (struct tree_exp) + ((OPS) - 1) * sizeof (tree))
176 /* The Ith entry is the maximum size of an object to be stored in the
177 Ith extra order. Adding a new entry to this array is the *only*
178 thing you need to do to add a new special allocation size. */
180 static const size_t extra_order_size_table[] = {
181 /* Extra orders for small non-power-of-two multiples of MAX_ALIGNMENT.
182 There are a lot of structures with these sizes and explicitly
183 listing them risks orders being dropped because they changed size. */
184 MAX_ALIGNMENT * 3,
185 MAX_ALIGNMENT * 5,
186 MAX_ALIGNMENT * 6,
187 MAX_ALIGNMENT * 7,
188 MAX_ALIGNMENT * 9,
189 MAX_ALIGNMENT * 10,
190 MAX_ALIGNMENT * 11,
191 MAX_ALIGNMENT * 12,
192 MAX_ALIGNMENT * 13,
193 MAX_ALIGNMENT * 14,
194 MAX_ALIGNMENT * 15,
195 #if 0 // sdcpp
196 sizeof (struct tree_decl_non_common),
197 sizeof (struct tree_field_decl),
198 sizeof (struct tree_parm_decl),
199 sizeof (struct tree_var_decl),
200 sizeof (struct tree_type_non_common),
201 sizeof (struct function),
202 sizeof (struct basic_block_def),
203 sizeof (struct cgraph_node),
204 sizeof (class loop),
205 #endif // sdcpp
208 /* The total number of orders. */
210 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
212 /* Compute the smallest nonnegative number which when added to X gives
213 a multiple of F. */
215 #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
217 /* Round X to next multiple of the page size */
219 #define PAGE_ALIGN(x) ROUND_UP ((x), G.pagesize)
221 /* The Ith entry is the number of objects on a page or order I. */
223 static unsigned objects_per_page_table[NUM_ORDERS];
225 /* The Ith entry is the size of an object on a page of order I. */
227 static size_t object_size_table[NUM_ORDERS];
229 /* The Ith entry is a pair of numbers (mult, shift) such that
230 ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
231 for all k evenly divisible by OBJECT_SIZE(I). */
233 static struct
235 size_t mult;
236 unsigned int shift;
238 inverse_table[NUM_ORDERS];
240 /* A page_entry records the status of an allocation page. This
241 structure is dynamically sized to fit the bitmap in_use_p. */
242 struct page_entry
244 /* The next page-entry with objects of the same size, or NULL if
245 this is the last page-entry. */
246 struct page_entry *next;
248 /* The previous page-entry with objects of the same size, or NULL if
249 this is the first page-entry. The PREV pointer exists solely to
250 keep the cost of ggc_free manageable. */
251 struct page_entry *prev;
253 /* The number of bytes allocated. (This will always be a multiple
254 of the host system page size.) */
255 size_t bytes;
257 /* The address at which the memory is allocated. */
258 char *page;
260 #ifdef USING_MALLOC_PAGE_GROUPS
261 /* Back pointer to the page group this page came from. */
262 struct page_group *group;
263 #endif
265 /* This is the index in the by_depth varray where this page table
266 can be found. */
267 unsigned long index_by_depth;
269 /* Context depth of this page. */
270 unsigned short context_depth;
272 /* The number of free objects remaining on this page. */
273 unsigned short num_free_objects;
275 /* A likely candidate for the bit position of a free object for the
276 next allocation from this page. */
277 unsigned short next_bit_hint;
279 /* The lg of size of objects allocated from this page. */
280 unsigned char order;
282 /* Discarded page? */
283 bool discarded;
285 /* A bit vector indicating whether or not objects are in use. The
286 Nth bit is one if the Nth object on this page is allocated. This
287 array is dynamically sized. */
288 unsigned long in_use_p[1];
291 #ifdef USING_MALLOC_PAGE_GROUPS
292 /* A page_group describes a large allocation from malloc, from which
293 we parcel out aligned pages. */
294 struct page_group
296 /* A linked list of all extant page groups. */
297 struct page_group *next;
299 /* The address we received from malloc. */
300 char *allocation;
302 /* The size of the block. */
303 size_t alloc_size;
305 /* A bitmask of pages in use. */
306 unsigned int in_use;
308 #endif
310 #if HOST_BITS_PER_PTR <= 32
312 /* On 32-bit hosts, we use a two level page table, as pictured above. */
313 typedef page_entry **page_table[PAGE_L1_SIZE];
315 #else
317 /* On 64-bit hosts, we use the same two level page tables plus a linked
318 list that disambiguates the top 32-bits. There will almost always be
319 exactly one entry in the list. */
320 typedef struct page_table_chain
322 struct page_table_chain *next;
323 size_t high_bits;
324 page_entry **table[PAGE_L1_SIZE];
325 } *page_table;
327 #endif
329 class finalizer
331 public:
332 finalizer (void *addr, void (*f)(void *)) : m_addr (addr), m_function (f) {}
334 void *addr () const { return m_addr; }
336 void call () const { m_function (m_addr); }
338 private:
339 void *m_addr;
340 void (*m_function)(void *);
343 class vec_finalizer
345 public:
346 vec_finalizer (uintptr_t addr, void (*f)(void *), size_t s, size_t n) :
347 m_addr (addr), m_function (f), m_object_size (s), m_n_objects (n) {}
349 void call () const
351 for (size_t i = 0; i < m_n_objects; i++)
352 m_function (reinterpret_cast<void *> (m_addr + (i * m_object_size)));
355 void *addr () const { return reinterpret_cast<void *> (m_addr); }
357 private:
358 uintptr_t m_addr;
359 void (*m_function)(void *);
360 size_t m_object_size;
361 size_t m_n_objects;
364 #ifdef ENABLE_GC_ALWAYS_COLLECT
365 /* List of free objects to be verified as actually free on the
366 next collection. */
367 struct free_object
369 void *object;
370 struct free_object *next;
372 #endif
374 /* The rest of the global variables. */
375 static struct ggc_globals
377 /* The Nth element in this array is a page with objects of size 2^N.
378 If there are any pages with free objects, they will be at the
379 head of the list. NULL if there are no page-entries for this
380 object size. */
381 page_entry *pages[NUM_ORDERS];
383 /* The Nth element in this array is the last page with objects of
384 size 2^N. NULL if there are no page-entries for this object
385 size. */
386 page_entry *page_tails[NUM_ORDERS];
388 /* Lookup table for associating allocation pages with object addresses. */
389 page_table lookup;
391 /* The system's page size. */
392 size_t pagesize;
393 size_t lg_pagesize;
395 /* Bytes currently allocated. */
396 size_t allocated;
398 /* Bytes currently allocated at the end of the last collection. */
399 size_t allocated_last_gc;
401 /* Total amount of memory mapped. */
402 size_t bytes_mapped;
404 /* Bit N set if any allocations have been done at context depth N. */
405 unsigned long context_depth_allocations;
407 /* Bit N set if any collections have been done at context depth N. */
408 unsigned long context_depth_collections;
410 /* The current depth in the context stack. */
411 unsigned short context_depth;
413 /* A file descriptor open to /dev/zero for reading. */
414 #if defined (HAVE_MMAP_DEV_ZERO)
415 int dev_zero_fd;
416 #endif
418 /* A cache of free system pages. */
419 page_entry *free_pages;
421 #ifdef USING_MALLOC_PAGE_GROUPS
422 page_group *page_groups;
423 #endif
425 /* The file descriptor for debugging output. */
426 FILE *debug_file;
428 /* Current number of elements in use in depth below. */
429 unsigned int depth_in_use;
431 /* Maximum number of elements that can be used before resizing. */
432 unsigned int depth_max;
434 /* Each element of this array is an index in by_depth where the given
435 depth starts. This structure is indexed by that given depth we
436 are interested in. */
437 unsigned int *depth;
439 /* Current number of elements in use in by_depth below. */
440 unsigned int by_depth_in_use;
442 /* Maximum number of elements that can be used before resizing. */
443 unsigned int by_depth_max;
445 /* Each element of this array is a pointer to a page_entry, all
446 page_entries can be found in here by increasing depth.
447 index_by_depth in the page_entry is the index into this data
448 structure where that page_entry can be found. This is used to
449 speed up finding all page_entries at a particular depth. */
450 page_entry **by_depth;
452 /* Each element is a pointer to the saved in_use_p bits, if any,
453 zero otherwise. We allocate them all together, to enable a
454 better runtime data access pattern. */
455 unsigned long **save_in_use;
457 /* Finalizers for single objects. The first index is collection_depth. */
458 vec<vec<finalizer> > finalizers;
460 /* Finalizers for vectors of objects. */
461 vec<vec<vec_finalizer> > vec_finalizers;
463 #ifdef ENABLE_GC_ALWAYS_COLLECT
464 /* List of free objects to be verified as actually free on the
465 next collection. */
466 struct free_object *free_object_list;
467 #endif
469 struct
471 /* Total GC-allocated memory. */
472 unsigned long long total_allocated;
473 /* Total overhead for GC-allocated memory. */
474 unsigned long long total_overhead;
476 /* Total allocations and overhead for sizes less than 32, 64 and 128.
477 These sizes are interesting because they are typical cache line
478 sizes. */
480 unsigned long long total_allocated_under32;
481 unsigned long long total_overhead_under32;
483 unsigned long long total_allocated_under64;
484 unsigned long long total_overhead_under64;
486 unsigned long long total_allocated_under128;
487 unsigned long long total_overhead_under128;
489 /* The allocations for each of the allocation orders. */
490 unsigned long long total_allocated_per_order[NUM_ORDERS];
492 /* The overhead for each of the allocation orders. */
493 unsigned long long total_overhead_per_order[NUM_ORDERS];
494 } stats;
495 } G;
497 /* True if a gc is currently taking place. */
499 static bool in_gc = false;
501 /* The size in bytes required to maintain a bitmap for the objects
502 on a page-entry. */
503 #define BITMAP_SIZE(Num_objects) \
504 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof (long))
506 /* Allocate pages in chunks of this size, to throttle calls to memory
507 allocation routines. The first page is used, the rest go onto the
508 free list. This cannot be larger than HOST_BITS_PER_INT for the
509 in_use bitmask for page_group. Hosts that need a different value
510 can override this by defining GGC_QUIRE_SIZE explicitly. */
511 #ifndef GGC_QUIRE_SIZE
512 # ifdef USING_MMAP
513 # define GGC_QUIRE_SIZE 512 /* 2MB for 4K pages */
514 # else
515 # define GGC_QUIRE_SIZE 16
516 # endif
517 #endif
519 /* Initial guess as to how many page table entries we might need. */
520 #define INITIAL_PTE_COUNT 128
522 static page_entry *lookup_page_table_entry (const void *);
523 static void set_page_table_entry (void *, page_entry *);
524 #ifdef USING_MMAP
525 static char *alloc_anon (char *, size_t, bool check);
526 #endif
527 #ifdef USING_MALLOC_PAGE_GROUPS
528 static size_t page_group_index (char *, char *);
529 static void set_page_group_in_use (page_group *, char *);
530 static void clear_page_group_in_use (page_group *, char *);
531 #endif
532 static struct page_entry * alloc_page (unsigned);
533 static void free_page (struct page_entry *);
534 static void clear_marks (void);
535 static void sweep_pages (void);
536 static void ggc_recalculate_in_use_p (page_entry *);
537 static void compute_inverse (unsigned);
538 static inline void adjust_depth (void);
539 static void move_ptes_to_front (int, int);
541 void debug_print_page_list (int);
542 static void push_depth (unsigned int);
543 static void push_by_depth (page_entry *, unsigned long *);
545 /* Push an entry onto G.depth. */
547 inline static void
548 push_depth (unsigned int i)
550 if (G.depth_in_use >= G.depth_max)
552 G.depth_max *= 2;
553 G.depth = XRESIZEVEC (unsigned int, G.depth, G.depth_max);
555 G.depth[G.depth_in_use++] = i;
558 /* Push an entry onto G.by_depth and G.save_in_use. */
560 inline static void
561 push_by_depth (page_entry *p, unsigned long *s)
563 if (G.by_depth_in_use >= G.by_depth_max)
565 G.by_depth_max *= 2;
566 G.by_depth = XRESIZEVEC (page_entry *, G.by_depth, G.by_depth_max);
567 G.save_in_use = XRESIZEVEC (unsigned long *, G.save_in_use,
568 G.by_depth_max);
570 G.by_depth[G.by_depth_in_use] = p;
571 G.save_in_use[G.by_depth_in_use++] = s;
574 #if (GCC_VERSION < 3001)
575 #define prefetch(X) ((void) X)
576 #else
577 #define prefetch(X) __builtin_prefetch (X)
578 #endif
580 #define save_in_use_p_i(__i) \
581 (G.save_in_use[__i])
582 #define save_in_use_p(__p) \
583 (save_in_use_p_i (__p->index_by_depth))
585 /* Traverse the page table and find the entry for a page.
586 If the object wasn't allocated in GC return NULL. */
588 static inline page_entry *
589 safe_lookup_page_table_entry (const void *p)
591 page_entry ***base;
592 size_t L1, L2;
594 #if HOST_BITS_PER_PTR <= 32
595 base = &G.lookup[0];
596 #else
597 page_table table = G.lookup;
598 uintptr_t high_bits = (uintptr_t) p & ~ (uintptr_t) 0xffffffff;
599 while (1)
601 if (table == NULL)
602 return NULL;
603 if (table->high_bits == high_bits)
604 break;
605 table = table->next;
607 base = &table->table[0];
608 #endif
610 /* Extract the level 1 and 2 indices. */
611 L1 = LOOKUP_L1 (p);
612 L2 = LOOKUP_L2 (p);
613 if (! base[L1])
614 return NULL;
616 return base[L1][L2];
619 /* Traverse the page table and find the entry for a page.
620 Die (probably) if the object wasn't allocated via GC. */
622 static inline page_entry *
623 lookup_page_table_entry (const void *p)
625 page_entry ***base;
626 size_t L1, L2;
628 #if HOST_BITS_PER_PTR <= 32
629 base = &G.lookup[0];
630 #else
631 page_table table = G.lookup;
632 uintptr_t high_bits = (uintptr_t) p & ~ (uintptr_t) 0xffffffff;
633 while (table->high_bits != high_bits)
634 table = table->next;
635 base = &table->table[0];
636 #endif
638 /* Extract the level 1 and 2 indices. */
639 L1 = LOOKUP_L1 (p);
640 L2 = LOOKUP_L2 (p);
642 return base[L1][L2];
645 /* Set the page table entry for a page. */
647 static void
648 set_page_table_entry (void *p, page_entry *entry)
650 page_entry ***base;
651 size_t L1, L2;
653 #if HOST_BITS_PER_PTR <= 32
654 base = &G.lookup[0];
655 #else
656 page_table table;
657 uintptr_t high_bits = (uintptr_t) p & ~ (uintptr_t) 0xffffffff;
658 for (table = G.lookup; table; table = table->next)
659 if (table->high_bits == high_bits)
660 goto found;
662 /* Not found -- allocate a new table. */
663 table = XCNEW (struct page_table_chain);
664 table->next = G.lookup;
665 table->high_bits = high_bits;
666 G.lookup = table;
667 found:
668 base = &table->table[0];
669 #endif
671 /* Extract the level 1 and 2 indices. */
672 L1 = LOOKUP_L1 (p);
673 L2 = LOOKUP_L2 (p);
675 if (base[L1] == NULL)
676 base[L1] = XCNEWVEC (page_entry *, PAGE_L2_SIZE);
678 base[L1][L2] = entry;
681 /* Prints the page-entry for object size ORDER, for debugging. */
683 DEBUG_FUNCTION void
684 debug_print_page_list (int order)
686 page_entry *p;
687 printf ("Head=%p, Tail=%p:\n", (void *) G.pages[order],
688 (void *) G.page_tails[order]);
689 p = G.pages[order];
690 while (p != NULL)
692 printf ("%p(%1d|%3d) -> ", (void *) p, p->context_depth,
693 p->num_free_objects);
694 p = p->next;
696 printf ("NULL\n");
697 fflush (stdout);
700 #ifdef USING_MMAP
701 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
702 (if non-null). The ifdef structure here is intended to cause a
703 compile error unless exactly one of the HAVE_* is defined. */
705 static inline char *
706 alloc_anon (char *pref ATTRIBUTE_UNUSED, size_t size, bool check)
708 #ifdef HAVE_MMAP_ANON
709 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
710 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
711 #endif
712 #ifdef HAVE_MMAP_DEV_ZERO
713 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
714 MAP_PRIVATE, G.dev_zero_fd, 0);
715 #endif
717 if (page == (char *) MAP_FAILED)
719 if (!check)
720 return NULL;
721 perror ("virtual memory exhausted");
722 exit (FATAL_EXIT_CODE);
725 /* Remember that we allocated this memory. */
726 G.bytes_mapped += size;
728 /* Pretend we don't have access to the allocated pages. We'll enable
729 access to smaller pieces of the area in ggc_internal_alloc. Discard the
730 handle to avoid handle leak. */
731 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (page, size));
733 return page;
735 #endif
736 #ifdef USING_MALLOC_PAGE_GROUPS
737 /* Compute the index for this page into the page group. */
739 static inline size_t
740 page_group_index (char *allocation, char *page)
742 return (size_t) (page - allocation) >> G.lg_pagesize;
745 /* Set and clear the in_use bit for this page in the page group. */
747 static inline void
748 set_page_group_in_use (page_group *group, char *page)
750 group->in_use |= 1 << page_group_index (group->allocation, page);
753 static inline void
754 clear_page_group_in_use (page_group *group, char *page)
756 group->in_use &= ~(1 << page_group_index (group->allocation, page));
758 #endif
760 /* Allocate a new page for allocating objects of size 2^ORDER,
761 and return an entry for it. The entry is not added to the
762 appropriate page_table list. */
764 static inline struct page_entry *
765 alloc_page (unsigned order)
767 struct page_entry *entry, *p, **pp;
768 char *page;
769 size_t num_objects;
770 size_t bitmap_size;
771 size_t page_entry_size;
772 size_t entry_size;
773 #ifdef USING_MALLOC_PAGE_GROUPS
774 page_group *group;
775 #endif
777 num_objects = OBJECTS_PER_PAGE (order);
778 bitmap_size = BITMAP_SIZE (num_objects + 1);
779 page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
780 entry_size = num_objects * OBJECT_SIZE (order);
781 if (entry_size < G.pagesize)
782 entry_size = G.pagesize;
783 entry_size = PAGE_ALIGN (entry_size);
785 entry = NULL;
786 page = NULL;
788 /* Check the list of free pages for one we can use. */
789 for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
790 if (p->bytes == entry_size)
791 break;
793 if (p != NULL)
795 if (p->discarded)
796 G.bytes_mapped += p->bytes;
797 p->discarded = false;
799 /* Recycle the allocated memory from this page ... */
800 *pp = p->next;
801 page = p->page;
803 #ifdef USING_MALLOC_PAGE_GROUPS
804 group = p->group;
805 #endif
807 /* ... and, if possible, the page entry itself. */
808 if (p->order == order)
810 entry = p;
811 memset (entry, 0, page_entry_size);
813 else
814 free (p);
816 #ifdef USING_MMAP
817 else if (entry_size == G.pagesize)
819 /* We want just one page. Allocate a bunch of them and put the
820 extras on the freelist. (Can only do this optimization with
821 mmap for backing store.) */
822 struct page_entry *e, *f = G.free_pages;
823 int i, entries = GGC_QUIRE_SIZE;
825 page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE, false);
826 if (page == NULL)
828 page = alloc_anon (NULL, G.pagesize, true);
829 entries = 1;
832 /* This loop counts down so that the chain will be in ascending
833 memory order. */
834 for (i = entries - 1; i >= 1; i--)
836 e = XCNEWVAR (struct page_entry, page_entry_size);
837 e->order = order;
838 e->bytes = G.pagesize;
839 e->page = page + (i << G.lg_pagesize);
840 e->next = f;
841 f = e;
844 G.free_pages = f;
846 else
847 page = alloc_anon (NULL, entry_size, true);
848 #endif
849 #ifdef USING_MALLOC_PAGE_GROUPS
850 else
852 /* Allocate a large block of memory and serve out the aligned
853 pages therein. This results in much less memory wastage
854 than the traditional implementation of valloc. */
856 char *allocation, *a, *enda;
857 size_t alloc_size, head_slop, tail_slop;
858 int multiple_pages = (entry_size == G.pagesize);
860 if (multiple_pages)
861 alloc_size = GGC_QUIRE_SIZE * G.pagesize;
862 else
863 alloc_size = entry_size + G.pagesize - 1;
864 allocation = XNEWVEC (char, alloc_size);
866 page = (char *) (((uintptr_t) allocation + G.pagesize - 1) & -G.pagesize);
867 head_slop = page - allocation;
868 if (multiple_pages)
869 tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
870 else
871 tail_slop = alloc_size - entry_size - head_slop;
872 enda = allocation + alloc_size - tail_slop;
874 /* We allocated N pages, which are likely not aligned, leaving
875 us with N-1 usable pages. We plan to place the page_group
876 structure somewhere in the slop. */
877 if (head_slop >= sizeof (page_group))
878 group = (page_group *)page - 1;
879 else
881 /* We magically got an aligned allocation. Too bad, we have
882 to waste a page anyway. */
883 if (tail_slop == 0)
885 enda -= G.pagesize;
886 tail_slop += G.pagesize;
888 gcc_assert (tail_slop >= sizeof (page_group));
889 group = (page_group *)enda;
890 tail_slop -= sizeof (page_group);
893 /* Remember that we allocated this memory. */
894 group->next = G.page_groups;
895 group->allocation = allocation;
896 group->alloc_size = alloc_size;
897 group->in_use = 0;
898 G.page_groups = group;
899 G.bytes_mapped += alloc_size;
901 /* If we allocated multiple pages, put the rest on the free list. */
902 if (multiple_pages)
904 struct page_entry *e, *f = G.free_pages;
905 for (a = enda - G.pagesize; a != page; a -= G.pagesize)
907 e = XCNEWVAR (struct page_entry, page_entry_size);
908 e->order = order;
909 e->bytes = G.pagesize;
910 e->page = a;
911 e->group = group;
912 e->next = f;
913 f = e;
915 G.free_pages = f;
918 #endif
920 if (entry == NULL)
921 entry = XCNEWVAR (struct page_entry, page_entry_size);
923 entry->bytes = entry_size;
924 entry->page = page;
925 entry->context_depth = G.context_depth;
926 entry->order = order;
927 entry->num_free_objects = num_objects;
928 entry->next_bit_hint = 1;
930 G.context_depth_allocations |= (unsigned long)1 << G.context_depth;
932 #ifdef USING_MALLOC_PAGE_GROUPS
933 entry->group = group;
934 set_page_group_in_use (group, page);
935 #endif
937 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
938 increment the hint. */
939 entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
940 = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
942 set_page_table_entry (page, entry);
944 if (GGC_DEBUG_LEVEL >= 2)
945 fprintf (G.debug_file,
946 "Allocating page at %p, object size=%lu, data %p-%p\n",
947 (void *) entry, (unsigned long) OBJECT_SIZE (order),
948 (void *) page, (void *) (page + entry_size - 1));
950 return entry;
953 /* Adjust the size of G.depth so that no index greater than the one
954 used by the top of the G.by_depth is used. */
956 static inline void
957 adjust_depth (void)
959 page_entry *top;
961 if (G.by_depth_in_use)
963 top = G.by_depth[G.by_depth_in_use-1];
965 /* Peel back indices in depth that index into by_depth, so that
966 as new elements are added to by_depth, we note the indices
967 of those elements, if they are for new context depths. */
968 while (G.depth_in_use > (size_t)top->context_depth+1)
969 --G.depth_in_use;
973 /* For a page that is no longer needed, put it on the free page list. */
975 static void
976 free_page (page_entry *entry)
978 if (GGC_DEBUG_LEVEL >= 2)
979 fprintf (G.debug_file,
980 "Deallocating page at %p, data %p-%p\n", (void *) entry,
981 (void *) entry->page, (void *) (entry->page + entry->bytes - 1));
983 /* Mark the page as inaccessible. Discard the handle to avoid handle
984 leak. */
985 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (entry->page, entry->bytes));
987 set_page_table_entry (entry->page, NULL);
989 #ifdef USING_MALLOC_PAGE_GROUPS
990 clear_page_group_in_use (entry->group, entry->page);
991 #endif
993 if (G.by_depth_in_use > 1)
995 page_entry *top = G.by_depth[G.by_depth_in_use-1];
996 int i = entry->index_by_depth;
998 /* We cannot free a page from a context deeper than the current
999 one. */
1000 gcc_assert (entry->context_depth == top->context_depth);
1002 /* Put top element into freed slot. */
1003 G.by_depth[i] = top;
1004 G.save_in_use[i] = G.save_in_use[G.by_depth_in_use-1];
1005 top->index_by_depth = i;
1007 --G.by_depth_in_use;
1009 adjust_depth ();
1011 entry->next = G.free_pages;
1012 G.free_pages = entry;
1015 /* Release the free page cache to the system. */
1017 static void
1018 release_pages (void)
1020 size_t n1 = 0;
1021 size_t n2 = 0;
1022 #ifdef USING_MADVISE
1023 page_entry *p, *start_p;
1024 char *start;
1025 size_t len;
1026 size_t mapped_len;
1027 page_entry *next, *prev, *newprev;
1028 size_t free_unit = (GGC_QUIRE_SIZE/2) * G.pagesize;
1030 /* First free larger continuous areas to the OS.
1031 This allows other allocators to grab these areas if needed.
1032 This is only done on larger chunks to avoid fragmentation.
1033 This does not always work because the free_pages list is only
1034 approximately sorted. */
1036 p = G.free_pages;
1037 prev = NULL;
1038 while (p)
1040 start = p->page;
1041 start_p = p;
1042 len = 0;
1043 mapped_len = 0;
1044 newprev = prev;
1045 while (p && p->page == start + len)
1047 len += p->bytes;
1048 if (!p->discarded)
1049 mapped_len += p->bytes;
1050 newprev = p;
1051 p = p->next;
1053 if (len >= free_unit)
1055 while (start_p != p)
1057 next = start_p->next;
1058 free (start_p);
1059 start_p = next;
1061 munmap (start, len);
1062 if (prev)
1063 prev->next = p;
1064 else
1065 G.free_pages = p;
1066 G.bytes_mapped -= mapped_len;
1067 n1 += len;
1068 continue;
1070 prev = newprev;
1073 /* Now give back the fragmented pages to the OS, but keep the address
1074 space to reuse it next time. */
1076 for (p = G.free_pages; p; )
1078 if (p->discarded)
1080 p = p->next;
1081 continue;
1083 start = p->page;
1084 len = p->bytes;
1085 start_p = p;
1086 p = p->next;
1087 while (p && p->page == start + len)
1089 len += p->bytes;
1090 p = p->next;
1092 /* Give the page back to the kernel, but don't free the mapping.
1093 This avoids fragmentation in the virtual memory map of the
1094 process. Next time we can reuse it by just touching it. */
1095 madvise (start, len, MADV_DONTNEED);
1096 /* Don't count those pages as mapped to not touch the garbage collector
1097 unnecessarily. */
1098 G.bytes_mapped -= len;
1099 n2 += len;
1100 while (start_p != p)
1102 start_p->discarded = true;
1103 start_p = start_p->next;
1106 #endif
1107 #if defined(USING_MMAP) && !defined(USING_MADVISE)
1108 page_entry *p, *next;
1109 char *start;
1110 size_t len;
1112 /* Gather up adjacent pages so they are unmapped together. */
1113 p = G.free_pages;
1115 while (p)
1117 start = p->page;
1118 next = p->next;
1119 len = p->bytes;
1120 free (p);
1121 p = next;
1123 while (p && p->page == start + len)
1125 next = p->next;
1126 len += p->bytes;
1127 free (p);
1128 p = next;
1131 munmap (start, len);
1132 n1 += len;
1133 G.bytes_mapped -= len;
1136 G.free_pages = NULL;
1137 #endif
1138 #ifdef USING_MALLOC_PAGE_GROUPS
1139 page_entry **pp, *p;
1140 page_group **gp, *g;
1142 /* Remove all pages from free page groups from the list. */
1143 pp = &G.free_pages;
1144 while ((p = *pp) != NULL)
1145 if (p->group->in_use == 0)
1147 *pp = p->next;
1148 free (p);
1150 else
1151 pp = &p->next;
1153 /* Remove all free page groups, and release the storage. */
1154 gp = &G.page_groups;
1155 while ((g = *gp) != NULL)
1156 if (g->in_use == 0)
1158 *gp = g->next;
1159 G.bytes_mapped -= g->alloc_size;
1160 n1 += g->alloc_size;
1161 free (g->allocation);
1163 else
1164 gp = &g->next;
1165 #endif
1166 if (!quiet_flag && (n1 || n2))
1168 fprintf (stderr, " {GC");
1169 if (n1)
1170 fprintf (stderr, " released " PRsa (0), SIZE_AMOUNT (n1));
1171 if (n2)
1172 fprintf (stderr, " madv_dontneed " PRsa (0), SIZE_AMOUNT (n2));
1173 fprintf (stderr, "}");
1177 /* This table provides a fast way to determine ceil(log_2(size)) for
1178 allocation requests. The minimum allocation size is eight bytes. */
1179 #define NUM_SIZE_LOOKUP 512
1180 static unsigned char size_lookup[NUM_SIZE_LOOKUP] =
1182 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
1183 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1184 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1185 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1186 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1187 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1188 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1189 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1190 7, 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, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1194 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1195 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1196 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1197 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1198 8, 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,
1209 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1210 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1211 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1212 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1213 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9
1216 /* For a given size of memory requested for allocation, return the
1217 actual size that is going to be allocated, as well as the size
1218 order. */
1220 static void
1221 ggc_round_alloc_size_1 (size_t requested_size,
1222 size_t *size_order,
1223 size_t *alloced_size)
1225 size_t order, object_size;
1227 if (requested_size < NUM_SIZE_LOOKUP)
1229 order = size_lookup[requested_size];
1230 object_size = OBJECT_SIZE (order);
1232 else
1234 order = 10;
1235 while (requested_size > (object_size = OBJECT_SIZE (order)))
1236 order++;
1239 if (size_order)
1240 *size_order = order;
1241 if (alloced_size)
1242 *alloced_size = object_size;
1245 /* For a given size of memory requested for allocation, return the
1246 actual size that is going to be allocated. */
1248 size_t
1249 ggc_round_alloc_size (size_t requested_size)
1251 size_t size = 0;
1253 ggc_round_alloc_size_1 (requested_size, NULL, &size);
1254 return size;
1257 /* Push a finalizer onto the appropriate vec. */
1259 static void
1260 add_finalizer (void *result, void (*f)(void *), size_t s, size_t n)
1262 if (f == NULL)
1263 /* No finalizer. */;
1264 else if (n == 1)
1266 finalizer fin (result, f);
1267 G.finalizers[G.context_depth].safe_push (fin);
1269 else
1271 vec_finalizer fin (reinterpret_cast<uintptr_t> (result), f, s, n);
1272 G.vec_finalizers[G.context_depth].safe_push (fin);
1276 /* Allocate a chunk of memory of SIZE bytes. Its contents are undefined. */
1278 void *
1279 ggc_internal_alloc (size_t size, void (*f)(void *), size_t s, size_t n
1280 MEM_STAT_DECL)
1282 size_t order, word, bit, object_offset, object_size;
1283 struct page_entry *entry;
1284 void *result;
1286 ggc_round_alloc_size_1 (size, &order, &object_size);
1288 /* If there are non-full pages for this size allocation, they are at
1289 the head of the list. */
1290 entry = G.pages[order];
1292 /* If there is no page for this object size, or all pages in this
1293 context are full, allocate a new page. */
1294 if (entry == NULL || entry->num_free_objects == 0)
1296 struct page_entry *new_entry;
1297 new_entry = alloc_page (order);
1299 new_entry->index_by_depth = G.by_depth_in_use;
1300 push_by_depth (new_entry, 0);
1302 /* We can skip context depths, if we do, make sure we go all the
1303 way to the new depth. */
1304 while (new_entry->context_depth >= G.depth_in_use)
1305 push_depth (G.by_depth_in_use-1);
1307 /* If this is the only entry, it's also the tail. If it is not
1308 the only entry, then we must update the PREV pointer of the
1309 ENTRY (G.pages[order]) to point to our new page entry. */
1310 if (entry == NULL)
1311 G.page_tails[order] = new_entry;
1312 else
1313 entry->prev = new_entry;
1315 /* Put new pages at the head of the page list. By definition the
1316 entry at the head of the list always has a NULL pointer. */
1317 new_entry->next = entry;
1318 new_entry->prev = NULL;
1319 entry = new_entry;
1320 G.pages[order] = new_entry;
1322 /* For a new page, we know the word and bit positions (in the
1323 in_use bitmap) of the first available object -- they're zero. */
1324 new_entry->next_bit_hint = 1;
1325 word = 0;
1326 bit = 0;
1327 object_offset = 0;
1329 else
1331 /* First try to use the hint left from the previous allocation
1332 to locate a clear bit in the in-use bitmap. We've made sure
1333 that the one-past-the-end bit is always set, so if the hint
1334 has run over, this test will fail. */
1335 unsigned hint = entry->next_bit_hint;
1336 word = hint / HOST_BITS_PER_LONG;
1337 bit = hint % HOST_BITS_PER_LONG;
1339 /* If the hint didn't work, scan the bitmap from the beginning. */
1340 if ((entry->in_use_p[word] >> bit) & 1)
1342 word = bit = 0;
1343 while (~entry->in_use_p[word] == 0)
1344 ++word;
1346 #if GCC_VERSION >= 3004
1347 bit = __builtin_ctzl (~entry->in_use_p[word]);
1348 #else
1349 while ((entry->in_use_p[word] >> bit) & 1)
1350 ++bit;
1351 #endif
1353 hint = word * HOST_BITS_PER_LONG + bit;
1356 /* Next time, try the next bit. */
1357 entry->next_bit_hint = hint + 1;
1359 object_offset = hint * object_size;
1362 /* Set the in-use bit. */
1363 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
1365 /* Keep a running total of the number of free objects. If this page
1366 fills up, we may have to move it to the end of the list if the
1367 next page isn't full. If the next page is full, all subsequent
1368 pages are full, so there's no need to move it. */
1369 if (--entry->num_free_objects == 0
1370 && entry->next != NULL
1371 && entry->next->num_free_objects > 0)
1373 /* We have a new head for the list. */
1374 G.pages[order] = entry->next;
1376 /* We are moving ENTRY to the end of the page table list.
1377 The new page at the head of the list will have NULL in
1378 its PREV field and ENTRY will have NULL in its NEXT field. */
1379 entry->next->prev = NULL;
1380 entry->next = NULL;
1382 /* Append ENTRY to the tail of the list. */
1383 entry->prev = G.page_tails[order];
1384 G.page_tails[order]->next = entry;
1385 G.page_tails[order] = entry;
1388 /* Calculate the object's address. */
1389 result = entry->page + object_offset;
1390 if (GATHER_STATISTICS)
1391 ggc_record_overhead (OBJECT_SIZE (order), OBJECT_SIZE (order) - size,
1392 result FINAL_PASS_MEM_STAT);
1394 #ifdef ENABLE_GC_CHECKING
1395 /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
1396 exact same semantics in presence of memory bugs, regardless of
1397 ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
1398 handle to avoid handle leak. */
1399 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, object_size));
1401 /* `Poison' the entire allocated object, including any padding at
1402 the end. */
1403 memset (result, 0xaf, object_size);
1405 /* Make the bytes after the end of the object unaccessible. Discard the
1406 handle to avoid handle leak. */
1407 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) result + size,
1408 object_size - size));
1409 #endif
1411 /* Tell Valgrind that the memory is there, but its content isn't
1412 defined. The bytes at the end of the object are still marked
1413 unaccessible. */
1414 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, size));
1416 /* Keep track of how many bytes are being allocated. This
1417 information is used in deciding when to collect. */
1418 G.allocated += object_size;
1420 /* For timevar statistics. */
1421 timevar_ggc_mem_total += object_size;
1423 if (f)
1424 add_finalizer (result, f, s, n);
1426 if (GATHER_STATISTICS)
1428 size_t overhead = object_size - size;
1430 G.stats.total_overhead += overhead;
1431 G.stats.total_allocated += object_size;
1432 G.stats.total_overhead_per_order[order] += overhead;
1433 G.stats.total_allocated_per_order[order] += object_size;
1435 if (size <= 32)
1437 G.stats.total_overhead_under32 += overhead;
1438 G.stats.total_allocated_under32 += object_size;
1440 if (size <= 64)
1442 G.stats.total_overhead_under64 += overhead;
1443 G.stats.total_allocated_under64 += object_size;
1445 if (size <= 128)
1447 G.stats.total_overhead_under128 += overhead;
1448 G.stats.total_allocated_under128 += object_size;
1452 if (GGC_DEBUG_LEVEL >= 3)
1453 fprintf (G.debug_file,
1454 "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
1455 (unsigned long) size, (unsigned long) object_size, result,
1456 (void *) entry);
1458 return result;
1461 /* Mark function for strings. */
1463 #if 0 // sdcpp
1464 void
1465 gt_ggc_m_S (const void *p)
1467 page_entry *entry;
1468 unsigned bit, word;
1469 unsigned long mask;
1470 unsigned long offset;
1472 if (!p)
1473 return;
1475 /* Look up the page on which the object is alloced. If it was not
1476 GC allocated, gracefully bail out. */
1477 entry = safe_lookup_page_table_entry (p);
1478 if (!entry)
1479 return;
1481 /* Calculate the index of the object on the page; this is its bit
1482 position in the in_use_p bitmap. Note that because a char* might
1483 point to the middle of an object, we need special code here to
1484 make sure P points to the start of an object. */
1485 offset = ((const char *) p - entry->page) % object_size_table[entry->order];
1486 if (offset)
1488 /* Here we've seen a char* which does not point to the beginning
1489 of an allocated object. We assume it points to the middle of
1490 a STRING_CST. */
1491 gcc_assert (offset == offsetof (struct tree_string, str));
1492 p = ((const char *) p) - offset;
1493 gt_ggc_mx_lang_tree_node (CONST_CAST (void *, p));
1494 return;
1497 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1498 word = bit / HOST_BITS_PER_LONG;
1499 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1501 /* If the bit was previously set, skip it. */
1502 if (entry->in_use_p[word] & mask)
1503 return;
1505 /* Otherwise set it, and decrement the free object count. */
1506 entry->in_use_p[word] |= mask;
1507 entry->num_free_objects -= 1;
1509 if (GGC_DEBUG_LEVEL >= 4)
1510 fprintf (G.debug_file, "Marking %p\n", p);
1512 return;
1514 #endif // sdcpp
1517 /* User-callable entry points for marking string X. */
1519 void
1520 gt_ggc_mx (const char *& x)
1522 gt_ggc_m_S (x);
1525 void
1526 gt_ggc_mx (char *& x)
1528 gt_ggc_m_S (x);
1531 void
1532 gt_ggc_mx (unsigned char *& x)
1534 gt_ggc_m_S (x);
1537 void
1538 gt_ggc_mx (unsigned char& x ATTRIBUTE_UNUSED)
1542 /* If P is not marked, marks it and return false. Otherwise return true.
1543 P must have been allocated by the GC allocator; it mustn't point to
1544 static objects, stack variables, or memory allocated with malloc. */
1547 ggc_set_mark (const void *p)
1549 page_entry *entry;
1550 unsigned bit, word;
1551 unsigned long mask;
1553 /* Look up the page on which the object is alloced. If the object
1554 wasn't allocated by the collector, we'll probably die. */
1555 entry = lookup_page_table_entry (p);
1556 gcc_assert (entry);
1558 /* Calculate the index of the object on the page; this is its bit
1559 position in the in_use_p bitmap. */
1560 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1561 word = bit / HOST_BITS_PER_LONG;
1562 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1564 /* If the bit was previously set, skip it. */
1565 if (entry->in_use_p[word] & mask)
1566 return 1;
1568 /* Otherwise set it, and decrement the free object count. */
1569 entry->in_use_p[word] |= mask;
1570 entry->num_free_objects -= 1;
1572 if (GGC_DEBUG_LEVEL >= 4)
1573 fprintf (G.debug_file, "Marking %p\n", p);
1575 return 0;
1578 /* Return 1 if P has been marked, zero otherwise.
1579 P must have been allocated by the GC allocator; it mustn't point to
1580 static objects, stack variables, or memory allocated with malloc. */
1583 ggc_marked_p (const void *p)
1585 page_entry *entry;
1586 unsigned bit, word;
1587 unsigned long mask;
1589 /* Look up the page on which the object is alloced. If the object
1590 wasn't allocated by the collector, we'll probably die. */
1591 entry = lookup_page_table_entry (p);
1592 gcc_assert (entry);
1594 /* Calculate the index of the object on the page; this is its bit
1595 position in the in_use_p bitmap. */
1596 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1597 word = bit / HOST_BITS_PER_LONG;
1598 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1600 return (entry->in_use_p[word] & mask) != 0;
1603 /* Return the size of the gc-able object P. */
1605 size_t
1606 ggc_get_size (const void *p)
1608 page_entry *pe = lookup_page_table_entry (p);
1609 return OBJECT_SIZE (pe->order);
1612 /* Release the memory for object P. */
1614 void
1615 ggc_free (void *p)
1617 if (in_gc)
1618 return;
1620 page_entry *pe = lookup_page_table_entry (p);
1621 size_t order = pe->order;
1622 size_t size = OBJECT_SIZE (order);
1624 if (GATHER_STATISTICS)
1625 ggc_free_overhead (p);
1627 if (GGC_DEBUG_LEVEL >= 3)
1628 fprintf (G.debug_file,
1629 "Freeing object, actual size=%lu, at %p on %p\n",
1630 (unsigned long) size, p, (void *) pe);
1632 #ifdef ENABLE_GC_CHECKING
1633 /* Poison the data, to indicate the data is garbage. */
1634 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
1635 memset (p, 0xa5, size);
1636 #endif
1637 /* Let valgrind know the object is free. */
1638 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
1640 #ifdef ENABLE_GC_ALWAYS_COLLECT
1641 /* In the completely-anal-checking mode, we do *not* immediately free
1642 the data, but instead verify that the data is *actually* not
1643 reachable the next time we collect. */
1645 struct free_object *fo = XNEW (struct free_object);
1646 fo->object = p;
1647 fo->next = G.free_object_list;
1648 G.free_object_list = fo;
1650 #else
1652 unsigned int bit_offset, word, bit;
1654 G.allocated -= size;
1656 /* Mark the object not-in-use. */
1657 bit_offset = OFFSET_TO_BIT (((const char *) p) - pe->page, order);
1658 word = bit_offset / HOST_BITS_PER_LONG;
1659 bit = bit_offset % HOST_BITS_PER_LONG;
1660 pe->in_use_p[word] &= ~(1UL << bit);
1662 if (pe->num_free_objects++ == 0)
1664 page_entry *p, *q;
1666 /* If the page is completely full, then it's supposed to
1667 be after all pages that aren't. Since we've freed one
1668 object from a page that was full, we need to move the
1669 page to the head of the list.
1671 PE is the node we want to move. Q is the previous node
1672 and P is the next node in the list. */
1673 q = pe->prev;
1674 if (q && q->num_free_objects == 0)
1676 p = pe->next;
1678 q->next = p;
1680 /* If PE was at the end of the list, then Q becomes the
1681 new end of the list. If PE was not the end of the
1682 list, then we need to update the PREV field for P. */
1683 if (!p)
1684 G.page_tails[order] = q;
1685 else
1686 p->prev = q;
1688 /* Move PE to the head of the list. */
1689 pe->next = G.pages[order];
1690 pe->prev = NULL;
1691 G.pages[order]->prev = pe;
1692 G.pages[order] = pe;
1695 /* Reset the hint bit to point to the only free object. */
1696 pe->next_bit_hint = bit_offset;
1699 #endif
1702 /* Subroutine of init_ggc which computes the pair of numbers used to
1703 perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1705 This algorithm is taken from Granlund and Montgomery's paper
1706 "Division by Invariant Integers using Multiplication"
1707 (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1708 constants). */
1710 static void
1711 compute_inverse (unsigned order)
1713 size_t size, inv;
1714 unsigned int e;
1716 size = OBJECT_SIZE (order);
1717 e = 0;
1718 while (size % 2 == 0)
1720 e++;
1721 size >>= 1;
1724 inv = size;
1725 while (inv * size != 1)
1726 inv = inv * (2 - inv*size);
1728 DIV_MULT (order) = inv;
1729 DIV_SHIFT (order) = e;
1732 /* Initialize the ggc-mmap allocator. */
1733 void
1734 init_ggc (void)
1736 static bool init_p = false;
1737 unsigned order;
1739 if (init_p)
1740 return;
1741 init_p = true;
1743 G.pagesize = getpagesize ();
1744 G.lg_pagesize = exact_log2 (G.pagesize);
1746 #ifdef HAVE_MMAP_DEV_ZERO
1747 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1748 if (G.dev_zero_fd == -1)
1749 internal_error ("open /dev/zero: %m");
1750 #endif
1752 #if 0
1753 G.debug_file = fopen ("ggc-mmap.debug", "w");
1754 #else
1755 G.debug_file = stdout;
1756 #endif
1758 #ifdef USING_MMAP
1759 /* StunOS has an amazing off-by-one error for the first mmap allocation
1760 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1761 believe, is an unaligned page allocation, which would cause us to
1762 hork badly if we tried to use it. */
1764 char *p = alloc_anon (NULL, G.pagesize, true);
1765 struct page_entry *e;
1766 if ((uintptr_t)p & (G.pagesize - 1))
1768 /* How losing. Discard this one and try another. If we still
1769 can't get something useful, give up. */
1771 p = alloc_anon (NULL, G.pagesize, true);
1772 gcc_assert (!((uintptr_t)p & (G.pagesize - 1)));
1775 /* We have a good page, might as well hold onto it... */
1776 e = XCNEW (struct page_entry);
1777 e->bytes = G.pagesize;
1778 e->page = p;
1779 e->next = G.free_pages;
1780 G.free_pages = e;
1782 #endif
1784 /* Initialize the object size table. */
1785 for (order = 0; order < HOST_BITS_PER_PTR; ++order)
1786 object_size_table[order] = (size_t) 1 << order;
1787 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1789 size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
1791 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1792 so that we're sure of getting aligned memory. */
1793 s = ROUND_UP (s, MAX_ALIGNMENT);
1794 object_size_table[order] = s;
1797 /* Initialize the objects-per-page and inverse tables. */
1798 for (order = 0; order < NUM_ORDERS; ++order)
1800 objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
1801 if (objects_per_page_table[order] == 0)
1802 objects_per_page_table[order] = 1;
1803 compute_inverse (order);
1806 /* Reset the size_lookup array to put appropriately sized objects in
1807 the special orders. All objects bigger than the previous power
1808 of two, but no greater than the special size, should go in the
1809 new order. */
1810 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1812 int o;
1813 int i;
1815 i = OBJECT_SIZE (order);
1816 if (i >= NUM_SIZE_LOOKUP)
1817 continue;
1819 for (o = size_lookup[i]; o == size_lookup [i]; --i)
1820 size_lookup[i] = order;
1823 G.depth_in_use = 0;
1824 G.depth_max = 10;
1825 G.depth = XNEWVEC (unsigned int, G.depth_max);
1827 G.by_depth_in_use = 0;
1828 G.by_depth_max = INITIAL_PTE_COUNT;
1829 G.by_depth = XNEWVEC (page_entry *, G.by_depth_max);
1830 G.save_in_use = XNEWVEC (unsigned long *, G.by_depth_max);
1832 /* Allocate space for the depth 0 finalizers. */
1833 G.finalizers.safe_push (vNULL);
1834 G.vec_finalizers.safe_push (vNULL);
1835 gcc_assert (G.finalizers.length() == 1);
1838 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1839 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1841 static void
1842 ggc_recalculate_in_use_p (page_entry *p)
1844 unsigned int i;
1845 size_t num_objects;
1847 /* Because the past-the-end bit in in_use_p is always set, we
1848 pretend there is one additional object. */
1849 num_objects = OBJECTS_IN_PAGE (p) + 1;
1851 /* Reset the free object count. */
1852 p->num_free_objects = num_objects;
1854 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1855 for (i = 0;
1856 i < CEIL (BITMAP_SIZE (num_objects),
1857 sizeof (*p->in_use_p));
1858 ++i)
1860 unsigned long j;
1862 /* Something is in use if it is marked, or if it was in use in a
1863 context further down the context stack. */
1864 p->in_use_p[i] |= save_in_use_p (p)[i];
1866 /* Decrement the free object count for every object allocated. */
1867 for (j = p->in_use_p[i]; j; j >>= 1)
1868 p->num_free_objects -= (j & 1);
1871 gcc_assert (p->num_free_objects < num_objects);
1874 /* Unmark all objects. */
1876 static void
1877 clear_marks (void)
1879 unsigned order;
1881 for (order = 2; order < NUM_ORDERS; order++)
1883 page_entry *p;
1885 for (p = G.pages[order]; p != NULL; p = p->next)
1887 size_t num_objects = OBJECTS_IN_PAGE (p);
1888 size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
1890 /* The data should be page-aligned. */
1891 gcc_assert (!((uintptr_t) p->page & (G.pagesize - 1)));
1893 /* Pages that aren't in the topmost context are not collected;
1894 nevertheless, we need their in-use bit vectors to store GC
1895 marks. So, back them up first. */
1896 if (p->context_depth < G.context_depth)
1898 if (! save_in_use_p (p))
1899 save_in_use_p (p) = XNEWVAR (unsigned long, bitmap_size);
1900 memcpy (save_in_use_p (p), p->in_use_p, bitmap_size);
1903 /* Reset reset the number of free objects and clear the
1904 in-use bits. These will be adjusted by mark_obj. */
1905 p->num_free_objects = num_objects;
1906 memset (p->in_use_p, 0, bitmap_size);
1908 /* Make sure the one-past-the-end bit is always set. */
1909 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
1910 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
1915 /* Check if any blocks with a registered finalizer have become unmarked. If so
1916 run the finalizer and unregister it because the block is about to be freed.
1917 Note that no garantee is made about what order finalizers will run in so
1918 touching other objects in gc memory is extremely unwise. */
1920 static void
1921 ggc_handle_finalizers ()
1923 unsigned dlen = G.finalizers.length();
1924 for (unsigned d = G.context_depth; d < dlen; ++d)
1926 vec<finalizer> &v = G.finalizers[d];
1927 unsigned length = v.length ();
1928 for (unsigned int i = 0; i < length;)
1930 finalizer &f = v[i];
1931 if (!ggc_marked_p (f.addr ()))
1933 f.call ();
1934 v.unordered_remove (i);
1935 length--;
1937 else
1938 i++;
1942 gcc_assert (dlen == G.vec_finalizers.length());
1943 for (unsigned d = G.context_depth; d < dlen; ++d)
1945 vec<vec_finalizer> &vv = G.vec_finalizers[d];
1946 unsigned length = vv.length ();
1947 for (unsigned int i = 0; i < length;)
1949 vec_finalizer &f = vv[i];
1950 if (!ggc_marked_p (f.addr ()))
1952 f.call ();
1953 vv.unordered_remove (i);
1954 length--;
1956 else
1957 i++;
1962 /* Free all empty pages. Partially empty pages need no attention
1963 because the `mark' bit doubles as an `unused' bit. */
1965 static void
1966 sweep_pages (void)
1968 unsigned order;
1970 for (order = 2; order < NUM_ORDERS; order++)
1972 /* The last page-entry to consider, regardless of entries
1973 placed at the end of the list. */
1974 page_entry * const last = G.page_tails[order];
1976 size_t num_objects;
1977 size_t live_objects;
1978 page_entry *p, *previous;
1979 int done;
1981 p = G.pages[order];
1982 if (p == NULL)
1983 continue;
1985 previous = NULL;
1988 page_entry *next = p->next;
1990 /* Loop until all entries have been examined. */
1991 done = (p == last);
1993 num_objects = OBJECTS_IN_PAGE (p);
1995 /* Add all live objects on this page to the count of
1996 allocated memory. */
1997 live_objects = num_objects - p->num_free_objects;
1999 G.allocated += OBJECT_SIZE (order) * live_objects;
2001 /* Only objects on pages in the topmost context should get
2002 collected. */
2003 if (p->context_depth < G.context_depth)
2006 /* Remove the page if it's empty. */
2007 else if (live_objects == 0)
2009 /* If P was the first page in the list, then NEXT
2010 becomes the new first page in the list, otherwise
2011 splice P out of the forward pointers. */
2012 if (! previous)
2013 G.pages[order] = next;
2014 else
2015 previous->next = next;
2017 /* Splice P out of the back pointers too. */
2018 if (next)
2019 next->prev = previous;
2021 /* Are we removing the last element? */
2022 if (p == G.page_tails[order])
2023 G.page_tails[order] = previous;
2024 free_page (p);
2025 p = previous;
2028 /* If the page is full, move it to the end. */
2029 else if (p->num_free_objects == 0)
2031 /* Don't move it if it's already at the end. */
2032 if (p != G.page_tails[order])
2034 /* Move p to the end of the list. */
2035 p->next = NULL;
2036 p->prev = G.page_tails[order];
2037 G.page_tails[order]->next = p;
2039 /* Update the tail pointer... */
2040 G.page_tails[order] = p;
2042 /* ... and the head pointer, if necessary. */
2043 if (! previous)
2044 G.pages[order] = next;
2045 else
2046 previous->next = next;
2048 /* And update the backpointer in NEXT if necessary. */
2049 if (next)
2050 next->prev = previous;
2052 p = previous;
2056 /* If we've fallen through to here, it's a page in the
2057 topmost context that is neither full nor empty. Such a
2058 page must precede pages at lesser context depth in the
2059 list, so move it to the head. */
2060 else if (p != G.pages[order])
2062 previous->next = p->next;
2064 /* Update the backchain in the next node if it exists. */
2065 if (p->next)
2066 p->next->prev = previous;
2068 /* Move P to the head of the list. */
2069 p->next = G.pages[order];
2070 p->prev = NULL;
2071 G.pages[order]->prev = p;
2073 /* Update the head pointer. */
2074 G.pages[order] = p;
2076 /* Are we moving the last element? */
2077 if (G.page_tails[order] == p)
2078 G.page_tails[order] = previous;
2079 p = previous;
2082 previous = p;
2083 p = next;
2085 while (! done);
2087 /* Now, restore the in_use_p vectors for any pages from contexts
2088 other than the current one. */
2089 for (p = G.pages[order]; p; p = p->next)
2090 if (p->context_depth != G.context_depth)
2091 ggc_recalculate_in_use_p (p);
2095 #ifdef ENABLE_GC_CHECKING
2096 /* Clobber all free objects. */
2098 static void
2099 poison_pages (void)
2101 unsigned order;
2103 for (order = 2; order < NUM_ORDERS; order++)
2105 size_t size = OBJECT_SIZE (order);
2106 page_entry *p;
2108 for (p = G.pages[order]; p != NULL; p = p->next)
2110 size_t num_objects;
2111 size_t i;
2113 if (p->context_depth != G.context_depth)
2114 /* Since we don't do any collection for pages in pushed
2115 contexts, there's no need to do any poisoning. And
2116 besides, the IN_USE_P array isn't valid until we pop
2117 contexts. */
2118 continue;
2120 num_objects = OBJECTS_IN_PAGE (p);
2121 for (i = 0; i < num_objects; i++)
2123 size_t word, bit;
2124 word = i / HOST_BITS_PER_LONG;
2125 bit = i % HOST_BITS_PER_LONG;
2126 if (((p->in_use_p[word] >> bit) & 1) == 0)
2128 char *object = p->page + i * size;
2130 /* Keep poison-by-write when we expect to use Valgrind,
2131 so the exact same memory semantics is kept, in case
2132 there are memory errors. We override this request
2133 below. */
2134 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (object,
2135 size));
2136 memset (object, 0xa5, size);
2138 /* Drop the handle to avoid handle leak. */
2139 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (object, size));
2145 #else
2146 #define poison_pages()
2147 #endif
2149 #ifdef ENABLE_GC_ALWAYS_COLLECT
2150 /* Validate that the reportedly free objects actually are. */
2152 static void
2153 validate_free_objects (void)
2155 struct free_object *f, *next, *still_free = NULL;
2157 for (f = G.free_object_list; f ; f = next)
2159 page_entry *pe = lookup_page_table_entry (f->object);
2160 size_t bit, word;
2162 bit = OFFSET_TO_BIT ((char *)f->object - pe->page, pe->order);
2163 word = bit / HOST_BITS_PER_LONG;
2164 bit = bit % HOST_BITS_PER_LONG;
2165 next = f->next;
2167 /* Make certain it isn't visible from any root. Notice that we
2168 do this check before sweep_pages merges save_in_use_p. */
2169 gcc_assert (!(pe->in_use_p[word] & (1UL << bit)));
2171 /* If the object comes from an outer context, then retain the
2172 free_object entry, so that we can verify that the address
2173 isn't live on the stack in some outer context. */
2174 if (pe->context_depth != G.context_depth)
2176 f->next = still_free;
2177 still_free = f;
2179 else
2180 free (f);
2183 G.free_object_list = still_free;
2185 #else
2186 #define validate_free_objects()
2187 #endif
2189 /* Top level mark-and-sweep routine. */
2191 void
2192 ggc_collect (enum ggc_collect mode)
2194 /* Avoid frequent unnecessary work by skipping collection if the
2195 total allocations haven't expanded much since the last
2196 collection. */
2197 float allocated_last_gc =
2198 MAX (G.allocated_last_gc, (size_t)param_ggc_min_heapsize * ONE_K);
2200 /* It is also good time to get memory block pool into limits. */
2201 memory_block_pool::trim ();
2203 float min_expand = allocated_last_gc * param_ggc_min_expand / 100;
2204 if (mode == GGC_COLLECT_HEURISTIC
2205 && G.allocated < allocated_last_gc + min_expand)
2206 return;
2208 timevar_push (TV_GC);
2209 if (GGC_DEBUG_LEVEL >= 2)
2210 fprintf (G.debug_file, "BEGIN COLLECTING\n");
2212 /* Zero the total allocated bytes. This will be recalculated in the
2213 sweep phase. */
2214 size_t allocated = G.allocated;
2215 G.allocated = 0;
2217 /* Release the pages we freed the last time we collected, but didn't
2218 reuse in the interim. */
2219 release_pages ();
2221 /* Output this later so we do not interfere with release_pages. */
2222 if (!quiet_flag)
2223 fprintf (stderr, " {GC " PRsa (0) " -> ", SIZE_AMOUNT (allocated));
2225 /* Indicate that we've seen collections at this context depth. */
2226 G.context_depth_collections = ((unsigned long)1 << (G.context_depth + 1)) - 1;
2228 invoke_plugin_callbacks (PLUGIN_GGC_START, NULL);
2230 in_gc = true;
2231 clear_marks ();
2232 ggc_mark_roots ();
2233 ggc_handle_finalizers ();
2235 if (GATHER_STATISTICS)
2236 ggc_prune_overhead_list ();
2238 poison_pages ();
2239 validate_free_objects ();
2240 sweep_pages ();
2242 in_gc = false;
2243 G.allocated_last_gc = G.allocated;
2245 invoke_plugin_callbacks (PLUGIN_GGC_END, NULL);
2247 timevar_pop (TV_GC);
2249 if (!quiet_flag)
2250 fprintf (stderr, PRsa (0) "}", SIZE_AMOUNT (G.allocated));
2251 if (GGC_DEBUG_LEVEL >= 2)
2252 fprintf (G.debug_file, "END COLLECTING\n");
2255 /* Return free pages to the system. */
2257 void
2258 ggc_trim ()
2260 timevar_push (TV_GC);
2261 G.allocated = 0;
2262 sweep_pages ();
2263 release_pages ();
2264 if (!quiet_flag)
2265 fprintf (stderr, " {GC trimmed to " PRsa (0) ", " PRsa (0) " mapped}",
2266 SIZE_AMOUNT (G.allocated), SIZE_AMOUNT (G.bytes_mapped));
2267 timevar_pop (TV_GC);
2270 /* Assume that all GGC memory is reachable and grow the limits for next
2271 collection. With checking, trigger GGC so -Q compilation outputs how much
2272 of memory really is reachable. */
2274 void
2275 ggc_grow (void)
2277 if (!flag_checking)
2278 G.allocated_last_gc = MAX (G.allocated_last_gc,
2279 G.allocated);
2280 else
2281 ggc_collect ();
2282 if (!quiet_flag)
2283 fprintf (stderr, " {GC " PRsa (0) "} ", SIZE_AMOUNT (G.allocated));
2286 void
2287 ggc_print_statistics (void)
2289 struct ggc_statistics stats;
2290 unsigned int i;
2291 size_t total_overhead = 0;
2293 /* Clear the statistics. */
2294 memset (&stats, 0, sizeof (stats));
2296 /* Make sure collection will really occur. */
2297 G.allocated_last_gc = 0;
2299 /* Collect and print the statistics common across collectors. */
2300 ggc_print_common_statistics (stderr, &stats);
2302 /* Release free pages so that we will not count the bytes allocated
2303 there as part of the total allocated memory. */
2304 release_pages ();
2306 /* Collect some information about the various sizes of
2307 allocation. */
2308 fprintf (stderr,
2309 "Memory still allocated at the end of the compilation process\n");
2310 fprintf (stderr, "%-8s %10s %10s %10s\n",
2311 "Size", "Allocated", "Used", "Overhead");
2312 for (i = 0; i < NUM_ORDERS; ++i)
2314 page_entry *p;
2315 size_t allocated;
2316 size_t in_use;
2317 size_t overhead;
2319 /* Skip empty entries. */
2320 if (!G.pages[i])
2321 continue;
2323 overhead = allocated = in_use = 0;
2325 /* Figure out the total number of bytes allocated for objects of
2326 this size, and how many of them are actually in use. Also figure
2327 out how much memory the page table is using. */
2328 for (p = G.pages[i]; p; p = p->next)
2330 allocated += p->bytes;
2331 in_use +=
2332 (OBJECTS_IN_PAGE (p) - p->num_free_objects) * OBJECT_SIZE (i);
2334 overhead += (sizeof (page_entry) - sizeof (long)
2335 + BITMAP_SIZE (OBJECTS_IN_PAGE (p) + 1));
2337 fprintf (stderr, "%-8" PRIu64 " " PRsa (10) " " PRsa (10) " "
2338 PRsa (10) "\n",
2339 (uint64_t)OBJECT_SIZE (i),
2340 SIZE_AMOUNT (allocated),
2341 SIZE_AMOUNT (in_use),
2342 SIZE_AMOUNT (overhead));
2343 total_overhead += overhead;
2345 fprintf (stderr, "%-8s " PRsa (10) " " PRsa (10) " " PRsa (10) "\n",
2346 "Total",
2347 SIZE_AMOUNT (G.bytes_mapped),
2348 SIZE_AMOUNT (G.allocated),
2349 SIZE_AMOUNT (total_overhead));
2351 if (GATHER_STATISTICS)
2353 fprintf (stderr, "\nTotal allocations and overheads during "
2354 "the compilation process\n");
2356 fprintf (stderr, "Total Overhead: "
2357 PRsa (9) "\n",
2358 SIZE_AMOUNT (G.stats.total_overhead));
2359 fprintf (stderr, "Total Allocated: "
2360 PRsa (9) "\n",
2361 SIZE_AMOUNT (G.stats.total_allocated));
2363 fprintf (stderr, "Total Overhead under 32B: "
2364 PRsa (9) "\n",
2365 SIZE_AMOUNT (G.stats.total_overhead_under32));
2366 fprintf (stderr, "Total Allocated under 32B: "
2367 PRsa (9) "\n",
2368 SIZE_AMOUNT (G.stats.total_allocated_under32));
2369 fprintf (stderr, "Total Overhead under 64B: "
2370 PRsa (9) "\n",
2371 SIZE_AMOUNT (G.stats.total_overhead_under64));
2372 fprintf (stderr, "Total Allocated under 64B: "
2373 PRsa (9) "\n",
2374 SIZE_AMOUNT (G.stats.total_allocated_under64));
2375 fprintf (stderr, "Total Overhead under 128B: "
2376 PRsa (9) "\n",
2377 SIZE_AMOUNT (G.stats.total_overhead_under128));
2378 fprintf (stderr, "Total Allocated under 128B: "
2379 PRsa (9) "\n",
2380 SIZE_AMOUNT (G.stats.total_allocated_under128));
2382 for (i = 0; i < NUM_ORDERS; i++)
2383 if (G.stats.total_allocated_per_order[i])
2385 fprintf (stderr, "Total Overhead page size %9" PRIu64 ": "
2386 PRsa (9) "\n",
2387 (uint64_t)OBJECT_SIZE (i),
2388 SIZE_AMOUNT (G.stats.total_overhead_per_order[i]));
2389 fprintf (stderr, "Total Allocated page size %9" PRIu64 ": "
2390 PRsa (9) "\n",
2391 (uint64_t)OBJECT_SIZE (i),
2392 SIZE_AMOUNT (G.stats.total_allocated_per_order[i]));
2397 struct ggc_pch_ondisk
2399 unsigned totals[NUM_ORDERS];
2402 struct ggc_pch_data
2404 struct ggc_pch_ondisk d;
2405 uintptr_t base[NUM_ORDERS];
2406 size_t written[NUM_ORDERS];
2409 struct ggc_pch_data *
2410 init_ggc_pch (void)
2412 return XCNEW (struct ggc_pch_data);
2415 void
2416 ggc_pch_count_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
2417 size_t size, bool is_string ATTRIBUTE_UNUSED)
2419 unsigned order;
2421 if (size < NUM_SIZE_LOOKUP)
2422 order = size_lookup[size];
2423 else
2425 order = 10;
2426 while (size > OBJECT_SIZE (order))
2427 order++;
2430 d->d.totals[order]++;
2433 size_t
2434 ggc_pch_total_size (struct ggc_pch_data *d)
2436 size_t a = 0;
2437 unsigned i;
2439 for (i = 0; i < NUM_ORDERS; i++)
2440 a += PAGE_ALIGN (d->d.totals[i] * OBJECT_SIZE (i));
2441 return a;
2444 void
2445 ggc_pch_this_base (struct ggc_pch_data *d, void *base)
2447 uintptr_t a = (uintptr_t) base;
2448 unsigned i;
2450 for (i = 0; i < NUM_ORDERS; i++)
2452 d->base[i] = a;
2453 a += PAGE_ALIGN (d->d.totals[i] * OBJECT_SIZE (i));
2458 char *
2459 ggc_pch_alloc_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
2460 size_t size, bool is_string ATTRIBUTE_UNUSED)
2462 unsigned order;
2463 char *result;
2465 if (size < NUM_SIZE_LOOKUP)
2466 order = size_lookup[size];
2467 else
2469 order = 10;
2470 while (size > OBJECT_SIZE (order))
2471 order++;
2474 result = (char *) d->base[order];
2475 d->base[order] += OBJECT_SIZE (order);
2476 return result;
2479 void
2480 ggc_pch_prepare_write (struct ggc_pch_data *d ATTRIBUTE_UNUSED,
2481 FILE *f ATTRIBUTE_UNUSED)
2483 /* Nothing to do. */
2486 void
2487 ggc_pch_write_object (struct ggc_pch_data *d,
2488 FILE *f, void *x, void *newx ATTRIBUTE_UNUSED,
2489 size_t size, bool is_string ATTRIBUTE_UNUSED)
2491 unsigned order;
2492 static const char emptyBytes[256] = { 0 };
2494 if (size < NUM_SIZE_LOOKUP)
2495 order = size_lookup[size];
2496 else
2498 order = 10;
2499 while (size > OBJECT_SIZE (order))
2500 order++;
2503 if (fwrite (x, size, 1, f) != 1)
2504 fatal_error (input_location, "cannot write PCH file: %m");
2506 /* If SIZE is not the same as OBJECT_SIZE(order), then we need to pad the
2507 object out to OBJECT_SIZE(order). This happens for strings. */
2509 if (size != OBJECT_SIZE (order))
2511 unsigned padding = OBJECT_SIZE (order) - size;
2513 /* To speed small writes, we use a nulled-out array that's larger
2514 than most padding requests as the source for our null bytes. This
2515 permits us to do the padding with fwrite() rather than fseek(), and
2516 limits the chance the OS may try to flush any outstanding writes. */
2517 if (padding <= sizeof (emptyBytes))
2519 if (fwrite (emptyBytes, 1, padding, f) != padding)
2520 fatal_error (input_location, "cannot write PCH file");
2522 else
2524 /* Larger than our buffer? Just default to fseek. */
2525 if (fseek (f, padding, SEEK_CUR) != 0)
2526 fatal_error (input_location, "cannot write PCH file");
2530 d->written[order]++;
2531 if (d->written[order] == d->d.totals[order]
2532 && fseek (f, ROUND_UP_VALUE (d->d.totals[order] * OBJECT_SIZE (order),
2533 G.pagesize),
2534 SEEK_CUR) != 0)
2535 fatal_error (input_location, "cannot write PCH file: %m");
2538 void
2539 ggc_pch_finish (struct ggc_pch_data *d, FILE *f)
2541 if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
2542 fatal_error (input_location, "cannot write PCH file: %m");
2543 free (d);
2546 /* Move the PCH PTE entries just added to the end of by_depth, to the
2547 front. */
2549 static void
2550 move_ptes_to_front (int count_old_page_tables, int count_new_page_tables)
2552 /* First, we swap the new entries to the front of the varrays. */
2553 page_entry **new_by_depth;
2554 unsigned long **new_save_in_use;
2556 new_by_depth = XNEWVEC (page_entry *, G.by_depth_max);
2557 new_save_in_use = XNEWVEC (unsigned long *, G.by_depth_max);
2559 memcpy (&new_by_depth[0],
2560 &G.by_depth[count_old_page_tables],
2561 count_new_page_tables * sizeof (void *));
2562 memcpy (&new_by_depth[count_new_page_tables],
2563 &G.by_depth[0],
2564 count_old_page_tables * sizeof (void *));
2565 memcpy (&new_save_in_use[0],
2566 &G.save_in_use[count_old_page_tables],
2567 count_new_page_tables * sizeof (void *));
2568 memcpy (&new_save_in_use[count_new_page_tables],
2569 &G.save_in_use[0],
2570 count_old_page_tables * sizeof (void *));
2572 free (G.by_depth);
2573 free (G.save_in_use);
2575 G.by_depth = new_by_depth;
2576 G.save_in_use = new_save_in_use;
2578 /* Now update all the index_by_depth fields. */
2579 for (unsigned i = G.by_depth_in_use; i--;)
2581 page_entry *p = G.by_depth[i];
2582 p->index_by_depth = i;
2585 /* And last, we update the depth pointers in G.depth. The first
2586 entry is already 0, and context 0 entries always start at index
2587 0, so there is nothing to update in the first slot. We need a
2588 second slot, only if we have old ptes, and if we do, they start
2589 at index count_new_page_tables. */
2590 if (count_old_page_tables)
2591 push_depth (count_new_page_tables);
2594 void
2595 ggc_pch_read (FILE *f, void *addr)
2597 struct ggc_pch_ondisk d;
2598 unsigned i;
2599 char *offs = (char *) addr;
2600 unsigned long count_old_page_tables;
2601 unsigned long count_new_page_tables;
2603 count_old_page_tables = G.by_depth_in_use;
2605 if (fread (&d, sizeof (d), 1, f) != 1)
2606 fatal_error (input_location, "cannot read PCH file: %m");
2608 /* We've just read in a PCH file. So, every object that used to be
2609 allocated is now free. */
2610 clear_marks ();
2611 #ifdef ENABLE_GC_CHECKING
2612 poison_pages ();
2613 #endif
2614 /* Since we free all the allocated objects, the free list becomes
2615 useless. Validate it now, which will also clear it. */
2616 validate_free_objects ();
2618 /* No object read from a PCH file should ever be freed. So, set the
2619 context depth to 1, and set the depth of all the currently-allocated
2620 pages to be 1 too. PCH pages will have depth 0. */
2621 gcc_assert (!G.context_depth);
2622 G.context_depth = 1;
2623 /* Allocate space for the depth 1 finalizers. */
2624 G.finalizers.safe_push (vNULL);
2625 G.vec_finalizers.safe_push (vNULL);
2626 gcc_assert (G.finalizers.length() == 2);
2627 for (i = 0; i < NUM_ORDERS; i++)
2629 page_entry *p;
2630 for (p = G.pages[i]; p != NULL; p = p->next)
2631 p->context_depth = G.context_depth;
2634 /* Allocate the appropriate page-table entries for the pages read from
2635 the PCH file. */
2637 for (i = 0; i < NUM_ORDERS; i++)
2639 struct page_entry *entry;
2640 char *pte;
2641 size_t bytes;
2642 size_t num_objs;
2643 size_t j;
2645 if (d.totals[i] == 0)
2646 continue;
2648 bytes = PAGE_ALIGN (d.totals[i] * OBJECT_SIZE (i));
2649 num_objs = bytes / OBJECT_SIZE (i);
2650 entry = XCNEWVAR (struct page_entry, (sizeof (struct page_entry)
2651 - sizeof (long)
2652 + BITMAP_SIZE (num_objs + 1)));
2653 entry->bytes = bytes;
2654 entry->page = offs;
2655 entry->context_depth = 0;
2656 offs += bytes;
2657 entry->num_free_objects = 0;
2658 entry->order = i;
2660 for (j = 0;
2661 j + HOST_BITS_PER_LONG <= num_objs + 1;
2662 j += HOST_BITS_PER_LONG)
2663 entry->in_use_p[j / HOST_BITS_PER_LONG] = -1;
2664 for (; j < num_objs + 1; j++)
2665 entry->in_use_p[j / HOST_BITS_PER_LONG]
2666 |= 1L << (j % HOST_BITS_PER_LONG);
2668 for (pte = entry->page;
2669 pte < entry->page + entry->bytes;
2670 pte += G.pagesize)
2671 set_page_table_entry (pte, entry);
2673 if (G.page_tails[i] != NULL)
2674 G.page_tails[i]->next = entry;
2675 else
2676 G.pages[i] = entry;
2677 G.page_tails[i] = entry;
2679 /* We start off by just adding all the new information to the
2680 end of the varrays, later, we will move the new information
2681 to the front of the varrays, as the PCH page tables are at
2682 context 0. */
2683 push_by_depth (entry, 0);
2686 /* Now, we update the various data structures that speed page table
2687 handling. */
2688 count_new_page_tables = G.by_depth_in_use - count_old_page_tables;
2690 move_ptes_to_front (count_old_page_tables, count_new_page_tables);
2692 /* Update the statistics. */
2693 G.allocated = G.allocated_last_gc = offs - (char *)addr;