Merge basic-improvements-branch to trunk
[gcc.git] / gcc / ggc-page.c
1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002 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 "varray.h"
30 #include "flags.h"
31 #include "ggc.h"
32 #include "timevar.h"
33 #include "params.h"
34 #ifdef ENABLE_VALGRIND_CHECKING
35 #include <valgrind.h>
36 #else
37 /* Avoid #ifdef:s when we can help it. */
38 #define VALGRIND_DISCARD(x)
39 #endif
40
41 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
42 file open. Prefer either to valloc. */
43 #ifdef HAVE_MMAP_ANON
44 # undef HAVE_MMAP_DEV_ZERO
45
46 # include <sys/mman.h>
47 # ifndef MAP_FAILED
48 # define MAP_FAILED -1
49 # endif
50 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
51 # define MAP_ANONYMOUS MAP_ANON
52 # endif
53 # define USING_MMAP
54
55 #endif
56
57 #ifdef HAVE_MMAP_DEV_ZERO
58
59 # include <sys/mman.h>
60 # ifndef MAP_FAILED
61 # define MAP_FAILED -1
62 # endif
63 # define USING_MMAP
64
65 #endif
66
67 #ifndef USING_MMAP
68 #define USING_MALLOC_PAGE_GROUPS
69 #endif
70
71 /* Stategy:
72
73 This garbage-collecting allocator allocates objects on one of a set
74 of pages. Each page can allocate objects of a single size only;
75 available sizes are powers of two starting at four bytes. The size
76 of an allocation request is rounded up to the next power of two
77 (`order'), and satisfied from the appropriate page.
78
79 Each page is recorded in a page-entry, which also maintains an
80 in-use bitmap of object positions on the page. This allows the
81 allocation state of a particular object to be flipped without
82 touching the page itself.
83
84 Each page-entry also has a context depth, which is used to track
85 pushing and popping of allocation contexts. Only objects allocated
86 in the current (highest-numbered) context may be collected.
87
88 Page entries are arranged in an array of singly-linked lists. The
89 array is indexed by the allocation size, in bits, of the pages on
90 it; i.e. all pages on a list allocate objects of the same size.
91 Pages are ordered on the list such that all non-full pages precede
92 all full pages, with non-full pages arranged in order of decreasing
93 context depth.
94
95 Empty pages (of all orders) are kept on a single page cache list,
96 and are considered first when new pages are required; they are
97 deallocated at the start of the next collection if they haven't
98 been recycled by then. */
99
100 /* Define GGC_DEBUG_LEVEL to print debugging information.
101 0: No debugging output.
102 1: GC statistics only.
103 2: Page-entry allocations/deallocations as well.
104 3: Object allocations as well.
105 4: Object marks as well. */
106 #define GGC_DEBUG_LEVEL (0)
107 \f
108 #ifndef HOST_BITS_PER_PTR
109 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
110 #endif
111
112 \f
113 /* A two-level tree is used to look up the page-entry for a given
114 pointer. Two chunks of the pointer's bits are extracted to index
115 the first and second levels of the tree, as follows:
116
117 HOST_PAGE_SIZE_BITS
118 32 | |
119 msb +----------------+----+------+------+ lsb
120 | | |
121 PAGE_L1_BITS |
122 | |
123 PAGE_L2_BITS
124
125 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
126 pages are aligned on system page boundaries. The next most
127 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
128 index values in the lookup table, respectively.
129
130 For 32-bit architectures and the settings below, there are no
131 leftover bits. For architectures with wider pointers, the lookup
132 tree points to a list of pages, which must be scanned to find the
133 correct one. */
134
135 #define PAGE_L1_BITS (8)
136 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
137 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
138 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
139
140 #define LOOKUP_L1(p) \
141 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
142
143 #define LOOKUP_L2(p) \
144 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
145
146 /* The number of objects per allocation page, for objects on a page of
147 the indicated ORDER. */
148 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
149
150 /* The size of an object on a page of the indicated ORDER. */
151 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
152
153 /* For speed, we avoid doing a general integer divide to locate the
154 offset in the allocation bitmap, by precalculating numbers M, S
155 such that (O * M) >> S == O / Z (modulo 2^32), for any offset O
156 within the page which is evenly divisible by the object size Z. */
157 #define DIV_MULT(ORDER) inverse_table[ORDER].mult
158 #define DIV_SHIFT(ORDER) inverse_table[ORDER].shift
159 #define OFFSET_TO_BIT(OFFSET, ORDER) \
160 (((OFFSET) * DIV_MULT (ORDER)) >> DIV_SHIFT (ORDER))
161
162 /* The number of extra orders, not corresponding to power-of-two sized
163 objects. */
164
165 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
166
167 #define RTL_SIZE(NSLOTS) \
168 (sizeof (struct rtx_def) + ((NSLOTS) - 1) * sizeof (rtunion))
169
170 /* The Ith entry is the maximum size of an object to be stored in the
171 Ith extra order. Adding a new entry to this array is the *only*
172 thing you need to do to add a new special allocation size. */
173
174 static const size_t extra_order_size_table[] = {
175 sizeof (struct tree_decl),
176 sizeof (struct tree_list),
177 RTL_SIZE (2), /* REG, MEM, PLUS, etc. */
178 RTL_SIZE (10), /* INSN, CALL_INSN, JUMP_INSN */
179 };
180
181 /* The total number of orders. */
182
183 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
184
185 /* We use this structure to determine the alignment required for
186 allocations. For power-of-two sized allocations, that's not a
187 problem, but it does matter for odd-sized allocations. */
188
189 struct max_alignment {
190 char c;
191 union {
192 HOST_WIDEST_INT i;
193 #ifdef HAVE_LONG_DOUBLE
194 long double d;
195 #else
196 double d;
197 #endif
198 } u;
199 };
200
201 /* The biggest alignment required. */
202
203 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
204
205 /* The Ith entry is the number of objects on a page or order I. */
206
207 static unsigned objects_per_page_table[NUM_ORDERS];
208
209 /* The Ith entry is the size of an object on a page of order I. */
210
211 static size_t object_size_table[NUM_ORDERS];
212
213 /* The Ith entry is a pair of numbers (mult, shift) such that
214 ((k * mult) >> shift) mod 2^32 == (k / OBJECT_SIZE(I)) mod 2^32,
215 for all k evenly divisible by OBJECT_SIZE(I). */
216
217 static struct
218 {
219 unsigned int mult;
220 unsigned int shift;
221 }
222 inverse_table[NUM_ORDERS];
223
224 /* A page_entry records the status of an allocation page. This
225 structure is dynamically sized to fit the bitmap in_use_p. */
226 typedef struct page_entry
227 {
228 /* The next page-entry with objects of the same size, or NULL if
229 this is the last page-entry. */
230 struct page_entry *next;
231
232 /* The number of bytes allocated. (This will always be a multiple
233 of the host system page size.) */
234 size_t bytes;
235
236 /* The address at which the memory is allocated. */
237 char *page;
238
239 #ifdef USING_MALLOC_PAGE_GROUPS
240 /* Back pointer to the page group this page came from. */
241 struct page_group *group;
242 #endif
243
244 /* Saved in-use bit vector for pages that aren't in the topmost
245 context during collection. */
246 unsigned long *save_in_use_p;
247
248 /* Context depth of this page. */
249 unsigned short context_depth;
250
251 /* The number of free objects remaining on this page. */
252 unsigned short num_free_objects;
253
254 /* A likely candidate for the bit position of a free object for the
255 next allocation from this page. */
256 unsigned short next_bit_hint;
257
258 /* The lg of size of objects allocated from this page. */
259 unsigned char order;
260
261 /* A bit vector indicating whether or not objects are in use. The
262 Nth bit is one if the Nth object on this page is allocated. This
263 array is dynamically sized. */
264 unsigned long in_use_p[1];
265 } page_entry;
266
267 #ifdef USING_MALLOC_PAGE_GROUPS
268 /* A page_group describes a large allocation from malloc, from which
269 we parcel out aligned pages. */
270 typedef struct page_group
271 {
272 /* A linked list of all extant page groups. */
273 struct page_group *next;
274
275 /* The address we received from malloc. */
276 char *allocation;
277
278 /* The size of the block. */
279 size_t alloc_size;
280
281 /* A bitmask of pages in use. */
282 unsigned int in_use;
283 } page_group;
284 #endif
285
286 #if HOST_BITS_PER_PTR <= 32
287
288 /* On 32-bit hosts, we use a two level page table, as pictured above. */
289 typedef page_entry **page_table[PAGE_L1_SIZE];
290
291 #else
292
293 /* On 64-bit hosts, we use the same two level page tables plus a linked
294 list that disambiguates the top 32-bits. There will almost always be
295 exactly one entry in the list. */
296 typedef struct page_table_chain
297 {
298 struct page_table_chain *next;
299 size_t high_bits;
300 page_entry **table[PAGE_L1_SIZE];
301 } *page_table;
302
303 #endif
304
305 /* The rest of the global variables. */
306 static struct globals
307 {
308 /* The Nth element in this array is a page with objects of size 2^N.
309 If there are any pages with free objects, they will be at the
310 head of the list. NULL if there are no page-entries for this
311 object size. */
312 page_entry *pages[NUM_ORDERS];
313
314 /* The Nth element in this array is the last page with objects of
315 size 2^N. NULL if there are no page-entries for this object
316 size. */
317 page_entry *page_tails[NUM_ORDERS];
318
319 /* Lookup table for associating allocation pages with object addresses. */
320 page_table lookup;
321
322 /* The system's page size. */
323 size_t pagesize;
324 size_t lg_pagesize;
325
326 /* Bytes currently allocated. */
327 size_t allocated;
328
329 /* Bytes currently allocated at the end of the last collection. */
330 size_t allocated_last_gc;
331
332 /* Total amount of memory mapped. */
333 size_t bytes_mapped;
334
335 /* The current depth in the context stack. */
336 unsigned short context_depth;
337
338 /* A file descriptor open to /dev/zero for reading. */
339 #if defined (HAVE_MMAP_DEV_ZERO)
340 int dev_zero_fd;
341 #endif
342
343 /* A cache of free system pages. */
344 page_entry *free_pages;
345
346 #ifdef USING_MALLOC_PAGE_GROUPS
347 page_group *page_groups;
348 #endif
349
350 /* The file descriptor for debugging output. */
351 FILE *debug_file;
352 } G;
353
354 /* The size in bytes required to maintain a bitmap for the objects
355 on a page-entry. */
356 #define BITMAP_SIZE(Num_objects) \
357 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
358
359 /* Allocate pages in chunks of this size, to throttle calls to memory
360 allocation routines. The first page is used, the rest go onto the
361 free list. This cannot be larger than HOST_BITS_PER_INT for the
362 in_use bitmask for page_group. */
363 #define GGC_QUIRE_SIZE 16
364 \f
365 static int ggc_allocated_p PARAMS ((const void *));
366 static page_entry *lookup_page_table_entry PARAMS ((const void *));
367 static void set_page_table_entry PARAMS ((void *, page_entry *));
368 #ifdef USING_MMAP
369 static char *alloc_anon PARAMS ((char *, size_t));
370 #endif
371 #ifdef USING_MALLOC_PAGE_GROUPS
372 static size_t page_group_index PARAMS ((char *, char *));
373 static void set_page_group_in_use PARAMS ((page_group *, char *));
374 static void clear_page_group_in_use PARAMS ((page_group *, char *));
375 #endif
376 static struct page_entry * alloc_page PARAMS ((unsigned));
377 static void free_page PARAMS ((struct page_entry *));
378 static void release_pages PARAMS ((void));
379 static void clear_marks PARAMS ((void));
380 static void sweep_pages PARAMS ((void));
381 static void ggc_recalculate_in_use_p PARAMS ((page_entry *));
382 static void compute_inverse PARAMS ((unsigned));
383
384 #ifdef ENABLE_GC_CHECKING
385 static void poison_pages PARAMS ((void));
386 #endif
387
388 void debug_print_page_list PARAMS ((int));
389 \f
390 /* Returns nonzero if P was allocated in GC'able memory. */
391
392 static inline int
393 ggc_allocated_p (p)
394 const void *p;
395 {
396 page_entry ***base;
397 size_t L1, L2;
398
399 #if HOST_BITS_PER_PTR <= 32
400 base = &G.lookup[0];
401 #else
402 page_table table = G.lookup;
403 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
404 while (1)
405 {
406 if (table == NULL)
407 return 0;
408 if (table->high_bits == high_bits)
409 break;
410 table = table->next;
411 }
412 base = &table->table[0];
413 #endif
414
415 /* Extract the level 1 and 2 indices. */
416 L1 = LOOKUP_L1 (p);
417 L2 = LOOKUP_L2 (p);
418
419 return base[L1] && base[L1][L2];
420 }
421
422 /* Traverse the page table and find the entry for a page.
423 Die (probably) if the object wasn't allocated via GC. */
424
425 static inline page_entry *
426 lookup_page_table_entry(p)
427 const void *p;
428 {
429 page_entry ***base;
430 size_t L1, L2;
431
432 #if HOST_BITS_PER_PTR <= 32
433 base = &G.lookup[0];
434 #else
435 page_table table = G.lookup;
436 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
437 while (table->high_bits != high_bits)
438 table = table->next;
439 base = &table->table[0];
440 #endif
441
442 /* Extract the level 1 and 2 indices. */
443 L1 = LOOKUP_L1 (p);
444 L2 = LOOKUP_L2 (p);
445
446 return base[L1][L2];
447 }
448
449 /* Set the page table entry for a page. */
450
451 static void
452 set_page_table_entry(p, entry)
453 void *p;
454 page_entry *entry;
455 {
456 page_entry ***base;
457 size_t L1, L2;
458
459 #if HOST_BITS_PER_PTR <= 32
460 base = &G.lookup[0];
461 #else
462 page_table table;
463 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
464 for (table = G.lookup; table; table = table->next)
465 if (table->high_bits == high_bits)
466 goto found;
467
468 /* Not found -- allocate a new table. */
469 table = (page_table) xcalloc (1, sizeof(*table));
470 table->next = G.lookup;
471 table->high_bits = high_bits;
472 G.lookup = table;
473 found:
474 base = &table->table[0];
475 #endif
476
477 /* Extract the level 1 and 2 indices. */
478 L1 = LOOKUP_L1 (p);
479 L2 = LOOKUP_L2 (p);
480
481 if (base[L1] == NULL)
482 base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
483
484 base[L1][L2] = entry;
485 }
486
487 /* Prints the page-entry for object size ORDER, for debugging. */
488
489 void
490 debug_print_page_list (order)
491 int order;
492 {
493 page_entry *p;
494 printf ("Head=%p, Tail=%p:\n", (PTR) G.pages[order],
495 (PTR) G.page_tails[order]);
496 p = G.pages[order];
497 while (p != NULL)
498 {
499 printf ("%p(%1d|%3d) -> ", (PTR) p, p->context_depth,
500 p->num_free_objects);
501 p = p->next;
502 }
503 printf ("NULL\n");
504 fflush (stdout);
505 }
506
507 #ifdef USING_MMAP
508 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
509 (if non-null). The ifdef structure here is intended to cause a
510 compile error unless exactly one of the HAVE_* is defined. */
511
512 static inline char *
513 alloc_anon (pref, size)
514 char *pref ATTRIBUTE_UNUSED;
515 size_t size;
516 {
517 #ifdef HAVE_MMAP_ANON
518 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
519 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
520 #endif
521 #ifdef HAVE_MMAP_DEV_ZERO
522 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
523 MAP_PRIVATE, G.dev_zero_fd, 0);
524 #endif
525
526 if (page == (char *) MAP_FAILED)
527 {
528 perror ("virtual memory exhausted");
529 exit (FATAL_EXIT_CODE);
530 }
531
532 /* Remember that we allocated this memory. */
533 G.bytes_mapped += size;
534
535 /* Pretend we don't have access to the allocated pages. We'll enable
536 access to smaller pieces of the area in ggc_alloc. Discard the
537 handle to avoid handle leak. */
538 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (page, size));
539
540 return page;
541 }
542 #endif
543 #ifdef USING_MALLOC_PAGE_GROUPS
544 /* Compute the index for this page into the page group. */
545
546 static inline size_t
547 page_group_index (allocation, page)
548 char *allocation, *page;
549 {
550 return (size_t) (page - allocation) >> G.lg_pagesize;
551 }
552
553 /* Set and clear the in_use bit for this page in the page group. */
554
555 static inline void
556 set_page_group_in_use (group, page)
557 page_group *group;
558 char *page;
559 {
560 group->in_use |= 1 << page_group_index (group->allocation, page);
561 }
562
563 static inline void
564 clear_page_group_in_use (group, page)
565 page_group *group;
566 char *page;
567 {
568 group->in_use &= ~(1 << page_group_index (group->allocation, page));
569 }
570 #endif
571
572 /* Allocate a new page for allocating objects of size 2^ORDER,
573 and return an entry for it. The entry is not added to the
574 appropriate page_table list. */
575
576 static inline struct page_entry *
577 alloc_page (order)
578 unsigned order;
579 {
580 struct page_entry *entry, *p, **pp;
581 char *page;
582 size_t num_objects;
583 size_t bitmap_size;
584 size_t page_entry_size;
585 size_t entry_size;
586 #ifdef USING_MALLOC_PAGE_GROUPS
587 page_group *group;
588 #endif
589
590 num_objects = OBJECTS_PER_PAGE (order);
591 bitmap_size = BITMAP_SIZE (num_objects + 1);
592 page_entry_size = sizeof (page_entry) - sizeof (long) + bitmap_size;
593 entry_size = num_objects * OBJECT_SIZE (order);
594 if (entry_size < G.pagesize)
595 entry_size = G.pagesize;
596
597 entry = NULL;
598 page = NULL;
599
600 /* Check the list of free pages for one we can use. */
601 for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
602 if (p->bytes == entry_size)
603 break;
604
605 if (p != NULL)
606 {
607 /* Recycle the allocated memory from this page ... */
608 *pp = p->next;
609 page = p->page;
610
611 #ifdef USING_MALLOC_PAGE_GROUPS
612 group = p->group;
613 #endif
614
615 /* ... and, if possible, the page entry itself. */
616 if (p->order == order)
617 {
618 entry = p;
619 memset (entry, 0, page_entry_size);
620 }
621 else
622 free (p);
623 }
624 #ifdef USING_MMAP
625 else if (entry_size == G.pagesize)
626 {
627 /* We want just one page. Allocate a bunch of them and put the
628 extras on the freelist. (Can only do this optimization with
629 mmap for backing store.) */
630 struct page_entry *e, *f = G.free_pages;
631 int i;
632
633 page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE);
634
635 /* This loop counts down so that the chain will be in ascending
636 memory order. */
637 for (i = GGC_QUIRE_SIZE - 1; i >= 1; i--)
638 {
639 e = (struct page_entry *) xcalloc (1, page_entry_size);
640 e->order = order;
641 e->bytes = G.pagesize;
642 e->page = page + (i << G.lg_pagesize);
643 e->next = f;
644 f = e;
645 }
646
647 G.free_pages = f;
648 }
649 else
650 page = alloc_anon (NULL, entry_size);
651 #endif
652 #ifdef USING_MALLOC_PAGE_GROUPS
653 else
654 {
655 /* Allocate a large block of memory and serve out the aligned
656 pages therein. This results in much less memory wastage
657 than the traditional implementation of valloc. */
658
659 char *allocation, *a, *enda;
660 size_t alloc_size, head_slop, tail_slop;
661 int multiple_pages = (entry_size == G.pagesize);
662
663 if (multiple_pages)
664 alloc_size = GGC_QUIRE_SIZE * G.pagesize;
665 else
666 alloc_size = entry_size + G.pagesize - 1;
667 allocation = xmalloc (alloc_size);
668
669 page = (char *) (((size_t) allocation + G.pagesize - 1) & -G.pagesize);
670 head_slop = page - allocation;
671 if (multiple_pages)
672 tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
673 else
674 tail_slop = alloc_size - entry_size - head_slop;
675 enda = allocation + alloc_size - tail_slop;
676
677 /* We allocated N pages, which are likely not aligned, leaving
678 us with N-1 usable pages. We plan to place the page_group
679 structure somewhere in the slop. */
680 if (head_slop >= sizeof (page_group))
681 group = (page_group *)page - 1;
682 else
683 {
684 /* We magically got an aligned allocation. Too bad, we have
685 to waste a page anyway. */
686 if (tail_slop == 0)
687 {
688 enda -= G.pagesize;
689 tail_slop += G.pagesize;
690 }
691 if (tail_slop < sizeof (page_group))
692 abort ();
693 group = (page_group *)enda;
694 tail_slop -= sizeof (page_group);
695 }
696
697 /* Remember that we allocated this memory. */
698 group->next = G.page_groups;
699 group->allocation = allocation;
700 group->alloc_size = alloc_size;
701 group->in_use = 0;
702 G.page_groups = group;
703 G.bytes_mapped += alloc_size;
704
705 /* If we allocated multiple pages, put the rest on the free list. */
706 if (multiple_pages)
707 {
708 struct page_entry *e, *f = G.free_pages;
709 for (a = enda - G.pagesize; a != page; a -= G.pagesize)
710 {
711 e = (struct page_entry *) xcalloc (1, page_entry_size);
712 e->order = order;
713 e->bytes = G.pagesize;
714 e->page = a;
715 e->group = group;
716 e->next = f;
717 f = e;
718 }
719 G.free_pages = f;
720 }
721 }
722 #endif
723
724 if (entry == NULL)
725 entry = (struct page_entry *) xcalloc (1, page_entry_size);
726
727 entry->bytes = entry_size;
728 entry->page = page;
729 entry->context_depth = G.context_depth;
730 entry->order = order;
731 entry->num_free_objects = num_objects;
732 entry->next_bit_hint = 1;
733
734 #ifdef USING_MALLOC_PAGE_GROUPS
735 entry->group = group;
736 set_page_group_in_use (group, page);
737 #endif
738
739 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
740 increment the hint. */
741 entry->in_use_p[num_objects / HOST_BITS_PER_LONG]
742 = (unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG);
743
744 set_page_table_entry (page, entry);
745
746 if (GGC_DEBUG_LEVEL >= 2)
747 fprintf (G.debug_file,
748 "Allocating page at %p, object size=%lu, data %p-%p\n",
749 (PTR) entry, (unsigned long) OBJECT_SIZE (order), page,
750 page + entry_size - 1);
751
752 return entry;
753 }
754
755 /* For a page that is no longer needed, put it on the free page list. */
756
757 static inline void
758 free_page (entry)
759 page_entry *entry;
760 {
761 if (GGC_DEBUG_LEVEL >= 2)
762 fprintf (G.debug_file,
763 "Deallocating page at %p, data %p-%p\n", (PTR) entry,
764 entry->page, entry->page + entry->bytes - 1);
765
766 /* Mark the page as inaccessible. Discard the handle to avoid handle
767 leak. */
768 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (entry->page, entry->bytes));
769
770 set_page_table_entry (entry->page, NULL);
771
772 #ifdef USING_MALLOC_PAGE_GROUPS
773 clear_page_group_in_use (entry->group, entry->page);
774 #endif
775
776 entry->next = G.free_pages;
777 G.free_pages = entry;
778 }
779
780 /* Release the free page cache to the system. */
781
782 static void
783 release_pages ()
784 {
785 #ifdef USING_MMAP
786 page_entry *p, *next;
787 char *start;
788 size_t len;
789
790 /* Gather up adjacent pages so they are unmapped together. */
791 p = G.free_pages;
792
793 while (p)
794 {
795 start = p->page;
796 next = p->next;
797 len = p->bytes;
798 free (p);
799 p = next;
800
801 while (p && p->page == start + len)
802 {
803 next = p->next;
804 len += p->bytes;
805 free (p);
806 p = next;
807 }
808
809 munmap (start, len);
810 G.bytes_mapped -= len;
811 }
812
813 G.free_pages = NULL;
814 #endif
815 #ifdef USING_MALLOC_PAGE_GROUPS
816 page_entry **pp, *p;
817 page_group **gp, *g;
818
819 /* Remove all pages from free page groups from the list. */
820 pp = &G.free_pages;
821 while ((p = *pp) != NULL)
822 if (p->group->in_use == 0)
823 {
824 *pp = p->next;
825 free (p);
826 }
827 else
828 pp = &p->next;
829
830 /* Remove all free page groups, and release the storage. */
831 gp = &G.page_groups;
832 while ((g = *gp) != NULL)
833 if (g->in_use == 0)
834 {
835 *gp = g->next;
836 G.bytes_mapped -= g->alloc_size;
837 free (g->allocation);
838 }
839 else
840 gp = &g->next;
841 #endif
842 }
843
844 /* This table provides a fast way to determine ceil(log_2(size)) for
845 allocation requests. The minimum allocation size is eight bytes. */
846
847 static unsigned char size_lookup[257] =
848 {
849 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
850 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
851 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
852 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
853 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
854 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
855 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
856 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
857 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
858 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
859 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
860 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
861 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
862 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
863 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
864 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
865 8
866 };
867
868 /* Allocate a chunk of memory of SIZE bytes. If ZERO is nonzero, the
869 memory is zeroed; otherwise, its contents are undefined. */
870
871 void *
872 ggc_alloc (size)
873 size_t size;
874 {
875 unsigned order, word, bit, object_offset;
876 struct page_entry *entry;
877 void *result;
878
879 if (size <= 256)
880 order = size_lookup[size];
881 else
882 {
883 order = 9;
884 while (size > OBJECT_SIZE (order))
885 order++;
886 }
887
888 /* If there are non-full pages for this size allocation, they are at
889 the head of the list. */
890 entry = G.pages[order];
891
892 /* If there is no page for this object size, or all pages in this
893 context are full, allocate a new page. */
894 if (entry == NULL || entry->num_free_objects == 0)
895 {
896 struct page_entry *new_entry;
897 new_entry = alloc_page (order);
898
899 /* If this is the only entry, it's also the tail. */
900 if (entry == NULL)
901 G.page_tails[order] = new_entry;
902
903 /* Put new pages at the head of the page list. */
904 new_entry->next = entry;
905 entry = new_entry;
906 G.pages[order] = new_entry;
907
908 /* For a new page, we know the word and bit positions (in the
909 in_use bitmap) of the first available object -- they're zero. */
910 new_entry->next_bit_hint = 1;
911 word = 0;
912 bit = 0;
913 object_offset = 0;
914 }
915 else
916 {
917 /* First try to use the hint left from the previous allocation
918 to locate a clear bit in the in-use bitmap. We've made sure
919 that the one-past-the-end bit is always set, so if the hint
920 has run over, this test will fail. */
921 unsigned hint = entry->next_bit_hint;
922 word = hint / HOST_BITS_PER_LONG;
923 bit = hint % HOST_BITS_PER_LONG;
924
925 /* If the hint didn't work, scan the bitmap from the beginning. */
926 if ((entry->in_use_p[word] >> bit) & 1)
927 {
928 word = bit = 0;
929 while (~entry->in_use_p[word] == 0)
930 ++word;
931 while ((entry->in_use_p[word] >> bit) & 1)
932 ++bit;
933 hint = word * HOST_BITS_PER_LONG + bit;
934 }
935
936 /* Next time, try the next bit. */
937 entry->next_bit_hint = hint + 1;
938
939 object_offset = hint * OBJECT_SIZE (order);
940 }
941
942 /* Set the in-use bit. */
943 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
944
945 /* Keep a running total of the number of free objects. If this page
946 fills up, we may have to move it to the end of the list if the
947 next page isn't full. If the next page is full, all subsequent
948 pages are full, so there's no need to move it. */
949 if (--entry->num_free_objects == 0
950 && entry->next != NULL
951 && entry->next->num_free_objects > 0)
952 {
953 G.pages[order] = entry->next;
954 entry->next = NULL;
955 G.page_tails[order]->next = entry;
956 G.page_tails[order] = entry;
957 }
958
959 /* Calculate the object's address. */
960 result = entry->page + object_offset;
961
962 #ifdef ENABLE_GC_CHECKING
963 /* Keep poisoning-by-writing-0xaf the object, in an attempt to keep the
964 exact same semantics in presence of memory bugs, regardless of
965 ENABLE_VALGRIND_CHECKING. We override this request below. Drop the
966 handle to avoid handle leak. */
967 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, OBJECT_SIZE (order)));
968
969 /* `Poison' the entire allocated object, including any padding at
970 the end. */
971 memset (result, 0xaf, OBJECT_SIZE (order));
972
973 /* Make the bytes after the end of the object unaccessible. Discard the
974 handle to avoid handle leak. */
975 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS ((char *) result + size,
976 OBJECT_SIZE (order) - size));
977 #endif
978
979 /* Tell Valgrind that the memory is there, but its content isn't
980 defined. The bytes at the end of the object are still marked
981 unaccessible. */
982 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (result, size));
983
984 /* Keep track of how many bytes are being allocated. This
985 information is used in deciding when to collect. */
986 G.allocated += OBJECT_SIZE (order);
987
988 if (GGC_DEBUG_LEVEL >= 3)
989 fprintf (G.debug_file,
990 "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
991 (unsigned long) size, (unsigned long) OBJECT_SIZE (order), result,
992 (PTR) entry);
993
994 return result;
995 }
996
997 /* If P is not marked, marks it and return false. Otherwise return true.
998 P must have been allocated by the GC allocator; it mustn't point to
999 static objects, stack variables, or memory allocated with malloc. */
1000
1001 int
1002 ggc_set_mark (p)
1003 const void *p;
1004 {
1005 page_entry *entry;
1006 unsigned bit, word;
1007 unsigned long mask;
1008
1009 /* Look up the page on which the object is alloced. If the object
1010 wasn't allocated by the collector, we'll probably die. */
1011 entry = lookup_page_table_entry (p);
1012 #ifdef ENABLE_CHECKING
1013 if (entry == NULL)
1014 abort ();
1015 #endif
1016
1017 /* Calculate the index of the object on the page; this is its bit
1018 position in the in_use_p bitmap. */
1019 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1020 word = bit / HOST_BITS_PER_LONG;
1021 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1022
1023 /* If the bit was previously set, skip it. */
1024 if (entry->in_use_p[word] & mask)
1025 return 1;
1026
1027 /* Otherwise set it, and decrement the free object count. */
1028 entry->in_use_p[word] |= mask;
1029 entry->num_free_objects -= 1;
1030
1031 if (GGC_DEBUG_LEVEL >= 4)
1032 fprintf (G.debug_file, "Marking %p\n", p);
1033
1034 return 0;
1035 }
1036
1037 /* Return 1 if P has been marked, zero otherwise.
1038 P must have been allocated by the GC allocator; it mustn't point to
1039 static objects, stack variables, or memory allocated with malloc. */
1040
1041 int
1042 ggc_marked_p (p)
1043 const void *p;
1044 {
1045 page_entry *entry;
1046 unsigned bit, word;
1047 unsigned long mask;
1048
1049 /* Look up the page on which the object is alloced. If the object
1050 wasn't allocated by the collector, we'll probably die. */
1051 entry = lookup_page_table_entry (p);
1052 #ifdef ENABLE_CHECKING
1053 if (entry == NULL)
1054 abort ();
1055 #endif
1056
1057 /* Calculate the index of the object on the page; this is its bit
1058 position in the in_use_p bitmap. */
1059 bit = OFFSET_TO_BIT (((const char *) p) - entry->page, entry->order);
1060 word = bit / HOST_BITS_PER_LONG;
1061 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1062
1063 return (entry->in_use_p[word] & mask) != 0;
1064 }
1065
1066 /* Return the size of the gc-able object P. */
1067
1068 size_t
1069 ggc_get_size (p)
1070 const void *p;
1071 {
1072 page_entry *pe = lookup_page_table_entry (p);
1073 return OBJECT_SIZE (pe->order);
1074 }
1075 \f
1076 /* Subroutine of init_ggc which computes the pair of numbers used to
1077 perform division by OBJECT_SIZE (order) and fills in inverse_table[].
1078
1079 This algorithm is taken from Granlund and Montgomery's paper
1080 "Division by Invariant Integers using Multiplication"
1081 (Proc. SIGPLAN PLDI, 1994), section 9 (Exact division by
1082 constants). */
1083
1084 static void
1085 compute_inverse (order)
1086 unsigned order;
1087 {
1088 unsigned size, inv, e;
1089
1090 /* There can be only one object per "page" in a bucket for sizes
1091 larger than half a machine page; it will always have offset zero. */
1092 if (OBJECT_SIZE (order) > G.pagesize/2)
1093 {
1094 if (OBJECTS_PER_PAGE (order) != 1)
1095 abort ();
1096
1097 DIV_MULT (order) = 1;
1098 DIV_SHIFT (order) = 0;
1099 return;
1100 }
1101
1102 size = OBJECT_SIZE (order);
1103 e = 0;
1104 while (size % 2 == 0)
1105 {
1106 e++;
1107 size >>= 1;
1108 }
1109
1110 inv = size;
1111 while (inv * size != 1)
1112 inv = inv * (2 - inv*size);
1113
1114 DIV_MULT (order) = inv;
1115 DIV_SHIFT (order) = e;
1116 }
1117
1118 /* Initialize the ggc-mmap allocator. */
1119 void
1120 init_ggc ()
1121 {
1122 unsigned order;
1123
1124 G.pagesize = getpagesize();
1125 G.lg_pagesize = exact_log2 (G.pagesize);
1126
1127 #ifdef HAVE_MMAP_DEV_ZERO
1128 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1129 if (G.dev_zero_fd == -1)
1130 abort ();
1131 #endif
1132
1133 #if 0
1134 G.debug_file = fopen ("ggc-mmap.debug", "w");
1135 #else
1136 G.debug_file = stdout;
1137 #endif
1138
1139 #ifdef USING_MMAP
1140 /* StunOS has an amazing off-by-one error for the first mmap allocation
1141 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1142 believe, is an unaligned page allocation, which would cause us to
1143 hork badly if we tried to use it. */
1144 {
1145 char *p = alloc_anon (NULL, G.pagesize);
1146 struct page_entry *e;
1147 if ((size_t)p & (G.pagesize - 1))
1148 {
1149 /* How losing. Discard this one and try another. If we still
1150 can't get something useful, give up. */
1151
1152 p = alloc_anon (NULL, G.pagesize);
1153 if ((size_t)p & (G.pagesize - 1))
1154 abort ();
1155 }
1156
1157 /* We have a good page, might as well hold onto it... */
1158 e = (struct page_entry *) xcalloc (1, sizeof (struct page_entry));
1159 e->bytes = G.pagesize;
1160 e->page = p;
1161 e->next = G.free_pages;
1162 G.free_pages = e;
1163 }
1164 #endif
1165
1166 /* Initialize the object size table. */
1167 for (order = 0; order < HOST_BITS_PER_PTR; ++order)
1168 object_size_table[order] = (size_t) 1 << order;
1169 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1170 {
1171 size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
1172
1173 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1174 so that we're sure of getting aligned memory. */
1175 s = CEIL (s, MAX_ALIGNMENT) * MAX_ALIGNMENT;
1176 object_size_table[order] = s;
1177 }
1178
1179 /* Initialize the objects-per-page and inverse tables. */
1180 for (order = 0; order < NUM_ORDERS; ++order)
1181 {
1182 objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
1183 if (objects_per_page_table[order] == 0)
1184 objects_per_page_table[order] = 1;
1185 compute_inverse (order);
1186 }
1187
1188 /* Reset the size_lookup array to put appropriately sized objects in
1189 the special orders. All objects bigger than the previous power
1190 of two, but no greater than the special size, should go in the
1191 new order. */
1192 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1193 {
1194 int o;
1195 int i;
1196
1197 o = size_lookup[OBJECT_SIZE (order)];
1198 for (i = OBJECT_SIZE (order); size_lookup [i] == o; --i)
1199 size_lookup[i] = order;
1200 }
1201 }
1202
1203 /* Increment the `GC context'. Objects allocated in an outer context
1204 are never freed, eliminating the need to register their roots. */
1205
1206 void
1207 ggc_push_context ()
1208 {
1209 ++G.context_depth;
1210
1211 /* Die on wrap. */
1212 if (G.context_depth == 0)
1213 abort ();
1214 }
1215
1216 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1217 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1218
1219 static void
1220 ggc_recalculate_in_use_p (p)
1221 page_entry *p;
1222 {
1223 unsigned int i;
1224 size_t num_objects;
1225
1226 /* Because the past-the-end bit in in_use_p is always set, we
1227 pretend there is one additional object. */
1228 num_objects = OBJECTS_PER_PAGE (p->order) + 1;
1229
1230 /* Reset the free object count. */
1231 p->num_free_objects = num_objects;
1232
1233 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1234 for (i = 0;
1235 i < CEIL (BITMAP_SIZE (num_objects),
1236 sizeof (*p->in_use_p));
1237 ++i)
1238 {
1239 unsigned long j;
1240
1241 /* Something is in use if it is marked, or if it was in use in a
1242 context further down the context stack. */
1243 p->in_use_p[i] |= p->save_in_use_p[i];
1244
1245 /* Decrement the free object count for every object allocated. */
1246 for (j = p->in_use_p[i]; j; j >>= 1)
1247 p->num_free_objects -= (j & 1);
1248 }
1249
1250 if (p->num_free_objects >= num_objects)
1251 abort ();
1252 }
1253
1254 /* Decrement the `GC context'. All objects allocated since the
1255 previous ggc_push_context are migrated to the outer context. */
1256
1257 void
1258 ggc_pop_context ()
1259 {
1260 unsigned order, depth;
1261
1262 depth = --G.context_depth;
1263
1264 /* Any remaining pages in the popped context are lowered to the new
1265 current context; i.e. objects allocated in the popped context and
1266 left over are imported into the previous context. */
1267 for (order = 2; order < NUM_ORDERS; order++)
1268 {
1269 page_entry *p;
1270
1271 for (p = G.pages[order]; p != NULL; p = p->next)
1272 {
1273 if (p->context_depth > depth)
1274 p->context_depth = depth;
1275
1276 /* If this page is now in the topmost context, and we'd
1277 saved its allocation state, restore it. */
1278 else if (p->context_depth == depth && p->save_in_use_p)
1279 {
1280 ggc_recalculate_in_use_p (p);
1281 free (p->save_in_use_p);
1282 p->save_in_use_p = 0;
1283 }
1284 }
1285 }
1286 }
1287 \f
1288 /* Unmark all objects. */
1289
1290 static inline void
1291 clear_marks ()
1292 {
1293 unsigned order;
1294
1295 for (order = 2; order < NUM_ORDERS; order++)
1296 {
1297 size_t num_objects = OBJECTS_PER_PAGE (order);
1298 size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
1299 page_entry *p;
1300
1301 for (p = G.pages[order]; p != NULL; p = p->next)
1302 {
1303 #ifdef ENABLE_CHECKING
1304 /* The data should be page-aligned. */
1305 if ((size_t) p->page & (G.pagesize - 1))
1306 abort ();
1307 #endif
1308
1309 /* Pages that aren't in the topmost context are not collected;
1310 nevertheless, we need their in-use bit vectors to store GC
1311 marks. So, back them up first. */
1312 if (p->context_depth < G.context_depth)
1313 {
1314 if (! p->save_in_use_p)
1315 p->save_in_use_p = xmalloc (bitmap_size);
1316 memcpy (p->save_in_use_p, p->in_use_p, bitmap_size);
1317 }
1318
1319 /* Reset reset the number of free objects and clear the
1320 in-use bits. These will be adjusted by mark_obj. */
1321 p->num_free_objects = num_objects;
1322 memset (p->in_use_p, 0, bitmap_size);
1323
1324 /* Make sure the one-past-the-end bit is always set. */
1325 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
1326 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
1327 }
1328 }
1329 }
1330
1331 /* Free all empty pages. Partially empty pages need no attention
1332 because the `mark' bit doubles as an `unused' bit. */
1333
1334 static inline void
1335 sweep_pages ()
1336 {
1337 unsigned order;
1338
1339 for (order = 2; order < NUM_ORDERS; order++)
1340 {
1341 /* The last page-entry to consider, regardless of entries
1342 placed at the end of the list. */
1343 page_entry * const last = G.page_tails[order];
1344
1345 size_t num_objects = OBJECTS_PER_PAGE (order);
1346 size_t live_objects;
1347 page_entry *p, *previous;
1348 int done;
1349
1350 p = G.pages[order];
1351 if (p == NULL)
1352 continue;
1353
1354 previous = NULL;
1355 do
1356 {
1357 page_entry *next = p->next;
1358
1359 /* Loop until all entries have been examined. */
1360 done = (p == last);
1361
1362 /* Add all live objects on this page to the count of
1363 allocated memory. */
1364 live_objects = num_objects - p->num_free_objects;
1365
1366 G.allocated += OBJECT_SIZE (order) * live_objects;
1367
1368 /* Only objects on pages in the topmost context should get
1369 collected. */
1370 if (p->context_depth < G.context_depth)
1371 ;
1372
1373 /* Remove the page if it's empty. */
1374 else if (live_objects == 0)
1375 {
1376 if (! previous)
1377 G.pages[order] = next;
1378 else
1379 previous->next = next;
1380
1381 /* Are we removing the last element? */
1382 if (p == G.page_tails[order])
1383 G.page_tails[order] = previous;
1384 free_page (p);
1385 p = previous;
1386 }
1387
1388 /* If the page is full, move it to the end. */
1389 else if (p->num_free_objects == 0)
1390 {
1391 /* Don't move it if it's already at the end. */
1392 if (p != G.page_tails[order])
1393 {
1394 /* Move p to the end of the list. */
1395 p->next = NULL;
1396 G.page_tails[order]->next = p;
1397
1398 /* Update the tail pointer... */
1399 G.page_tails[order] = p;
1400
1401 /* ... and the head pointer, if necessary. */
1402 if (! previous)
1403 G.pages[order] = next;
1404 else
1405 previous->next = next;
1406 p = previous;
1407 }
1408 }
1409
1410 /* If we've fallen through to here, it's a page in the
1411 topmost context that is neither full nor empty. Such a
1412 page must precede pages at lesser context depth in the
1413 list, so move it to the head. */
1414 else if (p != G.pages[order])
1415 {
1416 previous->next = p->next;
1417 p->next = G.pages[order];
1418 G.pages[order] = p;
1419 /* Are we moving the last element? */
1420 if (G.page_tails[order] == p)
1421 G.page_tails[order] = previous;
1422 p = previous;
1423 }
1424
1425 previous = p;
1426 p = next;
1427 }
1428 while (! done);
1429
1430 /* Now, restore the in_use_p vectors for any pages from contexts
1431 other than the current one. */
1432 for (p = G.pages[order]; p; p = p->next)
1433 if (p->context_depth != G.context_depth)
1434 ggc_recalculate_in_use_p (p);
1435 }
1436 }
1437
1438 #ifdef ENABLE_GC_CHECKING
1439 /* Clobber all free objects. */
1440
1441 static inline void
1442 poison_pages ()
1443 {
1444 unsigned order;
1445
1446 for (order = 2; order < NUM_ORDERS; order++)
1447 {
1448 size_t num_objects = OBJECTS_PER_PAGE (order);
1449 size_t size = OBJECT_SIZE (order);
1450 page_entry *p;
1451
1452 for (p = G.pages[order]; p != NULL; p = p->next)
1453 {
1454 size_t i;
1455
1456 if (p->context_depth != G.context_depth)
1457 /* Since we don't do any collection for pages in pushed
1458 contexts, there's no need to do any poisoning. And
1459 besides, the IN_USE_P array isn't valid until we pop
1460 contexts. */
1461 continue;
1462
1463 for (i = 0; i < num_objects; i++)
1464 {
1465 size_t word, bit;
1466 word = i / HOST_BITS_PER_LONG;
1467 bit = i % HOST_BITS_PER_LONG;
1468 if (((p->in_use_p[word] >> bit) & 1) == 0)
1469 {
1470 char *object = p->page + i * size;
1471
1472 /* Keep poison-by-write when we expect to use Valgrind,
1473 so the exact same memory semantics is kept, in case
1474 there are memory errors. We override this request
1475 below. */
1476 VALGRIND_DISCARD (VALGRIND_MAKE_WRITABLE (object, size));
1477 memset (object, 0xa5, size);
1478
1479 /* Drop the handle to avoid handle leak. */
1480 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (object, size));
1481 }
1482 }
1483 }
1484 }
1485 }
1486 #endif
1487
1488 /* Top level mark-and-sweep routine. */
1489
1490 void
1491 ggc_collect ()
1492 {
1493 /* Avoid frequent unnecessary work by skipping collection if the
1494 total allocations haven't expanded much since the last
1495 collection. */
1496 size_t allocated_last_gc =
1497 MAX (G.allocated_last_gc, (size_t)PARAM_VALUE (GGC_MIN_HEAPSIZE) * 1024);
1498
1499 size_t min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
1500
1501 if (G.allocated < allocated_last_gc + min_expand)
1502 return;
1503
1504 timevar_push (TV_GC);
1505 if (!quiet_flag)
1506 fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
1507
1508 /* Zero the total allocated bytes. This will be recalculated in the
1509 sweep phase. */
1510 G.allocated = 0;
1511
1512 /* Release the pages we freed the last time we collected, but didn't
1513 reuse in the interim. */
1514 release_pages ();
1515
1516 clear_marks ();
1517 ggc_mark_roots ();
1518
1519 #ifdef ENABLE_GC_CHECKING
1520 poison_pages ();
1521 #endif
1522
1523 sweep_pages ();
1524
1525 G.allocated_last_gc = G.allocated;
1526
1527 timevar_pop (TV_GC);
1528
1529 if (!quiet_flag)
1530 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
1531 }
1532
1533 /* Print allocation statistics. */
1534 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1535 ? (x) \
1536 : ((x) < 1024*1024*10 \
1537 ? (x) / 1024 \
1538 : (x) / (1024*1024))))
1539 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1540
1541 void
1542 ggc_print_statistics ()
1543 {
1544 struct ggc_statistics stats;
1545 unsigned int i;
1546 size_t total_overhead = 0;
1547
1548 /* Clear the statistics. */
1549 memset (&stats, 0, sizeof (stats));
1550
1551 /* Make sure collection will really occur. */
1552 G.allocated_last_gc = 0;
1553
1554 /* Collect and print the statistics common across collectors. */
1555 ggc_print_common_statistics (stderr, &stats);
1556
1557 /* Release free pages so that we will not count the bytes allocated
1558 there as part of the total allocated memory. */
1559 release_pages ();
1560
1561 /* Collect some information about the various sizes of
1562 allocation. */
1563 fprintf (stderr, "\n%-5s %10s %10s %10s\n",
1564 "Size", "Allocated", "Used", "Overhead");
1565 for (i = 0; i < NUM_ORDERS; ++i)
1566 {
1567 page_entry *p;
1568 size_t allocated;
1569 size_t in_use;
1570 size_t overhead;
1571
1572 /* Skip empty entries. */
1573 if (!G.pages[i])
1574 continue;
1575
1576 overhead = allocated = in_use = 0;
1577
1578 /* Figure out the total number of bytes allocated for objects of
1579 this size, and how many of them are actually in use. Also figure
1580 out how much memory the page table is using. */
1581 for (p = G.pages[i]; p; p = p->next)
1582 {
1583 allocated += p->bytes;
1584 in_use +=
1585 (OBJECTS_PER_PAGE (i) - p->num_free_objects) * OBJECT_SIZE (i);
1586
1587 overhead += (sizeof (page_entry) - sizeof (long)
1588 + BITMAP_SIZE (OBJECTS_PER_PAGE (i) + 1));
1589 }
1590 fprintf (stderr, "%-5lu %10lu%c %10lu%c %10lu%c\n",
1591 (unsigned long) OBJECT_SIZE (i),
1592 SCALE (allocated), LABEL (allocated),
1593 SCALE (in_use), LABEL (in_use),
1594 SCALE (overhead), LABEL (overhead));
1595 total_overhead += overhead;
1596 }
1597 fprintf (stderr, "%-5s %10lu%c %10lu%c %10lu%c\n", "Total",
1598 SCALE (G.bytes_mapped), LABEL (G.bytes_mapped),
1599 SCALE (G.allocated), LABEL(G.allocated),
1600 SCALE (total_overhead), LABEL (total_overhead));
1601 }