1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
29 #include "diagnostic-core.h"
31 #include "ggc-internal.h"
34 #include "hard-reg-set.h"
40 #include "basic-block.h"
42 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
43 file open. Prefer either to valloc. */
45 # undef HAVE_MMAP_DEV_ZERO
49 #ifdef HAVE_MMAP_DEV_ZERO
54 #define USING_MALLOC_PAGE_GROUPS
57 #if defined(HAVE_MADVISE) && HAVE_DECL_MADVISE && defined(MADV_DONTNEED) \
58 && defined(USING_MMAP)
59 # define USING_MADVISE
64 This garbage-collecting allocator allocates objects on one of a set
65 of pages. Each page can allocate objects of a single size only;
66 available sizes are powers of two starting at four bytes. The size
67 of an allocation request is rounded up to the next power of two
68 (`order'), and satisfied from the appropriate page.
70 Each page is recorded in a page-entry, which also maintains an
71 in-use bitmap of object positions on the page. This allows the
72 allocation state of a particular object to be flipped without
73 touching the page itself.
75 Each page-entry also has a context depth, which is used to track
76 pushing and popping of allocation contexts. Only objects allocated
77 in the current (highest-numbered) context may be collected.
79 Page entries are arranged in an array of singly-linked lists. The
80 array is indexed by the allocation size, in bits, of the pages on
81 it; i.e. all pages on a list allocate objects of the same size.
82 Pages are ordered on the list such that all non-full pages precede
83 all full pages, with non-full pages arranged in order of decreasing
86 Empty pages (of all orders) are kept on a single page cache list,
87 and are considered first when new pages are required; they are
88 deallocated at the start of the next collection if they haven't
89 been recycled by then. */
91 /* Define GGC_DEBUG_LEVEL to print debugging information.
92 0: No debugging output.
93 1: GC statistics only.
94 2: Page-entry allocations/deallocations as well.
95 3: Object allocations as well.
96 4: Object marks as well. */
97 #define GGC_DEBUG_LEVEL (0)
99 #ifndef HOST_BITS_PER_PTR
100 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
104 /* A two-level tree is used to look up the page-entry for a given
105 pointer. Two chunks of the pointer's bits are extracted to index
106 the first and second levels of the tree, as follows:
110 msb +----------------+----+------+------+ lsb
116 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
117 pages are aligned on system page boundaries. The next most
118 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
119 index values in the lookup table, respectively.
121 For 32-bit architectures and the settings below, there are no
122 leftover bits. For architectures with wider pointers, the lookup
123 tree points to a list of pages, which must be scanned to find the
126 #define PAGE_L1_BITS (8)
127 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
128 #define PAGE_L1_SIZE ((uintptr_t) 1 << PAGE_L1_BITS)
129 #define PAGE_L2_SIZE ((uintptr_t) 1 << PAGE_L2_BITS)
131 #define LOOKUP_L1(p) \
132 (((uintptr_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
134 #define LOOKUP_L2(p) \
135 (((uintptr_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
137 /* The number of objects per allocation page, for objects on a page of
138 the indicated ORDER. */
139 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
141 /* The number of objects in P. */
142 #define OBJECTS_IN_PAGE(P) ((P)->bytes / OBJECT_SIZE ((P)->order))
144 /* The size of an object on a page of the indicated ORDER. */
145 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
147 /* For speed, we avoid doing a general integer divide to locate the
148 offset in the allocation bitmap, by precalculating numbers M, S
149 such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
150 within the page which is evenly divisible by the object size Z. */
151 #define DIV_MULT(ORDER) inverse_table[ORDER].mult
152 #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
153 #define OFFSET_TO_BIT(OFFSET, ORDER) \
154 (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
156 /* We use this structure to determine the alignment required for
157 allocations. For power-of-two sized allocations, that's not a
158 problem, but it does matter for odd-sized allocations.
159 We do not care about alignment for floating-point types. */
161 struct max_alignment
{
169 /* The biggest alignment required. */
171 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
174 /* The number of extra orders, not corresponding to power-of-two sized
177 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
179 #define RTL_SIZE(NSLOTS) \
180 (RTX_HDR_SIZE + (NSLOTS) * sizeof (rtunion))
182 #define TREE_EXP_SIZE(OPS) \
183 (sizeof (struct tree_exp) + ((OPS) - 1) * sizeof (tree))
185 /* The Ith entry is the maximum size of an object to be stored in the
186 Ith extra order. Adding a new entry to this array is the *only*
187 thing you need to do to add a new special allocation size. */
189 static const size_t extra_order_size_table
[] = {
190 /* Extra orders for small non-power-of-two multiples of MAX_ALIGNMENT.
191 There are a lot of structures with these sizes and explicitly
192 listing them risks orders being dropped because they changed size. */
204 sizeof (struct tree_decl_non_common
),
205 sizeof (struct tree_field_decl
),
206 sizeof (struct tree_parm_decl
),
207 sizeof (struct tree_var_decl
),
208 sizeof (struct tree_type_non_common
),
209 sizeof (struct function
),
210 sizeof (struct basic_block_def
),
211 sizeof (struct cgraph_node
),
212 sizeof (struct loop
),
215 /* The total number of orders. */
217 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
219 /* Compute the smallest nonnegative number which when added to X gives
222 #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
224 /* Compute the smallest multiple of F that is >= X. */
226 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
228 /* Round X to next multiple of the page size */
230 #define PAGE_ALIGN(x) (((x) + G.pagesize - 1) & ~(G.pagesize - 1))
232 /* The Ith entry is the number of objects on a page or order I. */
234 static unsigned objects_per_page_table
[NUM_ORDERS
];
236 /* The Ith entry is the size of an object on a page of order I. */
238 static size_t object_size_table
[NUM_ORDERS
];
240 /* The Ith entry is a pair of numbers (mult, shift) such that
241 ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
242 for all k evenly divisible by OBJECT_SIZE(I). */
249 inverse_table
[NUM_ORDERS
];
251 /* A page_entry records the status of an allocation page. This
252 structure is dynamically sized to fit the bitmap in_use_p. */
253 typedef struct page_entry
255 /* The next page-entry with objects of the same size, or NULL if
256 this is the last page-entry. */
257 struct page_entry
*next
;
259 /* The previous page-entry with objects of the same size, or NULL if
260 this is the first page-entry. The PREV pointer exists solely to
261 keep the cost of ggc_free manageable. */
262 struct page_entry
*prev
;
264 /* The number of bytes allocated. (This will always be a multiple
265 of the host system page size.) */
268 /* The address at which the memory is allocated. */
271 #ifdef USING_MALLOC_PAGE_GROUPS
272 /* Back pointer to the page group this page came from. */
273 struct page_group
*group
;
276 /* This is the index in the by_depth varray where this page table
278 unsigned long index_by_depth
;
280 /* Context depth of this page. */
281 unsigned short context_depth
;
283 /* The number of free objects remaining on this page. */
284 unsigned short num_free_objects
;
286 /* A likely candidate for the bit position of a free object for the
287 next allocation from this page. */
288 unsigned short next_bit_hint
;
290 /* The lg of size of objects allocated from this page. */
293 /* Discarded page? */
296 /* A bit vector indicating whether or not objects are in use. The
297 Nth bit is one if the Nth object on this page is allocated. This
298 array is dynamically sized. */
299 unsigned long in_use_p
[1];
302 #ifdef USING_MALLOC_PAGE_GROUPS
303 /* A page_group describes a large allocation from malloc, from which
304 we parcel out aligned pages. */
305 typedef struct page_group
307 /* A linked list of all extant page groups. */
308 struct page_group
*next
;
310 /* The address we received from malloc. */
313 /* The size of the block. */
316 /* A bitmask of pages in use. */
321 #if HOST_BITS_PER_PTR <= 32
323 /* On 32-bit hosts, we use a two level page table, as pictured above. */
324 typedef page_entry
**page_table
[PAGE_L1_SIZE
];
328 /* On 64-bit hosts, we use the same two level page tables plus a linked
329 list that disambiguates the top 32-bits. There will almost always be
330 exactly one entry in the list. */
331 typedef struct page_table_chain
333 struct page_table_chain
*next
;
335 page_entry
**table
[PAGE_L1_SIZE
];
343 finalizer (void *addr
, void (*f
)(void *)) : m_addr (addr
), m_function (f
) {}
345 void *addr () const { return m_addr
; }
347 void call () const { m_function (m_addr
); }
351 void (*m_function
)(void *);
357 vec_finalizer (uintptr_t addr
, void (*f
)(void *), size_t s
, size_t n
) :
358 m_addr (addr
), m_function (f
), m_object_size (s
), m_n_objects (n
) {}
362 for (size_t i
= 0; i
< m_n_objects
; i
++)
363 m_function (reinterpret_cast<void *> (m_addr
+ (i
* m_object_size
)));
366 void *addr () const { return reinterpret_cast<void *> (m_addr
); }
370 void (*m_function
)(void *);
371 size_t m_object_size
;
375 #ifdef ENABLE_GC_ALWAYS_COLLECT
376 /* List of free objects to be verified as actually free on the
381 struct free_object
*next
;
385 /* The rest of the global variables. */
386 static struct ggc_globals
388 /* The Nth element in this array is a page with objects of size 2^N.
389 If there are any pages with free objects, they will be at the
390 head of the list. NULL if there are no page-entries for this
392 page_entry
*pages
[NUM_ORDERS
];
394 /* The Nth element in this array is the last page with objects of
395 size 2^N. NULL if there are no page-entries for this object
397 page_entry
*page_tails
[NUM_ORDERS
];
399 /* Lookup table for associating allocation pages with object addresses. */
402 /* The system's page size. */
406 /* Bytes currently allocated. */
409 /* Bytes currently allocated at the end of the last collection. */
410 size_t allocated_last_gc
;
412 /* Total amount of memory mapped. */
415 /* Bit N set if any allocations have been done at context depth N. */
416 unsigned long context_depth_allocations
;
418 /* Bit N set if any collections have been done at context depth N. */
419 unsigned long context_depth_collections
;
421 /* The current depth in the context stack. */
422 unsigned short context_depth
;
424 /* A file descriptor open to /dev/zero for reading. */
425 #if defined (HAVE_MMAP_DEV_ZERO)
429 /* A cache of free system pages. */
430 page_entry
*free_pages
;
432 #ifdef USING_MALLOC_PAGE_GROUPS
433 page_group
*page_groups
;
436 /* The file descriptor for debugging output. */
439 /* Current number of elements in use in depth below. */
440 unsigned int depth_in_use
;
442 /* Maximum number of elements that can be used before resizing. */
443 unsigned int depth_max
;
445 /* Each element of this array is an index in by_depth where the given
446 depth starts. This structure is indexed by that given depth we
447 are interested in. */
450 /* Current number of elements in use in by_depth below. */
451 unsigned int by_depth_in_use
;
453 /* Maximum number of elements that can be used before resizing. */
454 unsigned int by_depth_max
;
456 /* Each element of this array is a pointer to a page_entry, all
457 page_entries can be found in here by increasing depth.
458 index_by_depth in the page_entry is the index into this data
459 structure where that page_entry can be found. This is used to
460 speed up finding all page_entries at a particular depth. */
461 page_entry
**by_depth
;
463 /* Each element is a pointer to the saved in_use_p bits, if any,
464 zero otherwise. We allocate them all together, to enable a
465 better runtime data access pattern. */
466 unsigned long **save_in_use
;
468 /* Finalizers for single objects. */
469 vec
<finalizer
> finalizers
;
471 /* Finalizers for vectors of objects. */
472 vec
<vec_finalizer
> vec_finalizers
;
474 #ifdef ENABLE_GC_ALWAYS_COLLECT
475 /* List of free objects to be verified as actually free on the
477 struct free_object
*free_object_list
;
482 /* Total GC-allocated memory. */
483 unsigned long long total_allocated
;
484 /* Total overhead for GC-allocated memory. */
485 unsigned long long total_overhead
;
487 /* Total allocations and overhead for sizes less than 32, 64 and 128.
488 These sizes are interesting because they are typical cache line
491 unsigned long long total_allocated_under32
;
492 unsigned long long total_overhead_under32
;
494 unsigned long long total_allocated_under64
;
495 unsigned long long total_overhead_under64
;
497 unsigned long long total_allocated_under128
;
498 unsigned long long total_overhead_under128
;
500 /* The allocations for each of the allocation orders. */
501 unsigned long long total_allocated_per_order
[NUM_ORDERS
];
503 /* The overhead for each of the allocation orders. */
504 unsigned long long total_overhead_per_order
[NUM_ORDERS
];
508 /* True if a gc is currently taking place. */
510 static bool in_gc
= false;
512 /* The size in bytes required to maintain a bitmap for the objects
514 #define BITMAP_SIZE(Num_objects) \
515 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof (long))
517 /* Allocate pages in chunks of this size, to throttle calls to memory
518 allocation routines. The first page is used, the rest go onto the
519 free list. This cannot be larger than HOST_BITS_PER_INT for the
520 in_use bitmask for page_group. Hosts that need a different value
521 can override this by defining GGC_QUIRE_SIZE explicitly. */
522 #ifndef GGC_QUIRE_SIZE
524 # define GGC_QUIRE_SIZE 512 /* 2MB for 4K pages */
526 # define GGC_QUIRE_SIZE 16
530 /* Initial guess as to how many page table entries we might need. */
531 #define INITIAL_PTE_COUNT 128
533 static int ggc_allocated_p (const void *);
534 static page_entry
*lookup_page_table_entry (const void *);
535 static void set_page_table_entry (void *, page_entry
*);
537 static char *alloc_anon (char *, size_t, bool check
);
539 #ifdef USING_MALLOC_PAGE_GROUPS
540 static size_t page_group_index (char *, char *);
541 static void set_page_group_in_use (page_group
*, char *);
542 static void clear_page_group_in_use (page_group
*, char *);
544 static struct page_entry
* alloc_page (unsigned);
545 static void free_page (struct page_entry
*);
546 static void release_pages (void);
547 static void clear_marks (void);
548 static void sweep_pages (void);
549 static void ggc_recalculate_in_use_p (page_entry
*);
550 static void compute_inverse (unsigned);
551 static inline void adjust_depth (void);
552 static void move_ptes_to_front (int, int);
554 void debug_print_page_list (int);
555 static void push_depth (unsigned int);
556 static void push_by_depth (page_entry
*, unsigned long *);
558 /* Push an entry onto G.depth. */
561 push_depth (unsigned int i
)
563 if (G
.depth_in_use
>= G
.depth_max
)
566 G
.depth
= XRESIZEVEC (unsigned int, G
.depth
, G
.depth_max
);
568 G
.depth
[G
.depth_in_use
++] = i
;
571 /* Push an entry onto G.by_depth and G.save_in_use. */
574 push_by_depth (page_entry
*p
, unsigned long *s
)
576 if (G
.by_depth_in_use
>= G
.by_depth_max
)
579 G
.by_depth
= XRESIZEVEC (page_entry
*, G
.by_depth
, G
.by_depth_max
);
580 G
.save_in_use
= XRESIZEVEC (unsigned long *, G
.save_in_use
,
583 G
.by_depth
[G
.by_depth_in_use
] = p
;
584 G
.save_in_use
[G
.by_depth_in_use
++] = s
;
587 #if (GCC_VERSION < 3001)
588 #define prefetch(X) ((void) X)
590 #define prefetch(X) __builtin_prefetch (X)
593 #define save_in_use_p_i(__i) \
595 #define save_in_use_p(__p) \
596 (save_in_use_p_i (__p->index_by_depth))
598 /* Returns nonzero if P was allocated in GC'able memory. */
601 ggc_allocated_p (const void *p
)
606 #if HOST_BITS_PER_PTR <= 32
609 page_table table
= G
.lookup
;
610 uintptr_t high_bits
= (uintptr_t) p
& ~ (uintptr_t) 0xffffffff;
615 if (table
->high_bits
== high_bits
)
619 base
= &table
->table
[0];
622 /* Extract the level 1 and 2 indices. */
626 return base
[L1
] && base
[L1
][L2
];
629 /* Traverse the page table and find the entry for a page.
630 Die (probably) if the object wasn't allocated via GC. */
632 static inline page_entry
*
633 lookup_page_table_entry (const void *p
)
638 #if HOST_BITS_PER_PTR <= 32
641 page_table table
= G
.lookup
;
642 uintptr_t high_bits
= (uintptr_t) p
& ~ (uintptr_t) 0xffffffff;
643 while (table
->high_bits
!= high_bits
)
645 base
= &table
->table
[0];
648 /* Extract the level 1 and 2 indices. */
655 /* Set the page table entry for a page. */
658 set_page_table_entry (void *p
, page_entry
*entry
)
663 #if HOST_BITS_PER_PTR <= 32
667 uintptr_t high_bits
= (uintptr_t) p
& ~ (uintptr_t) 0xffffffff;
668 for (table
= G
.lookup
; table
; table
= table
->next
)
669 if (table
->high_bits
== high_bits
)
672 /* Not found -- allocate a new table. */
673 table
= XCNEW (struct page_table_chain
);
674 table
->next
= G
.lookup
;
675 table
->high_bits
= high_bits
;
678 base
= &table
->table
[0];
681 /* Extract the level 1 and 2 indices. */
685 if (base
[L1
] == NULL
)
686 base
[L1
] = XCNEWVEC (page_entry
*, PAGE_L2_SIZE
);
688 base
[L1
][L2
] = entry
;
691 /* Prints the page-entry for object size ORDER, for debugging. */
694 debug_print_page_list (int order
)
697 printf ("Head=%p, Tail=%p:\n", (void *) G
.pages
[order
],
698 (void *) G
.page_tails
[order
]);
702 printf ("%p(%1d|%3d) -> ", (void *) p
, p
->context_depth
,
703 p
->num_free_objects
);
711 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
712 (if non-null). The ifdef structure here is intended to cause a
713 compile error unless exactly one of the HAVE_* is defined. */
716 alloc_anon (char *pref ATTRIBUTE_UNUSED
, size_t size
, bool check
)
718 #ifdef HAVE_MMAP_ANON
719 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
720 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
722 #ifdef HAVE_MMAP_DEV_ZERO
723 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
724 MAP_PRIVATE
, G
.dev_zero_fd
, 0);
727 if (page
== (char *) MAP_FAILED
)
731 perror ("virtual memory exhausted");
732 exit (FATAL_EXIT_CODE
);
735 /* Remember that we allocated this memory. */
736 G
.bytes_mapped
+= size
;
738 /* Pretend we don't have access to the allocated pages. We'll enable
739 access to smaller pieces of the area in ggc_internal_alloc. Discard the
740 handle to avoid handle leak. */
741 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (page
, size
));
746 #ifdef USING_MALLOC_PAGE_GROUPS
747 /* Compute the index for this page into the page group. */
750 page_group_index (char *allocation
, char *page
)
752 return (size_t) (page
- allocation
) >> G
.lg_pagesize
;
755 /* Set and clear the in_use bit for this page in the page group. */
758 set_page_group_in_use (page_group
*group
, char *page
)
760 group
->in_use
|= 1 << page_group_index (group
->allocation
, page
);
764 clear_page_group_in_use (page_group
*group
, char *page
)
766 group
->in_use
&= ~(1 << page_group_index (group
->allocation
, page
));
770 /* Allocate a new page for allocating objects of size 2^ORDER,
771 and return an entry for it. The entry is not added to the
772 appropriate page_table list. */
774 static inline struct page_entry
*
775 alloc_page (unsigned order
)
777 struct page_entry
*entry
, *p
, **pp
;
781 size_t page_entry_size
;
783 #ifdef USING_MALLOC_PAGE_GROUPS
787 num_objects
= OBJECTS_PER_PAGE (order
);
788 bitmap_size
= BITMAP_SIZE (num_objects
+ 1);
789 page_entry_size
= sizeof (page_entry
) - sizeof (long) + bitmap_size
;
790 entry_size
= num_objects
* OBJECT_SIZE (order
);
791 if (entry_size
< G
.pagesize
)
792 entry_size
= G
.pagesize
;
793 entry_size
= PAGE_ALIGN (entry_size
);
798 /* Check the list of free pages for one we can use. */
799 for (pp
= &G
.free_pages
, p
= *pp
; p
; pp
= &p
->next
, p
= *pp
)
800 if (p
->bytes
== entry_size
)
806 G
.bytes_mapped
+= p
->bytes
;
807 p
->discarded
= false;
809 /* Recycle the allocated memory from this page ... */
813 #ifdef USING_MALLOC_PAGE_GROUPS
817 /* ... and, if possible, the page entry itself. */
818 if (p
->order
== order
)
821 memset (entry
, 0, page_entry_size
);
827 else if (entry_size
== G
.pagesize
)
829 /* We want just one page. Allocate a bunch of them and put the
830 extras on the freelist. (Can only do this optimization with
831 mmap for backing store.) */
832 struct page_entry
*e
, *f
= G
.free_pages
;
833 int i
, entries
= GGC_QUIRE_SIZE
;
835 page
= alloc_anon (NULL
, G
.pagesize
* GGC_QUIRE_SIZE
, false);
838 page
= alloc_anon (NULL
, G
.pagesize
, true);
842 /* This loop counts down so that the chain will be in ascending
844 for (i
= entries
- 1; i
>= 1; i
--)
846 e
= XCNEWVAR (struct page_entry
, page_entry_size
);
848 e
->bytes
= G
.pagesize
;
849 e
->page
= page
+ (i
<< G
.lg_pagesize
);
857 page
= alloc_anon (NULL
, entry_size
, true);
859 #ifdef USING_MALLOC_PAGE_GROUPS
862 /* Allocate a large block of memory and serve out the aligned
863 pages therein. This results in much less memory wastage
864 than the traditional implementation of valloc. */
866 char *allocation
, *a
, *enda
;
867 size_t alloc_size
, head_slop
, tail_slop
;
868 int multiple_pages
= (entry_size
== G
.pagesize
);
871 alloc_size
= GGC_QUIRE_SIZE
* G
.pagesize
;
873 alloc_size
= entry_size
+ G
.pagesize
- 1;
874 allocation
= XNEWVEC (char, alloc_size
);
876 page
= (char *) (((uintptr_t) allocation
+ G
.pagesize
- 1) & -G
.pagesize
);
877 head_slop
= page
- allocation
;
879 tail_slop
= ((size_t) allocation
+ alloc_size
) & (G
.pagesize
- 1);
881 tail_slop
= alloc_size
- entry_size
- head_slop
;
882 enda
= allocation
+ alloc_size
- tail_slop
;
884 /* We allocated N pages, which are likely not aligned, leaving
885 us with N-1 usable pages. We plan to place the page_group
886 structure somewhere in the slop. */
887 if (head_slop
>= sizeof (page_group
))
888 group
= (page_group
*)page
- 1;
891 /* We magically got an aligned allocation. Too bad, we have
892 to waste a page anyway. */
896 tail_slop
+= G
.pagesize
;
898 gcc_assert (tail_slop
>= sizeof (page_group
));
899 group
= (page_group
*)enda
;
900 tail_slop
-= sizeof (page_group
);
903 /* Remember that we allocated this memory. */
904 group
->next
= G
.page_groups
;
905 group
->allocation
= allocation
;
906 group
->alloc_size
= alloc_size
;
908 G
.page_groups
= group
;
909 G
.bytes_mapped
+= alloc_size
;
911 /* If we allocated multiple pages, put the rest on the free list. */
914 struct page_entry
*e
, *f
= G
.free_pages
;
915 for (a
= enda
- G
.pagesize
; a
!= page
; a
-= G
.pagesize
)
917 e
= XCNEWVAR (struct page_entry
, page_entry_size
);
919 e
->bytes
= G
.pagesize
;
931 entry
= XCNEWVAR (struct page_entry
, page_entry_size
);
933 entry
->bytes
= entry_size
;
935 entry
->context_depth
= G
.context_depth
;
936 entry
->order
= order
;
937 entry
->num_free_objects
= num_objects
;
938 entry
->next_bit_hint
= 1;
940 G
.context_depth_allocations
|= (unsigned long)1 << G
.context_depth
;
942 #ifdef USING_MALLOC_PAGE_GROUPS
943 entry
->group
= group
;
944 set_page_group_in_use (group
, page
);
947 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
948 increment the hint. */
949 entry
->in_use_p
[num_objects
/ HOST_BITS_PER_LONG
]
950 = (unsigned long) 1 << (num_objects
% HOST_BITS_PER_LONG
);
952 set_page_table_entry (page
, entry
);
954 if (GGC_DEBUG_LEVEL
>= 2)
955 fprintf (G
.debug_file
,
956 "Allocating page at %p, object size=%lu, data %p-%p\n",
957 (void *) entry
, (unsigned long) OBJECT_SIZE (order
), page
,
958 page
+ entry_size
- 1);
963 /* Adjust the size of G.depth so that no index greater than the one
964 used by the top of the G.by_depth is used. */
971 if (G
.by_depth_in_use
)
973 top
= G
.by_depth
[G
.by_depth_in_use
-1];
975 /* Peel back indices in depth that index into by_depth, so that
976 as new elements are added to by_depth, we note the indices
977 of those elements, if they are for new context depths. */
978 while (G
.depth_in_use
> (size_t)top
->context_depth
+1)
983 /* For a page that is no longer needed, put it on the free page list. */
986 free_page (page_entry
*entry
)
988 if (GGC_DEBUG_LEVEL
>= 2)
989 fprintf (G
.debug_file
,
990 "Deallocating page at %p, data %p-%p\n", (void *) entry
,
991 entry
->page
, entry
->page
+ entry
->bytes
- 1);
993 /* Mark the page as inaccessible. Discard the handle to avoid handle
995 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (entry
->page
, entry
->bytes
));
997 set_page_table_entry (entry
->page
, NULL
);
999 #ifdef USING_MALLOC_PAGE_GROUPS
1000 clear_page_group_in_use (entry
->group
, entry
->page
);
1003 if (G
.by_depth_in_use
> 1)
1005 page_entry
*top
= G
.by_depth
[G
.by_depth_in_use
-1];
1006 int i
= entry
->index_by_depth
;
1008 /* We cannot free a page from a context deeper than the current
1010 gcc_assert (entry
->context_depth
== top
->context_depth
);
1012 /* Put top element into freed slot. */
1013 G
.by_depth
[i
] = top
;
1014 G
.save_in_use
[i
] = G
.save_in_use
[G
.by_depth_in_use
-1];
1015 top
->index_by_depth
= i
;
1017 --G
.by_depth_in_use
;
1021 entry
->next
= G
.free_pages
;
1022 G
.free_pages
= entry
;
1025 /* Release the free page cache to the system. */
1028 release_pages (void)
1030 #ifdef USING_MADVISE
1031 page_entry
*p
, *start_p
;
1035 page_entry
*next
, *prev
, *newprev
;
1036 size_t free_unit
= (GGC_QUIRE_SIZE
/2) * G
.pagesize
;
1038 /* First free larger continuous areas to the OS.
1039 This allows other allocators to grab these areas if needed.
1040 This is only done on larger chunks to avoid fragmentation.
1041 This does not always work because the free_pages list is only
1042 approximately sorted. */
1053 while (p
&& p
->page
== start
+ len
)
1057 mapped_len
+= p
->bytes
;
1061 if (len
>= free_unit
)
1063 while (start_p
!= p
)
1065 next
= start_p
->next
;
1069 munmap (start
, len
);
1074 G
.bytes_mapped
-= mapped_len
;
1080 /* Now give back the fragmented pages to the OS, but keep the address
1081 space to reuse it next time. */
1083 for (p
= G
.free_pages
; p
; )
1094 while (p
&& p
->page
== start
+ len
)
1099 /* Give the page back to the kernel, but don't free the mapping.
1100 This avoids fragmentation in the virtual memory map of the
1101 process. Next time we can reuse it by just touching it. */
1102 madvise (start
, len
, MADV_DONTNEED
);
1103 /* Don't count those pages as mapped to not touch the garbage collector
1105 G
.bytes_mapped
-= len
;
1106 while (start_p
!= p
)
1108 start_p
->discarded
= true;
1109 start_p
= start_p
->next
;
1113 #if defined(USING_MMAP) && !defined(USING_MADVISE)
1114 page_entry
*p
, *next
;
1118 /* Gather up adjacent pages so they are unmapped together. */
1129 while (p
&& p
->page
== start
+ len
)
1137 munmap (start
, len
);
1138 G
.bytes_mapped
-= len
;
1141 G
.free_pages
= NULL
;
1143 #ifdef USING_MALLOC_PAGE_GROUPS
1144 page_entry
**pp
, *p
;
1145 page_group
**gp
, *g
;
1147 /* Remove all pages from free page groups from the list. */
1149 while ((p
= *pp
) != NULL
)
1150 if (p
->group
->in_use
== 0)
1158 /* Remove all free page groups, and release the storage. */
1159 gp
= &G
.page_groups
;
1160 while ((g
= *gp
) != NULL
)
1164 G
.bytes_mapped
-= g
->alloc_size
;
1165 free (g
->allocation
);
1172 /* This table provides a fast way to determine ceil(log_2(size)) for
1173 allocation requests. The minimum allocation size is eight bytes. */
1174 #define NUM_SIZE_LOOKUP 512
1175 static unsigned char size_lookup
[NUM_SIZE_LOOKUP
] =
1177 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
1178 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1179 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1180 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1181 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1182 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1183 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1184 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1185 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1186 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1187 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1188 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1189 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1190 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1191 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1192 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1193 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1194 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1195 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1196 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1197 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1198 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1199 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1200 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1201 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1202 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1203 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1204 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1205 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1206 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1207 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
1208 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9
1211 /* For a given size of memory requested for allocation, return the
1212 actual size that is going to be allocated, as well as the size
1216 ggc_round_alloc_size_1 (size_t requested_size
,
1218 size_t *alloced_size
)
1220 size_t order
, object_size
;
1222 if (requested_size
< NUM_SIZE_LOOKUP
)
1224 order
= size_lookup
[requested_size
];
1225 object_size
= OBJECT_SIZE (order
);
1230 while (requested_size
> (object_size
= OBJECT_SIZE (order
)))
1235 *size_order
= order
;
1237 *alloced_size
= object_size
;
1240 /* For a given size of memory requested for allocation, return the
1241 actual size that is going to be allocated. */
1244 ggc_round_alloc_size (size_t requested_size
)
1248 ggc_round_alloc_size_1 (requested_size
, NULL
, &size
);
1252 /* Allocate a chunk of memory of SIZE bytes. Its contents are undefined. */
1255 ggc_internal_alloc (size_t size
, void (*f
)(void *), size_t s
, size_t n
1258 size_t order
, word
, bit
, object_offset
, object_size
;
1259 struct page_entry
*entry
;
1262 ggc_round_alloc_size_1 (size
, &order
, &object_size
);
1264 /* If there are non-full pages for this size allocation, they are at
1265 the head of the list. */
1266 entry
= G
.pages
[order
];
1268 /* If there is no page for this object size, or all pages in this
1269 context are full, allocate a new page. */
1270 if (entry
== NULL
|| entry
->num_free_objects
== 0)
1272 struct page_entry
*new_entry
;
1273 new_entry
= alloc_page (order
);
1275 new_entry
->index_by_depth
= G
.by_depth_in_use
;
1276 push_by_depth (new_entry
, 0);
1278 /* We can skip context depths, if we do, make sure we go all the
1279 way to the new depth. */
1280 while (new_entry
->context_depth
>= G
.depth_in_use
)
1281 push_depth (G
.by_depth_in_use
-1);
1283 /* If this is the only entry, it's also the tail. If it is not
1284 the only entry, then we must update the PREV pointer of the
1285 ENTRY (G.pages[order]) to point to our new page entry. */
1287 G
.page_tails
[order
] = new_entry
;
1289 entry
->prev
= new_entry
;
1291 /* Put new pages at the head of the page list. By definition the
1292 entry at the head of the list always has a NULL pointer. */
1293 new_entry
->next
= entry
;
1294 new_entry
->prev
= NULL
;
1296 G
.pages
[order
] = new_entry
;
1298 /* For a new page, we know the word and bit positions (in the
1299 in_use bitmap) of the first available object -- they're zero. */
1300 new_entry
->next_bit_hint
= 1;
1307 /* First try to use the hint left from the previous allocation
1308 to locate a clear bit in the in-use bitmap. We've made sure
1309 that the one-past-the-end bit is always set, so if the hint
1310 has run over, this test will fail. */
1311 unsigned hint
= entry
->next_bit_hint
;
1312 word
= hint
/ HOST_BITS_PER_LONG
;
1313 bit
= hint
% HOST_BITS_PER_LONG
;
1315 /* If the hint didn't work, scan the bitmap from the beginning. */
1316 if ((entry
->in_use_p
[word
] >> bit
) & 1)
1319 while (~entry
->in_use_p
[word
] == 0)
1322 #if GCC_VERSION >= 3004
1323 bit
= __builtin_ctzl (~entry
->in_use_p
[word
]);
1325 while ((entry
->in_use_p
[word
] >> bit
) & 1)
1329 hint
= word
* HOST_BITS_PER_LONG
+ bit
;
1332 /* Next time, try the next bit. */
1333 entry
->next_bit_hint
= hint
+ 1;
1335 object_offset
= hint
* object_size
;
1338 /* Set the in-use bit. */
1339 entry
->in_use_p
[word
] |= ((unsigned long) 1 << bit
);
1341 /* Keep a running total of the number of free objects. If this page
1342 fills up, we may have to move it to the end of the list if the
1343 next page isn't full. If the next page is full, all subsequent
1344 pages are full, so there's no need to move it. */
1345 if (--entry
->num_free_objects
== 0
1346 && entry
->next
!= NULL
1347 && entry
->next
->num_free_objects
> 0)
1349 /* We have a new head for the list. */
1350 G
.pages
[order
] = entry
->next
;
1352 /* We are moving ENTRY to the end of the page table list.
1353 The new page at the head of the list will have NULL in
1354 its PREV field and ENTRY will have NULL in its NEXT field. */
1355 entry
->next
->prev
= NULL
;
1358 /* Append ENTRY to the tail of the list. */
1359 entry
->prev
= G
.page_tails
[order
];
1360 G
.page_tails
[order
]->next
= entry
;
1361 G
.page_tails
[order
] = entry
;
1364 /* Calculate the object's address. */
1365 result
= entry
->page
+ object_offset
;
1366 if (GATHER_STATISTICS
)
1367 ggc_record_overhead (OBJECT_SIZE (order
), OBJECT_SIZE (order
) - size
,
1368 result FINAL_PASS_MEM_STAT
);
1370 #ifdef ENABLE_GC_CHECKING
1371 /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
1372 exact same semantics in presence of memory bugs, regardless of
1373 ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
1374 handle to avoid handle leak. */
1375 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result
, object_size
));
1377 /* `Poison' the entire allocated object, including any padding at
1379 memset (result
, 0xaf, object_size
);
1381 /* Make the bytes after the end of the object unaccessible. Discard the
1382 handle to avoid handle leak. */
1383 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((char *) result
+ size
,
1384 object_size
- size
));
1387 /* Tell Valgrind that the memory is there, but its content isn't
1388 defined. The bytes at the end of the object are still marked
1390 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result
, size
));
1392 /* Keep track of how many bytes are being allocated. This
1393 information is used in deciding when to collect. */
1394 G
.allocated
+= object_size
;
1396 /* For timevar statistics. */
1397 timevar_ggc_mem_total
+= object_size
;
1400 G
.finalizers
.safe_push (finalizer (result
, f
));
1402 G
.vec_finalizers
.safe_push
1403 (vec_finalizer (reinterpret_cast<uintptr_t> (result
), f
, s
, n
));
1405 if (GATHER_STATISTICS
)
1407 size_t overhead
= object_size
- size
;
1409 G
.stats
.total_overhead
+= overhead
;
1410 G
.stats
.total_allocated
+= object_size
;
1411 G
.stats
.total_overhead_per_order
[order
] += overhead
;
1412 G
.stats
.total_allocated_per_order
[order
] += object_size
;
1416 G
.stats
.total_overhead_under32
+= overhead
;
1417 G
.stats
.total_allocated_under32
+= object_size
;
1421 G
.stats
.total_overhead_under64
+= overhead
;
1422 G
.stats
.total_allocated_under64
+= object_size
;
1426 G
.stats
.total_overhead_under128
+= overhead
;
1427 G
.stats
.total_allocated_under128
+= object_size
;
1431 if (GGC_DEBUG_LEVEL
>= 3)
1432 fprintf (G
.debug_file
,
1433 "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
1434 (unsigned long) size
, (unsigned long) object_size
, result
,
1440 /* Mark function for strings. */
1443 gt_ggc_m_S (const void *p
)
1448 unsigned long offset
;
1450 if (!p
|| !ggc_allocated_p (p
))
1453 /* Look up the page on which the object is alloced. . */
1454 entry
= lookup_page_table_entry (p
);
1457 /* Calculate the index of the object on the page; this is its bit
1458 position in the in_use_p bitmap. Note that because a char* might
1459 point to the middle of an object, we need special code here to
1460 make sure P points to the start of an object. */
1461 offset
= ((const char *) p
- entry
->page
) % object_size_table
[entry
->order
];
1464 /* Here we've seen a char* which does not point to the beginning
1465 of an allocated object. We assume it points to the middle of
1467 gcc_assert (offset
== offsetof (struct tree_string
, str
));
1468 p
= ((const char *) p
) - offset
;
1469 gt_ggc_mx_lang_tree_node (CONST_CAST (void *, p
));
1473 bit
= OFFSET_TO_BIT (((const char *) p
) - entry
->page
, entry
->order
);
1474 word
= bit
/ HOST_BITS_PER_LONG
;
1475 mask
= (unsigned long) 1 << (bit
% HOST_BITS_PER_LONG
);
1477 /* If the bit was previously set, skip it. */
1478 if (entry
->in_use_p
[word
] & mask
)
1481 /* Otherwise set it, and decrement the free object count. */
1482 entry
->in_use_p
[word
] |= mask
;
1483 entry
->num_free_objects
-= 1;
1485 if (GGC_DEBUG_LEVEL
>= 4)
1486 fprintf (G
.debug_file
, "Marking %p\n", p
);
1492 /* User-callable entry points for marking string X. */
1495 gt_ggc_mx (const char *& x
)
1501 gt_ggc_mx (unsigned char *& x
)
1507 gt_ggc_mx (unsigned char& x ATTRIBUTE_UNUSED
)
1511 /* If P is not marked, marks it and return false. Otherwise return true.
1512 P must have been allocated by the GC allocator; it mustn't point to
1513 static objects, stack variables, or memory allocated with malloc. */
1516 ggc_set_mark (const void *p
)
1522 /* Look up the page on which the object is alloced. If the object
1523 wasn't allocated by the collector, we'll probably die. */
1524 entry
= lookup_page_table_entry (p
);
1527 /* Calculate the index of the object on the page; this is its bit
1528 position in the in_use_p bitmap. */
1529 bit
= OFFSET_TO_BIT (((const char *) p
) - entry
->page
, entry
->order
);
1530 word
= bit
/ HOST_BITS_PER_LONG
;
1531 mask
= (unsigned long) 1 << (bit
% HOST_BITS_PER_LONG
);
1533 /* If the bit was previously set, skip it. */
1534 if (entry
->in_use_p
[word
] & mask
)
1537 /* Otherwise set it, and decrement the free object count. */
1538 entry
->in_use_p
[word
] |= mask
;
1539 entry
->num_free_objects
-= 1;
1541 if (GGC_DEBUG_LEVEL
>= 4)
1542 fprintf (G
.debug_file
, "Marking %p\n", p
);
1547 /* Return 1 if P has been marked, zero otherwise.
1548 P must have been allocated by the GC allocator; it mustn't point to
1549 static objects, stack variables, or memory allocated with malloc. */
1552 ggc_marked_p (const void *p
)
1558 /* Look up the page on which the object is alloced. If the object
1559 wasn't allocated by the collector, we'll probably die. */
1560 entry
= lookup_page_table_entry (p
);
1563 /* Calculate the index of the object on the page; this is its bit
1564 position in the in_use_p bitmap. */
1565 bit
= OFFSET_TO_BIT (((const char *) p
) - entry
->page
, entry
->order
);
1566 word
= bit
/ HOST_BITS_PER_LONG
;
1567 mask
= (unsigned long) 1 << (bit
% HOST_BITS_PER_LONG
);
1569 return (entry
->in_use_p
[word
] & mask
) != 0;
1572 /* Return the size of the gc-able object P. */
1575 ggc_get_size (const void *p
)
1577 page_entry
*pe
= lookup_page_table_entry (p
);
1578 return OBJECT_SIZE (pe
->order
);
1581 /* Release the memory for object P. */
1589 page_entry
*pe
= lookup_page_table_entry (p
);
1590 size_t order
= pe
->order
;
1591 size_t size
= OBJECT_SIZE (order
);
1593 if (GATHER_STATISTICS
)
1594 ggc_free_overhead (p
);
1596 if (GGC_DEBUG_LEVEL
>= 3)
1597 fprintf (G
.debug_file
,
1598 "Freeing object, actual size=%lu, at %p on %p\n",
1599 (unsigned long) size
, p
, (void *) pe
);
1601 #ifdef ENABLE_GC_CHECKING
1602 /* Poison the data, to indicate the data is garbage. */
1603 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p
, size
));
1604 memset (p
, 0xa5, size
);
1606 /* Let valgrind know the object is free. */
1607 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p
, size
));
1609 #ifdef ENABLE_GC_ALWAYS_COLLECT
1610 /* In the completely-anal-checking mode, we do *not* immediately free
1611 the data, but instead verify that the data is *actually* not
1612 reachable the next time we collect. */
1614 struct free_object
*fo
= XNEW (struct free_object
);
1616 fo
->next
= G
.free_object_list
;
1617 G
.free_object_list
= fo
;
1621 unsigned int bit_offset
, word
, bit
;
1623 G
.allocated
-= size
;
1625 /* Mark the object not-in-use. */
1626 bit_offset
= OFFSET_TO_BIT (((const char *) p
) - pe
->page
, order
);
1627 word
= bit_offset
/ HOST_BITS_PER_LONG
;
1628 bit
= bit_offset
% HOST_BITS_PER_LONG
;
1629 pe
->in_use_p
[word
] &= ~(1UL << bit
);
1631 if (pe
->num_free_objects
++ == 0)
1635 /* If the page is completely full, then it's supposed to
1636 be after all pages that aren't. Since we've freed one
1637 object from a page that was full, we need to move the
1638 page to the head of the list.
1640 PE is the node we want to move. Q is the previous node
1641 and P is the next node in the list. */
1643 if (q
&& q
->num_free_objects
== 0)
1649 /* If PE was at the end of the list, then Q becomes the
1650 new end of the list. If PE was not the end of the
1651 list, then we need to update the PREV field for P. */
1653 G
.page_tails
[order
] = q
;
1657 /* Move PE to the head of the list. */
1658 pe
->next
= G
.pages
[order
];
1660 G
.pages
[order
]->prev
= pe
;
1661 G
.pages
[order
] = pe
;
1664 /* Reset the hint bit to point to the only free object. */
1665 pe
->next_bit_hint
= bit_offset
;
1671 /* Subroutine of init_ggc which computes the pair of numbers used to
1672 perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1674 This algorithm is taken from Granlund and Montgomery's paper
1675 "Division by Invariant Integers using Multiplication"
1676 (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1680 compute_inverse (unsigned order
)
1685 size
= OBJECT_SIZE (order
);
1687 while (size
% 2 == 0)
1694 while (inv
* size
!= 1)
1695 inv
= inv
* (2 - inv
*size
);
1697 DIV_MULT (order
) = inv
;
1698 DIV_SHIFT (order
) = e
;
1701 /* Initialize the ggc-mmap allocator. */
1705 static bool init_p
= false;
1712 G
.pagesize
= getpagesize ();
1713 G
.lg_pagesize
= exact_log2 (G
.pagesize
);
1715 #ifdef HAVE_MMAP_DEV_ZERO
1716 G
.dev_zero_fd
= open ("/dev/zero", O_RDONLY
);
1717 if (G
.dev_zero_fd
== -1)
1718 internal_error ("open /dev/zero: %m");
1722 G
.debug_file
= fopen ("ggc-mmap.debug", "w");
1724 G
.debug_file
= stdout
;
1728 /* StunOS has an amazing off-by-one error for the first mmap allocation
1729 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1730 believe, is an unaligned page allocation, which would cause us to
1731 hork badly if we tried to use it. */
1733 char *p
= alloc_anon (NULL
, G
.pagesize
, true);
1734 struct page_entry
*e
;
1735 if ((uintptr_t)p
& (G
.pagesize
- 1))
1737 /* How losing. Discard this one and try another. If we still
1738 can't get something useful, give up. */
1740 p
= alloc_anon (NULL
, G
.pagesize
, true);
1741 gcc_assert (!((uintptr_t)p
& (G
.pagesize
- 1)));
1744 /* We have a good page, might as well hold onto it... */
1745 e
= XCNEW (struct page_entry
);
1746 e
->bytes
= G
.pagesize
;
1748 e
->next
= G
.free_pages
;
1753 /* Initialize the object size table. */
1754 for (order
= 0; order
< HOST_BITS_PER_PTR
; ++order
)
1755 object_size_table
[order
] = (size_t) 1 << order
;
1756 for (order
= HOST_BITS_PER_PTR
; order
< NUM_ORDERS
; ++order
)
1758 size_t s
= extra_order_size_table
[order
- HOST_BITS_PER_PTR
];
1760 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1761 so that we're sure of getting aligned memory. */
1762 s
= ROUND_UP (s
, MAX_ALIGNMENT
);
1763 object_size_table
[order
] = s
;
1766 /* Initialize the objects-per-page and inverse tables. */
1767 for (order
= 0; order
< NUM_ORDERS
; ++order
)
1769 objects_per_page_table
[order
] = G
.pagesize
/ OBJECT_SIZE (order
);
1770 if (objects_per_page_table
[order
] == 0)
1771 objects_per_page_table
[order
] = 1;
1772 compute_inverse (order
);
1775 /* Reset the size_lookup array to put appropriately sized objects in
1776 the special orders. All objects bigger than the previous power
1777 of two, but no greater than the special size, should go in the
1779 for (order
= HOST_BITS_PER_PTR
; order
< NUM_ORDERS
; ++order
)
1784 i
= OBJECT_SIZE (order
);
1785 if (i
>= NUM_SIZE_LOOKUP
)
1788 for (o
= size_lookup
[i
]; o
== size_lookup
[i
]; --i
)
1789 size_lookup
[i
] = order
;
1794 G
.depth
= XNEWVEC (unsigned int, G
.depth_max
);
1796 G
.by_depth_in_use
= 0;
1797 G
.by_depth_max
= INITIAL_PTE_COUNT
;
1798 G
.by_depth
= XNEWVEC (page_entry
*, G
.by_depth_max
);
1799 G
.save_in_use
= XNEWVEC (unsigned long *, G
.by_depth_max
);
1802 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1803 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1806 ggc_recalculate_in_use_p (page_entry
*p
)
1811 /* Because the past-the-end bit in in_use_p is always set, we
1812 pretend there is one additional object. */
1813 num_objects
= OBJECTS_IN_PAGE (p
) + 1;
1815 /* Reset the free object count. */
1816 p
->num_free_objects
= num_objects
;
1818 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1820 i
< CEIL (BITMAP_SIZE (num_objects
),
1821 sizeof (*p
->in_use_p
));
1826 /* Something is in use if it is marked, or if it was in use in a
1827 context further down the context stack. */
1828 p
->in_use_p
[i
] |= save_in_use_p (p
)[i
];
1830 /* Decrement the free object count for every object allocated. */
1831 for (j
= p
->in_use_p
[i
]; j
; j
>>= 1)
1832 p
->num_free_objects
-= (j
& 1);
1835 gcc_assert (p
->num_free_objects
< num_objects
);
1838 /* Unmark all objects. */
1845 for (order
= 2; order
< NUM_ORDERS
; order
++)
1849 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
1851 size_t num_objects
= OBJECTS_IN_PAGE (p
);
1852 size_t bitmap_size
= BITMAP_SIZE (num_objects
+ 1);
1854 /* The data should be page-aligned. */
1855 gcc_assert (!((uintptr_t) p
->page
& (G
.pagesize
- 1)));
1857 /* Pages that aren't in the topmost context are not collected;
1858 nevertheless, we need their in-use bit vectors to store GC
1859 marks. So, back them up first. */
1860 if (p
->context_depth
< G
.context_depth
)
1862 if (! save_in_use_p (p
))
1863 save_in_use_p (p
) = XNEWVAR (unsigned long, bitmap_size
);
1864 memcpy (save_in_use_p (p
), p
->in_use_p
, bitmap_size
);
1867 /* Reset reset the number of free objects and clear the
1868 in-use bits. These will be adjusted by mark_obj. */
1869 p
->num_free_objects
= num_objects
;
1870 memset (p
->in_use_p
, 0, bitmap_size
);
1872 /* Make sure the one-past-the-end bit is always set. */
1873 p
->in_use_p
[num_objects
/ HOST_BITS_PER_LONG
]
1874 = ((unsigned long) 1 << (num_objects
% HOST_BITS_PER_LONG
));
1879 /* Check if any blocks with a registered finalizer have become unmarked. If so
1880 run the finalizer and unregister it because the block is about to be freed.
1881 Note that no garantee is made about what order finalizers will run in so
1882 touching other objects in gc memory is extremely unwise. */
1885 ggc_handle_finalizers ()
1887 if (G
.context_depth
!= 0)
1890 unsigned length
= G
.finalizers
.length ();
1891 for (unsigned int i
= 0; i
< length
;)
1893 finalizer
&f
= G
.finalizers
[i
];
1894 if (!ggc_marked_p (f
.addr ()))
1897 G
.finalizers
.unordered_remove (i
);
1905 length
= G
.vec_finalizers
.length ();
1906 for (unsigned int i
= 0; i
< length
;)
1908 vec_finalizer
&f
= G
.vec_finalizers
[i
];
1909 if (!ggc_marked_p (f
.addr ()))
1912 G
.vec_finalizers
.unordered_remove (i
);
1920 /* Free all empty pages. Partially empty pages need no attention
1921 because the `mark' bit doubles as an `unused' bit. */
1928 for (order
= 2; order
< NUM_ORDERS
; order
++)
1930 /* The last page-entry to consider, regardless of entries
1931 placed at the end of the list. */
1932 page_entry
* const last
= G
.page_tails
[order
];
1935 size_t live_objects
;
1936 page_entry
*p
, *previous
;
1946 page_entry
*next
= p
->next
;
1948 /* Loop until all entries have been examined. */
1951 num_objects
= OBJECTS_IN_PAGE (p
);
1953 /* Add all live objects on this page to the count of
1954 allocated memory. */
1955 live_objects
= num_objects
- p
->num_free_objects
;
1957 G
.allocated
+= OBJECT_SIZE (order
) * live_objects
;
1959 /* Only objects on pages in the topmost context should get
1961 if (p
->context_depth
< G
.context_depth
)
1964 /* Remove the page if it's empty. */
1965 else if (live_objects
== 0)
1967 /* If P was the first page in the list, then NEXT
1968 becomes the new first page in the list, otherwise
1969 splice P out of the forward pointers. */
1971 G
.pages
[order
] = next
;
1973 previous
->next
= next
;
1975 /* Splice P out of the back pointers too. */
1977 next
->prev
= previous
;
1979 /* Are we removing the last element? */
1980 if (p
== G
.page_tails
[order
])
1981 G
.page_tails
[order
] = previous
;
1986 /* If the page is full, move it to the end. */
1987 else if (p
->num_free_objects
== 0)
1989 /* Don't move it if it's already at the end. */
1990 if (p
!= G
.page_tails
[order
])
1992 /* Move p to the end of the list. */
1994 p
->prev
= G
.page_tails
[order
];
1995 G
.page_tails
[order
]->next
= p
;
1997 /* Update the tail pointer... */
1998 G
.page_tails
[order
] = p
;
2000 /* ... and the head pointer, if necessary. */
2002 G
.pages
[order
] = next
;
2004 previous
->next
= next
;
2006 /* And update the backpointer in NEXT if necessary. */
2008 next
->prev
= previous
;
2014 /* If we've fallen through to here, it's a page in the
2015 topmost context that is neither full nor empty. Such a
2016 page must precede pages at lesser context depth in the
2017 list, so move it to the head. */
2018 else if (p
!= G
.pages
[order
])
2020 previous
->next
= p
->next
;
2022 /* Update the backchain in the next node if it exists. */
2024 p
->next
->prev
= previous
;
2026 /* Move P to the head of the list. */
2027 p
->next
= G
.pages
[order
];
2029 G
.pages
[order
]->prev
= p
;
2031 /* Update the head pointer. */
2034 /* Are we moving the last element? */
2035 if (G
.page_tails
[order
] == p
)
2036 G
.page_tails
[order
] = previous
;
2045 /* Now, restore the in_use_p vectors for any pages from contexts
2046 other than the current one. */
2047 for (p
= G
.pages
[order
]; p
; p
= p
->next
)
2048 if (p
->context_depth
!= G
.context_depth
)
2049 ggc_recalculate_in_use_p (p
);
2053 #ifdef ENABLE_GC_CHECKING
2054 /* Clobber all free objects. */
2061 for (order
= 2; order
< NUM_ORDERS
; order
++)
2063 size_t size
= OBJECT_SIZE (order
);
2066 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
2071 if (p
->context_depth
!= G
.context_depth
)
2072 /* Since we don't do any collection for pages in pushed
2073 contexts, there's no need to do any poisoning. And
2074 besides, the IN_USE_P array isn't valid until we pop
2078 num_objects
= OBJECTS_IN_PAGE (p
);
2079 for (i
= 0; i
< num_objects
; i
++)
2082 word
= i
/ HOST_BITS_PER_LONG
;
2083 bit
= i
% HOST_BITS_PER_LONG
;
2084 if (((p
->in_use_p
[word
] >> bit
) & 1) == 0)
2086 char *object
= p
->page
+ i
* size
;
2088 /* Keep poison-by-write when we expect to use Valgrind,
2089 so the exact same memory semantics is kept, in case
2090 there are memory errors. We override this request
2092 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (object
,
2094 memset (object
, 0xa5, size
);
2096 /* Drop the handle to avoid handle leak. */
2097 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (object
, size
));
2104 #define poison_pages()
2107 #ifdef ENABLE_GC_ALWAYS_COLLECT
2108 /* Validate that the reportedly free objects actually are. */
2111 validate_free_objects (void)
2113 struct free_object
*f
, *next
, *still_free
= NULL
;
2115 for (f
= G
.free_object_list
; f
; f
= next
)
2117 page_entry
*pe
= lookup_page_table_entry (f
->object
);
2120 bit
= OFFSET_TO_BIT ((char *)f
->object
- pe
->page
, pe
->order
);
2121 word
= bit
/ HOST_BITS_PER_LONG
;
2122 bit
= bit
% HOST_BITS_PER_LONG
;
2125 /* Make certain it isn't visible from any root. Notice that we
2126 do this check before sweep_pages merges save_in_use_p. */
2127 gcc_assert (!(pe
->in_use_p
[word
] & (1UL << bit
)));
2129 /* If the object comes from an outer context, then retain the
2130 free_object entry, so that we can verify that the address
2131 isn't live on the stack in some outer context. */
2132 if (pe
->context_depth
!= G
.context_depth
)
2134 f
->next
= still_free
;
2141 G
.free_object_list
= still_free
;
2144 #define validate_free_objects()
2147 /* Top level mark-and-sweep routine. */
2152 /* Avoid frequent unnecessary work by skipping collection if the
2153 total allocations haven't expanded much since the last
2155 float allocated_last_gc
=
2156 MAX (G
.allocated_last_gc
, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE
) * 1024);
2158 float min_expand
= allocated_last_gc
* PARAM_VALUE (GGC_MIN_EXPAND
) / 100;
2159 if (G
.allocated
< allocated_last_gc
+ min_expand
&& !ggc_force_collect
)
2162 timevar_push (TV_GC
);
2164 fprintf (stderr
, " {GC %luk -> ", (unsigned long) G
.allocated
/ 1024);
2165 if (GGC_DEBUG_LEVEL
>= 2)
2166 fprintf (G
.debug_file
, "BEGIN COLLECTING\n");
2168 /* Zero the total allocated bytes. This will be recalculated in the
2172 /* Release the pages we freed the last time we collected, but didn't
2173 reuse in the interim. */
2176 /* Indicate that we've seen collections at this context depth. */
2177 G
.context_depth_collections
= ((unsigned long)1 << (G
.context_depth
+ 1)) - 1;
2179 invoke_plugin_callbacks (PLUGIN_GGC_START
, NULL
);
2184 ggc_handle_finalizers ();
2186 if (GATHER_STATISTICS
)
2187 ggc_prune_overhead_list ();
2190 validate_free_objects ();
2194 G
.allocated_last_gc
= G
.allocated
;
2196 invoke_plugin_callbacks (PLUGIN_GGC_END
, NULL
);
2198 timevar_pop (TV_GC
);
2201 fprintf (stderr
, "%luk}", (unsigned long) G
.allocated
/ 1024);
2202 if (GGC_DEBUG_LEVEL
>= 2)
2203 fprintf (G
.debug_file
, "END COLLECTING\n");
2206 /* Assume that all GGC memory is reachable and grow the limits for next collection.
2207 With checking, trigger GGC so -Q compilation outputs how much of memory really is
2213 #ifndef ENABLE_CHECKING
2214 G
.allocated_last_gc
= MAX (G
.allocated_last_gc
,
2220 fprintf (stderr
, " {GC start %luk} ", (unsigned long) G
.allocated
/ 1024);
2223 /* Print allocation statistics. */
2224 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
2226 : ((x) < 1024*1024*10 \
2228 : (x) / (1024*1024))))
2229 #define STAT_LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
2232 ggc_print_statistics (void)
2234 struct ggc_statistics stats
;
2236 size_t total_overhead
= 0;
2238 /* Clear the statistics. */
2239 memset (&stats
, 0, sizeof (stats
));
2241 /* Make sure collection will really occur. */
2242 G
.allocated_last_gc
= 0;
2244 /* Collect and print the statistics common across collectors. */
2245 ggc_print_common_statistics (stderr
, &stats
);
2247 /* Release free pages so that we will not count the bytes allocated
2248 there as part of the total allocated memory. */
2251 /* Collect some information about the various sizes of
2254 "Memory still allocated at the end of the compilation process\n");
2255 fprintf (stderr
, "%-8s %10s %10s %10s\n",
2256 "Size", "Allocated", "Used", "Overhead");
2257 for (i
= 0; i
< NUM_ORDERS
; ++i
)
2264 /* Skip empty entries. */
2268 overhead
= allocated
= in_use
= 0;
2270 /* Figure out the total number of bytes allocated for objects of
2271 this size, and how many of them are actually in use. Also figure
2272 out how much memory the page table is using. */
2273 for (p
= G
.pages
[i
]; p
; p
= p
->next
)
2275 allocated
+= p
->bytes
;
2277 (OBJECTS_IN_PAGE (p
) - p
->num_free_objects
) * OBJECT_SIZE (i
);
2279 overhead
+= (sizeof (page_entry
) - sizeof (long)
2280 + BITMAP_SIZE (OBJECTS_IN_PAGE (p
) + 1));
2282 fprintf (stderr
, "%-8lu %10lu%c %10lu%c %10lu%c\n",
2283 (unsigned long) OBJECT_SIZE (i
),
2284 SCALE (allocated
), STAT_LABEL (allocated
),
2285 SCALE (in_use
), STAT_LABEL (in_use
),
2286 SCALE (overhead
), STAT_LABEL (overhead
));
2287 total_overhead
+= overhead
;
2289 fprintf (stderr
, "%-8s %10lu%c %10lu%c %10lu%c\n", "Total",
2290 SCALE (G
.bytes_mapped
), STAT_LABEL (G
.bytes_mapped
),
2291 SCALE (G
.allocated
), STAT_LABEL (G
.allocated
),
2292 SCALE (total_overhead
), STAT_LABEL (total_overhead
));
2294 if (GATHER_STATISTICS
)
2296 fprintf (stderr
, "\nTotal allocations and overheads during "
2297 "the compilation process\n");
2299 fprintf (stderr
, "Total Overhead: %10"
2300 HOST_LONG_LONG_FORMAT
"d\n", G
.stats
.total_overhead
);
2301 fprintf (stderr
, "Total Allocated: %10"
2302 HOST_LONG_LONG_FORMAT
"d\n",
2303 G
.stats
.total_allocated
);
2305 fprintf (stderr
, "Total Overhead under 32B: %10"
2306 HOST_LONG_LONG_FORMAT
"d\n", G
.stats
.total_overhead_under32
);
2307 fprintf (stderr
, "Total Allocated under 32B: %10"
2308 HOST_LONG_LONG_FORMAT
"d\n", G
.stats
.total_allocated_under32
);
2309 fprintf (stderr
, "Total Overhead under 64B: %10"
2310 HOST_LONG_LONG_FORMAT
"d\n", G
.stats
.total_overhead_under64
);
2311 fprintf (stderr
, "Total Allocated under 64B: %10"
2312 HOST_LONG_LONG_FORMAT
"d\n", G
.stats
.total_allocated_under64
);
2313 fprintf (stderr
, "Total Overhead under 128B: %10"
2314 HOST_LONG_LONG_FORMAT
"d\n", G
.stats
.total_overhead_under128
);
2315 fprintf (stderr
, "Total Allocated under 128B: %10"
2316 HOST_LONG_LONG_FORMAT
"d\n", G
.stats
.total_allocated_under128
);
2318 for (i
= 0; i
< NUM_ORDERS
; i
++)
2319 if (G
.stats
.total_allocated_per_order
[i
])
2321 fprintf (stderr
, "Total Overhead page size %9lu: %10"
2322 HOST_LONG_LONG_FORMAT
"d\n",
2323 (unsigned long) OBJECT_SIZE (i
),
2324 G
.stats
.total_overhead_per_order
[i
]);
2325 fprintf (stderr
, "Total Allocated page size %9lu: %10"
2326 HOST_LONG_LONG_FORMAT
"d\n",
2327 (unsigned long) OBJECT_SIZE (i
),
2328 G
.stats
.total_allocated_per_order
[i
]);
2333 struct ggc_pch_ondisk
2335 unsigned totals
[NUM_ORDERS
];
2340 struct ggc_pch_ondisk d
;
2341 uintptr_t base
[NUM_ORDERS
];
2342 size_t written
[NUM_ORDERS
];
2345 struct ggc_pch_data
*
2348 return XCNEW (struct ggc_pch_data
);
2352 ggc_pch_count_object (struct ggc_pch_data
*d
, void *x ATTRIBUTE_UNUSED
,
2353 size_t size
, bool is_string ATTRIBUTE_UNUSED
)
2357 if (size
< NUM_SIZE_LOOKUP
)
2358 order
= size_lookup
[size
];
2362 while (size
> OBJECT_SIZE (order
))
2366 d
->d
.totals
[order
]++;
2370 ggc_pch_total_size (struct ggc_pch_data
*d
)
2375 for (i
= 0; i
< NUM_ORDERS
; i
++)
2376 a
+= PAGE_ALIGN (d
->d
.totals
[i
] * OBJECT_SIZE (i
));
2381 ggc_pch_this_base (struct ggc_pch_data
*d
, void *base
)
2383 uintptr_t a
= (uintptr_t) base
;
2386 for (i
= 0; i
< NUM_ORDERS
; i
++)
2389 a
+= PAGE_ALIGN (d
->d
.totals
[i
] * OBJECT_SIZE (i
));
2395 ggc_pch_alloc_object (struct ggc_pch_data
*d
, void *x ATTRIBUTE_UNUSED
,
2396 size_t size
, bool is_string ATTRIBUTE_UNUSED
)
2401 if (size
< NUM_SIZE_LOOKUP
)
2402 order
= size_lookup
[size
];
2406 while (size
> OBJECT_SIZE (order
))
2410 result
= (char *) d
->base
[order
];
2411 d
->base
[order
] += OBJECT_SIZE (order
);
2416 ggc_pch_prepare_write (struct ggc_pch_data
*d ATTRIBUTE_UNUSED
,
2417 FILE *f ATTRIBUTE_UNUSED
)
2419 /* Nothing to do. */
2423 ggc_pch_write_object (struct ggc_pch_data
*d
,
2424 FILE *f
, void *x
, void *newx ATTRIBUTE_UNUSED
,
2425 size_t size
, bool is_string ATTRIBUTE_UNUSED
)
2428 static const char emptyBytes
[256] = { 0 };
2430 if (size
< NUM_SIZE_LOOKUP
)
2431 order
= size_lookup
[size
];
2435 while (size
> OBJECT_SIZE (order
))
2439 if (fwrite (x
, size
, 1, f
) != 1)
2440 fatal_error (input_location
, "can%'t write PCH file: %m");
2442 /* If SIZE is not the same as OBJECT_SIZE(order), then we need to pad the
2443 object out to OBJECT_SIZE(order). This happens for strings. */
2445 if (size
!= OBJECT_SIZE (order
))
2447 unsigned padding
= OBJECT_SIZE (order
) - size
;
2449 /* To speed small writes, we use a nulled-out array that's larger
2450 than most padding requests as the source for our null bytes. This
2451 permits us to do the padding with fwrite() rather than fseek(), and
2452 limits the chance the OS may try to flush any outstanding writes. */
2453 if (padding
<= sizeof (emptyBytes
))
2455 if (fwrite (emptyBytes
, 1, padding
, f
) != padding
)
2456 fatal_error (input_location
, "can%'t write PCH file");
2460 /* Larger than our buffer? Just default to fseek. */
2461 if (fseek (f
, padding
, SEEK_CUR
) != 0)
2462 fatal_error (input_location
, "can%'t write PCH file");
2466 d
->written
[order
]++;
2467 if (d
->written
[order
] == d
->d
.totals
[order
]
2468 && fseek (f
, ROUND_UP_VALUE (d
->d
.totals
[order
] * OBJECT_SIZE (order
),
2471 fatal_error (input_location
, "can%'t write PCH file: %m");
2475 ggc_pch_finish (struct ggc_pch_data
*d
, FILE *f
)
2477 if (fwrite (&d
->d
, sizeof (d
->d
), 1, f
) != 1)
2478 fatal_error (input_location
, "can%'t write PCH file: %m");
2482 /* Move the PCH PTE entries just added to the end of by_depth, to the
2486 move_ptes_to_front (int count_old_page_tables
, int count_new_page_tables
)
2490 /* First, we swap the new entries to the front of the varrays. */
2491 page_entry
**new_by_depth
;
2492 unsigned long **new_save_in_use
;
2494 new_by_depth
= XNEWVEC (page_entry
*, G
.by_depth_max
);
2495 new_save_in_use
= XNEWVEC (unsigned long *, G
.by_depth_max
);
2497 memcpy (&new_by_depth
[0],
2498 &G
.by_depth
[count_old_page_tables
],
2499 count_new_page_tables
* sizeof (void *));
2500 memcpy (&new_by_depth
[count_new_page_tables
],
2502 count_old_page_tables
* sizeof (void *));
2503 memcpy (&new_save_in_use
[0],
2504 &G
.save_in_use
[count_old_page_tables
],
2505 count_new_page_tables
* sizeof (void *));
2506 memcpy (&new_save_in_use
[count_new_page_tables
],
2508 count_old_page_tables
* sizeof (void *));
2511 free (G
.save_in_use
);
2513 G
.by_depth
= new_by_depth
;
2514 G
.save_in_use
= new_save_in_use
;
2516 /* Now update all the index_by_depth fields. */
2517 for (i
= G
.by_depth_in_use
; i
> 0; --i
)
2519 page_entry
*p
= G
.by_depth
[i
-1];
2520 p
->index_by_depth
= i
-1;
2523 /* And last, we update the depth pointers in G.depth. The first
2524 entry is already 0, and context 0 entries always start at index
2525 0, so there is nothing to update in the first slot. We need a
2526 second slot, only if we have old ptes, and if we do, they start
2527 at index count_new_page_tables. */
2528 if (count_old_page_tables
)
2529 push_depth (count_new_page_tables
);
2533 ggc_pch_read (FILE *f
, void *addr
)
2535 struct ggc_pch_ondisk d
;
2537 char *offs
= (char *) addr
;
2538 unsigned long count_old_page_tables
;
2539 unsigned long count_new_page_tables
;
2541 count_old_page_tables
= G
.by_depth_in_use
;
2543 /* We've just read in a PCH file. So, every object that used to be
2544 allocated is now free. */
2546 #ifdef ENABLE_GC_CHECKING
2549 /* Since we free all the allocated objects, the free list becomes
2550 useless. Validate it now, which will also clear it. */
2551 validate_free_objects ();
2553 /* No object read from a PCH file should ever be freed. So, set the
2554 context depth to 1, and set the depth of all the currently-allocated
2555 pages to be 1 too. PCH pages will have depth 0. */
2556 gcc_assert (!G
.context_depth
);
2557 G
.context_depth
= 1;
2558 for (i
= 0; i
< NUM_ORDERS
; i
++)
2561 for (p
= G
.pages
[i
]; p
!= NULL
; p
= p
->next
)
2562 p
->context_depth
= G
.context_depth
;
2565 /* Allocate the appropriate page-table entries for the pages read from
2567 if (fread (&d
, sizeof (d
), 1, f
) != 1)
2568 fatal_error (input_location
, "can%'t read PCH file: %m");
2570 for (i
= 0; i
< NUM_ORDERS
; i
++)
2572 struct page_entry
*entry
;
2578 if (d
.totals
[i
] == 0)
2581 bytes
= PAGE_ALIGN (d
.totals
[i
] * OBJECT_SIZE (i
));
2582 num_objs
= bytes
/ OBJECT_SIZE (i
);
2583 entry
= XCNEWVAR (struct page_entry
, (sizeof (struct page_entry
)
2585 + BITMAP_SIZE (num_objs
+ 1)));
2586 entry
->bytes
= bytes
;
2588 entry
->context_depth
= 0;
2590 entry
->num_free_objects
= 0;
2594 j
+ HOST_BITS_PER_LONG
<= num_objs
+ 1;
2595 j
+= HOST_BITS_PER_LONG
)
2596 entry
->in_use_p
[j
/ HOST_BITS_PER_LONG
] = -1;
2597 for (; j
< num_objs
+ 1; j
++)
2598 entry
->in_use_p
[j
/ HOST_BITS_PER_LONG
]
2599 |= 1L << (j
% HOST_BITS_PER_LONG
);
2601 for (pte
= entry
->page
;
2602 pte
< entry
->page
+ entry
->bytes
;
2604 set_page_table_entry (pte
, entry
);
2606 if (G
.page_tails
[i
] != NULL
)
2607 G
.page_tails
[i
]->next
= entry
;
2610 G
.page_tails
[i
] = entry
;
2612 /* We start off by just adding all the new information to the
2613 end of the varrays, later, we will move the new information
2614 to the front of the varrays, as the PCH page tables are at
2616 push_by_depth (entry
, 0);
2619 /* Now, we update the various data structures that speed page table
2621 count_new_page_tables
= G
.by_depth_in_use
- count_old_page_tables
;
2623 move_ptes_to_front (count_old_page_tables
, count_new_page_tables
);
2625 /* Update the statistics. */
2626 G
.allocated
= G
.allocated_last_gc
= offs
- (char *)addr
;