xref: /dragonfly/contrib/gcc-4.7/gcc/ggc-zone.c (revision e4b17023)
1*e4b17023SJohn Marino /* "Bag-of-pages" zone garbage collector for the GNU compiler.
2*e4b17023SJohn Marino    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008,
3*e4b17023SJohn Marino    2010 Free Software Foundation, Inc.
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino    Contributed by Richard Henderson (rth@redhat.com) and Daniel Berlin
6*e4b17023SJohn Marino    (dberlin@dberlin.org).  Rewritten by Daniel Jacobowitz
7*e4b17023SJohn Marino    <dan@codesourcery.com>.
8*e4b17023SJohn Marino 
9*e4b17023SJohn Marino This file is part of GCC.
10*e4b17023SJohn Marino 
11*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
12*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
13*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
14*e4b17023SJohn Marino version.
15*e4b17023SJohn Marino 
16*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
18*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19*e4b17023SJohn Marino for more details.
20*e4b17023SJohn Marino 
21*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
22*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
23*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
24*e4b17023SJohn Marino 
25*e4b17023SJohn Marino #include "config.h"
26*e4b17023SJohn Marino #include "system.h"
27*e4b17023SJohn Marino #include "coretypes.h"
28*e4b17023SJohn Marino #include "tm.h"
29*e4b17023SJohn Marino #include "tree.h"
30*e4b17023SJohn Marino #include "rtl.h"
31*e4b17023SJohn Marino #include "tm_p.h"
32*e4b17023SJohn Marino #include "diagnostic-core.h"
33*e4b17023SJohn Marino #include "flags.h"
34*e4b17023SJohn Marino #include "ggc.h"
35*e4b17023SJohn Marino #include "ggc-internal.h"
36*e4b17023SJohn Marino #include "timevar.h"
37*e4b17023SJohn Marino #include "params.h"
38*e4b17023SJohn Marino #include "bitmap.h"
39*e4b17023SJohn Marino #include "plugin.h"
40*e4b17023SJohn Marino 
41*e4b17023SJohn Marino /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
42*e4b17023SJohn Marino    file open.  Prefer either to valloc.  */
43*e4b17023SJohn Marino #ifdef HAVE_MMAP_ANON
44*e4b17023SJohn Marino # undef HAVE_MMAP_DEV_ZERO
45*e4b17023SJohn Marino # define USING_MMAP
46*e4b17023SJohn Marino #endif
47*e4b17023SJohn Marino 
48*e4b17023SJohn Marino #ifdef HAVE_MMAP_DEV_ZERO
49*e4b17023SJohn Marino # define USING_MMAP
50*e4b17023SJohn Marino #endif
51*e4b17023SJohn Marino 
52*e4b17023SJohn Marino #ifndef USING_MMAP
53*e4b17023SJohn Marino #error Zone collector requires mmap
54*e4b17023SJohn Marino #endif
55*e4b17023SJohn Marino 
56*e4b17023SJohn Marino #if (GCC_VERSION < 3001)
57*e4b17023SJohn Marino #define prefetch(X) ((void) X)
58*e4b17023SJohn Marino #define prefetchw(X) ((void) X)
59*e4b17023SJohn Marino #else
60*e4b17023SJohn Marino #define prefetch(X) __builtin_prefetch (X)
61*e4b17023SJohn Marino #define prefetchw(X) __builtin_prefetch (X, 1, 3)
62*e4b17023SJohn Marino #endif
63*e4b17023SJohn Marino 
64*e4b17023SJohn Marino /* FUTURE NOTES:
65*e4b17023SJohn Marino 
66*e4b17023SJohn Marino    If we track inter-zone pointers, we can mark single zones at a
67*e4b17023SJohn Marino    time.
68*e4b17023SJohn Marino 
69*e4b17023SJohn Marino    If we have a zone where we guarantee no inter-zone pointers, we
70*e4b17023SJohn Marino    could mark that zone separately.
71*e4b17023SJohn Marino 
72*e4b17023SJohn Marino    The garbage zone should not be marked, and we should return 1 in
73*e4b17023SJohn Marino    ggc_set_mark for any object in the garbage zone, which cuts off
74*e4b17023SJohn Marino    marking quickly.  */
75*e4b17023SJohn Marino 
76*e4b17023SJohn Marino /* Strategy:
77*e4b17023SJohn Marino 
78*e4b17023SJohn Marino    This garbage-collecting allocator segregates objects into zones.
79*e4b17023SJohn Marino    It also segregates objects into "large" and "small" bins.  Large
80*e4b17023SJohn Marino    objects are greater than page size.
81*e4b17023SJohn Marino 
82*e4b17023SJohn Marino    Pages for small objects are broken up into chunks.  The page has
83*e4b17023SJohn Marino    a bitmap which marks the start position of each chunk (whether
84*e4b17023SJohn Marino    allocated or free).  Free chunks are on one of the zone's free
85*e4b17023SJohn Marino    lists and contain a pointer to the next free chunk.  Chunks in
86*e4b17023SJohn Marino    most of the free lists have a fixed size determined by the
87*e4b17023SJohn Marino    free list.  Chunks in the "other" sized free list have their size
88*e4b17023SJohn Marino    stored right after their chain pointer.
89*e4b17023SJohn Marino 
90*e4b17023SJohn Marino    Empty pages (of all sizes) are kept on a single page cache list,
91*e4b17023SJohn Marino    and are considered first when new pages are required; they are
92*e4b17023SJohn Marino    deallocated at the start of the next collection if they haven't
93*e4b17023SJohn Marino    been recycled by then.  The free page list is currently per-zone.  */
94*e4b17023SJohn Marino 
95*e4b17023SJohn Marino /* Define GGC_DEBUG_LEVEL to print debugging information.
96*e4b17023SJohn Marino      0: No debugging output.
97*e4b17023SJohn Marino      1: GC statistics only.
98*e4b17023SJohn Marino      2: Page-entry allocations/deallocations as well.
99*e4b17023SJohn Marino      3: Object allocations as well.
100*e4b17023SJohn Marino      4: Object marks as well.  */
101*e4b17023SJohn Marino #define GGC_DEBUG_LEVEL (0)
102*e4b17023SJohn Marino 
103*e4b17023SJohn Marino #ifndef HOST_BITS_PER_PTR
104*e4b17023SJohn Marino #define HOST_BITS_PER_PTR  HOST_BITS_PER_LONG
105*e4b17023SJohn Marino #endif
106*e4b17023SJohn Marino 
107*e4b17023SJohn Marino /* This structure manages small free chunks.  The SIZE field is only
108*e4b17023SJohn Marino    initialized if the chunk is in the "other" sized free list.  Large
109*e4b17023SJohn Marino    chunks are allocated one at a time to their own page, and so don't
110*e4b17023SJohn Marino    come in here.  */
111*e4b17023SJohn Marino 
112*e4b17023SJohn Marino struct alloc_chunk {
113*e4b17023SJohn Marino   struct alloc_chunk *next_free;
114*e4b17023SJohn Marino   unsigned int size;
115*e4b17023SJohn Marino };
116*e4b17023SJohn Marino 
117*e4b17023SJohn Marino /* The size of the fixed-size portion of a small page descriptor.  */
118*e4b17023SJohn Marino #define PAGE_OVERHEAD   (offsetof (struct small_page_entry, alloc_bits))
119*e4b17023SJohn Marino 
120*e4b17023SJohn Marino /* The collector's idea of the page size.  This must be a power of two
121*e4b17023SJohn Marino    no larger than the system page size, because pages must be aligned
122*e4b17023SJohn Marino    to this amount and are tracked at this granularity in the page
123*e4b17023SJohn Marino    table.  We choose a size at compile time for efficiency.
124*e4b17023SJohn Marino 
125*e4b17023SJohn Marino    We could make a better guess at compile time if PAGE_SIZE is a
126*e4b17023SJohn Marino    constant in system headers, and PAGE_SHIFT is defined...  */
127*e4b17023SJohn Marino #define GGC_PAGE_SIZE	4096
128*e4b17023SJohn Marino #define GGC_PAGE_MASK	(GGC_PAGE_SIZE - 1)
129*e4b17023SJohn Marino #define GGC_PAGE_SHIFT	12
130*e4b17023SJohn Marino 
131*e4b17023SJohn Marino #if 0
132*e4b17023SJohn Marino /* Alternative definitions which use the runtime page size.  */
133*e4b17023SJohn Marino #define GGC_PAGE_SIZE	G.pagesize
134*e4b17023SJohn Marino #define GGC_PAGE_MASK	G.page_mask
135*e4b17023SJohn Marino #define GGC_PAGE_SHIFT	G.lg_pagesize
136*e4b17023SJohn Marino #endif
137*e4b17023SJohn Marino 
138*e4b17023SJohn Marino /* The size of a small page managed by the garbage collector.  This
139*e4b17023SJohn Marino    must currently be GGC_PAGE_SIZE, but with a few changes could
140*e4b17023SJohn Marino    be any multiple of it to reduce certain kinds of overhead.  */
141*e4b17023SJohn Marino #define SMALL_PAGE_SIZE GGC_PAGE_SIZE
142*e4b17023SJohn Marino 
143*e4b17023SJohn Marino /* Free bin information.  These numbers may be in need of re-tuning.
144*e4b17023SJohn Marino    In general, decreasing the number of free bins would seem to
145*e4b17023SJohn Marino    increase the time it takes to allocate... */
146*e4b17023SJohn Marino 
147*e4b17023SJohn Marino /* FIXME: We can't use anything but MAX_ALIGNMENT for the bin size
148*e4b17023SJohn Marino    today.  */
149*e4b17023SJohn Marino 
150*e4b17023SJohn Marino #define NUM_FREE_BINS		64
151*e4b17023SJohn Marino #define FREE_BIN_DELTA		MAX_ALIGNMENT
152*e4b17023SJohn Marino #define SIZE_BIN_DOWN(SIZE)	((SIZE) / FREE_BIN_DELTA)
153*e4b17023SJohn Marino 
154*e4b17023SJohn Marino /* Allocation and marking parameters.  */
155*e4b17023SJohn Marino 
156*e4b17023SJohn Marino /* The smallest allocatable unit to keep track of.  */
157*e4b17023SJohn Marino #define BYTES_PER_ALLOC_BIT	MAX_ALIGNMENT
158*e4b17023SJohn Marino 
159*e4b17023SJohn Marino /* The smallest markable unit.  If we require each allocated object
160*e4b17023SJohn Marino    to contain at least two allocatable units, we can use half as many
161*e4b17023SJohn Marino    bits for the mark bitmap.  But this adds considerable complexity
162*e4b17023SJohn Marino    to sweeping.  */
163*e4b17023SJohn Marino #define BYTES_PER_MARK_BIT	BYTES_PER_ALLOC_BIT
164*e4b17023SJohn Marino 
165*e4b17023SJohn Marino #define BYTES_PER_MARK_WORD	(8 * BYTES_PER_MARK_BIT * sizeof (mark_type))
166*e4b17023SJohn Marino 
167*e4b17023SJohn Marino /* We use this structure to determine the alignment required for
168*e4b17023SJohn Marino    allocations.
169*e4b17023SJohn Marino 
170*e4b17023SJohn Marino    There are several things wrong with this estimation of alignment.
171*e4b17023SJohn Marino 
172*e4b17023SJohn Marino    The maximum alignment for a structure is often less than the
173*e4b17023SJohn Marino    maximum alignment for a basic data type; for instance, on some
174*e4b17023SJohn Marino    targets long long must be aligned to sizeof (int) in a structure
175*e4b17023SJohn Marino    and sizeof (long long) in a variable.  i386-linux is one example;
176*e4b17023SJohn Marino    Darwin is another (sometimes, depending on the compiler in use).
177*e4b17023SJohn Marino 
178*e4b17023SJohn Marino    Also, long double is not included.  Nothing in GCC uses long
179*e4b17023SJohn Marino    double, so we assume that this is OK.  On powerpc-darwin, adding
180*e4b17023SJohn Marino    long double would bring the maximum alignment up to 16 bytes,
181*e4b17023SJohn Marino    and until we need long double (or to vectorize compiler operations)
182*e4b17023SJohn Marino    that's painfully wasteful.  This will need to change, some day.  */
183*e4b17023SJohn Marino 
184*e4b17023SJohn Marino struct max_alignment {
185*e4b17023SJohn Marino   char c;
186*e4b17023SJohn Marino   union {
187*e4b17023SJohn Marino     HOST_WIDEST_INT i;
188*e4b17023SJohn Marino     double d;
189*e4b17023SJohn Marino   } u;
190*e4b17023SJohn Marino };
191*e4b17023SJohn Marino 
192*e4b17023SJohn Marino /* The biggest alignment required.  */
193*e4b17023SJohn Marino 
194*e4b17023SJohn Marino #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
195*e4b17023SJohn Marino 
196*e4b17023SJohn Marino /* Compute the smallest multiple of F that is >= X.  */
197*e4b17023SJohn Marino 
198*e4b17023SJohn Marino #define ROUND_UP(x, f) (CEIL (x, f) * (f))
199*e4b17023SJohn Marino 
200*e4b17023SJohn Marino /* Types to use for the allocation and mark bitmaps.  It might be
201*e4b17023SJohn Marino    a good idea to add ffsl to libiberty and use unsigned long
202*e4b17023SJohn Marino    instead; that could speed us up where long is wider than int.  */
203*e4b17023SJohn Marino 
204*e4b17023SJohn Marino typedef unsigned int alloc_type;
205*e4b17023SJohn Marino typedef unsigned int mark_type;
206*e4b17023SJohn Marino #define alloc_ffs(x) ffs(x)
207*e4b17023SJohn Marino 
208*e4b17023SJohn Marino /* A page_entry records the status of an allocation page.  This is the
209*e4b17023SJohn Marino    common data between all three kinds of pages - small, large, and
210*e4b17023SJohn Marino    PCH.  */
211*e4b17023SJohn Marino typedef struct page_entry
212*e4b17023SJohn Marino {
213*e4b17023SJohn Marino   /* The address at which the memory is allocated.  */
214*e4b17023SJohn Marino   char *page;
215*e4b17023SJohn Marino 
216*e4b17023SJohn Marino   /* The zone that this page entry belongs to.  */
217*e4b17023SJohn Marino   struct alloc_zone *zone;
218*e4b17023SJohn Marino 
219*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
220*e4b17023SJohn Marino   /* How many collections we've survived.  */
221*e4b17023SJohn Marino   size_t survived;
222*e4b17023SJohn Marino #endif
223*e4b17023SJohn Marino 
224*e4b17023SJohn Marino   /* Does this page contain small objects, or one large object?  */
225*e4b17023SJohn Marino   bool large_p;
226*e4b17023SJohn Marino 
227*e4b17023SJohn Marino   /* Is this page part of the loaded PCH?  */
228*e4b17023SJohn Marino   bool pch_p;
229*e4b17023SJohn Marino } page_entry;
230*e4b17023SJohn Marino 
231*e4b17023SJohn Marino /* Additional data needed for small pages.  */
232*e4b17023SJohn Marino struct small_page_entry
233*e4b17023SJohn Marino {
234*e4b17023SJohn Marino   struct page_entry common;
235*e4b17023SJohn Marino 
236*e4b17023SJohn Marino   /* The next small page entry, or NULL if this is the last.  */
237*e4b17023SJohn Marino   struct small_page_entry *next;
238*e4b17023SJohn Marino 
239*e4b17023SJohn Marino   /* If currently marking this zone, a pointer to the mark bits
240*e4b17023SJohn Marino      for this page.  If we aren't currently marking this zone,
241*e4b17023SJohn Marino      this pointer may be stale (pointing to freed memory).  */
242*e4b17023SJohn Marino   mark_type *mark_bits;
243*e4b17023SJohn Marino 
244*e4b17023SJohn Marino   /* The allocation bitmap.  This array extends far enough to have
245*e4b17023SJohn Marino      one bit for every BYTES_PER_ALLOC_BIT bytes in the page.  */
246*e4b17023SJohn Marino   alloc_type alloc_bits[1];
247*e4b17023SJohn Marino };
248*e4b17023SJohn Marino 
249*e4b17023SJohn Marino /* Additional data needed for large pages.  */
250*e4b17023SJohn Marino struct large_page_entry
251*e4b17023SJohn Marino {
252*e4b17023SJohn Marino   struct page_entry common;
253*e4b17023SJohn Marino 
254*e4b17023SJohn Marino   /* The next large page entry, or NULL if this is the last.  */
255*e4b17023SJohn Marino   struct large_page_entry *next;
256*e4b17023SJohn Marino 
257*e4b17023SJohn Marino   /* The number of bytes allocated, not including the page entry.  */
258*e4b17023SJohn Marino   size_t bytes;
259*e4b17023SJohn Marino 
260*e4b17023SJohn Marino   /* The previous page in the list, so that we can unlink this one.  */
261*e4b17023SJohn Marino   struct large_page_entry *prev;
262*e4b17023SJohn Marino 
263*e4b17023SJohn Marino   /* During marking, is this object marked?  */
264*e4b17023SJohn Marino   bool mark_p;
265*e4b17023SJohn Marino };
266*e4b17023SJohn Marino 
267*e4b17023SJohn Marino /* A two-level tree is used to look up the page-entry for a given
268*e4b17023SJohn Marino    pointer.  Two chunks of the pointer's bits are extracted to index
269*e4b17023SJohn Marino    the first and second levels of the tree, as follows:
270*e4b17023SJohn Marino 
271*e4b17023SJohn Marino 				   HOST_PAGE_SIZE_BITS
272*e4b17023SJohn Marino 			   32		|      |
273*e4b17023SJohn Marino        msb +----------------+----+------+------+ lsb
274*e4b17023SJohn Marino 			    |    |      |
275*e4b17023SJohn Marino 			 PAGE_L1_BITS   |
276*e4b17023SJohn Marino 				 |      |
277*e4b17023SJohn Marino 			       PAGE_L2_BITS
278*e4b17023SJohn Marino 
279*e4b17023SJohn Marino    The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
280*e4b17023SJohn Marino    pages are aligned on system page boundaries.  The next most
281*e4b17023SJohn Marino    significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
282*e4b17023SJohn Marino    index values in the lookup table, respectively.
283*e4b17023SJohn Marino 
284*e4b17023SJohn Marino    For 32-bit architectures and the settings below, there are no
285*e4b17023SJohn Marino    leftover bits.  For architectures with wider pointers, the lookup
286*e4b17023SJohn Marino    tree points to a list of pages, which must be scanned to find the
287*e4b17023SJohn Marino    correct one.  */
288*e4b17023SJohn Marino 
289*e4b17023SJohn Marino #define PAGE_L1_BITS	(8)
290*e4b17023SJohn Marino #define PAGE_L2_BITS	(32 - PAGE_L1_BITS - GGC_PAGE_SHIFT)
291*e4b17023SJohn Marino #define PAGE_L1_SIZE	((size_t) 1 << PAGE_L1_BITS)
292*e4b17023SJohn Marino #define PAGE_L2_SIZE	((size_t) 1 << PAGE_L2_BITS)
293*e4b17023SJohn Marino 
294*e4b17023SJohn Marino #define LOOKUP_L1(p) \
295*e4b17023SJohn Marino   (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
296*e4b17023SJohn Marino 
297*e4b17023SJohn Marino #define LOOKUP_L2(p) \
298*e4b17023SJohn Marino   (((size_t) (p) >> GGC_PAGE_SHIFT) & ((1 << PAGE_L2_BITS) - 1))
299*e4b17023SJohn Marino 
300*e4b17023SJohn Marino #if HOST_BITS_PER_PTR <= 32
301*e4b17023SJohn Marino 
302*e4b17023SJohn Marino /* On 32-bit hosts, we use a two level page table, as pictured above.  */
303*e4b17023SJohn Marino typedef page_entry **page_table[PAGE_L1_SIZE];
304*e4b17023SJohn Marino 
305*e4b17023SJohn Marino #else
306*e4b17023SJohn Marino 
307*e4b17023SJohn Marino /* On 64-bit hosts, we use the same two level page tables plus a linked
308*e4b17023SJohn Marino    list that disambiguates the top 32-bits.  There will almost always be
309*e4b17023SJohn Marino    exactly one entry in the list.  */
310*e4b17023SJohn Marino typedef struct page_table_chain
311*e4b17023SJohn Marino {
312*e4b17023SJohn Marino   struct page_table_chain *next;
313*e4b17023SJohn Marino   size_t high_bits;
314*e4b17023SJohn Marino   page_entry **table[PAGE_L1_SIZE];
315*e4b17023SJohn Marino } *page_table;
316*e4b17023SJohn Marino 
317*e4b17023SJohn Marino #endif
318*e4b17023SJohn Marino 
319*e4b17023SJohn Marino /* The global variables.  */
320*e4b17023SJohn Marino static struct globals
321*e4b17023SJohn Marino {
322*e4b17023SJohn Marino   /* The linked list of zones.  */
323*e4b17023SJohn Marino   struct alloc_zone *zones;
324*e4b17023SJohn Marino 
325*e4b17023SJohn Marino   /* Lookup table for associating allocation pages with object addresses.  */
326*e4b17023SJohn Marino   page_table lookup;
327*e4b17023SJohn Marino 
328*e4b17023SJohn Marino   /* The system's page size, and related constants.  */
329*e4b17023SJohn Marino   size_t pagesize;
330*e4b17023SJohn Marino   size_t lg_pagesize;
331*e4b17023SJohn Marino   size_t page_mask;
332*e4b17023SJohn Marino 
333*e4b17023SJohn Marino   /* The size to allocate for a small page entry.  This includes
334*e4b17023SJohn Marino      the size of the structure and the size of the allocation
335*e4b17023SJohn Marino      bitmap.  */
336*e4b17023SJohn Marino   size_t small_page_overhead;
337*e4b17023SJohn Marino 
338*e4b17023SJohn Marino #if defined (HAVE_MMAP_DEV_ZERO)
339*e4b17023SJohn Marino   /* A file descriptor open to /dev/zero for reading.  */
340*e4b17023SJohn Marino   int dev_zero_fd;
341*e4b17023SJohn Marino #endif
342*e4b17023SJohn Marino 
343*e4b17023SJohn Marino   /* Allocate pages in chunks of this size, to throttle calls to memory
344*e4b17023SJohn Marino      allocation routines.  The first page is used, the rest go onto the
345*e4b17023SJohn Marino      free list.  */
346*e4b17023SJohn Marino   size_t quire_size;
347*e4b17023SJohn Marino 
348*e4b17023SJohn Marino   /* The file descriptor for debugging output.  */
349*e4b17023SJohn Marino   FILE *debug_file;
350*e4b17023SJohn Marino } G;
351*e4b17023SJohn Marino 
352*e4b17023SJohn Marino /* A zone allocation structure.  There is one of these for every
353*e4b17023SJohn Marino    distinct allocation zone.  */
354*e4b17023SJohn Marino struct alloc_zone
355*e4b17023SJohn Marino {
356*e4b17023SJohn Marino   /* The most recent free chunk is saved here, instead of in the linked
357*e4b17023SJohn Marino      free list, to decrease list manipulation.  It is most likely that we
358*e4b17023SJohn Marino      will want this one.  */
359*e4b17023SJohn Marino   char *cached_free;
360*e4b17023SJohn Marino   size_t cached_free_size;
361*e4b17023SJohn Marino 
362*e4b17023SJohn Marino   /* Linked lists of free storage.  Slots 1 ... NUM_FREE_BINS have chunks of size
363*e4b17023SJohn Marino      FREE_BIN_DELTA.  All other chunks are in slot 0.  */
364*e4b17023SJohn Marino   struct alloc_chunk *free_chunks[NUM_FREE_BINS + 1];
365*e4b17023SJohn Marino 
366*e4b17023SJohn Marino   /* The highest bin index which might be non-empty.  It may turn out
367*e4b17023SJohn Marino      to be empty, in which case we have to search downwards.  */
368*e4b17023SJohn Marino   size_t high_free_bin;
369*e4b17023SJohn Marino 
370*e4b17023SJohn Marino   /* Bytes currently allocated in this zone.  */
371*e4b17023SJohn Marino   size_t allocated;
372*e4b17023SJohn Marino 
373*e4b17023SJohn Marino   /* Linked list of the small pages in this zone.  */
374*e4b17023SJohn Marino   struct small_page_entry *pages;
375*e4b17023SJohn Marino 
376*e4b17023SJohn Marino   /* Doubly linked list of large pages in this zone.  */
377*e4b17023SJohn Marino   struct large_page_entry *large_pages;
378*e4b17023SJohn Marino 
379*e4b17023SJohn Marino   /* If we are currently marking this zone, a pointer to the mark bits.  */
380*e4b17023SJohn Marino   mark_type *mark_bits;
381*e4b17023SJohn Marino 
382*e4b17023SJohn Marino   /* Name of the zone.  */
383*e4b17023SJohn Marino   const char *name;
384*e4b17023SJohn Marino 
385*e4b17023SJohn Marino   /* The number of small pages currently allocated in this zone.  */
386*e4b17023SJohn Marino   size_t n_small_pages;
387*e4b17023SJohn Marino 
388*e4b17023SJohn Marino   /* Bytes allocated at the end of the last collection.  */
389*e4b17023SJohn Marino   size_t allocated_last_gc;
390*e4b17023SJohn Marino 
391*e4b17023SJohn Marino   /* Total amount of memory mapped.  */
392*e4b17023SJohn Marino   size_t bytes_mapped;
393*e4b17023SJohn Marino 
394*e4b17023SJohn Marino   /* A cache of free system pages.  */
395*e4b17023SJohn Marino   struct small_page_entry *free_pages;
396*e4b17023SJohn Marino 
397*e4b17023SJohn Marino   /* Next zone in the linked list of zones.  */
398*e4b17023SJohn Marino   struct alloc_zone *next_zone;
399*e4b17023SJohn Marino 
400*e4b17023SJohn Marino   /* True if this zone was collected during this collection.  */
401*e4b17023SJohn Marino   bool was_collected;
402*e4b17023SJohn Marino 
403*e4b17023SJohn Marino   /* True if this zone should be destroyed after the next collection.  */
404*e4b17023SJohn Marino   bool dead;
405*e4b17023SJohn Marino 
406*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
407*e4b17023SJohn Marino   struct
408*e4b17023SJohn Marino   {
409*e4b17023SJohn Marino     /* Total GC-allocated memory.  */
410*e4b17023SJohn Marino     unsigned long long total_allocated;
411*e4b17023SJohn Marino     /* Total overhead for GC-allocated memory.  */
412*e4b17023SJohn Marino     unsigned long long total_overhead;
413*e4b17023SJohn Marino 
414*e4b17023SJohn Marino     /* Total allocations and overhead for sizes less than 32, 64 and 128.
415*e4b17023SJohn Marino        These sizes are interesting because they are typical cache line
416*e4b17023SJohn Marino        sizes.  */
417*e4b17023SJohn Marino 
418*e4b17023SJohn Marino     unsigned long long total_allocated_under32;
419*e4b17023SJohn Marino     unsigned long long total_overhead_under32;
420*e4b17023SJohn Marino 
421*e4b17023SJohn Marino     unsigned long long total_allocated_under64;
422*e4b17023SJohn Marino     unsigned long long total_overhead_under64;
423*e4b17023SJohn Marino 
424*e4b17023SJohn Marino     unsigned long long total_allocated_under128;
425*e4b17023SJohn Marino     unsigned long long total_overhead_under128;
426*e4b17023SJohn Marino   } stats;
427*e4b17023SJohn Marino #endif
428*e4b17023SJohn Marino } main_zone;
429*e4b17023SJohn Marino 
430*e4b17023SJohn Marino /* Some default zones.  */
431*e4b17023SJohn Marino struct alloc_zone rtl_zone;
432*e4b17023SJohn Marino struct alloc_zone tree_zone;
433*e4b17023SJohn Marino struct alloc_zone tree_id_zone;
434*e4b17023SJohn Marino 
435*e4b17023SJohn Marino /* The PCH zone does not need a normal zone structure, and it does
436*e4b17023SJohn Marino    not live on the linked list of zones.  */
437*e4b17023SJohn Marino struct pch_zone
438*e4b17023SJohn Marino {
439*e4b17023SJohn Marino   /* The start of the PCH zone.  NULL if there is none.  */
440*e4b17023SJohn Marino   char *page;
441*e4b17023SJohn Marino 
442*e4b17023SJohn Marino   /* The end of the PCH zone.  NULL if there is none.  */
443*e4b17023SJohn Marino   char *end;
444*e4b17023SJohn Marino 
445*e4b17023SJohn Marino   /* The size of the PCH zone.  0 if there is none.  */
446*e4b17023SJohn Marino   size_t bytes;
447*e4b17023SJohn Marino 
448*e4b17023SJohn Marino   /* The allocation bitmap for the PCH zone.  */
449*e4b17023SJohn Marino   alloc_type *alloc_bits;
450*e4b17023SJohn Marino 
451*e4b17023SJohn Marino   /* If we are currently marking, the mark bitmap for the PCH zone.
452*e4b17023SJohn Marino      When it is first read in, we could avoid marking the PCH,
453*e4b17023SJohn Marino      because it will not contain any pointers to GC memory outside
454*e4b17023SJohn Marino      of the PCH; however, the PCH is currently mapped as writable,
455*e4b17023SJohn Marino      so we must mark it in case new pointers are added.  */
456*e4b17023SJohn Marino   mark_type *mark_bits;
457*e4b17023SJohn Marino } pch_zone;
458*e4b17023SJohn Marino 
459*e4b17023SJohn Marino #ifdef USING_MMAP
460*e4b17023SJohn Marino static char *alloc_anon (char *, size_t, struct alloc_zone *);
461*e4b17023SJohn Marino #endif
462*e4b17023SJohn Marino static struct small_page_entry * alloc_small_page (struct alloc_zone *);
463*e4b17023SJohn Marino static struct large_page_entry * alloc_large_page (size_t, struct alloc_zone *);
464*e4b17023SJohn Marino static void free_chunk (char *, size_t, struct alloc_zone *);
465*e4b17023SJohn Marino static void free_small_page (struct small_page_entry *);
466*e4b17023SJohn Marino static void free_large_page (struct large_page_entry *);
467*e4b17023SJohn Marino static void release_pages (struct alloc_zone *);
468*e4b17023SJohn Marino static void sweep_pages (struct alloc_zone *);
469*e4b17023SJohn Marino static bool ggc_collect_1 (struct alloc_zone *, bool);
470*e4b17023SJohn Marino static void new_ggc_zone_1 (struct alloc_zone *, const char *);
471*e4b17023SJohn Marino 
472*e4b17023SJohn Marino /* Traverse the page table and find the entry for a page.
473*e4b17023SJohn Marino    Die (probably) if the object wasn't allocated via GC.  */
474*e4b17023SJohn Marino 
475*e4b17023SJohn Marino static inline page_entry *
lookup_page_table_entry(const void * p)476*e4b17023SJohn Marino lookup_page_table_entry (const void *p)
477*e4b17023SJohn Marino {
478*e4b17023SJohn Marino   page_entry ***base;
479*e4b17023SJohn Marino   size_t L1, L2;
480*e4b17023SJohn Marino 
481*e4b17023SJohn Marino #if HOST_BITS_PER_PTR <= 32
482*e4b17023SJohn Marino   base = &G.lookup[0];
483*e4b17023SJohn Marino #else
484*e4b17023SJohn Marino   page_table table = G.lookup;
485*e4b17023SJohn Marino   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
486*e4b17023SJohn Marino   while (table->high_bits != high_bits)
487*e4b17023SJohn Marino     table = table->next;
488*e4b17023SJohn Marino   base = &table->table[0];
489*e4b17023SJohn Marino #endif
490*e4b17023SJohn Marino 
491*e4b17023SJohn Marino   /* Extract the level 1 and 2 indices.  */
492*e4b17023SJohn Marino   L1 = LOOKUP_L1 (p);
493*e4b17023SJohn Marino   L2 = LOOKUP_L2 (p);
494*e4b17023SJohn Marino 
495*e4b17023SJohn Marino   return base[L1][L2];
496*e4b17023SJohn Marino }
497*e4b17023SJohn Marino 
498*e4b17023SJohn Marino /* Traverse the page table and find the entry for a page.
499*e4b17023SJohn Marino    Return NULL if the object wasn't allocated via the GC.  */
500*e4b17023SJohn Marino 
501*e4b17023SJohn Marino static inline page_entry *
lookup_page_table_if_allocated(const void * p)502*e4b17023SJohn Marino lookup_page_table_if_allocated (const void *p)
503*e4b17023SJohn Marino {
504*e4b17023SJohn Marino   page_entry ***base;
505*e4b17023SJohn Marino   size_t L1, L2;
506*e4b17023SJohn Marino 
507*e4b17023SJohn Marino #if HOST_BITS_PER_PTR <= 32
508*e4b17023SJohn Marino   base = &G.lookup[0];
509*e4b17023SJohn Marino #else
510*e4b17023SJohn Marino   page_table table = G.lookup;
511*e4b17023SJohn Marino   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
512*e4b17023SJohn Marino   while (1)
513*e4b17023SJohn Marino     {
514*e4b17023SJohn Marino       if (table == NULL)
515*e4b17023SJohn Marino 	return NULL;
516*e4b17023SJohn Marino       if (table->high_bits == high_bits)
517*e4b17023SJohn Marino 	break;
518*e4b17023SJohn Marino       table = table->next;
519*e4b17023SJohn Marino     }
520*e4b17023SJohn Marino   base = &table->table[0];
521*e4b17023SJohn Marino #endif
522*e4b17023SJohn Marino 
523*e4b17023SJohn Marino   /* Extract the level 1 and 2 indices.  */
524*e4b17023SJohn Marino   L1 = LOOKUP_L1 (p);
525*e4b17023SJohn Marino   if (! base[L1])
526*e4b17023SJohn Marino     return NULL;
527*e4b17023SJohn Marino 
528*e4b17023SJohn Marino   L2 = LOOKUP_L2 (p);
529*e4b17023SJohn Marino   if (L2 >= PAGE_L2_SIZE)
530*e4b17023SJohn Marino     return NULL;
531*e4b17023SJohn Marino   /* We might have a page entry which does not correspond exactly to a
532*e4b17023SJohn Marino      system page.  */
533*e4b17023SJohn Marino   if (base[L1][L2] && (const char *) p < base[L1][L2]->page)
534*e4b17023SJohn Marino     return NULL;
535*e4b17023SJohn Marino 
536*e4b17023SJohn Marino   return base[L1][L2];
537*e4b17023SJohn Marino }
538*e4b17023SJohn Marino 
539*e4b17023SJohn Marino /* Set the page table entry for the page that starts at P.  If ENTRY
540*e4b17023SJohn Marino    is NULL, clear the entry.  */
541*e4b17023SJohn Marino 
542*e4b17023SJohn Marino static void
set_page_table_entry(void * p,page_entry * entry)543*e4b17023SJohn Marino set_page_table_entry (void *p, page_entry *entry)
544*e4b17023SJohn Marino {
545*e4b17023SJohn Marino   page_entry ***base;
546*e4b17023SJohn Marino   size_t L1, L2;
547*e4b17023SJohn Marino 
548*e4b17023SJohn Marino #if HOST_BITS_PER_PTR <= 32
549*e4b17023SJohn Marino   base = &G.lookup[0];
550*e4b17023SJohn Marino #else
551*e4b17023SJohn Marino   page_table table;
552*e4b17023SJohn Marino   size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
553*e4b17023SJohn Marino   for (table = G.lookup; table; table = table->next)
554*e4b17023SJohn Marino     if (table->high_bits == high_bits)
555*e4b17023SJohn Marino       goto found;
556*e4b17023SJohn Marino 
557*e4b17023SJohn Marino   /* Not found -- allocate a new table.  */
558*e4b17023SJohn Marino   table = XCNEW (struct page_table_chain);
559*e4b17023SJohn Marino   table->next = G.lookup;
560*e4b17023SJohn Marino   table->high_bits = high_bits;
561*e4b17023SJohn Marino   G.lookup = table;
562*e4b17023SJohn Marino found:
563*e4b17023SJohn Marino   base = &table->table[0];
564*e4b17023SJohn Marino #endif
565*e4b17023SJohn Marino 
566*e4b17023SJohn Marino   /* Extract the level 1 and 2 indices.  */
567*e4b17023SJohn Marino   L1 = LOOKUP_L1 (p);
568*e4b17023SJohn Marino   L2 = LOOKUP_L2 (p);
569*e4b17023SJohn Marino 
570*e4b17023SJohn Marino   if (base[L1] == NULL)
571*e4b17023SJohn Marino     base[L1] = XCNEWVEC (page_entry *, PAGE_L2_SIZE);
572*e4b17023SJohn Marino 
573*e4b17023SJohn Marino   base[L1][L2] = entry;
574*e4b17023SJohn Marino }
575*e4b17023SJohn Marino 
576*e4b17023SJohn Marino /* Find the page table entry associated with OBJECT.  */
577*e4b17023SJohn Marino 
578*e4b17023SJohn Marino static inline struct page_entry *
zone_get_object_page(const void * object)579*e4b17023SJohn Marino zone_get_object_page (const void *object)
580*e4b17023SJohn Marino {
581*e4b17023SJohn Marino   return lookup_page_table_entry (object);
582*e4b17023SJohn Marino }
583*e4b17023SJohn Marino 
584*e4b17023SJohn Marino /* Find which element of the alloc_bits array OBJECT should be
585*e4b17023SJohn Marino    recorded in.  */
586*e4b17023SJohn Marino static inline unsigned int
zone_get_object_alloc_word(const void * object)587*e4b17023SJohn Marino zone_get_object_alloc_word (const void *object)
588*e4b17023SJohn Marino {
589*e4b17023SJohn Marino   return (((size_t) object & (GGC_PAGE_SIZE - 1))
590*e4b17023SJohn Marino 	  / (8 * sizeof (alloc_type) * BYTES_PER_ALLOC_BIT));
591*e4b17023SJohn Marino }
592*e4b17023SJohn Marino 
593*e4b17023SJohn Marino /* Find which bit of the appropriate word in the alloc_bits array
594*e4b17023SJohn Marino    OBJECT should be recorded in.  */
595*e4b17023SJohn Marino static inline unsigned int
zone_get_object_alloc_bit(const void * object)596*e4b17023SJohn Marino zone_get_object_alloc_bit (const void *object)
597*e4b17023SJohn Marino {
598*e4b17023SJohn Marino   return (((size_t) object / BYTES_PER_ALLOC_BIT)
599*e4b17023SJohn Marino 	  % (8 * sizeof (alloc_type)));
600*e4b17023SJohn Marino }
601*e4b17023SJohn Marino 
602*e4b17023SJohn Marino /* Find which element of the mark_bits array OBJECT should be recorded
603*e4b17023SJohn Marino    in.  */
604*e4b17023SJohn Marino static inline unsigned int
zone_get_object_mark_word(const void * object)605*e4b17023SJohn Marino zone_get_object_mark_word (const void *object)
606*e4b17023SJohn Marino {
607*e4b17023SJohn Marino   return (((size_t) object & (GGC_PAGE_SIZE - 1))
608*e4b17023SJohn Marino 	  / (8 * sizeof (mark_type) * BYTES_PER_MARK_BIT));
609*e4b17023SJohn Marino }
610*e4b17023SJohn Marino 
611*e4b17023SJohn Marino /* Find which bit of the appropriate word in the mark_bits array
612*e4b17023SJohn Marino    OBJECT should be recorded in.  */
613*e4b17023SJohn Marino static inline unsigned int
zone_get_object_mark_bit(const void * object)614*e4b17023SJohn Marino zone_get_object_mark_bit (const void *object)
615*e4b17023SJohn Marino {
616*e4b17023SJohn Marino   return (((size_t) object / BYTES_PER_MARK_BIT)
617*e4b17023SJohn Marino 	  % (8 * sizeof (mark_type)));
618*e4b17023SJohn Marino }
619*e4b17023SJohn Marino 
620*e4b17023SJohn Marino /* Set the allocation bit corresponding to OBJECT in its page's
621*e4b17023SJohn Marino    bitmap.  Used to split this object from the preceding one.  */
622*e4b17023SJohn Marino static inline void
zone_set_object_alloc_bit(const void * object)623*e4b17023SJohn Marino zone_set_object_alloc_bit (const void *object)
624*e4b17023SJohn Marino {
625*e4b17023SJohn Marino   struct small_page_entry *page
626*e4b17023SJohn Marino     = (struct small_page_entry *) zone_get_object_page (object);
627*e4b17023SJohn Marino   unsigned int start_word = zone_get_object_alloc_word (object);
628*e4b17023SJohn Marino   unsigned int start_bit = zone_get_object_alloc_bit (object);
629*e4b17023SJohn Marino 
630*e4b17023SJohn Marino   page->alloc_bits[start_word] |= 1L << start_bit;
631*e4b17023SJohn Marino }
632*e4b17023SJohn Marino 
633*e4b17023SJohn Marino /* Clear the allocation bit corresponding to OBJECT in PAGE's
634*e4b17023SJohn Marino    bitmap.  Used to coalesce this object with the preceding
635*e4b17023SJohn Marino    one.  */
636*e4b17023SJohn Marino static inline void
zone_clear_object_alloc_bit(struct small_page_entry * page,const void * object)637*e4b17023SJohn Marino zone_clear_object_alloc_bit (struct small_page_entry *page,
638*e4b17023SJohn Marino 			     const void *object)
639*e4b17023SJohn Marino {
640*e4b17023SJohn Marino   unsigned int start_word = zone_get_object_alloc_word (object);
641*e4b17023SJohn Marino   unsigned int start_bit = zone_get_object_alloc_bit (object);
642*e4b17023SJohn Marino 
643*e4b17023SJohn Marino   /* Would xor be quicker?  */
644*e4b17023SJohn Marino   page->alloc_bits[start_word] &= ~(1L << start_bit);
645*e4b17023SJohn Marino }
646*e4b17023SJohn Marino 
647*e4b17023SJohn Marino /* Find the size of the object which starts at START_WORD and
648*e4b17023SJohn Marino    START_BIT in ALLOC_BITS, which is at most MAX_SIZE bytes.
649*e4b17023SJohn Marino    Helper function for ggc_get_size and zone_find_object_size.  */
650*e4b17023SJohn Marino 
651*e4b17023SJohn Marino static inline size_t
zone_object_size_1(alloc_type * alloc_bits,size_t start_word,size_t start_bit,size_t max_size)652*e4b17023SJohn Marino zone_object_size_1 (alloc_type *alloc_bits,
653*e4b17023SJohn Marino 		    size_t start_word, size_t start_bit,
654*e4b17023SJohn Marino 		    size_t max_size)
655*e4b17023SJohn Marino {
656*e4b17023SJohn Marino   size_t size;
657*e4b17023SJohn Marino   alloc_type alloc_word;
658*e4b17023SJohn Marino   int indx;
659*e4b17023SJohn Marino 
660*e4b17023SJohn Marino   /* Load the first word.  */
661*e4b17023SJohn Marino   alloc_word = alloc_bits[start_word++];
662*e4b17023SJohn Marino 
663*e4b17023SJohn Marino   /* If that was the last bit in this word, we'll want to continue
664*e4b17023SJohn Marino      with the next word.  Otherwise, handle the rest of this word.  */
665*e4b17023SJohn Marino   if (start_bit)
666*e4b17023SJohn Marino     {
667*e4b17023SJohn Marino       indx = alloc_ffs (alloc_word >> start_bit);
668*e4b17023SJohn Marino       if (indx)
669*e4b17023SJohn Marino 	/* indx is 1-based.  We started at the bit after the object's
670*e4b17023SJohn Marino 	   start, but we also ended at the bit after the object's end.
671*e4b17023SJohn Marino 	   It cancels out.  */
672*e4b17023SJohn Marino 	return indx * BYTES_PER_ALLOC_BIT;
673*e4b17023SJohn Marino 
674*e4b17023SJohn Marino       /* The extra 1 accounts for the starting unit, before start_bit.  */
675*e4b17023SJohn Marino       size = (sizeof (alloc_type) * 8 - start_bit + 1) * BYTES_PER_ALLOC_BIT;
676*e4b17023SJohn Marino 
677*e4b17023SJohn Marino       if (size >= max_size)
678*e4b17023SJohn Marino 	return max_size;
679*e4b17023SJohn Marino 
680*e4b17023SJohn Marino       alloc_word = alloc_bits[start_word++];
681*e4b17023SJohn Marino     }
682*e4b17023SJohn Marino   else
683*e4b17023SJohn Marino     size = BYTES_PER_ALLOC_BIT;
684*e4b17023SJohn Marino 
685*e4b17023SJohn Marino   while (alloc_word == 0)
686*e4b17023SJohn Marino     {
687*e4b17023SJohn Marino       size += sizeof (alloc_type) * 8 * BYTES_PER_ALLOC_BIT;
688*e4b17023SJohn Marino       if (size >= max_size)
689*e4b17023SJohn Marino 	return max_size;
690*e4b17023SJohn Marino       alloc_word = alloc_bits[start_word++];
691*e4b17023SJohn Marino     }
692*e4b17023SJohn Marino 
693*e4b17023SJohn Marino   indx = alloc_ffs (alloc_word);
694*e4b17023SJohn Marino   return size + (indx - 1) * BYTES_PER_ALLOC_BIT;
695*e4b17023SJohn Marino }
696*e4b17023SJohn Marino 
697*e4b17023SJohn Marino /* Find the size of OBJECT on small page PAGE.  */
698*e4b17023SJohn Marino 
699*e4b17023SJohn Marino static inline size_t
zone_find_object_size(struct small_page_entry * page,const void * object)700*e4b17023SJohn Marino zone_find_object_size (struct small_page_entry *page,
701*e4b17023SJohn Marino 		       const void *object)
702*e4b17023SJohn Marino {
703*e4b17023SJohn Marino   const char *object_midptr = (const char *) object + BYTES_PER_ALLOC_BIT;
704*e4b17023SJohn Marino   unsigned int start_word = zone_get_object_alloc_word (object_midptr);
705*e4b17023SJohn Marino   unsigned int start_bit = zone_get_object_alloc_bit (object_midptr);
706*e4b17023SJohn Marino   size_t max_size = (page->common.page + SMALL_PAGE_SIZE
707*e4b17023SJohn Marino 		     - (const char *) object);
708*e4b17023SJohn Marino 
709*e4b17023SJohn Marino   return zone_object_size_1 (page->alloc_bits, start_word, start_bit,
710*e4b17023SJohn Marino 			     max_size);
711*e4b17023SJohn Marino }
712*e4b17023SJohn Marino 
713*e4b17023SJohn Marino /* highest_bit assumes that alloc_type is 32 bits.  */
714*e4b17023SJohn Marino extern char check_alloc_type_size[(sizeof (alloc_type) == 4) ? 1 : -1];
715*e4b17023SJohn Marino 
716*e4b17023SJohn Marino /* Find the highest set bit in VALUE.  Returns the bit number of that
717*e4b17023SJohn Marino    bit, using the same values as ffs.  */
718*e4b17023SJohn Marino static inline alloc_type
highest_bit(alloc_type value)719*e4b17023SJohn Marino highest_bit (alloc_type value)
720*e4b17023SJohn Marino {
721*e4b17023SJohn Marino   /* This also assumes that alloc_type is unsigned.  */
722*e4b17023SJohn Marino   value |= value >> 1;
723*e4b17023SJohn Marino   value |= value >> 2;
724*e4b17023SJohn Marino   value |= value >> 4;
725*e4b17023SJohn Marino   value |= value >> 8;
726*e4b17023SJohn Marino   value |= value >> 16;
727*e4b17023SJohn Marino   value = value ^ (value >> 1);
728*e4b17023SJohn Marino   return alloc_ffs (value);
729*e4b17023SJohn Marino }
730*e4b17023SJohn Marino 
731*e4b17023SJohn Marino /* Find the offset from the start of an object to P, which may point
732*e4b17023SJohn Marino    into the interior of the object.  */
733*e4b17023SJohn Marino 
734*e4b17023SJohn Marino static unsigned long
zone_find_object_offset(alloc_type * alloc_bits,size_t start_word,size_t start_bit)735*e4b17023SJohn Marino zone_find_object_offset (alloc_type *alloc_bits, size_t start_word,
736*e4b17023SJohn Marino 			 size_t start_bit)
737*e4b17023SJohn Marino {
738*e4b17023SJohn Marino   unsigned int offset_in_bits;
739*e4b17023SJohn Marino   alloc_type alloc_word = alloc_bits[start_word];
740*e4b17023SJohn Marino 
741*e4b17023SJohn Marino   /* Mask off any bits after the initial bit, but make sure to include
742*e4b17023SJohn Marino      the initial bit in the result.  Note that START_BIT is
743*e4b17023SJohn Marino      0-based.  */
744*e4b17023SJohn Marino   if (start_bit < 8 * sizeof (alloc_type) - 1)
745*e4b17023SJohn Marino     alloc_word &= (1 << (start_bit + 1)) - 1;
746*e4b17023SJohn Marino   offset_in_bits = start_bit;
747*e4b17023SJohn Marino 
748*e4b17023SJohn Marino   /* Search for the start of the object.  */
749*e4b17023SJohn Marino   while (alloc_word == 0 && start_word > 0)
750*e4b17023SJohn Marino     {
751*e4b17023SJohn Marino       alloc_word = alloc_bits[--start_word];
752*e4b17023SJohn Marino       offset_in_bits += 8 * sizeof (alloc_type);
753*e4b17023SJohn Marino     }
754*e4b17023SJohn Marino   /* We must always find a set bit.  */
755*e4b17023SJohn Marino   gcc_assert (alloc_word != 0);
756*e4b17023SJohn Marino   /* Note that the result of highest_bit is 1-based.  */
757*e4b17023SJohn Marino   offset_in_bits -= highest_bit (alloc_word) - 1;
758*e4b17023SJohn Marino 
759*e4b17023SJohn Marino   return BYTES_PER_ALLOC_BIT * offset_in_bits;
760*e4b17023SJohn Marino }
761*e4b17023SJohn Marino 
762*e4b17023SJohn Marino /* Allocate the mark bits for every zone, and set the pointers on each
763*e4b17023SJohn Marino    page.  */
764*e4b17023SJohn Marino static void
zone_allocate_marks(void)765*e4b17023SJohn Marino zone_allocate_marks (void)
766*e4b17023SJohn Marino {
767*e4b17023SJohn Marino   struct alloc_zone *zone;
768*e4b17023SJohn Marino 
769*e4b17023SJohn Marino   for (zone = G.zones; zone; zone = zone->next_zone)
770*e4b17023SJohn Marino     {
771*e4b17023SJohn Marino       struct small_page_entry *page;
772*e4b17023SJohn Marino       mark_type *cur_marks;
773*e4b17023SJohn Marino       size_t mark_words, mark_words_per_page;
774*e4b17023SJohn Marino #ifdef ENABLE_CHECKING
775*e4b17023SJohn Marino       size_t n = 0;
776*e4b17023SJohn Marino #endif
777*e4b17023SJohn Marino 
778*e4b17023SJohn Marino       mark_words_per_page
779*e4b17023SJohn Marino 	= (GGC_PAGE_SIZE + BYTES_PER_MARK_WORD - 1) / BYTES_PER_MARK_WORD;
780*e4b17023SJohn Marino       mark_words = zone->n_small_pages * mark_words_per_page;
781*e4b17023SJohn Marino       zone->mark_bits = (mark_type *) xcalloc (sizeof (mark_type),
782*e4b17023SJohn Marino 						   mark_words);
783*e4b17023SJohn Marino       cur_marks = zone->mark_bits;
784*e4b17023SJohn Marino       for (page = zone->pages; page; page = page->next)
785*e4b17023SJohn Marino 	{
786*e4b17023SJohn Marino 	  page->mark_bits = cur_marks;
787*e4b17023SJohn Marino 	  cur_marks += mark_words_per_page;
788*e4b17023SJohn Marino #ifdef ENABLE_CHECKING
789*e4b17023SJohn Marino 	  n++;
790*e4b17023SJohn Marino #endif
791*e4b17023SJohn Marino 	}
792*e4b17023SJohn Marino       gcc_checking_assert (n == zone->n_small_pages);
793*e4b17023SJohn Marino     }
794*e4b17023SJohn Marino 
795*e4b17023SJohn Marino   /* We don't collect the PCH zone, but we do have to mark it
796*e4b17023SJohn Marino      (for now).  */
797*e4b17023SJohn Marino   if (pch_zone.bytes)
798*e4b17023SJohn Marino     pch_zone.mark_bits
799*e4b17023SJohn Marino       = (mark_type *) xcalloc (sizeof (mark_type),
800*e4b17023SJohn Marino 			       CEIL (pch_zone.bytes, BYTES_PER_MARK_WORD));
801*e4b17023SJohn Marino }
802*e4b17023SJohn Marino 
803*e4b17023SJohn Marino /* After marking and sweeping, release the memory used for mark bits.  */
804*e4b17023SJohn Marino static void
zone_free_marks(void)805*e4b17023SJohn Marino zone_free_marks (void)
806*e4b17023SJohn Marino {
807*e4b17023SJohn Marino   struct alloc_zone *zone;
808*e4b17023SJohn Marino 
809*e4b17023SJohn Marino   for (zone = G.zones; zone; zone = zone->next_zone)
810*e4b17023SJohn Marino     if (zone->mark_bits)
811*e4b17023SJohn Marino       {
812*e4b17023SJohn Marino 	free (zone->mark_bits);
813*e4b17023SJohn Marino 	zone->mark_bits = NULL;
814*e4b17023SJohn Marino       }
815*e4b17023SJohn Marino 
816*e4b17023SJohn Marino   if (pch_zone.bytes)
817*e4b17023SJohn Marino     {
818*e4b17023SJohn Marino       free (pch_zone.mark_bits);
819*e4b17023SJohn Marino       pch_zone.mark_bits = NULL;
820*e4b17023SJohn Marino     }
821*e4b17023SJohn Marino }
822*e4b17023SJohn Marino 
823*e4b17023SJohn Marino #ifdef USING_MMAP
824*e4b17023SJohn Marino /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
825*e4b17023SJohn Marino    (if non-null).  The ifdef structure here is intended to cause a
826*e4b17023SJohn Marino    compile error unless exactly one of the HAVE_* is defined.  */
827*e4b17023SJohn Marino 
828*e4b17023SJohn Marino static inline char *
alloc_anon(char * pref ATTRIBUTE_UNUSED,size_t size,struct alloc_zone * zone)829*e4b17023SJohn Marino alloc_anon (char *pref ATTRIBUTE_UNUSED, size_t size, struct alloc_zone *zone)
830*e4b17023SJohn Marino {
831*e4b17023SJohn Marino #ifdef HAVE_MMAP_ANON
832*e4b17023SJohn Marino   char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
833*e4b17023SJohn Marino 			      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
834*e4b17023SJohn Marino #endif
835*e4b17023SJohn Marino #ifdef HAVE_MMAP_DEV_ZERO
836*e4b17023SJohn Marino   char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
837*e4b17023SJohn Marino 			      MAP_PRIVATE, G.dev_zero_fd, 0);
838*e4b17023SJohn Marino #endif
839*e4b17023SJohn Marino 
840*e4b17023SJohn Marino   if (page == (char *) MAP_FAILED)
841*e4b17023SJohn Marino     {
842*e4b17023SJohn Marino       perror ("virtual memory exhausted");
843*e4b17023SJohn Marino       exit (FATAL_EXIT_CODE);
844*e4b17023SJohn Marino     }
845*e4b17023SJohn Marino 
846*e4b17023SJohn Marino   /* Remember that we allocated this memory.  */
847*e4b17023SJohn Marino   zone->bytes_mapped += size;
848*e4b17023SJohn Marino 
849*e4b17023SJohn Marino   /* Pretend we don't have access to the allocated pages.  We'll enable
850*e4b17023SJohn Marino      access to smaller pieces of the area in ggc_internal_alloc.  Discard the
851*e4b17023SJohn Marino      handle to avoid handle leak.  */
852*e4b17023SJohn Marino   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (page, size));
853*e4b17023SJohn Marino 
854*e4b17023SJohn Marino   return page;
855*e4b17023SJohn Marino }
856*e4b17023SJohn Marino #endif
857*e4b17023SJohn Marino 
858*e4b17023SJohn Marino /* Allocate a new page for allocating small objects in ZONE, and
859*e4b17023SJohn Marino    return an entry for it.  */
860*e4b17023SJohn Marino 
861*e4b17023SJohn Marino static struct small_page_entry *
alloc_small_page(struct alloc_zone * zone)862*e4b17023SJohn Marino alloc_small_page (struct alloc_zone *zone)
863*e4b17023SJohn Marino {
864*e4b17023SJohn Marino   struct small_page_entry *entry;
865*e4b17023SJohn Marino 
866*e4b17023SJohn Marino   /* Check the list of free pages for one we can use.  */
867*e4b17023SJohn Marino   entry = zone->free_pages;
868*e4b17023SJohn Marino   if (entry != NULL)
869*e4b17023SJohn Marino     {
870*e4b17023SJohn Marino       /* Recycle the allocated memory from this page ...  */
871*e4b17023SJohn Marino       zone->free_pages = entry->next;
872*e4b17023SJohn Marino     }
873*e4b17023SJohn Marino   else
874*e4b17023SJohn Marino     {
875*e4b17023SJohn Marino       /* We want just one page.  Allocate a bunch of them and put the
876*e4b17023SJohn Marino 	 extras on the freelist.  (Can only do this optimization with
877*e4b17023SJohn Marino 	 mmap for backing store.)  */
878*e4b17023SJohn Marino       struct small_page_entry *e, *f = zone->free_pages;
879*e4b17023SJohn Marino       int i;
880*e4b17023SJohn Marino       char *page;
881*e4b17023SJohn Marino 
882*e4b17023SJohn Marino       page = alloc_anon (NULL, GGC_PAGE_SIZE * G.quire_size, zone);
883*e4b17023SJohn Marino 
884*e4b17023SJohn Marino       /* This loop counts down so that the chain will be in ascending
885*e4b17023SJohn Marino 	 memory order.  */
886*e4b17023SJohn Marino       for (i = G.quire_size - 1; i >= 1; i--)
887*e4b17023SJohn Marino 	{
888*e4b17023SJohn Marino 	  e = XCNEWVAR (struct small_page_entry, G.small_page_overhead);
889*e4b17023SJohn Marino 	  e->common.page = page + (i << GGC_PAGE_SHIFT);
890*e4b17023SJohn Marino 	  e->common.zone = zone;
891*e4b17023SJohn Marino 	  e->next = f;
892*e4b17023SJohn Marino 	  f = e;
893*e4b17023SJohn Marino 	  set_page_table_entry (e->common.page, &e->common);
894*e4b17023SJohn Marino 	}
895*e4b17023SJohn Marino 
896*e4b17023SJohn Marino       zone->free_pages = f;
897*e4b17023SJohn Marino 
898*e4b17023SJohn Marino       entry = XCNEWVAR (struct small_page_entry, G.small_page_overhead);
899*e4b17023SJohn Marino       entry->common.page = page;
900*e4b17023SJohn Marino       entry->common.zone = zone;
901*e4b17023SJohn Marino       set_page_table_entry (page, &entry->common);
902*e4b17023SJohn Marino     }
903*e4b17023SJohn Marino 
904*e4b17023SJohn Marino   zone->n_small_pages++;
905*e4b17023SJohn Marino 
906*e4b17023SJohn Marino   if (GGC_DEBUG_LEVEL >= 2)
907*e4b17023SJohn Marino     fprintf (G.debug_file,
908*e4b17023SJohn Marino 	     "Allocating %s page at %p, data %p-%p\n",
909*e4b17023SJohn Marino 	     entry->common.zone->name, (PTR) entry, entry->common.page,
910*e4b17023SJohn Marino 	     entry->common.page + SMALL_PAGE_SIZE - 1);
911*e4b17023SJohn Marino 
912*e4b17023SJohn Marino   return entry;
913*e4b17023SJohn Marino }
914*e4b17023SJohn Marino 
915*e4b17023SJohn Marino /* Allocate a large page of size SIZE in ZONE.  */
916*e4b17023SJohn Marino 
917*e4b17023SJohn Marino static struct large_page_entry *
alloc_large_page(size_t size,struct alloc_zone * zone)918*e4b17023SJohn Marino alloc_large_page (size_t size, struct alloc_zone *zone)
919*e4b17023SJohn Marino {
920*e4b17023SJohn Marino   struct large_page_entry *entry;
921*e4b17023SJohn Marino   char *page;
922*e4b17023SJohn Marino   size_t needed_size;
923*e4b17023SJohn Marino 
924*e4b17023SJohn Marino   needed_size = size + sizeof (struct large_page_entry);
925*e4b17023SJohn Marino   page = XNEWVAR (char, needed_size);
926*e4b17023SJohn Marino 
927*e4b17023SJohn Marino   entry = (struct large_page_entry *) page;
928*e4b17023SJohn Marino 
929*e4b17023SJohn Marino   entry->next = NULL;
930*e4b17023SJohn Marino   entry->common.page = page + sizeof (struct large_page_entry);
931*e4b17023SJohn Marino   entry->common.large_p = true;
932*e4b17023SJohn Marino   entry->common.pch_p = false;
933*e4b17023SJohn Marino   entry->common.zone = zone;
934*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
935*e4b17023SJohn Marino   entry->common.survived = 0;
936*e4b17023SJohn Marino #endif
937*e4b17023SJohn Marino   entry->mark_p = false;
938*e4b17023SJohn Marino   entry->bytes = size;
939*e4b17023SJohn Marino   entry->prev = NULL;
940*e4b17023SJohn Marino 
941*e4b17023SJohn Marino   set_page_table_entry (entry->common.page, &entry->common);
942*e4b17023SJohn Marino 
943*e4b17023SJohn Marino   if (GGC_DEBUG_LEVEL >= 2)
944*e4b17023SJohn Marino     fprintf (G.debug_file,
945*e4b17023SJohn Marino 	     "Allocating %s large page at %p, data %p-%p\n",
946*e4b17023SJohn Marino 	     entry->common.zone->name, (PTR) entry, entry->common.page,
947*e4b17023SJohn Marino 	     entry->common.page + SMALL_PAGE_SIZE - 1);
948*e4b17023SJohn Marino 
949*e4b17023SJohn Marino   return entry;
950*e4b17023SJohn Marino }
951*e4b17023SJohn Marino 
952*e4b17023SJohn Marino 
953*e4b17023SJohn Marino /* For a page that is no longer needed, put it on the free page list.  */
954*e4b17023SJohn Marino 
955*e4b17023SJohn Marino static inline void
free_small_page(struct small_page_entry * entry)956*e4b17023SJohn Marino free_small_page (struct small_page_entry *entry)
957*e4b17023SJohn Marino {
958*e4b17023SJohn Marino   if (GGC_DEBUG_LEVEL >= 2)
959*e4b17023SJohn Marino     fprintf (G.debug_file,
960*e4b17023SJohn Marino 	     "Deallocating %s page at %p, data %p-%p\n",
961*e4b17023SJohn Marino 	     entry->common.zone->name, (PTR) entry,
962*e4b17023SJohn Marino 	     entry->common.page, entry->common.page + SMALL_PAGE_SIZE - 1);
963*e4b17023SJohn Marino 
964*e4b17023SJohn Marino   gcc_assert (!entry->common.large_p);
965*e4b17023SJohn Marino 
966*e4b17023SJohn Marino   /* Mark the page as inaccessible.  Discard the handle to
967*e4b17023SJohn Marino      avoid handle leak.  */
968*e4b17023SJohn Marino   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (entry->common.page,
969*e4b17023SJohn Marino 						SMALL_PAGE_SIZE));
970*e4b17023SJohn Marino 
971*e4b17023SJohn Marino   entry->next = entry->common.zone->free_pages;
972*e4b17023SJohn Marino   entry->common.zone->free_pages = entry;
973*e4b17023SJohn Marino   entry->common.zone->n_small_pages--;
974*e4b17023SJohn Marino }
975*e4b17023SJohn Marino 
976*e4b17023SJohn Marino /* Release a large page that is no longer needed.  */
977*e4b17023SJohn Marino 
978*e4b17023SJohn Marino static inline void
free_large_page(struct large_page_entry * entry)979*e4b17023SJohn Marino free_large_page (struct large_page_entry *entry)
980*e4b17023SJohn Marino {
981*e4b17023SJohn Marino   if (GGC_DEBUG_LEVEL >= 2)
982*e4b17023SJohn Marino     fprintf (G.debug_file,
983*e4b17023SJohn Marino 	     "Deallocating %s page at %p, data %p-%p\n",
984*e4b17023SJohn Marino 	     entry->common.zone->name, (PTR) entry,
985*e4b17023SJohn Marino 	     entry->common.page, entry->common.page + SMALL_PAGE_SIZE - 1);
986*e4b17023SJohn Marino 
987*e4b17023SJohn Marino   gcc_assert (entry->common.large_p);
988*e4b17023SJohn Marino 
989*e4b17023SJohn Marino   set_page_table_entry (entry->common.page, NULL);
990*e4b17023SJohn Marino   free (entry);
991*e4b17023SJohn Marino }
992*e4b17023SJohn Marino 
993*e4b17023SJohn Marino /* Release the free page cache to the system.  */
994*e4b17023SJohn Marino 
995*e4b17023SJohn Marino static void
release_pages(struct alloc_zone * zone)996*e4b17023SJohn Marino release_pages (struct alloc_zone *zone)
997*e4b17023SJohn Marino {
998*e4b17023SJohn Marino #ifdef USING_MMAP
999*e4b17023SJohn Marino   struct small_page_entry *p, *next;
1000*e4b17023SJohn Marino   char *start;
1001*e4b17023SJohn Marino   size_t len;
1002*e4b17023SJohn Marino 
1003*e4b17023SJohn Marino   /* Gather up adjacent pages so they are unmapped together.  */
1004*e4b17023SJohn Marino   p = zone->free_pages;
1005*e4b17023SJohn Marino 
1006*e4b17023SJohn Marino   while (p)
1007*e4b17023SJohn Marino     {
1008*e4b17023SJohn Marino       start = p->common.page;
1009*e4b17023SJohn Marino       next = p->next;
1010*e4b17023SJohn Marino       len = SMALL_PAGE_SIZE;
1011*e4b17023SJohn Marino       set_page_table_entry (p->common.page, NULL);
1012*e4b17023SJohn Marino       p = next;
1013*e4b17023SJohn Marino 
1014*e4b17023SJohn Marino       while (p && p->common.page == start + len)
1015*e4b17023SJohn Marino 	{
1016*e4b17023SJohn Marino 	  next = p->next;
1017*e4b17023SJohn Marino 	  len += SMALL_PAGE_SIZE;
1018*e4b17023SJohn Marino 	  set_page_table_entry (p->common.page, NULL);
1019*e4b17023SJohn Marino 	  p = next;
1020*e4b17023SJohn Marino 	}
1021*e4b17023SJohn Marino 
1022*e4b17023SJohn Marino       munmap (start, len);
1023*e4b17023SJohn Marino       zone->bytes_mapped -= len;
1024*e4b17023SJohn Marino     }
1025*e4b17023SJohn Marino 
1026*e4b17023SJohn Marino   zone->free_pages = NULL;
1027*e4b17023SJohn Marino #endif
1028*e4b17023SJohn Marino }
1029*e4b17023SJohn Marino 
1030*e4b17023SJohn Marino /* Place the block at PTR of size SIZE on the free list for ZONE.  */
1031*e4b17023SJohn Marino 
1032*e4b17023SJohn Marino static inline void
free_chunk(char * ptr,size_t size,struct alloc_zone * zone)1033*e4b17023SJohn Marino free_chunk (char *ptr, size_t size, struct alloc_zone *zone)
1034*e4b17023SJohn Marino {
1035*e4b17023SJohn Marino   struct alloc_chunk *chunk = (struct alloc_chunk *) ptr;
1036*e4b17023SJohn Marino   size_t bin = 0;
1037*e4b17023SJohn Marino 
1038*e4b17023SJohn Marino   bin = SIZE_BIN_DOWN (size);
1039*e4b17023SJohn Marino   gcc_assert (bin != 0);
1040*e4b17023SJohn Marino   if (bin > NUM_FREE_BINS)
1041*e4b17023SJohn Marino     {
1042*e4b17023SJohn Marino       bin = 0;
1043*e4b17023SJohn Marino       VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (chunk,
1044*e4b17023SJohn Marino 						     sizeof (struct
1045*e4b17023SJohn Marino 							     alloc_chunk)));
1046*e4b17023SJohn Marino       chunk->size = size;
1047*e4b17023SJohn Marino       chunk->next_free = zone->free_chunks[bin];
1048*e4b17023SJohn Marino       VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (ptr
1049*e4b17023SJohn Marino 						    + sizeof (struct
1050*e4b17023SJohn Marino 							      alloc_chunk),
1051*e4b17023SJohn Marino 						    size
1052*e4b17023SJohn Marino 						    - sizeof (struct
1053*e4b17023SJohn Marino 							      alloc_chunk)));
1054*e4b17023SJohn Marino     }
1055*e4b17023SJohn Marino   else
1056*e4b17023SJohn Marino     {
1057*e4b17023SJohn Marino       VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (chunk,
1058*e4b17023SJohn Marino 						     sizeof (struct
1059*e4b17023SJohn Marino 							     alloc_chunk *)));
1060*e4b17023SJohn Marino       chunk->next_free = zone->free_chunks[bin];
1061*e4b17023SJohn Marino       VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (ptr
1062*e4b17023SJohn Marino 						    + sizeof (struct
1063*e4b17023SJohn Marino 							      alloc_chunk *),
1064*e4b17023SJohn Marino 						    size
1065*e4b17023SJohn Marino 						    - sizeof (struct
1066*e4b17023SJohn Marino 							      alloc_chunk *)));
1067*e4b17023SJohn Marino     }
1068*e4b17023SJohn Marino 
1069*e4b17023SJohn Marino   zone->free_chunks[bin] = chunk;
1070*e4b17023SJohn Marino   if (bin > zone->high_free_bin)
1071*e4b17023SJohn Marino     zone->high_free_bin = bin;
1072*e4b17023SJohn Marino   if (GGC_DEBUG_LEVEL >= 3)
1073*e4b17023SJohn Marino     fprintf (G.debug_file, "Deallocating object, chunk=%p\n", (void *)chunk);
1074*e4b17023SJohn Marino }
1075*e4b17023SJohn Marino 
1076*e4b17023SJohn Marino /* For a given size of memory requested for allocation, return the
1077*e4b17023SJohn Marino    actual size that is going to be allocated.  */
1078*e4b17023SJohn Marino 
1079*e4b17023SJohn Marino size_t
ggc_round_alloc_size(size_t requested_size)1080*e4b17023SJohn Marino ggc_round_alloc_size (size_t requested_size)
1081*e4b17023SJohn Marino {
1082*e4b17023SJohn Marino   size_t size;
1083*e4b17023SJohn Marino 
1084*e4b17023SJohn Marino   /* Make sure that zero-sized allocations get a unique and freeable
1085*e4b17023SJohn Marino      pointer.  */
1086*e4b17023SJohn Marino   if (requested_size == 0)
1087*e4b17023SJohn Marino     size = MAX_ALIGNMENT;
1088*e4b17023SJohn Marino   else
1089*e4b17023SJohn Marino     size = (requested_size + MAX_ALIGNMENT - 1) & -MAX_ALIGNMENT;
1090*e4b17023SJohn Marino 
1091*e4b17023SJohn Marino   return size;
1092*e4b17023SJohn Marino }
1093*e4b17023SJohn Marino 
1094*e4b17023SJohn Marino /* Allocate a chunk of memory of at least ORIG_SIZE bytes, in ZONE.  */
1095*e4b17023SJohn Marino 
1096*e4b17023SJohn Marino void *
ggc_internal_alloc_zone_stat(size_t orig_size,struct alloc_zone * zone MEM_STAT_DECL)1097*e4b17023SJohn Marino ggc_internal_alloc_zone_stat (size_t orig_size, struct alloc_zone *zone
1098*e4b17023SJohn Marino 			      MEM_STAT_DECL)
1099*e4b17023SJohn Marino {
1100*e4b17023SJohn Marino   size_t bin;
1101*e4b17023SJohn Marino   size_t csize;
1102*e4b17023SJohn Marino   struct small_page_entry *entry;
1103*e4b17023SJohn Marino   struct alloc_chunk *chunk, **pp;
1104*e4b17023SJohn Marino   void *result;
1105*e4b17023SJohn Marino   size_t size = ggc_round_alloc_size (orig_size);
1106*e4b17023SJohn Marino 
1107*e4b17023SJohn Marino   /* Try to allocate the object from several different sources.  Each
1108*e4b17023SJohn Marino      of these cases is responsible for setting RESULT and SIZE to
1109*e4b17023SJohn Marino      describe the allocated block, before jumping to FOUND.  If a
1110*e4b17023SJohn Marino      chunk is split, the allocate bit for the new chunk should also be
1111*e4b17023SJohn Marino      set.
1112*e4b17023SJohn Marino 
1113*e4b17023SJohn Marino      Large objects are handled specially.  However, they'll just fail
1114*e4b17023SJohn Marino      the next couple of conditions, so we can wait to check for them
1115*e4b17023SJohn Marino      below.  The large object case is relatively rare (< 1%), so this
1116*e4b17023SJohn Marino      is a win.  */
1117*e4b17023SJohn Marino 
1118*e4b17023SJohn Marino   /* First try to split the last chunk we allocated.  For best
1119*e4b17023SJohn Marino      fragmentation behavior it would be better to look for a
1120*e4b17023SJohn Marino      free bin of the appropriate size for a small object.  However,
1121*e4b17023SJohn Marino      we're unlikely (1% - 7%) to find one, and this gives better
1122*e4b17023SJohn Marino      locality behavior anyway.  This case handles the lion's share
1123*e4b17023SJohn Marino      of all calls to this function.  */
1124*e4b17023SJohn Marino   if (size <= zone->cached_free_size)
1125*e4b17023SJohn Marino     {
1126*e4b17023SJohn Marino       result = zone->cached_free;
1127*e4b17023SJohn Marino 
1128*e4b17023SJohn Marino       zone->cached_free_size -= size;
1129*e4b17023SJohn Marino       if (zone->cached_free_size)
1130*e4b17023SJohn Marino 	{
1131*e4b17023SJohn Marino 	  zone->cached_free += size;
1132*e4b17023SJohn Marino 	  zone_set_object_alloc_bit (zone->cached_free);
1133*e4b17023SJohn Marino 	}
1134*e4b17023SJohn Marino 
1135*e4b17023SJohn Marino       goto found;
1136*e4b17023SJohn Marino     }
1137*e4b17023SJohn Marino 
1138*e4b17023SJohn Marino   /* Next, try to find a free bin of the exactly correct size.  */
1139*e4b17023SJohn Marino 
1140*e4b17023SJohn Marino   /* We want to round SIZE up, rather than down, but we know it's
1141*e4b17023SJohn Marino      already aligned to at least FREE_BIN_DELTA, so we can just
1142*e4b17023SJohn Marino      shift.  */
1143*e4b17023SJohn Marino   bin = SIZE_BIN_DOWN (size);
1144*e4b17023SJohn Marino 
1145*e4b17023SJohn Marino   if (bin <= NUM_FREE_BINS
1146*e4b17023SJohn Marino       && (chunk = zone->free_chunks[bin]) != NULL)
1147*e4b17023SJohn Marino     {
1148*e4b17023SJohn Marino       /* We have a chunk of the right size.  Pull it off the free list
1149*e4b17023SJohn Marino 	 and use it.  */
1150*e4b17023SJohn Marino 
1151*e4b17023SJohn Marino       zone->free_chunks[bin] = chunk->next_free;
1152*e4b17023SJohn Marino 
1153*e4b17023SJohn Marino       /* NOTE: SIZE is only guaranteed to be right if MAX_ALIGNMENT
1154*e4b17023SJohn Marino 	 == FREE_BIN_DELTA.  */
1155*e4b17023SJohn Marino       result = chunk;
1156*e4b17023SJohn Marino 
1157*e4b17023SJohn Marino       /* The allocation bits are already set correctly.  HIGH_FREE_BIN
1158*e4b17023SJohn Marino 	 may now be wrong, if this was the last chunk in the high bin.
1159*e4b17023SJohn Marino 	 Rather than fixing it up now, wait until we need to search
1160*e4b17023SJohn Marino 	 the free bins.  */
1161*e4b17023SJohn Marino 
1162*e4b17023SJohn Marino       goto found;
1163*e4b17023SJohn Marino     }
1164*e4b17023SJohn Marino 
1165*e4b17023SJohn Marino   /* Next, if there wasn't a chunk of the ideal size, look for a chunk
1166*e4b17023SJohn Marino      to split.  We can find one in the too-big bin, or in the largest
1167*e4b17023SJohn Marino      sized bin with a chunk in it.  Try the largest normal-sized bin
1168*e4b17023SJohn Marino      first.  */
1169*e4b17023SJohn Marino 
1170*e4b17023SJohn Marino   if (zone->high_free_bin > bin)
1171*e4b17023SJohn Marino     {
1172*e4b17023SJohn Marino       /* Find the highest numbered free bin.  It will be at or below
1173*e4b17023SJohn Marino 	 the watermark.  */
1174*e4b17023SJohn Marino       while (zone->high_free_bin > bin
1175*e4b17023SJohn Marino 	     && zone->free_chunks[zone->high_free_bin] == NULL)
1176*e4b17023SJohn Marino 	zone->high_free_bin--;
1177*e4b17023SJohn Marino 
1178*e4b17023SJohn Marino       if (zone->high_free_bin > bin)
1179*e4b17023SJohn Marino 	{
1180*e4b17023SJohn Marino 	  size_t tbin = zone->high_free_bin;
1181*e4b17023SJohn Marino 	  chunk = zone->free_chunks[tbin];
1182*e4b17023SJohn Marino 
1183*e4b17023SJohn Marino 	  /* Remove the chunk from its previous bin.  */
1184*e4b17023SJohn Marino 	  zone->free_chunks[tbin] = chunk->next_free;
1185*e4b17023SJohn Marino 
1186*e4b17023SJohn Marino 	  result = (char *) chunk;
1187*e4b17023SJohn Marino 
1188*e4b17023SJohn Marino 	  /* Save the rest of the chunk for future allocation.  */
1189*e4b17023SJohn Marino 	  if (zone->cached_free_size)
1190*e4b17023SJohn Marino 	    free_chunk (zone->cached_free, zone->cached_free_size, zone);
1191*e4b17023SJohn Marino 
1192*e4b17023SJohn Marino 	  chunk = (struct alloc_chunk *) ((char *) result + size);
1193*e4b17023SJohn Marino 	  zone->cached_free = (char *) chunk;
1194*e4b17023SJohn Marino 	  zone->cached_free_size = (tbin - bin) * FREE_BIN_DELTA;
1195*e4b17023SJohn Marino 
1196*e4b17023SJohn Marino 	  /* Mark the new free chunk as an object, so that we can
1197*e4b17023SJohn Marino 	     find the size of the newly allocated object.  */
1198*e4b17023SJohn Marino 	  zone_set_object_alloc_bit (chunk);
1199*e4b17023SJohn Marino 
1200*e4b17023SJohn Marino 	  /* HIGH_FREE_BIN may now be wrong, if this was the last
1201*e4b17023SJohn Marino 	     chunk in the high bin.  Rather than fixing it up now,
1202*e4b17023SJohn Marino 	     wait until we need to search the free bins.  */
1203*e4b17023SJohn Marino 
1204*e4b17023SJohn Marino 	  goto found;
1205*e4b17023SJohn Marino 	}
1206*e4b17023SJohn Marino     }
1207*e4b17023SJohn Marino 
1208*e4b17023SJohn Marino   /* Failing that, look through the "other" bucket for a chunk
1209*e4b17023SJohn Marino      that is large enough.  */
1210*e4b17023SJohn Marino   pp = &(zone->free_chunks[0]);
1211*e4b17023SJohn Marino   chunk = *pp;
1212*e4b17023SJohn Marino   while (chunk && chunk->size < size)
1213*e4b17023SJohn Marino     {
1214*e4b17023SJohn Marino       pp = &chunk->next_free;
1215*e4b17023SJohn Marino       chunk = *pp;
1216*e4b17023SJohn Marino     }
1217*e4b17023SJohn Marino 
1218*e4b17023SJohn Marino   if (chunk)
1219*e4b17023SJohn Marino     {
1220*e4b17023SJohn Marino       /* Remove the chunk from its previous bin.  */
1221*e4b17023SJohn Marino       *pp = chunk->next_free;
1222*e4b17023SJohn Marino 
1223*e4b17023SJohn Marino       result = (char *) chunk;
1224*e4b17023SJohn Marino 
1225*e4b17023SJohn Marino       /* Save the rest of the chunk for future allocation, if there's any
1226*e4b17023SJohn Marino 	 left over.  */
1227*e4b17023SJohn Marino       csize = chunk->size;
1228*e4b17023SJohn Marino       if (csize > size)
1229*e4b17023SJohn Marino 	{
1230*e4b17023SJohn Marino 	  if (zone->cached_free_size)
1231*e4b17023SJohn Marino 	    free_chunk (zone->cached_free, zone->cached_free_size, zone);
1232*e4b17023SJohn Marino 
1233*e4b17023SJohn Marino 	  chunk = (struct alloc_chunk *) ((char *) result + size);
1234*e4b17023SJohn Marino 	  zone->cached_free = (char *) chunk;
1235*e4b17023SJohn Marino 	  zone->cached_free_size = csize - size;
1236*e4b17023SJohn Marino 
1237*e4b17023SJohn Marino 	  /* Mark the new free chunk as an object.  */
1238*e4b17023SJohn Marino 	  zone_set_object_alloc_bit (chunk);
1239*e4b17023SJohn Marino 	}
1240*e4b17023SJohn Marino 
1241*e4b17023SJohn Marino       goto found;
1242*e4b17023SJohn Marino     }
1243*e4b17023SJohn Marino 
1244*e4b17023SJohn Marino   /* Handle large allocations.  We could choose any threshold between
1245*e4b17023SJohn Marino      GGC_PAGE_SIZE - sizeof (struct large_page_entry) and
1246*e4b17023SJohn Marino      GGC_PAGE_SIZE.  It can't be smaller, because then it wouldn't
1247*e4b17023SJohn Marino      be guaranteed to have a unique entry in the lookup table.  Large
1248*e4b17023SJohn Marino      allocations will always fall through to here.  */
1249*e4b17023SJohn Marino   if (size > GGC_PAGE_SIZE)
1250*e4b17023SJohn Marino     {
1251*e4b17023SJohn Marino       struct large_page_entry *entry = alloc_large_page (size, zone);
1252*e4b17023SJohn Marino 
1253*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
1254*e4b17023SJohn Marino       entry->common.survived = 0;
1255*e4b17023SJohn Marino #endif
1256*e4b17023SJohn Marino 
1257*e4b17023SJohn Marino       entry->next = zone->large_pages;
1258*e4b17023SJohn Marino       if (zone->large_pages)
1259*e4b17023SJohn Marino 	zone->large_pages->prev = entry;
1260*e4b17023SJohn Marino       zone->large_pages = entry;
1261*e4b17023SJohn Marino 
1262*e4b17023SJohn Marino       result = entry->common.page;
1263*e4b17023SJohn Marino 
1264*e4b17023SJohn Marino       goto found;
1265*e4b17023SJohn Marino     }
1266*e4b17023SJohn Marino 
1267*e4b17023SJohn Marino   /* Failing everything above, allocate a new small page.  */
1268*e4b17023SJohn Marino 
1269*e4b17023SJohn Marino   entry = alloc_small_page (zone);
1270*e4b17023SJohn Marino   entry->next = zone->pages;
1271*e4b17023SJohn Marino   zone->pages = entry;
1272*e4b17023SJohn Marino 
1273*e4b17023SJohn Marino   /* Mark the first chunk in the new page.  */
1274*e4b17023SJohn Marino   entry->alloc_bits[0] = 1;
1275*e4b17023SJohn Marino 
1276*e4b17023SJohn Marino   result = entry->common.page;
1277*e4b17023SJohn Marino   if (size < SMALL_PAGE_SIZE)
1278*e4b17023SJohn Marino     {
1279*e4b17023SJohn Marino       if (zone->cached_free_size)
1280*e4b17023SJohn Marino 	free_chunk (zone->cached_free, zone->cached_free_size, zone);
1281*e4b17023SJohn Marino 
1282*e4b17023SJohn Marino       zone->cached_free = (char *) result + size;
1283*e4b17023SJohn Marino       zone->cached_free_size = SMALL_PAGE_SIZE - size;
1284*e4b17023SJohn Marino 
1285*e4b17023SJohn Marino       /* Mark the new free chunk as an object.  */
1286*e4b17023SJohn Marino       zone_set_object_alloc_bit (zone->cached_free);
1287*e4b17023SJohn Marino     }
1288*e4b17023SJohn Marino 
1289*e4b17023SJohn Marino  found:
1290*e4b17023SJohn Marino 
1291*e4b17023SJohn Marino   /* We could save TYPE in the chunk, but we don't use that for
1292*e4b17023SJohn Marino      anything yet.  If we wanted to, we could do it by adding it
1293*e4b17023SJohn Marino      either before the beginning of the chunk or after its end,
1294*e4b17023SJohn Marino      and adjusting the size and pointer appropriately.  */
1295*e4b17023SJohn Marino 
1296*e4b17023SJohn Marino   /* We'll probably write to this after we return.  */
1297*e4b17023SJohn Marino   prefetchw (result);
1298*e4b17023SJohn Marino 
1299*e4b17023SJohn Marino #ifdef ENABLE_GC_CHECKING
1300*e4b17023SJohn Marino   /* `Poison' the entire allocated object.  */
1301*e4b17023SJohn Marino   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, size));
1302*e4b17023SJohn Marino   memset (result, 0xaf, size);
1303*e4b17023SJohn Marino   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (result + orig_size,
1304*e4b17023SJohn Marino 						size - orig_size));
1305*e4b17023SJohn Marino #endif
1306*e4b17023SJohn Marino 
1307*e4b17023SJohn Marino   /* Tell Valgrind that the memory is there, but its content isn't
1308*e4b17023SJohn Marino      defined.  The bytes at the end of the object are still marked
1309*e4b17023SJohn Marino      unaccessible.  */
1310*e4b17023SJohn Marino   VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (result, orig_size));
1311*e4b17023SJohn Marino 
1312*e4b17023SJohn Marino   /* Keep track of how many bytes are being allocated.  This
1313*e4b17023SJohn Marino      information is used in deciding when to collect.  */
1314*e4b17023SJohn Marino   zone->allocated += size;
1315*e4b17023SJohn Marino 
1316*e4b17023SJohn Marino   timevar_ggc_mem_total += size;
1317*e4b17023SJohn Marino 
1318*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
1319*e4b17023SJohn Marino   ggc_record_overhead (orig_size, size - orig_size, result PASS_MEM_STAT);
1320*e4b17023SJohn Marino 
1321*e4b17023SJohn Marino   {
1322*e4b17023SJohn Marino     size_t object_size = size;
1323*e4b17023SJohn Marino     size_t overhead = object_size - orig_size;
1324*e4b17023SJohn Marino 
1325*e4b17023SJohn Marino     zone->stats.total_overhead += overhead;
1326*e4b17023SJohn Marino     zone->stats.total_allocated += object_size;
1327*e4b17023SJohn Marino 
1328*e4b17023SJohn Marino     if (orig_size <= 32)
1329*e4b17023SJohn Marino       {
1330*e4b17023SJohn Marino 	zone->stats.total_overhead_under32 += overhead;
1331*e4b17023SJohn Marino 	zone->stats.total_allocated_under32 += object_size;
1332*e4b17023SJohn Marino       }
1333*e4b17023SJohn Marino     if (orig_size <= 64)
1334*e4b17023SJohn Marino       {
1335*e4b17023SJohn Marino 	zone->stats.total_overhead_under64 += overhead;
1336*e4b17023SJohn Marino 	zone->stats.total_allocated_under64 += object_size;
1337*e4b17023SJohn Marino       }
1338*e4b17023SJohn Marino     if (orig_size <= 128)
1339*e4b17023SJohn Marino       {
1340*e4b17023SJohn Marino 	zone->stats.total_overhead_under128 += overhead;
1341*e4b17023SJohn Marino 	zone->stats.total_allocated_under128 += object_size;
1342*e4b17023SJohn Marino       }
1343*e4b17023SJohn Marino   }
1344*e4b17023SJohn Marino #endif
1345*e4b17023SJohn Marino 
1346*e4b17023SJohn Marino   if (GGC_DEBUG_LEVEL >= 3)
1347*e4b17023SJohn Marino     fprintf (G.debug_file, "Allocating object, size=%lu at %p\n",
1348*e4b17023SJohn Marino 	     (unsigned long) size, result);
1349*e4b17023SJohn Marino 
1350*e4b17023SJohn Marino   return result;
1351*e4b17023SJohn Marino }
1352*e4b17023SJohn Marino 
1353*e4b17023SJohn Marino #define ggc_internal_alloc_zone_pass_stat(s,z)          \
1354*e4b17023SJohn Marino     ggc_internal_alloc_zone_stat (s,z PASS_MEM_STAT)
1355*e4b17023SJohn Marino 
1356*e4b17023SJohn Marino void *
ggc_internal_cleared_alloc_zone_stat(size_t orig_size,struct alloc_zone * zone MEM_STAT_DECL)1357*e4b17023SJohn Marino ggc_internal_cleared_alloc_zone_stat (size_t orig_size,
1358*e4b17023SJohn Marino 				      struct alloc_zone *zone MEM_STAT_DECL)
1359*e4b17023SJohn Marino {
1360*e4b17023SJohn Marino   void * result = ggc_internal_alloc_zone_pass_stat (orig_size, zone);
1361*e4b17023SJohn Marino   memset (result, 0, orig_size);
1362*e4b17023SJohn Marino   return result;
1363*e4b17023SJohn Marino }
1364*e4b17023SJohn Marino 
1365*e4b17023SJohn Marino 
1366*e4b17023SJohn Marino /* Allocate a SIZE of chunk memory of GTE type, into an appropriate zone
1367*e4b17023SJohn Marino    for that type.  */
1368*e4b17023SJohn Marino 
1369*e4b17023SJohn Marino void *
ggc_alloc_typed_stat(enum gt_types_enum gte,size_t size MEM_STAT_DECL)1370*e4b17023SJohn Marino ggc_alloc_typed_stat (enum gt_types_enum gte, size_t size
1371*e4b17023SJohn Marino 		      MEM_STAT_DECL)
1372*e4b17023SJohn Marino {
1373*e4b17023SJohn Marino   switch (gte)
1374*e4b17023SJohn Marino     {
1375*e4b17023SJohn Marino     case gt_ggc_e_14lang_tree_node:
1376*e4b17023SJohn Marino       return ggc_internal_alloc_zone_pass_stat (size, &tree_zone);
1377*e4b17023SJohn Marino 
1378*e4b17023SJohn Marino     case gt_ggc_e_7rtx_def:
1379*e4b17023SJohn Marino       return ggc_internal_alloc_zone_pass_stat (size, &rtl_zone);
1380*e4b17023SJohn Marino 
1381*e4b17023SJohn Marino     case gt_ggc_e_9rtvec_def:
1382*e4b17023SJohn Marino       return ggc_internal_alloc_zone_pass_stat (size, &rtl_zone);
1383*e4b17023SJohn Marino 
1384*e4b17023SJohn Marino     default:
1385*e4b17023SJohn Marino       return ggc_internal_alloc_zone_pass_stat (size, &main_zone);
1386*e4b17023SJohn Marino     }
1387*e4b17023SJohn Marino }
1388*e4b17023SJohn Marino 
1389*e4b17023SJohn Marino /* Normal GC allocation simply allocates into the main zone.  */
1390*e4b17023SJohn Marino 
1391*e4b17023SJohn Marino void *
ggc_internal_alloc_stat(size_t size MEM_STAT_DECL)1392*e4b17023SJohn Marino ggc_internal_alloc_stat (size_t size MEM_STAT_DECL)
1393*e4b17023SJohn Marino {
1394*e4b17023SJohn Marino   return ggc_internal_alloc_zone_pass_stat (size, &main_zone);
1395*e4b17023SJohn Marino }
1396*e4b17023SJohn Marino 
1397*e4b17023SJohn Marino /* Poison the chunk.  */
1398*e4b17023SJohn Marino #ifdef ENABLE_GC_CHECKING
1399*e4b17023SJohn Marino #define poison_region(PTR, SIZE)				      \
1400*e4b17023SJohn Marino   do {								      \
1401*e4b17023SJohn Marino     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED ((PTR), (SIZE)));   \
1402*e4b17023SJohn Marino     memset ((PTR), 0xa5, (SIZE));				      \
1403*e4b17023SJohn Marino     VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS ((PTR), (SIZE)));    \
1404*e4b17023SJohn Marino   } while (0)
1405*e4b17023SJohn Marino #else
1406*e4b17023SJohn Marino #define poison_region(PTR, SIZE)
1407*e4b17023SJohn Marino #endif
1408*e4b17023SJohn Marino 
1409*e4b17023SJohn Marino /* Free the object at P.  */
1410*e4b17023SJohn Marino 
1411*e4b17023SJohn Marino void
ggc_free(void * p)1412*e4b17023SJohn Marino ggc_free (void *p)
1413*e4b17023SJohn Marino {
1414*e4b17023SJohn Marino   struct page_entry *page;
1415*e4b17023SJohn Marino 
1416*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
1417*e4b17023SJohn Marino   ggc_free_overhead (p);
1418*e4b17023SJohn Marino #endif
1419*e4b17023SJohn Marino 
1420*e4b17023SJohn Marino   poison_region (p, ggc_get_size (p));
1421*e4b17023SJohn Marino 
1422*e4b17023SJohn Marino   page = zone_get_object_page (p);
1423*e4b17023SJohn Marino 
1424*e4b17023SJohn Marino   if (page->large_p)
1425*e4b17023SJohn Marino     {
1426*e4b17023SJohn Marino       struct large_page_entry *large_page
1427*e4b17023SJohn Marino 	= (struct large_page_entry *) page;
1428*e4b17023SJohn Marino 
1429*e4b17023SJohn Marino       /* Remove the page from the linked list.  */
1430*e4b17023SJohn Marino       if (large_page->prev)
1431*e4b17023SJohn Marino 	large_page->prev->next = large_page->next;
1432*e4b17023SJohn Marino       else
1433*e4b17023SJohn Marino 	{
1434*e4b17023SJohn Marino 	  gcc_assert (large_page->common.zone->large_pages == large_page);
1435*e4b17023SJohn Marino 	  large_page->common.zone->large_pages = large_page->next;
1436*e4b17023SJohn Marino 	}
1437*e4b17023SJohn Marino       if (large_page->next)
1438*e4b17023SJohn Marino 	large_page->next->prev = large_page->prev;
1439*e4b17023SJohn Marino 
1440*e4b17023SJohn Marino       large_page->common.zone->allocated -= large_page->bytes;
1441*e4b17023SJohn Marino 
1442*e4b17023SJohn Marino       /* Release the memory associated with this object.  */
1443*e4b17023SJohn Marino       free_large_page (large_page);
1444*e4b17023SJohn Marino     }
1445*e4b17023SJohn Marino   else if (page->pch_p)
1446*e4b17023SJohn Marino     /* Don't do anything.  We won't allocate a new object from the
1447*e4b17023SJohn Marino        PCH zone so there's no point in releasing anything.  */
1448*e4b17023SJohn Marino     ;
1449*e4b17023SJohn Marino   else
1450*e4b17023SJohn Marino     {
1451*e4b17023SJohn Marino       size_t size = ggc_get_size (p);
1452*e4b17023SJohn Marino 
1453*e4b17023SJohn Marino       page->zone->allocated -= size;
1454*e4b17023SJohn Marino 
1455*e4b17023SJohn Marino       /* Add the chunk to the free list.  We don't bother with coalescing,
1456*e4b17023SJohn Marino 	 since we are likely to want a chunk of this size again.  */
1457*e4b17023SJohn Marino       free_chunk ((char *)p, size, page->zone);
1458*e4b17023SJohn Marino     }
1459*e4b17023SJohn Marino }
1460*e4b17023SJohn Marino 
1461*e4b17023SJohn Marino /* Mark function for strings.  */
1462*e4b17023SJohn Marino 
1463*e4b17023SJohn Marino void
gt_ggc_m_S(const void * p)1464*e4b17023SJohn Marino gt_ggc_m_S (const void *p)
1465*e4b17023SJohn Marino {
1466*e4b17023SJohn Marino   page_entry *entry;
1467*e4b17023SJohn Marino   unsigned long offset;
1468*e4b17023SJohn Marino 
1469*e4b17023SJohn Marino   if (!p)
1470*e4b17023SJohn Marino     return;
1471*e4b17023SJohn Marino 
1472*e4b17023SJohn Marino   /* Look up the page on which the object is alloced.  .  */
1473*e4b17023SJohn Marino   entry = lookup_page_table_if_allocated (p);
1474*e4b17023SJohn Marino   if (! entry)
1475*e4b17023SJohn Marino     return;
1476*e4b17023SJohn Marino 
1477*e4b17023SJohn Marino   if (entry->pch_p)
1478*e4b17023SJohn Marino     {
1479*e4b17023SJohn Marino       size_t alloc_word, alloc_bit, t;
1480*e4b17023SJohn Marino       t = ((const char *) p - pch_zone.page) / BYTES_PER_ALLOC_BIT;
1481*e4b17023SJohn Marino       alloc_word = t / (8 * sizeof (alloc_type));
1482*e4b17023SJohn Marino       alloc_bit = t % (8 * sizeof (alloc_type));
1483*e4b17023SJohn Marino       offset = zone_find_object_offset (pch_zone.alloc_bits, alloc_word,
1484*e4b17023SJohn Marino 					alloc_bit);
1485*e4b17023SJohn Marino     }
1486*e4b17023SJohn Marino   else if (entry->large_p)
1487*e4b17023SJohn Marino     {
1488*e4b17023SJohn Marino       struct large_page_entry *le = (struct large_page_entry *) entry;
1489*e4b17023SJohn Marino       offset = ((const char *) p) - entry->page;
1490*e4b17023SJohn Marino       gcc_assert (offset < le->bytes);
1491*e4b17023SJohn Marino     }
1492*e4b17023SJohn Marino   else
1493*e4b17023SJohn Marino     {
1494*e4b17023SJohn Marino       struct small_page_entry *se = (struct small_page_entry *) entry;
1495*e4b17023SJohn Marino       unsigned int start_word = zone_get_object_alloc_word (p);
1496*e4b17023SJohn Marino       unsigned int start_bit = zone_get_object_alloc_bit (p);
1497*e4b17023SJohn Marino       offset = zone_find_object_offset (se->alloc_bits, start_word, start_bit);
1498*e4b17023SJohn Marino 
1499*e4b17023SJohn Marino       /* On some platforms a char* will not necessarily line up on an
1500*e4b17023SJohn Marino 	 allocation boundary, so we have to update the offset to
1501*e4b17023SJohn Marino 	 account for the leftover bytes.  */
1502*e4b17023SJohn Marino       offset += (size_t) p % BYTES_PER_ALLOC_BIT;
1503*e4b17023SJohn Marino     }
1504*e4b17023SJohn Marino 
1505*e4b17023SJohn Marino   if (offset)
1506*e4b17023SJohn Marino     {
1507*e4b17023SJohn Marino       /* Here we've seen a char* which does not point to the beginning
1508*e4b17023SJohn Marino 	 of an allocated object.  We assume it points to the middle of
1509*e4b17023SJohn Marino 	 a STRING_CST.  */
1510*e4b17023SJohn Marino       gcc_assert (offset == offsetof (struct tree_string, str));
1511*e4b17023SJohn Marino       p = ((const char *) p) - offset;
1512*e4b17023SJohn Marino       gt_ggc_mx_lang_tree_node (CONST_CAST(void *, p));
1513*e4b17023SJohn Marino       return;
1514*e4b17023SJohn Marino     }
1515*e4b17023SJohn Marino 
1516*e4b17023SJohn Marino   /* Inefficient, but also unlikely to matter.  */
1517*e4b17023SJohn Marino   ggc_set_mark (p);
1518*e4b17023SJohn Marino }
1519*e4b17023SJohn Marino 
1520*e4b17023SJohn Marino /* If P is not marked, mark it and return false.  Otherwise return true.
1521*e4b17023SJohn Marino    P must have been allocated by the GC allocator; it mustn't point to
1522*e4b17023SJohn Marino    static objects, stack variables, or memory allocated with malloc.  */
1523*e4b17023SJohn Marino 
1524*e4b17023SJohn Marino int
ggc_set_mark(const void * p)1525*e4b17023SJohn Marino ggc_set_mark (const void *p)
1526*e4b17023SJohn Marino {
1527*e4b17023SJohn Marino   struct page_entry *page;
1528*e4b17023SJohn Marino   const char *ptr = (const char *) p;
1529*e4b17023SJohn Marino 
1530*e4b17023SJohn Marino   page = zone_get_object_page (p);
1531*e4b17023SJohn Marino 
1532*e4b17023SJohn Marino   if (page->pch_p)
1533*e4b17023SJohn Marino     {
1534*e4b17023SJohn Marino       size_t mark_word, mark_bit, offset;
1535*e4b17023SJohn Marino       offset = (ptr - pch_zone.page) / BYTES_PER_MARK_BIT;
1536*e4b17023SJohn Marino       mark_word = offset / (8 * sizeof (mark_type));
1537*e4b17023SJohn Marino       mark_bit = offset % (8 * sizeof (mark_type));
1538*e4b17023SJohn Marino 
1539*e4b17023SJohn Marino       if (pch_zone.mark_bits[mark_word] & (1 << mark_bit))
1540*e4b17023SJohn Marino 	return 1;
1541*e4b17023SJohn Marino       pch_zone.mark_bits[mark_word] |= (1 << mark_bit);
1542*e4b17023SJohn Marino     }
1543*e4b17023SJohn Marino   else if (page->large_p)
1544*e4b17023SJohn Marino     {
1545*e4b17023SJohn Marino       struct large_page_entry *large_page
1546*e4b17023SJohn Marino 	= (struct large_page_entry *) page;
1547*e4b17023SJohn Marino 
1548*e4b17023SJohn Marino       if (large_page->mark_p)
1549*e4b17023SJohn Marino 	return 1;
1550*e4b17023SJohn Marino       large_page->mark_p = true;
1551*e4b17023SJohn Marino     }
1552*e4b17023SJohn Marino   else
1553*e4b17023SJohn Marino     {
1554*e4b17023SJohn Marino       struct small_page_entry *small_page
1555*e4b17023SJohn Marino 	= (struct small_page_entry *) page;
1556*e4b17023SJohn Marino 
1557*e4b17023SJohn Marino       if (small_page->mark_bits[zone_get_object_mark_word (p)]
1558*e4b17023SJohn Marino 	  & (1 << zone_get_object_mark_bit (p)))
1559*e4b17023SJohn Marino 	return 1;
1560*e4b17023SJohn Marino       small_page->mark_bits[zone_get_object_mark_word (p)]
1561*e4b17023SJohn Marino 	|= (1 << zone_get_object_mark_bit (p));
1562*e4b17023SJohn Marino     }
1563*e4b17023SJohn Marino 
1564*e4b17023SJohn Marino   if (GGC_DEBUG_LEVEL >= 4)
1565*e4b17023SJohn Marino     fprintf (G.debug_file, "Marking %p\n", p);
1566*e4b17023SJohn Marino 
1567*e4b17023SJohn Marino   return 0;
1568*e4b17023SJohn Marino }
1569*e4b17023SJohn Marino 
1570*e4b17023SJohn Marino /* Return 1 if P has been marked, zero otherwise.
1571*e4b17023SJohn Marino    P must have been allocated by the GC allocator; it mustn't point to
1572*e4b17023SJohn Marino    static objects, stack variables, or memory allocated with malloc.  */
1573*e4b17023SJohn Marino 
1574*e4b17023SJohn Marino int
ggc_marked_p(const void * p)1575*e4b17023SJohn Marino ggc_marked_p (const void *p)
1576*e4b17023SJohn Marino {
1577*e4b17023SJohn Marino   struct page_entry *page;
1578*e4b17023SJohn Marino   const char *ptr = (const char *) p;
1579*e4b17023SJohn Marino 
1580*e4b17023SJohn Marino   page = zone_get_object_page (p);
1581*e4b17023SJohn Marino 
1582*e4b17023SJohn Marino   if (page->pch_p)
1583*e4b17023SJohn Marino     {
1584*e4b17023SJohn Marino       size_t mark_word, mark_bit, offset;
1585*e4b17023SJohn Marino       offset = (ptr - pch_zone.page) / BYTES_PER_MARK_BIT;
1586*e4b17023SJohn Marino       mark_word = offset / (8 * sizeof (mark_type));
1587*e4b17023SJohn Marino       mark_bit = offset % (8 * sizeof (mark_type));
1588*e4b17023SJohn Marino 
1589*e4b17023SJohn Marino       return (pch_zone.mark_bits[mark_word] & (1 << mark_bit)) != 0;
1590*e4b17023SJohn Marino     }
1591*e4b17023SJohn Marino 
1592*e4b17023SJohn Marino   if (page->large_p)
1593*e4b17023SJohn Marino     {
1594*e4b17023SJohn Marino       struct large_page_entry *large_page
1595*e4b17023SJohn Marino 	= (struct large_page_entry *) page;
1596*e4b17023SJohn Marino 
1597*e4b17023SJohn Marino       return large_page->mark_p;
1598*e4b17023SJohn Marino     }
1599*e4b17023SJohn Marino   else
1600*e4b17023SJohn Marino     {
1601*e4b17023SJohn Marino       struct small_page_entry *small_page
1602*e4b17023SJohn Marino 	= (struct small_page_entry *) page;
1603*e4b17023SJohn Marino 
1604*e4b17023SJohn Marino       return 0 != (small_page->mark_bits[zone_get_object_mark_word (p)]
1605*e4b17023SJohn Marino 		   & (1 << zone_get_object_mark_bit (p)));
1606*e4b17023SJohn Marino     }
1607*e4b17023SJohn Marino }
1608*e4b17023SJohn Marino 
1609*e4b17023SJohn Marino /* Return the size of the gc-able object P.  */
1610*e4b17023SJohn Marino 
1611*e4b17023SJohn Marino size_t
ggc_get_size(const void * p)1612*e4b17023SJohn Marino ggc_get_size (const void *p)
1613*e4b17023SJohn Marino {
1614*e4b17023SJohn Marino   struct page_entry *page;
1615*e4b17023SJohn Marino   const char *ptr = (const char *) p;
1616*e4b17023SJohn Marino 
1617*e4b17023SJohn Marino   page = zone_get_object_page (p);
1618*e4b17023SJohn Marino 
1619*e4b17023SJohn Marino   if (page->pch_p)
1620*e4b17023SJohn Marino     {
1621*e4b17023SJohn Marino       size_t alloc_word, alloc_bit, offset, max_size;
1622*e4b17023SJohn Marino       offset = (ptr - pch_zone.page) / BYTES_PER_ALLOC_BIT + 1;
1623*e4b17023SJohn Marino       alloc_word = offset / (8 * sizeof (alloc_type));
1624*e4b17023SJohn Marino       alloc_bit = offset % (8 * sizeof (alloc_type));
1625*e4b17023SJohn Marino       max_size = pch_zone.bytes - (ptr - pch_zone.page);
1626*e4b17023SJohn Marino       return zone_object_size_1 (pch_zone.alloc_bits, alloc_word, alloc_bit,
1627*e4b17023SJohn Marino 				 max_size);
1628*e4b17023SJohn Marino     }
1629*e4b17023SJohn Marino 
1630*e4b17023SJohn Marino   if (page->large_p)
1631*e4b17023SJohn Marino     return ((struct large_page_entry *)page)->bytes;
1632*e4b17023SJohn Marino   else
1633*e4b17023SJohn Marino     return zone_find_object_size ((struct small_page_entry *) page, p);
1634*e4b17023SJohn Marino }
1635*e4b17023SJohn Marino 
1636*e4b17023SJohn Marino /* Initialize the ggc-zone-mmap allocator.  */
1637*e4b17023SJohn Marino void
init_ggc(void)1638*e4b17023SJohn Marino init_ggc (void)
1639*e4b17023SJohn Marino {
1640*e4b17023SJohn Marino   /* The allocation size must be greater than BYTES_PER_MARK_BIT, and
1641*e4b17023SJohn Marino      a multiple of both BYTES_PER_ALLOC_BIT and FREE_BIN_DELTA, for
1642*e4b17023SJohn Marino      the current assumptions to hold.  */
1643*e4b17023SJohn Marino 
1644*e4b17023SJohn Marino   gcc_assert (FREE_BIN_DELTA == MAX_ALIGNMENT);
1645*e4b17023SJohn Marino 
1646*e4b17023SJohn Marino   /* Set up the main zone by hand.  */
1647*e4b17023SJohn Marino   main_zone.name = "Main zone";
1648*e4b17023SJohn Marino   G.zones = &main_zone;
1649*e4b17023SJohn Marino 
1650*e4b17023SJohn Marino   /* Allocate the default zones.  */
1651*e4b17023SJohn Marino   new_ggc_zone_1 (&rtl_zone, "RTL zone");
1652*e4b17023SJohn Marino   new_ggc_zone_1 (&tree_zone, "Tree zone");
1653*e4b17023SJohn Marino   new_ggc_zone_1 (&tree_id_zone, "Tree identifier zone");
1654*e4b17023SJohn Marino 
1655*e4b17023SJohn Marino   G.pagesize = getpagesize();
1656*e4b17023SJohn Marino   G.lg_pagesize = exact_log2 (G.pagesize);
1657*e4b17023SJohn Marino   G.page_mask = ~(G.pagesize - 1);
1658*e4b17023SJohn Marino 
1659*e4b17023SJohn Marino   /* Require the system page size to be a multiple of GGC_PAGE_SIZE.  */
1660*e4b17023SJohn Marino   gcc_assert ((G.pagesize & (GGC_PAGE_SIZE - 1)) == 0);
1661*e4b17023SJohn Marino 
1662*e4b17023SJohn Marino   /* Allocate 16 system pages at a time.  */
1663*e4b17023SJohn Marino   G.quire_size = 16 * G.pagesize / GGC_PAGE_SIZE;
1664*e4b17023SJohn Marino 
1665*e4b17023SJohn Marino   /* Calculate the size of the allocation bitmap and other overhead.  */
1666*e4b17023SJohn Marino   /* Right now we allocate bits for the page header and bitmap.  These
1667*e4b17023SJohn Marino      are wasted, but a little tricky to eliminate.  */
1668*e4b17023SJohn Marino   G.small_page_overhead
1669*e4b17023SJohn Marino     = PAGE_OVERHEAD + (GGC_PAGE_SIZE / BYTES_PER_ALLOC_BIT / 8);
1670*e4b17023SJohn Marino   /* G.small_page_overhead = ROUND_UP (G.small_page_overhead, MAX_ALIGNMENT); */
1671*e4b17023SJohn Marino 
1672*e4b17023SJohn Marino #ifdef HAVE_MMAP_DEV_ZERO
1673*e4b17023SJohn Marino   G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1674*e4b17023SJohn Marino   gcc_assert (G.dev_zero_fd != -1);
1675*e4b17023SJohn Marino #endif
1676*e4b17023SJohn Marino 
1677*e4b17023SJohn Marino #if 0
1678*e4b17023SJohn Marino   G.debug_file = fopen ("ggc-mmap.debug", "w");
1679*e4b17023SJohn Marino   setlinebuf (G.debug_file);
1680*e4b17023SJohn Marino #else
1681*e4b17023SJohn Marino   G.debug_file = stdout;
1682*e4b17023SJohn Marino #endif
1683*e4b17023SJohn Marino 
1684*e4b17023SJohn Marino #ifdef USING_MMAP
1685*e4b17023SJohn Marino   /* StunOS has an amazing off-by-one error for the first mmap allocation
1686*e4b17023SJohn Marino      after fiddling with RLIMIT_STACK.  The result, as hard as it is to
1687*e4b17023SJohn Marino      believe, is an unaligned page allocation, which would cause us to
1688*e4b17023SJohn Marino      hork badly if we tried to use it.  */
1689*e4b17023SJohn Marino   {
1690*e4b17023SJohn Marino     char *p = alloc_anon (NULL, G.pagesize, &main_zone);
1691*e4b17023SJohn Marino     struct small_page_entry *e;
1692*e4b17023SJohn Marino     if ((size_t)p & (G.pagesize - 1))
1693*e4b17023SJohn Marino       {
1694*e4b17023SJohn Marino 	/* How losing.  Discard this one and try another.  If we still
1695*e4b17023SJohn Marino 	   can't get something useful, give up.  */
1696*e4b17023SJohn Marino 
1697*e4b17023SJohn Marino 	p = alloc_anon (NULL, G.pagesize, &main_zone);
1698*e4b17023SJohn Marino 	gcc_assert (!((size_t)p & (G.pagesize - 1)));
1699*e4b17023SJohn Marino       }
1700*e4b17023SJohn Marino 
1701*e4b17023SJohn Marino     if (GGC_PAGE_SIZE == G.pagesize)
1702*e4b17023SJohn Marino       {
1703*e4b17023SJohn Marino 	/* We have a good page, might as well hold onto it...  */
1704*e4b17023SJohn Marino 	e = XCNEWVAR (struct small_page_entry, G.small_page_overhead);
1705*e4b17023SJohn Marino 	e->common.page = p;
1706*e4b17023SJohn Marino 	e->common.zone = &main_zone;
1707*e4b17023SJohn Marino 	e->next = main_zone.free_pages;
1708*e4b17023SJohn Marino 	set_page_table_entry (e->common.page, &e->common);
1709*e4b17023SJohn Marino 	main_zone.free_pages = e;
1710*e4b17023SJohn Marino       }
1711*e4b17023SJohn Marino     else
1712*e4b17023SJohn Marino       {
1713*e4b17023SJohn Marino 	munmap (p, G.pagesize);
1714*e4b17023SJohn Marino       }
1715*e4b17023SJohn Marino   }
1716*e4b17023SJohn Marino #endif
1717*e4b17023SJohn Marino }
1718*e4b17023SJohn Marino 
1719*e4b17023SJohn Marino /* Start a new GGC zone.  */
1720*e4b17023SJohn Marino 
1721*e4b17023SJohn Marino static void
new_ggc_zone_1(struct alloc_zone * new_zone,const char * name)1722*e4b17023SJohn Marino new_ggc_zone_1 (struct alloc_zone *new_zone, const char * name)
1723*e4b17023SJohn Marino {
1724*e4b17023SJohn Marino   new_zone->name = name;
1725*e4b17023SJohn Marino   new_zone->next_zone = G.zones->next_zone;
1726*e4b17023SJohn Marino   G.zones->next_zone = new_zone;
1727*e4b17023SJohn Marino }
1728*e4b17023SJohn Marino 
1729*e4b17023SJohn Marino /* Free all empty pages and objects within a page for a given zone  */
1730*e4b17023SJohn Marino 
1731*e4b17023SJohn Marino static void
sweep_pages(struct alloc_zone * zone)1732*e4b17023SJohn Marino sweep_pages (struct alloc_zone *zone)
1733*e4b17023SJohn Marino {
1734*e4b17023SJohn Marino   struct large_page_entry **lpp, *lp, *lnext;
1735*e4b17023SJohn Marino   struct small_page_entry **spp, *sp, *snext;
1736*e4b17023SJohn Marino   char *last_free;
1737*e4b17023SJohn Marino   size_t allocated = 0;
1738*e4b17023SJohn Marino   bool nomarksinpage;
1739*e4b17023SJohn Marino 
1740*e4b17023SJohn Marino   /* First, reset the free_chunks lists, since we are going to
1741*e4b17023SJohn Marino      re-free free chunks in hopes of coalescing them into large chunks.  */
1742*e4b17023SJohn Marino   memset (zone->free_chunks, 0, sizeof (zone->free_chunks));
1743*e4b17023SJohn Marino   zone->high_free_bin = 0;
1744*e4b17023SJohn Marino   zone->cached_free = NULL;
1745*e4b17023SJohn Marino   zone->cached_free_size = 0;
1746*e4b17023SJohn Marino 
1747*e4b17023SJohn Marino   /* Large pages are all or none affairs. Either they are completely
1748*e4b17023SJohn Marino      empty, or they are completely full.  */
1749*e4b17023SJohn Marino   lpp = &zone->large_pages;
1750*e4b17023SJohn Marino   for (lp = zone->large_pages; lp != NULL; lp = lnext)
1751*e4b17023SJohn Marino     {
1752*e4b17023SJohn Marino       gcc_assert (lp->common.large_p);
1753*e4b17023SJohn Marino 
1754*e4b17023SJohn Marino       lnext = lp->next;
1755*e4b17023SJohn Marino 
1756*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
1757*e4b17023SJohn Marino       /* This page has now survived another collection.  */
1758*e4b17023SJohn Marino       lp->common.survived++;
1759*e4b17023SJohn Marino #endif
1760*e4b17023SJohn Marino 
1761*e4b17023SJohn Marino       if (lp->mark_p)
1762*e4b17023SJohn Marino 	{
1763*e4b17023SJohn Marino 	  lp->mark_p = false;
1764*e4b17023SJohn Marino 	  allocated += lp->bytes;
1765*e4b17023SJohn Marino 	  lpp = &lp->next;
1766*e4b17023SJohn Marino 	}
1767*e4b17023SJohn Marino       else
1768*e4b17023SJohn Marino 	{
1769*e4b17023SJohn Marino 	  *lpp = lnext;
1770*e4b17023SJohn Marino #ifdef ENABLE_GC_CHECKING
1771*e4b17023SJohn Marino 	  /* Poison the page.  */
1772*e4b17023SJohn Marino 	  memset (lp->common.page, 0xb5, SMALL_PAGE_SIZE);
1773*e4b17023SJohn Marino #endif
1774*e4b17023SJohn Marino 	  if (lp->prev)
1775*e4b17023SJohn Marino 	    lp->prev->next = lp->next;
1776*e4b17023SJohn Marino 	  if (lp->next)
1777*e4b17023SJohn Marino 	    lp->next->prev = lp->prev;
1778*e4b17023SJohn Marino 	  free_large_page (lp);
1779*e4b17023SJohn Marino 	}
1780*e4b17023SJohn Marino     }
1781*e4b17023SJohn Marino 
1782*e4b17023SJohn Marino   spp = &zone->pages;
1783*e4b17023SJohn Marino   for (sp = zone->pages; sp != NULL; sp = snext)
1784*e4b17023SJohn Marino     {
1785*e4b17023SJohn Marino       char *object, *last_object;
1786*e4b17023SJohn Marino       char *end;
1787*e4b17023SJohn Marino       alloc_type *alloc_word_p;
1788*e4b17023SJohn Marino       mark_type *mark_word_p;
1789*e4b17023SJohn Marino 
1790*e4b17023SJohn Marino       gcc_assert (!sp->common.large_p);
1791*e4b17023SJohn Marino 
1792*e4b17023SJohn Marino       snext = sp->next;
1793*e4b17023SJohn Marino 
1794*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
1795*e4b17023SJohn Marino       /* This page has now survived another collection.  */
1796*e4b17023SJohn Marino       sp->common.survived++;
1797*e4b17023SJohn Marino #endif
1798*e4b17023SJohn Marino 
1799*e4b17023SJohn Marino       /* Step through all chunks, consolidate those that are free and
1800*e4b17023SJohn Marino 	 insert them into the free lists.  Note that consolidation
1801*e4b17023SJohn Marino 	 slows down collection slightly.  */
1802*e4b17023SJohn Marino 
1803*e4b17023SJohn Marino       last_object = object = sp->common.page;
1804*e4b17023SJohn Marino       end = sp->common.page + SMALL_PAGE_SIZE;
1805*e4b17023SJohn Marino       last_free = NULL;
1806*e4b17023SJohn Marino       nomarksinpage = true;
1807*e4b17023SJohn Marino       mark_word_p = sp->mark_bits;
1808*e4b17023SJohn Marino       alloc_word_p = sp->alloc_bits;
1809*e4b17023SJohn Marino 
1810*e4b17023SJohn Marino       gcc_assert (BYTES_PER_ALLOC_BIT == BYTES_PER_MARK_BIT);
1811*e4b17023SJohn Marino 
1812*e4b17023SJohn Marino       object = sp->common.page;
1813*e4b17023SJohn Marino       do
1814*e4b17023SJohn Marino 	{
1815*e4b17023SJohn Marino 	  unsigned int i, n;
1816*e4b17023SJohn Marino 	  alloc_type alloc_word;
1817*e4b17023SJohn Marino 	  mark_type mark_word;
1818*e4b17023SJohn Marino 
1819*e4b17023SJohn Marino 	  alloc_word = *alloc_word_p++;
1820*e4b17023SJohn Marino 	  mark_word = *mark_word_p++;
1821*e4b17023SJohn Marino 
1822*e4b17023SJohn Marino 	  if (mark_word)
1823*e4b17023SJohn Marino 	    nomarksinpage = false;
1824*e4b17023SJohn Marino 
1825*e4b17023SJohn Marino 	  /* There ought to be some way to do this without looping...  */
1826*e4b17023SJohn Marino 	  i = 0;
1827*e4b17023SJohn Marino 	  while ((n = alloc_ffs (alloc_word)) != 0)
1828*e4b17023SJohn Marino 	    {
1829*e4b17023SJohn Marino 	      /* Extend the current state for n - 1 bits.  We can't
1830*e4b17023SJohn Marino 		 shift alloc_word by n, even though it isn't used in the
1831*e4b17023SJohn Marino 		 loop, in case only the highest bit was set.  */
1832*e4b17023SJohn Marino 	      alloc_word >>= n - 1;
1833*e4b17023SJohn Marino 	      mark_word >>= n - 1;
1834*e4b17023SJohn Marino 	      object += BYTES_PER_MARK_BIT * (n - 1);
1835*e4b17023SJohn Marino 
1836*e4b17023SJohn Marino 	      if (mark_word & 1)
1837*e4b17023SJohn Marino 		{
1838*e4b17023SJohn Marino 		  if (last_free)
1839*e4b17023SJohn Marino 		    {
1840*e4b17023SJohn Marino 		      VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (last_free,
1841*e4b17023SJohn Marino 								     object
1842*e4b17023SJohn Marino 								     - last_free));
1843*e4b17023SJohn Marino 		      poison_region (last_free, object - last_free);
1844*e4b17023SJohn Marino 		      free_chunk (last_free, object - last_free, zone);
1845*e4b17023SJohn Marino 		      last_free = NULL;
1846*e4b17023SJohn Marino 		    }
1847*e4b17023SJohn Marino 		  else
1848*e4b17023SJohn Marino 		    allocated += object - last_object;
1849*e4b17023SJohn Marino 		  last_object = object;
1850*e4b17023SJohn Marino 		}
1851*e4b17023SJohn Marino 	      else
1852*e4b17023SJohn Marino 		{
1853*e4b17023SJohn Marino 		  if (last_free == NULL)
1854*e4b17023SJohn Marino 		    {
1855*e4b17023SJohn Marino 		      last_free = object;
1856*e4b17023SJohn Marino 		      allocated += object - last_object;
1857*e4b17023SJohn Marino 		    }
1858*e4b17023SJohn Marino 		  else
1859*e4b17023SJohn Marino 		    zone_clear_object_alloc_bit (sp, object);
1860*e4b17023SJohn Marino 		}
1861*e4b17023SJohn Marino 
1862*e4b17023SJohn Marino 	      /* Shift to just after the alloc bit we handled.  */
1863*e4b17023SJohn Marino 	      alloc_word >>= 1;
1864*e4b17023SJohn Marino 	      mark_word >>= 1;
1865*e4b17023SJohn Marino 	      object += BYTES_PER_MARK_BIT;
1866*e4b17023SJohn Marino 
1867*e4b17023SJohn Marino 	      i += n;
1868*e4b17023SJohn Marino 	    }
1869*e4b17023SJohn Marino 
1870*e4b17023SJohn Marino 	  object += BYTES_PER_MARK_BIT * (8 * sizeof (alloc_type) - i);
1871*e4b17023SJohn Marino 	}
1872*e4b17023SJohn Marino       while (object < end);
1873*e4b17023SJohn Marino 
1874*e4b17023SJohn Marino       if (nomarksinpage)
1875*e4b17023SJohn Marino 	{
1876*e4b17023SJohn Marino 	  *spp = snext;
1877*e4b17023SJohn Marino #ifdef ENABLE_GC_CHECKING
1878*e4b17023SJohn Marino 	  VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (sp->common.page,
1879*e4b17023SJohn Marino 							 SMALL_PAGE_SIZE));
1880*e4b17023SJohn Marino 	  /* Poison the page.  */
1881*e4b17023SJohn Marino 	  memset (sp->common.page, 0xb5, SMALL_PAGE_SIZE);
1882*e4b17023SJohn Marino #endif
1883*e4b17023SJohn Marino 	  free_small_page (sp);
1884*e4b17023SJohn Marino 	  continue;
1885*e4b17023SJohn Marino 	}
1886*e4b17023SJohn Marino       else if (last_free)
1887*e4b17023SJohn Marino 	{
1888*e4b17023SJohn Marino 	  VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (last_free,
1889*e4b17023SJohn Marino 							 object - last_free));
1890*e4b17023SJohn Marino 	  poison_region (last_free, object - last_free);
1891*e4b17023SJohn Marino 	  free_chunk (last_free, object - last_free, zone);
1892*e4b17023SJohn Marino 	}
1893*e4b17023SJohn Marino       else
1894*e4b17023SJohn Marino 	allocated += object - last_object;
1895*e4b17023SJohn Marino 
1896*e4b17023SJohn Marino       spp = &sp->next;
1897*e4b17023SJohn Marino     }
1898*e4b17023SJohn Marino 
1899*e4b17023SJohn Marino   zone->allocated = allocated;
1900*e4b17023SJohn Marino }
1901*e4b17023SJohn Marino 
1902*e4b17023SJohn Marino /* mark-and-sweep routine for collecting a single zone.  NEED_MARKING
1903*e4b17023SJohn Marino    is true if we need to mark before sweeping, false if some other
1904*e4b17023SJohn Marino    zone collection has already performed marking for us.  Returns true
1905*e4b17023SJohn Marino    if we collected, false otherwise.  */
1906*e4b17023SJohn Marino 
1907*e4b17023SJohn Marino static bool
ggc_collect_1(struct alloc_zone * zone,bool need_marking)1908*e4b17023SJohn Marino ggc_collect_1 (struct alloc_zone *zone, bool need_marking)
1909*e4b17023SJohn Marino {
1910*e4b17023SJohn Marino #if 0
1911*e4b17023SJohn Marino   /* */
1912*e4b17023SJohn Marino   {
1913*e4b17023SJohn Marino     int i;
1914*e4b17023SJohn Marino     for (i = 0; i < NUM_FREE_BINS + 1; i++)
1915*e4b17023SJohn Marino       {
1916*e4b17023SJohn Marino 	struct alloc_chunk *chunk;
1917*e4b17023SJohn Marino 	int n, tot;
1918*e4b17023SJohn Marino 
1919*e4b17023SJohn Marino 	n = 0;
1920*e4b17023SJohn Marino 	tot = 0;
1921*e4b17023SJohn Marino 	chunk = zone->free_chunks[i];
1922*e4b17023SJohn Marino 	while (chunk)
1923*e4b17023SJohn Marino 	  {
1924*e4b17023SJohn Marino 	    n++;
1925*e4b17023SJohn Marino 	    tot += chunk->size;
1926*e4b17023SJohn Marino 	    chunk = chunk->next_free;
1927*e4b17023SJohn Marino 	  }
1928*e4b17023SJohn Marino 	fprintf (stderr, "Bin %d: %d free chunks (%d bytes)\n",
1929*e4b17023SJohn Marino 		 i, n, tot);
1930*e4b17023SJohn Marino       }
1931*e4b17023SJohn Marino   }
1932*e4b17023SJohn Marino   /* */
1933*e4b17023SJohn Marino #endif
1934*e4b17023SJohn Marino 
1935*e4b17023SJohn Marino   if (!quiet_flag)
1936*e4b17023SJohn Marino     fprintf (stderr, " {%s GC %luk -> ",
1937*e4b17023SJohn Marino 	     zone->name, (unsigned long) zone->allocated / 1024);
1938*e4b17023SJohn Marino 
1939*e4b17023SJohn Marino   /* Zero the total allocated bytes.  This will be recalculated in the
1940*e4b17023SJohn Marino      sweep phase.  */
1941*e4b17023SJohn Marino   zone->allocated = 0;
1942*e4b17023SJohn Marino 
1943*e4b17023SJohn Marino   /* Release the pages we freed the last time we collected, but didn't
1944*e4b17023SJohn Marino      reuse in the interim.  */
1945*e4b17023SJohn Marino   release_pages (zone);
1946*e4b17023SJohn Marino 
1947*e4b17023SJohn Marino   if (need_marking)
1948*e4b17023SJohn Marino     {
1949*e4b17023SJohn Marino       zone_allocate_marks ();
1950*e4b17023SJohn Marino       ggc_mark_roots ();
1951*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
1952*e4b17023SJohn Marino       ggc_prune_overhead_list ();
1953*e4b17023SJohn Marino #endif
1954*e4b17023SJohn Marino     }
1955*e4b17023SJohn Marino 
1956*e4b17023SJohn Marino   sweep_pages (zone);
1957*e4b17023SJohn Marino   zone->was_collected = true;
1958*e4b17023SJohn Marino   zone->allocated_last_gc = zone->allocated;
1959*e4b17023SJohn Marino 
1960*e4b17023SJohn Marino   if (!quiet_flag)
1961*e4b17023SJohn Marino     fprintf (stderr, "%luk}", (unsigned long) zone->allocated / 1024);
1962*e4b17023SJohn Marino   return true;
1963*e4b17023SJohn Marino }
1964*e4b17023SJohn Marino 
1965*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
1966*e4b17023SJohn Marino /* Calculate the average page survival rate in terms of number of
1967*e4b17023SJohn Marino    collections.  */
1968*e4b17023SJohn Marino 
1969*e4b17023SJohn Marino static float
calculate_average_page_survival(struct alloc_zone * zone)1970*e4b17023SJohn Marino calculate_average_page_survival (struct alloc_zone *zone)
1971*e4b17023SJohn Marino {
1972*e4b17023SJohn Marino   float count = 0.0;
1973*e4b17023SJohn Marino   float survival = 0.0;
1974*e4b17023SJohn Marino   struct small_page_entry *p;
1975*e4b17023SJohn Marino   struct large_page_entry *lp;
1976*e4b17023SJohn Marino   for (p = zone->pages; p; p = p->next)
1977*e4b17023SJohn Marino     {
1978*e4b17023SJohn Marino       count += 1.0;
1979*e4b17023SJohn Marino       survival += p->common.survived;
1980*e4b17023SJohn Marino     }
1981*e4b17023SJohn Marino   for (lp = zone->large_pages; lp; lp = lp->next)
1982*e4b17023SJohn Marino     {
1983*e4b17023SJohn Marino       count += 1.0;
1984*e4b17023SJohn Marino       survival += lp->common.survived;
1985*e4b17023SJohn Marino     }
1986*e4b17023SJohn Marino   return survival/count;
1987*e4b17023SJohn Marino }
1988*e4b17023SJohn Marino #endif
1989*e4b17023SJohn Marino 
1990*e4b17023SJohn Marino /* Top level collection routine.  */
1991*e4b17023SJohn Marino 
1992*e4b17023SJohn Marino void
ggc_collect(void)1993*e4b17023SJohn Marino ggc_collect (void)
1994*e4b17023SJohn Marino {
1995*e4b17023SJohn Marino   struct alloc_zone *zone;
1996*e4b17023SJohn Marino   bool marked = false;
1997*e4b17023SJohn Marino 
1998*e4b17023SJohn Marino   timevar_push (TV_GC);
1999*e4b17023SJohn Marino 
2000*e4b17023SJohn Marino   if (!ggc_force_collect)
2001*e4b17023SJohn Marino     {
2002*e4b17023SJohn Marino       float allocated_last_gc = 0, allocated = 0, min_expand;
2003*e4b17023SJohn Marino 
2004*e4b17023SJohn Marino       for (zone = G.zones; zone; zone = zone->next_zone)
2005*e4b17023SJohn Marino 	{
2006*e4b17023SJohn Marino 	  allocated_last_gc += zone->allocated_last_gc;
2007*e4b17023SJohn Marino 	  allocated += zone->allocated;
2008*e4b17023SJohn Marino 	}
2009*e4b17023SJohn Marino 
2010*e4b17023SJohn Marino       allocated_last_gc =
2011*e4b17023SJohn Marino 	MAX (allocated_last_gc,
2012*e4b17023SJohn Marino 	     (size_t) PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
2013*e4b17023SJohn Marino       min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
2014*e4b17023SJohn Marino 
2015*e4b17023SJohn Marino       if (allocated < allocated_last_gc + min_expand)
2016*e4b17023SJohn Marino 	{
2017*e4b17023SJohn Marino 	  timevar_pop (TV_GC);
2018*e4b17023SJohn Marino 	  return;
2019*e4b17023SJohn Marino 	}
2020*e4b17023SJohn Marino     }
2021*e4b17023SJohn Marino 
2022*e4b17023SJohn Marino   invoke_plugin_callbacks (PLUGIN_GGC_START, NULL);
2023*e4b17023SJohn Marino 
2024*e4b17023SJohn Marino   /* Start by possibly collecting the main zone.  */
2025*e4b17023SJohn Marino   main_zone.was_collected = false;
2026*e4b17023SJohn Marino   marked |= ggc_collect_1 (&main_zone, true);
2027*e4b17023SJohn Marino 
2028*e4b17023SJohn Marino   /* In order to keep the number of collections down, we don't
2029*e4b17023SJohn Marino      collect other zones unless we are collecting the main zone.  This
2030*e4b17023SJohn Marino      gives us roughly the same number of collections as we used to
2031*e4b17023SJohn Marino      have with the old gc.  The number of collection is important
2032*e4b17023SJohn Marino      because our main slowdown (according to profiling) is now in
2033*e4b17023SJohn Marino      marking.  So if we mark twice as often as we used to, we'll be
2034*e4b17023SJohn Marino      twice as slow.  Hopefully we'll avoid this cost when we mark
2035*e4b17023SJohn Marino      zone-at-a-time.  */
2036*e4b17023SJohn Marino   /* NOTE drow/2004-07-28: We now always collect the main zone, but
2037*e4b17023SJohn Marino      keep this code in case the heuristics are further refined.  */
2038*e4b17023SJohn Marino 
2039*e4b17023SJohn Marino   if (main_zone.was_collected)
2040*e4b17023SJohn Marino     {
2041*e4b17023SJohn Marino       struct alloc_zone *zone;
2042*e4b17023SJohn Marino 
2043*e4b17023SJohn Marino       for (zone = main_zone.next_zone; zone; zone = zone->next_zone)
2044*e4b17023SJohn Marino 	{
2045*e4b17023SJohn Marino 	  zone->was_collected = false;
2046*e4b17023SJohn Marino 	  marked |= ggc_collect_1 (zone, !marked);
2047*e4b17023SJohn Marino 	}
2048*e4b17023SJohn Marino     }
2049*e4b17023SJohn Marino 
2050*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
2051*e4b17023SJohn Marino   /* Print page survival stats, if someone wants them.  */
2052*e4b17023SJohn Marino   if (GGC_DEBUG_LEVEL >= 2)
2053*e4b17023SJohn Marino     {
2054*e4b17023SJohn Marino       for (zone = G.zones; zone; zone = zone->next_zone)
2055*e4b17023SJohn Marino 	{
2056*e4b17023SJohn Marino 	  if (zone->was_collected)
2057*e4b17023SJohn Marino 	    {
2058*e4b17023SJohn Marino 	      float f = calculate_average_page_survival (zone);
2059*e4b17023SJohn Marino 	      printf ("Average page survival in zone `%s' is %f\n",
2060*e4b17023SJohn Marino 		      zone->name, f);
2061*e4b17023SJohn Marino 	    }
2062*e4b17023SJohn Marino 	}
2063*e4b17023SJohn Marino     }
2064*e4b17023SJohn Marino #endif
2065*e4b17023SJohn Marino 
2066*e4b17023SJohn Marino   if (marked)
2067*e4b17023SJohn Marino     zone_free_marks ();
2068*e4b17023SJohn Marino 
2069*e4b17023SJohn Marino   /* Free dead zones.  */
2070*e4b17023SJohn Marino   for (zone = G.zones; zone && zone->next_zone; zone = zone->next_zone)
2071*e4b17023SJohn Marino     {
2072*e4b17023SJohn Marino       if (zone->next_zone->dead)
2073*e4b17023SJohn Marino 	{
2074*e4b17023SJohn Marino 	  struct alloc_zone *dead_zone = zone->next_zone;
2075*e4b17023SJohn Marino 
2076*e4b17023SJohn Marino 	  printf ("Zone `%s' is dead and will be freed.\n", dead_zone->name);
2077*e4b17023SJohn Marino 
2078*e4b17023SJohn Marino 	  /* The zone must be empty.  */
2079*e4b17023SJohn Marino 	  gcc_assert (!dead_zone->allocated);
2080*e4b17023SJohn Marino 
2081*e4b17023SJohn Marino 	  /* Unchain the dead zone, release all its pages and free it.  */
2082*e4b17023SJohn Marino 	  zone->next_zone = zone->next_zone->next_zone;
2083*e4b17023SJohn Marino 	  release_pages (dead_zone);
2084*e4b17023SJohn Marino 	  free (dead_zone);
2085*e4b17023SJohn Marino 	}
2086*e4b17023SJohn Marino     }
2087*e4b17023SJohn Marino 
2088*e4b17023SJohn Marino   invoke_plugin_callbacks (PLUGIN_GGC_END, NULL);
2089*e4b17023SJohn Marino 
2090*e4b17023SJohn Marino   timevar_pop (TV_GC);
2091*e4b17023SJohn Marino }
2092*e4b17023SJohn Marino 
2093*e4b17023SJohn Marino /* Print allocation statistics.  */
2094*e4b17023SJohn Marino #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
2095*e4b17023SJohn Marino 		  ? (x) \
2096*e4b17023SJohn Marino 		  : ((x) < 1024*1024*10 \
2097*e4b17023SJohn Marino 		     ? (x) / 1024 \
2098*e4b17023SJohn Marino 		     : (x) / (1024*1024))))
2099*e4b17023SJohn Marino #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
2100*e4b17023SJohn Marino 
2101*e4b17023SJohn Marino void
ggc_print_statistics(void)2102*e4b17023SJohn Marino ggc_print_statistics (void)
2103*e4b17023SJohn Marino {
2104*e4b17023SJohn Marino   struct alloc_zone *zone;
2105*e4b17023SJohn Marino   struct ggc_statistics stats;
2106*e4b17023SJohn Marino   size_t total_overhead = 0, total_allocated = 0, total_bytes_mapped = 0;
2107*e4b17023SJohn Marino   size_t pte_overhead, i;
2108*e4b17023SJohn Marino 
2109*e4b17023SJohn Marino   /* Clear the statistics.  */
2110*e4b17023SJohn Marino   memset (&stats, 0, sizeof (stats));
2111*e4b17023SJohn Marino 
2112*e4b17023SJohn Marino   /* Make sure collection will really occur.  */
2113*e4b17023SJohn Marino   ggc_force_collect = true;
2114*e4b17023SJohn Marino 
2115*e4b17023SJohn Marino   /* Collect and print the statistics common across collectors.  */
2116*e4b17023SJohn Marino   ggc_print_common_statistics (stderr, &stats);
2117*e4b17023SJohn Marino 
2118*e4b17023SJohn Marino   ggc_force_collect = false;
2119*e4b17023SJohn Marino 
2120*e4b17023SJohn Marino   /* Release free pages so that we will not count the bytes allocated
2121*e4b17023SJohn Marino      there as part of the total allocated memory.  */
2122*e4b17023SJohn Marino   for (zone = G.zones; zone; zone = zone->next_zone)
2123*e4b17023SJohn Marino     release_pages (zone);
2124*e4b17023SJohn Marino 
2125*e4b17023SJohn Marino   /* Collect some information about the various sizes of
2126*e4b17023SJohn Marino      allocation.  */
2127*e4b17023SJohn Marino   fprintf (stderr,
2128*e4b17023SJohn Marino            "Memory still allocated at the end of the compilation process\n");
2129*e4b17023SJohn Marino 
2130*e4b17023SJohn Marino   fprintf (stderr, "%20s %10s  %10s  %10s\n",
2131*e4b17023SJohn Marino 	   "Zone", "Allocated", "Used", "Overhead");
2132*e4b17023SJohn Marino   for (zone = G.zones; zone; zone = zone->next_zone)
2133*e4b17023SJohn Marino     {
2134*e4b17023SJohn Marino       struct large_page_entry *large_page;
2135*e4b17023SJohn Marino       size_t overhead, allocated, in_use;
2136*e4b17023SJohn Marino 
2137*e4b17023SJohn Marino       /* Skip empty zones.  */
2138*e4b17023SJohn Marino       if (!zone->pages && !zone->large_pages)
2139*e4b17023SJohn Marino 	continue;
2140*e4b17023SJohn Marino 
2141*e4b17023SJohn Marino       allocated = in_use = 0;
2142*e4b17023SJohn Marino 
2143*e4b17023SJohn Marino       overhead = sizeof (struct alloc_zone);
2144*e4b17023SJohn Marino 
2145*e4b17023SJohn Marino       for (large_page = zone->large_pages; large_page != NULL;
2146*e4b17023SJohn Marino 	   large_page = large_page->next)
2147*e4b17023SJohn Marino 	{
2148*e4b17023SJohn Marino 	  allocated += large_page->bytes;
2149*e4b17023SJohn Marino 	  in_use += large_page->bytes;
2150*e4b17023SJohn Marino 	  overhead += sizeof (struct large_page_entry);
2151*e4b17023SJohn Marino 	}
2152*e4b17023SJohn Marino 
2153*e4b17023SJohn Marino       /* There's no easy way to walk through the small pages finding
2154*e4b17023SJohn Marino 	 used and unused objects.  Instead, add all the pages, and
2155*e4b17023SJohn Marino 	 subtract out the free list.  */
2156*e4b17023SJohn Marino 
2157*e4b17023SJohn Marino       allocated += GGC_PAGE_SIZE * zone->n_small_pages;
2158*e4b17023SJohn Marino       in_use += GGC_PAGE_SIZE * zone->n_small_pages;
2159*e4b17023SJohn Marino       overhead += G.small_page_overhead * zone->n_small_pages;
2160*e4b17023SJohn Marino 
2161*e4b17023SJohn Marino       for (i = 0; i <= NUM_FREE_BINS; i++)
2162*e4b17023SJohn Marino 	{
2163*e4b17023SJohn Marino 	  struct alloc_chunk *chunk = zone->free_chunks[i];
2164*e4b17023SJohn Marino 	  while (chunk)
2165*e4b17023SJohn Marino 	    {
2166*e4b17023SJohn Marino 	      in_use -= ggc_get_size (chunk);
2167*e4b17023SJohn Marino 	      chunk = chunk->next_free;
2168*e4b17023SJohn Marino 	    }
2169*e4b17023SJohn Marino 	}
2170*e4b17023SJohn Marino 
2171*e4b17023SJohn Marino       fprintf (stderr, "%20s %10lu%c %10lu%c %10lu%c\n",
2172*e4b17023SJohn Marino 	       zone->name,
2173*e4b17023SJohn Marino 	       SCALE (allocated), LABEL (allocated),
2174*e4b17023SJohn Marino 	       SCALE (in_use), LABEL (in_use),
2175*e4b17023SJohn Marino 	       SCALE (overhead), LABEL (overhead));
2176*e4b17023SJohn Marino 
2177*e4b17023SJohn Marino       gcc_assert (in_use == zone->allocated);
2178*e4b17023SJohn Marino 
2179*e4b17023SJohn Marino       total_overhead += overhead;
2180*e4b17023SJohn Marino       total_allocated += zone->allocated;
2181*e4b17023SJohn Marino       total_bytes_mapped += zone->bytes_mapped;
2182*e4b17023SJohn Marino     }
2183*e4b17023SJohn Marino 
2184*e4b17023SJohn Marino   /* Count the size of the page table as best we can.  */
2185*e4b17023SJohn Marino #if HOST_BITS_PER_PTR <= 32
2186*e4b17023SJohn Marino   pte_overhead = sizeof (G.lookup);
2187*e4b17023SJohn Marino   for (i = 0; i < PAGE_L1_SIZE; i++)
2188*e4b17023SJohn Marino     if (G.lookup[i])
2189*e4b17023SJohn Marino       pte_overhead += PAGE_L2_SIZE * sizeof (struct page_entry *);
2190*e4b17023SJohn Marino #else
2191*e4b17023SJohn Marino   {
2192*e4b17023SJohn Marino     page_table table = G.lookup;
2193*e4b17023SJohn Marino     pte_overhead = 0;
2194*e4b17023SJohn Marino     while (table)
2195*e4b17023SJohn Marino       {
2196*e4b17023SJohn Marino 	pte_overhead += sizeof (*table);
2197*e4b17023SJohn Marino 	for (i = 0; i < PAGE_L1_SIZE; i++)
2198*e4b17023SJohn Marino 	  if (table->table[i])
2199*e4b17023SJohn Marino 	    pte_overhead += PAGE_L2_SIZE * sizeof (struct page_entry *);
2200*e4b17023SJohn Marino 	table = table->next;
2201*e4b17023SJohn Marino       }
2202*e4b17023SJohn Marino   }
2203*e4b17023SJohn Marino #endif
2204*e4b17023SJohn Marino   fprintf (stderr, "%20s %11s %11s %10lu%c\n", "Page Table",
2205*e4b17023SJohn Marino 	   "", "", SCALE (pte_overhead), LABEL (pte_overhead));
2206*e4b17023SJohn Marino   total_overhead += pte_overhead;
2207*e4b17023SJohn Marino 
2208*e4b17023SJohn Marino   fprintf (stderr, "%20s %10lu%c %10lu%c %10lu%c\n", "Total",
2209*e4b17023SJohn Marino 	   SCALE (total_bytes_mapped), LABEL (total_bytes_mapped),
2210*e4b17023SJohn Marino 	   SCALE (total_allocated), LABEL(total_allocated),
2211*e4b17023SJohn Marino 	   SCALE (total_overhead), LABEL (total_overhead));
2212*e4b17023SJohn Marino 
2213*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
2214*e4b17023SJohn Marino   {
2215*e4b17023SJohn Marino     unsigned long long all_overhead = 0, all_allocated = 0;
2216*e4b17023SJohn Marino     unsigned long long all_overhead_under32 = 0, all_allocated_under32 = 0;
2217*e4b17023SJohn Marino     unsigned long long all_overhead_under64 = 0, all_allocated_under64 = 0;
2218*e4b17023SJohn Marino     unsigned long long all_overhead_under128 = 0, all_allocated_under128 = 0;
2219*e4b17023SJohn Marino 
2220*e4b17023SJohn Marino     fprintf (stderr, "\nTotal allocations and overheads during the compilation process\n");
2221*e4b17023SJohn Marino 
2222*e4b17023SJohn Marino     for (zone = G.zones; zone; zone = zone->next_zone)
2223*e4b17023SJohn Marino       {
2224*e4b17023SJohn Marino 	all_overhead += zone->stats.total_overhead;
2225*e4b17023SJohn Marino 	all_allocated += zone->stats.total_allocated;
2226*e4b17023SJohn Marino 
2227*e4b17023SJohn Marino 	all_allocated_under32 += zone->stats.total_allocated_under32;
2228*e4b17023SJohn Marino 	all_overhead_under32 += zone->stats.total_overhead_under32;
2229*e4b17023SJohn Marino 
2230*e4b17023SJohn Marino 	all_allocated_under64 += zone->stats.total_allocated_under64;
2231*e4b17023SJohn Marino 	all_overhead_under64 += zone->stats.total_overhead_under64;
2232*e4b17023SJohn Marino 
2233*e4b17023SJohn Marino 	all_allocated_under128 += zone->stats.total_allocated_under128;
2234*e4b17023SJohn Marino 	all_overhead_under128 += zone->stats.total_overhead_under128;
2235*e4b17023SJohn Marino 
2236*e4b17023SJohn Marino 	fprintf (stderr, "%20s:                  %10lld\n",
2237*e4b17023SJohn Marino 		 zone->name, zone->stats.total_allocated);
2238*e4b17023SJohn Marino       }
2239*e4b17023SJohn Marino 
2240*e4b17023SJohn Marino     fprintf (stderr, "\n");
2241*e4b17023SJohn Marino 
2242*e4b17023SJohn Marino     fprintf (stderr, "Total Overhead:                        %10lld\n",
2243*e4b17023SJohn Marino              all_overhead);
2244*e4b17023SJohn Marino     fprintf (stderr, "Total Allocated:                       %10lld\n",
2245*e4b17023SJohn Marino              all_allocated);
2246*e4b17023SJohn Marino 
2247*e4b17023SJohn Marino     fprintf (stderr, "Total Overhead  under  32B:            %10lld\n",
2248*e4b17023SJohn Marino              all_overhead_under32);
2249*e4b17023SJohn Marino     fprintf (stderr, "Total Allocated under  32B:            %10lld\n",
2250*e4b17023SJohn Marino              all_allocated_under32);
2251*e4b17023SJohn Marino     fprintf (stderr, "Total Overhead  under  64B:            %10lld\n",
2252*e4b17023SJohn Marino              all_overhead_under64);
2253*e4b17023SJohn Marino     fprintf (stderr, "Total Allocated under  64B:            %10lld\n",
2254*e4b17023SJohn Marino              all_allocated_under64);
2255*e4b17023SJohn Marino     fprintf (stderr, "Total Overhead  under 128B:            %10lld\n",
2256*e4b17023SJohn Marino              all_overhead_under128);
2257*e4b17023SJohn Marino     fprintf (stderr, "Total Allocated under 128B:            %10lld\n",
2258*e4b17023SJohn Marino              all_allocated_under128);
2259*e4b17023SJohn Marino   }
2260*e4b17023SJohn Marino #endif
2261*e4b17023SJohn Marino }
2262*e4b17023SJohn Marino 
2263*e4b17023SJohn Marino /* Precompiled header support.  */
2264*e4b17023SJohn Marino 
2265*e4b17023SJohn Marino /* For precompiled headers, we sort objects based on their type.  We
2266*e4b17023SJohn Marino    also sort various objects into their own buckets; currently this
2267*e4b17023SJohn Marino    covers strings and IDENTIFIER_NODE trees.  The choices of how
2268*e4b17023SJohn Marino    to sort buckets have not yet been tuned.  */
2269*e4b17023SJohn Marino 
2270*e4b17023SJohn Marino #define NUM_PCH_BUCKETS		(gt_types_enum_last + 3)
2271*e4b17023SJohn Marino 
2272*e4b17023SJohn Marino #define OTHER_BUCKET		(gt_types_enum_last + 0)
2273*e4b17023SJohn Marino #define IDENTIFIER_BUCKET	(gt_types_enum_last + 1)
2274*e4b17023SJohn Marino #define STRING_BUCKET		(gt_types_enum_last + 2)
2275*e4b17023SJohn Marino 
2276*e4b17023SJohn Marino struct ggc_pch_ondisk
2277*e4b17023SJohn Marino {
2278*e4b17023SJohn Marino   size_t total;
2279*e4b17023SJohn Marino   size_t type_totals[NUM_PCH_BUCKETS];
2280*e4b17023SJohn Marino };
2281*e4b17023SJohn Marino 
2282*e4b17023SJohn Marino struct ggc_pch_data
2283*e4b17023SJohn Marino {
2284*e4b17023SJohn Marino   struct ggc_pch_ondisk d;
2285*e4b17023SJohn Marino   size_t base;
2286*e4b17023SJohn Marino   size_t orig_base;
2287*e4b17023SJohn Marino   size_t alloc_size;
2288*e4b17023SJohn Marino   alloc_type *alloc_bits;
2289*e4b17023SJohn Marino   size_t type_bases[NUM_PCH_BUCKETS];
2290*e4b17023SJohn Marino   size_t start_offset;
2291*e4b17023SJohn Marino };
2292*e4b17023SJohn Marino 
2293*e4b17023SJohn Marino /* Initialize the PCH data structure.  */
2294*e4b17023SJohn Marino 
2295*e4b17023SJohn Marino struct ggc_pch_data *
init_ggc_pch(void)2296*e4b17023SJohn Marino init_ggc_pch (void)
2297*e4b17023SJohn Marino {
2298*e4b17023SJohn Marino   return XCNEW (struct ggc_pch_data);
2299*e4b17023SJohn Marino }
2300*e4b17023SJohn Marino 
2301*e4b17023SJohn Marino /* Return which of the page-aligned buckets the object at X, with type
2302*e4b17023SJohn Marino    TYPE, should be sorted into in the PCH.  Strings will have
2303*e4b17023SJohn Marino    IS_STRING set and TYPE will be gt_types_enum_last.  Other objects
2304*e4b17023SJohn Marino    of unknown type will also have TYPE equal to gt_types_enum_last.  */
2305*e4b17023SJohn Marino 
2306*e4b17023SJohn Marino static int
pch_bucket(void * x,enum gt_types_enum type,bool is_string)2307*e4b17023SJohn Marino pch_bucket (void *x, enum gt_types_enum type,
2308*e4b17023SJohn Marino 	    bool is_string)
2309*e4b17023SJohn Marino {
2310*e4b17023SJohn Marino   /* Sort identifiers into their own bucket, to improve locality
2311*e4b17023SJohn Marino      when searching the identifier hash table.  */
2312*e4b17023SJohn Marino   if (type == gt_ggc_e_14lang_tree_node
2313*e4b17023SJohn Marino       && TREE_CODE ((tree) x) == IDENTIFIER_NODE)
2314*e4b17023SJohn Marino     return IDENTIFIER_BUCKET;
2315*e4b17023SJohn Marino   else if (type == gt_types_enum_last)
2316*e4b17023SJohn Marino     {
2317*e4b17023SJohn Marino       if (is_string)
2318*e4b17023SJohn Marino 	return STRING_BUCKET;
2319*e4b17023SJohn Marino       return OTHER_BUCKET;
2320*e4b17023SJohn Marino     }
2321*e4b17023SJohn Marino   return type;
2322*e4b17023SJohn Marino }
2323*e4b17023SJohn Marino 
2324*e4b17023SJohn Marino /* Add the size of object X to the size of the PCH data.  */
2325*e4b17023SJohn Marino 
2326*e4b17023SJohn Marino void
ggc_pch_count_object(struct ggc_pch_data * d,void * x ATTRIBUTE_UNUSED,size_t size,bool is_string,enum gt_types_enum type)2327*e4b17023SJohn Marino ggc_pch_count_object (struct ggc_pch_data *d, void *x ATTRIBUTE_UNUSED,
2328*e4b17023SJohn Marino 		      size_t size, bool is_string, enum gt_types_enum type)
2329*e4b17023SJohn Marino {
2330*e4b17023SJohn Marino   /* NOTE: Right now we don't need to align up the size of any objects.
2331*e4b17023SJohn Marino      Strings can be unaligned, and everything else is allocated to a
2332*e4b17023SJohn Marino      MAX_ALIGNMENT boundary already.  */
2333*e4b17023SJohn Marino 
2334*e4b17023SJohn Marino   d->d.type_totals[pch_bucket (x, type, is_string)] += size;
2335*e4b17023SJohn Marino }
2336*e4b17023SJohn Marino 
2337*e4b17023SJohn Marino /* Return the total size of the PCH data.  */
2338*e4b17023SJohn Marino 
2339*e4b17023SJohn Marino size_t
ggc_pch_total_size(struct ggc_pch_data * d)2340*e4b17023SJohn Marino ggc_pch_total_size (struct ggc_pch_data *d)
2341*e4b17023SJohn Marino {
2342*e4b17023SJohn Marino   int i;
2343*e4b17023SJohn Marino   size_t alloc_size, total_size;
2344*e4b17023SJohn Marino 
2345*e4b17023SJohn Marino   total_size = 0;
2346*e4b17023SJohn Marino   for (i = 0; i < NUM_PCH_BUCKETS; i++)
2347*e4b17023SJohn Marino     {
2348*e4b17023SJohn Marino       d->d.type_totals[i] = ROUND_UP (d->d.type_totals[i], GGC_PAGE_SIZE);
2349*e4b17023SJohn Marino       total_size += d->d.type_totals[i];
2350*e4b17023SJohn Marino     }
2351*e4b17023SJohn Marino   d->d.total = total_size;
2352*e4b17023SJohn Marino 
2353*e4b17023SJohn Marino   /* Include the size of the allocation bitmap.  */
2354*e4b17023SJohn Marino   alloc_size = CEIL (d->d.total, BYTES_PER_ALLOC_BIT * 8);
2355*e4b17023SJohn Marino   alloc_size = ROUND_UP (alloc_size, MAX_ALIGNMENT);
2356*e4b17023SJohn Marino   d->alloc_size = alloc_size;
2357*e4b17023SJohn Marino 
2358*e4b17023SJohn Marino   return d->d.total + alloc_size;
2359*e4b17023SJohn Marino }
2360*e4b17023SJohn Marino 
2361*e4b17023SJohn Marino /* Set the base address for the objects in the PCH file.  */
2362*e4b17023SJohn Marino 
2363*e4b17023SJohn Marino void
ggc_pch_this_base(struct ggc_pch_data * d,void * base_)2364*e4b17023SJohn Marino ggc_pch_this_base (struct ggc_pch_data *d, void *base_)
2365*e4b17023SJohn Marino {
2366*e4b17023SJohn Marino   int i;
2367*e4b17023SJohn Marino   size_t base = (size_t) base_;
2368*e4b17023SJohn Marino 
2369*e4b17023SJohn Marino   d->base = d->orig_base = base;
2370*e4b17023SJohn Marino   for (i = 0; i < NUM_PCH_BUCKETS; i++)
2371*e4b17023SJohn Marino     {
2372*e4b17023SJohn Marino       d->type_bases[i] = base;
2373*e4b17023SJohn Marino       base += d->d.type_totals[i];
2374*e4b17023SJohn Marino     }
2375*e4b17023SJohn Marino 
2376*e4b17023SJohn Marino   if (d->alloc_bits == NULL)
2377*e4b17023SJohn Marino     d->alloc_bits = XCNEWVAR (alloc_type, d->alloc_size);
2378*e4b17023SJohn Marino }
2379*e4b17023SJohn Marino 
2380*e4b17023SJohn Marino /* Allocate a place for object X of size SIZE in the PCH file.  */
2381*e4b17023SJohn Marino 
2382*e4b17023SJohn Marino char *
ggc_pch_alloc_object(struct ggc_pch_data * d,void * x,size_t size,bool is_string,enum gt_types_enum type)2383*e4b17023SJohn Marino ggc_pch_alloc_object (struct ggc_pch_data *d, void *x,
2384*e4b17023SJohn Marino 		      size_t size, bool is_string,
2385*e4b17023SJohn Marino 		      enum gt_types_enum type)
2386*e4b17023SJohn Marino {
2387*e4b17023SJohn Marino   size_t alloc_word, alloc_bit;
2388*e4b17023SJohn Marino   char *result;
2389*e4b17023SJohn Marino   int bucket = pch_bucket (x, type, is_string);
2390*e4b17023SJohn Marino 
2391*e4b17023SJohn Marino   /* Record the start of the object in the allocation bitmap.  We
2392*e4b17023SJohn Marino      can't assert that the allocation bit is previously clear, because
2393*e4b17023SJohn Marino      strings may violate the invariant that they are at least
2394*e4b17023SJohn Marino      BYTES_PER_ALLOC_BIT long.  This is harmless - ggc_get_size
2395*e4b17023SJohn Marino      should not be called for strings.  */
2396*e4b17023SJohn Marino   alloc_word = ((d->type_bases[bucket] - d->orig_base)
2397*e4b17023SJohn Marino 		/ (8 * sizeof (alloc_type) * BYTES_PER_ALLOC_BIT));
2398*e4b17023SJohn Marino   alloc_bit = ((d->type_bases[bucket] - d->orig_base)
2399*e4b17023SJohn Marino 	       / BYTES_PER_ALLOC_BIT) % (8 * sizeof (alloc_type));
2400*e4b17023SJohn Marino   d->alloc_bits[alloc_word] |= 1L << alloc_bit;
2401*e4b17023SJohn Marino 
2402*e4b17023SJohn Marino   /* Place the object at the current pointer for this bucket.  */
2403*e4b17023SJohn Marino   result = (char *) d->type_bases[bucket];
2404*e4b17023SJohn Marino   d->type_bases[bucket] += size;
2405*e4b17023SJohn Marino   return result;
2406*e4b17023SJohn Marino }
2407*e4b17023SJohn Marino 
2408*e4b17023SJohn Marino /* Prepare to write out the PCH data to file F.  */
2409*e4b17023SJohn Marino 
2410*e4b17023SJohn Marino void
ggc_pch_prepare_write(struct ggc_pch_data * d,FILE * f)2411*e4b17023SJohn Marino ggc_pch_prepare_write (struct ggc_pch_data *d,
2412*e4b17023SJohn Marino 		       FILE *f)
2413*e4b17023SJohn Marino {
2414*e4b17023SJohn Marino   /* We seek around a lot while writing.  Record where the end
2415*e4b17023SJohn Marino      of the padding in the PCH file is, so that we can
2416*e4b17023SJohn Marino      locate each object's offset.  */
2417*e4b17023SJohn Marino   d->start_offset = ftell (f);
2418*e4b17023SJohn Marino }
2419*e4b17023SJohn Marino 
2420*e4b17023SJohn Marino /* Write out object X of SIZE to file F.  */
2421*e4b17023SJohn Marino 
2422*e4b17023SJohn Marino void
ggc_pch_write_object(struct ggc_pch_data * d,FILE * f,void * x,void * newx,size_t size,bool is_string ATTRIBUTE_UNUSED)2423*e4b17023SJohn Marino ggc_pch_write_object (struct ggc_pch_data *d,
2424*e4b17023SJohn Marino 		      FILE *f, void *x, void *newx,
2425*e4b17023SJohn Marino 		      size_t size, bool is_string ATTRIBUTE_UNUSED)
2426*e4b17023SJohn Marino {
2427*e4b17023SJohn Marino   if (fseek (f, (size_t) newx - d->orig_base + d->start_offset, SEEK_SET) != 0)
2428*e4b17023SJohn Marino     fatal_error ("can%'t seek PCH file: %m");
2429*e4b17023SJohn Marino 
2430*e4b17023SJohn Marino   if (fwrite (x, size, 1, f) != 1)
2431*e4b17023SJohn Marino     fatal_error ("can%'t write PCH file: %m");
2432*e4b17023SJohn Marino }
2433*e4b17023SJohn Marino 
2434*e4b17023SJohn Marino void
ggc_pch_finish(struct ggc_pch_data * d,FILE * f)2435*e4b17023SJohn Marino ggc_pch_finish (struct ggc_pch_data *d, FILE *f)
2436*e4b17023SJohn Marino {
2437*e4b17023SJohn Marino   /* Write out the allocation bitmap.  */
2438*e4b17023SJohn Marino   if (fseek (f, d->start_offset + d->d.total, SEEK_SET) != 0)
2439*e4b17023SJohn Marino     fatal_error ("can%'t seek PCH file: %m");
2440*e4b17023SJohn Marino 
2441*e4b17023SJohn Marino   if (fwrite (d->alloc_bits, d->alloc_size, 1, f) != 1)
2442*e4b17023SJohn Marino     fatal_error ("can%'t write PCH file: %m");
2443*e4b17023SJohn Marino 
2444*e4b17023SJohn Marino   /* Done with the PCH, so write out our footer.  */
2445*e4b17023SJohn Marino   if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
2446*e4b17023SJohn Marino     fatal_error ("can%'t write PCH file: %m");
2447*e4b17023SJohn Marino 
2448*e4b17023SJohn Marino   free (d->alloc_bits);
2449*e4b17023SJohn Marino   free (d);
2450*e4b17023SJohn Marino }
2451*e4b17023SJohn Marino 
2452*e4b17023SJohn Marino /* The PCH file from F has been mapped at ADDR.  Read in any
2453*e4b17023SJohn Marino    additional data from the file and set up the GC state.  */
2454*e4b17023SJohn Marino 
2455*e4b17023SJohn Marino void
ggc_pch_read(FILE * f,void * addr)2456*e4b17023SJohn Marino ggc_pch_read (FILE *f, void *addr)
2457*e4b17023SJohn Marino {
2458*e4b17023SJohn Marino   struct ggc_pch_ondisk d;
2459*e4b17023SJohn Marino   size_t alloc_size;
2460*e4b17023SJohn Marino   struct alloc_zone *zone;
2461*e4b17023SJohn Marino   struct page_entry *pch_page;
2462*e4b17023SJohn Marino   char *p;
2463*e4b17023SJohn Marino 
2464*e4b17023SJohn Marino   if (fread (&d, sizeof (d), 1, f) != 1)
2465*e4b17023SJohn Marino     fatal_error ("can%'t read PCH file: %m");
2466*e4b17023SJohn Marino 
2467*e4b17023SJohn Marino   alloc_size = CEIL (d.total, BYTES_PER_ALLOC_BIT * 8);
2468*e4b17023SJohn Marino   alloc_size = ROUND_UP (alloc_size, MAX_ALIGNMENT);
2469*e4b17023SJohn Marino 
2470*e4b17023SJohn Marino   pch_zone.bytes = d.total;
2471*e4b17023SJohn Marino   pch_zone.alloc_bits = (alloc_type *) ((char *) addr + pch_zone.bytes);
2472*e4b17023SJohn Marino   pch_zone.page = (char *) addr;
2473*e4b17023SJohn Marino   pch_zone.end = (char *) pch_zone.alloc_bits;
2474*e4b17023SJohn Marino 
2475*e4b17023SJohn Marino   /* We've just read in a PCH file.  So, every object that used to be
2476*e4b17023SJohn Marino      allocated is now free.  */
2477*e4b17023SJohn Marino #ifdef GATHER_STATISTICS
2478*e4b17023SJohn Marino   zone_allocate_marks ();
2479*e4b17023SJohn Marino   ggc_prune_overhead_list ();
2480*e4b17023SJohn Marino   zone_free_marks ();
2481*e4b17023SJohn Marino #endif
2482*e4b17023SJohn Marino 
2483*e4b17023SJohn Marino   for (zone = G.zones; zone; zone = zone->next_zone)
2484*e4b17023SJohn Marino     {
2485*e4b17023SJohn Marino       struct small_page_entry *page, *next_page;
2486*e4b17023SJohn Marino       struct large_page_entry *large_page, *next_large_page;
2487*e4b17023SJohn Marino 
2488*e4b17023SJohn Marino       zone->allocated = 0;
2489*e4b17023SJohn Marino 
2490*e4b17023SJohn Marino       /* Clear the zone's free chunk list.  */
2491*e4b17023SJohn Marino       memset (zone->free_chunks, 0, sizeof (zone->free_chunks));
2492*e4b17023SJohn Marino       zone->high_free_bin = 0;
2493*e4b17023SJohn Marino       zone->cached_free = NULL;
2494*e4b17023SJohn Marino       zone->cached_free_size = 0;
2495*e4b17023SJohn Marino 
2496*e4b17023SJohn Marino       /* Move all the small pages onto the free list.  */
2497*e4b17023SJohn Marino       for (page = zone->pages; page != NULL; page = next_page)
2498*e4b17023SJohn Marino 	{
2499*e4b17023SJohn Marino 	  next_page = page->next;
2500*e4b17023SJohn Marino 	  memset (page->alloc_bits, 0,
2501*e4b17023SJohn Marino 		  G.small_page_overhead - PAGE_OVERHEAD);
2502*e4b17023SJohn Marino 	  free_small_page (page);
2503*e4b17023SJohn Marino 	}
2504*e4b17023SJohn Marino 
2505*e4b17023SJohn Marino       /* Discard all the large pages.  */
2506*e4b17023SJohn Marino       for (large_page = zone->large_pages; large_page != NULL;
2507*e4b17023SJohn Marino 	   large_page = next_large_page)
2508*e4b17023SJohn Marino 	{
2509*e4b17023SJohn Marino 	  next_large_page = large_page->next;
2510*e4b17023SJohn Marino 	  free_large_page (large_page);
2511*e4b17023SJohn Marino 	}
2512*e4b17023SJohn Marino 
2513*e4b17023SJohn Marino       zone->pages = NULL;
2514*e4b17023SJohn Marino       zone->large_pages = NULL;
2515*e4b17023SJohn Marino     }
2516*e4b17023SJohn Marino 
2517*e4b17023SJohn Marino   /* Allocate the dummy page entry for the PCH, and set all pages
2518*e4b17023SJohn Marino      mapped into the PCH to reference it.  */
2519*e4b17023SJohn Marino   pch_page = XCNEW (struct page_entry);
2520*e4b17023SJohn Marino   pch_page->page = pch_zone.page;
2521*e4b17023SJohn Marino   pch_page->pch_p = true;
2522*e4b17023SJohn Marino 
2523*e4b17023SJohn Marino   for (p = pch_zone.page; p < pch_zone.end; p += GGC_PAGE_SIZE)
2524*e4b17023SJohn Marino     set_page_table_entry (p, pch_page);
2525*e4b17023SJohn Marino }
2526