gcc/
[official-gcc.git] / gcc / ggc-zone.c
blob12dc8740529565cadb55a93108ebc28796c977dc
1 /* "Bag-of-pages" zone garbage collector for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
3 2010 Free Software Foundation, Inc.
5 Contributed by Richard Henderson (rth@redhat.com) and Daniel Berlin
6 (dberlin@dberlin.org). Rewritten by Daniel Jacobowitz
7 <dan@codesourcery.com>.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3. If not see
23 <http://www.gnu.org/licenses/>. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "rtl.h"
31 #include "tm_p.h"
32 #include "toplev.h"
33 #include "flags.h"
34 #include "ggc.h"
35 #include "ggc-internal.h"
36 #include "timevar.h"
37 #include "params.h"
38 #include "bitmap.h"
39 #include "plugin.h"
41 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
42 file open. Prefer either to valloc. */
43 #ifdef HAVE_MMAP_ANON
44 # undef HAVE_MMAP_DEV_ZERO
46 # include <sys/mman.h>
47 # ifndef MAP_FAILED
48 # define MAP_FAILED -1
49 # endif
50 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
51 # define MAP_ANONYMOUS MAP_ANON
52 # endif
53 # define USING_MMAP
54 #endif
56 #ifdef HAVE_MMAP_DEV_ZERO
57 # include <sys/mman.h>
58 # ifndef MAP_FAILED
59 # define MAP_FAILED -1
60 # endif
61 # define USING_MMAP
62 #endif
64 #ifndef USING_MMAP
65 #error Zone collector requires mmap
66 #endif
68 #if (GCC_VERSION < 3001)
69 #define prefetch(X) ((void) X)
70 #define prefetchw(X) ((void) X)
71 #else
72 #define prefetch(X) __builtin_prefetch (X)
73 #define prefetchw(X) __builtin_prefetch (X, 1, 3)
74 #endif
76 /* FUTURE NOTES:
78 If we track inter-zone pointers, we can mark single zones at a
79 time.
81 If we have a zone where we guarantee no inter-zone pointers, we
82 could mark that zone separately.
84 The garbage zone should not be marked, and we should return 1 in
85 ggc_set_mark for any object in the garbage zone, which cuts off
86 marking quickly. */
88 /* Strategy:
90 This garbage-collecting allocator segregates objects into zones.
91 It also segregates objects into "large" and "small" bins. Large
92 objects are greater than page size.
94 Pages for small objects are broken up into chunks. The page has
95 a bitmap which marks the start position of each chunk (whether
96 allocated or free). Free chunks are on one of the zone's free
97 lists and contain a pointer to the next free chunk. Chunks in
98 most of the free lists have a fixed size determined by the
99 free list. Chunks in the "other" sized free list have their size
100 stored right after their chain pointer.
102 Empty pages (of all sizes) are kept on a single page cache list,
103 and are considered first when new pages are required; they are
104 deallocated at the start of the next collection if they haven't
105 been recycled by then. The free page list is currently per-zone. */
107 /* Define GGC_DEBUG_LEVEL to print debugging information.
108 0: No debugging output.
109 1: GC statistics only.
110 2: Page-entry allocations/deallocations as well.
111 3: Object allocations as well.
112 4: Object marks as well. */
113 #define GGC_DEBUG_LEVEL (0)
115 #ifndef HOST_BITS_PER_PTR
116 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
117 #endif
119 /* This structure manages small free chunks. The SIZE field is only
120 initialized if the chunk is in the "other" sized free list. Large
121 chunks are allocated one at a time to their own page, and so don't
122 come in here. */
124 struct alloc_chunk {
125 struct alloc_chunk *next_free;
126 unsigned int size;
129 /* The size of the fixed-size portion of a small page descriptor. */
130 #define PAGE_OVERHEAD (offsetof (struct small_page_entry, alloc_bits))
132 /* The collector's idea of the page size. This must be a power of two
133 no larger than the system page size, because pages must be aligned
134 to this amount and are tracked at this granularity in the page
135 table. We choose a size at compile time for efficiency.
137 We could make a better guess at compile time if PAGE_SIZE is a
138 constant in system headers, and PAGE_SHIFT is defined... */
139 #define GGC_PAGE_SIZE 4096
140 #define GGC_PAGE_MASK (GGC_PAGE_SIZE - 1)
141 #define GGC_PAGE_SHIFT 12
143 #if 0
144 /* Alternative definitions which use the runtime page size. */
145 #define GGC_PAGE_SIZE G.pagesize
146 #define GGC_PAGE_MASK G.page_mask
147 #define GGC_PAGE_SHIFT G.lg_pagesize
148 #endif
150 /* The size of a small page managed by the garbage collector. This
151 must currently be GGC_PAGE_SIZE, but with a few changes could
152 be any multiple of it to reduce certain kinds of overhead. */
153 #define SMALL_PAGE_SIZE GGC_PAGE_SIZE
155 /* Free bin information. These numbers may be in need of re-tuning.
156 In general, decreasing the number of free bins would seem to
157 increase the time it takes to allocate... */
159 /* FIXME: We can't use anything but MAX_ALIGNMENT for the bin size
160 today. */
162 #define NUM_FREE_BINS 64
163 #define FREE_BIN_DELTA MAX_ALIGNMENT
164 #define SIZE_BIN_DOWN(SIZE) ((SIZE) / FREE_BIN_DELTA)
166 /* Allocation and marking parameters. */
168 /* The smallest allocatable unit to keep track of. */
169 #define BYTES_PER_ALLOC_BIT MAX_ALIGNMENT
171 /* The smallest markable unit. If we require each allocated object
172 to contain at least two allocatable units, we can use half as many
173 bits for the mark bitmap. But this adds considerable complexity
174 to sweeping. */
175 #define BYTES_PER_MARK_BIT BYTES_PER_ALLOC_BIT
177 #define BYTES_PER_MARK_WORD (8 * BYTES_PER_MARK_BIT * sizeof (mark_type))
179 /* We use this structure to determine the alignment required for
180 allocations.
182 There are several things wrong with this estimation of alignment.
184 The maximum alignment for a structure is often less than the
185 maximum alignment for a basic data type; for instance, on some
186 targets long long must be aligned to sizeof (int) in a structure
187 and sizeof (long long) in a variable. i386-linux is one example;
188 Darwin is another (sometimes, depending on the compiler in use).
190 Also, long double is not included. Nothing in GCC uses long
191 double, so we assume that this is OK. On powerpc-darwin, adding
192 long double would bring the maximum alignment up to 16 bytes,
193 and until we need long double (or to vectorize compiler operations)
194 that's painfully wasteful. This will need to change, some day. */
196 struct max_alignment {
197 char c;
198 union {
199 HOST_WIDEST_INT i;
200 double d;
201 } u;
204 /* The biggest alignment required. */
206 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
208 /* Compute the smallest multiple of F that is >= X. */
210 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
212 /* Types to use for the allocation and mark bitmaps. It might be
213 a good idea to add ffsl to libiberty and use unsigned long
214 instead; that could speed us up where long is wider than int. */
216 typedef unsigned int alloc_type;
217 typedef unsigned int mark_type;
218 #define alloc_ffs(x) ffs(x)
220 /* A page_entry records the status of an allocation page. This is the
221 common data between all three kinds of pages - small, large, and
222 PCH. */
223 typedef struct page_entry
225 /* The address at which the memory is allocated. */
226 char *page;
228 /* The zone that this page entry belongs to. */
229 struct alloc_zone *zone;
231 #ifdef GATHER_STATISTICS
232 /* How many collections we've survived. */
233 size_t survived;
234 #endif
236 /* Does this page contain small objects, or one large object? */
237 bool large_p;
239 /* Is this page part of the loaded PCH? */
240 bool pch_p;
241 } page_entry;
243 /* Additional data needed for small pages. */
244 struct small_page_entry
246 struct page_entry common;
248 /* The next small page entry, or NULL if this is the last. */
249 struct small_page_entry *next;
251 /* If currently marking this zone, a pointer to the mark bits
252 for this page. If we aren't currently marking this zone,
253 this pointer may be stale (pointing to freed memory). */
254 mark_type *mark_bits;
256 /* The allocation bitmap. This array extends far enough to have
257 one bit for every BYTES_PER_ALLOC_BIT bytes in the page. */
258 alloc_type alloc_bits[1];
261 /* Additional data needed for large pages. */
262 struct large_page_entry
264 struct page_entry common;
266 /* The next large page entry, or NULL if this is the last. */
267 struct large_page_entry *next;
269 /* The number of bytes allocated, not including the page entry. */
270 size_t bytes;
272 /* The previous page in the list, so that we can unlink this one. */
273 struct large_page_entry *prev;
275 /* During marking, is this object marked? */
276 bool mark_p;
279 /* A two-level tree is used to look up the page-entry for a given
280 pointer. Two chunks of the pointer's bits are extracted to index
281 the first and second levels of the tree, as follows:
283 HOST_PAGE_SIZE_BITS
284 32 | |
285 msb +----------------+----+------+------+ lsb
286 | | |
287 PAGE_L1_BITS |
289 PAGE_L2_BITS
291 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
292 pages are aligned on system page boundaries. The next most
293 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
294 index values in the lookup table, respectively.
296 For 32-bit architectures and the settings below, there are no
297 leftover bits. For architectures with wider pointers, the lookup
298 tree points to a list of pages, which must be scanned to find the
299 correct one. */
301 #define PAGE_L1_BITS (8)
302 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - GGC_PAGE_SHIFT)
303 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
304 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
306 #define LOOKUP_L1(p) \
307 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
309 #define LOOKUP_L2(p) \
310 (((size_t) (p) >> GGC_PAGE_SHIFT) & ((1 << PAGE_L2_BITS) - 1))
312 #if HOST_BITS_PER_PTR <= 32
314 /* On 32-bit hosts, we use a two level page table, as pictured above. */
315 typedef page_entry **page_table[PAGE_L1_SIZE];
317 #else
319 /* On 64-bit hosts, we use the same two level page tables plus a linked
320 list that disambiguates the top 32-bits. There will almost always be
321 exactly one entry in the list. */
322 typedef struct page_table_chain
324 struct page_table_chain *next;
325 size_t high_bits;
326 page_entry **table[PAGE_L1_SIZE];
327 } *page_table;
329 #endif
331 /* The global variables. */
332 static struct globals
334 /* The linked list of zones. */
335 struct alloc_zone *zones;
337 /* Lookup table for associating allocation pages with object addresses. */
338 page_table lookup;
340 /* The system's page size, and related constants. */
341 size_t pagesize;
342 size_t lg_pagesize;
343 size_t page_mask;
345 /* The size to allocate for a small page entry. This includes
346 the size of the structure and the size of the allocation
347 bitmap. */
348 size_t small_page_overhead;
350 #if defined (HAVE_MMAP_DEV_ZERO)
351 /* A file descriptor open to /dev/zero for reading. */
352 int dev_zero_fd;
353 #endif
355 /* Allocate pages in chunks of this size, to throttle calls to memory
356 allocation routines. The first page is used, the rest go onto the
357 free list. */
358 size_t quire_size;
360 /* The file descriptor for debugging output. */
361 FILE *debug_file;
362 } G;
364 /* A zone allocation structure. There is one of these for every
365 distinct allocation zone. */
366 struct alloc_zone
368 /* The most recent free chunk is saved here, instead of in the linked
369 free list, to decrease list manipulation. It is most likely that we
370 will want this one. */
371 char *cached_free;
372 size_t cached_free_size;
374 /* Linked lists of free storage. Slots 1 ... NUM_FREE_BINS have chunks of size
375 FREE_BIN_DELTA. All other chunks are in slot 0. */
376 struct alloc_chunk *free_chunks[NUM_FREE_BINS + 1];
378 /* The highest bin index which might be non-empty. It may turn out
379 to be empty, in which case we have to search downwards. */
380 size_t high_free_bin;
382 /* Bytes currently allocated in this zone. */
383 size_t allocated;
385 /* Linked list of the small pages in this zone. */
386 struct small_page_entry *pages;
388 /* Doubly linked list of large pages in this zone. */
389 struct large_page_entry *large_pages;
391 /* If we are currently marking this zone, a pointer to the mark bits. */
392 mark_type *mark_bits;
394 /* Name of the zone. */
395 const char *name;
397 /* The number of small pages currently allocated in this zone. */
398 size_t n_small_pages;
400 /* Bytes allocated at the end of the last collection. */
401 size_t allocated_last_gc;
403 /* Total amount of memory mapped. */
404 size_t bytes_mapped;
406 /* A cache of free system pages. */
407 struct small_page_entry *free_pages;
409 /* Next zone in the linked list of zones. */
410 struct alloc_zone *next_zone;
412 /* True if this zone was collected during this collection. */
413 bool was_collected;
415 /* True if this zone should be destroyed after the next collection. */
416 bool dead;
418 #ifdef GATHER_STATISTICS
419 struct
421 /* Total GC-allocated memory. */
422 unsigned long long total_allocated;
423 /* Total overhead for GC-allocated memory. */
424 unsigned long long total_overhead;
426 /* Total allocations and overhead for sizes less than 32, 64 and 128.
427 These sizes are interesting because they are typical cache line
428 sizes. */
430 unsigned long long total_allocated_under32;
431 unsigned long long total_overhead_under32;
433 unsigned long long total_allocated_under64;
434 unsigned long long total_overhead_under64;
436 unsigned long long total_allocated_under128;
437 unsigned long long total_overhead_under128;
438 } stats;
439 #endif
440 } main_zone;
442 /* Some default zones. */
443 struct alloc_zone rtl_zone;
444 struct alloc_zone tree_zone;
445 struct alloc_zone tree_id_zone;
447 /* The PCH zone does not need a normal zone structure, and it does
448 not live on the linked list of zones. */
449 struct pch_zone
451 /* The start of the PCH zone. NULL if there is none. */
452 char *page;
454 /* The end of the PCH zone. NULL if there is none. */
455 char *end;
457 /* The size of the PCH zone. 0 if there is none. */
458 size_t bytes;
460 /* The allocation bitmap for the PCH zone. */
461 alloc_type *alloc_bits;
463 /* If we are currently marking, the mark bitmap for the PCH zone.
464 When it is first read in, we could avoid marking the PCH,
465 because it will not contain any pointers to GC memory outside
466 of the PCH; however, the PCH is currently mapped as writable,
467 so we must mark it in case new pointers are added. */
468 mark_type *mark_bits;
469 } pch_zone;
471 #ifdef USING_MMAP
472 static char *alloc_anon (char *, size_t, struct alloc_zone *);
473 #endif
474 static struct small_page_entry * alloc_small_page (struct alloc_zone *);
475 static struct large_page_entry * alloc_large_page (size_t, struct alloc_zone *);
476 static void free_chunk (char *, size_t, struct alloc_zone *);
477 static void free_small_page (struct small_page_entry *);
478 static void free_large_page (struct large_page_entry *);
479 static void release_pages (struct alloc_zone *);
480 static void sweep_pages (struct alloc_zone *);
481 static bool ggc_collect_1 (struct alloc_zone *, bool);
482 static void new_ggc_zone_1 (struct alloc_zone *, const char *);
484 /* Traverse the page table and find the entry for a page.
485 Die (probably) if the object wasn't allocated via GC. */
487 static inline page_entry *
488 lookup_page_table_entry (const void *p)
490 page_entry ***base;
491 size_t L1, L2;
493 #if HOST_BITS_PER_PTR <= 32
494 base = &G.lookup[0];
495 #else
496 page_table table = G.lookup;
497 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
498 while (table->high_bits != high_bits)
499 table = table->next;
500 base = &table->table[0];
501 #endif
503 /* Extract the level 1 and 2 indices. */
504 L1 = LOOKUP_L1 (p);
505 L2 = LOOKUP_L2 (p);
507 return base[L1][L2];
510 /* Traverse the page table and find the entry for a page.
511 Return NULL if the object wasn't allocated via the GC. */
513 static inline page_entry *
514 lookup_page_table_if_allocated (const void *p)
516 page_entry ***base;
517 size_t L1, L2;
519 #if HOST_BITS_PER_PTR <= 32
520 base = &G.lookup[0];
521 #else
522 page_table table = G.lookup;
523 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
524 while (1)
526 if (table == NULL)
527 return NULL;
528 if (table->high_bits == high_bits)
529 break;
530 table = table->next;
532 base = &table->table[0];
533 #endif
535 /* Extract the level 1 and 2 indices. */
536 L1 = LOOKUP_L1 (p);
537 if (! base[L1])
538 return NULL;
540 L2 = LOOKUP_L2 (p);
541 if (L2 >= PAGE_L2_SIZE)
542 return NULL;
543 /* We might have a page entry which does not correspond exactly to a
544 system page. */
545 if (base[L1][L2] && (const char *) p < base[L1][L2]->page)
546 return NULL;
548 return base[L1][L2];
551 /* Set the page table entry for the page that starts at P. If ENTRY
552 is NULL, clear the entry. */
554 static void
555 set_page_table_entry (void *p, page_entry *entry)
557 page_entry ***base;
558 size_t L1, L2;
560 #if HOST_BITS_PER_PTR <= 32
561 base = &G.lookup[0];
562 #else
563 page_table table;
564 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
565 for (table = G.lookup; table; table = table->next)
566 if (table->high_bits == high_bits)
567 goto found;
569 /* Not found -- allocate a new table. */
570 table = XCNEW (struct page_table_chain);
571 table->next = G.lookup;
572 table->high_bits = high_bits;
573 G.lookup = table;
574 found:
575 base = &table->table[0];
576 #endif
578 /* Extract the level 1 and 2 indices. */
579 L1 = LOOKUP_L1 (p);
580 L2 = LOOKUP_L2 (p);
582 if (base[L1] == NULL)
583 base[L1] = XCNEWVEC (page_entry *, PAGE_L2_SIZE);
585 base[L1][L2] = entry;
588 /* Find the page table entry associated with OBJECT. */
590 static inline struct page_entry *
591 zone_get_object_page (const void *object)
593 return lookup_page_table_entry (object);
596 /* Find which element of the alloc_bits array OBJECT should be
597 recorded in. */
598 static inline unsigned int
599 zone_get_object_alloc_word (const void *object)
601 return (((size_t) object & (GGC_PAGE_SIZE - 1))
602 / (8 * sizeof (alloc_type) * BYTES_PER_ALLOC_BIT));
605 /* Find which bit of the appropriate word in the alloc_bits array
606 OBJECT should be recorded in. */
607 static inline unsigned int
608 zone_get_object_alloc_bit (const void *object)
610 return (((size_t) object / BYTES_PER_ALLOC_BIT)
611 % (8 * sizeof (alloc_type)));
614 /* Find which element of the mark_bits array OBJECT should be recorded
615 in. */
616 static inline unsigned int
617 zone_get_object_mark_word (const void *object)
619 return (((size_t) object & (GGC_PAGE_SIZE - 1))
620 / (8 * sizeof (mark_type) * BYTES_PER_MARK_BIT));
623 /* Find which bit of the appropriate word in the mark_bits array
624 OBJECT should be recorded in. */
625 static inline unsigned int
626 zone_get_object_mark_bit (const void *object)
628 return (((size_t) object / BYTES_PER_MARK_BIT)
629 % (8 * sizeof (mark_type)));
632 /* Set the allocation bit corresponding to OBJECT in its page's
633 bitmap. Used to split this object from the preceding one. */
634 static inline void
635 zone_set_object_alloc_bit (const void *object)
637 struct small_page_entry *page
638 = (struct small_page_entry *) zone_get_object_page (object);
639 unsigned int start_word = zone_get_object_alloc_word (object);
640 unsigned int start_bit = zone_get_object_alloc_bit (object);
642 page->alloc_bits[start_word] |= 1L << start_bit;
645 /* Clear the allocation bit corresponding to OBJECT in PAGE's
646 bitmap. Used to coalesce this object with the preceding
647 one. */
648 static inline void
649 zone_clear_object_alloc_bit (struct small_page_entry *page,
650 const void *object)
652 unsigned int start_word = zone_get_object_alloc_word (object);
653 unsigned int start_bit = zone_get_object_alloc_bit (object);
655 /* Would xor be quicker? */
656 page->alloc_bits[start_word] &= ~(1L << start_bit);
659 /* Find the size of the object which starts at START_WORD and
660 START_BIT in ALLOC_BITS, which is at most MAX_SIZE bytes.
661 Helper function for ggc_get_size and zone_find_object_size. */
663 static inline size_t
664 zone_object_size_1 (alloc_type *alloc_bits,
665 size_t start_word, size_t start_bit,
666 size_t max_size)
668 size_t size;
669 alloc_type alloc_word;
670 int indx;
672 /* Load the first word. */
673 alloc_word = alloc_bits[start_word++];
675 /* If that was the last bit in this word, we'll want to continue
676 with the next word. Otherwise, handle the rest of this word. */
677 if (start_bit)
679 indx = alloc_ffs (alloc_word >> start_bit);
680 if (indx)
681 /* indx is 1-based. We started at the bit after the object's
682 start, but we also ended at the bit after the object's end.
683 It cancels out. */
684 return indx * BYTES_PER_ALLOC_BIT;
686 /* The extra 1 accounts for the starting unit, before start_bit. */
687 size = (sizeof (alloc_type) * 8 - start_bit + 1) * BYTES_PER_ALLOC_BIT;
689 if (size >= max_size)
690 return max_size;
692 alloc_word = alloc_bits[start_word++];
694 else
695 size = BYTES_PER_ALLOC_BIT;
697 while (alloc_word == 0)
699 size += sizeof (alloc_type) * 8 * BYTES_PER_ALLOC_BIT;
700 if (size >= max_size)
701 return max_size;
702 alloc_word = alloc_bits[start_word++];
705 indx = alloc_ffs (alloc_word);
706 return size + (indx - 1) * BYTES_PER_ALLOC_BIT;
709 /* Find the size of OBJECT on small page PAGE. */
711 static inline size_t
712 zone_find_object_size (struct small_page_entry *page,
713 const void *object)
715 const char *object_midptr = (const char *) object + BYTES_PER_ALLOC_BIT;
716 unsigned int start_word = zone_get_object_alloc_word (object_midptr);
717 unsigned int start_bit = zone_get_object_alloc_bit (object_midptr);
718 size_t max_size = (page->common.page + SMALL_PAGE_SIZE
719 - (const char *) object);
721 return zone_object_size_1 (page->alloc_bits, start_word, start_bit,
722 max_size);
725 /* highest_bit assumes that alloc_type is 32 bits. */
726 extern char check_alloc_type_size[(sizeof (alloc_type) == 4) ? 1 : -1];
728 /* Find the highest set bit in VALUE. Returns the bit number of that
729 bit, using the same values as ffs. */
730 static inline alloc_type
731 highest_bit (alloc_type value)
733 /* This also assumes that alloc_type is unsigned. */
734 value |= value >> 1;
735 value |= value >> 2;
736 value |= value >> 4;
737 value |= value >> 8;
738 value |= value >> 16;
739 value = value ^ (value >> 1);
740 return alloc_ffs (value);
743 /* Find the offset from the start of an object to P, which may point
744 into the interior of the object. */
746 static unsigned long
747 zone_find_object_offset (alloc_type *alloc_bits, size_t start_word,
748 size_t start_bit)
750 unsigned int offset_in_bits;
751 alloc_type alloc_word = alloc_bits[start_word];
753 /* Mask off any bits after the initial bit, but make sure to include
754 the initial bit in the result. Note that START_BIT is
755 0-based. */
756 if (start_bit < 8 * sizeof (alloc_type) - 1)
757 alloc_word &= (1 << (start_bit + 1)) - 1;
758 offset_in_bits = start_bit;
760 /* Search for the start of the object. */
761 while (alloc_word == 0 && start_word > 0)
763 alloc_word = alloc_bits[--start_word];
764 offset_in_bits += 8 * sizeof (alloc_type);
766 /* We must always find a set bit. */
767 gcc_assert (alloc_word != 0);
768 /* Note that the result of highest_bit is 1-based. */
769 offset_in_bits -= highest_bit (alloc_word) - 1;
771 return BYTES_PER_ALLOC_BIT * offset_in_bits;
774 /* Allocate the mark bits for every zone, and set the pointers on each
775 page. */
776 static void
777 zone_allocate_marks (void)
779 struct alloc_zone *zone;
781 for (zone = G.zones; zone; zone = zone->next_zone)
783 struct small_page_entry *page;
784 mark_type *cur_marks;
785 size_t mark_words, mark_words_per_page;
786 #ifdef ENABLE_CHECKING
787 size_t n = 0;
788 #endif
790 mark_words_per_page
791 = (GGC_PAGE_SIZE + BYTES_PER_MARK_WORD - 1) / BYTES_PER_MARK_WORD;
792 mark_words = zone->n_small_pages * mark_words_per_page;
793 zone->mark_bits = (mark_type *) xcalloc (sizeof (mark_type),
794 mark_words);
795 cur_marks = zone->mark_bits;
796 for (page = zone->pages; page; page = page->next)
798 page->mark_bits = cur_marks;
799 cur_marks += mark_words_per_page;
800 #ifdef ENABLE_CHECKING
801 n++;
802 #endif
804 #ifdef ENABLE_CHECKING
805 gcc_assert (n == zone->n_small_pages);
806 #endif
809 /* We don't collect the PCH zone, but we do have to mark it
810 (for now). */
811 if (pch_zone.bytes)
812 pch_zone.mark_bits
813 = (mark_type *) xcalloc (sizeof (mark_type),
814 CEIL (pch_zone.bytes, BYTES_PER_MARK_WORD));
817 /* After marking and sweeping, release the memory used for mark bits. */
818 static void
819 zone_free_marks (void)
821 struct alloc_zone *zone;
823 for (zone = G.zones; zone; zone = zone->next_zone)
824 if (zone->mark_bits)
826 free (zone->mark_bits);
827 zone->mark_bits = NULL;
830 if (pch_zone.bytes)
832 free (pch_zone.mark_bits);
833 pch_zone.mark_bits = NULL;
837 #ifdef USING_MMAP
838 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
839 (if non-null). The ifdef structure here is intended to cause a
840 compile error unless exactly one of the HAVE_* is defined. */
842 static inline char *
843 alloc_anon (char *pref ATTRIBUTE_UNUSED, size_t size, struct alloc_zone *zone)
845 #ifdef HAVE_MMAP_ANON
846 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
847 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
848 #endif
849 #ifdef HAVE_MMAP_DEV_ZERO
850 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
851 MAP_PRIVATE, G.dev_zero_fd, 0);
852 #endif
854 if (page == (char *) MAP_FAILED)
856 perror ("virtual memory exhausted");
857 exit (FATAL_EXIT_CODE);
860 /* Remember that we allocated this memory. */
861 zone->bytes_mapped += size;
863 /* Pretend we don't have access to the allocated pages. We'll enable
864 access to smaller pieces of the area in ggc_internal_alloc. Discard the
865 handle to avoid handle leak. */
866 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (page, size));
868 return page;
870 #endif
872 /* Allocate a new page for allocating small objects in ZONE, and
873 return an entry for it. */
875 static struct small_page_entry *
876 alloc_small_page (struct alloc_zone *zone)
878 struct small_page_entry *entry;
880 /* Check the list of free pages for one we can use. */
881 entry = zone->free_pages;
882 if (entry != NULL)
884 /* Recycle the allocated memory from this page ... */
885 zone->free_pages = entry->next;
887 else
889 /* We want just one page. Allocate a bunch of them and put the
890 extras on the freelist. (Can only do this optimization with
891 mmap for backing store.) */
892 struct small_page_entry *e, *f = zone->free_pages;
893 int i;
894 char *page;
896 page = alloc_anon (NULL, GGC_PAGE_SIZE * G.quire_size, zone);
898 /* This loop counts down so that the chain will be in ascending
899 memory order. */
900 for (i = G.quire_size - 1; i >= 1; i--)
902 e = XCNEWVAR (struct small_page_entry, G.small_page_overhead);
903 e->common.page = page + (i << GGC_PAGE_SHIFT);
904 e->common.zone = zone;
905 e->next = f;
906 f = e;
907 set_page_table_entry (e->common.page, &e->common);
910 zone->free_pages = f;
912 entry = XCNEWVAR (struct small_page_entry, G.small_page_overhead);
913 entry->common.page = page;
914 entry->common.zone = zone;
915 set_page_table_entry (page, &entry->common);
918 zone->n_small_pages++;
920 if (GGC_DEBUG_LEVEL >= 2)
921 fprintf (G.debug_file,
922 "Allocating %s page at %p, data %p-%p\n",
923 entry->common.zone->name, (PTR) entry, entry->common.page,
924 entry->common.page + SMALL_PAGE_SIZE - 1);
926 return entry;
929 /* Allocate a large page of size SIZE in ZONE. */
931 static struct large_page_entry *
932 alloc_large_page (size_t size, struct alloc_zone *zone)
934 struct large_page_entry *entry;
935 char *page;
936 size_t needed_size;
938 needed_size = size + sizeof (struct large_page_entry);
939 page = XNEWVAR (char, needed_size);
941 entry = (struct large_page_entry *) page;
943 entry->next = NULL;
944 entry->common.page = page + sizeof (struct large_page_entry);
945 entry->common.large_p = true;
946 entry->common.pch_p = false;
947 entry->common.zone = zone;
948 #ifdef GATHER_STATISTICS
949 entry->common.survived = 0;
950 #endif
951 entry->mark_p = false;
952 entry->bytes = size;
953 entry->prev = NULL;
955 set_page_table_entry (entry->common.page, &entry->common);
957 if (GGC_DEBUG_LEVEL >= 2)
958 fprintf (G.debug_file,
959 "Allocating %s large page at %p, data %p-%p\n",
960 entry->common.zone->name, (PTR) entry, entry->common.page,
961 entry->common.page + SMALL_PAGE_SIZE - 1);
963 return entry;
967 /* For a page that is no longer needed, put it on the free page list. */
969 static inline void
970 free_small_page (struct small_page_entry *entry)
972 if (GGC_DEBUG_LEVEL >= 2)
973 fprintf (G.debug_file,
974 "Deallocating %s page at %p, data %p-%p\n",
975 entry->common.zone->name, (PTR) entry,
976 entry->common.page, entry->common.page + SMALL_PAGE_SIZE - 1);
978 gcc_assert (!entry->common.large_p);
980 /* Mark the page as inaccessible. Discard the handle to
981 avoid handle leak. */
982 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (entry->common.page,
983 SMALL_PAGE_SIZE));
985 entry->next = entry->common.zone->free_pages;
986 entry->common.zone->free_pages = entry;
987 entry->common.zone->n_small_pages--;
990 /* Release a large page that is no longer needed. */
992 static inline void
993 free_large_page (struct large_page_entry *entry)
995 if (GGC_DEBUG_LEVEL >= 2)
996 fprintf (G.debug_file,
997 "Deallocating %s page at %p, data %p-%p\n",
998 entry->common.zone->name, (PTR) entry,
999 entry->common.page, entry->common.page + SMALL_PAGE_SIZE - 1);
1001 gcc_assert (entry->common.large_p);
1003 set_page_table_entry (entry->common.page, NULL);
1004 free (entry);
1007 /* Release the free page cache to the system. */
1009 static void
1010 release_pages (struct alloc_zone *zone)
1012 #ifdef USING_MMAP
1013 struct small_page_entry *p, *next;
1014 char *start;
1015 size_t len;
1017 /* Gather up adjacent pages so they are unmapped together. */
1018 p = zone->free_pages;
1020 while (p)
1022 start = p->common.page;
1023 next = p->next;
1024 len = SMALL_PAGE_SIZE;
1025 set_page_table_entry (p->common.page, NULL);
1026 p = next;
1028 while (p && p->common.page == start + len)
1030 next = p->next;
1031 len += SMALL_PAGE_SIZE;
1032 set_page_table_entry (p->common.page, NULL);
1033 p = next;
1036 munmap (start, len);
1037 zone->bytes_mapped -= len;
1040 zone->free_pages = NULL;
1041 #endif
1044 /* Place the block at PTR of size SIZE on the free list for ZONE. */
1046 static inline void
1047 free_chunk (char *ptr, size_t size, struct alloc_zone *zone)
1049 struct alloc_chunk *chunk = (struct alloc_chunk *) ptr;
1050 size_t bin = 0;
1052 bin = SIZE_BIN_DOWN (size);
1053 gcc_assert (bin != 0);
1054 if (bin > NUM_FREE_BINS)
1056 bin = 0;
1057 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (chunk,
1058 sizeof (struct
1059 alloc_chunk)));
1060 chunk->size = size;
1061 chunk->next_free = zone->free_chunks[bin];
1062 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (ptr
1063 + sizeof (struct
1064 alloc_chunk),
1065 size
1066 - sizeof (struct
1067 alloc_chunk)));
1069 else
1071 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (chunk,
1072 sizeof (struct
1073 alloc_chunk *)));
1074 chunk->next_free = zone->free_chunks[bin];
1075 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (ptr
1076 + sizeof (struct
1077 alloc_chunk *),
1078 size
1079 - sizeof (struct
1080 alloc_chunk *)));
1083 zone->free_chunks[bin] = chunk;
1084 if (bin > zone->high_free_bin)
1085 zone->high_free_bin = bin;
1086 if (GGC_DEBUG_LEVEL >= 3)
1087 fprintf (G.debug_file, "Deallocating object, chunk=%p\n", (void *)chunk);
1090 /* Allocate a chunk of memory of at least ORIG_SIZE bytes, in ZONE. */
1092 void *
1093 ggc_internal_alloc_zone_stat (size_t orig_size, struct alloc_zone *zone
1094 MEM_STAT_DECL)
1096 size_t bin;
1097 size_t csize;
1098 struct small_page_entry *entry;
1099 struct alloc_chunk *chunk, **pp;
1100 void *result;
1101 size_t size = orig_size;
1103 /* Make sure that zero-sized allocations get a unique and freeable
1104 pointer. */
1105 if (size == 0)
1106 size = MAX_ALIGNMENT;
1107 else
1108 size = (size + MAX_ALIGNMENT - 1) & -MAX_ALIGNMENT;
1110 /* Try to allocate the object from several different sources. Each
1111 of these cases is responsible for setting RESULT and SIZE to
1112 describe the allocated block, before jumping to FOUND. If a
1113 chunk is split, the allocate bit for the new chunk should also be
1114 set.
1116 Large objects are handled specially. However, they'll just fail
1117 the next couple of conditions, so we can wait to check for them
1118 below. The large object case is relatively rare (< 1%), so this
1119 is a win. */
1121 /* First try to split the last chunk we allocated. For best
1122 fragmentation behavior it would be better to look for a
1123 free bin of the appropriate size for a small object. However,
1124 we're unlikely (1% - 7%) to find one, and this gives better
1125 locality behavior anyway. This case handles the lion's share
1126 of all calls to this function. */
1127 if (size <= zone->cached_free_size)
1129 result = zone->cached_free;
1131 zone->cached_free_size -= size;
1132 if (zone->cached_free_size)
1134 zone->cached_free += size;
1135 zone_set_object_alloc_bit (zone->cached_free);
1138 goto found;
1141 /* Next, try to find a free bin of the exactly correct size. */
1143 /* We want to round SIZE up, rather than down, but we know it's
1144 already aligned to at least FREE_BIN_DELTA, so we can just
1145 shift. */
1146 bin = SIZE_BIN_DOWN (size);
1148 if (bin <= NUM_FREE_BINS
1149 && (chunk = zone->free_chunks[bin]) != NULL)
1151 /* We have a chunk of the right size. Pull it off the free list
1152 and use it. */
1154 zone->free_chunks[bin] = chunk->next_free;
1156 /* NOTE: SIZE is only guaranteed to be right if MAX_ALIGNMENT
1157 == FREE_BIN_DELTA. */
1158 result = chunk;
1160 /* The allocation bits are already set correctly. HIGH_FREE_BIN
1161 may now be wrong, if this was the last chunk in the high bin.
1162 Rather than fixing it up now, wait until we need to search
1163 the free bins. */
1165 goto found;
1168 /* Next, if there wasn't a chunk of the ideal size, look for a chunk
1169 to split. We can find one in the too-big bin, or in the largest
1170 sized bin with a chunk in it. Try the largest normal-sized bin
1171 first. */
1173 if (zone->high_free_bin > bin)
1175 /* Find the highest numbered free bin. It will be at or below
1176 the watermark. */
1177 while (zone->high_free_bin > bin
1178 && zone->free_chunks[zone->high_free_bin] == NULL)
1179 zone->high_free_bin--;
1181 if (zone->high_free_bin > bin)
1183 size_t tbin = zone->high_free_bin;
1184 chunk = zone->free_chunks[tbin];
1186 /* Remove the chunk from its previous bin. */
1187 zone->free_chunks[tbin] = chunk->next_free;
1189 result = (char *) chunk;
1191 /* Save the rest of the chunk for future allocation. */
1192 if (zone->cached_free_size)
1193 free_chunk (zone->cached_free, zone->cached_free_size, zone);
1195 chunk = (struct alloc_chunk *) ((char *) result + size);
1196 zone->cached_free = (char *) chunk;
1197 zone->cached_free_size = (tbin - bin) * FREE_BIN_DELTA;
1199 /* Mark the new free chunk as an object, so that we can
1200 find the size of the newly allocated object. */
1201 zone_set_object_alloc_bit (chunk);
1203 /* HIGH_FREE_BIN may now be wrong, if this was the last
1204 chunk in the high bin. Rather than fixing it up now,
1205 wait until we need to search the free bins. */
1207 goto found;
1211 /* Failing that, look through the "other" bucket for a chunk
1212 that is large enough. */
1213 pp = &(zone->free_chunks[0]);
1214 chunk = *pp;
1215 while (chunk && chunk->size < size)
1217 pp = &chunk->next_free;
1218 chunk = *pp;
1221 if (chunk)
1223 /* Remove the chunk from its previous bin. */
1224 *pp = chunk->next_free;
1226 result = (char *) chunk;
1228 /* Save the rest of the chunk for future allocation, if there's any
1229 left over. */
1230 csize = chunk->size;
1231 if (csize > size)
1233 if (zone->cached_free_size)
1234 free_chunk (zone->cached_free, zone->cached_free_size, zone);
1236 chunk = (struct alloc_chunk *) ((char *) result + size);
1237 zone->cached_free = (char *) chunk;
1238 zone->cached_free_size = csize - size;
1240 /* Mark the new free chunk as an object. */
1241 zone_set_object_alloc_bit (chunk);
1244 goto found;
1247 /* Handle large allocations. We could choose any threshold between
1248 GGC_PAGE_SIZE - sizeof (struct large_page_entry) and
1249 GGC_PAGE_SIZE. It can't be smaller, because then it wouldn't
1250 be guaranteed to have a unique entry in the lookup table. Large
1251 allocations will always fall through to here. */
1252 if (size > GGC_PAGE_SIZE)
1254 struct large_page_entry *entry = alloc_large_page (size, zone);
1256 #ifdef GATHER_STATISTICS
1257 entry->common.survived = 0;
1258 #endif
1260 entry->next = zone->large_pages;
1261 if (zone->large_pages)
1262 zone->large_pages->prev = entry;
1263 zone->large_pages = entry;
1265 result = entry->common.page;
1267 goto found;
1270 /* Failing everything above, allocate a new small page. */
1272 entry = alloc_small_page (zone);
1273 entry->next = zone->pages;
1274 zone->pages = entry;
1276 /* Mark the first chunk in the new page. */
1277 entry->alloc_bits[0] = 1;
1279 result = entry->common.page;
1280 if (size < SMALL_PAGE_SIZE)
1282 if (zone->cached_free_size)
1283 free_chunk (zone->cached_free, zone->cached_free_size, zone);
1285 zone->cached_free = (char *) result + size;
1286 zone->cached_free_size = SMALL_PAGE_SIZE - size;
1288 /* Mark the new free chunk as an object. */
1289 zone_set_object_alloc_bit (zone->cached_free);
1292 found:
1294 /* We could save TYPE in the chunk, but we don't use that for
1295 anything yet. If we wanted to, we could do it by adding it
1296 either before the beginning of the chunk or after its end,
1297 and adjusting the size and pointer appropriately. */
1299 /* We'll probably write to this after we return. */
1300 prefetchw (result);
1302 #ifdef ENABLE_GC_CHECKING
1303 /* `Poison' the entire allocated object. */
1304 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, size));
1305 memset (result, 0xaf, size);
1306 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (result + orig_size,
1307 size - orig_size));
1308 #endif
1310 /* Tell Valgrind that the memory is there, but its content isn't
1311 defined. The bytes at the end of the object are still marked
1312 unaccessible. */
1313 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, orig_size));
1315 /* Keep track of how many bytes are being allocated. This
1316 information is used in deciding when to collect. */
1317 zone->allocated += size;
1319 timevar_ggc_mem_total += size;
1321 #ifdef GATHER_STATISTICS
1322 ggc_record_overhead (orig_size, size - orig_size, result PASS_MEM_STAT);
1325 size_t object_size = size;
1326 size_t overhead = object_size - orig_size;
1328 zone->stats.total_overhead += overhead;
1329 zone->stats.total_allocated += object_size;
1331 if (orig_size <= 32)
1333 zone->stats.total_overhead_under32 += overhead;
1334 zone->stats.total_allocated_under32 += object_size;
1336 if (orig_size <= 64)
1338 zone->stats.total_overhead_under64 += overhead;
1339 zone->stats.total_allocated_under64 += object_size;
1341 if (orig_size <= 128)
1343 zone->stats.total_overhead_under128 += overhead;
1344 zone->stats.total_allocated_under128 += object_size;
1347 #endif
1349 if (GGC_DEBUG_LEVEL >= 3)
1350 fprintf (G.debug_file, "Allocating object, size=%lu at %p\n",
1351 (unsigned long) size, result);
1353 return result;
1356 #define ggc_internal_alloc_zone_pass_stat(s,z) \
1357 ggc_internal_alloc_zone_stat (s,z PASS_MEM_STAT)
1359 void *
1360 ggc_internal_cleared_alloc_zone_stat (size_t orig_size,
1361 struct alloc_zone *zone MEM_STAT_DECL)
1363 void * result = ggc_internal_alloc_zone_pass_stat (orig_size, zone);
1364 memset (result, 0, orig_size);
1365 return result;
1369 /* Allocate a SIZE of chunk memory of GTE type, into an appropriate zone
1370 for that type. */
1372 void *
1373 ggc_alloc_typed_stat (enum gt_types_enum gte, size_t size
1374 MEM_STAT_DECL)
1376 switch (gte)
1378 case gt_ggc_e_14lang_tree_node:
1379 return ggc_internal_alloc_zone_pass_stat (size, &tree_zone);
1381 case gt_ggc_e_7rtx_def:
1382 return ggc_internal_alloc_zone_pass_stat (size, &rtl_zone);
1384 case gt_ggc_e_9rtvec_def:
1385 return ggc_internal_alloc_zone_pass_stat (size, &rtl_zone);
1387 default:
1388 return ggc_internal_alloc_zone_pass_stat (size, &main_zone);
1392 /* Normal GC allocation simply allocates into the main zone. */
1394 void *
1395 ggc_internal_alloc_stat (size_t size MEM_STAT_DECL)
1397 return ggc_internal_alloc_zone_pass_stat (size, &main_zone);
1400 /* Poison the chunk. */
1401 #ifdef ENABLE_GC_CHECKING
1402 #define poison_region(PTR, SIZE) \
1403 do { \
1404 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED ((PTR), (SIZE))); \
1405 memset ((PTR), 0xa5, (SIZE)); \
1406 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((PTR), (SIZE))); \
1407 } while (0)
1408 #else
1409 #define poison_region(PTR, SIZE)
1410 #endif
1412 /* Free the object at P. */
1414 void
1415 ggc_free (void *p)
1417 struct page_entry *page;
1419 #ifdef GATHER_STATISTICS
1420 ggc_free_overhead (p);
1421 #endif
1423 poison_region (p, ggc_get_size (p));
1425 page = zone_get_object_page (p);
1427 if (page->large_p)
1429 struct large_page_entry *large_page
1430 = (struct large_page_entry *) page;
1432 /* Remove the page from the linked list. */
1433 if (large_page->prev)
1434 large_page->prev->next = large_page->next;
1435 else
1437 gcc_assert (large_page->common.zone->large_pages == large_page);
1438 large_page->common.zone->large_pages = large_page->next;
1440 if (large_page->next)
1441 large_page->next->prev = large_page->prev;
1443 large_page->common.zone->allocated -= large_page->bytes;
1445 /* Release the memory associated with this object. */
1446 free_large_page (large_page);
1448 else if (page->pch_p)
1449 /* Don't do anything. We won't allocate a new object from the
1450 PCH zone so there's no point in releasing anything. */
1452 else
1454 size_t size = ggc_get_size (p);
1456 page->zone->allocated -= size;
1458 /* Add the chunk to the free list. We don't bother with coalescing,
1459 since we are likely to want a chunk of this size again. */
1460 free_chunk ((char *)p, size, page->zone);
1464 /* Mark function for strings. */
1466 void
1467 gt_ggc_m_S (const void *p)
1469 page_entry *entry;
1470 unsigned long offset;
1472 if (!p)
1473 return;
1475 /* Look up the page on which the object is alloced. . */
1476 entry = lookup_page_table_if_allocated (p);
1477 if (! entry)
1478 return;
1480 if (entry->pch_p)
1482 size_t alloc_word, alloc_bit, t;
1483 t = ((const char *) p - pch_zone.page) / BYTES_PER_ALLOC_BIT;
1484 alloc_word = t / (8 * sizeof (alloc_type));
1485 alloc_bit = t % (8 * sizeof (alloc_type));
1486 offset = zone_find_object_offset (pch_zone.alloc_bits, alloc_word,
1487 alloc_bit);
1489 else if (entry->large_p)
1491 struct large_page_entry *le = (struct large_page_entry *) entry;
1492 offset = ((const char *) p) - entry->page;
1493 gcc_assert (offset < le->bytes);
1495 else
1497 struct small_page_entry *se = (struct small_page_entry *) entry;
1498 unsigned int start_word = zone_get_object_alloc_word (p);
1499 unsigned int start_bit = zone_get_object_alloc_bit (p);
1500 offset = zone_find_object_offset (se->alloc_bits, start_word, start_bit);
1502 /* On some platforms a char* will not necessarily line up on an
1503 allocation boundary, so we have to update the offset to
1504 account for the leftover bytes. */
1505 offset += (size_t) p % BYTES_PER_ALLOC_BIT;
1508 if (offset)
1510 /* Here we've seen a char* which does not point to the beginning
1511 of an allocated object. We assume it points to the middle of
1512 a STRING_CST. */
1513 gcc_assert (offset == offsetof (struct tree_string, str));
1514 p = ((const char *) p) - offset;
1515 gt_ggc_mx_lang_tree_node (CONST_CAST(void *, p));
1516 return;
1519 /* Inefficient, but also unlikely to matter. */
1520 ggc_set_mark (p);
1523 /* If P is not marked, mark it and return false. Otherwise return true.
1524 P must have been allocated by the GC allocator; it mustn't point to
1525 static objects, stack variables, or memory allocated with malloc. */
1528 ggc_set_mark (const void *p)
1530 struct page_entry *page;
1531 const char *ptr = (const char *) p;
1533 page = zone_get_object_page (p);
1535 if (page->pch_p)
1537 size_t mark_word, mark_bit, offset;
1538 offset = (ptr - pch_zone.page) / BYTES_PER_MARK_BIT;
1539 mark_word = offset / (8 * sizeof (mark_type));
1540 mark_bit = offset % (8 * sizeof (mark_type));
1542 if (pch_zone.mark_bits[mark_word] & (1 << mark_bit))
1543 return 1;
1544 pch_zone.mark_bits[mark_word] |= (1 << mark_bit);
1546 else if (page->large_p)
1548 struct large_page_entry *large_page
1549 = (struct large_page_entry *) page;
1551 if (large_page->mark_p)
1552 return 1;
1553 large_page->mark_p = true;
1555 else
1557 struct small_page_entry *small_page
1558 = (struct small_page_entry *) page;
1560 if (small_page->mark_bits[zone_get_object_mark_word (p)]
1561 & (1 << zone_get_object_mark_bit (p)))
1562 return 1;
1563 small_page->mark_bits[zone_get_object_mark_word (p)]
1564 |= (1 << zone_get_object_mark_bit (p));
1567 if (GGC_DEBUG_LEVEL >= 4)
1568 fprintf (G.debug_file, "Marking %p\n", p);
1570 return 0;
1573 /* Return 1 if P has been marked, zero otherwise.
1574 P must have been allocated by the GC allocator; it mustn't point to
1575 static objects, stack variables, or memory allocated with malloc. */
1578 ggc_marked_p (const void *p)
1580 struct page_entry *page;
1581 const char *ptr = (const char *) p;
1583 page = zone_get_object_page (p);
1585 if (page->pch_p)
1587 size_t mark_word, mark_bit, offset;
1588 offset = (ptr - pch_zone.page) / BYTES_PER_MARK_BIT;
1589 mark_word = offset / (8 * sizeof (mark_type));
1590 mark_bit = offset % (8 * sizeof (mark_type));
1592 return (pch_zone.mark_bits[mark_word] & (1 << mark_bit)) != 0;
1595 if (page->large_p)
1597 struct large_page_entry *large_page
1598 = (struct large_page_entry *) page;
1600 return large_page->mark_p;
1602 else
1604 struct small_page_entry *small_page
1605 = (struct small_page_entry *) page;
1607 return 0 != (small_page->mark_bits[zone_get_object_mark_word (p)]
1608 & (1 << zone_get_object_mark_bit (p)));
1612 /* Return the size of the gc-able object P. */
1614 size_t
1615 ggc_get_size (const void *p)
1617 struct page_entry *page;
1618 const char *ptr = (const char *) p;
1620 page = zone_get_object_page (p);
1622 if (page->pch_p)
1624 size_t alloc_word, alloc_bit, offset, max_size;
1625 offset = (ptr - pch_zone.page) / BYTES_PER_ALLOC_BIT + 1;
1626 alloc_word = offset / (8 * sizeof (alloc_type));
1627 alloc_bit = offset % (8 * sizeof (alloc_type));
1628 max_size = pch_zone.bytes - (ptr - pch_zone.page);
1629 return zone_object_size_1 (pch_zone.alloc_bits, alloc_word, alloc_bit,
1630 max_size);
1633 if (page->large_p)
1634 return ((struct large_page_entry *)page)->bytes;
1635 else
1636 return zone_find_object_size ((struct small_page_entry *) page, p);
1639 /* Initialize the ggc-zone-mmap allocator. */
1640 void
1641 init_ggc (void)
1643 /* The allocation size must be greater than BYTES_PER_MARK_BIT, and
1644 a multiple of both BYTES_PER_ALLOC_BIT and FREE_BIN_DELTA, for
1645 the current assumptions to hold. */
1647 gcc_assert (FREE_BIN_DELTA == MAX_ALIGNMENT);
1649 /* Set up the main zone by hand. */
1650 main_zone.name = "Main zone";
1651 G.zones = &main_zone;
1653 /* Allocate the default zones. */
1654 new_ggc_zone_1 (&rtl_zone, "RTL zone");
1655 new_ggc_zone_1 (&tree_zone, "Tree zone");
1656 new_ggc_zone_1 (&tree_id_zone, "Tree identifier zone");
1658 G.pagesize = getpagesize();
1659 G.lg_pagesize = exact_log2 (G.pagesize);
1660 G.page_mask = ~(G.pagesize - 1);
1662 /* Require the system page size to be a multiple of GGC_PAGE_SIZE. */
1663 gcc_assert ((G.pagesize & (GGC_PAGE_SIZE - 1)) == 0);
1665 /* Allocate 16 system pages at a time. */
1666 G.quire_size = 16 * G.pagesize / GGC_PAGE_SIZE;
1668 /* Calculate the size of the allocation bitmap and other overhead. */
1669 /* Right now we allocate bits for the page header and bitmap. These
1670 are wasted, but a little tricky to eliminate. */
1671 G.small_page_overhead
1672 = PAGE_OVERHEAD + (GGC_PAGE_SIZE / BYTES_PER_ALLOC_BIT / 8);
1673 /* G.small_page_overhead = ROUND_UP (G.small_page_overhead, MAX_ALIGNMENT); */
1675 #ifdef HAVE_MMAP_DEV_ZERO
1676 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1677 gcc_assert (G.dev_zero_fd != -1);
1678 #endif
1680 #if 0
1681 G.debug_file = fopen ("ggc-mmap.debug", "w");
1682 setlinebuf (G.debug_file);
1683 #else
1684 G.debug_file = stdout;
1685 #endif
1687 #ifdef USING_MMAP
1688 /* StunOS has an amazing off-by-one error for the first mmap allocation
1689 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1690 believe, is an unaligned page allocation, which would cause us to
1691 hork badly if we tried to use it. */
1693 char *p = alloc_anon (NULL, G.pagesize, &main_zone);
1694 struct small_page_entry *e;
1695 if ((size_t)p & (G.pagesize - 1))
1697 /* How losing. Discard this one and try another. If we still
1698 can't get something useful, give up. */
1700 p = alloc_anon (NULL, G.pagesize, &main_zone);
1701 gcc_assert (!((size_t)p & (G.pagesize - 1)));
1704 if (GGC_PAGE_SIZE == G.pagesize)
1706 /* We have a good page, might as well hold onto it... */
1707 e = XCNEWVAR (struct small_page_entry, G.small_page_overhead);
1708 e->common.page = p;
1709 e->common.zone = &main_zone;
1710 e->next = main_zone.free_pages;
1711 set_page_table_entry (e->common.page, &e->common);
1712 main_zone.free_pages = e;
1714 else
1716 munmap (p, G.pagesize);
1719 #endif
1722 /* Start a new GGC zone. */
1724 static void
1725 new_ggc_zone_1 (struct alloc_zone *new_zone, const char * name)
1727 new_zone->name = name;
1728 new_zone->next_zone = G.zones->next_zone;
1729 G.zones->next_zone = new_zone;
1732 /* Free all empty pages and objects within a page for a given zone */
1734 static void
1735 sweep_pages (struct alloc_zone *zone)
1737 struct large_page_entry **lpp, *lp, *lnext;
1738 struct small_page_entry **spp, *sp, *snext;
1739 char *last_free;
1740 size_t allocated = 0;
1741 bool nomarksinpage;
1743 /* First, reset the free_chunks lists, since we are going to
1744 re-free free chunks in hopes of coalescing them into large chunks. */
1745 memset (zone->free_chunks, 0, sizeof (zone->free_chunks));
1746 zone->high_free_bin = 0;
1747 zone->cached_free = NULL;
1748 zone->cached_free_size = 0;
1750 /* Large pages are all or none affairs. Either they are completely
1751 empty, or they are completely full. */
1752 lpp = &zone->large_pages;
1753 for (lp = zone->large_pages; lp != NULL; lp = lnext)
1755 gcc_assert (lp->common.large_p);
1757 lnext = lp->next;
1759 #ifdef GATHER_STATISTICS
1760 /* This page has now survived another collection. */
1761 lp->common.survived++;
1762 #endif
1764 if (lp->mark_p)
1766 lp->mark_p = false;
1767 allocated += lp->bytes;
1768 lpp = &lp->next;
1770 else
1772 *lpp = lnext;
1773 #ifdef ENABLE_GC_CHECKING
1774 /* Poison the page. */
1775 memset (lp->common.page, 0xb5, SMALL_PAGE_SIZE);
1776 #endif
1777 if (lp->prev)
1778 lp->prev->next = lp->next;
1779 if (lp->next)
1780 lp->next->prev = lp->prev;
1781 free_large_page (lp);
1785 spp = &zone->pages;
1786 for (sp = zone->pages; sp != NULL; sp = snext)
1788 char *object, *last_object;
1789 char *end;
1790 alloc_type *alloc_word_p;
1791 mark_type *mark_word_p;
1793 gcc_assert (!sp->common.large_p);
1795 snext = sp->next;
1797 #ifdef GATHER_STATISTICS
1798 /* This page has now survived another collection. */
1799 sp->common.survived++;
1800 #endif
1802 /* Step through all chunks, consolidate those that are free and
1803 insert them into the free lists. Note that consolidation
1804 slows down collection slightly. */
1806 last_object = object = sp->common.page;
1807 end = sp->common.page + SMALL_PAGE_SIZE;
1808 last_free = NULL;
1809 nomarksinpage = true;
1810 mark_word_p = sp->mark_bits;
1811 alloc_word_p = sp->alloc_bits;
1813 gcc_assert (BYTES_PER_ALLOC_BIT == BYTES_PER_MARK_BIT);
1815 object = sp->common.page;
1818 unsigned int i, n;
1819 alloc_type alloc_word;
1820 mark_type mark_word;
1822 alloc_word = *alloc_word_p++;
1823 mark_word = *mark_word_p++;
1825 if (mark_word)
1826 nomarksinpage = false;
1828 /* There ought to be some way to do this without looping... */
1829 i = 0;
1830 while ((n = alloc_ffs (alloc_word)) != 0)
1832 /* Extend the current state for n - 1 bits. We can't
1833 shift alloc_word by n, even though it isn't used in the
1834 loop, in case only the highest bit was set. */
1835 alloc_word >>= n - 1;
1836 mark_word >>= n - 1;
1837 object += BYTES_PER_MARK_BIT * (n - 1);
1839 if (mark_word & 1)
1841 if (last_free)
1843 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (last_free,
1844 object
1845 - last_free));
1846 poison_region (last_free, object - last_free);
1847 free_chunk (last_free, object - last_free, zone);
1848 last_free = NULL;
1850 else
1851 allocated += object - last_object;
1852 last_object = object;
1854 else
1856 if (last_free == NULL)
1858 last_free = object;
1859 allocated += object - last_object;
1861 else
1862 zone_clear_object_alloc_bit (sp, object);
1865 /* Shift to just after the alloc bit we handled. */
1866 alloc_word >>= 1;
1867 mark_word >>= 1;
1868 object += BYTES_PER_MARK_BIT;
1870 i += n;
1873 object += BYTES_PER_MARK_BIT * (8 * sizeof (alloc_type) - i);
1875 while (object < end);
1877 if (nomarksinpage)
1879 *spp = snext;
1880 #ifdef ENABLE_GC_CHECKING
1881 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (sp->common.page,
1882 SMALL_PAGE_SIZE));
1883 /* Poison the page. */
1884 memset (sp->common.page, 0xb5, SMALL_PAGE_SIZE);
1885 #endif
1886 free_small_page (sp);
1887 continue;
1889 else if (last_free)
1891 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (last_free,
1892 object - last_free));
1893 poison_region (last_free, object - last_free);
1894 free_chunk (last_free, object - last_free, zone);
1896 else
1897 allocated += object - last_object;
1899 spp = &sp->next;
1902 zone->allocated = allocated;
1905 /* mark-and-sweep routine for collecting a single zone. NEED_MARKING
1906 is true if we need to mark before sweeping, false if some other
1907 zone collection has already performed marking for us. Returns true
1908 if we collected, false otherwise. */
1910 static bool
1911 ggc_collect_1 (struct alloc_zone *zone, bool need_marking)
1913 #if 0
1914 /* */
1916 int i;
1917 for (i = 0; i < NUM_FREE_BINS + 1; i++)
1919 struct alloc_chunk *chunk;
1920 int n, tot;
1922 n = 0;
1923 tot = 0;
1924 chunk = zone->free_chunks[i];
1925 while (chunk)
1927 n++;
1928 tot += chunk->size;
1929 chunk = chunk->next_free;
1931 fprintf (stderr, "Bin %d: %d free chunks (%d bytes)\n",
1932 i, n, tot);
1935 /* */
1936 #endif
1938 if (!quiet_flag)
1939 fprintf (stderr, " {%s GC %luk -> ",
1940 zone->name, (unsigned long) zone->allocated / 1024);
1942 /* Zero the total allocated bytes. This will be recalculated in the
1943 sweep phase. */
1944 zone->allocated = 0;
1946 /* Release the pages we freed the last time we collected, but didn't
1947 reuse in the interim. */
1948 release_pages (zone);
1950 if (need_marking)
1952 zone_allocate_marks ();
1953 ggc_mark_roots ();
1954 #ifdef GATHER_STATISTICS
1955 ggc_prune_overhead_list ();
1956 #endif
1959 sweep_pages (zone);
1960 zone->was_collected = true;
1961 zone->allocated_last_gc = zone->allocated;
1963 if (!quiet_flag)
1964 fprintf (stderr, "%luk}", (unsigned long) zone->allocated / 1024);
1965 return true;
1968 #ifdef GATHER_STATISTICS
1969 /* Calculate the average page survival rate in terms of number of
1970 collections. */
1972 static float
1973 calculate_average_page_survival (struct alloc_zone *zone)
1975 float count = 0.0;
1976 float survival = 0.0;
1977 struct small_page_entry *p;
1978 struct large_page_entry *lp;
1979 for (p = zone->pages; p; p = p->next)
1981 count += 1.0;
1982 survival += p->common.survived;
1984 for (lp = zone->large_pages; lp; lp = lp->next)
1986 count += 1.0;
1987 survival += lp->common.survived;
1989 return survival/count;
1991 #endif
1993 /* Top level collection routine. */
1995 void
1996 ggc_collect (void)
1998 struct alloc_zone *zone;
1999 bool marked = false;
2001 timevar_push (TV_GC);
2003 if (!ggc_force_collect)
2005 float allocated_last_gc = 0, allocated = 0, min_expand;
2007 for (zone = G.zones; zone; zone = zone->next_zone)
2009 allocated_last_gc += zone->allocated_last_gc;
2010 allocated += zone->allocated;
2013 allocated_last_gc =
2014 MAX (allocated_last_gc,
2015 (size_t) PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
2016 min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
2018 if (allocated < allocated_last_gc + min_expand)
2020 timevar_pop (TV_GC);
2021 return;
2025 invoke_plugin_callbacks (PLUGIN_GGC_START, NULL);
2027 /* Start by possibly collecting the main zone. */
2028 main_zone.was_collected = false;
2029 marked |= ggc_collect_1 (&main_zone, true);
2031 /* In order to keep the number of collections down, we don't
2032 collect other zones unless we are collecting the main zone. This
2033 gives us roughly the same number of collections as we used to
2034 have with the old gc. The number of collection is important
2035 because our main slowdown (according to profiling) is now in
2036 marking. So if we mark twice as often as we used to, we'll be
2037 twice as slow. Hopefully we'll avoid this cost when we mark
2038 zone-at-a-time. */
2039 /* NOTE drow/2004-07-28: We now always collect the main zone, but
2040 keep this code in case the heuristics are further refined. */
2042 if (main_zone.was_collected)
2044 struct alloc_zone *zone;
2046 for (zone = main_zone.next_zone; zone; zone = zone->next_zone)
2048 zone->was_collected = false;
2049 marked |= ggc_collect_1 (zone, !marked);
2053 #ifdef GATHER_STATISTICS
2054 /* Print page survival stats, if someone wants them. */
2055 if (GGC_DEBUG_LEVEL >= 2)
2057 for (zone = G.zones; zone; zone = zone->next_zone)
2059 if (zone->was_collected)
2061 float f = calculate_average_page_survival (zone);
2062 printf ("Average page survival in zone `%s' is %f\n",
2063 zone->name, f);
2067 #endif
2069 if (marked)
2070 zone_free_marks ();
2072 /* Free dead zones. */
2073 for (zone = G.zones; zone && zone->next_zone; zone = zone->next_zone)
2075 if (zone->next_zone->dead)
2077 struct alloc_zone *dead_zone = zone->next_zone;
2079 printf ("Zone `%s' is dead and will be freed.\n", dead_zone->name);
2081 /* The zone must be empty. */
2082 gcc_assert (!dead_zone->allocated);
2084 /* Unchain the dead zone, release all its pages and free it. */
2085 zone->next_zone = zone->next_zone->next_zone;
2086 release_pages (dead_zone);
2087 free (dead_zone);
2091 invoke_plugin_callbacks (PLUGIN_GGC_END, NULL);
2093 timevar_pop (TV_GC);
2096 /* Print allocation statistics. */
2097 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
2098 ? (x) \
2099 : ((x) < 1024*1024*10 \
2100 ? (x) / 1024 \
2101 : (x) / (1024*1024))))
2102 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
2104 void
2105 ggc_print_statistics (void)
2107 struct alloc_zone *zone;
2108 struct ggc_statistics stats;
2109 size_t total_overhead = 0, total_allocated = 0, total_bytes_mapped = 0;
2110 size_t pte_overhead, i;
2112 /* Clear the statistics. */
2113 memset (&stats, 0, sizeof (stats));
2115 /* Make sure collection will really occur. */
2116 ggc_force_collect = true;
2118 /* Collect and print the statistics common across collectors. */
2119 ggc_print_common_statistics (stderr, &stats);
2121 ggc_force_collect = false;
2123 /* Release free pages so that we will not count the bytes allocated
2124 there as part of the total allocated memory. */
2125 for (zone = G.zones; zone; zone = zone->next_zone)
2126 release_pages (zone);
2128 /* Collect some information about the various sizes of
2129 allocation. */
2130 fprintf (stderr,
2131 "Memory still allocated at the end of the compilation process\n");
2133 fprintf (stderr, "%20s %10s %10s %10s\n",
2134 "Zone", "Allocated", "Used", "Overhead");
2135 for (zone = G.zones; zone; zone = zone->next_zone)
2137 struct large_page_entry *large_page;
2138 size_t overhead, allocated, in_use;
2140 /* Skip empty zones. */
2141 if (!zone->pages && !zone->large_pages)
2142 continue;
2144 allocated = in_use = 0;
2146 overhead = sizeof (struct alloc_zone);
2148 for (large_page = zone->large_pages; large_page != NULL;
2149 large_page = large_page->next)
2151 allocated += large_page->bytes;
2152 in_use += large_page->bytes;
2153 overhead += sizeof (struct large_page_entry);
2156 /* There's no easy way to walk through the small pages finding
2157 used and unused objects. Instead, add all the pages, and
2158 subtract out the free list. */
2160 allocated += GGC_PAGE_SIZE * zone->n_small_pages;
2161 in_use += GGC_PAGE_SIZE * zone->n_small_pages;
2162 overhead += G.small_page_overhead * zone->n_small_pages;
2164 for (i = 0; i <= NUM_FREE_BINS; i++)
2166 struct alloc_chunk *chunk = zone->free_chunks[i];
2167 while (chunk)
2169 in_use -= ggc_get_size (chunk);
2170 chunk = chunk->next_free;
2174 fprintf (stderr, "%20s %10lu%c %10lu%c %10lu%c\n",
2175 zone->name,
2176 SCALE (allocated), LABEL (allocated),
2177 SCALE (in_use), LABEL (in_use),
2178 SCALE (overhead), LABEL (overhead));
2180 gcc_assert (in_use == zone->allocated);
2182 total_overhead += overhead;
2183 total_allocated += zone->allocated;
2184 total_bytes_mapped += zone->bytes_mapped;
2187 /* Count the size of the page table as best we can. */
2188 #if HOST_BITS_PER_PTR <= 32
2189 pte_overhead = sizeof (G.lookup);
2190 for (i = 0; i < PAGE_L1_SIZE; i++)
2191 if (G.lookup[i])
2192 pte_overhead += PAGE_L2_SIZE * sizeof (struct page_entry *);
2193 #else
2195 page_table table = G.lookup;
2196 pte_overhead = 0;
2197 while (table)
2199 pte_overhead += sizeof (*table);
2200 for (i = 0; i < PAGE_L1_SIZE; i++)
2201 if (table->table[i])
2202 pte_overhead += PAGE_L2_SIZE * sizeof (struct page_entry *);
2203 table = table->next;
2206 #endif
2207 fprintf (stderr, "%20s %11s %11s %10lu%c\n", "Page Table",
2208 "", "", SCALE (pte_overhead), LABEL (pte_overhead));
2209 total_overhead += pte_overhead;
2211 fprintf (stderr, "%20s %10lu%c %10lu%c %10lu%c\n", "Total",
2212 SCALE (total_bytes_mapped), LABEL (total_bytes_mapped),
2213 SCALE (total_allocated), LABEL(total_allocated),
2214 SCALE (total_overhead), LABEL (total_overhead));
2216 #ifdef GATHER_STATISTICS
2218 unsigned long long all_overhead = 0, all_allocated = 0;
2219 unsigned long long all_overhead_under32 = 0, all_allocated_under32 = 0;
2220 unsigned long long all_overhead_under64 = 0, all_allocated_under64 = 0;
2221 unsigned long long all_overhead_under128 = 0, all_allocated_under128 = 0;
2223 fprintf (stderr, "\nTotal allocations and overheads during the compilation process\n");
2225 for (zone = G.zones; zone; zone = zone->next_zone)
2227 all_overhead += zone->stats.total_overhead;
2228 all_allocated += zone->stats.total_allocated;
2230 all_allocated_under32 += zone->stats.total_allocated_under32;
2231 all_overhead_under32 += zone->stats.total_overhead_under32;
2233 all_allocated_under64 += zone->stats.total_allocated_under64;
2234 all_overhead_under64 += zone->stats.total_overhead_under64;
2236 all_allocated_under128 += zone->stats.total_allocated_under128;
2237 all_overhead_under128 += zone->stats.total_overhead_under128;
2239 fprintf (stderr, "%20s: %10lld\n",
2240 zone->name, zone->stats.total_allocated);
2243 fprintf (stderr, "\n");
2245 fprintf (stderr, "Total Overhead: %10lld\n",
2246 all_overhead);
2247 fprintf (stderr, "Total Allocated: %10lld\n",
2248 all_allocated);
2250 fprintf (stderr, "Total Overhead under 32B: %10lld\n",
2251 all_overhead_under32);
2252 fprintf (stderr, "Total Allocated under 32B: %10lld\n",
2253 all_allocated_under32);
2254 fprintf (stderr, "Total Overhead under 64B: %10lld\n",
2255 all_overhead_under64);
2256 fprintf (stderr, "Total Allocated under 64B: %10lld\n",
2257 all_allocated_under64);
2258 fprintf (stderr, "Total Overhead under 128B: %10lld\n",
2259 all_overhead_under128);
2260 fprintf (stderr, "Total Allocated under 128B: %10lld\n",
2261 all_allocated_under128);
2263 #endif
2266 /* Precompiled header support. */
2268 /* For precompiled headers, we sort objects based on their type. We
2269 also sort various objects into their own buckets; currently this
2270 covers strings and IDENTIFIER_NODE trees. The choices of how
2271 to sort buckets have not yet been tuned. */
2273 #define NUM_PCH_BUCKETS (gt_types_enum_last + 3)
2275 #define OTHER_BUCKET (gt_types_enum_last + 0)
2276 #define IDENTIFIER_BUCKET (gt_types_enum_last + 1)
2277 #define STRING_BUCKET (gt_types_enum_last + 2)
2279 struct ggc_pch_ondisk
2281 size_t total;
2282 size_t type_totals[NUM_PCH_BUCKETS];
2285 struct ggc_pch_data
2287 struct ggc_pch_ondisk d;
2288 size_t base;
2289 size_t orig_base;
2290 size_t alloc_size;
2291 alloc_type *alloc_bits;
2292 size_t type_bases[NUM_PCH_BUCKETS];
2293 size_t start_offset;
2296 /* Initialize the PCH data structure. */
2298 struct ggc_pch_data *
2299 init_ggc_pch (void)
2301 return XCNEW (struct ggc_pch_data);
2304 /* Return which of the page-aligned buckets the object at X, with type
2305 TYPE, should be sorted into in the PCH. Strings will have
2306 IS_STRING set and TYPE will be gt_types_enum_last. Other objects
2307 of unknown type will also have TYPE equal to gt_types_enum_last. */
2309 static int
2310 pch_bucket (void *x, enum gt_types_enum type,
2311 bool is_string)
2313 /* Sort identifiers into their own bucket, to improve locality
2314 when searching the identifier hash table. */
2315 if (type == gt_ggc_e_14lang_tree_node
2316 && TREE_CODE ((tree) x) == IDENTIFIER_NODE)
2317 return IDENTIFIER_BUCKET;
2318 else if (type == gt_types_enum_last)
2320 if (is_string)
2321 return STRING_BUCKET;
2322 return OTHER_BUCKET;
2324 return type;
2327 /* Add the size of object X to the size of the PCH data. */
2329 void
2330 ggc_pch_count_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
2331 size_t size, bool is_string, enum gt_types_enum type)
2333 /* NOTE: Right now we don't need to align up the size of any objects.
2334 Strings can be unaligned, and everything else is allocated to a
2335 MAX_ALIGNMENT boundary already. */
2337 d->d.type_totals[pch_bucket (x, type, is_string)] += size;
2340 /* Return the total size of the PCH data. */
2342 size_t
2343 ggc_pch_total_size (struct ggc_pch_data *d)
2345 int i;
2346 size_t alloc_size, total_size;
2348 total_size = 0;
2349 for (i = 0; i < NUM_PCH_BUCKETS; i++)
2351 d->d.type_totals[i] = ROUND_UP (d->d.type_totals[i], GGC_PAGE_SIZE);
2352 total_size += d->d.type_totals[i];
2354 d->d.total = total_size;
2356 /* Include the size of the allocation bitmap. */
2357 alloc_size = CEIL (d->d.total, BYTES_PER_ALLOC_BIT * 8);
2358 alloc_size = ROUND_UP (alloc_size, MAX_ALIGNMENT);
2359 d->alloc_size = alloc_size;
2361 return d->d.total + alloc_size;
2364 /* Set the base address for the objects in the PCH file. */
2366 void
2367 ggc_pch_this_base (struct ggc_pch_data *d, void *base_)
2369 int i;
2370 size_t base = (size_t) base_;
2372 d->base = d->orig_base = base;
2373 for (i = 0; i < NUM_PCH_BUCKETS; i++)
2375 d->type_bases[i] = base;
2376 base += d->d.type_totals[i];
2379 if (d->alloc_bits == NULL)
2380 d->alloc_bits = XCNEWVAR (alloc_type, d->alloc_size);
2383 /* Allocate a place for object X of size SIZE in the PCH file. */
2385 char *
2386 ggc_pch_alloc_object (struct ggc_pch_data *d, void *x,
2387 size_t size, bool is_string,
2388 enum gt_types_enum type)
2390 size_t alloc_word, alloc_bit;
2391 char *result;
2392 int bucket = pch_bucket (x, type, is_string);
2394 /* Record the start of the object in the allocation bitmap. We
2395 can't assert that the allocation bit is previously clear, because
2396 strings may violate the invariant that they are at least
2397 BYTES_PER_ALLOC_BIT long. This is harmless - ggc_get_size
2398 should not be called for strings. */
2399 alloc_word = ((d->type_bases[bucket] - d->orig_base)
2400 / (8 * sizeof (alloc_type) * BYTES_PER_ALLOC_BIT));
2401 alloc_bit = ((d->type_bases[bucket] - d->orig_base)
2402 / BYTES_PER_ALLOC_BIT) % (8 * sizeof (alloc_type));
2403 d->alloc_bits[alloc_word] |= 1L << alloc_bit;
2405 /* Place the object at the current pointer for this bucket. */
2406 result = (char *) d->type_bases[bucket];
2407 d->type_bases[bucket] += size;
2408 return result;
2411 /* Prepare to write out the PCH data to file F. */
2413 void
2414 ggc_pch_prepare_write (struct ggc_pch_data *d,
2415 FILE *f)
2417 /* We seek around a lot while writing. Record where the end
2418 of the padding in the PCH file is, so that we can
2419 locate each object's offset. */
2420 d->start_offset = ftell (f);
2423 /* Write out object X of SIZE to file F. */
2425 void
2426 ggc_pch_write_object (struct ggc_pch_data *d,
2427 FILE *f, void *x, void *newx,
2428 size_t size, bool is_string ATTRIBUTE_UNUSED)
2430 if (fseek (f, (size_t) newx - d->orig_base + d->start_offset, SEEK_SET) != 0)
2431 fatal_error ("can't seek PCH file: %m");
2433 if (fwrite (x, size, 1, f) != 1)
2434 fatal_error ("can't write PCH file: %m");
2437 void
2438 ggc_pch_finish (struct ggc_pch_data *d, FILE *f)
2440 /* Write out the allocation bitmap. */
2441 if (fseek (f, d->start_offset + d->d.total, SEEK_SET) != 0)
2442 fatal_error ("can't seek PCH file: %m");
2444 if (fwrite (d->alloc_bits, d->alloc_size, 1, f) != 1)
2445 fatal_error ("can't write PCH file: %m");
2447 /* Done with the PCH, so write out our footer. */
2448 if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
2449 fatal_error ("can't write PCH file: %m");
2451 free (d->alloc_bits);
2452 free (d);
2455 /* The PCH file from F has been mapped at ADDR. Read in any
2456 additional data from the file and set up the GC state. */
2458 void
2459 ggc_pch_read (FILE *f, void *addr)
2461 struct ggc_pch_ondisk d;
2462 size_t alloc_size;
2463 struct alloc_zone *zone;
2464 struct page_entry *pch_page;
2465 char *p;
2467 if (fread (&d, sizeof (d), 1, f) != 1)
2468 fatal_error ("can't read PCH file: %m");
2470 alloc_size = CEIL (d.total, BYTES_PER_ALLOC_BIT * 8);
2471 alloc_size = ROUND_UP (alloc_size, MAX_ALIGNMENT);
2473 pch_zone.bytes = d.total;
2474 pch_zone.alloc_bits = (alloc_type *) ((char *) addr + pch_zone.bytes);
2475 pch_zone.page = (char *) addr;
2476 pch_zone.end = (char *) pch_zone.alloc_bits;
2478 /* We've just read in a PCH file. So, every object that used to be
2479 allocated is now free. */
2480 #ifdef 0 && GATHER_STATISTICS
2481 zone_allocate_marks ();
2482 ggc_prune_overhead_list ();
2483 zone_free_marks ();
2484 #endif
2486 for (zone = G.zones; zone; zone = zone->next_zone)
2488 struct small_page_entry *page, *next_page;
2489 struct large_page_entry *large_page, *next_large_page;
2491 zone->allocated = 0;
2493 /* Clear the zone's free chunk list. */
2494 memset (zone->free_chunks, 0, sizeof (zone->free_chunks));
2495 zone->high_free_bin = 0;
2496 zone->cached_free = NULL;
2497 zone->cached_free_size = 0;
2499 /* Move all the small pages onto the free list. */
2500 for (page = zone->pages; page != NULL; page = next_page)
2502 next_page = page->next;
2503 memset (page->alloc_bits, 0,
2504 G.small_page_overhead - PAGE_OVERHEAD);
2505 free_small_page (page);
2508 /* Discard all the large pages. */
2509 for (large_page = zone->large_pages; large_page != NULL;
2510 large_page = next_large_page)
2512 next_large_page = large_page->next;
2513 free_large_page (large_page);
2516 zone->pages = NULL;
2517 zone->large_pages = NULL;
2520 /* Allocate the dummy page entry for the PCH, and set all pages
2521 mapped into the PCH to reference it. */
2522 pch_page = XCNEW (struct page_entry);
2523 pch_page->page = pch_zone.page;
2524 pch_page->pch_p = true;
2526 for (p = pch_zone.page; p < pch_zone.end; p += GGC_PAGE_SIZE)
2527 set_page_table_entry (p, pch_page);