1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "coretypes.h"
34 #ifdef ENABLE_VALGRIND_CHECKING
37 /* Avoid #ifdef:s when we can help it. */
38 #define VALGRIND_DISCARD(x)
41 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
42 file open. Prefer either to valloc. */
44 # undef HAVE_MMAP_DEV_ZERO
46 # include <sys/mman.h>
48 # define MAP_FAILED -1
50 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
51 # define MAP_ANONYMOUS MAP_ANON
57 #ifdef HAVE_MMAP_DEV_ZERO
59 # include <sys/mman.h>
61 # define MAP_FAILED -1
68 #define USING_MALLOC_PAGE_GROUPS
73 This garbage-collecting allocator allocates objects on one of a set
74 of pages. Each page can allocate objects of a single size only;
75 available sizes are powers of two starting at four bytes. The size
76 of an allocation request is rounded up to the next power of two
77 (`order'), and satisfied from the appropriate page.
79 Each page is recorded in a page-entry, which also maintains an
80 in-use bitmap of object positions on the page. This allows the
81 allocation state of a particular object to be flipped without
82 touching the page itself.
84 Each page-entry also has a context depth, which is used to track
85 pushing and popping of allocation contexts. Only objects allocated
86 in the current (highest-numbered) context may be collected.
88 Page entries are arranged in an array of singly-linked lists. The
89 array is indexed by the allocation size, in bits, of the pages on
90 it; i.e. all pages on a list allocate objects of the same size.
91 Pages are ordered on the list such that all non-full pages precede
92 all full pages, with non-full pages arranged in order of decreasing
95 Empty pages (of all orders) are kept on a single page cache list,
96 and are considered first when new pages are required; they are
97 deallocated at the start of the next collection if they haven't
98 been recycled by then. */
100 /* Define GGC_DEBUG_LEVEL to print debugging information.
101 0: No debugging output.
102 1: GC statistics only.
103 2: Page-entry allocations/deallocations as well.
104 3: Object allocations as well.
105 4: Object marks as well. */
106 #define GGC_DEBUG_LEVEL (0)
108 #ifndef HOST_BITS_PER_PTR
109 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
113 /* A two-level tree is used to look up the page-entry for a given
114 pointer. Two chunks of the pointer's bits are extracted to index
115 the first and second levels of the tree, as follows:
119 msb +----------------+----+------+------+ lsb
125 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
126 pages are aligned on system page boundaries. The next most
127 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
128 index values in the lookup table, respectively.
130 For 32-bit architectures and the settings below, there are no
131 leftover bits. For architectures with wider pointers, the lookup
132 tree points to a list of pages, which must be scanned to find the
135 #define PAGE_L1_BITS (8)
136 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
137 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
138 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
140 #define LOOKUP_L1(p) \
141 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
143 #define LOOKUP_L2(p) \
144 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
146 /* The number of objects per allocation page, for objects on a page of
147 the indicated ORDER. */
148 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
150 /* The number of objects in P. */
151 #define OBJECTS_IN_PAGE(P) ((P)->bytes / OBJECT_SIZE ((P)->order))
153 /* The size of an object on a page of the indicated ORDER. */
154 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
156 /* For speed, we avoid doing a general integer divide to locate the
157 offset in the allocation bitmap, by precalculating numbers M, S
158 such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
159 within the page which is evenly divisible by the object size Z. */
160 #define DIV_MULT(ORDER) inverse_table[ORDER].mult
161 #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
162 #define OFFSET_TO_BIT(OFFSET, ORDER) \
163 (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
165 /* The number of extra orders, not corresponding to power-of-two sized
168 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
170 #define RTL_SIZE(NSLOTS) \
171 (sizeof (struct rtx_def) + ((NSLOTS) - 1) * sizeof (rtunion))
173 /* The Ith entry is the maximum size of an object to be stored in the
174 Ith extra order. Adding a new entry to this array is the *only*
175 thing you need to do to add a new special allocation size. */
177 static const size_t extra_order_size_table
[] = {
178 sizeof (struct tree_decl
),
179 sizeof (struct tree_list
),
180 RTL_SIZE (2), /* REG, MEM, PLUS, etc. */
181 RTL_SIZE (10), /* INSN, CALL_INSN, JUMP_INSN */
184 /* The total number of orders. */
186 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
188 /* We use this structure to determine the alignment required for
189 allocations. For power-of-two sized allocations, that's not a
190 problem, but it does matter for odd-sized allocations. */
192 struct max_alignment
{
196 #ifdef HAVE_LONG_DOUBLE
204 /* The biggest alignment required. */
206 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
208 /* Compute the smallest nonnegative number which when added to X gives
211 #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
213 /* Compute the smallest multiple of F that is >= X. */
215 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
217 /* The Ith entry is the number of objects on a page or order I. */
219 static unsigned objects_per_page_table
[NUM_ORDERS
];
221 /* The Ith entry is the size of an object on a page of order I. */
223 static size_t object_size_table
[NUM_ORDERS
];
225 /* The Ith entry is a pair of numbers (mult, shift) such that
226 ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
227 for all k evenly divisible by OBJECT_SIZE(I). */
234 inverse_table
[NUM_ORDERS
];
236 /* A page_entry records the status of an allocation page. This
237 structure is dynamically sized to fit the bitmap in_use_p. */
238 typedef struct page_entry
240 /* The next page-entry with objects of the same size, or NULL if
241 this is the last page-entry. */
242 struct page_entry
*next
;
244 /* The number of bytes allocated. (This will always be a multiple
245 of the host system page size.) */
248 /* The address at which the memory is allocated. */
251 #ifdef USING_MALLOC_PAGE_GROUPS
252 /* Back pointer to the page group this page came from. */
253 struct page_group
*group
;
256 /* Saved in-use bit vector for pages that aren't in the topmost
257 context during collection. */
258 unsigned long *save_in_use_p
;
260 /* Context depth of this page. */
261 unsigned short context_depth
;
263 /* The number of free objects remaining on this page. */
264 unsigned short num_free_objects
;
266 /* A likely candidate for the bit position of a free object for the
267 next allocation from this page. */
268 unsigned short next_bit_hint
;
270 /* The lg of size of objects allocated from this page. */
273 /* A bit vector indicating whether or not objects are in use. The
274 Nth bit is one if the Nth object on this page is allocated. This
275 array is dynamically sized. */
276 unsigned long in_use_p
[1];
279 #ifdef USING_MALLOC_PAGE_GROUPS
280 /* A page_group describes a large allocation from malloc, from which
281 we parcel out aligned pages. */
282 typedef struct page_group
284 /* A linked list of all extant page groups. */
285 struct page_group
*next
;
287 /* The address we received from malloc. */
290 /* The size of the block. */
293 /* A bitmask of pages in use. */
298 #if HOST_BITS_PER_PTR <= 32
300 /* On 32-bit hosts, we use a two level page table, as pictured above. */
301 typedef page_entry
**page_table
[PAGE_L1_SIZE
];
305 /* On 64-bit hosts, we use the same two level page tables plus a linked
306 list that disambiguates the top 32-bits. There will almost always be
307 exactly one entry in the list. */
308 typedef struct page_table_chain
310 struct page_table_chain
*next
;
312 page_entry
**table
[PAGE_L1_SIZE
];
317 /* The rest of the global variables. */
318 static struct globals
320 /* The Nth element in this array is a page with objects of size 2^N.
321 If there are any pages with free objects, they will be at the
322 head of the list. NULL if there are no page-entries for this
324 page_entry
*pages
[NUM_ORDERS
];
326 /* The Nth element in this array is the last page with objects of
327 size 2^N. NULL if there are no page-entries for this object
329 page_entry
*page_tails
[NUM_ORDERS
];
331 /* Lookup table for associating allocation pages with object addresses. */
334 /* The system's page size. */
338 /* Bytes currently allocated. */
341 /* Bytes currently allocated at the end of the last collection. */
342 size_t allocated_last_gc
;
344 /* Total amount of memory mapped. */
347 /* Bit N set if any allocations have been done at context depth N. */
348 unsigned long context_depth_allocations
;
350 /* Bit N set if any collections have been done at context depth N. */
351 unsigned long context_depth_collections
;
353 /* The current depth in the context stack. */
354 unsigned short context_depth
;
356 /* A file descriptor open to /dev/zero for reading. */
357 #if defined (HAVE_MMAP_DEV_ZERO)
361 /* A cache of free system pages. */
362 page_entry
*free_pages
;
364 #ifdef USING_MALLOC_PAGE_GROUPS
365 page_group
*page_groups
;
368 /* The file descriptor for debugging output. */
372 /* The size in bytes required to maintain a bitmap for the objects
374 #define BITMAP_SIZE(Num_objects) \
375 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
377 /* Allocate pages in chunks of this size, to throttle calls to memory
378 allocation routines. The first page is used, the rest go onto the
379 free list. This cannot be larger than HOST_BITS_PER_INT for the
380 in_use bitmask for page_group. */
381 #define GGC_QUIRE_SIZE 16
383 static int ggc_allocated_p
PARAMS ((const void *));
384 static page_entry
*lookup_page_table_entry
PARAMS ((const void *));
385 static void set_page_table_entry
PARAMS ((void *, page_entry
*));
387 static char *alloc_anon
PARAMS ((char *, size_t));
389 #ifdef USING_MALLOC_PAGE_GROUPS
390 static size_t page_group_index
PARAMS ((char *, char *));
391 static void set_page_group_in_use
PARAMS ((page_group
*, char *));
392 static void clear_page_group_in_use
PARAMS ((page_group
*, char *));
394 static struct page_entry
* alloc_page
PARAMS ((unsigned));
395 static void free_page
PARAMS ((struct page_entry
*));
396 static void release_pages
PARAMS ((void));
397 static void clear_marks
PARAMS ((void));
398 static void sweep_pages
PARAMS ((void));
399 static void ggc_recalculate_in_use_p
PARAMS ((page_entry
*));
400 static void compute_inverse
PARAMS ((unsigned));
402 #ifdef ENABLE_GC_CHECKING
403 static void poison_pages
PARAMS ((void));
406 void debug_print_page_list
PARAMS ((int));
408 /* Returns nonzero if P was allocated in GC'able memory. */
417 #if HOST_BITS_PER_PTR <= 32
420 page_table table
= G
.lookup
;
421 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
426 if (table
->high_bits
== high_bits
)
430 base
= &table
->table
[0];
433 /* Extract the level 1 and 2 indices. */
437 return base
[L1
] && base
[L1
][L2
];
440 /* Traverse the page table and find the entry for a page.
441 Die (probably) if the object wasn't allocated via GC. */
443 static inline page_entry
*
444 lookup_page_table_entry(p
)
450 #if HOST_BITS_PER_PTR <= 32
453 page_table table
= G
.lookup
;
454 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
455 while (table
->high_bits
!= high_bits
)
457 base
= &table
->table
[0];
460 /* Extract the level 1 and 2 indices. */
467 /* Set the page table entry for a page. */
470 set_page_table_entry(p
, entry
)
477 #if HOST_BITS_PER_PTR <= 32
481 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
482 for (table
= G
.lookup
; table
; table
= table
->next
)
483 if (table
->high_bits
== high_bits
)
486 /* Not found -- allocate a new table. */
487 table
= (page_table
) xcalloc (1, sizeof(*table
));
488 table
->next
= G
.lookup
;
489 table
->high_bits
= high_bits
;
492 base
= &table
->table
[0];
495 /* Extract the level 1 and 2 indices. */
499 if (base
[L1
] == NULL
)
500 base
[L1
] = (page_entry
**) xcalloc (PAGE_L2_SIZE
, sizeof (page_entry
*));
502 base
[L1
][L2
] = entry
;
505 /* Prints the page-entry for object size ORDER, for debugging. */
508 debug_print_page_list (order
)
512 printf ("Head=%p, Tail=%p:\n", (PTR
) G
.pages
[order
],
513 (PTR
) G
.page_tails
[order
]);
517 printf ("%p(%1d|%3d) -> ", (PTR
) p
, p
->context_depth
,
518 p
->num_free_objects
);
526 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
527 (if non-null). The ifdef structure here is intended to cause a
528 compile error unless exactly one of the HAVE_* is defined. */
531 alloc_anon (pref
, size
)
532 char *pref ATTRIBUTE_UNUSED
;
535 #ifdef HAVE_MMAP_ANON
536 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
537 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
539 #ifdef HAVE_MMAP_DEV_ZERO
540 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
541 MAP_PRIVATE
, G
.dev_zero_fd
, 0);
544 if (page
== (char *) MAP_FAILED
)
546 perror ("virtual memory exhausted");
547 exit (FATAL_EXIT_CODE
);
550 /* Remember that we allocated this memory. */
551 G
.bytes_mapped
+= size
;
553 /* Pretend we don't have access to the allocated pages. We'll enable
554 access to smaller pieces of the area in ggc_alloc. Discard the
555 handle to avoid handle leak. */
556 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (page
, size
));
561 #ifdef USING_MALLOC_PAGE_GROUPS
562 /* Compute the index for this page into the page group. */
565 page_group_index (allocation
, page
)
566 char *allocation
, *page
;
568 return (size_t) (page
- allocation
) >> G
.lg_pagesize
;
571 /* Set and clear the in_use bit for this page in the page group. */
574 set_page_group_in_use (group
, page
)
578 group
->in_use
|= 1 << page_group_index (group
->allocation
, page
);
582 clear_page_group_in_use (group
, page
)
586 group
->in_use
&= ~(1 << page_group_index (group
->allocation
, page
));
590 /* Allocate a new page for allocating objects of size 2^ORDER,
591 and return an entry for it. The entry is not added to the
592 appropriate page_table list. */
594 static inline struct page_entry
*
598 struct page_entry
*entry
, *p
, **pp
;
602 size_t page_entry_size
;
604 #ifdef USING_MALLOC_PAGE_GROUPS
608 num_objects
= OBJECTS_PER_PAGE (order
);
609 bitmap_size
= BITMAP_SIZE (num_objects
+ 1);
610 page_entry_size
= sizeof (page_entry
) - sizeof (long) + bitmap_size
;
611 entry_size
= num_objects
* OBJECT_SIZE (order
);
612 if (entry_size
< G
.pagesize
)
613 entry_size
= G
.pagesize
;
618 /* Check the list of free pages for one we can use. */
619 for (pp
= &G
.free_pages
, p
= *pp
; p
; pp
= &p
->next
, p
= *pp
)
620 if (p
->bytes
== entry_size
)
625 /* Recycle the allocated memory from this page ... */
629 #ifdef USING_MALLOC_PAGE_GROUPS
633 /* ... and, if possible, the page entry itself. */
634 if (p
->order
== order
)
637 memset (entry
, 0, page_entry_size
);
643 else if (entry_size
== G
.pagesize
)
645 /* We want just one page. Allocate a bunch of them and put the
646 extras on the freelist. (Can only do this optimization with
647 mmap for backing store.) */
648 struct page_entry
*e
, *f
= G
.free_pages
;
651 page
= alloc_anon (NULL
, G
.pagesize
* GGC_QUIRE_SIZE
);
653 /* This loop counts down so that the chain will be in ascending
655 for (i
= GGC_QUIRE_SIZE
- 1; i
>= 1; i
--)
657 e
= (struct page_entry
*) xcalloc (1, page_entry_size
);
659 e
->bytes
= G
.pagesize
;
660 e
->page
= page
+ (i
<< G
.lg_pagesize
);
668 page
= alloc_anon (NULL
, entry_size
);
670 #ifdef USING_MALLOC_PAGE_GROUPS
673 /* Allocate a large block of memory and serve out the aligned
674 pages therein. This results in much less memory wastage
675 than the traditional implementation of valloc. */
677 char *allocation
, *a
, *enda
;
678 size_t alloc_size
, head_slop
, tail_slop
;
679 int multiple_pages
= (entry_size
== G
.pagesize
);
682 alloc_size
= GGC_QUIRE_SIZE
* G
.pagesize
;
684 alloc_size
= entry_size
+ G
.pagesize
- 1;
685 allocation
= xmalloc (alloc_size
);
687 page
= (char *) (((size_t) allocation
+ G
.pagesize
- 1) & -G
.pagesize
);
688 head_slop
= page
- allocation
;
690 tail_slop
= ((size_t) allocation
+ alloc_size
) & (G
.pagesize
- 1);
692 tail_slop
= alloc_size
- entry_size
- head_slop
;
693 enda
= allocation
+ alloc_size
- tail_slop
;
695 /* We allocated N pages, which are likely not aligned, leaving
696 us with N-1 usable pages. We plan to place the page_group
697 structure somewhere in the slop. */
698 if (head_slop
>= sizeof (page_group
))
699 group
= (page_group
*)page
- 1;
702 /* We magically got an aligned allocation. Too bad, we have
703 to waste a page anyway. */
707 tail_slop
+= G
.pagesize
;
709 if (tail_slop
< sizeof (page_group
))
711 group
= (page_group
*)enda
;
712 tail_slop
-= sizeof (page_group
);
715 /* Remember that we allocated this memory. */
716 group
->next
= G
.page_groups
;
717 group
->allocation
= allocation
;
718 group
->alloc_size
= alloc_size
;
720 G
.page_groups
= group
;
721 G
.bytes_mapped
+= alloc_size
;
723 /* If we allocated multiple pages, put the rest on the free list. */
726 struct page_entry
*e
, *f
= G
.free_pages
;
727 for (a
= enda
- G
.pagesize
; a
!= page
; a
-= G
.pagesize
)
729 e
= (struct page_entry
*) xcalloc (1, page_entry_size
);
731 e
->bytes
= G
.pagesize
;
743 entry
= (struct page_entry
*) xcalloc (1, page_entry_size
);
745 entry
->bytes
= entry_size
;
747 entry
->context_depth
= G
.context_depth
;
748 entry
->order
= order
;
749 entry
->num_free_objects
= num_objects
;
750 entry
->next_bit_hint
= 1;
752 G
.context_depth_allocations
|= (unsigned long)1 << G
.context_depth
;
754 #ifdef USING_MALLOC_PAGE_GROUPS
755 entry
->group
= group
;
756 set_page_group_in_use (group
, page
);
759 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
760 increment the hint. */
761 entry
->in_use_p
[num_objects
/ HOST_BITS_PER_LONG
]
762 = (unsigned long) 1 << (num_objects
% HOST_BITS_PER_LONG
);
764 set_page_table_entry (page
, entry
);
766 if (GGC_DEBUG_LEVEL
>= 2)
767 fprintf (G
.debug_file
,
768 "Allocating page at %p, object size=%lu, data %p-%p\n",
769 (PTR
) entry
, (unsigned long) OBJECT_SIZE (order
), page
,
770 page
+ entry_size
- 1);
775 /* For a page that is no longer needed, put it on the free page list. */
781 if (GGC_DEBUG_LEVEL
>= 2)
782 fprintf (G
.debug_file
,
783 "Deallocating page at %p, data %p-%p\n", (PTR
) entry
,
784 entry
->page
, entry
->page
+ entry
->bytes
- 1);
786 /* Mark the page as inaccessible. Discard the handle to avoid handle
788 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (entry
->page
, entry
->bytes
));
790 set_page_table_entry (entry
->page
, NULL
);
792 #ifdef USING_MALLOC_PAGE_GROUPS
793 clear_page_group_in_use (entry
->group
, entry
->page
);
796 entry
->next
= G
.free_pages
;
797 G
.free_pages
= entry
;
800 /* Release the free page cache to the system. */
806 page_entry
*p
, *next
;
810 /* Gather up adjacent pages so they are unmapped together. */
821 while (p
&& p
->page
== start
+ len
)
830 G
.bytes_mapped
-= len
;
835 #ifdef USING_MALLOC_PAGE_GROUPS
839 /* Remove all pages from free page groups from the list. */
841 while ((p
= *pp
) != NULL
)
842 if (p
->group
->in_use
== 0)
850 /* Remove all free page groups, and release the storage. */
852 while ((g
= *gp
) != NULL
)
856 G
.bytes_mapped
-= g
->alloc_size
;
857 free (g
->allocation
);
864 /* This table provides a fast way to determine ceil(log_2(size)) for
865 allocation requests. The minimum allocation size is eight bytes. */
867 static unsigned char size_lookup
[257] =
869 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
870 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
871 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
872 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
873 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
874 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
875 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
876 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
877 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
878 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
879 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
880 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
881 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
882 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
883 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
884 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
888 /* Allocate a chunk of memory of SIZE bytes. If ZERO is nonzero, the
889 memory is zeroed; otherwise, its contents are undefined. */
895 unsigned order
, word
, bit
, object_offset
;
896 struct page_entry
*entry
;
900 order
= size_lookup
[size
];
904 while (size
> OBJECT_SIZE (order
))
908 /* If there are non-full pages for this size allocation, they are at
909 the head of the list. */
910 entry
= G
.pages
[order
];
912 /* If there is no page for this object size, or all pages in this
913 context are full, allocate a new page. */
914 if (entry
== NULL
|| entry
->num_free_objects
== 0)
916 struct page_entry
*new_entry
;
917 new_entry
= alloc_page (order
);
919 /* If this is the only entry, it's also the tail. */
921 G
.page_tails
[order
] = new_entry
;
923 /* Put new pages at the head of the page list. */
924 new_entry
->next
= entry
;
926 G
.pages
[order
] = new_entry
;
928 /* For a new page, we know the word and bit positions (in the
929 in_use bitmap) of the first available object -- they're zero. */
930 new_entry
->next_bit_hint
= 1;
937 /* First try to use the hint left from the previous allocation
938 to locate a clear bit in the in-use bitmap. We've made sure
939 that the one-past-the-end bit is always set, so if the hint
940 has run over, this test will fail. */
941 unsigned hint
= entry
->next_bit_hint
;
942 word
= hint
/ HOST_BITS_PER_LONG
;
943 bit
= hint
% HOST_BITS_PER_LONG
;
945 /* If the hint didn't work, scan the bitmap from the beginning. */
946 if ((entry
->in_use_p
[word
] >> bit
) & 1)
949 while (~entry
->in_use_p
[word
] == 0)
951 while ((entry
->in_use_p
[word
] >> bit
) & 1)
953 hint
= word
* HOST_BITS_PER_LONG
+ bit
;
956 /* Next time, try the next bit. */
957 entry
->next_bit_hint
= hint
+ 1;
959 object_offset
= hint
* OBJECT_SIZE (order
);
962 /* Set the in-use bit. */
963 entry
->in_use_p
[word
] |= ((unsigned long) 1 << bit
);
965 /* Keep a running total of the number of free objects. If this page
966 fills up, we may have to move it to the end of the list if the
967 next page isn't full. If the next page is full, all subsequent
968 pages are full, so there's no need to move it. */
969 if (--entry
->num_free_objects
== 0
970 && entry
->next
!= NULL
971 && entry
->next
->num_free_objects
> 0)
973 G
.pages
[order
] = entry
->next
;
975 G
.page_tails
[order
]->next
= entry
;
976 G
.page_tails
[order
] = entry
;
979 /* Calculate the object's address. */
980 result
= entry
->page
+ object_offset
;
982 #ifdef ENABLE_GC_CHECKING
983 /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
984 exact same semantics in presence of memory bugs, regardless of
985 ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
986 handle to avoid handle leak. */
987 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result
, OBJECT_SIZE (order
)));
989 /* `Poison' the entire allocated object, including any padding at
991 memset (result
, 0xaf, OBJECT_SIZE (order
));
993 /* Make the bytes after the end of the object unaccessible. Discard the
994 handle to avoid handle leak. */
995 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) result
+ size
,
996 OBJECT_SIZE (order
) - size
));
999 /* Tell Valgrind that the memory is there, but its content isn't
1000 defined. The bytes at the end of the object are still marked
1002 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result
, size
));
1004 /* Keep track of how many bytes are being allocated. This
1005 information is used in deciding when to collect. */
1006 G
.allocated
+= OBJECT_SIZE (order
);
1008 if (GGC_DEBUG_LEVEL
>= 3)
1009 fprintf (G
.debug_file
,
1010 "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
1011 (unsigned long) size
, (unsigned long) OBJECT_SIZE (order
), result
,
1017 /* If P is not marked, marks it and return false. Otherwise return true.
1018 P must have been allocated by the GC allocator; it mustn't point to
1019 static objects, stack variables, or memory allocated with malloc. */
1029 /* Look up the page on which the object is alloced. If the object
1030 wasn't allocated by the collector, we'll probably die. */
1031 entry
= lookup_page_table_entry (p
);
1032 #ifdef ENABLE_CHECKING
1037 /* Calculate the index of the object on the page; this is its bit
1038 position in the in_use_p bitmap. */
1039 bit
= OFFSET_TO_BIT (((const char *) p
) - entry
->page
, entry
->order
);
1040 word
= bit
/ HOST_BITS_PER_LONG
;
1041 mask
= (unsigned long) 1 << (bit
% HOST_BITS_PER_LONG
);
1043 /* If the bit was previously set, skip it. */
1044 if (entry
->in_use_p
[word
] & mask
)
1047 /* Otherwise set it, and decrement the free object count. */
1048 entry
->in_use_p
[word
] |= mask
;
1049 entry
->num_free_objects
-= 1;
1051 if (GGC_DEBUG_LEVEL
>= 4)
1052 fprintf (G
.debug_file
, "Marking %p\n", p
);
1057 /* Return 1 if P has been marked, zero otherwise.
1058 P must have been allocated by the GC allocator; it mustn't point to
1059 static objects, stack variables, or memory allocated with malloc. */
1069 /* Look up the page on which the object is alloced. If the object
1070 wasn't allocated by the collector, we'll probably die. */
1071 entry
= lookup_page_table_entry (p
);
1072 #ifdef ENABLE_CHECKING
1077 /* Calculate the index of the object on the page; this is its bit
1078 position in the in_use_p bitmap. */
1079 bit
= OFFSET_TO_BIT (((const char *) p
) - entry
->page
, entry
->order
);
1080 word
= bit
/ HOST_BITS_PER_LONG
;
1081 mask
= (unsigned long) 1 << (bit
% HOST_BITS_PER_LONG
);
1083 return (entry
->in_use_p
[word
] & mask
) != 0;
1086 /* Return the size of the gc-able object P. */
1092 page_entry
*pe
= lookup_page_table_entry (p
);
1093 return OBJECT_SIZE (pe
->order
);
1096 /* Subroutine of init_ggc which computes the pair of numbers used to
1097 perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1099 This algorithm is taken from Granlund and Montgomery's paper
1100 "Division by Invariant Integers using Multiplication"
1101 (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1105 compute_inverse (order
)
1108 unsigned size
, inv
, e
;
1110 /* There can be only one object per "page" in a bucket for sizes
1111 larger than half a machine page; it will always have offset zero. */
1112 if (OBJECT_SIZE (order
) > G
.pagesize
/2)
1114 if (OBJECTS_PER_PAGE (order
) != 1)
1117 DIV_MULT (order
) = 1;
1118 DIV_SHIFT (order
) = 0;
1122 size
= OBJECT_SIZE (order
);
1124 while (size
% 2 == 0)
1131 while (inv
* size
!= 1)
1132 inv
= inv
* (2 - inv
*size
);
1134 DIV_MULT (order
) = inv
;
1135 DIV_SHIFT (order
) = e
;
1138 /* Initialize the ggc-mmap allocator. */
1144 G
.pagesize
= getpagesize();
1145 G
.lg_pagesize
= exact_log2 (G
.pagesize
);
1147 #ifdef HAVE_MMAP_DEV_ZERO
1148 G
.dev_zero_fd
= open ("/dev/zero", O_RDONLY
);
1149 if (G
.dev_zero_fd
== -1)
1154 G
.debug_file
= fopen ("ggc-mmap.debug", "w");
1156 G
.debug_file
= stdout
;
1160 /* StunOS has an amazing off-by-one error for the first mmap allocation
1161 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1162 believe, is an unaligned page allocation, which would cause us to
1163 hork badly if we tried to use it. */
1165 char *p
= alloc_anon (NULL
, G
.pagesize
);
1166 struct page_entry
*e
;
1167 if ((size_t)p
& (G
.pagesize
- 1))
1169 /* How losing. Discard this one and try another. If we still
1170 can't get something useful, give up. */
1172 p
= alloc_anon (NULL
, G
.pagesize
);
1173 if ((size_t)p
& (G
.pagesize
- 1))
1177 /* We have a good page, might as well hold onto it... */
1178 e
= (struct page_entry
*) xcalloc (1, sizeof (struct page_entry
));
1179 e
->bytes
= G
.pagesize
;
1181 e
->next
= G
.free_pages
;
1186 /* Initialize the object size table. */
1187 for (order
= 0; order
< HOST_BITS_PER_PTR
; ++order
)
1188 object_size_table
[order
] = (size_t) 1 << order
;
1189 for (order
= HOST_BITS_PER_PTR
; order
< NUM_ORDERS
; ++order
)
1191 size_t s
= extra_order_size_table
[order
- HOST_BITS_PER_PTR
];
1193 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1194 so that we're sure of getting aligned memory. */
1195 s
= ROUND_UP (s
, MAX_ALIGNMENT
);
1196 object_size_table
[order
] = s
;
1199 /* Initialize the objects-per-page and inverse tables. */
1200 for (order
= 0; order
< NUM_ORDERS
; ++order
)
1202 objects_per_page_table
[order
] = G
.pagesize
/ OBJECT_SIZE (order
);
1203 if (objects_per_page_table
[order
] == 0)
1204 objects_per_page_table
[order
] = 1;
1205 compute_inverse (order
);
1208 /* Reset the size_lookup array to put appropriately sized objects in
1209 the special orders. All objects bigger than the previous power
1210 of two, but no greater than the special size, should go in the
1212 for (order
= HOST_BITS_PER_PTR
; order
< NUM_ORDERS
; ++order
)
1217 o
= size_lookup
[OBJECT_SIZE (order
)];
1218 for (i
= OBJECT_SIZE (order
); size_lookup
[i
] == o
; --i
)
1219 size_lookup
[i
] = order
;
1223 /* Increment the `GC context'. Objects allocated in an outer context
1224 are never freed, eliminating the need to register their roots. */
1232 if (G
.context_depth
>= HOST_BITS_PER_LONG
)
1236 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1237 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1240 ggc_recalculate_in_use_p (p
)
1246 /* Because the past-the-end bit in in_use_p is always set, we
1247 pretend there is one additional object. */
1248 num_objects
= OBJECTS_IN_PAGE (p
) + 1;
1250 /* Reset the free object count. */
1251 p
->num_free_objects
= num_objects
;
1253 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1255 i
< CEIL (BITMAP_SIZE (num_objects
),
1256 sizeof (*p
->in_use_p
));
1261 /* Something is in use if it is marked, or if it was in use in a
1262 context further down the context stack. */
1263 p
->in_use_p
[i
] |= p
->save_in_use_p
[i
];
1265 /* Decrement the free object count for every object allocated. */
1266 for (j
= p
->in_use_p
[i
]; j
; j
>>= 1)
1267 p
->num_free_objects
-= (j
& 1);
1270 if (p
->num_free_objects
>= num_objects
)
1274 /* Decrement the `GC context'. All objects allocated since the
1275 previous ggc_push_context are migrated to the outer context. */
1280 unsigned long omask
;
1281 unsigned order
, depth
;
1283 depth
= --G
.context_depth
;
1284 omask
= (unsigned long)1 << (depth
+ 1);
1286 if (!((G
.context_depth_allocations
| G
.context_depth_collections
) & omask
))
1289 G
.context_depth_allocations
|= (G
.context_depth_allocations
& omask
) >> 1;
1290 G
.context_depth_allocations
&= omask
- 1;
1291 G
.context_depth_collections
&= omask
- 1;
1293 /* Any remaining pages in the popped context are lowered to the new
1294 current context; i.e. objects allocated in the popped context and
1295 left over are imported into the previous context. */
1296 for (order
= 2; order
< NUM_ORDERS
; order
++)
1300 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
1302 if (p
->context_depth
> depth
)
1303 p
->context_depth
= depth
;
1305 /* If this page is now in the topmost context, and we'd
1306 saved its allocation state, restore it. */
1307 else if (p
->context_depth
== depth
&& p
->save_in_use_p
)
1309 ggc_recalculate_in_use_p (p
);
1310 free (p
->save_in_use_p
);
1311 p
->save_in_use_p
= 0;
1317 /* Unmark all objects. */
1324 for (order
= 2; order
< NUM_ORDERS
; order
++)
1328 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
1330 size_t num_objects
= OBJECTS_IN_PAGE (p
);
1331 size_t bitmap_size
= BITMAP_SIZE (num_objects
+ 1);
1333 #ifdef ENABLE_CHECKING
1334 /* The data should be page-aligned. */
1335 if ((size_t) p
->page
& (G
.pagesize
- 1))
1339 /* Pages that aren't in the topmost context are not collected;
1340 nevertheless, we need their in-use bit vectors to store GC
1341 marks. So, back them up first. */
1342 if (p
->context_depth
< G
.context_depth
)
1344 if (! p
->save_in_use_p
)
1345 p
->save_in_use_p
= xmalloc (bitmap_size
);
1346 memcpy (p
->save_in_use_p
, p
->in_use_p
, bitmap_size
);
1349 /* Reset reset the number of free objects and clear the
1350 in-use bits. These will be adjusted by mark_obj. */
1351 p
->num_free_objects
= num_objects
;
1352 memset (p
->in_use_p
, 0, bitmap_size
);
1354 /* Make sure the one-past-the-end bit is always set. */
1355 p
->in_use_p
[num_objects
/ HOST_BITS_PER_LONG
]
1356 = ((unsigned long) 1 << (num_objects
% HOST_BITS_PER_LONG
));
1361 /* Free all empty pages. Partially empty pages need no attention
1362 because the `mark' bit doubles as an `unused' bit. */
1369 for (order
= 2; order
< NUM_ORDERS
; order
++)
1371 /* The last page-entry to consider, regardless of entries
1372 placed at the end of the list. */
1373 page_entry
* const last
= G
.page_tails
[order
];
1376 size_t live_objects
;
1377 page_entry
*p
, *previous
;
1387 page_entry
*next
= p
->next
;
1389 /* Loop until all entries have been examined. */
1392 num_objects
= OBJECTS_IN_PAGE (p
);
1394 /* Add all live objects on this page to the count of
1395 allocated memory. */
1396 live_objects
= num_objects
- p
->num_free_objects
;
1398 G
.allocated
+= OBJECT_SIZE (order
) * live_objects
;
1400 /* Only objects on pages in the topmost context should get
1402 if (p
->context_depth
< G
.context_depth
)
1405 /* Remove the page if it's empty. */
1406 else if (live_objects
== 0)
1409 G
.pages
[order
] = next
;
1411 previous
->next
= next
;
1413 /* Are we removing the last element? */
1414 if (p
== G
.page_tails
[order
])
1415 G
.page_tails
[order
] = previous
;
1420 /* If the page is full, move it to the end. */
1421 else if (p
->num_free_objects
== 0)
1423 /* Don't move it if it's already at the end. */
1424 if (p
!= G
.page_tails
[order
])
1426 /* Move p to the end of the list. */
1428 G
.page_tails
[order
]->next
= p
;
1430 /* Update the tail pointer... */
1431 G
.page_tails
[order
] = p
;
1433 /* ... and the head pointer, if necessary. */
1435 G
.pages
[order
] = next
;
1437 previous
->next
= next
;
1442 /* If we've fallen through to here, it's a page in the
1443 topmost context that is neither full nor empty. Such a
1444 page must precede pages at lesser context depth in the
1445 list, so move it to the head. */
1446 else if (p
!= G
.pages
[order
])
1448 previous
->next
= p
->next
;
1449 p
->next
= G
.pages
[order
];
1451 /* Are we moving the last element? */
1452 if (G
.page_tails
[order
] == p
)
1453 G
.page_tails
[order
] = previous
;
1462 /* Now, restore the in_use_p vectors for any pages from contexts
1463 other than the current one. */
1464 for (p
= G
.pages
[order
]; p
; p
= p
->next
)
1465 if (p
->context_depth
!= G
.context_depth
)
1466 ggc_recalculate_in_use_p (p
);
1470 #ifdef ENABLE_GC_CHECKING
1471 /* Clobber all free objects. */
1478 for (order
= 2; order
< NUM_ORDERS
; order
++)
1480 size_t size
= OBJECT_SIZE (order
);
1483 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
1488 if (p
->context_depth
!= G
.context_depth
)
1489 /* Since we don't do any collection for pages in pushed
1490 contexts, there's no need to do any poisoning. And
1491 besides, the IN_USE_P array isn't valid until we pop
1495 num_objects
= OBJECTS_IN_PAGE (p
);
1496 for (i
= 0; i
< num_objects
; i
++)
1499 word
= i
/ HOST_BITS_PER_LONG
;
1500 bit
= i
% HOST_BITS_PER_LONG
;
1501 if (((p
->in_use_p
[word
] >> bit
) & 1) == 0)
1503 char *object
= p
->page
+ i
* size
;
1505 /* Keep poison-by-write when we expect to use Valgrind,
1506 so the exact same memory semantics is kept, in case
1507 there are memory errors. We override this request
1509 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (object
, size
));
1510 memset (object
, 0xa5, size
);
1512 /* Drop the handle to avoid handle leak. */
1513 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (object
, size
));
1521 /* Top level mark-and-sweep routine. */
1526 /* Avoid frequent unnecessary work by skipping collection if the
1527 total allocations haven't expanded much since the last
1529 float allocated_last_gc
=
1530 MAX (G
.allocated_last_gc
, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE
) * 1024);
1532 float min_expand
= allocated_last_gc
* PARAM_VALUE (GGC_MIN_EXPAND
) / 100;
1534 if (G
.allocated
< allocated_last_gc
+ min_expand
)
1537 timevar_push (TV_GC
);
1539 fprintf (stderr
, " {GC %luk -> ", (unsigned long) G
.allocated
/ 1024);
1541 /* Zero the total allocated bytes. This will be recalculated in the
1545 /* Release the pages we freed the last time we collected, but didn't
1546 reuse in the interim. */
1549 /* Indicate that we've seen collections at this context depth. */
1550 G
.context_depth_collections
= ((unsigned long)1 << (G
.context_depth
+ 1)) - 1;
1555 #ifdef ENABLE_GC_CHECKING
1561 G
.allocated_last_gc
= G
.allocated
;
1563 timevar_pop (TV_GC
);
1566 fprintf (stderr
, "%luk}", (unsigned long) G
.allocated
/ 1024);
1569 /* Print allocation statistics. */
1570 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1572 : ((x) < 1024*1024*10 \
1574 : (x) / (1024*1024))))
1575 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1578 ggc_print_statistics ()
1580 struct ggc_statistics stats
;
1582 size_t total_overhead
= 0;
1584 /* Clear the statistics. */
1585 memset (&stats
, 0, sizeof (stats
));
1587 /* Make sure collection will really occur. */
1588 G
.allocated_last_gc
= 0;
1590 /* Collect and print the statistics common across collectors. */
1591 ggc_print_common_statistics (stderr
, &stats
);
1593 /* Release free pages so that we will not count the bytes allocated
1594 there as part of the total allocated memory. */
1597 /* Collect some information about the various sizes of
1599 fprintf (stderr
, "\n%-5s %10s %10s %10s\n",
1600 "Size", "Allocated", "Used", "Overhead");
1601 for (i
= 0; i
< NUM_ORDERS
; ++i
)
1608 /* Skip empty entries. */
1612 overhead
= allocated
= in_use
= 0;
1614 /* Figure out the total number of bytes allocated for objects of
1615 this size, and how many of them are actually in use. Also figure
1616 out how much memory the page table is using. */
1617 for (p
= G
.pages
[i
]; p
; p
= p
->next
)
1619 allocated
+= p
->bytes
;
1621 (OBJECTS_IN_PAGE (p
) - p
->num_free_objects
) * OBJECT_SIZE (i
);
1623 overhead
+= (sizeof (page_entry
) - sizeof (long)
1624 + BITMAP_SIZE (OBJECTS_IN_PAGE (p
) + 1));
1626 fprintf (stderr
, "%-5lu %10lu%c %10lu%c %10lu%c\n",
1627 (unsigned long) OBJECT_SIZE (i
),
1628 SCALE (allocated
), LABEL (allocated
),
1629 SCALE (in_use
), LABEL (in_use
),
1630 SCALE (overhead
), LABEL (overhead
));
1631 total_overhead
+= overhead
;
1633 fprintf (stderr
, "%-5s %10lu%c %10lu%c %10lu%c\n", "Total",
1634 SCALE (G
.bytes_mapped
), LABEL (G
.bytes_mapped
),
1635 SCALE (G
.allocated
), LABEL(G
.allocated
),
1636 SCALE (total_overhead
), LABEL (total_overhead
));
1641 struct ggc_pch_ondisk
1643 unsigned totals
[NUM_ORDERS
];
1645 size_t base
[NUM_ORDERS
];
1646 size_t written
[NUM_ORDERS
];
1649 struct ggc_pch_data
*
1652 return xcalloc (sizeof (struct ggc_pch_data
), 1);
1656 ggc_pch_count_object (d
, x
, size
)
1657 struct ggc_pch_data
*d
;
1658 void *x ATTRIBUTE_UNUSED
;
1664 order
= size_lookup
[size
];
1668 while (size
> OBJECT_SIZE (order
))
1672 d
->d
.totals
[order
]++;
1676 ggc_pch_total_size (d
)
1677 struct ggc_pch_data
*d
;
1682 for (i
= 0; i
< NUM_ORDERS
; i
++)
1683 a
+= ROUND_UP (d
->d
.totals
[i
] * OBJECT_SIZE (i
), G
.pagesize
);
1688 ggc_pch_this_base (d
, base
)
1689 struct ggc_pch_data
*d
;
1692 size_t a
= (size_t) base
;
1695 for (i
= 0; i
< NUM_ORDERS
; i
++)
1698 a
+= ROUND_UP (d
->d
.totals
[i
] * OBJECT_SIZE (i
), G
.pagesize
);
1704 ggc_pch_alloc_object (d
, x
, size
)
1705 struct ggc_pch_data
*d
;
1706 void *x ATTRIBUTE_UNUSED
;
1713 order
= size_lookup
[size
];
1717 while (size
> OBJECT_SIZE (order
))
1721 result
= (char *) d
->base
[order
];
1722 d
->base
[order
] += OBJECT_SIZE (order
);
1727 ggc_pch_prepare_write (d
, f
)
1728 struct ggc_pch_data
* d ATTRIBUTE_UNUSED
;
1729 FILE * f ATTRIBUTE_UNUSED
;
1731 /* Nothing to do. */
1735 ggc_pch_write_object (d
, f
, x
, newx
, size
)
1736 struct ggc_pch_data
* d ATTRIBUTE_UNUSED
;
1739 void *newx ATTRIBUTE_UNUSED
;
1745 order
= size_lookup
[size
];
1749 while (size
> OBJECT_SIZE (order
))
1753 if (fwrite (x
, size
, 1, f
) != 1)
1754 fatal_io_error ("can't write PCH file");
1756 /* In the current implementation, SIZE is always equal to
1757 OBJECT_SIZE (order) and so the fseek is never executed. */
1758 if (size
!= OBJECT_SIZE (order
)
1759 && fseek (f
, OBJECT_SIZE (order
) - size
, SEEK_CUR
) != 0)
1760 fatal_io_error ("can't write PCH file");
1762 d
->written
[order
]++;
1763 if (d
->written
[order
] == d
->d
.totals
[order
]
1764 && fseek (f
, ROUND_UP_VALUE (d
->d
.totals
[order
] * OBJECT_SIZE (order
),
1767 fatal_io_error ("can't write PCH file");
1771 ggc_pch_finish (d
, f
)
1772 struct ggc_pch_data
* d
;
1775 if (fwrite (&d
->d
, sizeof (d
->d
), 1, f
) != 1)
1776 fatal_io_error ("can't write PCH file");
1781 ggc_pch_read (f
, addr
)
1785 struct ggc_pch_ondisk d
;
1789 /* We've just read in a PCH file. So, every object that used to be allocated
1796 /* No object read from a PCH file should ever be freed. So, set the
1797 context depth to 1, and set the depth of all the currently-allocated
1798 pages to be 1 too. PCH pages will have depth 0. */
1799 if (G
.context_depth
!= 0)
1801 G
.context_depth
= 1;
1802 for (i
= 0; i
< NUM_ORDERS
; i
++)
1805 for (p
= G
.pages
[i
]; p
!= NULL
; p
= p
->next
)
1806 p
->context_depth
= G
.context_depth
;
1809 /* Allocate the appropriate page-table entries for the pages read from
1811 if (fread (&d
, sizeof (d
), 1, f
) != 1)
1812 fatal_io_error ("can't read PCH file");
1814 for (i
= 0; i
< NUM_ORDERS
; i
++)
1816 struct page_entry
*entry
;
1822 if (d
.totals
[i
] == 0)
1825 bytes
= ROUND_UP (d
.totals
[i
] * OBJECT_SIZE (i
), G
.pagesize
);
1826 num_objs
= bytes
/ OBJECT_SIZE (i
);
1827 entry
= xcalloc (1, (sizeof (struct page_entry
)
1829 + BITMAP_SIZE (num_objs
+ 1)));
1830 entry
->bytes
= bytes
;
1832 entry
->context_depth
= 0;
1834 entry
->num_free_objects
= 0;
1838 j
+ HOST_BITS_PER_LONG
<= num_objs
+ 1;
1839 j
+= HOST_BITS_PER_LONG
)
1840 entry
->in_use_p
[j
/ HOST_BITS_PER_LONG
] = -1;
1841 for (; j
< num_objs
+ 1; j
++)
1842 entry
->in_use_p
[j
/ HOST_BITS_PER_LONG
]
1843 |= 1L << (j
% HOST_BITS_PER_LONG
);
1845 for (pte
= entry
->page
;
1846 pte
< entry
->page
+ entry
->bytes
;
1848 set_page_table_entry (pte
, entry
);
1850 if (G
.page_tails
[i
] != NULL
)
1851 G
.page_tails
[i
]->next
= entry
;
1854 G
.page_tails
[i
] = entry
;
1857 /* Update the statistics. */
1858 G
.allocated
= G
.allocated_last_gc
= offs
- (char *)addr
;