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
23 #include "coretypes.h"
34 #ifdef ENABLE_VALGRIND_CHECKING
37 /* Avoid #ifdef:s when we can help it. */
38 #define VALGRIND_DISCARD(x)
41 /* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
42 file open. Prefer either to valloc. */
44 # undef HAVE_MMAP_DEV_ZERO
46 # include <sys/mman.h>
48 # define MAP_FAILED -1
50 # if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
51 # define MAP_ANONYMOUS MAP_ANON
57 #ifdef HAVE_MMAP_DEV_ZERO
59 # include <sys/mman.h>
61 # define MAP_FAILED -1
68 #define USING_MALLOC_PAGE_GROUPS
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
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
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:
119 msb +----------------+----+------+------+ lsb
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
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
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
{
193 #ifdef HAVE_LONG_DOUBLE
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). */
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.) */
236 /* The address at which the memory is allocated. */
239 #ifdef USING_MALLOC_PAGE_GROUPS
240 /* Back pointer to the page group this page came from. */
241 struct page_group
*group
;
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. */
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];
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. */
278 /* The size of the block. */
281 /* A bitmask of pages in use. */
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
];
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
;
300 page_entry
**table
[PAGE_L1_SIZE
];
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
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
317 page_entry
*page_tails
[NUM_ORDERS
];
319 /* Lookup table for associating allocation pages with object addresses. */
322 /* The system's page size. */
326 /* Bytes currently allocated. */
329 /* Bytes currently allocated at the end of the last collection. */
330 size_t allocated_last_gc
;
332 /* Total amount of memory 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)
343 /* A cache of free system pages. */
344 page_entry
*free_pages
;
346 #ifdef USING_MALLOC_PAGE_GROUPS
347 page_group
*page_groups
;
350 /* The file descriptor for debugging output. */
354 /* The size in bytes required to maintain a bitmap for the objects
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
*));
369 static char *alloc_anon
PARAMS ((char *, size_t));
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 *));
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));
388 void debug_print_page_list
PARAMS ((int));
390 /* Returns nonzero if P was allocated in GC'able memory. */
399 #if HOST_BITS_PER_PTR <= 32
402 page_table table
= G
.lookup
;
403 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
408 if (table
->high_bits
== high_bits
)
412 base
= &table
->table
[0];
415 /* Extract the level 1 and 2 indices. */
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
)
432 #if HOST_BITS_PER_PTR <= 32
435 page_table table
= G
.lookup
;
436 size_t high_bits
= (size_t) p
& ~ (size_t) 0xffffffff;
437 while (table
->high_bits
!= high_bits
)
439 base
= &table
->table
[0];
442 /* Extract the level 1 and 2 indices. */
449 /* Set the page table entry for a page. */
452 set_page_table_entry(p
, entry
)
459 #if HOST_BITS_PER_PTR <= 32
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
)
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
;
474 base
= &table
->table
[0];
477 /* Extract the level 1 and 2 indices. */
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. */
490 debug_print_page_list (order
)
494 printf ("Head=%p, Tail=%p:\n", (PTR
) G
.pages
[order
],
495 (PTR
) G
.page_tails
[order
]);
499 printf ("%p(%1d|%3d) -> ", (PTR
) p
, p
->context_depth
,
500 p
->num_free_objects
);
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. */
513 alloc_anon (pref
, size
)
514 char *pref ATTRIBUTE_UNUSED
;
517 #ifdef HAVE_MMAP_ANON
518 char *page
= (char *) mmap (pref
, size
, PROT_READ
| PROT_WRITE
,
519 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
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);
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
));
543 #ifdef USING_MALLOC_PAGE_GROUPS
544 /* Compute the index for this page into the page group. */
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. */
556 set_page_group_in_use (group
, page
)
560 group
->in_use
|= 1 << page_group_index (group
->allocation
, page
);
564 clear_page_group_in_use (group
, page
)
568 group
->in_use
&= ~(1 << page_group_index (group
->allocation
, page
));
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
*
580 struct page_entry
*entry
, *p
, **pp
;
584 size_t page_entry_size
;
586 #ifdef USING_MALLOC_PAGE_GROUPS
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
;
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
)
607 /* Recycle the allocated memory from this page ... */
611 #ifdef USING_MALLOC_PAGE_GROUPS
615 /* ... and, if possible, the page entry itself. */
616 if (p
->order
== order
)
619 memset (entry
, 0, page_entry_size
);
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
;
633 page
= alloc_anon (NULL
, G
.pagesize
* GGC_QUIRE_SIZE
);
635 /* This loop counts down so that the chain will be in ascending
637 for (i
= GGC_QUIRE_SIZE
- 1; i
>= 1; i
--)
639 e
= (struct page_entry
*) xcalloc (1, page_entry_size
);
641 e
->bytes
= G
.pagesize
;
642 e
->page
= page
+ (i
<< G
.lg_pagesize
);
650 page
= alloc_anon (NULL
, entry_size
);
652 #ifdef USING_MALLOC_PAGE_GROUPS
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
);
664 alloc_size
= GGC_QUIRE_SIZE
* G
.pagesize
;
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
;
672 tail_slop
= ((size_t) allocation
+ alloc_size
) & (G
.pagesize
- 1);
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;
684 /* We magically got an aligned allocation. Too bad, we have
685 to waste a page anyway. */
689 tail_slop
+= G
.pagesize
;
691 if (tail_slop
< sizeof (page_group
))
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
;
702 G
.page_groups
= group
;
703 G
.bytes_mapped
+= alloc_size
;
705 /* If we allocated multiple pages, put the rest on the free list. */
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
);
713 e
->bytes
= G
.pagesize
;
725 entry
= (struct page_entry
*) xcalloc (1, page_entry_size
);
727 entry
->bytes
= entry_size
;
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
);
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);
755 /* For a page that is no longer needed, put it on the free page list. */
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
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
);
776 entry
->next
= G
.free_pages
;
777 G
.free_pages
= entry
;
780 /* Release the free page cache to the system. */
786 page_entry
*p
, *next
;
790 /* Gather up adjacent pages so they are unmapped together. */
801 while (p
&& p
->page
== start
+ len
)
810 G
.bytes_mapped
-= len
;
815 #ifdef USING_MALLOC_PAGE_GROUPS
819 /* Remove all pages from free page groups from the list. */
821 while ((p
= *pp
) != NULL
)
822 if (p
->group
->in_use
== 0)
830 /* Remove all free page groups, and release the storage. */
832 while ((g
= *gp
) != NULL
)
836 G
.bytes_mapped
-= g
->alloc_size
;
837 free (g
->allocation
);
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. */
875 unsigned order
, word
, bit
, object_offset
;
876 struct page_entry
*entry
;
880 order
= size_lookup
[size
];
884 while (size
> OBJECT_SIZE (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. */
901 G
.page_tails
[order
] = new_entry
;
903 /* Put new pages at the head of the page list. */
904 new_entry
->next
= 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;
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)
929 while (~entry
->in_use_p
[word
] == 0)
931 while ((entry
->in_use_p
[word
] >> bit
) & 1)
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
;
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
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
));
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
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
,
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. */
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
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
)
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
);
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. */
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
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. */
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
1085 compute_inverse (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)
1097 DIV_MULT (order
) = 1;
1098 DIV_SHIFT (order
) = 0;
1102 size
= OBJECT_SIZE (order
);
1104 while (size
% 2 == 0)
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. */
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)
1134 G
.debug_file
= fopen ("ggc-mmap.debug", "w");
1136 G
.debug_file
= stdout
;
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))
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
;
1161 e
->next
= G
.free_pages
;
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
1192 for (order
= HOST_BITS_PER_PTR
; order
< NUM_ORDERS
; ++order
)
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. */
1212 if (G
.context_depth
== 0)
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. */
1220 ggc_recalculate_in_use_p (p
)
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. */
1235 i
< CEIL (BITMAP_SIZE (num_objects
),
1236 sizeof (*p
->in_use_p
));
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
)
1254 /* Decrement the `GC context'. All objects allocated since the
1255 previous ggc_push_context are migrated to the outer 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
++)
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. */
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);
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))
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. */
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
;
1357 page_entry
*next
= p
->next
;
1359 /* Loop until all entries have been examined. */
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
1370 if (p
->context_depth
< G
.context_depth
)
1373 /* Remove the page if it's empty. */
1374 else if (live_objects
== 0)
1377 G
.pages
[order
] = next
;
1379 previous
->next
= next
;
1381 /* Are we removing the last element? */
1382 if (p
== G
.page_tails
[order
])
1383 G
.page_tails
[order
] = 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. */
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. */
1403 G
.pages
[order
] = next
;
1405 previous
->next
= next
;
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
];
1419 /* Are we moving the last element? */
1420 if (G
.page_tails
[order
] == p
)
1421 G
.page_tails
[order
] = previous
;
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. */
1446 for (order
= 2; order
< NUM_ORDERS
; order
++)
1448 size_t num_objects
= OBJECTS_PER_PAGE (order
);
1449 size_t size
= OBJECT_SIZE (order
);
1452 for (p
= G
.pages
[order
]; p
!= NULL
; p
= p
->next
)
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
1463 for (i
= 0; i
< num_objects
; i
++)
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
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
));
1488 /* Top level mark-and-sweep routine. */
1493 /* Avoid frequent unnecessary work by skipping collection if the
1494 total allocations haven't expanded much since the last
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
)
1504 timevar_push (TV_GC
);
1506 fprintf (stderr
, " {GC %luk -> ", (unsigned long) G
.allocated
/ 1024);
1508 /* Zero the total allocated bytes. This will be recalculated in the
1512 /* Release the pages we freed the last time we collected, but didn't
1513 reuse in the interim. */
1519 #ifdef ENABLE_GC_CHECKING
1525 G
.allocated_last_gc
= G
.allocated
;
1527 timevar_pop (TV_GC
);
1530 fprintf (stderr
, "%luk}", (unsigned long) G
.allocated
/ 1024);
1533 /* Print allocation statistics. */
1534 #define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1536 : ((x) < 1024*1024*10 \
1538 : (x) / (1024*1024))))
1539 #define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
1542 ggc_print_statistics ()
1544 struct ggc_statistics stats
;
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. */
1561 /* Collect some information about the various sizes of
1563 fprintf (stderr
, "\n%-5s %10s %10s %10s\n",
1564 "Size", "Allocated", "Used", "Overhead");
1565 for (i
= 0; i
< NUM_ORDERS
; ++i
)
1572 /* Skip empty entries. */
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
;
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
));