ggc-page.c (struct page_entry): Remove varray.h header.
[gcc.git] / gcc / ggc-page.c
1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "toplev.h"
29 #include "flags.h"
30 #include "ggc.h"
31 #include "timevar.h"
32 #include "params.h"
33 #ifdef ENABLE_VALGRIND_CHECKING
34 # ifdef HAVE_MEMCHECK_H
35 # include <memcheck.h>
36 # else
37 # include <valgrind.h>
38 # endif
39 #else
40 /* Avoid #ifdef:s when we can help it. */
41 #define VALGRIND_DISCARD(x)
42 #endif
43
44 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
45 file open. Prefer either to valloc. */
46 #ifdef HAVE_MMAP_ANON
47 # undef HAVE_MMAP_DEV_ZERO
48
49 # include <sys/mman.h>
50 # ifndef MAP_FAILED
51 # define MAP_FAILED -1
52 # endif
53 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
54 # define MAP_ANONYMOUS MAP_ANON
55 # endif
56 # define USING_MMAP
57
58 #endif
59
60 #ifdef HAVE_MMAP_DEV_ZERO
61
62 # include <sys/mman.h>
63 # ifndef MAP_FAILED
64 # define MAP_FAILED -1
65 # endif
66 # define USING_MMAP
67
68 #endif
69
70 #ifndef USING_MMAP
71 #define USING_MALLOC_PAGE_GROUPS
72 #endif
73
74 /* Stategy:
75
76 This garbage-collecting allocator allocates objects on one of a set
77 of pages. Each page can allocate objects of a single size only;
78 available sizes are powers of two starting at four bytes. The size
79 of an allocation request is rounded up to the next power of two
80 (`order'), and satisfied from the appropriate page.
81
82 Each page is recorded in a page-entry, which also maintains an
83 in-use bitmap of object positions on the page. This allows the
84 allocation state of a particular object to be flipped without
85 touching the page itself.
86
87 Each page-entry also has a context depth, which is used to track
88 pushing and popping of allocation contexts. Only objects allocated
89 in the current (highest-numbered) context may be collected.
90
91 Page entries are arranged in an array of singly-linked lists. The
92 array is indexed by the allocation size, in bits, of the pages on
93 it; i.e. all pages on a list allocate objects of the same size.
94 Pages are ordered on the list such that all non-full pages precede
95 all full pages, with non-full pages arranged in order of decreasing
96 context depth.
97
98 Empty pages (of all orders) are kept on a single page cache list,
99 and are considered first when new pages are required; they are
100 deallocated at the start of the next collection if they haven't
101 been recycled by then. */
102
103 /* Define GGC_DEBUG_LEVEL to print debugging information.
104 0: No debugging output.
105 1: GC statistics only.
106 2: Page-entry allocations/deallocations as well.
107 3: Object allocations as well.
108 4: Object marks as well. */
109 #define GGC_DEBUG_LEVEL (0)
110 \f
111 #ifndef HOST_BITS_PER_PTR
112 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
113 #endif
114
115 \f
116 /* A two-level tree is used to look up the page-entry for a given
117 pointer. Two chunks of the pointer's bits are extracted to index
118 the first and second levels of the tree, as follows:
119
120 HOST_PAGE_SIZE_BITS
121 32 | |
122 msb +----------------+----+------+------+ lsb
123 | | |
124 PAGE_L1_BITS |
125 | |
126 PAGE_L2_BITS
127
128 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
129 pages are aligned on system page boundaries. The next most
130 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
131 index values in the lookup table, respectively.
132
133 For 32-bit architectures and the settings below, there are no
134 leftover bits. For architectures with wider pointers, the lookup
135 tree points to a list of pages, which must be scanned to find the
136 correct one. */
137
138 #define PAGE_L1_BITS (8)
139 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
140 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
141 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
142
143 #define LOOKUP_L1(p) \
144 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
145
146 #define LOOKUP_L2(p) \
147 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
148
149 /* The number of objects per allocation page, for objects on a page of
150 the indicated ORDER. */
151 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
152
153 /* The number of objects in P. */
154 #define OBJECTS_IN_PAGE(P) ((P)->bytes / OBJECT_SIZE ((P)->order))
155
156 /* The size of an object on a page of the indicated ORDER. */
157 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
158
159 /* For speed, we avoid doing a general integer divide to locate the
160 offset in the allocation bitmap, by precalculating numbers M, S
161 such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
162 within the page which is evenly divisible by the object size Z. */
163 #define DIV_MULT(ORDER) inverse_table[ORDER].mult
164 #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
165 #define OFFSET_TO_BIT(OFFSET, ORDER) \
166 (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
167
168 /* The number of extra orders, not corresponding to power-of-two sized
169 objects. */
170
171 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
172
173 #define RTL_SIZE(NSLOTS) \
174 (sizeof (struct rtx_def) + ((NSLOTS) - 1) * sizeof (rtunion))
175
176 /* The Ith entry is the maximum size of an object to be stored in the
177 Ith extra order. Adding a new entry to this array is the *only*
178 thing you need to do to add a new special allocation size. */
179
180 static const size_t extra_order_size_table[] = {
181 sizeof (struct tree_decl),
182 sizeof (struct tree_list),
183 RTL_SIZE (2), /* REG, MEM, PLUS, etc. */
184 RTL_SIZE (10), /* INSN, CALL_INSN, JUMP_INSN */
185 };
186
187 /* The total number of orders. */
188
189 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
190
191 /* We use this structure to determine the alignment required for
192 allocations. For power-of-two sized allocations, that's not a
193 problem, but it does matter for odd-sized allocations. */
194
195 struct max_alignment {
196 char c;
197 union {
198 HOST_WIDEST_INT i;
199 #ifdef HAVE_LONG_DOUBLE
200 long double d;
201 #else
202 double d;
203 #endif
204 } u;
205 };
206
207 /* The biggest alignment required. */
208
209 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
210
211 /* Compute the smallest nonnegative number which when added to X gives
212 a multiple of F. */
213
214 #define ROUND_UP_VALUE(x, f) ((f) - 1 - ((f) - 1 + (x)) % (f))
215
216 /* Compute the smallest multiple of F that is >= X. */
217
218 #define ROUND_UP(x, f) (CEIL (x, f) * (f))
219
220 /* The Ith entry is the number of objects on a page or order I. */
221
222 static unsigned objects_per_page_table[NUM_ORDERS];
223
224 /* The Ith entry is the size of an object on a page of order I. */
225
226 static size_t object_size_table[NUM_ORDERS];
227
228 /* The Ith entry is a pair of numbers (mult, shift) such that
229 ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
230 for all k evenly divisible by OBJECT_SIZE(I). */
231
232 static struct
233 {
234 unsigned int mult;
235 unsigned int shift;
236 }
237 inverse_table[NUM_ORDERS];
238
239 /* A page_entry records the status of an allocation page. This
240 structure is dynamically sized to fit the bitmap in_use_p. */
241 typedef struct page_entry
242 {
243 /* The next page-entry with objects of the same size, or NULL if
244 this is the last page-entry. */
245 struct page_entry *next;
246
247 /* The number of bytes allocated. (This will always be a multiple
248 of the host system page size.) */
249 size_t bytes;
250
251 /* The address at which the memory is allocated. */
252 char *page;
253
254 #ifdef USING_MALLOC_PAGE_GROUPS
255 /* Back pointer to the page group this page came from. */
256 struct page_group *group;
257 #endif
258
259 /* This is the index in the by_depth varray where this page table
260 can be found. */
261 unsigned long index_by_depth;
262
263 /* Context depth of this page. */
264 unsigned short context_depth;
265
266 /* The number of free objects remaining on this page. */
267 unsigned short num_free_objects;
268
269 /* A likely candidate for the bit position of a free object for the
270 next allocation from this page. */
271 unsigned short next_bit_hint;
272
273 /* The lg of size of objects allocated from this page. */
274 unsigned char order;
275
276 /* A bit vector indicating whether or not objects are in use. The
277 Nth bit is one if the Nth object on this page is allocated. This
278 array is dynamically sized. */
279 unsigned long in_use_p[1];
280 } page_entry;
281
282 #ifdef USING_MALLOC_PAGE_GROUPS
283 /* A page_group describes a large allocation from malloc, from which
284 we parcel out aligned pages. */
285 typedef struct page_group
286 {
287 /* A linked list of all extant page groups. */
288 struct page_group *next;
289
290 /* The address we received from malloc. */
291 char *allocation;
292
293 /* The size of the block. */
294 size_t alloc_size;
295
296 /* A bitmask of pages in use. */
297 unsigned int in_use;
298 } page_group;
299 #endif
300
301 #if HOST_BITS_PER_PTR <= 32
302
303 /* On 32-bit hosts, we use a two level page table, as pictured above. */
304 typedef page_entry **page_table[PAGE_L1_SIZE];
305
306 #else
307
308 /* On 64-bit hosts, we use the same two level page tables plus a linked
309 list that disambiguates the top 32-bits. There will almost always be
310 exactly one entry in the list. */
311 typedef struct page_table_chain
312 {
313 struct page_table_chain *next;
314 size_t high_bits;
315 page_entry **table[PAGE_L1_SIZE];
316 } *page_table;
317
318 #endif
319
320 /* The rest of the global variables. */
321 static struct globals
322 {
323 /* The Nth element in this array is a page with objects of size 2^N.
324 If there are any pages with free objects, they will be at the
325 head of the list. NULL if there are no page-entries for this
326 object size. */
327 page_entry *pages[NUM_ORDERS];
328
329 /* The Nth element in this array is the last page with objects of
330 size 2^N. NULL if there are no page-entries for this object
331 size. */
332 page_entry *page_tails[NUM_ORDERS];
333
334 /* Lookup table for associating allocation pages with object addresses. */
335 page_table lookup;
336
337 /* The system's page size. */
338 size_t pagesize;
339 size_t lg_pagesize;
340
341 /* Bytes currently allocated. */
342 size_t allocated;
343
344 /* Bytes currently allocated at the end of the last collection. */
345 size_t allocated_last_gc;
346
347 /* Total amount of memory mapped. */
348 size_t bytes_mapped;
349
350 /* Bit N set if any allocations have been done at context depth N. */
351 unsigned long context_depth_allocations;
352
353 /* Bit N set if any collections have been done at context depth N. */
354 unsigned long context_depth_collections;
355
356 /* The current depth in the context stack. */
357 unsigned short context_depth;
358
359 /* A file descriptor open to /dev/zero for reading. */
360 #if defined (HAVE_MMAP_DEV_ZERO)
361 int dev_zero_fd;
362 #endif
363
364 /* A cache of free system pages. */
365 page_entry *free_pages;
366
367 #ifdef USING_MALLOC_PAGE_GROUPS
368 page_group *page_groups;
369 #endif
370
371 /* The file descriptor for debugging output. */
372 FILE *debug_file;
373
374 /* Current number of elements in use in depth below. */
375 unsigned int depth_in_use;
376
377 /* Maximum number of elements that can be used before resizing. */
378 unsigned int depth_max;
379
380 /* Each element of this arry is an index in by_depth where the given
381 depth starts. This structure is indexed by that given depth we
382 are interested in. */
383 unsigned int *depth;
384
385 /* Current number of elements in use in by_depth below. */
386 unsigned int by_depth_in_use;
387
388 /* Maximum number of elements that can be used before resizing. */
389 unsigned int by_depth_max;
390
391 /* Each element of this array is a pointer to a page_entry, all
392 page_entries can be found in here by increasing depth.
393 index_by_depth in the page_entry is the index into this data
394 structure where that page_entry can be found. This is used to
395 speed up finding all page_entries at a particular depth. */
396 page_entry **by_depth;
397
398 /* Each element is a pointer to the saved in_use_p bits, if any,
399 zero otherwise. We allocate them all together, to enable a
400 better runtime data access pattern. */
401 unsigned long **save_in_use;
402
403 } G;
404
405 /* The size in bytes required to maintain a bitmap for the objects
406 on a page-entry. */
407 #define BITMAP_SIZE(Num_objects) \
408 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
409
410 /* Allocate pages in chunks of this size, to throttle calls to memory
411 allocation routines. The first page is used, the rest go onto the
412 free list. This cannot be larger than HOST_BITS_PER_INT for the
413 in_use bitmask for page_group. */
414 #define GGC_QUIRE_SIZE 16
415
416 /* Initial guess as to how many page table entries we might need. */
417 #define INITIAL_PTE_COUNT 128
418 \f
419 static int ggc_allocated_p PARAMS ((const void *));
420 static page_entry *lookup_page_table_entry PARAMS ((const void *));
421 static void set_page_table_entry PARAMS ((void *, page_entry *));
422 #ifdef USING_MMAP
423 static char *alloc_anon PARAMS ((char *, size_t));
424 #endif
425 #ifdef USING_MALLOC_PAGE_GROUPS
426 static size_t page_group_index PARAMS ((char *, char *));
427 static void set_page_group_in_use PARAMS ((page_group *, char *));
428 static void clear_page_group_in_use PARAMS ((page_group *, char *));
429 #endif
430 static struct page_entry * alloc_page PARAMS ((unsigned));
431 static void free_page PARAMS ((struct page_entry *));
432 static void release_pages PARAMS ((void));
433 static void clear_marks PARAMS ((void));
434 static void sweep_pages PARAMS ((void));
435 static void ggc_recalculate_in_use_p PARAMS ((page_entry *));
436 static void compute_inverse PARAMS ((unsigned));
437 static inline void adjust_depth PARAMS ((void));
438 static void move_ptes_to_front PARAMS ((int, int));
439
440 #ifdef ENABLE_GC_CHECKING
441 static void poison_pages PARAMS ((void));
442 #endif
443
444 void debug_print_page_list PARAMS ((int));
445 static void push_depth PARAMS ((unsigned int));
446 static void push_by_depth PARAMS ((page_entry *, unsigned long *));
447 \f
448 /* Push an entry onto G.depth. */
449
450 inline static void
451 push_depth (i)
452 unsigned int i;
453 {
454 if (G.depth_in_use >= G.depth_max)
455 {
456 G.depth_max *= 2;
457 G.depth = (unsigned int *) xrealloc ((char *) G.depth,
458 G.depth_max * sizeof (unsigned int));
459 }
460 G.depth[G.depth_in_use++] = i;
461 }
462
463 /* Push an entry onto G.by_depth and G.save_in_use. */
464
465 inline static void
466 push_by_depth (p, s)
467 page_entry *p;
468 unsigned long *s;
469 {
470 if (G.by_depth_in_use >= G.by_depth_max)
471 {
472 G.by_depth_max *= 2;
473 G.by_depth = (page_entry **) xrealloc ((char *) G.by_depth,
474 G.by_depth_max * sizeof (page_entry *));
475 G.save_in_use = (unsigned long **) xrealloc ((char *) G.save_in_use,
476 G.by_depth_max * sizeof (unsigned long *));
477 }
478 G.by_depth[G.by_depth_in_use] = p;
479 G.save_in_use[G.by_depth_in_use++] = s;
480 }
481
482 #if (GCC_VERSION < 3001)
483 #define prefetch(X) ((void) X)
484 #else
485 #define prefetch(X) __builtin_prefetch (X)
486 #endif
487
488 #define save_in_use_p_i(__i) \
489 (G.save_in_use[__i])
490 #define save_in_use_p(__p) \
491 (save_in_use_p_i (__p->index_by_depth))
492
493 /* Returns nonzero if P was allocated in GC'able memory. */
494
495 static inline int
496 ggc_allocated_p (p)
497 const void *p;
498 {
499 page_entry ***base;
500 size_t L1, L2;
501
502 #if HOST_BITS_PER_PTR <= 32
503 base = &G.lookup[0];
504 #else
505 page_table table = G.lookup;
506 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
507 while (1)
508 {
509 if (table == NULL)
510 return 0;
511 if (table->high_bits == high_bits)
512 break;
513 table = table->next;
514 }
515 base = &table->table[0];
516 #endif
517
518 /* Extract the level 1 and 2 indices. */
519 L1 = LOOKUP_L1 (p);
520 L2 = LOOKUP_L2 (p);
521
522 return base[L1] && base[L1][L2];
523 }
524
525 /* Traverse the page table and find the entry for a page.
526 Die (probably) if the object wasn't allocated via GC. */
527
528 static inline page_entry *
529 lookup_page_table_entry(p)
530 const void *p;
531 {
532 page_entry ***base;
533 size_t L1, L2;
534
535 #if HOST_BITS_PER_PTR <= 32
536 base = &G.lookup[0];
537 #else
538 page_table table = G.lookup;
539 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
540 while (table->high_bits != high_bits)
541 table = table->next;
542 base = &table->table[0];
543 #endif
544
545 /* Extract the level 1 and 2 indices. */
546 L1 = LOOKUP_L1 (p);
547 L2 = LOOKUP_L2 (p);
548
549 return base[L1][L2];
550 }
551
552 /* Set the page table entry for a page. */
553
554 static void
555 set_page_table_entry(p, entry)
556 void *p;
557 page_entry *entry;
558 {
559 page_entry ***base;
560 size_t L1, L2;
561
562 #if HOST_BITS_PER_PTR <= 32
563 base = &G.lookup[0];
564 #else
565 page_table table;
566 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
567 for (table = G.lookup; table; table = table->next)
568 if (table->high_bits == high_bits)
569 goto found;
570
571 /* Not found -- allocate a new table. */
572 table = (page_table) xcalloc (1, sizeof(*table));
573 table->next = G.lookup;
574 table->high_bits = high_bits;
575 G.lookup = table;
576 found:
577 base = &table->table[0];
578 #endif
579
580 /* Extract the level 1 and 2 indices. */
581 L1 = LOOKUP_L1 (p);
582 L2 = LOOKUP_L2 (p);
583
584 if (base[L1] == NULL)
585 base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
586
587 base[L1][L2] = entry;
588 }
589
590 /* Prints the page-entry for object size ORDER, for debugging. */
591
592 void
593 debug_print_page_list (order)
594 int order;
595 {
596 page_entry *p;
597 printf ("Head=%p, Tail=%p:\n", (PTR) G.pages[order],
598 (PTR) G.page_tails[order]);
599 p = G.pages[order];
600 while (p != NULL)
601 {
602 printf ("%p(%1d|%3d) -> ", (PTR) p, p->context_depth,
603 p->num_free_objects);
604 p = p->next;
605 }
606 printf ("NULL\n");
607 fflush (stdout);
608 }
609
610 #ifdef USING_MMAP
611 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
612 (if non-null). The ifdef structure here is intended to cause a
613 compile error unless exactly one of the HAVE_* is defined. */
614
615 static inline char *
616 alloc_anon (pref, size)
617 char *pref ATTRIBUTE_UNUSED;
618 size_t size;
619 {
620 #ifdef HAVE_MMAP_ANON
621 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
622 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
623 #endif
624 #ifdef HAVE_MMAP_DEV_ZERO
625 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
626 MAP_PRIVATE, G.dev_zero_fd, 0);
627 #endif
628
629 if (page == (char *) MAP_FAILED)
630 {
631 perror ("virtual memory exhausted");
632 exit (FATAL_EXIT_CODE);
633 }
634
635 /* Remember that we allocated this memory. */
636 G.bytes_mapped += size;
637
638 /* Pretend we don't have access to the allocated pages. We'll enable
639 access to smaller pieces of the area in ggc_alloc. Discard the
640 handle to avoid handle leak. */
641 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (page, size));
642
643 return page;
644 }
645 #endif
646 #ifdef USING_MALLOC_PAGE_GROUPS
647 /* Compute the index for this page into the page group. */
648
649 static inline size_t
650 page_group_index (allocation, page)
651 char *allocation, *page;
652 {
653 return (size_t) (page - allocation) >> G.lg_pagesize;
654 }
655
656 /* Set and clear the in_use bit for this page in the page group. */
657
658 static inline void
659 set_page_group_in_use (group, page)
660 page_group *group;
661 char *page;
662 {
663 group->in_use |= 1 << page_group_index (group->allocation, page);
664 }
665
666 static inline void
667 clear_page_group_in_use (group, page)
668 page_group *group;
669 char *page;
670 {
671 group->in_use &= ~(1 << page_group_index (group->allocation, page));
672 }
673 #endif
674
675 /* Allocate a new page for allocating objects of size 2^ORDER,
676 and return an entry for it. The entry is not added to the
677 appropriate page_table list. */
678
679 static inline struct page_entry *
680 alloc_page (order)
681 unsigned order;
682 {
683 struct page_entry *entry, *p, **pp;
684 char *page;
685 size_t num_objects;
686 size_t bitmap_size;
687 size_t page_entry_size;
688 size_t entry_size;
689 #ifdef USING_MALLOC_PAGE_GROUPS
690 page_group *group;
691 #endif
692
693 num_objects = OBJECTS_PER_PAGE (order);
694 bitmap_size = BITMAP_SIZE (num_objects + 1);
695 page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
696 entry_size = num_objects * OBJECT_SIZE (order);
697 if (entry_size < G.pagesize)
698 entry_size = G.pagesize;
699
700 entry = NULL;
701 page = NULL;
702
703 /* Check the list of free pages for one we can use. */
704 for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
705 if (p->bytes == entry_size)
706 break;
707
708 if (p != NULL)
709 {
710 /* Recycle the allocated memory from this page ... */
711 *pp = p->next;
712 page = p->page;
713
714 #ifdef USING_MALLOC_PAGE_GROUPS
715 group = p->group;
716 #endif
717
718 /* ... and, if possible, the page entry itself. */
719 if (p->order == order)
720 {
721 entry = p;
722 memset (entry, 0, page_entry_size);
723 }
724 else
725 free (p);
726 }
727 #ifdef USING_MMAP
728 else if (entry_size == G.pagesize)
729 {
730 /* We want just one page. Allocate a bunch of them and put the
731 extras on the freelist. (Can only do this optimization with
732 mmap for backing store.) */
733 struct page_entry *e, *f = G.free_pages;
734 int i;
735
736 page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE);
737
738 /* This loop counts down so that the chain will be in ascending
739 memory order. */
740 for (i = GGC_QUIRE_SIZE - 1; i >= 1; i--)
741 {
742 e = (struct page_entry *) xcalloc (1, page_entry_size);
743 e->order = order;
744 e->bytes = G.pagesize;
745 e->page = page + (i << G.lg_pagesize);
746 e->next = f;
747 f = e;
748 }
749
750 G.free_pages = f;
751 }
752 else
753 page = alloc_anon (NULL, entry_size);
754 #endif
755 #ifdef USING_MALLOC_PAGE_GROUPS
756 else
757 {
758 /* Allocate a large block of memory and serve out the aligned
759 pages therein. This results in much less memory wastage
760 than the traditional implementation of valloc. */
761
762 char *allocation, *a, *enda;
763 size_t alloc_size, head_slop, tail_slop;
764 int multiple_pages = (entry_size == G.pagesize);
765
766 if (multiple_pages)
767 alloc_size = GGC_QUIRE_SIZE * G.pagesize;
768 else
769 alloc_size = entry_size + G.pagesize - 1;
770 allocation = xmalloc (alloc_size);
771
772 page = (char *) (((size_t) allocation + G.pagesize - 1) & -G.pagesize);
773 head_slop = page - allocation;
774 if (multiple_pages)
775 tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
776 else
777 tail_slop = alloc_size - entry_size - head_slop;
778 enda = allocation + alloc_size - tail_slop;
779
780 /* We allocated N pages, which are likely not aligned, leaving
781 us with N-1 usable pages. We plan to place the page_group
782 structure somewhere in the slop. */
783 if (head_slop >= sizeof (page_group))
784 group = (page_group *)page - 1;
785 else
786 {
787 /* We magically got an aligned allocation. Too bad, we have
788 to waste a page anyway. */
789 if (tail_slop == 0)
790 {
791 enda -= G.pagesize;
792 tail_slop += G.pagesize;
793 }
794 if (tail_slop < sizeof (page_group))
795 abort ();
796 group = (page_group *)enda;
797 tail_slop -= sizeof (page_group);
798 }
799
800 /* Remember that we allocated this memory. */
801 group->next = G.page_groups;
802 group->allocation = allocation;
803 group->alloc_size = alloc_size;
804 group->in_use = 0;
805 G.page_groups = group;
806 G.bytes_mapped += alloc_size;
807
808 /* If we allocated multiple pages, put the rest on the free list. */
809 if (multiple_pages)
810 {
811 struct page_entry *e, *f = G.free_pages;
812 for (a = enda - G.pagesize; a != page; a -= G.pagesize)
813 {
814 e = (struct page_entry *) xcalloc (1, page_entry_size);
815 e->order = order;
816 e->bytes = G.pagesize;
817 e->page = a;
818 e->group = group;
819 e->next = f;
820 f = e;
821 }
822 G.free_pages = f;
823 }
824 }
825 #endif
826
827 if (entry == NULL)
828 entry = (struct page_entry *) xcalloc (1, page_entry_size);
829
830 entry->bytes = entry_size;
831 entry->page = page;
832 entry->context_depth = G.context_depth;
833 entry->order = order;
834 entry->num_free_objects = num_objects;
835 entry->next_bit_hint = 1;
836
837 G.context_depth_allocations |= (unsigned long)1 << G.context_depth;
838
839 #ifdef USING_MALLOC_PAGE_GROUPS
840 entry->group = group;
841 set_page_group_in_use (group, page);
842 #endif
843
844 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
845 increment the hint. */
846 entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
847 = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
848
849 set_page_table_entry (page, entry);
850
851 if (GGC_DEBUG_LEVEL >= 2)
852 fprintf (G.debug_file,
853 "Allocating page at %p, object size=%lu, data %p-%p\n",
854 (PTR) entry, (unsigned long) OBJECT_SIZE (order), page,
855 page + entry_size - 1);
856
857 return entry;
858 }
859
860 /* Adjust the size of G.depth so that no index greater than the one
861 used by the top of the G.by_depth is used. */
862
863 static inline void
864 adjust_depth ()
865 {
866 page_entry *top;
867
868 if (G.by_depth_in_use)
869 {
870 top = G.by_depth[G.by_depth_in_use-1];
871
872 /* Peel back indicies in depth that index into by_depth, so that
873 as new elements are added to by_depth, we note the indicies
874 of those elements, if they are for new context depths. */
875 while (G.depth_in_use > (size_t)top->context_depth+1)
876 --G.depth_in_use;
877 }
878 }
879
880 /* For a page that is no longer needed, put it on the free page list. */
881
882 static inline void
883 free_page (entry)
884 page_entry *entry;
885 {
886 if (GGC_DEBUG_LEVEL >= 2)
887 fprintf (G.debug_file,
888 "Deallocating page at %p, data %p-%p\n", (PTR) entry,
889 entry->page, entry->page + entry->bytes - 1);
890
891 /* Mark the page as inaccessible. Discard the handle to avoid handle
892 leak. */
893 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (entry->page, entry->bytes));
894
895 set_page_table_entry (entry->page, NULL);
896
897 #ifdef USING_MALLOC_PAGE_GROUPS
898 clear_page_group_in_use (entry->group, entry->page);
899 #endif
900
901 if (G.by_depth_in_use > 1)
902 {
903 page_entry *top = G.by_depth[G.by_depth_in_use-1];
904
905 /* If they are at the same depth, put top element into freed
906 slot. */
907 if (entry->context_depth == top->context_depth)
908 {
909 int i = entry->index_by_depth;
910 G.by_depth[i] = top;
911 G.save_in_use[i] = G.save_in_use[G.by_depth_in_use-1];
912 top->index_by_depth = i;
913 }
914 else
915 {
916 /* We cannot free a page from a context deeper than the
917 current one. */
918 abort ();
919 }
920 }
921 --G.by_depth_in_use;
922
923 adjust_depth ();
924
925 entry->next = G.free_pages;
926 G.free_pages = entry;
927 }
928
929 /* Release the free page cache to the system. */
930
931 static void
932 release_pages ()
933 {
934 #ifdef USING_MMAP
935 page_entry *p, *next;
936 char *start;
937 size_t len;
938
939 /* Gather up adjacent pages so they are unmapped together. */
940 p = G.free_pages;
941
942 while (p)
943 {
944 start = p->page;
945 next = p->next;
946 len = p->bytes;
947 free (p);
948 p = next;
949
950 while (p && p->page == start + len)
951 {
952 next = p->next;
953 len += p->bytes;
954 free (p);
955 p = next;
956 }
957
958 munmap (start, len);
959 G.bytes_mapped -= len;
960 }
961
962 G.free_pages = NULL;
963 #endif
964 #ifdef USING_MALLOC_PAGE_GROUPS
965 page_entry **pp, *p;
966 page_group **gp, *g;
967
968 /* Remove all pages from free page groups from the list. */
969 pp = &G.free_pages;
970 while ((p = *pp) != NULL)
971 if (p->group->in_use == 0)
972 {
973 *pp = p->next;
974 free (p);
975 }
976 else
977 pp = &p->next;
978
979 /* Remove all free page groups, and release the storage. */
980 gp = &G.page_groups;
981 while ((g = *gp) != NULL)
982 if (g->in_use == 0)
983 {
984 *gp = g->next;
985 G.bytes_mapped -= g->alloc_size;
986 free (g->allocation);
987 }
988 else
989 gp = &g->next;
990 #endif
991 }
992
993 /* This table provides a fast way to determine ceil(log_2(size)) for
994 allocation requests. The minimum allocation size is eight bytes. */
995
996 static unsigned char size_lookup[257] =
997 {
998 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
999 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
1000 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1001 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
1002 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1003 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1004 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1005 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1006 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1007 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1008 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1009 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1010 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1011 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1012 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1013 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1014 8
1015 };
1016
1017 /* Allocate a chunk of memory of SIZE bytes. If ZERO is nonzero, the
1018 memory is zeroed; otherwise, its contents are undefined. */
1019
1020 void *
1021 ggc_alloc (size)
1022 size_t size;
1023 {
1024 unsigned order, word, bit, object_offset;
1025 struct page_entry *entry;
1026 void *result;
1027
1028 if (size <= 256)
1029 order = size_lookup[size];
1030 else
1031 {
1032 order = 9;
1033 while (size > OBJECT_SIZE (order))
1034 order++;
1035 }
1036
1037 /* If there are non-full pages for this size allocation, they are at
1038 the head of the list. */
1039 entry = G.pages[order];
1040
1041 /* If there is no page for this object size, or all pages in this
1042 context are full, allocate a new page. */
1043 if (entry == NULL || entry->num_free_objects == 0)
1044 {
1045 struct page_entry *new_entry;
1046 new_entry = alloc_page (order);
1047
1048 new_entry->index_by_depth = G.by_depth_in_use;
1049 push_by_depth (new_entry, 0);
1050
1051 /* We can skip context depths, if we do, make sure we go all the
1052 way to the new depth. */
1053 while (new_entry->context_depth >= G.depth_in_use)
1054 push_depth (G.by_depth_in_use-1);
1055
1056 /* If this is the only entry, it's also the tail. */
1057 if (entry == NULL)
1058 G.page_tails[order] = new_entry;
1059
1060 /* Put new pages at the head of the page list. */
1061 new_entry->next = entry;
1062 entry = new_entry;
1063 G.pages[order] = new_entry;
1064
1065 /* For a new page, we know the word and bit positions (in the
1066 in_use bitmap) of the first available object -- they're zero. */
1067 new_entry->next_bit_hint = 1;
1068 word = 0;
1069 bit = 0;
1070 object_offset = 0;
1071 }
1072 else
1073 {
1074 /* First try to use the hint left from the previous allocation
1075 to locate a clear bit in the in-use bitmap. We've made sure
1076 that the one-past-the-end bit is always set, so if the hint
1077 has run over, this test will fail. */
1078 unsigned hint = entry->next_bit_hint;
1079 word = hint / HOST_BITS_PER_LONG;
1080 bit = hint % HOST_BITS_PER_LONG;
1081
1082 /* If the hint didn't work, scan the bitmap from the beginning. */
1083 if ((entry->in_use_p[word] >> bit) & 1)
1084 {
1085 word = bit = 0;
1086 while (~entry->in_use_p[word] == 0)
1087 ++word;
1088 while ((entry->in_use_p[word] >> bit) & 1)
1089 ++bit;
1090 hint = word * HOST_BITS_PER_LONG + bit;
1091 }
1092
1093 /* Next time, try the next bit. */
1094 entry->next_bit_hint = hint + 1;
1095
1096 object_offset = hint * OBJECT_SIZE (order);
1097 }
1098
1099 /* Set the in-use bit. */
1100 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
1101
1102 /* Keep a running total of the number of free objects. If this page
1103 fills up, we may have to move it to the end of the list if the
1104 next page isn't full. If the next page is full, all subsequent
1105 pages are full, so there's no need to move it. */
1106 if (--entry->num_free_objects == 0
1107 && entry->next != NULL
1108 && entry->next->num_free_objects > 0)
1109 {
1110 G.pages[order] = entry->next;
1111 entry->next = NULL;
1112 G.page_tails[order]->next = entry;
1113 G.page_tails[order] = entry;
1114 }
1115
1116 /* Calculate the object's address. */
1117 result = entry->page + object_offset;
1118
1119 #ifdef ENABLE_GC_CHECKING
1120 /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
1121 exact same semantics in presence of memory bugs, regardless of
1122 ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
1123 handle to avoid handle leak. */
1124 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, OBJECT_SIZE (order)));
1125
1126 /* `Poison' the entire allocated object, including any padding at
1127 the end. */
1128 memset (result, 0xaf, OBJECT_SIZE (order));
1129
1130 /* Make the bytes after the end of the object unaccessible. Discard the
1131 handle to avoid handle leak. */
1132 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) result + size,
1133 OBJECT_SIZE (order) - size));
1134 #endif
1135
1136 /* Tell Valgrind that the memory is there, but its content isn't
1137 defined. The bytes at the end of the object are still marked
1138 unaccessible. */
1139 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, size));
1140
1141 /* Keep track of how many bytes are being allocated. This
1142 information is used in deciding when to collect. */
1143 G.allocated += OBJECT_SIZE (order);
1144
1145 if (GGC_DEBUG_LEVEL >= 3)
1146 fprintf (G.debug_file,
1147 "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
1148 (unsigned long) size, (unsigned long) OBJECT_SIZE (order), result,
1149 (PTR) entry);
1150
1151 return result;
1152 }
1153
1154 /* If P is not marked, marks it and return false. Otherwise return true.
1155 P must have been allocated by the GC allocator; it mustn't point to
1156 static objects, stack variables, or memory allocated with malloc. */
1157
1158 int
1159 ggc_set_mark (p)
1160 const void *p;
1161 {
1162 page_entry *entry;
1163 unsigned bit, word;
1164 unsigned long mask;
1165
1166 /* Look up the page on which the object is alloced. If the object
1167 wasn't allocated by the collector, we'll probably die. */
1168 entry = lookup_page_table_entry (p);
1169 #ifdef ENABLE_CHECKING
1170 if (entry == NULL)
1171 abort ();
1172 #endif
1173
1174 /* Calculate the index of the object on the page; this is its bit
1175 position in the in_use_p bitmap. */
1176 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1177 word = bit / HOST_BITS_PER_LONG;
1178 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1179
1180 /* If the bit was previously set, skip it. */
1181 if (entry->in_use_p[word] & mask)
1182 return 1;
1183
1184 /* Otherwise set it, and decrement the free object count. */
1185 entry->in_use_p[word] |= mask;
1186 entry->num_free_objects -= 1;
1187
1188 if (GGC_DEBUG_LEVEL >= 4)
1189 fprintf (G.debug_file, "Marking %p\n", p);
1190
1191 return 0;
1192 }
1193
1194 /* Return 1 if P has been marked, zero otherwise.
1195 P must have been allocated by the GC allocator; it mustn't point to
1196 static objects, stack variables, or memory allocated with malloc. */
1197
1198 int
1199 ggc_marked_p (p)
1200 const void *p;
1201 {
1202 page_entry *entry;
1203 unsigned bit, word;
1204 unsigned long mask;
1205
1206 /* Look up the page on which the object is alloced. If the object
1207 wasn't allocated by the collector, we'll probably die. */
1208 entry = lookup_page_table_entry (p);
1209 #ifdef ENABLE_CHECKING
1210 if (entry == NULL)
1211 abort ();
1212 #endif
1213
1214 /* Calculate the index of the object on the page; this is its bit
1215 position in the in_use_p bitmap. */
1216 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1217 word = bit / HOST_BITS_PER_LONG;
1218 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1219
1220 return (entry->in_use_p[word] & mask) != 0;
1221 }
1222
1223 /* Return the size of the gc-able object P. */
1224
1225 size_t
1226 ggc_get_size (p)
1227 const void *p;
1228 {
1229 page_entry *pe = lookup_page_table_entry (p);
1230 return OBJECT_SIZE (pe->order);
1231 }
1232 \f
1233 /* Subroutine of init_ggc which computes the pair of numbers used to
1234 perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1235
1236 This algorithm is taken from Granlund and Montgomery's paper
1237 "Division by Invariant Integers using Multiplication"
1238 (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1239 constants). */
1240
1241 static void
1242 compute_inverse (order)
1243 unsigned order;
1244 {
1245 unsigned size, inv, e;
1246
1247 /* There can be only one object per "page" in a bucket for sizes
1248 larger than half a machine page; it will always have offset zero. */
1249 if (OBJECT_SIZE (order) > G.pagesize/2)
1250 {
1251 if (OBJECTS_PER_PAGE (order) != 1)
1252 abort ();
1253
1254 DIV_MULT (order) = 1;
1255 DIV_SHIFT (order) = 0;
1256 return;
1257 }
1258
1259 size = OBJECT_SIZE (order);
1260 e = 0;
1261 while (size % 2 == 0)
1262 {
1263 e++;
1264 size >>= 1;
1265 }
1266
1267 inv = size;
1268 while (inv * size != 1)
1269 inv = inv * (2 - inv*size);
1270
1271 DIV_MULT (order) = inv;
1272 DIV_SHIFT (order) = e;
1273 }
1274
1275 /* Initialize the ggc-mmap allocator. */
1276 void
1277 init_ggc ()
1278 {
1279 unsigned order;
1280
1281 G.pagesize = getpagesize();
1282 G.lg_pagesize = exact_log2 (G.pagesize);
1283
1284 #ifdef HAVE_MMAP_DEV_ZERO
1285 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1286 if (G.dev_zero_fd == -1)
1287 abort ();
1288 #endif
1289
1290 #if 0
1291 G.debug_file = fopen ("ggc-mmap.debug", "w");
1292 #else
1293 G.debug_file = stdout;
1294 #endif
1295
1296 #ifdef USING_MMAP
1297 /* StunOS has an amazing off-by-one error for the first mmap allocation
1298 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1299 believe, is an unaligned page allocation, which would cause us to
1300 hork badly if we tried to use it. */
1301 {
1302 char *p = alloc_anon (NULL, G.pagesize);
1303 struct page_entry *e;
1304 if ((size_t)p & (G.pagesize - 1))
1305 {
1306 /* How losing. Discard this one and try another. If we still
1307 can't get something useful, give up. */
1308
1309 p = alloc_anon (NULL, G.pagesize);
1310 if ((size_t)p & (G.pagesize - 1))
1311 abort ();
1312 }
1313
1314 /* We have a good page, might as well hold onto it... */
1315 e = (struct page_entry *) xcalloc (1, sizeof (struct page_entry));
1316 e->bytes = G.pagesize;
1317 e->page = p;
1318 e->next = G.free_pages;
1319 G.free_pages = e;
1320 }
1321 #endif
1322
1323 /* Initialize the object size table. */
1324 for (order = 0; order < HOST_BITS_PER_PTR; ++order)
1325 object_size_table[order] = (size_t) 1 << order;
1326 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1327 {
1328 size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
1329
1330 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1331 so that we're sure of getting aligned memory. */
1332 s = ROUND_UP (s, MAX_ALIGNMENT);
1333 object_size_table[order] = s;
1334 }
1335
1336 /* Initialize the objects-per-page and inverse tables. */
1337 for (order = 0; order < NUM_ORDERS; ++order)
1338 {
1339 objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
1340 if (objects_per_page_table[order] == 0)
1341 objects_per_page_table[order] = 1;
1342 compute_inverse (order);
1343 }
1344
1345 /* Reset the size_lookup array to put appropriately sized objects in
1346 the special orders. All objects bigger than the previous power
1347 of two, but no greater than the special size, should go in the
1348 new order. */
1349 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1350 {
1351 int o;
1352 int i;
1353
1354 o = size_lookup[OBJECT_SIZE (order)];
1355 for (i = OBJECT_SIZE (order); size_lookup [i] == o; --i)
1356 size_lookup[i] = order;
1357 }
1358
1359 G.depth_in_use = 0;
1360 G.depth_max = 10;
1361 G.depth = (unsigned int *) xmalloc (G.depth_max * sizeof (unsigned int));
1362
1363 G.by_depth_in_use = 0;
1364 G.by_depth_max = INITIAL_PTE_COUNT;
1365 G.by_depth = (page_entry **) xmalloc (G.by_depth_max * sizeof (page_entry *));
1366 G.save_in_use = (unsigned long **) xmalloc (G.by_depth_max * sizeof (unsigned long *));
1367 }
1368
1369 /* Increment the `GC context'. Objects allocated in an outer context
1370 are never freed, eliminating the need to register their roots. */
1371
1372 void
1373 ggc_push_context ()
1374 {
1375 ++G.context_depth;
1376
1377 /* Die on wrap. */
1378 if (G.context_depth >= HOST_BITS_PER_LONG)
1379 abort ();
1380 }
1381
1382 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1383 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1384
1385 static void
1386 ggc_recalculate_in_use_p (p)
1387 page_entry *p;
1388 {
1389 unsigned int i;
1390 size_t num_objects;
1391
1392 /* Because the past-the-end bit in in_use_p is always set, we
1393 pretend there is one additional object. */
1394 num_objects = OBJECTS_IN_PAGE (p) + 1;
1395
1396 /* Reset the free object count. */
1397 p->num_free_objects = num_objects;
1398
1399 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1400 for (i = 0;
1401 i < CEIL (BITMAP_SIZE (num_objects),
1402 sizeof (*p->in_use_p));
1403 ++i)
1404 {
1405 unsigned long j;
1406
1407 /* Something is in use if it is marked, or if it was in use in a
1408 context further down the context stack. */
1409 p->in_use_p[i] |= save_in_use_p (p)[i];
1410
1411 /* Decrement the free object count for every object allocated. */
1412 for (j = p->in_use_p[i]; j; j >>= 1)
1413 p->num_free_objects -= (j & 1);
1414 }
1415
1416 if (p->num_free_objects >= num_objects)
1417 abort ();
1418 }
1419
1420 /* Decrement the `GC context'. All objects allocated since the
1421 previous ggc_push_context are migrated to the outer context. */
1422
1423 void
1424 ggc_pop_context ()
1425 {
1426 unsigned long omask;
1427 unsigned int depth, i, e;
1428 #ifdef ENABLE_CHECKING
1429 unsigned int order;
1430 #endif
1431
1432 depth = --G.context_depth;
1433 omask = (unsigned long)1 << (depth + 1);
1434
1435 if (!((G.context_depth_allocations | G.context_depth_collections) & omask))
1436 return;
1437
1438 G.context_depth_allocations |= (G.context_depth_allocations & omask) >> 1;
1439 G.context_depth_allocations &= omask - 1;
1440 G.context_depth_collections &= omask - 1;
1441
1442 /* The G.depth array is shortend so that the last index is the
1443 context_depth of the top element of by_depth. */
1444 if (depth+1 < G.depth_in_use)
1445 e = G.depth[depth+1];
1446 else
1447 e = G.by_depth_in_use;
1448
1449 /* We might not have any PTEs of depth depth. */
1450 if (depth < G.depth_in_use)
1451 {
1452
1453 /* First we go through all the pages at depth depth to
1454 recalculate the in use bits. */
1455 for (i = G.depth[depth]; i < e; ++i)
1456 {
1457 page_entry *p;
1458
1459 #ifdef ENABLE_CHECKING
1460 p = G.by_depth[i];
1461
1462 /* Check that all of the pages really are at the depth that
1463 we expect. */
1464 if (p->context_depth != depth)
1465 abort ();
1466 if (p->index_by_depth != i)
1467 abort ();
1468 #endif
1469
1470 prefetch (&save_in_use_p_i (i+8));
1471 prefetch (&save_in_use_p_i (i+16));
1472 if (save_in_use_p_i (i))
1473 {
1474 p = G.by_depth[i];
1475 ggc_recalculate_in_use_p (p);
1476 free (save_in_use_p_i (i));
1477 save_in_use_p_i (i) = 0;
1478 }
1479 }
1480 }
1481
1482 /* Then, we reset all page_entries with a depth greater than depth
1483 to be at depth. */
1484 for (i = e; i < G.by_depth_in_use; ++i)
1485 {
1486 page_entry *p = G.by_depth[i];
1487
1488 /* Check that all of the pages really are at the depth we
1489 expect. */
1490 #ifdef ENABLE_CHECKING
1491 if (p->context_depth <= depth)
1492 abort ();
1493 if (p->index_by_depth != i)
1494 abort ();
1495 #endif
1496 p->context_depth = depth;
1497 }
1498
1499 adjust_depth ();
1500
1501 #ifdef ENABLE_CHECKING
1502 for (order = 2; order < NUM_ORDERS; order++)
1503 {
1504 page_entry *p;
1505
1506 for (p = G.pages[order]; p != NULL; p = p->next)
1507 {
1508 if (p->context_depth > depth)
1509 abort ();
1510 else if (p->context_depth == depth && save_in_use_p (p))
1511 abort ();
1512 }
1513 }
1514 #endif
1515 }
1516 \f
1517 /* Unmark all objects. */
1518
1519 static inline void
1520 clear_marks ()
1521 {
1522 unsigned order;
1523
1524 for (order = 2; order < NUM_ORDERS; order++)
1525 {
1526 page_entry *p;
1527
1528 for (p = G.pages[order]; p != NULL; p = p->next)
1529 {
1530 size_t num_objects = OBJECTS_IN_PAGE (p);
1531 size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
1532
1533 #ifdef ENABLE_CHECKING
1534 /* The data should be page-aligned. */
1535 if ((size_t) p->page & (G.pagesize - 1))
1536 abort ();
1537 #endif
1538
1539 /* Pages that aren't in the topmost context are not collected;
1540 nevertheless, we need their in-use bit vectors to store GC
1541 marks. So, back them up first. */
1542 if (p->context_depth < G.context_depth)
1543 {
1544 if (! save_in_use_p (p))
1545 save_in_use_p (p) = xmalloc (bitmap_size);
1546 memcpy (save_in_use_p (p), p->in_use_p, bitmap_size);
1547 }
1548
1549 /* Reset reset the number of free objects and clear the
1550 in-use bits. These will be adjusted by mark_obj. */
1551 p->num_free_objects = num_objects;
1552 memset (p->in_use_p, 0, bitmap_size);
1553
1554 /* Make sure the one-past-the-end bit is always set. */
1555 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
1556 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
1557 }
1558 }
1559 }
1560
1561 /* Free all empty pages. Partially empty pages need no attention
1562 because the `mark' bit doubles as an `unused' bit. */
1563
1564 static inline void
1565 sweep_pages ()
1566 {
1567 unsigned order;
1568
1569 for (order = 2; order < NUM_ORDERS; order++)
1570 {
1571 /* The last page-entry to consider, regardless of entries
1572 placed at the end of the list. */
1573 page_entry * const last = G.page_tails[order];
1574
1575 size_t num_objects;
1576 size_t live_objects;
1577 page_entry *p, *previous;
1578 int done;
1579
1580 p = G.pages[order];
1581 if (p == NULL)
1582 continue;
1583
1584 previous = NULL;
1585 do
1586 {
1587 page_entry *next = p->next;
1588
1589 /* Loop until all entries have been examined. */
1590 done = (p == last);
1591
1592 num_objects = OBJECTS_IN_PAGE (p);
1593
1594 /* Add all live objects on this page to the count of
1595 allocated memory. */
1596 live_objects = num_objects - p->num_free_objects;
1597
1598 G.allocated += OBJECT_SIZE (order) * live_objects;
1599
1600 /* Only objects on pages in the topmost context should get
1601 collected. */
1602 if (p->context_depth < G.context_depth)
1603 ;
1604
1605 /* Remove the page if it's empty. */
1606 else if (live_objects == 0)
1607 {
1608 if (! previous)
1609 G.pages[order] = next;
1610 else
1611 previous->next = next;
1612
1613 /* Are we removing the last element? */
1614 if (p == G.page_tails[order])
1615 G.page_tails[order] = previous;
1616 free_page (p);
1617 p = previous;
1618 }
1619
1620 /* If the page is full, move it to the end. */
1621 else if (p->num_free_objects == 0)
1622 {
1623 /* Don't move it if it's already at the end. */
1624 if (p != G.page_tails[order])
1625 {
1626 /* Move p to the end of the list. */
1627 p->next = NULL;
1628 G.page_tails[order]->next = p;
1629
1630 /* Update the tail pointer... */
1631 G.page_tails[order] = p;
1632
1633 /* ... and the head pointer, if necessary. */
1634 if (! previous)
1635 G.pages[order] = next;
1636 else
1637 previous->next = next;
1638 p = previous;
1639 }
1640 }
1641
1642 /* If we've fallen through to here, it's a page in the
1643 topmost context that is neither full nor empty. Such a
1644 page must precede pages at lesser context depth in the
1645 list, so move it to the head. */
1646 else if (p != G.pages[order])
1647 {
1648 previous->next = p->next;
1649 p->next = G.pages[order];
1650 G.pages[order] = p;
1651 /* Are we moving the last element? */
1652 if (G.page_tails[order] == p)
1653 G.page_tails[order] = previous;
1654 p = previous;
1655 }
1656
1657 previous = p;
1658 p = next;
1659 }
1660 while (! done);
1661
1662 /* Now, restore the in_use_p vectors for any pages from contexts
1663 other than the current one. */
1664 for (p = G.pages[order]; p; p = p->next)
1665 if (p->context_depth != G.context_depth)
1666 ggc_recalculate_in_use_p (p);
1667 }
1668 }
1669
1670 #ifdef ENABLE_GC_CHECKING
1671 /* Clobber all free objects. */
1672
1673 static inline void
1674 poison_pages ()
1675 {
1676 unsigned order;
1677
1678 for (order = 2; order < NUM_ORDERS; order++)
1679 {
1680 size_t size = OBJECT_SIZE (order);
1681 page_entry *p;
1682
1683 for (p = G.pages[order]; p != NULL; p = p->next)
1684 {
1685 size_t num_objects;
1686 size_t i;
1687
1688 if (p->context_depth != G.context_depth)
1689 /* Since we don't do any collection for pages in pushed
1690 contexts, there's no need to do any poisoning. And
1691 besides, the IN_USE_P array isn't valid until we pop
1692 contexts. */
1693 continue;
1694
1695 num_objects = OBJECTS_IN_PAGE (p);
1696 for (i = 0; i < num_objects; i++)
1697 {
1698 size_t word, bit;
1699 word = i / HOST_BITS_PER_LONG;
1700 bit = i % HOST_BITS_PER_LONG;
1701 if (((p->in_use_p[word] >> bit) & 1) == 0)
1702 {
1703 char *object = p->page + i * size;
1704
1705 /* Keep poison-by-write when we expect to use Valgrind,
1706 so the exact same memory semantics is kept, in case
1707 there are memory errors. We override this request
1708 below. */
1709 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (object, size));
1710 memset (object, 0xa5, size);
1711
1712 /* Drop the handle to avoid handle leak. */
1713 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (object, size));
1714 }
1715 }
1716 }
1717 }
1718 }
1719 #endif
1720
1721 /* Top level mark-and-sweep routine. */
1722
1723 void
1724 ggc_collect ()
1725 {
1726 /* Avoid frequent unnecessary work by skipping collection if the
1727 total allocations haven't expanded much since the last
1728 collection. */
1729 float allocated_last_gc =
1730 MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
1731
1732 float min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
1733
1734 if (G.allocated < allocated_last_gc + min_expand)
1735 return;
1736
1737 timevar_push (TV_GC);
1738 if (!quiet_flag)
1739 fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
1740
1741 /* Zero the total allocated bytes. This will be recalculated in the
1742 sweep phase. */
1743 G.allocated = 0;
1744
1745 /* Release the pages we freed the last time we collected, but didn't
1746 reuse in the interim. */
1747 release_pages ();
1748
1749 /* Indicate that we've seen collections at this context depth. */
1750 G.context_depth_collections = ((unsigned long)1 << (G.context_depth + 1)) - 1;
1751
1752 clear_marks ();
1753 ggc_mark_roots ();
1754
1755 #ifdef ENABLE_GC_CHECKING
1756 poison_pages ();
1757 #endif
1758
1759 sweep_pages ();
1760
1761 G.allocated_last_gc = G.allocated;
1762
1763 timevar_pop (TV_GC);
1764
1765 if (!quiet_flag)
1766 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
1767 }
1768
1769 /* Print allocation statistics. */
1770 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1771 ? (x) \
1772 : ((x) < 1024*1024*10 \
1773 ? (x) / 1024 \
1774 : (x) / (1024*1024))))
1775 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1776
1777 void
1778 ggc_print_statistics ()
1779 {
1780 struct ggc_statistics stats;
1781 unsigned int i;
1782 size_t total_overhead = 0;
1783
1784 /* Clear the statistics. */
1785 memset (&stats, 0, sizeof (stats));
1786
1787 /* Make sure collection will really occur. */
1788 G.allocated_last_gc = 0;
1789
1790 /* Collect and print the statistics common across collectors. */
1791 ggc_print_common_statistics (stderr, &stats);
1792
1793 /* Release free pages so that we will not count the bytes allocated
1794 there as part of the total allocated memory. */
1795 release_pages ();
1796
1797 /* Collect some information about the various sizes of
1798 allocation. */
1799 fprintf (stderr, "\n%-5s %10s %10s %10s\n",
1800 "Size", "Allocated", "Used", "Overhead");
1801 for (i = 0; i < NUM_ORDERS; ++i)
1802 {
1803 page_entry *p;
1804 size_t allocated;
1805 size_t in_use;
1806 size_t overhead;
1807
1808 /* Skip empty entries. */
1809 if (!G.pages[i])
1810 continue;
1811
1812 overhead = allocated = in_use = 0;
1813
1814 /* Figure out the total number of bytes allocated for objects of
1815 this size, and how many of them are actually in use. Also figure
1816 out how much memory the page table is using. */
1817 for (p = G.pages[i]; p; p = p->next)
1818 {
1819 allocated += p->bytes;
1820 in_use +=
1821 (OBJECTS_IN_PAGE (p) - p->num_free_objects) * OBJECT_SIZE (i);
1822
1823 overhead += (sizeof (page_entry) - sizeof (long)
1824 + BITMAP_SIZE (OBJECTS_IN_PAGE (p) + 1));
1825 }
1826 fprintf (stderr, "%-5lu %10lu%c %10lu%c %10lu%c\n",
1827 (unsigned long) OBJECT_SIZE (i),
1828 SCALE (allocated), LABEL (allocated),
1829 SCALE (in_use), LABEL (in_use),
1830 SCALE (overhead), LABEL (overhead));
1831 total_overhead += overhead;
1832 }
1833 fprintf (stderr, "%-5s %10lu%c %10lu%c %10lu%c\n", "Total",
1834 SCALE (G.bytes_mapped), LABEL (G.bytes_mapped),
1835 SCALE (G.allocated), LABEL(G.allocated),
1836 SCALE (total_overhead), LABEL (total_overhead));
1837 }
1838 \f
1839 struct ggc_pch_data
1840 {
1841 struct ggc_pch_ondisk
1842 {
1843 unsigned totals[NUM_ORDERS];
1844 } d;
1845 size_t base[NUM_ORDERS];
1846 size_t written[NUM_ORDERS];
1847 };
1848
1849 struct ggc_pch_data *
1850 init_ggc_pch ()
1851 {
1852 return xcalloc (sizeof (struct ggc_pch_data), 1);
1853 }
1854
1855 void
1856 ggc_pch_count_object (d, x, size)
1857 struct ggc_pch_data *d;
1858 void *x ATTRIBUTE_UNUSED;
1859 size_t size;
1860 {
1861 unsigned order;
1862
1863 if (size <= 256)
1864 order = size_lookup[size];
1865 else
1866 {
1867 order = 9;
1868 while (size > OBJECT_SIZE (order))
1869 order++;
1870 }
1871
1872 d->d.totals[order]++;
1873 }
1874
1875 size_t
1876 ggc_pch_total_size (d)
1877 struct ggc_pch_data *d;
1878 {
1879 size_t a = 0;
1880 unsigned i;
1881
1882 for (i = 0; i < NUM_ORDERS; i++)
1883 a += ROUND_UP (d->d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1884 return a;
1885 }
1886
1887 void
1888 ggc_pch_this_base (d, base)
1889 struct ggc_pch_data *d;
1890 void *base;
1891 {
1892 size_t a = (size_t) base;
1893 unsigned i;
1894
1895 for (i = 0; i < NUM_ORDERS; i++)
1896 {
1897 d->base[i] = a;
1898 a += ROUND_UP (d->d.totals[i] * OBJECT_SIZE (i), G.pagesize);
1899 }
1900 }
1901
1902
1903 char *
1904 ggc_pch_alloc_object (d, x, size)
1905 struct ggc_pch_data *d;
1906 void *x ATTRIBUTE_UNUSED;
1907 size_t size;
1908 {
1909 unsigned order;
1910 char *result;
1911
1912 if (size <= 256)
1913 order = size_lookup[size];
1914 else
1915 {
1916 order = 9;
1917 while (size > OBJECT_SIZE (order))
1918 order++;
1919 }
1920
1921 result = (char *) d->base[order];
1922 d->base[order] += OBJECT_SIZE (order);
1923 return result;
1924 }
1925
1926 void
1927 ggc_pch_prepare_write (d, f)
1928 struct ggc_pch_data * d ATTRIBUTE_UNUSED;
1929 FILE * f ATTRIBUTE_UNUSED;
1930 {
1931 /* Nothing to do. */
1932 }
1933
1934 void
1935 ggc_pch_write_object (d, f, x, newx, size)
1936 struct ggc_pch_data * d ATTRIBUTE_UNUSED;
1937 FILE *f;
1938 void *x;
1939 void *newx ATTRIBUTE_UNUSED;
1940 size_t size;
1941 {
1942 unsigned order;
1943
1944 if (size <= 256)
1945 order = size_lookup[size];
1946 else
1947 {
1948 order = 9;
1949 while (size > OBJECT_SIZE (order))
1950 order++;
1951 }
1952
1953 if (fwrite (x, size, 1, f) != 1)
1954 fatal_io_error ("can't write PCH file");
1955
1956 /* In the current implementation, SIZE is always equal to
1957 OBJECT_SIZE (order) and so the fseek is never executed. */
1958 if (size != OBJECT_SIZE (order)
1959 && fseek (f, OBJECT_SIZE (order) - size, SEEK_CUR) != 0)
1960 fatal_io_error ("can't write PCH file");
1961
1962 d->written[order]++;
1963 if (d->written[order] == d->d.totals[order]
1964 && fseek (f, ROUND_UP_VALUE (d->d.totals[order] * OBJECT_SIZE (order),
1965 G.pagesize),
1966 SEEK_CUR) != 0)
1967 fatal_io_error ("can't write PCH file");
1968 }
1969
1970 void
1971 ggc_pch_finish (d, f)
1972 struct ggc_pch_data * d;
1973 FILE *f;
1974 {
1975 if (fwrite (&d->d, sizeof (d->d), 1, f) != 1)
1976 fatal_io_error ("can't write PCH file");
1977 free (d);
1978 }
1979
1980 /* Move the PCH PTE entries just added to the end of by_depth, to the
1981 front. */
1982
1983 static void
1984 move_ptes_to_front (count_old_page_tables, count_new_page_tables)
1985 int count_old_page_tables;
1986 int count_new_page_tables;
1987 {
1988 unsigned i;
1989
1990 /* First, we swap the new entries to the front of the varrays. */
1991 page_entry **new_by_depth;
1992 unsigned long **new_save_in_use;
1993
1994 new_by_depth = (page_entry **) xmalloc (G.by_depth_max * sizeof (page_entry *));
1995 new_save_in_use = (unsigned long **) xmalloc (G.by_depth_max * sizeof (unsigned long *));
1996
1997 memcpy (&new_by_depth[0],
1998 &G.by_depth[count_old_page_tables],
1999 count_new_page_tables * sizeof (void *));
2000 memcpy (&new_by_depth[count_new_page_tables],
2001 &G.by_depth[0],
2002 count_old_page_tables * sizeof (void *));
2003 memcpy (&new_save_in_use[0],
2004 &G.save_in_use[count_old_page_tables],
2005 count_new_page_tables * sizeof (void *));
2006 memcpy (&new_save_in_use[count_new_page_tables],
2007 &G.save_in_use[0],
2008 count_old_page_tables * sizeof (void *));
2009
2010 free (G.by_depth);
2011 free (G.save_in_use);
2012
2013 G.by_depth = new_by_depth;
2014 G.save_in_use = new_save_in_use;
2015
2016 /* Now update all the index_by_depth fields. */
2017 for (i = G.by_depth_in_use; i > 0; --i)
2018 {
2019 page_entry *p = G.by_depth[i-1];
2020 p->index_by_depth = i-1;
2021 }
2022
2023 /* And last, we update the depth pointers in G.depth. The first
2024 entry is already 0, and context 0 entries always start at index
2025 0, so there is nothing to update in the first slot. We need a
2026 second slot, only if we have old ptes, and if we do, they start
2027 at index count_new_page_tables. */
2028 if (count_old_page_tables)
2029 push_depth (count_new_page_tables);
2030 }
2031
2032 void
2033 ggc_pch_read (f, addr)
2034 FILE *f;
2035 void *addr;
2036 {
2037 struct ggc_pch_ondisk d;
2038 unsigned i;
2039 char *offs = addr;
2040 unsigned long count_old_page_tables;
2041 unsigned long count_new_page_tables;
2042
2043 count_old_page_tables = G.by_depth_in_use;
2044
2045 /* We've just read in a PCH file. So, every object that used to be
2046 allocated is now free. */
2047 clear_marks ();
2048 #ifdef GGC_POISON
2049 poison_pages ();
2050 #endif
2051
2052 /* No object read from a PCH file should ever be freed. So, set the
2053 context depth to 1, and set the depth of all the currently-allocated
2054 pages to be 1 too. PCH pages will have depth 0. */
2055 if (G.context_depth != 0)
2056 abort ();
2057 G.context_depth = 1;
2058 for (i = 0; i < NUM_ORDERS; i++)
2059 {
2060 page_entry *p;
2061 for (p = G.pages[i]; p != NULL; p = p->next)
2062 p->context_depth = G.context_depth;
2063 }
2064
2065 /* Allocate the appropriate page-table entries for the pages read from
2066 the PCH file. */
2067 if (fread (&d, sizeof (d), 1, f) != 1)
2068 fatal_io_error ("can't read PCH file");
2069
2070 for (i = 0; i < NUM_ORDERS; i++)
2071 {
2072 struct page_entry *entry;
2073 char *pte;
2074 size_t bytes;
2075 size_t num_objs;
2076 size_t j;
2077
2078 if (d.totals[i] == 0)
2079 continue;
2080
2081 bytes = ROUND_UP (d.totals[i] * OBJECT_SIZE (i), G.pagesize);
2082 num_objs = bytes / OBJECT_SIZE (i);
2083 entry = xcalloc (1, (sizeof (struct page_entry)
2084 - sizeof (long)
2085 + BITMAP_SIZE (num_objs + 1)));
2086 entry->bytes = bytes;
2087 entry->page = offs;
2088 entry->context_depth = 0;
2089 offs += bytes;
2090 entry->num_free_objects = 0;
2091 entry->order = i;
2092
2093 for (j = 0;
2094 j + HOST_BITS_PER_LONG <= num_objs + 1;
2095 j += HOST_BITS_PER_LONG)
2096 entry->in_use_p[j / HOST_BITS_PER_LONG] = -1;
2097 for (; j < num_objs + 1; j++)
2098 entry->in_use_p[j / HOST_BITS_PER_LONG]
2099 |= 1L << (j % HOST_BITS_PER_LONG);
2100
2101 for (pte = entry->page;
2102 pte < entry->page + entry->bytes;
2103 pte += G.pagesize)
2104 set_page_table_entry (pte, entry);
2105
2106 if (G.page_tails[i] != NULL)
2107 G.page_tails[i]->next = entry;
2108 else
2109 G.pages[i] = entry;
2110 G.page_tails[i] = entry;
2111
2112 /* We start off by just adding all the new information to the
2113 end of the varrays, later, we will move the new information
2114 to the front of the varrays, as the PCH page tables are at
2115 context 0. */
2116 push_by_depth (entry, 0);
2117 }
2118
2119 /* Now, we update the various data structures that speed page table
2120 handling. */
2121 count_new_page_tables = G.by_depth_in_use - count_old_page_tables;
2122
2123 move_ptes_to_front (count_old_page_tables, count_new_page_tables);
2124
2125 /* Update the statistics. */
2126 G.allocated = G.allocated_last_gc = offs - (char *)addr;
2127 }