cselib.c (cselib_current_insn_in_libcall): New static variable.
[official-gcc.git] / gcc / ggc-page.c
blob537f302578a015c2c3f5030bd7abd224f0413828
1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
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.
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. */
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
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
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
55 #endif
57 #ifdef HAVE_MMAP_DEV_ZERO
59 # include <sys/mman.h>
60 # ifndef MAP_FAILED
61 # define MAP_FAILED -1
62 # endif
63 # define USING_MMAP
65 #endif
67 #ifndef USING_MMAP
68 #define USING_MALLOC_PAGE_GROUPS
69 #endif
71 /* Stategy:
73 This garbage-collecting allocator allocates objects on one of a set
74 of pages. Each page can allocate objects of a single size only;
75 available sizes are powers of two starting at four bytes. The size
76 of an allocation request is rounded up to the next power of two
77 (`order'), and satisfied from the appropriate page.
79 Each page is recorded in a page-entry, which also maintains an
80 in-use bitmap of object positions on the page. This allows the
81 allocation state of a particular object to be flipped without
82 touching the page itself.
84 Each page-entry also has a context depth, which is used to track
85 pushing and popping of allocation contexts. Only objects allocated
86 in the current (highest-numbered) context may be collected.
88 Page entries are arranged in an array of singly-linked lists. The
89 array is indexed by the allocation size, in bits, of the pages on
90 it; i.e. all pages on a list allocate objects of the same size.
91 Pages are ordered on the list such that all non-full pages precede
92 all full pages, with non-full pages arranged in order of decreasing
93 context depth.
95 Empty pages (of all orders) are kept on a single page cache list,
96 and are considered first when new pages are required; they are
97 deallocated at the start of the next collection if they haven't
98 been recycled by then. */
100 /* Define GGC_DEBUG_LEVEL to print debugging information.
101 0: No debugging output.
102 1: GC statistics only.
103 2: Page-entry allocations/deallocations as well.
104 3: Object allocations as well.
105 4: Object marks as well. */
106 #define GGC_DEBUG_LEVEL (0)
108 #ifndef HOST_BITS_PER_PTR
109 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
110 #endif
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:
117 HOST_PAGE_SIZE_BITS
118 32 | |
119 msb +----------------+----+------+------+ lsb
120 | | |
121 PAGE_L1_BITS |
123 PAGE_L2_BITS
125 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
126 pages are aligned on system page boundaries. The next most
127 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
128 index values in the lookup table, respectively.
130 For 32-bit architectures and the settings below, there are no
131 leftover bits. For architectures with wider pointers, the lookup
132 tree points to a list of pages, which must be scanned to find the
133 correct one. */
135 #define PAGE_L1_BITS (8)
136 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
137 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
138 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
140 #define LOOKUP_L1(p) \
141 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
143 #define LOOKUP_L2(p) \
144 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
146 /* The number of objects per allocation page, for objects on a page of
147 the indicated ORDER. */
148 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
150 /* The size of an object on a page of the indicated ORDER. */
151 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
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))
162 /* The number of extra orders, not corresponding to power-of-two sized
163 objects. */
165 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
167 #define RTL_SIZE(NSLOTS) \
168 (sizeof (struct rtx_def) + ((NSLOTS) - 1) * sizeof (rtunion))
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. */
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 */
181 /* The total number of orders. */
183 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
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. */
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;
201 /* The biggest alignment required. */
203 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
205 /* The Ith entry is the number of objects on a page or order I. */
207 static unsigned objects_per_page_table[NUM_ORDERS];
209 /* The Ith entry is the size of an object on a page of order I. */
211 static size_t object_size_table[NUM_ORDERS];
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). */
217 static struct
219 unsigned int mult;
220 unsigned int shift;
222 inverse_table[NUM_ORDERS];
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
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;
232 /* The number of bytes allocated. (This will always be a multiple
233 of the host system page size.) */
234 size_t bytes;
236 /* The address at which the memory is allocated. */
237 char *page;
239 #ifdef USING_MALLOC_PAGE_GROUPS
240 /* Back pointer to the page group this page came from. */
241 struct page_group *group;
242 #endif
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;
248 /* Context depth of this page. */
249 unsigned short context_depth;
251 /* The number of free objects remaining on this page. */
252 unsigned short num_free_objects;
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;
258 /* The lg of size of objects allocated from this page. */
259 unsigned char order;
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;
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
272 /* A linked list of all extant page groups. */
273 struct page_group *next;
275 /* The address we received from malloc. */
276 char *allocation;
278 /* The size of the block. */
279 size_t alloc_size;
281 /* A bitmask of pages in use. */
282 unsigned int in_use;
283 } page_group;
284 #endif
286 #if HOST_BITS_PER_PTR <= 32
288 /* On 32-bit hosts, we use a two level page table, as pictured above. */
289 typedef page_entry **page_table[PAGE_L1_SIZE];
291 #else
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
298 struct page_table_chain *next;
299 size_t high_bits;
300 page_entry **table[PAGE_L1_SIZE];
301 } *page_table;
303 #endif
305 /* The rest of the global variables. */
306 static struct globals
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];
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];
319 /* Lookup table for associating allocation pages with object addresses. */
320 page_table lookup;
322 /* The system's page size. */
323 size_t pagesize;
324 size_t lg_pagesize;
326 /* Bytes currently allocated. */
327 size_t allocated;
329 /* Bytes currently allocated at the end of the last collection. */
330 size_t allocated_last_gc;
332 /* Total amount of memory mapped. */
333 size_t bytes_mapped;
335 /* The current depth in the context stack. */
336 unsigned short context_depth;
338 /* A file descriptor open to /dev/zero for reading. */
339 #if defined (HAVE_MMAP_DEV_ZERO)
340 int dev_zero_fd;
341 #endif
343 /* A cache of free system pages. */
344 page_entry *free_pages;
346 #ifdef USING_MALLOC_PAGE_GROUPS
347 page_group *page_groups;
348 #endif
350 /* The file descriptor for debugging output. */
351 FILE *debug_file;
352 } G;
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))
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
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));
384 #ifdef ENABLE_GC_CHECKING
385 static void poison_pages PARAMS ((void));
386 #endif
388 void debug_print_page_list PARAMS ((int));
390 /* Returns nonzero if P was allocated in GC'able memory. */
392 static inline int
393 ggc_allocated_p (p)
394 const void *p;
396 page_entry ***base;
397 size_t L1, L2;
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)
406 if (table == NULL)
407 return 0;
408 if (table->high_bits == high_bits)
409 break;
410 table = table->next;
412 base = &table->table[0];
413 #endif
415 /* Extract the level 1 and 2 indices. */
416 L1 = LOOKUP_L1 (p);
417 L2 = LOOKUP_L2 (p);
419 return base[L1] && base[L1][L2];
422 /* Traverse the page table and find the entry for a page.
423 Die (probably) if the object wasn't allocated via GC. */
425 static inline page_entry *
426 lookup_page_table_entry(p)
427 const void *p;
429 page_entry ***base;
430 size_t L1, L2;
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
442 /* Extract the level 1 and 2 indices. */
443 L1 = LOOKUP_L1 (p);
444 L2 = LOOKUP_L2 (p);
446 return base[L1][L2];
449 /* Set the page table entry for a page. */
451 static void
452 set_page_table_entry(p, entry)
453 void *p;
454 page_entry *entry;
456 page_entry ***base;
457 size_t L1, L2;
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;
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
477 /* Extract the level 1 and 2 indices. */
478 L1 = LOOKUP_L1 (p);
479 L2 = LOOKUP_L2 (p);
481 if (base[L1] == NULL)
482 base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
484 base[L1][L2] = entry;
487 /* Prints the page-entry for object size ORDER, for debugging. */
489 void
490 debug_print_page_list (order)
491 int order;
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)
499 printf ("%p(%1d|%3d) -> ", (PTR) p, p->context_depth,
500 p->num_free_objects);
501 p = p->next;
503 printf ("NULL\n");
504 fflush (stdout);
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. */
512 static inline char *
513 alloc_anon (pref, size)
514 char *pref ATTRIBUTE_UNUSED;
515 size_t size;
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
526 if (page == (char *) MAP_FAILED)
528 perror ("virtual memory exhausted");
529 exit (FATAL_EXIT_CODE);
532 /* Remember that we allocated this memory. */
533 G.bytes_mapped += size;
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));
540 return page;
542 #endif
543 #ifdef USING_MALLOC_PAGE_GROUPS
544 /* Compute the index for this page into the page group. */
546 static inline size_t
547 page_group_index (allocation, page)
548 char *allocation, *page;
550 return (size_t) (page - allocation) >> G.lg_pagesize;
553 /* Set and clear the in_use bit for this page in the page group. */
555 static inline void
556 set_page_group_in_use (group, page)
557 page_group *group;
558 char *page;
560 group->in_use |= 1 << page_group_index (group->allocation, page);
563 static inline void
564 clear_page_group_in_use (group, page)
565 page_group *group;
566 char *page;
568 group->in_use &= ~(1 << page_group_index (group->allocation, page));
570 #endif
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. */
576 static inline struct page_entry *
577 alloc_page (order)
578 unsigned order;
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
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;
597 entry = NULL;
598 page = NULL;
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;
605 if (p != NULL)
607 /* Recycle the allocated memory from this page ... */
608 *pp = p->next;
609 page = p->page;
611 #ifdef USING_MALLOC_PAGE_GROUPS
612 group = p->group;
613 #endif
615 /* ... and, if possible, the page entry itself. */
616 if (p->order == order)
618 entry = p;
619 memset (entry, 0, page_entry_size);
621 else
622 free (p);
624 #ifdef USING_MMAP
625 else if (entry_size == G.pagesize)
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;
633 page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE);
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--)
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;
647 G.free_pages = f;
649 else
650 page = alloc_anon (NULL, entry_size);
651 #endif
652 #ifdef USING_MALLOC_PAGE_GROUPS
653 else
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. */
659 char *allocation, *a, *enda;
660 size_t alloc_size, head_slop, tail_slop;
661 int multiple_pages = (entry_size == G.pagesize);
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);
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;
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
684 /* We magically got an aligned allocation. Too bad, we have
685 to waste a page anyway. */
686 if (tail_slop == 0)
688 enda -= G.pagesize;
689 tail_slop += G.pagesize;
691 if (tail_slop < sizeof (page_group))
692 abort ();
693 group = (page_group *)enda;
694 tail_slop -= sizeof (page_group);
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;
705 /* If we allocated multiple pages, put the rest on the free list. */
706 if (multiple_pages)
708 struct page_entry *e, *f = G.free_pages;
709 for (a = enda - G.pagesize; a != page; a -= G.pagesize)
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;
719 G.free_pages = f;
722 #endif
724 if (entry == NULL)
725 entry = (struct page_entry *) xcalloc (1, page_entry_size);
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;
734 #ifdef USING_MALLOC_PAGE_GROUPS
735 entry->group = group;
736 set_page_group_in_use (group, page);
737 #endif
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);
744 set_page_table_entry (page, entry);
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);
752 return entry;
755 /* For a page that is no longer needed, put it on the free page list. */
757 static inline void
758 free_page (entry)
759 page_entry *entry;
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);
766 /* Mark the page as inaccessible. Discard the handle to avoid handle
767 leak. */
768 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (entry->page, entry->bytes));
770 set_page_table_entry (entry->page, NULL);
772 #ifdef USING_MALLOC_PAGE_GROUPS
773 clear_page_group_in_use (entry->group, entry->page);
774 #endif
776 entry->next = G.free_pages;
777 G.free_pages = entry;
780 /* Release the free page cache to the system. */
782 static void
783 release_pages ()
785 #ifdef USING_MMAP
786 page_entry *p, *next;
787 char *start;
788 size_t len;
790 /* Gather up adjacent pages so they are unmapped together. */
791 p = G.free_pages;
793 while (p)
795 start = p->page;
796 next = p->next;
797 len = p->bytes;
798 free (p);
799 p = next;
801 while (p && p->page == start + len)
803 next = p->next;
804 len += p->bytes;
805 free (p);
806 p = next;
809 munmap (start, len);
810 G.bytes_mapped -= len;
813 G.free_pages = NULL;
814 #endif
815 #ifdef USING_MALLOC_PAGE_GROUPS
816 page_entry **pp, *p;
817 page_group **gp, *g;
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)
824 *pp = p->next;
825 free (p);
827 else
828 pp = &p->next;
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)
835 *gp = g->next;
836 G.bytes_mapped -= g->alloc_size;
837 free (g->allocation);
839 else
840 gp = &g->next;
841 #endif
844 /* This table provides a fast way to determine ceil(log_2(size)) for
845 allocation requests. The minimum allocation size is eight bytes. */
847 static unsigned char size_lookup[257] =
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,
868 /* Allocate a chunk of memory of SIZE bytes. If ZERO is nonzero, the
869 memory is zeroed; otherwise, its contents are undefined. */
871 void *
872 ggc_alloc (size)
873 size_t size;
875 unsigned order, word, bit, object_offset;
876 struct page_entry *entry;
877 void *result;
879 if (size <= 256)
880 order = size_lookup[size];
881 else
883 order = 9;
884 while (size > OBJECT_SIZE (order))
885 order++;
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];
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)
896 struct page_entry *new_entry;
897 new_entry = alloc_page (order);
899 /* If this is the only entry, it's also the tail. */
900 if (entry == NULL)
901 G.page_tails[order] = new_entry;
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;
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;
915 else
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;
925 /* If the hint didn't work, scan the bitmap from the beginning. */
926 if ((entry->in_use_p[word] >> bit) & 1)
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;
936 /* Next time, try the next bit. */
937 entry->next_bit_hint = hint + 1;
939 object_offset = hint * OBJECT_SIZE (order);
942 /* Set the in-use bit. */
943 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
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)
953 G.pages[order] = entry->next;
954 entry->next = NULL;
955 G.page_tails[order]->next = entry;
956 G.page_tails[order] = entry;
959 /* Calculate the object's address. */
960 result = entry->page + object_offset;
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)));
969 /* `Poison' the entire allocated object, including any padding at
970 the end. */
971 memset (result, 0xaf, OBJECT_SIZE (order));
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
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));
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);
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);
994 return result;
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. */
1002 ggc_set_mark (p)
1003 const void *p;
1005 page_entry *entry;
1006 unsigned bit, word;
1007 unsigned long mask;
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
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);
1023 /* If the bit was previously set, skip it. */
1024 if (entry->in_use_p[word] & mask)
1025 return 1;
1027 /* Otherwise set it, and decrement the free object count. */
1028 entry->in_use_p[word] |= mask;
1029 entry->num_free_objects -= 1;
1031 if (GGC_DEBUG_LEVEL >= 4)
1032 fprintf (G.debug_file, "Marking %p\n", p);
1034 return 0;
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. */
1042 ggc_marked_p (p)
1043 const void *p;
1045 page_entry *entry;
1046 unsigned bit, word;
1047 unsigned long mask;
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
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);
1063 return (entry->in_use_p[word] & mask) != 0;
1066 /* Return the size of the gc-able object P. */
1068 size_t
1069 ggc_get_size (p)
1070 const void *p;
1072 page_entry *pe = lookup_page_table_entry (p);
1073 return OBJECT_SIZE (pe->order);
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[].
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). */
1084 static void
1085 compute_inverse (order)
1086 unsigned order;
1088 unsigned size, inv, e;
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)
1094 if (OBJECTS_PER_PAGE (order) != 1)
1095 abort ();
1097 DIV_MULT (order) = 1;
1098 DIV_SHIFT (order) = 0;
1099 return;
1102 size = OBJECT_SIZE (order);
1103 e = 0;
1104 while (size % 2 == 0)
1106 e++;
1107 size >>= 1;
1110 inv = size;
1111 while (inv * size != 1)
1112 inv = inv * (2 - inv*size);
1114 DIV_MULT (order) = inv;
1115 DIV_SHIFT (order) = e;
1118 /* Initialize the ggc-mmap allocator. */
1119 void
1120 init_ggc ()
1122 unsigned order;
1124 G.pagesize = getpagesize();
1125 G.lg_pagesize = exact_log2 (G.pagesize);
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
1133 #if 0
1134 G.debug_file = fopen ("ggc-mmap.debug", "w");
1135 #else
1136 G.debug_file = stdout;
1137 #endif
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. */
1145 char *p = alloc_anon (NULL, G.pagesize);
1146 struct page_entry *e;
1147 if ((size_t)p & (G.pagesize - 1))
1149 /* How losing. Discard this one and try another. If we still
1150 can't get something useful, give up. */
1152 p = alloc_anon (NULL, G.pagesize);
1153 if ((size_t)p & (G.pagesize - 1))
1154 abort ();
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;
1164 #endif
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)
1171 size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
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;
1179 /* Initialize the objects-per-page and inverse tables. */
1180 for (order = 0; order < NUM_ORDERS; ++order)
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);
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)
1194 int o;
1195 int i;
1197 o = size_lookup[OBJECT_SIZE (order)];
1198 for (i = OBJECT_SIZE (order); size_lookup [i] == o; --i)
1199 size_lookup[i] = order;
1203 /* Increment the `GC context'. Objects allocated in an outer context
1204 are never freed, eliminating the need to register their roots. */
1206 void
1207 ggc_push_context ()
1209 ++G.context_depth;
1211 /* Die on wrap. */
1212 if (G.context_depth == 0)
1213 abort ();
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. */
1219 static void
1220 ggc_recalculate_in_use_p (p)
1221 page_entry *p;
1223 unsigned int i;
1224 size_t num_objects;
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;
1230 /* Reset the free object count. */
1231 p->num_free_objects = num_objects;
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)
1239 unsigned long j;
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];
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);
1250 if (p->num_free_objects >= num_objects)
1251 abort ();
1254 /* Decrement the `GC context'. All objects allocated since the
1255 previous ggc_push_context are migrated to the outer context. */
1257 void
1258 ggc_pop_context ()
1260 unsigned order, depth;
1262 depth = --G.context_depth;
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++)
1269 page_entry *p;
1271 for (p = G.pages[order]; p != NULL; p = p->next)
1273 if (p->context_depth > depth)
1274 p->context_depth = depth;
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)
1280 ggc_recalculate_in_use_p (p);
1281 free (p->save_in_use_p);
1282 p->save_in_use_p = 0;
1288 /* Unmark all objects. */
1290 static inline void
1291 clear_marks ()
1293 unsigned order;
1295 for (order = 2; order < NUM_ORDERS; order++)
1297 size_t num_objects = OBJECTS_PER_PAGE (order);
1298 size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
1299 page_entry *p;
1301 for (p = G.pages[order]; p != NULL; p = p->next)
1303 #ifdef ENABLE_CHECKING
1304 /* The data should be page-aligned. */
1305 if ((size_t) p->page & (G.pagesize - 1))
1306 abort ();
1307 #endif
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)
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);
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);
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));
1331 /* Free all empty pages. Partially empty pages need no attention
1332 because the `mark' bit doubles as an `unused' bit. */
1334 static inline void
1335 sweep_pages ()
1337 unsigned order;
1339 for (order = 2; order < NUM_ORDERS; order++)
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];
1345 size_t num_objects = OBJECTS_PER_PAGE (order);
1346 size_t live_objects;
1347 page_entry *p, *previous;
1348 int done;
1350 p = G.pages[order];
1351 if (p == NULL)
1352 continue;
1354 previous = NULL;
1357 page_entry *next = p->next;
1359 /* Loop until all entries have been examined. */
1360 done = (p == last);
1362 /* Add all live objects on this page to the count of
1363 allocated memory. */
1364 live_objects = num_objects - p->num_free_objects;
1366 G.allocated += OBJECT_SIZE (order) * live_objects;
1368 /* Only objects on pages in the topmost context should get
1369 collected. */
1370 if (p->context_depth < G.context_depth)
1373 /* Remove the page if it's empty. */
1374 else if (live_objects == 0)
1376 if (! previous)
1377 G.pages[order] = next;
1378 else
1379 previous->next = next;
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;
1388 /* If the page is full, move it to the end. */
1389 else if (p->num_free_objects == 0)
1391 /* Don't move it if it's already at the end. */
1392 if (p != G.page_tails[order])
1394 /* Move p to the end of the list. */
1395 p->next = NULL;
1396 G.page_tails[order]->next = p;
1398 /* Update the tail pointer... */
1399 G.page_tails[order] = p;
1401 /* ... and the head pointer, if necessary. */
1402 if (! previous)
1403 G.pages[order] = next;
1404 else
1405 previous->next = next;
1406 p = previous;
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])
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;
1425 previous = p;
1426 p = next;
1428 while (! done);
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);
1438 #ifdef ENABLE_GC_CHECKING
1439 /* Clobber all free objects. */
1441 static inline void
1442 poison_pages ()
1444 unsigned order;
1446 for (order = 2; order < NUM_ORDERS; order++)
1448 size_t num_objects = OBJECTS_PER_PAGE (order);
1449 size_t size = OBJECT_SIZE (order);
1450 page_entry *p;
1452 for (p = G.pages[order]; p != NULL; p = p->next)
1454 size_t i;
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;
1463 for (i = 0; i < num_objects; i++)
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)
1470 char *object = p->page + i * size;
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);
1479 /* Drop the handle to avoid handle leak. */
1480 VALGRIND_DISCARD (VALGRIND_MAKE_NOACCESS (object, size));
1486 #endif
1488 /* Top level mark-and-sweep routine. */
1490 void
1491 ggc_collect ()
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);
1499 size_t min_expand = allocated_last_gc * PARAM_VALUE (GGC_MIN_EXPAND) / 100;
1501 if (G.allocated < allocated_last_gc + min_expand)
1502 return;
1504 timevar_push (TV_GC);
1505 if (!quiet_flag)
1506 fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
1508 /* Zero the total allocated bytes. This will be recalculated in the
1509 sweep phase. */
1510 G.allocated = 0;
1512 /* Release the pages we freed the last time we collected, but didn't
1513 reuse in the interim. */
1514 release_pages ();
1516 clear_marks ();
1517 ggc_mark_roots ();
1519 #ifdef ENABLE_GC_CHECKING
1520 poison_pages ();
1521 #endif
1523 sweep_pages ();
1525 G.allocated_last_gc = G.allocated;
1527 timevar_pop (TV_GC);
1529 if (!quiet_flag)
1530 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
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'))
1541 void
1542 ggc_print_statistics ()
1544 struct ggc_statistics stats;
1545 unsigned int i;
1546 size_t total_overhead = 0;
1548 /* Clear the statistics. */
1549 memset (&stats, 0, sizeof (stats));
1551 /* Make sure collection will really occur. */
1552 G.allocated_last_gc = 0;
1554 /* Collect and print the statistics common across collectors. */
1555 ggc_print_common_statistics (stderr, &stats);
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 ();
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)
1567 page_entry *p;
1568 size_t allocated;
1569 size_t in_use;
1570 size_t overhead;
1572 /* Skip empty entries. */
1573 if (!G.pages[i])
1574 continue;
1576 overhead = allocated = in_use = 0;
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)
1583 allocated += p->bytes;
1584 in_use +=
1585 (OBJECTS_PER_PAGE (i) - p->num_free_objects) * OBJECT_SIZE (i);
1587 overhead += (sizeof (page_entry) - sizeof (long)
1588 + BITMAP_SIZE (OBJECTS_PER_PAGE (i) + 1));
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;
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));