1 /* "Bag-of-pages" garbage collector for the GNU compiler.
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
32 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
33 file open. Prefer either to valloc. */
35 # undef HAVE_MMAP_DEV_ZERO
38 # include <sys/mman.h>
40 # define MAP_FAILED -1
42 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
43 # define MAP_ANONYMOUS MAP_ANON
49 #ifdef HAVE_MMAP_DEV_ZERO
52 # include <sys/mman.h>
54 # define MAP_FAILED -1
67 This garbage-collecting allocator allocates objects on one of a set
68 of pages. Each page can allocate objects of a single size only;
69 available sizes are powers of two starting at four bytes. The size
70 of an allocation request is rounded up to the next power of two
71 (`order'), and satisfied from the appropriate page.
73 Each page is recorded in a page-entry, which also maintains an
74 in-use bitmap of object positions on the page. This allows the
75 allocation state of a particular object to be flipped without
76 touching the page itself.
78 Each page-entry also has a context depth, which is used to track
79 pushing and popping of allocation contexts. Only objects allocated
80 in the current (highest-numbered) context may be collected.
82 Page entries are arranged in an array of singly-linked lists. The
83 array is indexed by the allocation size, in bits, of the pages on
84 it; i.e. all pages on a list allocate objects of the same size.
85 Pages are ordered on the list such that all non-full pages precede
86 all full pages, with non-full pages arranged in order of decreasing
89 Empty pages (of all orders) are kept on a single page cache list,
90 and are considered first when new pages are required; they are
91 deallocated at the start of the next collection if they haven't
92 been recycled by then. */
95 /* Define GGC_POISON to poison memory marked unused by the collector. */
98 /* Define GGC_ALWAYS_COLLECT to perform collection every time
99 ggc_collect is invoked. Otherwise, collection is performed only
100 when a significant amount of memory has been allocated since the
102 #undef GGC_ALWAYS_COLLECT
104 #ifdef ENABLE_GC_CHECKING
107 #ifdef ENABLE_GC_ALWAYS_COLLECT
108 #define GGC_ALWAYS_COLLECT
111 /* Define GGC_DEBUG_LEVEL to print debugging information.
112 0: No debugging output.
113 1: GC statistics only.
114 2: Page-entry allocations/deallocations as well.
115 3: Object allocations as well.
116 4: Object marks as well. */
117 #define GGC_DEBUG_LEVEL (0)
119 #ifndef HOST_BITS_PER_PTR
120 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
124 /* A two-level tree is used to look up the page-entry for a given
125 pointer. Two chunks of the pointer's bits are extracted to index
126 the first and second levels of the tree, as follows:
130 msb +----------------+----+------+------+ lsb
136 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
137 pages are aligned on system page boundaries. The next most
138 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
139 index values in the lookup table, respectively.
141 For 32-bit architectures and the settings below, there are no
142 leftover bits. For architectures with wider pointers, the lookup
143 tree points to a list of pages, which must be scanned to find the
146 #define PAGE_L1_BITS (8)
147 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
148 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
149 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
151 #define LOOKUP_L1(p) \
152 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
154 #define LOOKUP_L2(p) \
155 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
157 /* The number of objects per allocation page, for objects on a page of
158 the indicated ORDER. */
159 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
161 /* The size of an object on a page of the indicated ORDER. */
162 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
164 /* The number of extra orders, not corresponding to power-of-two sized
167 #define NUM_EXTRA_ORDERS \
168 (sizeof (extra_order_size_table) / sizeof (extra_order_size_table[0]))
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
)
179 /* The total number of orders. */
181 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
183 /* We use this structure to determine the alignment required for
184 allocations. For power-of-two sized allocations, that's not a
185 problem, but it does matter for odd-sized allocations. */
187 struct max_alignment
{
191 #ifdef HAVE_LONG_DOUBLE
199 /* The biggest alignment required. */
201 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
203 /* The Ith entry is the number of objects on a page or order I. */
205 static unsigned objects_per_page_table
[NUM_ORDERS
];
207 /* The Ith entry is the size of an object on a page of order I. */
209 static size_t object_size_table
[NUM_ORDERS
];
211 /* A page_entry records the status of an allocation page. This
212 structure is dynamically sized to fit the bitmap in_use_p. */
213 typedef struct page_entry
215 /* The next page-entry with objects of the same size, or NULL if
216 this is the last page-entry. */
217 struct page_entry
*next
;
219 /* The number of bytes allocated. (This will always be a multiple
220 of the host system page size.) */
223 /* The address at which the memory is allocated. */
226 /* Saved in-use bit vector for pages that aren't in the topmost
227 context during collection. */
228 unsigned long *save_in_use_p
;
230 /* Context depth of this page. */
231 unsigned short context_depth
;
233 /* The number of free objects remaining on this page. */
234 unsigned short num_free_objects
;
236 /* A likely candidate for the bit position of a free object for the
237 next allocation from this page. */
238 unsigned short next_bit_hint
;
240 /* The lg of size of objects allocated from this page. */
243 /* A bit vector indicating whether or not objects are in use. The
244 Nth bit is one if the Nth object on this page is allocated. This
245 array is dynamically sized. */
246 unsigned long in_use_p
[1];
250 #if HOST_BITS_PER_PTR <= 32
252 /* On 32-bit hosts, we use a two level page table, as pictured above. */
253 typedef page_entry
**page_table
[PAGE_L1_SIZE
];
257 /* On 64-bit hosts, we use the same two level page tables plus a linked
258 list that disambiguates the top 32-bits. There will almost always be
259 exactly one entry in the list. */
260 typedef struct page_table_chain
262 struct page_table_chain
*next
;
264 page_entry
**table
[PAGE_L1_SIZE
];
269 /* The rest of the global variables. */
270 static struct globals
272 /* The Nth element in this array is a page with objects of size 2^N.
273 If there are any pages with free objects, they will be at the
274 head of the list. NULL if there are no page-entries for this
276 page_entry
*pages
[NUM_ORDERS
];
278 /* The Nth element in this array is the last page with objects of
279 size 2^N. NULL if there are no page-entries for this object
281 page_entry
*page_tails
[NUM_ORDERS
];
283 /* Lookup table for associating allocation pages with object addresses. */
286 /* The system's page size. */
290 /* Bytes currently allocated. */
293 /* Bytes currently allocated at the end of the last collection. */
294 size_t allocated_last_gc
;
296 /* Total amount of memory mapped. */
299 /* The current depth in the context stack. */
300 unsigned short context_depth
;
302 /* A file descriptor open to /dev/zero for reading. */
303 #if defined (HAVE_MMAP_DEV_ZERO)
307 /* A cache of free system pages. */
308 page_entry
*free_pages
;
310 /* The file descriptor for debugging output. */
314 /* The size in bytes required to maintain a bitmap for the objects
316 #define BITMAP_SIZE(Num_objects) \
317 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
319 /* Skip garbage collection if the current allocation is not at least
320 this factor times the allocation at the end of the last collection.
321 In other words, total allocation must expand by (this factor minus
322 one) before collection is performed. */
323 #define GGC_MIN_EXPAND_FOR_GC (1.3)
325 /* Bound `allocated_last_gc' to 4MB, to prevent the memory expansion
326 test from triggering too often when the heap is small. */
327 #define GGC_MIN_LAST_ALLOCATED (4 * 1024 * 1024)
329 /* Allocate pages in chunks of this size, to throttle calls to mmap.
330 The first page is used, the rest go onto the free list. */
331 #define GGC_QUIRE_SIZE 16
334 static int ggc_allocated_p
PARAMS ((const void *));
335 static page_entry
*lookup_page_table_entry
PARAMS ((const void *));
336 static void set_page_table_entry
PARAMS ((void *, page_entry
*));
337 static char *alloc_anon
PARAMS ((char *, size_t));
338 static struct page_entry
* alloc_page
PARAMS ((unsigned));
339 static void free_page
PARAMS ((struct page_entry
*));
340 static void release_pages
PARAMS ((void));
341 static void clear_marks
PARAMS ((void));
342 static void sweep_pages
PARAMS ((void));
343 static void ggc_recalculate_in_use_p
PARAMS ((page_entry
*));
346 static void poison_pages
PARAMS ((void));
349 void debug_print_page_list
PARAMS ((int));
351 /* Returns non-zero if P was allocated in GC'able memory. */
360 #if HOST_BITS_PER_PTR <= 32
363 page_table table
= G
.lookup
;
364 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
369 if (table
->high_bits
== high_bits
)
373 base
= &table
->table
[0];
376 /* Extract the level 1 and 2 indicies. */
380 return base
[L1
] && base
[L1
][L2
];
383 /* Traverse the page table and find the entry for a page.
384 Die (probably) if the object wasn't allocated via GC. */
386 static inline page_entry
*
387 lookup_page_table_entry(p
)
393 #if HOST_BITS_PER_PTR <= 32
396 page_table table
= G
.lookup
;
397 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
398 while (table
->high_bits
!= high_bits
)
400 base
= &table
->table
[0];
403 /* Extract the level 1 and 2 indicies. */
410 /* Set the page table entry for a page. */
413 set_page_table_entry(p
, entry
)
420 #if HOST_BITS_PER_PTR <= 32
424 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
425 for (table
= G
.lookup
; table
; table
= table
->next
)
426 if (table
->high_bits
== high_bits
)
429 /* Not found -- allocate a new table. */
430 table
= (page_table
) xcalloc (1, sizeof(*table
));
431 table
->next
= G
.lookup
;
432 table
->high_bits
= high_bits
;
435 base
= &table
->table
[0];
438 /* Extract the level 1 and 2 indicies. */
442 if (base
[L1
] == NULL
)
443 base
[L1
] = (page_entry
**) xcalloc (PAGE_L2_SIZE
, sizeof (page_entry
*));
445 base
[L1
][L2
] = entry
;
448 /* Prints the page-entry for object size ORDER, for debugging. */
451 debug_print_page_list (order
)
455 printf ("Head=%p, Tail=%p:\n", (PTR
) G
.pages
[order
],
456 (PTR
) G
.page_tails
[order
]);
460 printf ("%p(%1d|%3d) -> ", (PTR
) p
, p
->context_depth
,
461 p
->num_free_objects
);
468 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
469 (if non-null). The ifdef structure here is intended to cause a
470 compile error unless exactly one of the HAVE_* is defined. */
473 alloc_anon (pref
, size
)
474 char *pref ATTRIBUTE_UNUSED
;
477 #ifdef HAVE_MMAP_ANON
478 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
479 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
481 #ifdef HAVE_MMAP_DEV_ZERO
482 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
483 MAP_PRIVATE
, G
.dev_zero_fd
, 0);
486 char *page
= (char *) valloc (size
);
489 if (page
== (char *) MAP_FAILED
)
491 fputs ("Virtual memory exhausted!\n", stderr
);
495 /* Remember that we allocated this memory. */
496 G
.bytes_mapped
+= size
;
501 /* Allocate a new page for allocating objects of size 2^ORDER,
502 and return an entry for it. The entry is not added to the
503 appropriate page_table list. */
505 static inline struct page_entry
*
509 struct page_entry
*entry
, *p
, **pp
;
513 size_t page_entry_size
;
516 num_objects
= OBJECTS_PER_PAGE (order
);
517 bitmap_size
= BITMAP_SIZE (num_objects
+ 1);
518 page_entry_size
= sizeof (page_entry
) - sizeof (long) + bitmap_size
;
519 entry_size
= num_objects
* OBJECT_SIZE (order
);
524 /* Check the list of free pages for one we can use. */
525 for (pp
= &G
.free_pages
, p
= *pp
; p
; pp
= &p
->next
, p
= *pp
)
526 if (p
->bytes
== entry_size
)
531 /* Recycle the allocated memory from this page ... */
534 /* ... and, if possible, the page entry itself. */
535 if (p
->order
== order
)
538 memset (entry
, 0, page_entry_size
);
544 else if (entry_size
== G
.pagesize
)
546 /* We want just one page. Allocate a bunch of them and put the
547 extras on the freelist. (Can only do this optimization with
548 mmap for backing store.) */
549 struct page_entry
*e
, *f
= G
.free_pages
;
552 page
= alloc_anon (NULL
, entry_size
* GGC_QUIRE_SIZE
);
553 /* This loop counts down so that the chain will be in ascending
555 for (i
= GGC_QUIRE_SIZE
- 1; i
>= 1; i
--)
557 e
= (struct page_entry
*) xcalloc (1, sizeof (struct page_entry
));
558 e
->bytes
= entry_size
;
559 e
->page
= page
+ i
*entry_size
;
567 page
= alloc_anon (NULL
, entry_size
);
570 entry
= (struct page_entry
*) xcalloc (1, page_entry_size
);
572 entry
->bytes
= entry_size
;
574 entry
->context_depth
= G
.context_depth
;
575 entry
->order
= order
;
576 entry
->num_free_objects
= num_objects
;
577 entry
->next_bit_hint
= 1;
579 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
580 increment the hint. */
581 entry
->in_use_p
[num_objects
/ HOST_BITS_PER_LONG
]
582 = (unsigned long) 1 << (num_objects
% HOST_BITS_PER_LONG
);
584 set_page_table_entry (page
, entry
);
586 if (GGC_DEBUG_LEVEL
>= 2)
587 fprintf (G
.debug_file
,
588 "Allocating page at %p, object size=%d, data %p-%p\n",
589 (PTR
) entry
, OBJECT_SIZE (order
), page
, page
+ entry_size
- 1);
594 /* For a page that is no longer needed, put it on the free page list. */
600 if (GGC_DEBUG_LEVEL
>= 2)
601 fprintf (G
.debug_file
,
602 "Deallocating page at %p, data %p-%p\n", (PTR
) entry
,
603 entry
->page
, entry
->page
+ entry
->bytes
- 1);
605 set_page_table_entry (entry
->page
, NULL
);
607 entry
->next
= G
.free_pages
;
608 G
.free_pages
= entry
;
611 /* Release the free page cache to the system. */
616 page_entry
*p
, *next
;
622 /* Gather up adjacent pages so they are unmapped together. */
633 while (p
&& p
->page
== start
+ len
)
642 G
.bytes_mapped
-= len
;
645 for (p
= G
.free_pages
; p
; p
= next
)
649 G
.bytes_mapped
-= p
->bytes
;
652 #endif /* USING_MMAP */
657 /* This table provides a fast way to determine ceil(log_2(size)) for
658 allocation requests. The minimum allocation size is four bytes. */
660 static unsigned char size_lookup
[257] =
662 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
663 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
664 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
665 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
666 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
667 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
668 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
669 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
670 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
671 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
672 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
673 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
674 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
675 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
676 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
677 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
681 /* Allocate a chunk of memory of SIZE bytes. If ZERO is non-zero, the
682 memory is zeroed; otherwise, its contents are undefined. */
688 unsigned order
, word
, bit
, object_offset
;
689 struct page_entry
*entry
;
693 order
= size_lookup
[size
];
697 while (size
> OBJECT_SIZE (order
))
701 /* If there are non-full pages for this size allocation, they are at
702 the head of the list. */
703 entry
= G
.pages
[order
];
705 /* If there is no page for this object size, or all pages in this
706 context are full, allocate a new page. */
707 if (entry
== NULL
|| entry
->num_free_objects
== 0)
709 struct page_entry
*new_entry
;
710 new_entry
= alloc_page (order
);
712 /* If this is the only entry, it's also the tail. */
714 G
.page_tails
[order
] = new_entry
;
716 /* Put new pages at the head of the page list. */
717 new_entry
->next
= entry
;
719 G
.pages
[order
] = new_entry
;
721 /* For a new page, we know the word and bit positions (in the
722 in_use bitmap) of the first available object -- they're zero. */
723 new_entry
->next_bit_hint
= 1;
730 /* First try to use the hint left from the previous allocation
731 to locate a clear bit in the in-use bitmap. We've made sure
732 that the one-past-the-end bit is always set, so if the hint
733 has run over, this test will fail. */
734 unsigned hint
= entry
->next_bit_hint
;
735 word
= hint
/ HOST_BITS_PER_LONG
;
736 bit
= hint
% HOST_BITS_PER_LONG
;
738 /* If the hint didn't work, scan the bitmap from the beginning. */
739 if ((entry
->in_use_p
[word
] >> bit
) & 1)
742 while (~entry
->in_use_p
[word
] == 0)
744 while ((entry
->in_use_p
[word
] >> bit
) & 1)
746 hint
= word
* HOST_BITS_PER_LONG
+ bit
;
749 /* Next time, try the next bit. */
750 entry
->next_bit_hint
= hint
+ 1;
752 object_offset
= hint
* OBJECT_SIZE (order
);
755 /* Set the in-use bit. */
756 entry
->in_use_p
[word
] |= ((unsigned long) 1 << bit
);
758 /* Keep a running total of the number of free objects. If this page
759 fills up, we may have to move it to the end of the list if the
760 next page isn't full. If the next page is full, all subsequent
761 pages are full, so there's no need to move it. */
762 if (--entry
->num_free_objects
== 0
763 && entry
->next
!= NULL
764 && entry
->next
->num_free_objects
> 0)
766 G
.pages
[order
] = entry
->next
;
768 G
.page_tails
[order
]->next
= entry
;
769 G
.page_tails
[order
] = entry
;
772 /* Calculate the object's address. */
773 result
= entry
->page
+ object_offset
;
776 /* `Poison' the entire allocated object, including any padding at
778 memset (result
, 0xaf, OBJECT_SIZE (order
));
781 /* Keep track of how many bytes are being allocated. This
782 information is used in deciding when to collect. */
783 G
.allocated
+= OBJECT_SIZE (order
);
785 if (GGC_DEBUG_LEVEL
>= 3)
786 fprintf (G
.debug_file
,
787 "Allocating object, requested size=%d, actual=%d at %p on %p\n",
788 (int) size
, OBJECT_SIZE (order
), result
, (PTR
) entry
);
793 /* If P is not marked, marks it and return false. Otherwise return true.
794 P must have been allocated by the GC allocator; it mustn't point to
795 static objects, stack variables, or memory allocated with malloc. */
805 /* Look up the page on which the object is alloced. If the object
806 wasn't allocated by the collector, we'll probably die. */
807 entry
= lookup_page_table_entry (p
);
808 #ifdef ENABLE_CHECKING
813 /* Calculate the index of the object on the page; this is its bit
814 position in the in_use_p bitmap. */
815 bit
= (((const char *) p
) - entry
->page
) / OBJECT_SIZE (entry
->order
);
816 word
= bit
/ HOST_BITS_PER_LONG
;
817 mask
= (unsigned long) 1 << (bit
% HOST_BITS_PER_LONG
);
819 /* If the bit was previously set, skip it. */
820 if (entry
->in_use_p
[word
] & mask
)
823 /* Otherwise set it, and decrement the free object count. */
824 entry
->in_use_p
[word
] |= mask
;
825 entry
->num_free_objects
-= 1;
827 if (GGC_DEBUG_LEVEL
>= 4)
828 fprintf (G
.debug_file
, "Marking %p\n", p
);
833 /* Mark P, but check first that it was allocated by the collector. */
836 ggc_mark_if_gcable (p
)
839 if (p
&& ggc_allocated_p (p
))
843 /* Return the size of the gc-able object P. */
849 page_entry
*pe
= lookup_page_table_entry (p
);
850 return OBJECT_SIZE (pe
->order
);
853 /* Initialize the ggc-mmap allocator. */
860 G
.pagesize
= getpagesize();
861 G
.lg_pagesize
= exact_log2 (G
.pagesize
);
863 #ifdef HAVE_MMAP_DEV_ZERO
864 G
.dev_zero_fd
= open ("/dev/zero", O_RDONLY
);
865 if (G
.dev_zero_fd
== -1)
870 G
.debug_file
= fopen ("ggc-mmap.debug", "w");
872 G
.debug_file
= stdout
;
875 G
.allocated_last_gc
= GGC_MIN_LAST_ALLOCATED
;
878 /* StunOS has an amazing off-by-one error for the first mmap allocation
879 after fiddling with RLIMIT_STACK. The result, as hard as it is to
880 believe, is an unaligned page allocation, which would cause us to
881 hork badly if we tried to use it. */
883 char *p
= alloc_anon (NULL
, G
.pagesize
);
884 struct page_entry
*e
;
885 if ((size_t)p
& (G
.pagesize
- 1))
887 /* How losing. Discard this one and try another. If we still
888 can't get something useful, give up. */
890 p
= alloc_anon (NULL
, G
.pagesize
);
891 if ((size_t)p
& (G
.pagesize
- 1))
895 /* We have a good page, might as well hold onto it... */
896 e
= (struct page_entry
*) xcalloc (1, sizeof (struct page_entry
));
897 e
->bytes
= G
.pagesize
;
899 e
->next
= G
.free_pages
;
904 /* Initialize the object size table. */
905 for (order
= 0; order
< HOST_BITS_PER_PTR
; ++order
)
906 object_size_table
[order
] = (size_t) 1 << order
;
907 for (order
= HOST_BITS_PER_PTR
; order
< NUM_ORDERS
; ++order
)
909 size_t s
= extra_order_size_table
[order
- HOST_BITS_PER_PTR
];
911 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
912 so that we're sure of getting aligned memory. */
913 s
= CEIL (s
, MAX_ALIGNMENT
) * MAX_ALIGNMENT
;
914 object_size_table
[order
] = s
;
917 /* Initialize the objects-per-page table. */
918 for (order
= 0; order
< NUM_ORDERS
; ++order
)
920 objects_per_page_table
[order
] = G
.pagesize
/ OBJECT_SIZE (order
);
921 if (objects_per_page_table
[order
] == 0)
922 objects_per_page_table
[order
] = 1;
925 /* Reset the size_lookup array to put appropriately sized objects in
926 the special orders. All objects bigger than the previous power
927 of two, but no greater than the special size, should go in the
929 for (order
= HOST_BITS_PER_PTR
; order
< NUM_ORDERS
; ++order
)
934 o
= size_lookup
[OBJECT_SIZE (order
)];
935 for (i
= OBJECT_SIZE (order
); size_lookup
[i
] == o
; --i
)
936 size_lookup
[i
] = order
;
940 /* Increment the `GC context'. Objects allocated in an outer context
941 are never freed, eliminating the need to register their roots. */
949 if (G
.context_depth
== 0)
953 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
954 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
957 ggc_recalculate_in_use_p (p
)
963 /* Because the past-the-end bit in in_use_p is always set, we
964 pretend there is one additional object. */
965 num_objects
= OBJECTS_PER_PAGE (p
->order
) + 1;
967 /* Reset the free object count. */
968 p
->num_free_objects
= num_objects
;
970 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
972 i
< CEIL (BITMAP_SIZE (num_objects
),
973 sizeof (*p
->in_use_p
));
978 /* Something is in use if it is marked, or if it was in use in a
979 context further down the context stack. */
980 p
->in_use_p
[i
] |= p
->save_in_use_p
[i
];
982 /* Decrement the free object count for every object allocated. */
983 for (j
= p
->in_use_p
[i
]; j
; j
>>= 1)
984 p
->num_free_objects
-= (j
& 1);
987 if (p
->num_free_objects
>= num_objects
)
991 /* Decrement the `GC context'. All objects allocated since the
992 previous ggc_push_context are migrated to the outer context. */
997 unsigned order
, depth
;
999 depth
= --G
.context_depth
;
1001 /* Any remaining pages in the popped context are lowered to the new
1002 current context; i.e. objects allocated in the popped context and
1003 left over are imported into the previous context. */
1004 for (order
= 2; order
< NUM_ORDERS
; order
++)
1008 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
1010 if (p
->context_depth
> depth
)
1011 p
->context_depth
= depth
;
1013 /* If this page is now in the topmost context, and we'd
1014 saved its allocation state, restore it. */
1015 else if (p
->context_depth
== depth
&& p
->save_in_use_p
)
1017 ggc_recalculate_in_use_p (p
);
1018 free (p
->save_in_use_p
);
1019 p
->save_in_use_p
= 0;
1025 /* Unmark all objects. */
1032 for (order
= 2; order
< NUM_ORDERS
; order
++)
1034 size_t num_objects
= OBJECTS_PER_PAGE (order
);
1035 size_t bitmap_size
= BITMAP_SIZE (num_objects
+ 1);
1038 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
1040 #ifdef ENABLE_CHECKING
1041 /* The data should be page-aligned. */
1042 if ((size_t) p
->page
& (G
.pagesize
- 1))
1046 /* Pages that aren't in the topmost context are not collected;
1047 nevertheless, we need their in-use bit vectors to store GC
1048 marks. So, back them up first. */
1049 if (p
->context_depth
< G
.context_depth
)
1051 if (! p
->save_in_use_p
)
1052 p
->save_in_use_p
= xmalloc (bitmap_size
);
1053 memcpy (p
->save_in_use_p
, p
->in_use_p
, bitmap_size
);
1056 /* Reset reset the number of free objects and clear the
1057 in-use bits. These will be adjusted by mark_obj. */
1058 p
->num_free_objects
= num_objects
;
1059 memset (p
->in_use_p
, 0, bitmap_size
);
1061 /* Make sure the one-past-the-end bit is always set. */
1062 p
->in_use_p
[num_objects
/ HOST_BITS_PER_LONG
]
1063 = ((unsigned long) 1 << (num_objects
% HOST_BITS_PER_LONG
));
1068 /* Free all empty pages. Partially empty pages need no attention
1069 because the `mark' bit doubles as an `unused' bit. */
1076 for (order
= 2; order
< NUM_ORDERS
; order
++)
1078 /* The last page-entry to consider, regardless of entries
1079 placed at the end of the list. */
1080 page_entry
* const last
= G
.page_tails
[order
];
1082 size_t num_objects
= OBJECTS_PER_PAGE (order
);
1083 size_t live_objects
;
1084 page_entry
*p
, *previous
;
1094 page_entry
*next
= p
->next
;
1096 /* Loop until all entries have been examined. */
1099 /* Add all live objects on this page to the count of
1100 allocated memory. */
1101 live_objects
= num_objects
- p
->num_free_objects
;
1103 G
.allocated
+= OBJECT_SIZE (order
) * live_objects
;
1105 /* Only objects on pages in the topmost context should get
1107 if (p
->context_depth
< G
.context_depth
)
1110 /* Remove the page if it's empty. */
1111 else if (live_objects
== 0)
1114 G
.pages
[order
] = next
;
1116 previous
->next
= next
;
1118 /* Are we removing the last element? */
1119 if (p
== G
.page_tails
[order
])
1120 G
.page_tails
[order
] = previous
;
1125 /* If the page is full, move it to the end. */
1126 else if (p
->num_free_objects
== 0)
1128 /* Don't move it if it's already at the end. */
1129 if (p
!= G
.page_tails
[order
])
1131 /* Move p to the end of the list. */
1133 G
.page_tails
[order
]->next
= p
;
1135 /* Update the tail pointer... */
1136 G
.page_tails
[order
] = p
;
1138 /* ... and the head pointer, if necessary. */
1140 G
.pages
[order
] = next
;
1142 previous
->next
= next
;
1147 /* If we've fallen through to here, it's a page in the
1148 topmost context that is neither full nor empty. Such a
1149 page must precede pages at lesser context depth in the
1150 list, so move it to the head. */
1151 else if (p
!= G
.pages
[order
])
1153 previous
->next
= p
->next
;
1154 p
->next
= G
.pages
[order
];
1156 /* Are we moving the last element? */
1157 if (G
.page_tails
[order
] == p
)
1158 G
.page_tails
[order
] = previous
;
1167 /* Now, restore the in_use_p vectors for any pages from contexts
1168 other than the current one. */
1169 for (p
= G
.pages
[order
]; p
; p
= p
->next
)
1170 if (p
->context_depth
!= G
.context_depth
)
1171 ggc_recalculate_in_use_p (p
);
1176 /* Clobber all free objects. */
1183 for (order
= 2; order
< NUM_ORDERS
; order
++)
1185 size_t num_objects
= OBJECTS_PER_PAGE (order
);
1186 size_t size
= OBJECT_SIZE (order
);
1189 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
1193 if (p
->context_depth
!= G
.context_depth
)
1194 /* Since we don't do any collection for pages in pushed
1195 contexts, there's no need to do any poisoning. And
1196 besides, the IN_USE_P array isn't valid until we pop
1200 for (i
= 0; i
< num_objects
; i
++)
1203 word
= i
/ HOST_BITS_PER_LONG
;
1204 bit
= i
% HOST_BITS_PER_LONG
;
1205 if (((p
->in_use_p
[word
] >> bit
) & 1) == 0)
1206 memset (p
->page
+ i
* size
, 0xa5, size
);
1213 /* Top level mark-and-sweep routine. */
1218 /* Avoid frequent unnecessary work by skipping collection if the
1219 total allocations haven't expanded much since the last
1221 #ifndef GGC_ALWAYS_COLLECT
1222 if (G
.allocated
< GGC_MIN_EXPAND_FOR_GC
* G
.allocated_last_gc
)
1226 timevar_push (TV_GC
);
1228 fprintf (stderr
, " {GC %luk -> ", (unsigned long) G
.allocated
/ 1024);
1230 /* Zero the total allocated bytes. This will be recalculated in the
1234 /* Release the pages we freed the last time we collected, but didn't
1235 reuse in the interim. */
1247 G
.allocated_last_gc
= G
.allocated
;
1248 if (G
.allocated_last_gc
< GGC_MIN_LAST_ALLOCATED
)
1249 G
.allocated_last_gc
= GGC_MIN_LAST_ALLOCATED
;
1251 timevar_pop (TV_GC
);
1254 fprintf (stderr
, "%luk}", (unsigned long) G
.allocated
/ 1024);
1257 /* Print allocation statistics. */
1258 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1260 : ((x) < 1024*1024*10 \
1262 : (x) / (1024*1024))))
1263 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1266 ggc_print_statistics ()
1268 struct ggc_statistics stats
;
1270 size_t total_overhead
= 0;
1272 /* Clear the statistics. */
1273 memset (&stats
, 0, sizeof (stats
));
1275 /* Make sure collection will really occur. */
1276 G
.allocated_last_gc
= 0;
1278 /* Collect and print the statistics common across collectors. */
1279 ggc_print_common_statistics (stderr
, &stats
);
1281 /* Release free pages so that we will not count the bytes allocated
1282 there as part of the total allocated memory. */
1285 /* Collect some information about the various sizes of
1287 fprintf (stderr
, "\n%-5s %10s %10s %10s\n",
1288 "Log", "Allocated", "Used", "Overhead");
1289 for (i
= 0; i
< NUM_ORDERS
; ++i
)
1296 /* Skip empty entries. */
1300 overhead
= allocated
= in_use
= 0;
1302 /* Figure out the total number of bytes allocated for objects of
1303 this size, and how many of them are actually in use. Also figure
1304 out how much memory the page table is using. */
1305 for (p
= G
.pages
[i
]; p
; p
= p
->next
)
1307 allocated
+= p
->bytes
;
1309 (OBJECTS_PER_PAGE (i
) - p
->num_free_objects
) * OBJECT_SIZE (i
);
1311 overhead
+= (sizeof (page_entry
) - sizeof (long)
1312 + BITMAP_SIZE (OBJECTS_PER_PAGE (i
) + 1));
1314 fprintf (stderr
, "%-5d %10ld%c %10ld%c %10ld%c\n", i
,
1315 SCALE (allocated
), LABEL (allocated
),
1316 SCALE (in_use
), LABEL (in_use
),
1317 SCALE (overhead
), LABEL (overhead
));
1318 total_overhead
+= overhead
;
1320 fprintf (stderr
, "%-5s %10ld%c %10ld%c %10ld%c\n", "Total",
1321 SCALE (G
.bytes_mapped
), LABEL (G
.bytes_mapped
),
1322 SCALE (G
.allocated
), LABEL(G
.allocated
),
1323 SCALE (total_overhead
), LABEL (total_overhead
));