1 /* "Bag-of-pages" zone garbage collector for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Richard Henderson (rth@redhat.com) and Daniel Berlin (dberlin@dberlin.org)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
38 #ifdef ENABLE_VALGRIND_CHECKING
39 # ifdef HAVE_VALGRIND_MEMCHECK_H
40 # include <valgrind/memcheck.h>
41 # elif defined HAVE_MEMCHECK_H
42 # include <memcheck.h>
44 # include <valgrind.h>
47 /* Avoid #ifdef:s when we can help it. */
48 #define VALGRIND_DISCARD(x)
49 #define VALGRIND_MALLOCLIKE_BLOCK(w,x,y,z)
50 #define VALGRIND_FREELIKE_BLOCK(x,y)
52 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
53 file open. Prefer either to valloc. */
55 # undef HAVE_MMAP_DEV_ZERO
57 # include <sys/mman.h>
59 # define MAP_FAILED -1
61 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
62 # define MAP_ANONYMOUS MAP_ANON
68 #ifdef HAVE_MMAP_DEV_ZERO
70 # include <sys/mman.h>
72 # define MAP_FAILED -1
79 #define USING_MALLOC_PAGE_GROUPS
82 #if (GCC_VERSION < 3001)
83 #define prefetch(X) ((void) X)
85 #define prefetch(X) __builtin_prefetch (X)
89 If we track inter-zone pointers, we can mark single zones at a
91 If we have a zone where we guarantee no inter-zone pointers, we
92 could mark that zone separately.
93 The garbage zone should not be marked, and we should return 1 in
94 ggc_set_mark for any object in the garbage zone, which cuts off
98 This garbage-collecting allocator segregates objects into zones.
99 It also segregates objects into "large" and "small" bins. Large
100 objects are greater or equal to page size.
102 Pages for small objects are broken up into chunks, each of which
103 are described by a struct alloc_chunk. One can walk over all
104 chunks on the page by adding the chunk size to the chunk's data
105 address. The free space for a page exists in the free chunk bins.
107 Each page-entry also has a context depth, which is used to track
108 pushing and popping of allocation contexts. Only objects allocated
109 in the current (highest-numbered) context may be collected.
111 Empty pages (of all sizes) are kept on a single page cache list,
112 and are considered first when new pages are required; they are
113 deallocated at the start of the next collection if they haven't
114 been recycled by then. */
116 /* Define GGC_DEBUG_LEVEL to print debugging information.
117 0: No debugging output.
118 1: GC statistics only.
119 2: Page-entry allocations/deallocations as well.
120 3: Object allocations as well.
121 4: Object marks as well. */
122 #define GGC_DEBUG_LEVEL (0)
124 #ifndef HOST_BITS_PER_PTR
125 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
127 #ifdef COOKIE_CHECKING
128 #define CHUNK_MAGIC 0x95321123
129 #define DEADCHUNK_MAGIC 0x12817317
132 /* This structure manages small chunks. When the chunk is free, it's
133 linked with other chunks via free_next. When the chunk is allocated,
134 the data starts at u. Large chunks are allocated one at a time to
135 their own page, and so don't come in here.
137 The "type" field is a placeholder for a future change to do
138 generational collection. At present it is 0 when free and
139 and 1 when allocated. */
142 #ifdef COOKIE_CHECKING
146 unsigned int typecode
:15;
147 unsigned int size
:15;
150 struct alloc_chunk
*next_free
;
153 /* Make sure the data is sufficiently aligned. */
154 HOST_WIDEST_INT align_i
;
155 #ifdef HAVE_LONG_DOUBLE
161 } __attribute__ ((packed
));
163 #define CHUNK_OVERHEAD (offsetof (struct alloc_chunk, u))
165 /* We maintain several bins of free lists for chunks for very small
166 objects. We never exhaustively search other bins -- if we don't
167 find one of the proper size, we allocate from the "larger" bin. */
169 /* Decreasing the number of free bins increases the time it takes to allocate.
170 Similar with increasing max_free_bin_size without increasing num_free_bins.
172 After much histogramming of allocation sizes and time spent on gc,
173 on a powerpc G4 7450 - 667 mhz, and an pentium 4 - 2.8ghz,
174 these were determined to be the optimal values. */
175 #define NUM_FREE_BINS 64
176 #define MAX_FREE_BIN_SIZE 256
177 #define FREE_BIN_DELTA (MAX_FREE_BIN_SIZE / NUM_FREE_BINS)
178 #define SIZE_BIN_UP(SIZE) (((SIZE) + FREE_BIN_DELTA - 1) / FREE_BIN_DELTA)
179 #define SIZE_BIN_DOWN(SIZE) ((SIZE) / FREE_BIN_DELTA)
181 /* Marker used as chunk->size for a large object. Should correspond
182 to the size of the bitfield above. */
183 #define LARGE_OBJECT_SIZE 0x7fff
185 /* We use this structure to determine the alignment required for
186 allocations. For power-of-two sized allocations, that's not a
187 problem, but it does matter for odd-sized allocations. */
189 struct max_alignment
{
193 #ifdef HAVE_LONG_DOUBLE
201 /* The biggest alignment required. */
203 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
205 /* Compute the smallest nonnegative number which when added to X gives
208 #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
210 /* Compute the smallest multiple of F that is >= X. */
212 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
214 /* A two-level tree is used to look up the page-entry for a given
215 pointer. Two chunks of the pointer's bits are extracted to index
216 the first and second levels of the tree, as follows:
220 msb +----------------+----+------+------+ lsb
226 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
227 pages are aligned on system page boundaries. The next most
228 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
229 index values in the lookup table, respectively.
231 For 32-bit architectures and the settings below, there are no
232 leftover bits. For architectures with wider pointers, the lookup
233 tree points to a list of pages, which must be scanned to find the
236 #define PAGE_L1_BITS (8)
237 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
238 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
239 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
241 #define LOOKUP_L1(p) \
242 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
244 #define LOOKUP_L2(p) \
245 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
247 /* A page_entry records the status of an allocation page. */
248 typedef struct page_entry
250 /* The next page-entry with objects of the same size, or NULL if
251 this is the last page-entry. */
252 struct page_entry
*next
;
254 /* The number of bytes allocated. (This will always be a multiple
255 of the host system page size.) */
258 /* How many collections we've survived. */
261 /* The address at which the memory is allocated. */
264 #ifdef USING_MALLOC_PAGE_GROUPS
265 /* Back pointer to the page group this page came from. */
266 struct page_group
*group
;
269 /* Number of bytes on the page unallocated. Only used during
270 collection, and even then large pages merely set this nonzero. */
273 /* Context depth of this page. */
274 unsigned short context_depth
;
276 /* Does this page contain small objects, or one large object? */
279 /* The zone that this page entry belongs to. */
280 struct alloc_zone
*zone
;
283 #ifdef USING_MALLOC_PAGE_GROUPS
284 /* A page_group describes a large allocation from malloc, from which
285 we parcel out aligned pages. */
286 typedef struct page_group
288 /* A linked list of all extant page groups. */
289 struct page_group
*next
;
291 /* The address we received from malloc. */
294 /* The size of the block. */
297 /* A bitmask of pages in use. */
302 #if HOST_BITS_PER_PTR <= 32
304 /* On 32-bit hosts, we use a two level page table, as pictured above. */
305 typedef page_entry
**page_table
[PAGE_L1_SIZE
];
309 /* On 64-bit hosts, we use the same two level page tables plus a linked
310 list that disambiguates the top 32-bits. There will almost always be
311 exactly one entry in the list. */
312 typedef struct page_table_chain
314 struct page_table_chain
*next
;
316 page_entry
**table
[PAGE_L1_SIZE
];
321 /* The global variables. */
322 static struct globals
324 /* The page lookup table. A single page can only belong to one
325 zone. This means free pages are zone-specific ATM. */
327 /* The linked list of zones. */
328 struct alloc_zone
*zones
;
330 /* The system's page size. */
334 /* A file descriptor open to /dev/zero for reading. */
335 #if defined (HAVE_MMAP_DEV_ZERO)
339 /* The file descriptor for debugging output. */
343 /* The zone allocation structure. */
346 /* Name of the zone. */
349 /* Linked list of pages in a zone. */
352 /* Linked lists of free storage. Slots 1 ... NUM_FREE_BINS have chunks of size
353 FREE_BIN_DELTA. All other chunks are in slot 0. */
354 struct alloc_chunk
*free_chunks
[NUM_FREE_BINS
+ 1];
356 /* Bytes currently allocated. */
359 /* Bytes currently allocated at the end of the last collection. */
360 size_t allocated_last_gc
;
362 /* Total amount of memory mapped. */
365 /* Bit N set if any allocations have been done at context depth N. */
366 unsigned long context_depth_allocations
;
368 /* Bit N set if any collections have been done at context depth N. */
369 unsigned long context_depth_collections
;
371 /* The current depth in the context stack. */
372 unsigned short context_depth
;
374 /* A cache of free system pages. */
375 page_entry
*free_pages
;
377 #ifdef USING_MALLOC_PAGE_GROUPS
378 page_group
*page_groups
;
381 /* Next zone in the linked list of zones. */
382 struct alloc_zone
*next_zone
;
384 /* True if this zone was collected during this collection. */
387 /* True if this zone should be destroyed after the next collection. */
391 struct alloc_zone
*rtl_zone
;
392 struct alloc_zone
*garbage_zone
;
393 struct alloc_zone
*tree_zone
;
395 /* Allocate pages in chunks of this size, to throttle calls to memory
396 allocation routines. The first page is used, the rest go onto the
397 free list. This cannot be larger than HOST_BITS_PER_INT for the
398 in_use bitmask for page_group. */
399 #define GGC_QUIRE_SIZE 16
401 static int ggc_allocated_p (const void *);
402 static page_entry
*lookup_page_table_entry (const void *);
403 static void set_page_table_entry (void *, page_entry
*);
405 static char *alloc_anon (char *, size_t, struct alloc_zone
*);
407 #ifdef USING_MALLOC_PAGE_GROUPS
408 static size_t page_group_index (char *, char *);
409 static void set_page_group_in_use (page_group
*, char *);
410 static void clear_page_group_in_use (page_group
*, char *);
412 static struct page_entry
* alloc_small_page ( struct alloc_zone
*);
413 static struct page_entry
* alloc_large_page (size_t, struct alloc_zone
*);
414 static void free_chunk (struct alloc_chunk
*, size_t, struct alloc_zone
*);
415 static void free_page (struct page_entry
*);
416 static void release_pages (struct alloc_zone
*);
417 static void sweep_pages (struct alloc_zone
*);
418 static void * ggc_alloc_zone_1 (size_t, struct alloc_zone
*, short);
419 static bool ggc_collect_1 (struct alloc_zone
*, bool);
420 static void check_cookies (void);
423 /* Returns nonzero if P was allocated in GC'able memory. */
426 ggc_allocated_p (const void *p
)
431 #if HOST_BITS_PER_PTR <= 32
434 page_table table
= G
.lookup
;
435 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
440 if (table
->high_bits
== high_bits
)
444 base
= &table
->table
[0];
447 /* Extract the level 1 and 2 indices. */
451 return base
[L1
] && base
[L1
][L2
];
454 /* Traverse the page table and find the entry for a page.
455 Die (probably) if the object wasn't allocated via GC. */
457 static inline page_entry
*
458 lookup_page_table_entry(const void *p
)
463 #if HOST_BITS_PER_PTR <= 32
466 page_table table
= G
.lookup
;
467 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
468 while (table
->high_bits
!= high_bits
)
470 base
= &table
->table
[0];
473 /* Extract the level 1 and 2 indices. */
481 /* Set the page table entry for a page. */
484 set_page_table_entry(void *p
, page_entry
*entry
)
489 #if HOST_BITS_PER_PTR <= 32
493 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
494 for (table
= G
.lookup
; table
; table
= table
->next
)
495 if (table
->high_bits
== high_bits
)
498 /* Not found -- allocate a new table. */
499 table
= (page_table
) xcalloc (1, sizeof(*table
));
500 table
->next
= G
.lookup
;
501 table
->high_bits
= high_bits
;
504 base
= &table
->table
[0];
507 /* Extract the level 1 and 2 indices. */
511 if (base
[L1
] == NULL
)
512 base
[L1
] = (page_entry
**) xcalloc (PAGE_L2_SIZE
, sizeof (page_entry
*));
514 base
[L1
][L2
] = entry
;
518 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
519 (if non-null). The ifdef structure here is intended to cause a
520 compile error unless exactly one of the HAVE_* is defined. */
523 alloc_anon (char *pref ATTRIBUTE_UNUSED
, size_t size
, struct alloc_zone
*zone
)
525 #ifdef HAVE_MMAP_ANON
526 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
527 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
529 #ifdef HAVE_MMAP_DEV_ZERO
530 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
531 MAP_PRIVATE
, G
.dev_zero_fd
, 0);
533 VALGRIND_MALLOCLIKE_BLOCK(page
, size
, 0, 0);
535 if (page
== (char *) MAP_FAILED
)
537 perror ("virtual memory exhausted");
538 exit (FATAL_EXIT_CODE
);
541 /* Remember that we allocated this memory. */
542 zone
->bytes_mapped
+= size
;
543 /* Pretend we don't have access to the allocated pages. We'll enable
544 access to smaller pieces of the area in ggc_alloc. Discard the
545 handle to avoid handle leak. */
546 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (page
, size
));
550 #ifdef USING_MALLOC_PAGE_GROUPS
551 /* Compute the index for this page into the page group. */
554 page_group_index (char *allocation
, char *page
)
556 return (size_t) (page
- allocation
) >> G
.lg_pagesize
;
559 /* Set and clear the in_use bit for this page in the page group. */
562 set_page_group_in_use (page_group
*group
, char *page
)
564 group
->in_use
|= 1 << page_group_index (group
->allocation
, page
);
568 clear_page_group_in_use (page_group
*group
, char *page
)
570 group
->in_use
&= ~(1 << page_group_index (group
->allocation
, page
));
574 /* Allocate a new page for allocating objects of size 2^ORDER,
575 and return an entry for it. The entry is not added to the
576 appropriate page_table list. */
578 static inline struct page_entry
*
579 alloc_small_page (struct alloc_zone
*zone
)
581 struct page_entry
*entry
;
583 #ifdef USING_MALLOC_PAGE_GROUPS
589 /* Check the list of free pages for one we can use. */
590 entry
= zone
->free_pages
;
593 /* Recycle the allocated memory from this page ... */
594 zone
->free_pages
= entry
->next
;
597 #ifdef USING_MALLOC_PAGE_GROUPS
598 group
= entry
->group
;
604 /* We want just one page. Allocate a bunch of them and put the
605 extras on the freelist. (Can only do this optimization with
606 mmap for backing store.) */
607 struct page_entry
*e
, *f
= zone
->free_pages
;
610 page
= alloc_anon (NULL
, G
.pagesize
* GGC_QUIRE_SIZE
, zone
);
612 /* This loop counts down so that the chain will be in ascending
614 for (i
= GGC_QUIRE_SIZE
- 1; i
>= 1; i
--)
616 e
= (struct page_entry
*) xmalloc (sizeof (struct page_entry
));
617 e
->bytes
= G
.pagesize
;
618 e
->page
= page
+ (i
<< G
.lg_pagesize
);
623 zone
->free_pages
= f
;
626 #ifdef USING_MALLOC_PAGE_GROUPS
629 /* Allocate a large block of memory and serve out the aligned
630 pages therein. This results in much less memory wastage
631 than the traditional implementation of valloc. */
633 char *allocation
, *a
, *enda
;
634 size_t alloc_size
, head_slop
, tail_slop
;
635 int multiple_pages
= (entry_size
== G
.pagesize
);
638 alloc_size
= GGC_QUIRE_SIZE
* G
.pagesize
;
640 alloc_size
= entry_size
+ G
.pagesize
- 1;
641 allocation
= xmalloc (alloc_size
);
642 VALGRIND_MALLOCLIKE_BLOCK(addr
, alloc_size
, 0, 0);
644 page
= (char *) (((size_t) allocation
+ G
.pagesize
- 1) & -G
.pagesize
);
645 head_slop
= page
- allocation
;
647 tail_slop
= ((size_t) allocation
+ alloc_size
) & (G
.pagesize
- 1);
649 tail_slop
= alloc_size
- entry_size
- head_slop
;
650 enda
= allocation
+ alloc_size
- tail_slop
;
652 /* We allocated N pages, which are likely not aligned, leaving
653 us with N-1 usable pages. We plan to place the page_group
654 structure somewhere in the slop. */
655 if (head_slop
>= sizeof (page_group
))
656 group
= (page_group
*)page
- 1;
659 /* We magically got an aligned allocation. Too bad, we have
660 to waste a page anyway. */
664 tail_slop
+= G
.pagesize
;
666 if (tail_slop
< sizeof (page_group
))
668 group
= (page_group
*)enda
;
669 tail_slop
-= sizeof (page_group
);
672 /* Remember that we allocated this memory. */
673 group
->next
= G
.page_groups
;
674 group
->allocation
= allocation
;
675 group
->alloc_size
= alloc_size
;
677 zone
->page_groups
= group
;
678 G
.bytes_mapped
+= alloc_size
;
680 /* If we allocated multiple pages, put the rest on the free list. */
683 struct page_entry
*e
, *f
= G
.free_pages
;
684 for (a
= enda
- G
.pagesize
; a
!= page
; a
-= G
.pagesize
)
686 e
= (struct page_entry
*) xmalloc (sizeof (struct page_entry
));
687 e
->bytes
= G
.pagesize
;
693 zone
->free_pages
= f
;
699 entry
= (struct page_entry
*) xmalloc (sizeof (struct page_entry
));
702 entry
->bytes
= G
.pagesize
;
703 entry
->bytes_free
= G
.pagesize
;
705 entry
->context_depth
= zone
->context_depth
;
706 entry
->large_p
= false;
708 zone
->context_depth_allocations
|= (unsigned long)1 << zone
->context_depth
;
710 #ifdef USING_MALLOC_PAGE_GROUPS
711 entry
->group
= group
;
712 set_page_group_in_use (group
, page
);
715 set_page_table_entry (page
, entry
);
717 if (GGC_DEBUG_LEVEL
>= 2)
718 fprintf (G
.debug_file
,
719 "Allocating %s page at %p, data %p-%p\n", entry
->zone
->name
,
720 (PTR
) entry
, page
, page
+ G
.pagesize
- 1);
725 /* Allocate a large page of size SIZE in ZONE. */
727 static inline struct page_entry
*
728 alloc_large_page (size_t size
, struct alloc_zone
*zone
)
730 struct page_entry
*entry
;
733 page
= (char *) xmalloc (size
+ CHUNK_OVERHEAD
+ sizeof (struct page_entry
));
734 entry
= (struct page_entry
*) (page
+ size
+ CHUNK_OVERHEAD
);
738 entry
->bytes_free
= LARGE_OBJECT_SIZE
+ CHUNK_OVERHEAD
;
740 entry
->context_depth
= zone
->context_depth
;
741 entry
->large_p
= true;
743 zone
->context_depth_allocations
|= (unsigned long)1 << zone
->context_depth
;
745 #ifdef USING_MALLOC_PAGE_GROUPS
748 set_page_table_entry (page
, entry
);
750 if (GGC_DEBUG_LEVEL
>= 2)
751 fprintf (G
.debug_file
,
752 "Allocating %s large page at %p, data %p-%p\n", entry
->zone
->name
,
753 (PTR
) entry
, page
, page
+ size
- 1);
759 /* For a page that is no longer needed, put it on the free page list. */
762 free_page (page_entry
*entry
)
764 if (GGC_DEBUG_LEVEL
>= 2)
765 fprintf (G
.debug_file
,
766 "Deallocating %s page at %p, data %p-%p\n", entry
->zone
->name
, (PTR
) entry
,
767 entry
->page
, entry
->page
+ entry
->bytes
- 1);
769 set_page_table_entry (entry
->page
, NULL
);
774 VALGRIND_FREELIKE_BLOCK (entry
->page
, entry
->bytes
);
778 /* Mark the page as inaccessible. Discard the handle to
779 avoid handle leak. */
780 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (entry
->page
, entry
->bytes
));
782 #ifdef USING_MALLOC_PAGE_GROUPS
783 clear_page_group_in_use (entry
->group
, entry
->page
);
786 entry
->next
= entry
->zone
->free_pages
;
787 entry
->zone
->free_pages
= entry
;
791 /* Release the free page cache to the system. */
794 release_pages (struct alloc_zone
*zone
)
797 page_entry
*p
, *next
;
801 /* Gather up adjacent pages so they are unmapped together. */
802 p
= zone
->free_pages
;
812 while (p
&& p
->page
== start
+ len
)
821 zone
->bytes_mapped
-= len
;
824 zone
->free_pages
= NULL
;
826 #ifdef USING_MALLOC_PAGE_GROUPS
830 /* Remove all pages from free page groups from the list. */
831 pp
= &(zone
->free_pages
);
832 while ((p
= *pp
) != NULL
)
833 if (p
->group
->in_use
== 0)
841 /* Remove all free page groups, and release the storage. */
842 gp
= &(zone
->page_groups
);
843 while ((g
= *gp
) != NULL
)
847 zone
->bytes_mapped
-= g
->alloc_size
;
848 free (g
->allocation
);
849 VALGRIND_FREELIKE_BLOCK(g
->allocation
, 0);
856 /* Place CHUNK of size SIZE on the free list for ZONE. */
859 free_chunk (struct alloc_chunk
*chunk
, size_t size
, struct alloc_zone
*zone
)
863 bin
= SIZE_BIN_DOWN (size
);
866 if (bin
> NUM_FREE_BINS
)
868 #ifdef COOKIE_CHECKING
869 if (chunk
->magic
!= CHUNK_MAGIC
&& chunk
->magic
!= DEADCHUNK_MAGIC
)
871 chunk
->magic
= DEADCHUNK_MAGIC
;
873 chunk
->u
.next_free
= zone
->free_chunks
[bin
];
874 zone
->free_chunks
[bin
] = chunk
;
875 if (GGC_DEBUG_LEVEL
>= 3)
876 fprintf (G
.debug_file
, "Deallocating object, chunk=%p\n", (void *)chunk
);
877 VALGRIND_DISCARD (VALGRIND_MAKE_READABLE (chunk
, sizeof (struct alloc_chunk
)));
880 /* Allocate a chunk of memory of SIZE bytes. */
883 ggc_alloc_zone_1 (size_t size
, struct alloc_zone
*zone
, short type
)
887 struct page_entry
*entry
;
888 struct alloc_chunk
*chunk
, *lchunk
, **pp
;
891 /* Align size, so that we're assured of aligned allocations. */
892 if (size
< FREE_BIN_DELTA
)
893 size
= FREE_BIN_DELTA
;
894 size
= (size
+ MAX_ALIGNMENT
- 1) & -MAX_ALIGNMENT
;
896 /* Large objects are handled specially. */
897 if (size
>= G
.pagesize
- 2*CHUNK_OVERHEAD
- FREE_BIN_DELTA
)
899 entry
= alloc_large_page (size
, zone
);
901 entry
->next
= entry
->zone
->pages
;
902 entry
->zone
->pages
= entry
;
905 chunk
= (struct alloc_chunk
*) entry
->page
;
906 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (chunk
, sizeof (struct alloc_chunk
)));
907 chunk
->size
= LARGE_OBJECT_SIZE
;
912 /* First look for a tiny object already segregated into its own
914 bin
= SIZE_BIN_UP (size
);
915 if (bin
<= NUM_FREE_BINS
)
917 chunk
= zone
->free_chunks
[bin
];
920 zone
->free_chunks
[bin
] = chunk
->u
.next_free
;
921 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (chunk
, sizeof (struct alloc_chunk
)));
926 /* Failing that, look through the "other" bucket for a chunk
927 that is large enough. */
928 pp
= &(zone
->free_chunks
[0]);
930 while (chunk
&& chunk
->size
< size
)
932 pp
= &chunk
->u
.next_free
;
936 /* Failing that, allocate new storage. */
939 entry
= alloc_small_page (zone
);
940 entry
->next
= entry
->zone
->pages
;
941 entry
->zone
->pages
= entry
;
943 chunk
= (struct alloc_chunk
*) entry
->page
;
944 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (chunk
, sizeof (struct alloc_chunk
)));
945 chunk
->size
= G
.pagesize
- CHUNK_OVERHEAD
;
949 *pp
= chunk
->u
.next_free
;
950 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (chunk
, sizeof (struct alloc_chunk
)));
952 /* Release extra memory from a chunk that's too big. */
953 lsize
= chunk
->size
- size
;
954 if (lsize
>= CHUNK_OVERHEAD
+ FREE_BIN_DELTA
)
956 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (chunk
, sizeof (struct alloc_chunk
)));
959 lsize
-= CHUNK_OVERHEAD
;
960 lchunk
= (struct alloc_chunk
*)(chunk
->u
.data
+ size
);
961 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (lchunk
, sizeof (struct alloc_chunk
)));
962 #ifdef COOKIE_CHECKING
963 lchunk
->magic
= CHUNK_MAGIC
;
967 lchunk
->size
= lsize
;
968 free_chunk (lchunk
, lsize
, zone
);
970 /* Calculate the object's address. */
972 #ifdef COOKIE_CHECKING
973 chunk
->magic
= CHUNK_MAGIC
;
977 chunk
->typecode
= type
;
978 result
= chunk
->u
.data
;
980 #ifdef ENABLE_GC_CHECKING
981 /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
982 exact same semantics in presence of memory bugs, regardless of
983 ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
984 handle to avoid handle leak. */
985 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result
, size
));
987 /* `Poison' the entire allocated object. */
988 memset (result
, 0xaf, size
);
991 /* Tell Valgrind that the memory is there, but its content isn't
992 defined. The bytes at the end of the object are still marked
994 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result
, size
));
996 /* Keep track of how many bytes are being allocated. This
997 information is used in deciding when to collect. */
998 zone
->allocated
+= size
+ CHUNK_OVERHEAD
;
1000 if (GGC_DEBUG_LEVEL
>= 3)
1001 fprintf (G
.debug_file
, "Allocating object, chunk=%p size=%lu at %p\n",
1002 (void *)chunk
, (unsigned long) size
, result
);
1007 /* Allocate a SIZE of chunk memory of GTE type, into an appropriate zone
1011 ggc_alloc_typed (enum gt_types_enum gte
, size_t size
)
1015 case gt_ggc_e_14lang_tree_node
:
1016 return ggc_alloc_zone_1 (size
, tree_zone
, gte
);
1018 case gt_ggc_e_7rtx_def
:
1019 return ggc_alloc_zone_1 (size
, rtl_zone
, gte
);
1021 case gt_ggc_e_9rtvec_def
:
1022 return ggc_alloc_zone_1 (size
, rtl_zone
, gte
);
1025 return ggc_alloc_zone_1 (size
, &main_zone
, gte
);
1029 /* Normal ggc_alloc simply allocates into the main zone. */
1032 ggc_alloc (size_t size
)
1034 return ggc_alloc_zone_1 (size
, &main_zone
, -1);
1037 /* Zone allocation allocates into the specified zone. */
1040 ggc_alloc_zone (size_t size
, struct alloc_zone
*zone
)
1042 return ggc_alloc_zone_1 (size
, zone
, -1);
1045 /* If P is not marked, mark it and return false. Otherwise return true.
1046 P must have been allocated by the GC allocator; it mustn't point to
1047 static objects, stack variables, or memory allocated with malloc. */
1050 ggc_set_mark (const void *p
)
1053 struct alloc_chunk
*chunk
;
1055 #ifdef ENABLE_CHECKING
1056 /* Look up the page on which the object is alloced. If the object
1057 wasn't allocated by the collector, we'll probably die. */
1058 entry
= lookup_page_table_entry (p
);
1062 chunk
= (struct alloc_chunk
*) ((char *)p
- CHUNK_OVERHEAD
);
1063 #ifdef COOKIE_CHECKING
1064 if (chunk
->magic
!= CHUNK_MAGIC
)
1071 #ifndef ENABLE_CHECKING
1072 entry
= lookup_page_table_entry (p
);
1075 /* Large pages are either completely full or completely empty. So if
1076 they are marked, they are completely full. */
1078 entry
->bytes_free
= 0;
1080 entry
->bytes_free
-= chunk
->size
+ CHUNK_OVERHEAD
;
1082 if (GGC_DEBUG_LEVEL
>= 4)
1083 fprintf (G
.debug_file
, "Marking %p\n", p
);
1088 /* Return 1 if P has been marked, zero otherwise.
1089 P must have been allocated by the GC allocator; it mustn't point to
1090 static objects, stack variables, or memory allocated with malloc. */
1093 ggc_marked_p (const void *p
)
1095 struct alloc_chunk
*chunk
;
1097 #ifdef ENABLE_CHECKING
1099 page_entry
*entry
= lookup_page_table_entry (p
);
1105 chunk
= (struct alloc_chunk
*) ((char *)p
- CHUNK_OVERHEAD
);
1106 #ifdef COOKIE_CHECKING
1107 if (chunk
->magic
!= CHUNK_MAGIC
)
1113 /* Return the size of the gc-able object P. */
1116 ggc_get_size (const void *p
)
1118 struct alloc_chunk
*chunk
;
1119 struct page_entry
*entry
;
1121 #ifdef ENABLE_CHECKING
1122 entry
= lookup_page_table_entry (p
);
1127 chunk
= (struct alloc_chunk
*) ((char *)p
- CHUNK_OVERHEAD
);
1128 #ifdef COOKIE_CHECKING
1129 if (chunk
->magic
!= CHUNK_MAGIC
)
1132 if (chunk
->size
== LARGE_OBJECT_SIZE
)
1134 #ifndef ENABLE_CHECKING
1135 entry
= lookup_page_table_entry (p
);
1137 return entry
->bytes
;
1143 /* Initialize the ggc-zone-mmap allocator. */
1147 /* Set up the main zone by hand. */
1148 main_zone
.name
= "Main zone";
1149 G
.zones
= &main_zone
;
1151 /* Allocate the default zones. */
1152 rtl_zone
= new_ggc_zone ("RTL zone");
1153 tree_zone
= new_ggc_zone ("Tree zone");
1154 garbage_zone
= new_ggc_zone ("Garbage zone");
1156 G
.pagesize
= getpagesize();
1157 G
.lg_pagesize
= exact_log2 (G
.pagesize
);
1158 #ifdef HAVE_MMAP_DEV_ZERO
1159 G
.dev_zero_fd
= open ("/dev/zero", O_RDONLY
);
1160 if (G
.dev_zero_fd
== -1)
1165 G
.debug_file
= fopen ("ggc-mmap.debug", "w");
1166 setlinebuf (G
.debug_file
);
1168 G
.debug_file
= stdout
;
1172 /* StunOS has an amazing off-by-one error for the first mmap allocation
1173 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1174 believe, is an unaligned page allocation, which would cause us to
1175 hork badly if we tried to use it. */
1177 char *p
= alloc_anon (NULL
, G
.pagesize
, &main_zone
);
1178 struct page_entry
*e
;
1179 if ((size_t)p
& (G
.pagesize
- 1))
1181 /* How losing. Discard this one and try another. If we still
1182 can't get something useful, give up. */
1184 p
= alloc_anon (NULL
, G
.pagesize
, &main_zone
);
1185 if ((size_t)p
& (G
.pagesize
- 1))
1189 /* We have a good page, might as well hold onto it... */
1190 e
= (struct page_entry
*) xmalloc (sizeof (struct page_entry
));
1191 e
->bytes
= G
.pagesize
;
1193 e
->next
= main_zone
.free_pages
;
1194 main_zone
.free_pages
= e
;
1199 /* Start a new GGC zone. */
1202 new_ggc_zone (const char * name
)
1204 struct alloc_zone
*new_zone
= xcalloc (1, sizeof (struct alloc_zone
));
1205 new_zone
->name
= name
;
1206 new_zone
->next_zone
= G
.zones
->next_zone
;
1207 G
.zones
->next_zone
= new_zone
;
1211 /* Destroy a GGC zone. */
1213 destroy_ggc_zone (struct alloc_zone
* dead_zone
)
1215 struct alloc_zone
*z
;
1217 for (z
= G
.zones
; z
&& z
->next_zone
!= dead_zone
; z
= z
->next_zone
)
1218 /* Just find that zone. */ ;
1220 #ifdef ENABLE_CHECKING
1221 /* We should have found the zone in the list. Anything else is fatal. */
1226 /* z is dead, baby. z is dead. */
1230 /* Increment the `GC context'. Objects allocated in an outer context
1231 are never freed, eliminating the need to register their roots. */
1234 ggc_push_context (void)
1236 struct alloc_zone
*zone
;
1237 for (zone
= G
.zones
; zone
; zone
= zone
->next_zone
)
1238 ++(zone
->context_depth
);
1240 if (main_zone
.context_depth
>= HOST_BITS_PER_LONG
)
1244 /* Decrement the `GC context'. All objects allocated since the
1245 previous ggc_push_context are migrated to the outer context. */
1248 ggc_pop_context_1 (struct alloc_zone
*zone
)
1250 unsigned long omask
;
1254 depth
= --(zone
->context_depth
);
1255 omask
= (unsigned long)1 << (depth
+ 1);
1257 if (!((zone
->context_depth_allocations
| zone
->context_depth_collections
) & omask
))
1260 zone
->context_depth_allocations
|= (zone
->context_depth_allocations
& omask
) >> 1;
1261 zone
->context_depth_allocations
&= omask
- 1;
1262 zone
->context_depth_collections
&= omask
- 1;
1264 /* Any remaining pages in the popped context are lowered to the new
1265 current context; i.e. objects allocated in the popped context and
1266 left over are imported into the previous context. */
1267 for (p
= zone
->pages
; p
!= NULL
; p
= p
->next
)
1268 if (p
->context_depth
> depth
)
1269 p
->context_depth
= depth
;
1272 /* Pop all the zone contexts. */
1275 ggc_pop_context (void)
1277 struct alloc_zone
*zone
;
1278 for (zone
= G
.zones
; zone
; zone
= zone
->next_zone
)
1279 ggc_pop_context_1 (zone
);
1283 /* Poison the chunk. */
1284 #ifdef ENABLE_GC_CHECKING
1285 #define poison_chunk(CHUNK, SIZE) \
1286 memset ((CHUNK)->u.data, 0xa5, (SIZE))
1288 #define poison_chunk(CHUNK, SIZE)
1291 /* Free all empty pages and objects within a page for a given zone */
1294 sweep_pages (struct alloc_zone
*zone
)
1296 page_entry
**pp
, *p
, *next
;
1297 struct alloc_chunk
*chunk
, *last_free
, *end
;
1298 size_t last_free_size
, allocated
= 0;
1300 /* First, reset the free_chunks lists, since we are going to
1301 re-free free chunks in hopes of coalescing them into large chunks. */
1302 memset (zone
->free_chunks
, 0, sizeof (zone
->free_chunks
));
1304 for (p
= zone
->pages
; p
; p
= next
)
1308 /* For empty pages, just free the page. */
1309 if (p
->bytes_free
== G
.pagesize
&& p
->context_depth
== zone
->context_depth
)
1312 #ifdef ENABLE_GC_CHECKING
1313 /* Poison the page. */
1314 memset (p
->page
, 0xb5, p
->bytes
);
1320 /* Large pages are all or none affairs. Either they are
1321 completely empty, or they are completely full.
1322 Thus, if the above didn't catch it, we need not do anything
1323 except remove the mark and reset the bytes_free.
1325 XXX: Should we bother to increment allocated. */
1326 else if (p
->large_p
)
1328 p
->bytes_free
= p
->bytes
;
1329 ((struct alloc_chunk
*)p
->page
)->mark
= 0;
1334 /* This page has now survived another collection. */
1337 /* Which leaves full and partial pages. Step through all chunks,
1338 consolidate those that are free and insert them into the free
1339 lists. Note that consolidation slows down collection
1342 chunk
= (struct alloc_chunk
*)p
->page
;
1343 end
= (struct alloc_chunk
*)(p
->page
+ G
.pagesize
);
1349 prefetch ((struct alloc_chunk
*)(chunk
->u
.data
+ chunk
->size
));
1350 if (chunk
->mark
|| p
->context_depth
< zone
->context_depth
)
1354 last_free
->type
= 0;
1355 last_free
->size
= last_free_size
;
1356 last_free
->mark
= 0;
1357 poison_chunk (last_free
, last_free_size
);
1358 free_chunk (last_free
, last_free_size
, zone
);
1363 allocated
+= chunk
->size
+ CHUNK_OVERHEAD
;
1364 p
->bytes_free
+= chunk
->size
+ CHUNK_OVERHEAD
;
1367 #ifdef ENABLE_CHECKING
1368 if (p
->bytes_free
> p
->bytes
)
1376 last_free_size
+= CHUNK_OVERHEAD
+ chunk
->size
;
1381 last_free_size
= chunk
->size
;
1385 chunk
= (struct alloc_chunk
*)(chunk
->u
.data
+ chunk
->size
);
1387 while (chunk
< end
);
1391 last_free
->type
= 0;
1392 last_free
->size
= last_free_size
;
1393 last_free
->mark
= 0;
1394 poison_chunk (last_free
, last_free_size
);
1395 free_chunk (last_free
, last_free_size
, zone
);
1399 zone
->allocated
= allocated
;
1402 /* mark-and-sweep routine for collecting a single zone. NEED_MARKING
1403 is true if we need to mark before sweeping, false if some other
1404 zone collection has already performed marking for us. Returns true
1405 if we collected, false otherwise. */
1408 ggc_collect_1 (struct alloc_zone
*zone
, bool need_marking
)
1412 /* Avoid frequent unnecessary work by skipping collection if the
1413 total allocations haven't expanded much since the last
1415 float allocated_last_gc
=
1416 MAX (zone
->allocated_last_gc
,
1417 (size_t) PARAM_VALUE (GGC_MIN_HEAPSIZE
) * 1024);
1419 float min_expand
= allocated_last_gc
* PARAM_VALUE (GGC_MIN_EXPAND
) / 100;
1421 if (zone
->allocated
< allocated_last_gc
+ min_expand
)
1426 fprintf (stderr
, " {%s GC %luk -> ",
1427 zone
->name
, (unsigned long) zone
->allocated
/ 1024);
1429 /* Zero the total allocated bytes. This will be recalculated in the
1431 zone
->allocated
= 0;
1433 /* Release the pages we freed the last time we collected, but didn't
1434 reuse in the interim. */
1435 release_pages (zone
);
1437 /* Indicate that we've seen collections at this context depth. */
1438 zone
->context_depth_collections
1439 = ((unsigned long)1 << (zone
->context_depth
+ 1)) - 1;
1443 zone
->was_collected
= true;
1444 zone
->allocated_last_gc
= zone
->allocated
;
1447 fprintf (stderr
, "%luk}", (unsigned long) zone
->allocated
/ 1024);
1451 /* Calculate the average page survival rate in terms of number of
1455 calculate_average_page_survival (struct alloc_zone
*zone
)
1458 float survival
= 0.0;
1460 for (p
= zone
->pages
; p
; p
= p
->next
)
1463 survival
+= p
->survived
;
1465 return survival
/count
;
1468 /* Check the magic cookies all of the chunks contain, to make sure we
1469 aren't doing anything stupid, like stomping on alloc_chunk
1473 check_cookies (void)
1475 #ifdef COOKIE_CHECKING
1477 struct alloc_zone
*zone
;
1479 for (zone
= G
.zones
; zone
; zone
= zone
->next_zone
)
1481 for (p
= zone
->pages
; p
; p
= p
->next
)
1485 struct alloc_chunk
*chunk
= (struct alloc_chunk
*)p
->page
;
1486 struct alloc_chunk
*end
= (struct alloc_chunk
*)(p
->page
+ G
.pagesize
);
1489 if (chunk
->magic
!= CHUNK_MAGIC
&& chunk
->magic
!= DEADCHUNK_MAGIC
)
1491 chunk
= (struct alloc_chunk
*)(chunk
->u
.data
+ chunk
->size
);
1493 while (chunk
< end
);
1501 /* Top level collection routine. */
1506 struct alloc_zone
*zone
;
1507 bool marked
= false;
1510 timevar_push (TV_GC
);
1512 /* Start by possibly collecting the main zone. */
1513 main_zone
.was_collected
= false;
1514 marked
|= ggc_collect_1 (&main_zone
, true);
1516 /* In order to keep the number of collections down, we don't
1517 collect other zones unless we are collecting the main zone. This
1518 gives us roughly the same number of collections as we used to
1519 have with the old gc. The number of collection is important
1520 because our main slowdown (according to profiling) is now in
1521 marking. So if we mark twice as often as we used to, we'll be
1522 twice as slow. Hopefully we'll avoid this cost when we mark
1525 if (main_zone
.was_collected
)
1527 struct alloc_zone
*zone
;
1529 for (zone
= main_zone
.next_zone
; zone
; zone
= zone
->next_zone
)
1532 zone
->was_collected
= false;
1533 marked
|= ggc_collect_1 (zone
, !marked
);
1537 /* Print page survival stats, if someone wants them. */
1538 if (GGC_DEBUG_LEVEL
>= 2)
1540 for (zone
= G
.zones
; zone
; zone
= zone
->next_zone
)
1542 if (zone
->was_collected
)
1544 f
= calculate_average_page_survival (zone
);
1545 printf ("Average page survival in zone `%s' is %f\n",
1551 /* Since we don't mark zone at a time right now, marking in any
1552 zone means marking in every zone. So we have to clear all the
1553 marks in all the zones that weren't collected already. */
1557 for (zone
= G
.zones
; zone
; zone
= zone
->next_zone
)
1559 if (zone
->was_collected
)
1561 for (p
= zone
->pages
; p
; p
= p
->next
)
1565 struct alloc_chunk
*chunk
= (struct alloc_chunk
*)p
->page
;
1566 struct alloc_chunk
*end
= (struct alloc_chunk
*)(p
->page
+ G
.pagesize
);
1569 prefetch ((struct alloc_chunk
*)(chunk
->u
.data
+ chunk
->size
));
1570 if (chunk
->mark
|| p
->context_depth
< zone
->context_depth
)
1573 p
->bytes_free
+= chunk
->size
+ CHUNK_OVERHEAD
;
1574 #ifdef ENABLE_CHECKING
1575 if (p
->bytes_free
> p
->bytes
)
1580 chunk
= (struct alloc_chunk
*)(chunk
->u
.data
+ chunk
->size
);
1582 while (chunk
< end
);
1586 p
->bytes_free
= p
->bytes
;
1587 ((struct alloc_chunk
*)p
->page
)->mark
= 0;
1593 /* Free dead zones. */
1594 for (zone
= G
.zones
; zone
&& zone
->next_zone
; zone
= zone
->next_zone
)
1596 if (zone
->next_zone
->dead
)
1598 struct alloc_zone
*dead_zone
= zone
->next_zone
;
1600 printf ("Zone `%s' is dead and will be freed.\n", dead_zone
->name
);
1602 /* The zone must be empty. */
1603 if (dead_zone
->allocated
!= 0)
1606 /* Unchain the dead zone, release all its pages and free it. */
1607 zone
->next_zone
= zone
->next_zone
->next_zone
;
1608 release_pages (dead_zone
);
1613 timevar_pop (TV_GC
);
1616 /* Print allocation statistics. */
1619 ggc_print_statistics (void)
1625 struct ggc_pch_ondisk
1633 /* Initialize the PCH datastructure. */
1635 struct ggc_pch_data
*
1638 return xcalloc (sizeof (struct ggc_pch_data
), 1);
1641 /* Add the size of object X to the size of the PCH data. */
1644 ggc_pch_count_object (struct ggc_pch_data
*d
, void *x ATTRIBUTE_UNUSED
,
1645 size_t size
, bool is_string
)
1649 d
->d
.total
+= size
+ CHUNK_OVERHEAD
;
1655 /* Return the total size of the PCH data. */
1658 ggc_pch_total_size (struct ggc_pch_data
*d
)
1663 /* Set the base address for the objects in the PCH file. */
1666 ggc_pch_this_base (struct ggc_pch_data
*d
, void *base
)
1668 d
->base
= (size_t) base
;
1671 /* Allocate a place for object X of size SIZE in the PCH file. */
1674 ggc_pch_alloc_object (struct ggc_pch_data
*d
, void *x
,
1675 size_t size
, bool is_string
)
1678 result
= (char *)d
->base
;
1681 struct alloc_chunk
*chunk
= (struct alloc_chunk
*) ((char *)x
- CHUNK_OVERHEAD
);
1682 if (chunk
->size
== LARGE_OBJECT_SIZE
)
1683 d
->base
+= ggc_get_size (x
) + CHUNK_OVERHEAD
;
1685 d
->base
+= chunk
->size
+ CHUNK_OVERHEAD
;
1686 return result
+ CHUNK_OVERHEAD
;
1696 /* Prepare to write out the PCH data to file F. */
1699 ggc_pch_prepare_write (struct ggc_pch_data
*d ATTRIBUTE_UNUSED
,
1700 FILE *f ATTRIBUTE_UNUSED
)
1702 /* Nothing to do. */
1705 /* Write out object X of SIZE to file F. */
1708 ggc_pch_write_object (struct ggc_pch_data
*d ATTRIBUTE_UNUSED
,
1709 FILE *f
, void *x
, void *newx ATTRIBUTE_UNUSED
,
1710 size_t size
, bool is_string
)
1714 struct alloc_chunk
*chunk
= (struct alloc_chunk
*) ((char *)x
- CHUNK_OVERHEAD
);
1715 size
= ggc_get_size (x
);
1716 if (fwrite (chunk
, size
+ CHUNK_OVERHEAD
, 1, f
) != 1)
1717 fatal_error ("can't write PCH file: %m");
1718 d
->written
+= size
+ CHUNK_OVERHEAD
;
1722 if (fwrite (x
, size
, 1, f
) != 1)
1723 fatal_error ("can't write PCH file: %m");
1726 if (d
->written
== d
->d
.total
1727 && fseek (f
, ROUND_UP_VALUE (d
->d
.total
, G
.pagesize
), SEEK_CUR
) != 0)
1728 fatal_error ("can't write PCH file: %m");
1732 ggc_pch_finish (struct ggc_pch_data
*d
, FILE *f
)
1734 if (fwrite (&d
->d
, sizeof (d
->d
), 1, f
) != 1)
1735 fatal_error ("can't write PCH file: %m");
1741 ggc_pch_read (FILE *f
, void *addr
)
1743 struct ggc_pch_ondisk d
;
1744 struct page_entry
*entry
;
1746 if (fread (&d
, sizeof (d
), 1, f
) != 1)
1747 fatal_error ("can't read PCH file: %m");
1748 entry
= xcalloc (1, sizeof (struct page_entry
));
1749 entry
->bytes
= d
.total
;
1751 entry
->context_depth
= 0;
1752 entry
->zone
= &main_zone
;
1753 for (pte
= entry
->page
;
1754 pte
< entry
->page
+ entry
->bytes
;
1756 set_page_table_entry (pte
, entry
);