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
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
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
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
37 # include <sys/mman.h>
39 # define MAP_FAILED -1
41 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
42 # define MAP_ANONYMOUS MAP_ANON
48 #ifdef HAVE_MMAP_DEV_ZERO
50 # include <sys/mman.h>
52 # define MAP_FAILED -1
59 #define USING_MALLOC_PAGE_GROUPS
64 This garbage-collecting allocator allocates objects on one of a set
65 of pages. Each page can allocate objects of a single size only;
66 available sizes are powers of two starting at four bytes. The size
67 of an allocation request is rounded up to the next power of two
68 (`order'), and satisfied from the appropriate page.
70 Each page is recorded in a page-entry, which also maintains an
71 in-use bitmap of object positions on the page. This allows the
72 allocation state of a particular object to be flipped without
73 touching the page itself.
75 Each page-entry also has a context depth, which is used to track
76 pushing and popping of allocation contexts. Only objects allocated
77 in the current (highest-numbered) context may be collected.
79 Page entries are arranged in an array of singly-linked lists. The
80 array is indexed by the allocation size, in bits, of the pages on
81 it; i.e. all pages on a list allocate objects of the same size.
82 Pages are ordered on the list such that all non-full pages precede
83 all full pages, with non-full pages arranged in order of decreasing
86 Empty pages (of all orders) are kept on a single page cache list,
87 and are considered first when new pages are required; they are
88 deallocated at the start of the next collection if they haven't
89 been recycled by then. */
92 /* Define GGC_POISON to poison memory marked unused by the collector. */
95 /* Define GGC_ALWAYS_COLLECT to perform collection every time
96 ggc_collect is invoked. Otherwise, collection is performed only
97 when a significant amount of memory has been allocated since the
99 #undef GGC_ALWAYS_COLLECT
101 #ifdef ENABLE_GC_CHECKING
104 #ifdef ENABLE_GC_ALWAYS_COLLECT
105 #define GGC_ALWAYS_COLLECT
108 /* Define GGC_DEBUG_LEVEL to print debugging information.
109 0: No debugging output.
110 1: GC statistics only.
111 2: Page-entry allocations/deallocations as well.
112 3: Object allocations as well.
113 4: Object marks as well. */
114 #define GGC_DEBUG_LEVEL (0)
116 #ifndef HOST_BITS_PER_PTR
117 #define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
121 /* A two-level tree is used to look up the page-entry for a given
122 pointer. Two chunks of the pointer's bits are extracted to index
123 the first and second levels of the tree, as follows:
127 msb +----------------+----+------+------+ lsb
133 The bottommost HOST_PAGE_SIZE_BITS are ignored, since page-entry
134 pages are aligned on system page boundaries. The next most
135 significant PAGE_L2_BITS and PAGE_L1_BITS are the second and first
136 index values in the lookup table, respectively.
138 For 32-bit architectures and the settings below, there are no
139 leftover bits. For architectures with wider pointers, the lookup
140 tree points to a list of pages, which must be scanned to find the
143 #define PAGE_L1_BITS (8)
144 #define PAGE_L2_BITS (32 - PAGE_L1_BITS - G.lg_pagesize)
145 #define PAGE_L1_SIZE ((size_t) 1 << PAGE_L1_BITS)
146 #define PAGE_L2_SIZE ((size_t) 1 << PAGE_L2_BITS)
148 #define LOOKUP_L1(p) \
149 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
151 #define LOOKUP_L2(p) \
152 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
154 /* The number of objects per allocation page, for objects on a page of
155 the indicated ORDER. */
156 #define OBJECTS_PER_PAGE(ORDER) objects_per_page_table[ORDER]
158 /* The size of an object on a page of the indicated ORDER. */
159 #define OBJECT_SIZE(ORDER) object_size_table[ORDER]
161 /* The number of extra orders, not corresponding to power-of-two sized
164 #define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
166 /* The Ith entry is the maximum size of an object to be stored in the
167 Ith extra order. Adding a new entry to this array is the *only*
168 thing you need to do to add a new special allocation size. */
170 static const size_t extra_order_size_table
[] = {
171 sizeof (struct tree_decl
),
172 sizeof (struct tree_list
)
175 /* The total number of orders. */
177 #define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
179 /* We use this structure to determine the alignment required for
180 allocations. For power-of-two sized allocations, that's not a
181 problem, but it does matter for odd-sized allocations. */
183 struct max_alignment
{
187 #ifdef HAVE_LONG_DOUBLE
195 /* The biggest alignment required. */
197 #define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
199 /* The Ith entry is the number of objects on a page or order I. */
201 static unsigned objects_per_page_table
[NUM_ORDERS
];
203 /* The Ith entry is the size of an object on a page of order I. */
205 static size_t object_size_table
[NUM_ORDERS
];
207 /* A page_entry records the status of an allocation page. This
208 structure is dynamically sized to fit the bitmap in_use_p. */
209 typedef struct page_entry
211 /* The next page-entry with objects of the same size, or NULL if
212 this is the last page-entry. */
213 struct page_entry
*next
;
215 /* The number of bytes allocated. (This will always be a multiple
216 of the host system page size.) */
219 /* The address at which the memory is allocated. */
222 #ifdef USING_MALLOC_PAGE_GROUPS
223 /* Back pointer to the page group this page came from. */
224 struct page_group
*group
;
227 /* Saved in-use bit vector for pages that aren't in the topmost
228 context during collection. */
229 unsigned long *save_in_use_p
;
231 /* Context depth of this page. */
232 unsigned short context_depth
;
234 /* The number of free objects remaining on this page. */
235 unsigned short num_free_objects
;
237 /* A likely candidate for the bit position of a free object for the
238 next allocation from this page. */
239 unsigned short next_bit_hint
;
241 /* The lg of size of objects allocated from this page. */
244 /* A bit vector indicating whether or not objects are in use. The
245 Nth bit is one if the Nth object on this page is allocated. This
246 array is dynamically sized. */
247 unsigned long in_use_p
[1];
250 #ifdef USING_MALLOC_PAGE_GROUPS
251 /* A page_group describes a large allocation from malloc, from which
252 we parcel out aligned pages. */
253 typedef struct page_group
255 /* A linked list of all extant page groups. */
256 struct page_group
*next
;
258 /* The address we received from malloc. */
261 /* The size of the block. */
264 /* A bitmask of pages in use. */
269 #if HOST_BITS_PER_PTR <= 32
271 /* On 32-bit hosts, we use a two level page table, as pictured above. */
272 typedef page_entry
**page_table
[PAGE_L1_SIZE
];
276 /* On 64-bit hosts, we use the same two level page tables plus a linked
277 list that disambiguates the top 32-bits. There will almost always be
278 exactly one entry in the list. */
279 typedef struct page_table_chain
281 struct page_table_chain
*next
;
283 page_entry
**table
[PAGE_L1_SIZE
];
288 /* The rest of the global variables. */
289 static struct globals
291 /* The Nth element in this array is a page with objects of size 2^N.
292 If there are any pages with free objects, they will be at the
293 head of the list. NULL if there are no page-entries for this
295 page_entry
*pages
[NUM_ORDERS
];
297 /* The Nth element in this array is the last page with objects of
298 size 2^N. NULL if there are no page-entries for this object
300 page_entry
*page_tails
[NUM_ORDERS
];
302 /* Lookup table for associating allocation pages with object addresses. */
305 /* The system's page size. */
309 /* Bytes currently allocated. */
312 /* Bytes currently allocated at the end of the last collection. */
313 size_t allocated_last_gc
;
315 /* Total amount of memory mapped. */
318 /* The current depth in the context stack. */
319 unsigned short context_depth
;
321 /* A file descriptor open to /dev/zero for reading. */
322 #if defined (HAVE_MMAP_DEV_ZERO)
326 /* A cache of free system pages. */
327 page_entry
*free_pages
;
329 #ifdef USING_MALLOC_PAGE_GROUPS
330 page_group
*page_groups
;
333 /* The file descriptor for debugging output. */
337 /* The size in bytes required to maintain a bitmap for the objects
339 #define BITMAP_SIZE(Num_objects) \
340 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
342 /* Skip garbage collection if the current allocation is not at least
343 this factor times the allocation at the end of the last collection.
344 In other words, total allocation must expand by (this factor minus
345 one) before collection is performed. */
346 #define GGC_MIN_EXPAND_FOR_GC (1.3)
348 /* Bound `allocated_last_gc' to 4MB, to prevent the memory expansion
349 test from triggering too often when the heap is small. */
350 #define GGC_MIN_LAST_ALLOCATED (4 * 1024 * 1024)
352 /* Allocate pages in chunks of this size, to throttle calls to memory
353 allocation routines. The first page is used, the rest go onto the
354 free list. This cannot be larger than HOST_BITS_PER_INT for the
355 in_use bitmask for page_group. */
356 #define GGC_QUIRE_SIZE 16
358 static int ggc_allocated_p
PARAMS ((const void *));
359 static page_entry
*lookup_page_table_entry
PARAMS ((const void *));
360 static void set_page_table_entry
PARAMS ((void *, page_entry
*));
362 static char *alloc_anon
PARAMS ((char *, size_t));
364 #ifdef USING_MALLOC_PAGE_GROUPS
365 static size_t page_group_index
PARAMS ((char *, char *));
366 static void set_page_group_in_use
PARAMS ((page_group
*, char *));
367 static void clear_page_group_in_use
PARAMS ((page_group
*, char *));
369 static struct page_entry
* alloc_page
PARAMS ((unsigned));
370 static void free_page
PARAMS ((struct page_entry
*));
371 static void release_pages
PARAMS ((void));
372 static void clear_marks
PARAMS ((void));
373 static void sweep_pages
PARAMS ((void));
374 static void ggc_recalculate_in_use_p
PARAMS ((page_entry
*));
377 static void poison_pages
PARAMS ((void));
380 void debug_print_page_list
PARAMS ((int));
382 /* Returns non-zero if P was allocated in GC'able memory. */
391 #if HOST_BITS_PER_PTR <= 32
394 page_table table
= G
.lookup
;
395 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
400 if (table
->high_bits
== high_bits
)
404 base
= &table
->table
[0];
407 /* Extract the level 1 and 2 indices. */
411 return base
[L1
] && base
[L1
][L2
];
414 /* Traverse the page table and find the entry for a page.
415 Die (probably) if the object wasn't allocated via GC. */
417 static inline page_entry
*
418 lookup_page_table_entry(p
)
424 #if HOST_BITS_PER_PTR <= 32
427 page_table table
= G
.lookup
;
428 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
429 while (table
->high_bits
!= high_bits
)
431 base
= &table
->table
[0];
434 /* Extract the level 1 and 2 indices. */
441 /* Set the page table entry for a page. */
444 set_page_table_entry(p
, entry
)
451 #if HOST_BITS_PER_PTR <= 32
455 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
456 for (table
= G
.lookup
; table
; table
= table
->next
)
457 if (table
->high_bits
== high_bits
)
460 /* Not found -- allocate a new table. */
461 table
= (page_table
) xcalloc (1, sizeof(*table
));
462 table
->next
= G
.lookup
;
463 table
->high_bits
= high_bits
;
466 base
= &table
->table
[0];
469 /* Extract the level 1 and 2 indices. */
473 if (base
[L1
] == NULL
)
474 base
[L1
] = (page_entry
**) xcalloc (PAGE_L2_SIZE
, sizeof (page_entry
*));
476 base
[L1
][L2
] = entry
;
479 /* Prints the page-entry for object size ORDER, for debugging. */
482 debug_print_page_list (order
)
486 printf ("Head=%p, Tail=%p:\n", (PTR
) G
.pages
[order
],
487 (PTR
) G
.page_tails
[order
]);
491 printf ("%p(%1d|%3d) -> ", (PTR
) p
, p
->context_depth
,
492 p
->num_free_objects
);
500 /* Allocate SIZE bytes of anonymous memory, preferably near PREF,
501 (if non-null). The ifdef structure here is intended to cause a
502 compile error unless exactly one of the HAVE_* is defined. */
505 alloc_anon (pref
, size
)
506 char *pref ATTRIBUTE_UNUSED
;
509 #ifdef HAVE_MMAP_ANON
510 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
511 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
513 #ifdef HAVE_MMAP_DEV_ZERO
514 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
515 MAP_PRIVATE
, G
.dev_zero_fd
, 0);
518 if (page
== (char *) MAP_FAILED
)
520 perror ("virtual memory exhausted");
521 exit (FATAL_EXIT_CODE
);
524 /* Remember that we allocated this memory. */
525 G
.bytes_mapped
+= size
;
530 #ifdef USING_MALLOC_PAGE_GROUPS
531 /* Compute the index for this page into the page group. */
534 page_group_index (allocation
, page
)
535 char *allocation
, *page
;
537 return (size_t) (page
- allocation
) >> G
.lg_pagesize
;
540 /* Set and clear the in_use bit for this page in the page group. */
543 set_page_group_in_use (group
, page
)
547 group
->in_use
|= 1 << page_group_index (group
->allocation
, page
);
551 clear_page_group_in_use (group
, page
)
555 group
->in_use
&= ~(1 << page_group_index (group
->allocation
, page
));
559 /* Allocate a new page for allocating objects of size 2^ORDER,
560 and return an entry for it. The entry is not added to the
561 appropriate page_table list. */
563 static inline struct page_entry
*
567 struct page_entry
*entry
, *p
, **pp
;
571 size_t page_entry_size
;
573 #ifdef USING_MALLOC_PAGE_GROUPS
577 num_objects
= OBJECTS_PER_PAGE (order
);
578 bitmap_size
= BITMAP_SIZE (num_objects
+ 1);
579 page_entry_size
= sizeof (page_entry
) - sizeof (long) + bitmap_size
;
580 entry_size
= num_objects
* OBJECT_SIZE (order
);
581 if (entry_size
< G
.pagesize
)
582 entry_size
= G
.pagesize
;
587 /* Check the list of free pages for one we can use. */
588 for (pp
= &G
.free_pages
, p
= *pp
; p
; pp
= &p
->next
, p
= *pp
)
589 if (p
->bytes
== entry_size
)
594 /* Recycle the allocated memory from this page ... */
598 #ifdef USING_MALLOC_PAGE_GROUPS
602 /* ... and, if possible, the page entry itself. */
603 if (p
->order
== order
)
606 memset (entry
, 0, page_entry_size
);
612 else if (entry_size
== G
.pagesize
)
614 /* We want just one page. Allocate a bunch of them and put the
615 extras on the freelist. (Can only do this optimization with
616 mmap for backing store.) */
617 struct page_entry
*e
, *f
= G
.free_pages
;
620 page
= alloc_anon (NULL
, G
.pagesize
* GGC_QUIRE_SIZE
);
622 /* This loop counts down so that the chain will be in ascending
624 for (i
= GGC_QUIRE_SIZE
- 1; i
>= 1; i
--)
626 e
= (struct page_entry
*) xcalloc (1, page_entry_size
);
628 e
->bytes
= G
.pagesize
;
629 e
->page
= page
+ (i
<< G
.lg_pagesize
);
637 page
= alloc_anon (NULL
, entry_size
);
639 #ifdef USING_MALLOC_PAGE_GROUPS
642 /* Allocate a large block of memory and serve out the aligned
643 pages therein. This results in much less memory wastage
644 than the traditional implementation of valloc. */
646 char *allocation
, *a
, *enda
;
647 size_t alloc_size
, head_slop
, tail_slop
;
648 int multiple_pages
= (entry_size
== G
.pagesize
);
651 alloc_size
= GGC_QUIRE_SIZE
* G
.pagesize
;
653 alloc_size
= entry_size
+ G
.pagesize
- 1;
654 allocation
= xmalloc (alloc_size
);
656 page
= (char *) (((size_t) allocation
+ G
.pagesize
- 1) & -G
.pagesize
);
657 head_slop
= page
- allocation
;
659 tail_slop
= ((size_t) allocation
+ alloc_size
) & (G
.pagesize
- 1);
661 tail_slop
= alloc_size
- entry_size
- head_slop
;
662 enda
= allocation
+ alloc_size
- tail_slop
;
664 /* We allocated N pages, which are likely not aligned, leaving
665 us with N-1 usable pages. We plan to place the page_group
666 structure somewhere in the slop. */
667 if (head_slop
>= sizeof (page_group
))
668 group
= (page_group
*)page
- 1;
671 /* We magically got an aligned allocation. Too bad, we have
672 to waste a page anyway. */
676 tail_slop
+= G
.pagesize
;
678 if (tail_slop
< sizeof (page_group
))
680 group
= (page_group
*)enda
;
681 tail_slop
-= sizeof (page_group
);
684 /* Remember that we allocated this memory. */
685 group
->next
= G
.page_groups
;
686 group
->allocation
= allocation
;
687 group
->alloc_size
= alloc_size
;
689 G
.page_groups
= group
;
690 G
.bytes_mapped
+= alloc_size
;
692 /* If we allocated multiple pages, put the rest on the free list. */
695 struct page_entry
*e
, *f
= G
.free_pages
;
696 for (a
= enda
- G
.pagesize
; a
!= page
; a
-= G
.pagesize
)
698 e
= (struct page_entry
*) xcalloc (1, page_entry_size
);
700 e
->bytes
= G
.pagesize
;
712 entry
= (struct page_entry
*) xcalloc (1, page_entry_size
);
714 entry
->bytes
= entry_size
;
716 entry
->context_depth
= G
.context_depth
;
717 entry
->order
= order
;
718 entry
->num_free_objects
= num_objects
;
719 entry
->next_bit_hint
= 1;
721 #ifdef USING_MALLOC_PAGE_GROUPS
722 entry
->group
= group
;
723 set_page_group_in_use (group
, page
);
726 /* Set the one-past-the-end in-use bit. This acts as a sentry as we
727 increment the hint. */
728 entry
->in_use_p
[num_objects
/ HOST_BITS_PER_LONG
]
729 = (unsigned long) 1 << (num_objects
% HOST_BITS_PER_LONG
);
731 set_page_table_entry (page
, entry
);
733 if (GGC_DEBUG_LEVEL
>= 2)
734 fprintf (G
.debug_file
,
735 "Allocating page at %p, object size=%ld, data %p-%p\n",
736 (PTR
) entry
, (long) OBJECT_SIZE (order
), page
,
737 page
+ entry_size
- 1);
742 /* For a page that is no longer needed, put it on the free page list. */
748 if (GGC_DEBUG_LEVEL
>= 2)
749 fprintf (G
.debug_file
,
750 "Deallocating page at %p, data %p-%p\n", (PTR
) entry
,
751 entry
->page
, entry
->page
+ entry
->bytes
- 1);
753 set_page_table_entry (entry
->page
, NULL
);
755 #ifdef USING_MALLOC_PAGE_GROUPS
756 clear_page_group_in_use (entry
->group
, entry
->page
);
759 entry
->next
= G
.free_pages
;
760 G
.free_pages
= entry
;
763 /* Release the free page cache to the system. */
769 page_entry
*p
, *next
;
773 /* Gather up adjacent pages so they are unmapped together. */
784 while (p
&& p
->page
== start
+ len
)
793 G
.bytes_mapped
-= len
;
798 #ifdef USING_MALLOC_PAGE_GROUPS
802 /* Remove all pages from free page groups from the list. */
804 while ((p
= *pp
) != NULL
)
805 if (p
->group
->in_use
== 0)
813 /* Remove all free page groups, and release the storage. */
815 while ((g
= *gp
) != NULL
)
819 G
.bytes_mapped
-= g
->alloc_size
;
820 free (g
->allocation
);
827 /* This table provides a fast way to determine ceil(log_2(size)) for
828 allocation requests. The minimum allocation size is eight bytes. */
830 static unsigned char size_lookup
[257] =
832 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
833 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
834 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
835 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
836 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
837 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
838 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
839 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
840 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
841 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
842 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
843 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
844 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
845 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
846 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
847 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
851 /* Allocate a chunk of memory of SIZE bytes. If ZERO is non-zero, the
852 memory is zeroed; otherwise, its contents are undefined. */
858 unsigned order
, word
, bit
, object_offset
;
859 struct page_entry
*entry
;
863 order
= size_lookup
[size
];
867 while (size
> OBJECT_SIZE (order
))
871 /* If there are non-full pages for this size allocation, they are at
872 the head of the list. */
873 entry
= G
.pages
[order
];
875 /* If there is no page for this object size, or all pages in this
876 context are full, allocate a new page. */
877 if (entry
== NULL
|| entry
->num_free_objects
== 0)
879 struct page_entry
*new_entry
;
880 new_entry
= alloc_page (order
);
882 /* If this is the only entry, it's also the tail. */
884 G
.page_tails
[order
] = new_entry
;
886 /* Put new pages at the head of the page list. */
887 new_entry
->next
= entry
;
889 G
.pages
[order
] = new_entry
;
891 /* For a new page, we know the word and bit positions (in the
892 in_use bitmap) of the first available object -- they're zero. */
893 new_entry
->next_bit_hint
= 1;
900 /* First try to use the hint left from the previous allocation
901 to locate a clear bit in the in-use bitmap. We've made sure
902 that the one-past-the-end bit is always set, so if the hint
903 has run over, this test will fail. */
904 unsigned hint
= entry
->next_bit_hint
;
905 word
= hint
/ HOST_BITS_PER_LONG
;
906 bit
= hint
% HOST_BITS_PER_LONG
;
908 /* If the hint didn't work, scan the bitmap from the beginning. */
909 if ((entry
->in_use_p
[word
] >> bit
) & 1)
912 while (~entry
->in_use_p
[word
] == 0)
914 while ((entry
->in_use_p
[word
] >> bit
) & 1)
916 hint
= word
* HOST_BITS_PER_LONG
+ bit
;
919 /* Next time, try the next bit. */
920 entry
->next_bit_hint
= hint
+ 1;
922 object_offset
= hint
* OBJECT_SIZE (order
);
925 /* Set the in-use bit. */
926 entry
->in_use_p
[word
] |= ((unsigned long) 1 << bit
);
928 /* Keep a running total of the number of free objects. If this page
929 fills up, we may have to move it to the end of the list if the
930 next page isn't full. If the next page is full, all subsequent
931 pages are full, so there's no need to move it. */
932 if (--entry
->num_free_objects
== 0
933 && entry
->next
!= NULL
934 && entry
->next
->num_free_objects
> 0)
936 G
.pages
[order
] = entry
->next
;
938 G
.page_tails
[order
]->next
= entry
;
939 G
.page_tails
[order
] = entry
;
942 /* Calculate the object's address. */
943 result
= entry
->page
+ object_offset
;
946 /* `Poison' the entire allocated object, including any padding at
948 memset (result
, 0xaf, OBJECT_SIZE (order
));
951 /* Keep track of how many bytes are being allocated. This
952 information is used in deciding when to collect. */
953 G
.allocated
+= OBJECT_SIZE (order
);
955 if (GGC_DEBUG_LEVEL
>= 3)
956 fprintf (G
.debug_file
,
957 "Allocating object, requested size=%ld, actual=%ld at %p on %p\n",
958 (long) size
, (long) OBJECT_SIZE (order
), result
, (PTR
) entry
);
963 /* If P is not marked, marks it and return false. Otherwise return true.
964 P must have been allocated by the GC allocator; it mustn't point to
965 static objects, stack variables, or memory allocated with malloc. */
975 /* Look up the page on which the object is alloced. If the object
976 wasn't allocated by the collector, we'll probably die. */
977 entry
= lookup_page_table_entry (p
);
978 #ifdef ENABLE_CHECKING
983 /* Calculate the index of the object on the page; this is its bit
984 position in the in_use_p bitmap. */
985 bit
= (((const char *) p
) - entry
->page
) / OBJECT_SIZE (entry
->order
);
986 word
= bit
/ HOST_BITS_PER_LONG
;
987 mask
= (unsigned long) 1 << (bit
% HOST_BITS_PER_LONG
);
989 /* If the bit was previously set, skip it. */
990 if (entry
->in_use_p
[word
] & mask
)
993 /* Otherwise set it, and decrement the free object count. */
994 entry
->in_use_p
[word
] |= mask
;
995 entry
->num_free_objects
-= 1;
997 if (GGC_DEBUG_LEVEL
>= 4)
998 fprintf (G
.debug_file
, "Marking %p\n", p
);
1003 /* Return 1 if P has been marked, zero otherwise.
1004 P must have been allocated by the GC allocator; it mustn't point to
1005 static objects, stack variables, or memory allocated with malloc. */
1015 /* Look up the page on which the object is alloced. If the object
1016 wasn't allocated by the collector, we'll probably die. */
1017 entry
= lookup_page_table_entry (p
);
1018 #ifdef ENABLE_CHECKING
1023 /* Calculate the index of the object on the page; this is its bit
1024 position in the in_use_p bitmap. */
1025 bit
= (((const char *) p
) - entry
->page
) / OBJECT_SIZE (entry
->order
);
1026 word
= bit
/ HOST_BITS_PER_LONG
;
1027 mask
= (unsigned long) 1 << (bit
% HOST_BITS_PER_LONG
);
1029 return (entry
->in_use_p
[word
] & mask
) != 0;
1032 /* Return the size of the gc-able object P. */
1038 page_entry
*pe
= lookup_page_table_entry (p
);
1039 return OBJECT_SIZE (pe
->order
);
1042 /* Initialize the ggc-mmap allocator. */
1049 G
.pagesize
= getpagesize();
1050 G
.lg_pagesize
= exact_log2 (G
.pagesize
);
1052 #ifdef HAVE_MMAP_DEV_ZERO
1053 G
.dev_zero_fd
= open ("/dev/zero", O_RDONLY
);
1054 if (G
.dev_zero_fd
== -1)
1059 G
.debug_file
= fopen ("ggc-mmap.debug", "w");
1061 G
.debug_file
= stdout
;
1064 G
.allocated_last_gc
= GGC_MIN_LAST_ALLOCATED
;
1067 /* StunOS has an amazing off-by-one error for the first mmap allocation
1068 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1069 believe, is an unaligned page allocation, which would cause us to
1070 hork badly if we tried to use it. */
1072 char *p
= alloc_anon (NULL
, G
.pagesize
);
1073 struct page_entry
*e
;
1074 if ((size_t)p
& (G
.pagesize
- 1))
1076 /* How losing. Discard this one and try another. If we still
1077 can't get something useful, give up. */
1079 p
= alloc_anon (NULL
, G
.pagesize
);
1080 if ((size_t)p
& (G
.pagesize
- 1))
1084 /* We have a good page, might as well hold onto it... */
1085 e
= (struct page_entry
*) xcalloc (1, sizeof (struct page_entry
));
1086 e
->bytes
= G
.pagesize
;
1088 e
->next
= G
.free_pages
;
1093 /* Initialize the object size table. */
1094 for (order
= 0; order
< HOST_BITS_PER_PTR
; ++order
)
1095 object_size_table
[order
] = (size_t) 1 << order
;
1096 for (order
= HOST_BITS_PER_PTR
; order
< NUM_ORDERS
; ++order
)
1098 size_t s
= extra_order_size_table
[order
- HOST_BITS_PER_PTR
];
1100 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1101 so that we're sure of getting aligned memory. */
1102 s
= CEIL (s
, MAX_ALIGNMENT
) * MAX_ALIGNMENT
;
1103 object_size_table
[order
] = s
;
1106 /* Initialize the objects-per-page table. */
1107 for (order
= 0; order
< NUM_ORDERS
; ++order
)
1109 objects_per_page_table
[order
] = G
.pagesize
/ OBJECT_SIZE (order
);
1110 if (objects_per_page_table
[order
] == 0)
1111 objects_per_page_table
[order
] = 1;
1114 /* Reset the size_lookup array to put appropriately sized objects in
1115 the special orders. All objects bigger than the previous power
1116 of two, but no greater than the special size, should go in the
1118 for (order
= HOST_BITS_PER_PTR
; order
< NUM_ORDERS
; ++order
)
1123 o
= size_lookup
[OBJECT_SIZE (order
)];
1124 for (i
= OBJECT_SIZE (order
); size_lookup
[i
] == o
; --i
)
1125 size_lookup
[i
] = order
;
1129 /* Increment the `GC context'. Objects allocated in an outer context
1130 are never freed, eliminating the need to register their roots. */
1138 if (G
.context_depth
== 0)
1142 /* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1143 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1146 ggc_recalculate_in_use_p (p
)
1152 /* Because the past-the-end bit in in_use_p is always set, we
1153 pretend there is one additional object. */
1154 num_objects
= OBJECTS_PER_PAGE (p
->order
) + 1;
1156 /* Reset the free object count. */
1157 p
->num_free_objects
= num_objects
;
1159 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1161 i
< CEIL (BITMAP_SIZE (num_objects
),
1162 sizeof (*p
->in_use_p
));
1167 /* Something is in use if it is marked, or if it was in use in a
1168 context further down the context stack. */
1169 p
->in_use_p
[i
] |= p
->save_in_use_p
[i
];
1171 /* Decrement the free object count for every object allocated. */
1172 for (j
= p
->in_use_p
[i
]; j
; j
>>= 1)
1173 p
->num_free_objects
-= (j
& 1);
1176 if (p
->num_free_objects
>= num_objects
)
1180 /* Decrement the `GC context'. All objects allocated since the
1181 previous ggc_push_context are migrated to the outer context. */
1186 unsigned order
, depth
;
1188 depth
= --G
.context_depth
;
1190 /* Any remaining pages in the popped context are lowered to the new
1191 current context; i.e. objects allocated in the popped context and
1192 left over are imported into the previous context. */
1193 for (order
= 2; order
< NUM_ORDERS
; order
++)
1197 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
1199 if (p
->context_depth
> depth
)
1200 p
->context_depth
= depth
;
1202 /* If this page is now in the topmost context, and we'd
1203 saved its allocation state, restore it. */
1204 else if (p
->context_depth
== depth
&& p
->save_in_use_p
)
1206 ggc_recalculate_in_use_p (p
);
1207 free (p
->save_in_use_p
);
1208 p
->save_in_use_p
= 0;
1214 /* Unmark all objects. */
1221 for (order
= 2; order
< NUM_ORDERS
; order
++)
1223 size_t num_objects
= OBJECTS_PER_PAGE (order
);
1224 size_t bitmap_size
= BITMAP_SIZE (num_objects
+ 1);
1227 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
1229 #ifdef ENABLE_CHECKING
1230 /* The data should be page-aligned. */
1231 if ((size_t) p
->page
& (G
.pagesize
- 1))
1235 /* Pages that aren't in the topmost context are not collected;
1236 nevertheless, we need their in-use bit vectors to store GC
1237 marks. So, back them up first. */
1238 if (p
->context_depth
< G
.context_depth
)
1240 if (! p
->save_in_use_p
)
1241 p
->save_in_use_p
= xmalloc (bitmap_size
);
1242 memcpy (p
->save_in_use_p
, p
->in_use_p
, bitmap_size
);
1245 /* Reset reset the number of free objects and clear the
1246 in-use bits. These will be adjusted by mark_obj. */
1247 p
->num_free_objects
= num_objects
;
1248 memset (p
->in_use_p
, 0, bitmap_size
);
1250 /* Make sure the one-past-the-end bit is always set. */
1251 p
->in_use_p
[num_objects
/ HOST_BITS_PER_LONG
]
1252 = ((unsigned long) 1 << (num_objects
% HOST_BITS_PER_LONG
));
1257 /* Free all empty pages. Partially empty pages need no attention
1258 because the `mark' bit doubles as an `unused' bit. */
1265 for (order
= 2; order
< NUM_ORDERS
; order
++)
1267 /* The last page-entry to consider, regardless of entries
1268 placed at the end of the list. */
1269 page_entry
* const last
= G
.page_tails
[order
];
1271 size_t num_objects
= OBJECTS_PER_PAGE (order
);
1272 size_t live_objects
;
1273 page_entry
*p
, *previous
;
1283 page_entry
*next
= p
->next
;
1285 /* Loop until all entries have been examined. */
1288 /* Add all live objects on this page to the count of
1289 allocated memory. */
1290 live_objects
= num_objects
- p
->num_free_objects
;
1292 G
.allocated
+= OBJECT_SIZE (order
) * live_objects
;
1294 /* Only objects on pages in the topmost context should get
1296 if (p
->context_depth
< G
.context_depth
)
1299 /* Remove the page if it's empty. */
1300 else if (live_objects
== 0)
1303 G
.pages
[order
] = next
;
1305 previous
->next
= next
;
1307 /* Are we removing the last element? */
1308 if (p
== G
.page_tails
[order
])
1309 G
.page_tails
[order
] = previous
;
1314 /* If the page is full, move it to the end. */
1315 else if (p
->num_free_objects
== 0)
1317 /* Don't move it if it's already at the end. */
1318 if (p
!= G
.page_tails
[order
])
1320 /* Move p to the end of the list. */
1322 G
.page_tails
[order
]->next
= p
;
1324 /* Update the tail pointer... */
1325 G
.page_tails
[order
] = p
;
1327 /* ... and the head pointer, if necessary. */
1329 G
.pages
[order
] = next
;
1331 previous
->next
= next
;
1336 /* If we've fallen through to here, it's a page in the
1337 topmost context that is neither full nor empty. Such a
1338 page must precede pages at lesser context depth in the
1339 list, so move it to the head. */
1340 else if (p
!= G
.pages
[order
])
1342 previous
->next
= p
->next
;
1343 p
->next
= G
.pages
[order
];
1345 /* Are we moving the last element? */
1346 if (G
.page_tails
[order
] == p
)
1347 G
.page_tails
[order
] = previous
;
1356 /* Now, restore the in_use_p vectors for any pages from contexts
1357 other than the current one. */
1358 for (p
= G
.pages
[order
]; p
; p
= p
->next
)
1359 if (p
->context_depth
!= G
.context_depth
)
1360 ggc_recalculate_in_use_p (p
);
1365 /* Clobber all free objects. */
1372 for (order
= 2; order
< NUM_ORDERS
; order
++)
1374 size_t num_objects
= OBJECTS_PER_PAGE (order
);
1375 size_t size
= OBJECT_SIZE (order
);
1378 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
1382 if (p
->context_depth
!= G
.context_depth
)
1383 /* Since we don't do any collection for pages in pushed
1384 contexts, there's no need to do any poisoning. And
1385 besides, the IN_USE_P array isn't valid until we pop
1389 for (i
= 0; i
< num_objects
; i
++)
1392 word
= i
/ HOST_BITS_PER_LONG
;
1393 bit
= i
% HOST_BITS_PER_LONG
;
1394 if (((p
->in_use_p
[word
] >> bit
) & 1) == 0)
1395 memset (p
->page
+ i
* size
, 0xa5, size
);
1402 /* Top level mark-and-sweep routine. */
1407 /* Avoid frequent unnecessary work by skipping collection if the
1408 total allocations haven't expanded much since the last
1410 #ifndef GGC_ALWAYS_COLLECT
1411 if (G
.allocated
< GGC_MIN_EXPAND_FOR_GC
* G
.allocated_last_gc
)
1415 timevar_push (TV_GC
);
1417 fprintf (stderr
, " {GC %luk -> ", (unsigned long) G
.allocated
/ 1024);
1419 /* Zero the total allocated bytes. This will be recalculated in the
1423 /* Release the pages we freed the last time we collected, but didn't
1424 reuse in the interim. */
1436 G
.allocated_last_gc
= G
.allocated
;
1437 if (G
.allocated_last_gc
< GGC_MIN_LAST_ALLOCATED
)
1438 G
.allocated_last_gc
= GGC_MIN_LAST_ALLOCATED
;
1440 timevar_pop (TV_GC
);
1443 fprintf (stderr
, "%luk}", (unsigned long) G
.allocated
/ 1024);
1446 /* Print allocation statistics. */
1447 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1449 : ((x) < 1024*1024*10 \
1451 : (x) / (1024*1024))))
1452 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1455 ggc_print_statistics ()
1457 struct ggc_statistics stats
;
1459 size_t total_overhead
= 0;
1461 /* Clear the statistics. */
1462 memset (&stats
, 0, sizeof (stats
));
1464 /* Make sure collection will really occur. */
1465 G
.allocated_last_gc
= 0;
1467 /* Collect and print the statistics common across collectors. */
1468 ggc_print_common_statistics (stderr
, &stats
);
1470 /* Release free pages so that we will not count the bytes allocated
1471 there as part of the total allocated memory. */
1474 /* Collect some information about the various sizes of
1476 fprintf (stderr
, "\n%-5s %10s %10s %10s\n",
1477 "Size", "Allocated", "Used", "Overhead");
1478 for (i
= 0; i
< NUM_ORDERS
; ++i
)
1485 /* Skip empty entries. */
1489 overhead
= allocated
= in_use
= 0;
1491 /* Figure out the total number of bytes allocated for objects of
1492 this size, and how many of them are actually in use. Also figure
1493 out how much memory the page table is using. */
1494 for (p
= G
.pages
[i
]; p
; p
= p
->next
)
1496 allocated
+= p
->bytes
;
1498 (OBJECTS_PER_PAGE (i
) - p
->num_free_objects
) * OBJECT_SIZE (i
);
1500 overhead
+= (sizeof (page_entry
) - sizeof (long)
1501 + BITMAP_SIZE (OBJECTS_PER_PAGE (i
) + 1));
1503 fprintf (stderr
, "%-5d %10ld%c %10ld%c %10ld%c\n", OBJECT_SIZE (i
),
1504 SCALE (allocated
), LABEL (allocated
),
1505 SCALE (in_use
), LABEL (in_use
),
1506 SCALE (overhead
), LABEL (overhead
));
1507 total_overhead
+= overhead
;
1509 fprintf (stderr
, "%-5s %10ld%c %10ld%c %10ld%c\n", "Total",
1510 SCALE (G
.bytes_mapped
), LABEL (G
.bytes_mapped
),
1511 SCALE (G
.allocated
), LABEL(G
.allocated
),
1512 SCALE (total_overhead
), LABEL (total_overhead
));