Check in tree-dce enh to trunk
[official-gcc.git] / gcc / ggc-zone.c
blobe8185a06381b406cb458102fd7295263aacd1b33
1 /* "Bag-of-pages" zone garbage collector for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
3 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 "varray.h"
34 #include "flags.h"
35 #include "ggc.h"
36 #include "timevar.h"
37 #include "params.h"
38 #include "bitmap.h"
40 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
41 file open. Prefer either to valloc. */
42 #ifdef HAVE_MMAP_ANON
43 # undef HAVE_MMAP_DEV_ZERO
45 # include <sys/mman.h>
46 # ifndef MAP_FAILED
47 # define MAP_FAILED -1
48 # endif
49 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
50 # define MAP_ANONYMOUS MAP_ANON
51 # endif
52 # define USING_MMAP
53 #endif
55 #ifdef HAVE_MMAP_DEV_ZERO
56 # include <sys/mman.h>
57 # ifndef MAP_FAILED
58 # define MAP_FAILED -1
59 # endif
60 # define USING_MMAP
61 #endif
63 #ifndef USING_MMAP
64 #error Zone collector requires mmap
65 #endif
67 #if (GCC_VERSION < 3001)
68 #define prefetch(X) ((void) X)
69 #define prefetchw(X) ((void) X)
70 #else
71 #define prefetch(X) __builtin_prefetch (X)
72 #define prefetchw(X) __builtin_prefetch (X, 1, 3)
73 #endif
75 /* FUTURE NOTES:
77 If we track inter-zone pointers, we can mark single zones at a
78 time.
80 If we have a zone where we guarantee no inter-zone pointers, we
81 could mark that zone separately.
83 The garbage zone should not be marked, and we should return 1 in
84 ggc_set_mark for any object in the garbage zone, which cuts off
85 marking quickly. */
87 /* Strategy:
89 This garbage-collecting allocator segregates objects into zones.
90 It also segregates objects into "large" and "small" bins. Large
91 objects are greater than page size.
93 Pages for small objects are broken up into chunks. The page has
94 a bitmap which marks the start position of each chunk (whether
95 allocated or free). Free chunks are on one of the zone's free
96 lists and contain a pointer to the next free chunk. Chunks in
97 most of the free lists have a fixed size determined by the
98 free list. Chunks in the "other" sized free list have their size
99 stored right after their chain pointer.
101 Empty pages (of all sizes) are kept on a single page cache list,
102 and are considered first when new pages are required; they are
103 deallocated at the start of the next collection if they haven't
104 been recycled by then. The free page list is currently per-zone. */
106 /* Define GGC_DEBUG_LEVEL to print debugging information.
107 0: No debugging output.
108 1: GC statistics only.
109 2: Page-entry allocations/deallocations as well.
110 3: Object allocations as well.
111 4: Object marks as well. */
112 #define GGC_DEBUG_LEVEL (0)
114 #ifndef HOST_BITS_PER_PTR
115 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
116 #endif
118 /* This structure manages small free chunks. The SIZE field is only
119 initialized if the chunk is in the "other" sized free list. Large
120 chunks are allocated one at a time to their own page, and so don't
121 come in here. */
123 struct alloc_chunk {
124 struct alloc_chunk *next_free;
125 unsigned int size;
128 /* The size of the fixed-size portion of a small page descriptor. */
129 #define PAGE_OVERHEAD (offsetof (struct small_page_entry, alloc_bits))
131 /* The collector's idea of the page size. This must be a power of two
132 no larger than the system page size, because pages must be aligned
133 to this amount and are tracked at this granularity in the page
134 table. We choose a size at compile time for efficiency.
136 We could make a better guess at compile time if PAGE_SIZE is a
137 constant in system headers, and PAGE_SHIFT is defined... */
138 #define GGC_PAGE_SIZE 4096
139 #define GGC_PAGE_MASK (GGC_PAGE_SIZE - 1)
140 #define GGC_PAGE_SHIFT 12
142 #if 0
143 /* Alternative definitions which use the runtime page size. */
144 #define GGC_PAGE_SIZE G.pagesize
145 #define GGC_PAGE_MASK G.page_mask
146 #define GGC_PAGE_SHIFT G.lg_pagesize
147 #endif
149 /* The size of a small page managed by the garbage collector. This
150 must currently be GGC_PAGE_SIZE, but with a few changes could
151 be any multiple of it to reduce certain kinds of overhead. */
152 #define SMALL_PAGE_SIZE GGC_PAGE_SIZE
154 /* Free bin information. These numbers may be in need of re-tuning.
155 In general, decreasing the number of free bins would seem to
156 increase the time it takes to allocate... */
158 /* FIXME: We can't use anything but MAX_ALIGNMENT for the bin size
159 today. */
161 #define NUM_FREE_BINS 64
162 #define FREE_BIN_DELTA MAX_ALIGNMENT
163 #define SIZE_BIN_DOWN(SIZE) ((SIZE) / FREE_BIN_DELTA)
165 /* Allocation and marking parameters. */
167 /* The smallest allocatable unit to keep track of. */
168 #define BYTES_PER_ALLOC_BIT MAX_ALIGNMENT
170 /* The smallest markable unit. If we require each allocated object
171 to contain at least two allocatable units, we can use half as many
172 bits for the mark bitmap. But this adds considerable complexity
173 to sweeping. */
174 #define BYTES_PER_MARK_BIT BYTES_PER_ALLOC_BIT
176 #define BYTES_PER_MARK_WORD (8 * BYTES_PER_MARK_BIT * sizeof (mark_type))
178 /* We use this structure to determine the alignment required for
179 allocations.
181 There are several things wrong with this estimation of alignment.
183 The maximum alignment for a structure is often less than the
184 maximum alignment for a basic data type; for instance, on some
185 targets long long must be aligned to sizeof (int) in a structure
186 and sizeof (long long) in a variable. i386-linux is one example;
187 Darwin is another (sometimes, depending on the compiler in use).
189 Also, long double is not included. Nothing in GCC uses long
190 double, so we assume that this is OK. On powerpc-darwin, adding
191 long double would bring the maximum alignment up to 16 bytes,
192 and until we need long double (or to vectorize compiler operations)
193 that's painfully wasteful. This will need to change, some day. */
195 struct max_alignment {
196 char c;
197 union {
198 HOST_WIDEST_INT i;
199 double d;
200 } u;
203 /* The biggest alignment required. */
205 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
207 /* Compute the smallest multiple of F that is >= X. */
209 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
211 /* Types to use for the allocation and mark bitmaps. It might be
212 a good idea to add ffsl to libiberty and use unsigned long
213 instead; that could speed us up where long is wider than int. */
215 typedef unsigned int alloc_type;
216 typedef unsigned int mark_type;
217 #define alloc_ffs(x) ffs(x)
219 /* A page_entry records the status of an allocation page. This is the
220 common data between all three kinds of pages - small, large, and
221 PCH. */
222 typedef struct page_entry
224 /* The address at which the memory is allocated. */
225 char *page;
227 /* The zone that this page entry belongs to. */
228 struct alloc_zone *zone;
230 #ifdef GATHER_STATISTICS
231 /* How many collections we've survived. */
232 size_t survived;
233 #endif
235 /* Does this page contain small objects, or one large object? */
236 bool large_p;
238 /* Is this page part of the loaded PCH? */
239 bool pch_p;
240 } page_entry;
242 /* Additional data needed for small pages. */
243 struct small_page_entry
245 struct page_entry common;
247 /* The next small page entry, or NULL if this is the last. */
248 struct small_page_entry *next;
250 /* If currently marking this zone, a pointer to the mark bits
251 for this page. If we aren't currently marking this zone,
252 this pointer may be stale (pointing to freed memory). */
253 mark_type *mark_bits;
255 /* The allocation bitmap. This array extends far enough to have
256 one bit for every BYTES_PER_ALLOC_BIT bytes in the page. */
257 alloc_type alloc_bits[1];
260 /* Additional data needed for large pages. */
261 struct large_page_entry
263 struct page_entry common;
265 /* The next large page entry, or NULL if this is the last. */
266 struct large_page_entry *next;
268 /* The number of bytes allocated, not including the page entry. */
269 size_t bytes;
271 /* The previous page in the list, so that we can unlink this one. */
272 struct large_page_entry *prev;
274 /* During marking, is this object marked? */
275 bool mark_p;
278 /* A two-level tree is used to look up the page-entry for a given
279 pointer. Two chunks of the pointer's bits are extracted to index
280 the first and second levels of the tree, as follows:
282 HOST_PAGE_SIZE_BITS
283 32 | |
284 msb +----------------+----+------+------+ lsb
285 | | |
286 PAGE_L1_BITS |
288 PAGE_L2_BITS
290 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
291 pages are aligned on system page boundaries. The next most
292 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
293 index values in the lookup table, respectively.
295 For 32-bit architectures and the settings below, there are no
296 leftover bits. For architectures with wider pointers, the lookup
297 tree points to a list of pages, which must be scanned to find the
298 correct one. */
300 #define PAGE_L1_BITS (8)
301 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - GGC_PAGE_SHIFT)
302 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
303 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
305 #define LOOKUP_L1(p) \
306 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
308 #define LOOKUP_L2(p) \
309 (((size_t) (p) >> GGC_PAGE_SHIFT) & ((1 << PAGE_L2_BITS) - 1))
311 #if HOST_BITS_PER_PTR <= 32
313 /* On 32-bit hosts, we use a two level page table, as pictured above. */
314 typedef page_entry **page_table[PAGE_L1_SIZE];
316 #else
318 /* On 64-bit hosts, we use the same two level page tables plus a linked
319 list that disambiguates the top 32-bits. There will almost always be
320 exactly one entry in the list. */
321 typedef struct page_table_chain
323 struct page_table_chain *next;
324 size_t high_bits;
325 page_entry **table[PAGE_L1_SIZE];
326 } *page_table;
328 #endif
330 /* The global variables. */
331 static struct globals
333 /* The linked list of zones. */
334 struct alloc_zone *zones;
336 /* Lookup table for associating allocation pages with object addresses. */
337 page_table lookup;
339 /* The system's page size, and related constants. */
340 size_t pagesize;
341 size_t lg_pagesize;
342 size_t page_mask;
344 /* The size to allocate for a small page entry. This includes
345 the size of the structure and the size of the allocation
346 bitmap. */
347 size_t small_page_overhead;
349 #if defined (HAVE_MMAP_DEV_ZERO)
350 /* A file descriptor open to /dev/zero for reading. */
351 int dev_zero_fd;
352 #endif
354 /* Allocate pages in chunks of this size, to throttle calls to memory
355 allocation routines. The first page is used, the rest go onto the
356 free list. */
357 size_t quire_size;
359 /* The file descriptor for debugging output. */
360 FILE *debug_file;
361 } G;
363 /* A zone allocation structure. There is one of these for every
364 distinct allocation zone. */
365 struct alloc_zone
367 /* The most recent free chunk is saved here, instead of in the linked
368 free list, to decrease list manipulation. It is most likely that we
369 will want this one. */
370 char *cached_free;
371 size_t cached_free_size;
373 /* Linked lists of free storage. Slots 1 ... NUM_FREE_BINS have chunks of size
374 FREE_BIN_DELTA. All other chunks are in slot 0. */
375 struct alloc_chunk *free_chunks[NUM_FREE_BINS + 1];
377 /* The highest bin index which might be non-empty. It may turn out
378 to be empty, in which case we have to search downwards. */
379 size_t high_free_bin;
381 /* Bytes currently allocated in this zone. */
382 size_t allocated;
384 /* Linked list of the small pages in this zone. */
385 struct small_page_entry *pages;
387 /* Doubly linked list of large pages in this zone. */
388 struct large_page_entry *large_pages;
390 /* If we are currently marking this zone, a pointer to the mark bits. */
391 mark_type *mark_bits;
393 /* Name of the zone. */
394 const char *name;
396 /* The number of small pages currently allocated in this zone. */
397 size_t n_small_pages;
399 /* Bytes allocated at the end of the last collection. */
400 size_t allocated_last_gc;
402 /* Total amount of memory mapped. */
403 size_t bytes_mapped;
405 /* A cache of free system pages. */
406 struct small_page_entry *free_pages;
408 /* Next zone in the linked list of zones. */
409 struct alloc_zone *next_zone;
411 /* True if this zone was collected during this collection. */
412 bool was_collected;
414 /* True if this zone should be destroyed after the next collection. */
415 bool dead;
417 #ifdef GATHER_STATISTICS
418 struct
420 /* Total memory allocated with ggc_alloc. */
421 unsigned long long total_allocated;
422 /* Total overhead for memory to be allocated with ggc_alloc. */
423 unsigned long long total_overhead;
425 /* Total allocations and overhead for sizes less than 32, 64 and 128.
426 These sizes are interesting because they are typical cache line
427 sizes. */
429 unsigned long long total_allocated_under32;
430 unsigned long long total_overhead_under32;
432 unsigned long long total_allocated_under64;
433 unsigned long long total_overhead_under64;
435 unsigned long long total_allocated_under128;
436 unsigned long long total_overhead_under128;
437 } stats;
438 #endif
439 } main_zone;
441 /* Some default zones. */
442 struct alloc_zone rtl_zone;
443 struct alloc_zone tree_zone;
444 struct alloc_zone tree_id_zone;
446 /* The PCH zone does not need a normal zone structure, and it does
447 not live on the linked list of zones. */
448 struct pch_zone
450 /* The start of the PCH zone. NULL if there is none. */
451 char *page;
453 /* The end of the PCH zone. NULL if there is none. */
454 char *end;
456 /* The size of the PCH zone. 0 if there is none. */
457 size_t bytes;
459 /* The allocation bitmap for the PCH zone. */
460 alloc_type *alloc_bits;
462 /* If we are currently marking, the mark bitmap for the PCH zone.
463 When it is first read in, we could avoid marking the PCH,
464 because it will not contain any pointers to GC memory outside
465 of the PCH; however, the PCH is currently mapped as writable,
466 so we must mark it in case new pointers are added. */
467 mark_type *mark_bits;
468 } pch_zone;
470 #ifdef USING_MMAP
471 static char *alloc_anon (char *, size_t, struct alloc_zone *);
472 #endif
473 static struct small_page_entry * alloc_small_page (struct alloc_zone *);
474 static struct large_page_entry * alloc_large_page (size_t, struct alloc_zone *);
475 static void free_chunk (char *, size_t, struct alloc_zone *);
476 static void free_small_page (struct small_page_entry *);
477 static void free_large_page (struct large_page_entry *);
478 static void release_pages (struct alloc_zone *);
479 static void sweep_pages (struct alloc_zone *);
480 static bool ggc_collect_1 (struct alloc_zone *, bool);
481 static void new_ggc_zone_1 (struct alloc_zone *, const char *);
483 /* Traverse the page table and find the entry for a page.
484 Die (probably) if the object wasn't allocated via GC. */
486 static inline page_entry *
487 lookup_page_table_entry (const void *p)
489 page_entry ***base;
490 size_t L1, L2;
492 #if HOST_BITS_PER_PTR <= 32
493 base = &G.lookup[0];
494 #else
495 page_table table = G.lookup;
496 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
497 while (table->high_bits != high_bits)
498 table = table->next;
499 base = &table->table[0];
500 #endif
502 /* Extract the level 1 and 2 indices. */
503 L1 = LOOKUP_L1 (p);
504 L2 = LOOKUP_L2 (p);
506 return base[L1][L2];
509 /* Set the page table entry for the page that starts at P. If ENTRY
510 is NULL, clear the entry. */
512 static void
513 set_page_table_entry (void *p, page_entry *entry)
515 page_entry ***base;
516 size_t L1, L2;
518 #if HOST_BITS_PER_PTR <= 32
519 base = &G.lookup[0];
520 #else
521 page_table table;
522 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
523 for (table = G.lookup; table; table = table->next)
524 if (table->high_bits == high_bits)
525 goto found;
527 /* Not found -- allocate a new table. */
528 table = xcalloc (1, sizeof(*table));
529 table->next = G.lookup;
530 table->high_bits = high_bits;
531 G.lookup = table;
532 found:
533 base = &table->table[0];
534 #endif
536 /* Extract the level 1 and 2 indices. */
537 L1 = LOOKUP_L1 (p);
538 L2 = LOOKUP_L2 (p);
540 if (base[L1] == NULL)
541 base[L1] = xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
543 base[L1][L2] = entry;
546 /* Find the page table entry associated with OBJECT. */
548 static inline struct page_entry *
549 zone_get_object_page (const void *object)
551 return lookup_page_table_entry (object);
554 /* Find which element of the alloc_bits array OBJECT should be
555 recorded in. */
556 static inline unsigned int
557 zone_get_object_alloc_word (const void *object)
559 return (((size_t) object & (GGC_PAGE_SIZE - 1))
560 / (8 * sizeof (alloc_type) * BYTES_PER_ALLOC_BIT));
563 /* Find which bit of the appropriate word in the alloc_bits array
564 OBJECT should be recorded in. */
565 static inline unsigned int
566 zone_get_object_alloc_bit (const void *object)
568 return (((size_t) object / BYTES_PER_ALLOC_BIT)
569 % (8 * sizeof (alloc_type)));
572 /* Find which element of the mark_bits array OBJECT should be recorded
573 in. */
574 static inline unsigned int
575 zone_get_object_mark_word (const void *object)
577 return (((size_t) object & (GGC_PAGE_SIZE - 1))
578 / (8 * sizeof (mark_type) * BYTES_PER_MARK_BIT));
581 /* Find which bit of the appropriate word in the mark_bits array
582 OBJECT should be recorded in. */
583 static inline unsigned int
584 zone_get_object_mark_bit (const void *object)
586 return (((size_t) object / BYTES_PER_MARK_BIT)
587 % (8 * sizeof (mark_type)));
590 /* Set the allocation bit corresponding to OBJECT in its page's
591 bitmap. Used to split this object from the preceding one. */
592 static inline void
593 zone_set_object_alloc_bit (const void *object)
595 struct small_page_entry *page
596 = (struct small_page_entry *) zone_get_object_page (object);
597 unsigned int start_word = zone_get_object_alloc_word (object);
598 unsigned int start_bit = zone_get_object_alloc_bit (object);
600 page->alloc_bits[start_word] |= 1L << start_bit;
603 /* Clear the allocation bit corresponding to OBJECT in PAGE's
604 bitmap. Used to coalesce this object with the preceding
605 one. */
606 static inline void
607 zone_clear_object_alloc_bit (struct small_page_entry *page,
608 const void *object)
610 unsigned int start_word = zone_get_object_alloc_word (object);
611 unsigned int start_bit = zone_get_object_alloc_bit (object);
613 /* Would xor be quicker? */
614 page->alloc_bits[start_word] &= ~(1L << start_bit);
617 /* Find the size of the object which starts at START_WORD and
618 START_BIT in ALLOC_BITS, which is at most MAX_SIZE bytes.
619 Helper function for ggc_get_size and zone_find_object_size. */
621 static inline size_t
622 zone_object_size_1 (alloc_type *alloc_bits,
623 size_t start_word, size_t start_bit,
624 size_t max_size)
626 size_t size;
627 alloc_type alloc_word;
628 int indx;
630 /* Load the first word. */
631 alloc_word = alloc_bits[start_word++];
633 /* If that was the last bit in this word, we'll want to continue
634 with the next word. Otherwise, handle the rest of this word. */
635 if (start_bit)
637 indx = alloc_ffs (alloc_word >> start_bit);
638 if (indx)
639 /* indx is 1-based. We started at the bit after the object's
640 start, but we also ended at the bit after the object's end.
641 It cancels out. */
642 return indx * BYTES_PER_ALLOC_BIT;
644 /* The extra 1 accounts for the starting unit, before start_bit. */
645 size = (sizeof (alloc_type) * 8 - start_bit + 1) * BYTES_PER_ALLOC_BIT;
647 if (size >= max_size)
648 return max_size;
650 alloc_word = alloc_bits[start_word++];
652 else
653 size = BYTES_PER_ALLOC_BIT;
655 while (alloc_word == 0)
657 size += sizeof (alloc_type) * 8 * BYTES_PER_ALLOC_BIT;
658 if (size >= max_size)
659 return max_size;
660 alloc_word = alloc_bits[start_word++];
663 indx = alloc_ffs (alloc_word);
664 return size + (indx - 1) * BYTES_PER_ALLOC_BIT;
667 /* Find the size of OBJECT on small page PAGE. */
669 static inline size_t
670 zone_find_object_size (struct small_page_entry *page,
671 const void *object)
673 const char *object_midptr = (const char *) object + BYTES_PER_ALLOC_BIT;
674 unsigned int start_word = zone_get_object_alloc_word (object_midptr);
675 unsigned int start_bit = zone_get_object_alloc_bit (object_midptr);
676 size_t max_size = (page->common.page + SMALL_PAGE_SIZE
677 - (char *) object);
679 return zone_object_size_1 (page->alloc_bits, start_word, start_bit,
680 max_size);
683 /* Allocate the mark bits for every zone, and set the pointers on each
684 page. */
685 static void
686 zone_allocate_marks (void)
688 struct alloc_zone *zone;
690 for (zone = G.zones; zone; zone = zone->next_zone)
692 struct small_page_entry *page;
693 mark_type *cur_marks;
694 size_t mark_words, mark_words_per_page;
695 #ifdef ENABLE_CHECKING
696 size_t n = 0;
697 #endif
699 mark_words_per_page
700 = (GGC_PAGE_SIZE + BYTES_PER_MARK_WORD - 1) / BYTES_PER_MARK_WORD;
701 mark_words = zone->n_small_pages * mark_words_per_page;
702 zone->mark_bits = (mark_type *) xcalloc (sizeof (mark_type),
703 mark_words);
704 cur_marks = zone->mark_bits;
705 for (page = zone->pages; page; page = page->next)
707 page->mark_bits = cur_marks;
708 cur_marks += mark_words_per_page;
709 #ifdef ENABLE_CHECKING
710 n++;
711 #endif
713 #ifdef ENABLE_CHECKING
714 gcc_assert (n == zone->n_small_pages);
715 #endif
718 /* We don't collect the PCH zone, but we do have to mark it
719 (for now). */
720 if (pch_zone.bytes)
721 pch_zone.mark_bits
722 = (mark_type *) xcalloc (sizeof (mark_type),
723 CEIL (pch_zone.bytes, BYTES_PER_MARK_WORD));
726 /* After marking and sweeping, release the memory used for mark bits. */
727 static void
728 zone_free_marks (void)
730 struct alloc_zone *zone;
732 for (zone = G.zones; zone; zone = zone->next_zone)
733 if (zone->mark_bits)
735 free (zone->mark_bits);
736 zone->mark_bits = NULL;
739 if (pch_zone.bytes)
741 free (pch_zone.mark_bits);
742 pch_zone.mark_bits = NULL;
746 #ifdef USING_MMAP
747 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
748 (if non-null). The ifdef structure here is intended to cause a
749 compile error unless exactly one of the HAVE_* is defined. */
751 static inline char *
752 alloc_anon (char *pref ATTRIBUTE_UNUSED, size_t size, struct alloc_zone *zone)
754 #ifdef HAVE_MMAP_ANON
755 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
756 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
757 #endif
758 #ifdef HAVE_MMAP_DEV_ZERO
759 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
760 MAP_PRIVATE, G.dev_zero_fd, 0);
761 #endif
763 if (page == (char *) MAP_FAILED)
765 perror ("virtual memory exhausted");
766 exit (FATAL_EXIT_CODE);
769 /* Remember that we allocated this memory. */
770 zone->bytes_mapped += size;
772 /* Pretend we don't have access to the allocated pages. We'll enable
773 access to smaller pieces of the area in ggc_alloc. Discard the
774 handle to avoid handle leak. */
775 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (page, size));
777 return page;
779 #endif
781 /* Allocate a new page for allocating small objects in ZONE, and
782 return an entry for it. */
784 static struct small_page_entry *
785 alloc_small_page (struct alloc_zone *zone)
787 struct small_page_entry *entry;
789 /* Check the list of free pages for one we can use. */
790 entry = zone->free_pages;
791 if (entry != NULL)
793 /* Recycle the allocated memory from this page ... */
794 zone->free_pages = entry->next;
796 else
798 /* We want just one page. Allocate a bunch of them and put the
799 extras on the freelist. (Can only do this optimization with
800 mmap for backing store.) */
801 struct small_page_entry *e, *f = zone->free_pages;
802 int i;
803 char *page;
805 page = alloc_anon (NULL, GGC_PAGE_SIZE * G.quire_size, zone);
807 /* This loop counts down so that the chain will be in ascending
808 memory order. */
809 for (i = G.quire_size - 1; i >= 1; i--)
811 e = xcalloc (1, G.small_page_overhead);
812 e->common.page = page + (i << GGC_PAGE_SHIFT);
813 e->common.zone = zone;
814 e->next = f;
815 f = e;
816 set_page_table_entry (e->common.page, &e->common);
819 zone->free_pages = f;
821 entry = xcalloc (1, G.small_page_overhead);
822 entry->common.page = page;
823 entry->common.zone = zone;
824 set_page_table_entry (page, &entry->common);
827 zone->n_small_pages++;
829 if (GGC_DEBUG_LEVEL >= 2)
830 fprintf (G.debug_file,
831 "Allocating %s page at %p, data %p-%p\n",
832 entry->common.zone->name, (PTR) entry, entry->common.page,
833 entry->common.page + SMALL_PAGE_SIZE - 1);
835 return entry;
838 /* Allocate a large page of size SIZE in ZONE. */
840 static struct large_page_entry *
841 alloc_large_page (size_t size, struct alloc_zone *zone)
843 struct large_page_entry *entry;
844 char *page;
845 size_t needed_size;
847 needed_size = size + sizeof (struct large_page_entry);
848 page = xmalloc (needed_size);
850 entry = (struct large_page_entry *) page;
852 entry->next = NULL;
853 entry->common.page = page + sizeof (struct large_page_entry);
854 entry->common.large_p = true;
855 entry->common.pch_p = false;
856 entry->common.zone = zone;
857 #ifdef GATHER_STATISTICS
858 entry->common.survived = 0;
859 #endif
860 entry->mark_p = false;
861 entry->bytes = size;
862 entry->prev = NULL;
864 set_page_table_entry (entry->common.page, &entry->common);
866 if (GGC_DEBUG_LEVEL >= 2)
867 fprintf (G.debug_file,
868 "Allocating %s large page at %p, data %p-%p\n",
869 entry->common.zone->name, (PTR) entry, entry->common.page,
870 entry->common.page + SMALL_PAGE_SIZE - 1);
872 return entry;
876 /* For a page that is no longer needed, put it on the free page list. */
878 static inline void
879 free_small_page (struct small_page_entry *entry)
881 if (GGC_DEBUG_LEVEL >= 2)
882 fprintf (G.debug_file,
883 "Deallocating %s page at %p, data %p-%p\n",
884 entry->common.zone->name, (PTR) entry,
885 entry->common.page, entry->common.page + SMALL_PAGE_SIZE - 1);
887 gcc_assert (!entry->common.large_p);
889 /* Mark the page as inaccessible. Discard the handle to
890 avoid handle leak. */
891 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (entry->common.page,
892 SMALL_PAGE_SIZE));
894 entry->next = entry->common.zone->free_pages;
895 entry->common.zone->free_pages = entry;
896 entry->common.zone->n_small_pages--;
899 /* Release a large page that is no longer needed. */
901 static inline void
902 free_large_page (struct large_page_entry *entry)
904 if (GGC_DEBUG_LEVEL >= 2)
905 fprintf (G.debug_file,
906 "Deallocating %s page at %p, data %p-%p\n",
907 entry->common.zone->name, (PTR) entry,
908 entry->common.page, entry->common.page + SMALL_PAGE_SIZE - 1);
910 gcc_assert (entry->common.large_p);
912 set_page_table_entry (entry->common.page, NULL);
913 free (entry);
916 /* Release the free page cache to the system. */
918 static void
919 release_pages (struct alloc_zone *zone)
921 #ifdef USING_MMAP
922 struct small_page_entry *p, *next;
923 char *start;
924 size_t len;
926 /* Gather up adjacent pages so they are unmapped together. */
927 p = zone->free_pages;
929 while (p)
931 start = p->common.page;
932 next = p->next;
933 len = SMALL_PAGE_SIZE;
934 set_page_table_entry (p->common.page, NULL);
935 p = next;
937 while (p && p->common.page == start + len)
939 next = p->next;
940 len += SMALL_PAGE_SIZE;
941 set_page_table_entry (p->common.page, NULL);
942 p = next;
945 munmap (start, len);
946 zone->bytes_mapped -= len;
949 zone->free_pages = NULL;
950 #endif
953 /* Place the block at PTR of size SIZE on the free list for ZONE. */
955 static inline void
956 free_chunk (char *ptr, size_t size, struct alloc_zone *zone)
958 struct alloc_chunk *chunk = (struct alloc_chunk *) ptr;
959 size_t bin = 0;
961 bin = SIZE_BIN_DOWN (size);
962 gcc_assert (bin != 0);
963 if (bin > NUM_FREE_BINS)
965 bin = 0;
966 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (chunk,
967 sizeof (struct
968 alloc_chunk)));
969 chunk->size = size;
970 chunk->next_free = zone->free_chunks[bin];
971 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (ptr
972 + sizeof (struct
973 alloc_chunk),
974 size
975 - sizeof (struct
976 alloc_chunk)));
978 else
980 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (chunk,
981 sizeof (struct
982 alloc_chunk *)));
983 chunk->next_free = zone->free_chunks[bin];
984 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (ptr
985 + sizeof (struct
986 alloc_chunk *),
987 size
988 - sizeof (struct
989 alloc_chunk *)));
992 zone->free_chunks[bin] = chunk;
993 if (bin > zone->high_free_bin)
994 zone->high_free_bin = bin;
995 if (GGC_DEBUG_LEVEL >= 3)
996 fprintf (G.debug_file, "Deallocating object, chunk=%p\n", (void *)chunk);
999 /* Allocate a chunk of memory of at least ORIG_SIZE bytes, in ZONE. */
1001 void *
1002 ggc_alloc_zone_stat (size_t orig_size, struct alloc_zone *zone
1003 MEM_STAT_DECL)
1005 size_t bin;
1006 size_t csize;
1007 struct small_page_entry *entry;
1008 struct alloc_chunk *chunk, **pp;
1009 void *result;
1010 size_t size = orig_size;
1012 /* Make sure that zero-sized allocations get a unique and freeable
1013 pointer. */
1014 if (size == 0)
1015 size = MAX_ALIGNMENT;
1016 else
1017 size = (size + MAX_ALIGNMENT - 1) & -MAX_ALIGNMENT;
1019 /* Try to allocate the object from several different sources. Each
1020 of these cases is responsible for setting RESULT and SIZE to
1021 describe the allocated block, before jumping to FOUND. If a
1022 chunk is split, the allocate bit for the new chunk should also be
1023 set.
1025 Large objects are handled specially. However, they'll just fail
1026 the next couple of conditions, so we can wait to check for them
1027 below. The large object case is relatively rare (< 1%), so this
1028 is a win. */
1030 /* First try to split the last chunk we allocated. For best
1031 fragmentation behavior it would be better to look for a
1032 free bin of the appropriate size for a small object. However,
1033 we're unlikely (1% - 7%) to find one, and this gives better
1034 locality behavior anyway. This case handles the lion's share
1035 of all calls to this function. */
1036 if (size <= zone->cached_free_size)
1038 result = zone->cached_free;
1040 zone->cached_free_size -= size;
1041 if (zone->cached_free_size)
1043 zone->cached_free += size;
1044 zone_set_object_alloc_bit (zone->cached_free);
1047 goto found;
1050 /* Next, try to find a free bin of the exactly correct size. */
1052 /* We want to round SIZE up, rather than down, but we know it's
1053 already aligned to at least FREE_BIN_DELTA, so we can just
1054 shift. */
1055 bin = SIZE_BIN_DOWN (size);
1057 if (bin <= NUM_FREE_BINS
1058 && (chunk = zone->free_chunks[bin]) != NULL)
1060 /* We have a chunk of the right size. Pull it off the free list
1061 and use it. */
1063 zone->free_chunks[bin] = chunk->next_free;
1065 /* NOTE: SIZE is only guaranteed to be right if MAX_ALIGNMENT
1066 == FREE_BIN_DELTA. */
1067 result = chunk;
1069 /* The allocation bits are already set correctly. HIGH_FREE_BIN
1070 may now be wrong, if this was the last chunk in the high bin.
1071 Rather than fixing it up now, wait until we need to search
1072 the free bins. */
1074 goto found;
1077 /* Next, if there wasn't a chunk of the ideal size, look for a chunk
1078 to split. We can find one in the too-big bin, or in the largest
1079 sized bin with a chunk in it. Try the largest normal-sized bin
1080 first. */
1082 if (zone->high_free_bin > bin)
1084 /* Find the highest numbered free bin. It will be at or below
1085 the watermark. */
1086 while (zone->high_free_bin > bin
1087 && zone->free_chunks[zone->high_free_bin] == NULL)
1088 zone->high_free_bin--;
1090 if (zone->high_free_bin > bin)
1092 size_t tbin = zone->high_free_bin;
1093 chunk = zone->free_chunks[tbin];
1095 /* Remove the chunk from its previous bin. */
1096 zone->free_chunks[tbin] = chunk->next_free;
1098 result = (char *) chunk;
1100 /* Save the rest of the chunk for future allocation. */
1101 if (zone->cached_free_size)
1102 free_chunk (zone->cached_free, zone->cached_free_size, zone);
1104 chunk = (struct alloc_chunk *) ((char *) result + size);
1105 zone->cached_free = (char *) chunk;
1106 zone->cached_free_size = (tbin - bin) * FREE_BIN_DELTA;
1108 /* Mark the new free chunk as an object, so that we can
1109 find the size of the newly allocated object. */
1110 zone_set_object_alloc_bit (chunk);
1112 /* HIGH_FREE_BIN may now be wrong, if this was the last
1113 chunk in the high bin. Rather than fixing it up now,
1114 wait until we need to search the free bins. */
1116 goto found;
1120 /* Failing that, look through the "other" bucket for a chunk
1121 that is large enough. */
1122 pp = &(zone->free_chunks[0]);
1123 chunk = *pp;
1124 while (chunk && chunk->size < size)
1126 pp = &chunk->next_free;
1127 chunk = *pp;
1130 if (chunk)
1132 /* Remove the chunk from its previous bin. */
1133 *pp = chunk->next_free;
1135 result = (char *) chunk;
1137 /* Save the rest of the chunk for future allocation, if there's any
1138 left over. */
1139 csize = chunk->size;
1140 if (csize > size)
1142 if (zone->cached_free_size)
1143 free_chunk (zone->cached_free, zone->cached_free_size, zone);
1145 chunk = (struct alloc_chunk *) ((char *) result + size);
1146 zone->cached_free = (char *) chunk;
1147 zone->cached_free_size = csize - size;
1149 /* Mark the new free chunk as an object. */
1150 zone_set_object_alloc_bit (chunk);
1153 goto found;
1156 /* Handle large allocations. We could choose any threshold between
1157 GGC_PAGE_SIZE - sizeof (struct large_page_entry) and
1158 GGC_PAGE_SIZE. It can't be smaller, because then it wouldn't
1159 be guaranteed to have a unique entry in the lookup table. Large
1160 allocations will always fall through to here. */
1161 if (size > GGC_PAGE_SIZE)
1163 struct large_page_entry *entry = alloc_large_page (size, zone);
1165 #ifdef GATHER_STATISTICS
1166 entry->common.survived = 0;
1167 #endif
1169 entry->next = zone->large_pages;
1170 if (zone->large_pages)
1171 zone->large_pages->prev = entry;
1172 zone->large_pages = entry;
1174 result = entry->common.page;
1176 goto found;
1179 /* Failing everything above, allocate a new small page. */
1181 entry = alloc_small_page (zone);
1182 entry->next = zone->pages;
1183 zone->pages = entry;
1185 /* Mark the first chunk in the new page. */
1186 entry->alloc_bits[0] = 1;
1188 result = entry->common.page;
1189 if (size < SMALL_PAGE_SIZE)
1191 if (zone->cached_free_size)
1192 free_chunk (zone->cached_free, zone->cached_free_size, zone);
1194 zone->cached_free = (char *) result + size;
1195 zone->cached_free_size = SMALL_PAGE_SIZE - size;
1197 /* Mark the new free chunk as an object. */
1198 zone_set_object_alloc_bit (zone->cached_free);
1201 found:
1203 /* We could save TYPE in the chunk, but we don't use that for
1204 anything yet. If we wanted to, we could do it by adding it
1205 either before the beginning of the chunk or after its end,
1206 and adjusting the size and pointer appropriately. */
1208 /* We'll probably write to this after we return. */
1209 prefetchw (result);
1211 #ifdef ENABLE_GC_CHECKING
1212 /* `Poison' the entire allocated object. */
1213 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, size));
1214 memset (result, 0xaf, size);
1215 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (result + orig_size,
1216 size - orig_size));
1217 #endif
1219 /* Tell Valgrind that the memory is there, but its content isn't
1220 defined. The bytes at the end of the object are still marked
1221 unaccessible. */
1222 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, orig_size));
1224 /* Keep track of how many bytes are being allocated. This
1225 information is used in deciding when to collect. */
1226 zone->allocated += size;
1228 timevar_ggc_mem_total += size;
1230 #ifdef GATHER_STATISTICS
1231 ggc_record_overhead (orig_size, size - orig_size, result PASS_MEM_STAT);
1234 size_t object_size = size;
1235 size_t overhead = object_size - orig_size;
1237 zone->stats.total_overhead += overhead;
1238 zone->stats.total_allocated += object_size;
1240 if (orig_size <= 32)
1242 zone->stats.total_overhead_under32 += overhead;
1243 zone->stats.total_allocated_under32 += object_size;
1245 if (orig_size <= 64)
1247 zone->stats.total_overhead_under64 += overhead;
1248 zone->stats.total_allocated_under64 += object_size;
1250 if (orig_size <= 128)
1252 zone->stats.total_overhead_under128 += overhead;
1253 zone->stats.total_allocated_under128 += object_size;
1256 #endif
1258 if (GGC_DEBUG_LEVEL >= 3)
1259 fprintf (G.debug_file, "Allocating object, size=%lu at %p\n",
1260 (unsigned long) size, result);
1262 return result;
1265 /* Allocate a SIZE of chunk memory of GTE type, into an appropriate zone
1266 for that type. */
1268 void *
1269 ggc_alloc_typed_stat (enum gt_types_enum gte, size_t size
1270 MEM_STAT_DECL)
1272 switch (gte)
1274 case gt_ggc_e_14lang_tree_node:
1275 return ggc_alloc_zone_pass_stat (size, &tree_zone);
1277 case gt_ggc_e_7rtx_def:
1278 return ggc_alloc_zone_pass_stat (size, &rtl_zone);
1280 case gt_ggc_e_9rtvec_def:
1281 return ggc_alloc_zone_pass_stat (size, &rtl_zone);
1283 default:
1284 return ggc_alloc_zone_pass_stat (size, &main_zone);
1288 /* Normal ggc_alloc simply allocates into the main zone. */
1290 void *
1291 ggc_alloc_stat (size_t size MEM_STAT_DECL)
1293 return ggc_alloc_zone_pass_stat (size, &main_zone);
1296 /* Poison the chunk. */
1297 #ifdef ENABLE_GC_CHECKING
1298 #define poison_region(PTR, SIZE) \
1299 memset ((PTR), 0xa5, (SIZE))
1300 #else
1301 #define poison_region(PTR, SIZE)
1302 #endif
1304 /* Free the object at P. */
1306 void
1307 ggc_free (void *p)
1309 struct page_entry *page;
1311 #ifdef GATHER_STATISTICS
1312 ggc_free_overhead (p);
1313 #endif
1315 poison_region (p, ggc_get_size (p));
1317 page = zone_get_object_page (p);
1319 if (page->large_p)
1321 struct large_page_entry *large_page
1322 = (struct large_page_entry *) page;
1324 /* Remove the page from the linked list. */
1325 if (large_page->prev)
1326 large_page->prev->next = large_page->next;
1327 else
1329 gcc_assert (large_page->common.zone->large_pages == large_page);
1330 large_page->common.zone->large_pages = large_page->next;
1332 if (large_page->next)
1333 large_page->next->prev = large_page->prev;
1335 large_page->common.zone->allocated -= large_page->bytes;
1337 /* Release the memory associated with this object. */
1338 free_large_page (large_page);
1340 else if (page->pch_p)
1341 /* Don't do anything. We won't allocate a new object from the
1342 PCH zone so there's no point in releasing anything. */
1344 else
1346 size_t size = ggc_get_size (p);
1348 page->zone->allocated -= size;
1350 /* Add the chunk to the free list. We don't bother with coalescing,
1351 since we are likely to want a chunk of this size again. */
1352 free_chunk (p, size, page->zone);
1356 /* If P is not marked, mark it and return false. Otherwise return true.
1357 P must have been allocated by the GC allocator; it mustn't point to
1358 static objects, stack variables, or memory allocated with malloc. */
1361 ggc_set_mark (const void *p)
1363 struct page_entry *page;
1364 const char *ptr = (const char *) p;
1366 page = zone_get_object_page (p);
1368 if (page->pch_p)
1370 size_t mark_word, mark_bit, offset;
1371 offset = (ptr - pch_zone.page) / BYTES_PER_MARK_BIT;
1372 mark_word = offset / (8 * sizeof (mark_type));
1373 mark_bit = offset % (8 * sizeof (mark_type));
1375 if (pch_zone.mark_bits[mark_word] & (1 << mark_bit))
1376 return 1;
1377 pch_zone.mark_bits[mark_word] |= (1 << mark_bit);
1379 else if (page->large_p)
1381 struct large_page_entry *large_page
1382 = (struct large_page_entry *) page;
1384 if (large_page->mark_p)
1385 return 1;
1386 large_page->mark_p = true;
1388 else
1390 struct small_page_entry *small_page
1391 = (struct small_page_entry *) page;
1393 if (small_page->mark_bits[zone_get_object_mark_word (p)]
1394 & (1 << zone_get_object_mark_bit (p)))
1395 return 1;
1396 small_page->mark_bits[zone_get_object_mark_word (p)]
1397 |= (1 << zone_get_object_mark_bit (p));
1400 if (GGC_DEBUG_LEVEL >= 4)
1401 fprintf (G.debug_file, "Marking %p\n", p);
1403 return 0;
1406 /* Return 1 if P has been marked, zero otherwise.
1407 P must have been allocated by the GC allocator; it mustn't point to
1408 static objects, stack variables, or memory allocated with malloc. */
1411 ggc_marked_p (const void *p)
1413 struct page_entry *page;
1414 const char *ptr = p;
1416 page = zone_get_object_page (p);
1418 if (page->pch_p)
1420 size_t mark_word, mark_bit, offset;
1421 offset = (ptr - pch_zone.page) / BYTES_PER_MARK_BIT;
1422 mark_word = offset / (8 * sizeof (mark_type));
1423 mark_bit = offset % (8 * sizeof (mark_type));
1425 return (pch_zone.mark_bits[mark_word] & (1 << mark_bit)) != 0;
1428 if (page->large_p)
1430 struct large_page_entry *large_page
1431 = (struct large_page_entry *) page;
1433 return large_page->mark_p;
1435 else
1437 struct small_page_entry *small_page
1438 = (struct small_page_entry *) page;
1440 return 0 != (small_page->mark_bits[zone_get_object_mark_word (p)]
1441 & (1 << zone_get_object_mark_bit (p)));
1445 /* Return the size of the gc-able object P. */
1447 size_t
1448 ggc_get_size (const void *p)
1450 struct page_entry *page;
1451 const char *ptr = (const char *) p;
1453 page = zone_get_object_page (p);
1455 if (page->pch_p)
1457 size_t alloc_word, alloc_bit, offset, max_size;
1458 offset = (ptr - pch_zone.page) / BYTES_PER_ALLOC_BIT + 1;
1459 alloc_word = offset / (8 * sizeof (alloc_type));
1460 alloc_bit = offset % (8 * sizeof (alloc_type));
1461 max_size = pch_zone.bytes - (ptr - pch_zone.page);
1462 return zone_object_size_1 (pch_zone.alloc_bits, alloc_word, alloc_bit,
1463 max_size);
1466 if (page->large_p)
1467 return ((struct large_page_entry *)page)->bytes;
1468 else
1469 return zone_find_object_size ((struct small_page_entry *) page, p);
1472 /* Initialize the ggc-zone-mmap allocator. */
1473 void
1474 init_ggc (void)
1476 /* The allocation size must be greater than BYTES_PER_MARK_BIT, and
1477 a multiple of both BYTES_PER_ALLOC_BIT and FREE_BIN_DELTA, for
1478 the current assumptions to hold. */
1480 gcc_assert (FREE_BIN_DELTA == MAX_ALIGNMENT);
1482 /* Set up the main zone by hand. */
1483 main_zone.name = "Main zone";
1484 G.zones = &main_zone;
1486 /* Allocate the default zones. */
1487 new_ggc_zone_1 (&rtl_zone, "RTL zone");
1488 new_ggc_zone_1 (&tree_zone, "Tree zone");
1489 new_ggc_zone_1 (&tree_id_zone, "Tree identifier zone");
1491 G.pagesize = getpagesize();
1492 G.lg_pagesize = exact_log2 (G.pagesize);
1493 G.page_mask = ~(G.pagesize - 1);
1495 /* Require the system page size to be a multiple of GGC_PAGE_SIZE. */
1496 gcc_assert ((G.pagesize & (GGC_PAGE_SIZE - 1)) == 0);
1498 /* Allocate 16 system pages at a time. */
1499 G.quire_size = 16 * G.pagesize / GGC_PAGE_SIZE;
1501 /* Calculate the size of the allocation bitmap and other overhead. */
1502 /* Right now we allocate bits for the page header and bitmap. These
1503 are wasted, but a little tricky to eliminate. */
1504 G.small_page_overhead
1505 = PAGE_OVERHEAD + (GGC_PAGE_SIZE / BYTES_PER_ALLOC_BIT / 8);
1506 /* G.small_page_overhead = ROUND_UP (G.small_page_overhead, MAX_ALIGNMENT); */
1508 #ifdef HAVE_MMAP_DEV_ZERO
1509 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1510 gcc_assert (G.dev_zero_fd != -1);
1511 #endif
1513 #if 0
1514 G.debug_file = fopen ("ggc-mmap.debug", "w");
1515 setlinebuf (G.debug_file);
1516 #else
1517 G.debug_file = stdout;
1518 #endif
1520 #ifdef USING_MMAP
1521 /* StunOS has an amazing off-by-one error for the first mmap allocation
1522 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1523 believe, is an unaligned page allocation, which would cause us to
1524 hork badly if we tried to use it. */
1526 char *p = alloc_anon (NULL, G.pagesize, &main_zone);
1527 struct small_page_entry *e;
1528 if ((size_t)p & (G.pagesize - 1))
1530 /* How losing. Discard this one and try another. If we still
1531 can't get something useful, give up. */
1533 p = alloc_anon (NULL, G.pagesize, &main_zone);
1534 gcc_assert (!((size_t)p & (G.pagesize - 1)));
1537 if (GGC_PAGE_SIZE == G.pagesize)
1539 /* We have a good page, might as well hold onto it... */
1540 e = xcalloc (1, G.small_page_overhead);
1541 e->common.page = p;
1542 e->common.zone = &main_zone;
1543 e->next = main_zone.free_pages;
1544 set_page_table_entry (e->common.page, &e->common);
1545 main_zone.free_pages = e;
1547 else
1549 munmap (p, G.pagesize);
1552 #endif
1555 /* Start a new GGC zone. */
1557 static void
1558 new_ggc_zone_1 (struct alloc_zone *new_zone, const char * name)
1560 new_zone->name = name;
1561 new_zone->next_zone = G.zones->next_zone;
1562 G.zones->next_zone = new_zone;
1565 struct alloc_zone *
1566 new_ggc_zone (const char * name)
1568 struct alloc_zone *new_zone = xcalloc (1, sizeof (struct alloc_zone));
1569 new_ggc_zone_1 (new_zone, name);
1570 return new_zone;
1573 /* Destroy a GGC zone. */
1574 void
1575 destroy_ggc_zone (struct alloc_zone * dead_zone)
1577 struct alloc_zone *z;
1579 for (z = G.zones; z && z->next_zone != dead_zone; z = z->next_zone)
1580 /* Just find that zone. */
1581 continue;
1583 /* We should have found the zone in the list. Anything else is fatal. */
1584 gcc_assert (z);
1586 /* z is dead, baby. z is dead. */
1587 z->dead = true;
1590 /* Free all empty pages and objects within a page for a given zone */
1592 static void
1593 sweep_pages (struct alloc_zone *zone)
1595 struct large_page_entry **lpp, *lp, *lnext;
1596 struct small_page_entry **spp, *sp, *snext;
1597 char *last_free;
1598 size_t allocated = 0;
1599 bool nomarksinpage;
1601 /* First, reset the free_chunks lists, since we are going to
1602 re-free free chunks in hopes of coalescing them into large chunks. */
1603 memset (zone->free_chunks, 0, sizeof (zone->free_chunks));
1604 zone->high_free_bin = 0;
1605 zone->cached_free = NULL;
1606 zone->cached_free_size = 0;
1608 /* Large pages are all or none affairs. Either they are completely
1609 empty, or they are completely full. */
1610 lpp = &zone->large_pages;
1611 for (lp = zone->large_pages; lp != NULL; lp = lnext)
1613 gcc_assert (lp->common.large_p);
1615 lnext = lp->next;
1617 #ifdef GATHER_STATISTICS
1618 /* This page has now survived another collection. */
1619 lp->common.survived++;
1620 #endif
1622 if (lp->mark_p)
1624 lp->mark_p = false;
1625 allocated += lp->bytes;
1626 lpp = &lp->next;
1628 else
1630 *lpp = lnext;
1631 #ifdef ENABLE_GC_CHECKING
1632 /* Poison the page. */
1633 memset (lp->common.page, 0xb5, SMALL_PAGE_SIZE);
1634 #endif
1635 if (lp->prev)
1636 lp->prev->next = lp->next;
1637 if (lp->next)
1638 lp->next->prev = lp->prev;
1639 free_large_page (lp);
1643 spp = &zone->pages;
1644 for (sp = zone->pages; sp != NULL; sp = snext)
1646 char *object, *last_object;
1647 char *end;
1648 alloc_type *alloc_word_p;
1649 mark_type *mark_word_p;
1651 gcc_assert (!sp->common.large_p);
1653 snext = sp->next;
1655 #ifdef GATHER_STATISTICS
1656 /* This page has now survived another collection. */
1657 sp->common.survived++;
1658 #endif
1660 /* Step through all chunks, consolidate those that are free and
1661 insert them into the free lists. Note that consolidation
1662 slows down collection slightly. */
1664 last_object = object = sp->common.page;
1665 end = sp->common.page + SMALL_PAGE_SIZE;
1666 last_free = NULL;
1667 nomarksinpage = true;
1668 mark_word_p = sp->mark_bits;
1669 alloc_word_p = sp->alloc_bits;
1671 gcc_assert (BYTES_PER_ALLOC_BIT == BYTES_PER_MARK_BIT);
1673 object = sp->common.page;
1676 unsigned int i, n;
1677 alloc_type alloc_word;
1678 mark_type mark_word;
1680 alloc_word = *alloc_word_p++;
1681 mark_word = *mark_word_p++;
1683 if (mark_word)
1684 nomarksinpage = false;
1686 /* There ought to be some way to do this without looping... */
1687 i = 0;
1688 while ((n = alloc_ffs (alloc_word)) != 0)
1690 /* Extend the current state for n - 1 bits. We can't
1691 shift alloc_word by n, even though it isn't used in the
1692 loop, in case only the highest bit was set. */
1693 alloc_word >>= n - 1;
1694 mark_word >>= n - 1;
1695 object += BYTES_PER_MARK_BIT * (n - 1);
1697 if (mark_word & 1)
1699 if (last_free)
1701 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (last_free,
1702 object
1703 - last_free));
1704 poison_region (last_free, object - last_free);
1705 free_chunk (last_free, object - last_free, zone);
1706 last_free = NULL;
1708 else
1709 allocated += object - last_object;
1710 last_object = object;
1712 else
1714 if (last_free == NULL)
1716 last_free = object;
1717 allocated += object - last_object;
1719 else
1720 zone_clear_object_alloc_bit (sp, object);
1723 /* Shift to just after the alloc bit we handled. */
1724 alloc_word >>= 1;
1725 mark_word >>= 1;
1726 object += BYTES_PER_MARK_BIT;
1728 i += n;
1731 object += BYTES_PER_MARK_BIT * (8 * sizeof (alloc_type) - i);
1733 while (object < end);
1735 if (nomarksinpage)
1737 *spp = snext;
1738 #ifdef ENABLE_GC_CHECKING
1739 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (sp->common.page,
1740 SMALL_PAGE_SIZE));
1741 /* Poison the page. */
1742 memset (sp->common.page, 0xb5, SMALL_PAGE_SIZE);
1743 #endif
1744 free_small_page (sp);
1745 continue;
1747 else if (last_free)
1749 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (last_free,
1750 object - last_free));
1751 poison_region (last_free, object - last_free);
1752 free_chunk (last_free, object - last_free, zone);
1754 else
1755 allocated += object - last_object;
1757 spp = &sp->next;
1760 zone->allocated = allocated;
1763 /* mark-and-sweep routine for collecting a single zone. NEED_MARKING
1764 is true if we need to mark before sweeping, false if some other
1765 zone collection has already performed marking for us. Returns true
1766 if we collected, false otherwise. */
1768 static bool
1769 ggc_collect_1 (struct alloc_zone *zone, bool need_marking)
1771 #if 0
1772 /* */
1774 int i;
1775 for (i = 0; i < NUM_FREE_BINS + 1; i++)
1777 struct alloc_chunk *chunk;
1778 int n, tot;
1780 n = 0;
1781 tot = 0;
1782 chunk = zone->free_chunks[i];
1783 while (chunk)
1785 n++;
1786 tot += chunk->size;
1787 chunk = chunk->next_free;
1789 fprintf (stderr, "Bin %d: %d free chunks (%d bytes)\n",
1790 i, n, tot);
1793 /* */
1794 #endif
1796 if (!quiet_flag)
1797 fprintf (stderr, " {%s GC %luk -> ",
1798 zone->name, (unsigned long) zone->allocated / 1024);
1800 /* Zero the total allocated bytes. This will be recalculated in the
1801 sweep phase. */
1802 zone->allocated = 0;
1804 /* Release the pages we freed the last time we collected, but didn't
1805 reuse in the interim. */
1806 release_pages (zone);
1808 if (need_marking)
1810 zone_allocate_marks ();
1811 ggc_mark_roots ();
1812 #ifdef GATHER_STATISTICS
1813 ggc_prune_overhead_list ();
1814 #endif
1817 sweep_pages (zone);
1818 zone->was_collected = true;
1819 zone->allocated_last_gc = zone->allocated;
1821 if (!quiet_flag)
1822 fprintf (stderr, "%luk}", (unsigned long) zone->allocated / 1024);
1823 return true;
1826 #ifdef GATHER_STATISTICS
1827 /* Calculate the average page survival rate in terms of number of
1828 collections. */
1830 static float
1831 calculate_average_page_survival (struct alloc_zone *zone)
1833 float count = 0.0;
1834 float survival = 0.0;
1835 struct small_page_entry *p;
1836 struct large_page_entry *lp;
1837 for (p = zone->pages; p; p = p->next)
1839 count += 1.0;
1840 survival += p->common.survived;
1842 for (lp = zone->large_pages; lp; lp = lp->next)
1844 count += 1.0;
1845 survival += lp->common.survived;
1847 return survival/count;
1849 #endif
1851 /* Top level collection routine. */
1853 void
1854 ggc_collect (void)
1856 struct alloc_zone *zone;
1857 bool marked = false;
1859 timevar_push (TV_GC);
1861 if (!ggc_force_collect)
1863 float allocated_last_gc = 0, allocated = 0, min_expand;
1865 for (zone = G.zones; zone; zone = zone->next_zone)
1867 allocated_last_gc += zone->allocated_last_gc;
1868 allocated += zone->allocated;
1871 allocated_last_gc =
1872 MAX (allocated_last_gc,
1873 (size_t) PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
1874 min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
1876 if (allocated < allocated_last_gc + min_expand)
1878 timevar_pop (TV_GC);
1879 return;
1883 /* Start by possibly collecting the main zone. */
1884 main_zone.was_collected = false;
1885 marked |= ggc_collect_1 (&main_zone, true);
1887 /* In order to keep the number of collections down, we don't
1888 collect other zones unless we are collecting the main zone. This
1889 gives us roughly the same number of collections as we used to
1890 have with the old gc. The number of collection is important
1891 because our main slowdown (according to profiling) is now in
1892 marking. So if we mark twice as often as we used to, we'll be
1893 twice as slow. Hopefully we'll avoid this cost when we mark
1894 zone-at-a-time. */
1895 /* NOTE drow/2004-07-28: We now always collect the main zone, but
1896 keep this code in case the heuristics are further refined. */
1898 if (main_zone.was_collected)
1900 struct alloc_zone *zone;
1902 for (zone = main_zone.next_zone; zone; zone = zone->next_zone)
1904 zone->was_collected = false;
1905 marked |= ggc_collect_1 (zone, !marked);
1909 #ifdef GATHER_STATISTICS
1910 /* Print page survival stats, if someone wants them. */
1911 if (GGC_DEBUG_LEVEL >= 2)
1913 for (zone = G.zones; zone; zone = zone->next_zone)
1915 if (zone->was_collected)
1917 float f = calculate_average_page_survival (zone);
1918 printf ("Average page survival in zone `%s' is %f\n",
1919 zone->name, f);
1923 #endif
1925 if (marked)
1926 zone_free_marks ();
1928 /* Free dead zones. */
1929 for (zone = G.zones; zone && zone->next_zone; zone = zone->next_zone)
1931 if (zone->next_zone->dead)
1933 struct alloc_zone *dead_zone = zone->next_zone;
1935 printf ("Zone `%s' is dead and will be freed.\n", dead_zone->name);
1937 /* The zone must be empty. */
1938 gcc_assert (!dead_zone->allocated);
1940 /* Unchain the dead zone, release all its pages and free it. */
1941 zone->next_zone = zone->next_zone->next_zone;
1942 release_pages (dead_zone);
1943 free (dead_zone);
1947 timevar_pop (TV_GC);
1950 /* Print allocation statistics. */
1951 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1952 ? (x) \
1953 : ((x) < 1024*1024*10 \
1954 ? (x) / 1024 \
1955 : (x) / (1024*1024))))
1956 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1958 void
1959 ggc_print_statistics (void)
1961 struct alloc_zone *zone;
1962 struct ggc_statistics stats;
1963 size_t total_overhead = 0, total_allocated = 0, total_bytes_mapped = 0;
1964 size_t pte_overhead, i;
1966 /* Clear the statistics. */
1967 memset (&stats, 0, sizeof (stats));
1969 /* Make sure collection will really occur. */
1970 ggc_force_collect = true;
1972 /* Collect and print the statistics common across collectors. */
1973 ggc_print_common_statistics (stderr, &stats);
1975 ggc_force_collect = false;
1977 /* Release free pages so that we will not count the bytes allocated
1978 there as part of the total allocated memory. */
1979 for (zone = G.zones; zone; zone = zone->next_zone)
1980 release_pages (zone);
1982 /* Collect some information about the various sizes of
1983 allocation. */
1984 fprintf (stderr,
1985 "Memory still allocated at the end of the compilation process\n");
1987 fprintf (stderr, "%20s %10s %10s %10s\n",
1988 "Zone", "Allocated", "Used", "Overhead");
1989 for (zone = G.zones; zone; zone = zone->next_zone)
1991 struct large_page_entry *large_page;
1992 size_t overhead, allocated, in_use;
1994 /* Skip empty zones. */
1995 if (!zone->pages && !zone->large_pages)
1996 continue;
1998 allocated = in_use = 0;
2000 overhead = sizeof (struct alloc_zone);
2002 for (large_page = zone->large_pages; large_page != NULL;
2003 large_page = large_page->next)
2005 allocated += large_page->bytes;
2006 in_use += large_page->bytes;
2007 overhead += sizeof (struct large_page_entry);
2010 /* There's no easy way to walk through the small pages finding
2011 used and unused objects. Instead, add all the pages, and
2012 subtract out the free list. */
2014 allocated += GGC_PAGE_SIZE * zone->n_small_pages;
2015 in_use += GGC_PAGE_SIZE * zone->n_small_pages;
2016 overhead += G.small_page_overhead * zone->n_small_pages;
2018 for (i = 0; i <= NUM_FREE_BINS; i++)
2020 struct alloc_chunk *chunk = zone->free_chunks[i];
2021 while (chunk)
2023 in_use -= ggc_get_size (chunk);
2024 chunk = chunk->next_free;
2028 fprintf (stderr, "%20s %10lu%c %10lu%c %10lu%c\n",
2029 zone->name,
2030 SCALE (allocated), LABEL (allocated),
2031 SCALE (in_use), LABEL (in_use),
2032 SCALE (overhead), LABEL (overhead));
2034 gcc_assert (in_use == zone->allocated);
2036 total_overhead += overhead;
2037 total_allocated += zone->allocated;
2038 total_bytes_mapped += zone->bytes_mapped;
2041 /* Count the size of the page table as best we can. */
2042 #if HOST_BITS_PER_PTR <= 32
2043 pte_overhead = sizeof (G.lookup);
2044 for (i = 0; i < PAGE_L1_SIZE; i++)
2045 if (G.lookup[i])
2046 pte_overhead += PAGE_L2_SIZE * sizeof (struct page_entry *);
2047 #else
2049 page_table table = G.lookup;
2050 pte_overhead = 0;
2051 while (table)
2053 pte_overhead += sizeof (*table);
2054 for (i = 0; i < PAGE_L1_SIZE; i++)
2055 if (table->table[i])
2056 pte_overhead += PAGE_L2_SIZE * sizeof (struct page_entry *);
2057 table = table->next;
2060 #endif
2061 fprintf (stderr, "%20s %11s %11s %10lu%c\n", "Page Table",
2062 "", "", SCALE (pte_overhead), LABEL (pte_overhead));
2063 total_overhead += pte_overhead;
2065 fprintf (stderr, "%20s %10lu%c %10lu%c %10lu%c\n", "Total",
2066 SCALE (total_bytes_mapped), LABEL (total_bytes_mapped),
2067 SCALE (total_allocated), LABEL(total_allocated),
2068 SCALE (total_overhead), LABEL (total_overhead));
2070 #ifdef GATHER_STATISTICS
2072 unsigned long long all_overhead = 0, all_allocated = 0;
2073 unsigned long long all_overhead_under32 = 0, all_allocated_under32 = 0;
2074 unsigned long long all_overhead_under64 = 0, all_allocated_under64 = 0;
2075 unsigned long long all_overhead_under128 = 0, all_allocated_under128 = 0;
2077 fprintf (stderr, "\nTotal allocations and overheads during the compilation process\n");
2079 for (zone = G.zones; zone; zone = zone->next_zone)
2081 all_overhead += zone->stats.total_overhead;
2082 all_allocated += zone->stats.total_allocated;
2084 all_allocated_under32 += zone->stats.total_allocated_under32;
2085 all_overhead_under32 += zone->stats.total_overhead_under32;
2087 all_allocated_under64 += zone->stats.total_allocated_under64;
2088 all_overhead_under64 += zone->stats.total_overhead_under64;
2090 all_allocated_under128 += zone->stats.total_allocated_under128;
2091 all_overhead_under128 += zone->stats.total_overhead_under128;
2093 fprintf (stderr, "%20s: %10lld\n",
2094 zone->name, zone->stats.total_allocated);
2097 fprintf (stderr, "\n");
2099 fprintf (stderr, "Total Overhead: %10lld\n",
2100 all_overhead);
2101 fprintf (stderr, "Total Allocated: %10lld\n",
2102 all_allocated);
2104 fprintf (stderr, "Total Overhead under 32B: %10lld\n",
2105 all_overhead_under32);
2106 fprintf (stderr, "Total Allocated under 32B: %10lld\n",
2107 all_allocated_under32);
2108 fprintf (stderr, "Total Overhead under 64B: %10lld\n",
2109 all_overhead_under64);
2110 fprintf (stderr, "Total Allocated under 64B: %10lld\n",
2111 all_allocated_under64);
2112 fprintf (stderr, "Total Overhead under 128B: %10lld\n",
2113 all_overhead_under128);
2114 fprintf (stderr, "Total Allocated under 128B: %10lld\n",
2115 all_allocated_under128);
2117 #endif
2120 /* Precompiled header support. */
2122 /* For precompiled headers, we sort objects based on their type. We
2123 also sort various objects into their own buckets; currently this
2124 covers strings and IDENTIFIER_NODE trees. The choices of how
2125 to sort buckets have not yet been tuned. */
2127 #define NUM_PCH_BUCKETS (gt_types_enum_last + 3)
2129 #define OTHER_BUCKET (gt_types_enum_last + 0)
2130 #define IDENTIFIER_BUCKET (gt_types_enum_last + 1)
2131 #define STRING_BUCKET (gt_types_enum_last + 2)
2133 struct ggc_pch_ondisk
2135 size_t total;
2136 size_t type_totals[NUM_PCH_BUCKETS];
2139 struct ggc_pch_data
2141 struct ggc_pch_ondisk d;
2142 size_t base;
2143 size_t orig_base;
2144 size_t alloc_size;
2145 alloc_type *alloc_bits;
2146 size_t type_bases[NUM_PCH_BUCKETS];
2147 size_t start_offset;
2150 /* Initialize the PCH data structure. */
2152 struct ggc_pch_data *
2153 init_ggc_pch (void)
2155 return xcalloc (sizeof (struct ggc_pch_data), 1);
2158 /* Return which of the page-aligned buckets the object at X, with type
2159 TYPE, should be sorted into in the PCH. Strings will have
2160 IS_STRING set and TYPE will be gt_types_enum_last. Other objects
2161 of unknown type will also have TYPE equal to gt_types_enum_last. */
2163 static int
2164 pch_bucket (void *x, enum gt_types_enum type,
2165 bool is_string)
2167 /* Sort identifiers into their own bucket, to improve locality
2168 when searching the identifier hash table. */
2169 if (type == gt_ggc_e_14lang_tree_node
2170 && TREE_CODE ((tree) x) == IDENTIFIER_NODE)
2171 return IDENTIFIER_BUCKET;
2172 else if (type == gt_types_enum_last)
2174 if (is_string)
2175 return STRING_BUCKET;
2176 return OTHER_BUCKET;
2178 return type;
2181 /* Add the size of object X to the size of the PCH data. */
2183 void
2184 ggc_pch_count_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
2185 size_t size, bool is_string, enum gt_types_enum type)
2187 /* NOTE: Right now we don't need to align up the size of any objects.
2188 Strings can be unaligned, and everything else is allocated to a
2189 MAX_ALIGNMENT boundary already. */
2191 d->d.type_totals[pch_bucket (x, type, is_string)] += size;
2194 /* Return the total size of the PCH data. */
2196 size_t
2197 ggc_pch_total_size (struct ggc_pch_data *d)
2199 enum gt_types_enum i;
2200 size_t alloc_size, total_size;
2202 total_size = 0;
2203 for (i = 0; i < NUM_PCH_BUCKETS; i++)
2205 d->d.type_totals[i] = ROUND_UP (d->d.type_totals[i], GGC_PAGE_SIZE);
2206 total_size += d->d.type_totals[i];
2208 d->d.total = total_size;
2210 /* Include the size of the allocation bitmap. */
2211 alloc_size = CEIL (d->d.total, BYTES_PER_ALLOC_BIT * 8);
2212 alloc_size = ROUND_UP (alloc_size, MAX_ALIGNMENT);
2213 d->alloc_size = alloc_size;
2215 return d->d.total + alloc_size;
2218 /* Set the base address for the objects in the PCH file. */
2220 void
2221 ggc_pch_this_base (struct ggc_pch_data *d, void *base_)
2223 int i;
2224 size_t base = (size_t) base_;
2226 d->base = d->orig_base = base;
2227 for (i = 0; i < NUM_PCH_BUCKETS; i++)
2229 d->type_bases[i] = base;
2230 base += d->d.type_totals[i];
2233 if (d->alloc_bits == NULL)
2234 d->alloc_bits = xcalloc (1, d->alloc_size);
2237 /* Allocate a place for object X of size SIZE in the PCH file. */
2239 char *
2240 ggc_pch_alloc_object (struct ggc_pch_data *d, void *x,
2241 size_t size, bool is_string,
2242 enum gt_types_enum type)
2244 size_t alloc_word, alloc_bit;
2245 char *result;
2246 int bucket = pch_bucket (x, type, is_string);
2248 /* Record the start of the object in the allocation bitmap. We
2249 can't assert that the allocation bit is previously clear, because
2250 strings may violate the invariant that they are at least
2251 BYTES_PER_ALLOC_BIT long. This is harmless - ggc_get_size
2252 should not be called for strings. */
2253 alloc_word = ((d->type_bases[bucket] - d->orig_base)
2254 / (8 * sizeof (alloc_type) * BYTES_PER_ALLOC_BIT));
2255 alloc_bit = ((d->type_bases[bucket] - d->orig_base)
2256 / BYTES_PER_ALLOC_BIT) % (8 * sizeof (alloc_type));
2257 d->alloc_bits[alloc_word] |= 1L << alloc_bit;
2259 /* Place the object at the current pointer for this bucket. */
2260 result = (char *) d->type_bases[bucket];
2261 d->type_bases[bucket] += size;
2262 return result;
2265 /* Prepare to write out the PCH data to file F. */
2267 void
2268 ggc_pch_prepare_write (struct ggc_pch_data *d,
2269 FILE *f)
2271 /* We seek around a lot while writing. Record where the end
2272 of the padding in the PCH file is, so that we can
2273 locate each object's offset. */
2274 d->start_offset = ftell (f);
2277 /* Write out object X of SIZE to file F. */
2279 void
2280 ggc_pch_write_object (struct ggc_pch_data *d,
2281 FILE *f, void *x, void *newx,
2282 size_t size, bool is_string ATTRIBUTE_UNUSED)
2284 if (fseek (f, (size_t) newx - d->orig_base + d->start_offset, SEEK_SET) != 0)
2285 fatal_error ("can't seek PCH file: %m");
2287 if (fwrite (x, size, 1, f) != 1)
2288 fatal_error ("can't write PCH file: %m");
2291 void
2292 ggc_pch_finish (struct ggc_pch_data *d, FILE *f)
2294 /* Write out the allocation bitmap. */
2295 if (fseek (f, d->start_offset + d->d.total, SEEK_SET) != 0)
2296 fatal_error ("can't seek PCH file: %m");
2298 if (fwrite (d->alloc_bits, d->alloc_size, 1, f) != 1)
2299 fatal_error ("can't write PCH fle: %m");
2301 /* Done with the PCH, so write out our footer. */
2302 if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
2303 fatal_error ("can't write PCH file: %m");
2305 free (d->alloc_bits);
2306 free (d);
2309 /* The PCH file from F has been mapped at ADDR. Read in any
2310 additional data from the file and set up the GC state. */
2312 void
2313 ggc_pch_read (FILE *f, void *addr)
2315 struct ggc_pch_ondisk d;
2316 size_t alloc_size;
2317 struct alloc_zone *zone;
2318 struct page_entry *pch_page;
2319 char *p;
2321 if (fread (&d, sizeof (d), 1, f) != 1)
2322 fatal_error ("can't read PCH file: %m");
2324 alloc_size = CEIL (d.total, BYTES_PER_ALLOC_BIT * 8);
2325 alloc_size = ROUND_UP (alloc_size, MAX_ALIGNMENT);
2327 pch_zone.bytes = d.total;
2328 pch_zone.alloc_bits = (alloc_type *) ((char *) addr + pch_zone.bytes);
2329 pch_zone.page = (char *) addr;
2330 pch_zone.end = (char *) pch_zone.alloc_bits;
2332 /* We've just read in a PCH file. So, every object that used to be
2333 allocated is now free. */
2334 for (zone = G.zones; zone; zone = zone->next_zone)
2336 struct small_page_entry *page, *next_page;
2337 struct large_page_entry *large_page, *next_large_page;
2339 zone->allocated = 0;
2341 /* Clear the zone's free chunk list. */
2342 memset (zone->free_chunks, 0, sizeof (zone->free_chunks));
2343 zone->high_free_bin = 0;
2344 zone->cached_free = NULL;
2345 zone->cached_free_size = 0;
2347 /* Move all the small pages onto the free list. */
2348 for (page = zone->pages; page != NULL; page = next_page)
2350 next_page = page->next;
2351 memset (page->alloc_bits, 0,
2352 G.small_page_overhead - PAGE_OVERHEAD);
2353 free_small_page (page);
2356 /* Discard all the large pages. */
2357 for (large_page = zone->large_pages; large_page != NULL;
2358 large_page = next_large_page)
2360 next_large_page = large_page->next;
2361 free_large_page (large_page);
2364 zone->pages = NULL;
2365 zone->large_pages = NULL;
2368 /* Allocate the dummy page entry for the PCH, and set all pages
2369 mapped into the PCH to reference it. */
2370 pch_page = xcalloc (1, sizeof (struct page_entry));
2371 pch_page->page = pch_zone.page;
2372 pch_page->pch_p = true;
2374 for (p = pch_zone.page; p < pch_zone.end; p += GGC_PAGE_SIZE)
2375 set_page_table_entry (p, pch_page);