]> gcc.gnu.org Git - gcc.git/blame - gcc/ggc-page.c
* cfgloop.c (flow_loops_cfg_dump): Use bb->index, not i.
[gcc.git] / gcc / ggc-page.c
CommitLineData
21341cfd 1/* "Bag-of-pages" garbage collector for the GNU compiler.
c4f2c499 2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
21341cfd 3
1322177d 4This file is part of GCC.
21341cfd 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
21341cfd 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
21341cfd 15
b9bfacf0 16You should have received a copy of the GNU General Public License
1322177d
LB
17along with GCC; see the file COPYING. If not, write to the Free
18Software Foundation, 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
21341cfd 20
21341cfd 21#include "config.h"
21341cfd
AS
22#include "system.h"
23#include "tree.h"
e5ecd4ea 24#include "rtl.h"
1b42a6a9 25#include "tm_p.h"
b9bfacf0 26#include "toplev.h"
21341cfd
AS
27#include "varray.h"
28#include "flags.h"
e5ecd4ea 29#include "ggc.h"
2a9a326b 30#include "timevar.h"
e5ecd4ea 31
825b6926
ZW
32/* Prefer MAP_ANON(YMOUS) to /dev/zero, since we don't need to keep a
33 file open. Prefer either to valloc. */
34#ifdef HAVE_MMAP_ANON
35# undef HAVE_MMAP_DEV_ZERO
825b6926
ZW
36
37# include <sys/mman.h>
38# ifndef MAP_FAILED
39# define MAP_FAILED -1
40# endif
41# if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
42# define MAP_ANONYMOUS MAP_ANON
43# endif
44# define USING_MMAP
45
005537df 46#endif
21341cfd 47
825b6926 48#ifdef HAVE_MMAP_DEV_ZERO
825b6926
ZW
49
50# include <sys/mman.h>
51# ifndef MAP_FAILED
52# define MAP_FAILED -1
53# endif
54# define USING_MMAP
55
8342b467
RH
56#endif
57
130fadbb
RH
58#ifndef USING_MMAP
59#define USING_MALLOC_PAGE_GROUPS
5b918807 60#endif
21341cfd
AS
61
62/* Stategy:
63
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.
69
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.
74
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.
78
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
84 context depth.
85
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. */
90
91
92/* Define GGC_POISON to poison memory marked unused by the collector. */
93#undef GGC_POISON
94
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
98 last collection. */
85f88abf 99#undef GGC_ALWAYS_COLLECT
21341cfd 100
f4524c9e 101#ifdef ENABLE_GC_CHECKING
21341cfd 102#define GGC_POISON
f4524c9e
ZW
103#endif
104#ifdef ENABLE_GC_ALWAYS_COLLECT
21341cfd
AS
105#define GGC_ALWAYS_COLLECT
106#endif
107
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.
6d2f8887 113 4: Object marks as well. */
21341cfd
AS
114#define GGC_DEBUG_LEVEL (0)
115\f
116#ifndef HOST_BITS_PER_PTR
117#define HOST_BITS_PER_PTR HOST_BITS_PER_LONG
118#endif
119
21341cfd
AS
120\f
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:
124
125 HOST_PAGE_SIZE_BITS
126 32 | |
127 msb +----------------+----+------+------+ lsb
128 | | |
129 PAGE_L1_BITS |
130 | |
131 PAGE_L2_BITS
132
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.
137
005537df
RH
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
141 correct one. */
21341cfd
AS
142
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)
147
148#define LOOKUP_L1(p) \
149 (((size_t) (p) >> (32 - PAGE_L1_BITS)) & ((1 << PAGE_L1_BITS) - 1))
150
151#define LOOKUP_L2(p) \
152 (((size_t) (p) >> G.lg_pagesize) & ((1 << PAGE_L2_BITS) - 1))
153
2be510b8
MM
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]
157
158/* The size of an object on a page of the indicated ORDER. */
159#define OBJECT_SIZE(ORDER) object_size_table[ORDER]
160
161/* The number of extra orders, not corresponding to power-of-two sized
162 objects. */
163
ca7558fc 164#define NUM_EXTRA_ORDERS ARRAY_SIZE (extra_order_size_table)
2be510b8
MM
165
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. */
169
170static const size_t extra_order_size_table[] = {
171 sizeof (struct tree_decl),
172 sizeof (struct tree_list)
173};
174
175/* The total number of orders. */
176
177#define NUM_ORDERS (HOST_BITS_PER_PTR + NUM_EXTRA_ORDERS)
178
b1095f9c
MM
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. */
182
183struct max_alignment {
184 char c;
185 union {
186 HOST_WIDEST_INT i;
187#ifdef HAVE_LONG_DOUBLE
188 long double d;
189#else
190 double d;
191#endif
192 } u;
193};
194
195/* The biggest alignment required. */
196
197#define MAX_ALIGNMENT (offsetof (struct max_alignment, u))
198
2be510b8
MM
199/* The Ith entry is the number of objects on a page or order I. */
200
201static unsigned objects_per_page_table[NUM_ORDERS];
202
203/* The Ith entry is the size of an object on a page of order I. */
204
205static size_t object_size_table[NUM_ORDERS];
21341cfd
AS
206
207/* A page_entry records the status of an allocation page. This
208 structure is dynamically sized to fit the bitmap in_use_p. */
209typedef struct page_entry
210{
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;
214
215 /* The number of bytes allocated. (This will always be a multiple
216 of the host system page size.) */
217 size_t bytes;
218
219 /* The address at which the memory is allocated. */
220 char *page;
221
130fadbb
RH
222#ifdef USING_MALLOC_PAGE_GROUPS
223 /* Back pointer to the page group this page came from. */
224 struct page_group *group;
225#endif
226
21341cfd
AS
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;
230
231 /* Context depth of this page. */
ae373eda 232 unsigned short context_depth;
21341cfd
AS
233
234 /* The number of free objects remaining on this page. */
235 unsigned short num_free_objects;
236
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;
240
ae373eda
MM
241 /* The lg of size of objects allocated from this page. */
242 unsigned char order;
243
21341cfd
AS
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];
248} page_entry;
249
130fadbb
RH
250#ifdef USING_MALLOC_PAGE_GROUPS
251/* A page_group describes a large allocation from malloc, from which
252 we parcel out aligned pages. */
253typedef struct page_group
254{
255 /* A linked list of all extant page groups. */
256 struct page_group *next;
257
258 /* The address we received from malloc. */
259 char *allocation;
260
261 /* The size of the block. */
262 size_t alloc_size;
263
264 /* A bitmask of pages in use. */
265 unsigned int in_use;
266} page_group;
267#endif
21341cfd
AS
268
269#if HOST_BITS_PER_PTR <= 32
270
271/* On 32-bit hosts, we use a two level page table, as pictured above. */
272typedef page_entry **page_table[PAGE_L1_SIZE];
273
274#else
275
005537df
RH
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
21341cfd
AS
278 exactly one entry in the list. */
279typedef struct page_table_chain
280{
281 struct page_table_chain *next;
282 size_t high_bits;
283 page_entry **table[PAGE_L1_SIZE];
284} *page_table;
285
286#endif
287
288/* The rest of the global variables. */
289static struct globals
290{
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
294 object size. */
2be510b8 295 page_entry *pages[NUM_ORDERS];
21341cfd
AS
296
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
299 size. */
2be510b8 300 page_entry *page_tails[NUM_ORDERS];
21341cfd
AS
301
302 /* Lookup table for associating allocation pages with object addresses. */
303 page_table lookup;
304
305 /* The system's page size. */
306 size_t pagesize;
307 size_t lg_pagesize;
308
309 /* Bytes currently allocated. */
310 size_t allocated;
311
312 /* Bytes currently allocated at the end of the last collection. */
313 size_t allocated_last_gc;
314
3277221c
MM
315 /* Total amount of memory mapped. */
316 size_t bytes_mapped;
317
21341cfd 318 /* The current depth in the context stack. */
d416576b 319 unsigned short context_depth;
21341cfd
AS
320
321 /* A file descriptor open to /dev/zero for reading. */
825b6926 322#if defined (HAVE_MMAP_DEV_ZERO)
21341cfd
AS
323 int dev_zero_fd;
324#endif
325
326 /* A cache of free system pages. */
327 page_entry *free_pages;
328
130fadbb
RH
329#ifdef USING_MALLOC_PAGE_GROUPS
330 page_group *page_groups;
331#endif
332
21341cfd
AS
333 /* The file descriptor for debugging output. */
334 FILE *debug_file;
335} G;
336
21341cfd
AS
337/* The size in bytes required to maintain a bitmap for the objects
338 on a page-entry. */
339#define BITMAP_SIZE(Num_objects) \
2be510b8 340 (CEIL ((Num_objects), HOST_BITS_PER_LONG) * sizeof(long))
21341cfd
AS
341
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)
347
a70261ee
RH
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)
351
130fadbb
RH
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. */
054f5e69 356#define GGC_QUIRE_SIZE 16
21341cfd 357\f
3fe41456
KG
358static int ggc_allocated_p PARAMS ((const void *));
359static page_entry *lookup_page_table_entry PARAMS ((const void *));
360static void set_page_table_entry PARAMS ((void *, page_entry *));
130fadbb 361#ifdef USING_MMAP
3fe41456 362static char *alloc_anon PARAMS ((char *, size_t));
130fadbb
RH
363#endif
364#ifdef USING_MALLOC_PAGE_GROUPS
365static size_t page_group_index PARAMS ((char *, char *));
366static void set_page_group_in_use PARAMS ((page_group *, char *));
367static void clear_page_group_in_use PARAMS ((page_group *, char *));
368#endif
3fe41456
KG
369static struct page_entry * alloc_page PARAMS ((unsigned));
370static void free_page PARAMS ((struct page_entry *));
371static void release_pages PARAMS ((void));
372static void clear_marks PARAMS ((void));
373static void sweep_pages PARAMS ((void));
374static void ggc_recalculate_in_use_p PARAMS ((page_entry *));
21341cfd
AS
375
376#ifdef GGC_POISON
3fe41456 377static void poison_pages PARAMS ((void));
21341cfd
AS
378#endif
379
3fe41456 380void debug_print_page_list PARAMS ((int));
21341cfd 381\f
005537df 382/* Returns non-zero if P was allocated in GC'able memory. */
21341cfd 383
005537df
RH
384static inline int
385ggc_allocated_p (p)
386 const void *p;
21341cfd
AS
387{
388 page_entry ***base;
005537df 389 size_t L1, L2;
21341cfd
AS
390
391#if HOST_BITS_PER_PTR <= 32
392 base = &G.lookup[0];
393#else
394 page_table table = G.lookup;
395 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
005537df
RH
396 while (1)
397 {
398 if (table == NULL)
399 return 0;
400 if (table->high_bits == high_bits)
401 break;
402 table = table->next;
403 }
21341cfd
AS
404 base = &table->table[0];
405#endif
406
eaec9b3d 407 /* Extract the level 1 and 2 indices. */
74c937ca
MM
408 L1 = LOOKUP_L1 (p);
409 L2 = LOOKUP_L2 (p);
410
411 return base[L1] && base[L1][L2];
412}
413
414/* Traverse the page table and find the entry for a page.
415 Die (probably) if the object wasn't allocated via GC. */
416
417static inline page_entry *
418lookup_page_table_entry(p)
005537df 419 const void *p;
74c937ca
MM
420{
421 page_entry ***base;
422 size_t L1, L2;
423
005537df
RH
424#if HOST_BITS_PER_PTR <= 32
425 base = &G.lookup[0];
426#else
427 page_table table = G.lookup;
428 size_t high_bits = (size_t) p & ~ (size_t) 0xffffffff;
429 while (table->high_bits != high_bits)
430 table = table->next;
431 base = &table->table[0];
432#endif
74c937ca 433
eaec9b3d 434 /* Extract the level 1 and 2 indices. */
21341cfd
AS
435 L1 = LOOKUP_L1 (p);
436 L2 = LOOKUP_L2 (p);
437
438 return base[L1][L2];
439}
440
21341cfd 441/* Set the page table entry for a page. */
cb2ec151 442
21341cfd
AS
443static void
444set_page_table_entry(p, entry)
445 void *p;
446 page_entry *entry;
447{
448 page_entry ***base;
449 size_t L1, L2;
450
451#if HOST_BITS_PER_PTR <= 32
452 base = &G.lookup[0];
453#else
454 page_table table;
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)
458 goto found;
459
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;
464 G.lookup = table;
465found:
466 base = &table->table[0];
467#endif
468
eaec9b3d 469 /* Extract the level 1 and 2 indices. */
21341cfd
AS
470 L1 = LOOKUP_L1 (p);
471 L2 = LOOKUP_L2 (p);
472
473 if (base[L1] == NULL)
474 base[L1] = (page_entry **) xcalloc (PAGE_L2_SIZE, sizeof (page_entry *));
475
476 base[L1][L2] = entry;
477}
478
21341cfd 479/* Prints the page-entry for object size ORDER, for debugging. */
cb2ec151 480
21341cfd
AS
481void
482debug_print_page_list (order)
483 int order;
484{
485 page_entry *p;
683eb0e9
JM
486 printf ("Head=%p, Tail=%p:\n", (PTR) G.pages[order],
487 (PTR) G.page_tails[order]);
21341cfd
AS
488 p = G.pages[order];
489 while (p != NULL)
490 {
683eb0e9
JM
491 printf ("%p(%1d|%3d) -> ", (PTR) p, p->context_depth,
492 p->num_free_objects);
21341cfd
AS
493 p = p->next;
494 }
495 printf ("NULL\n");
496 fflush (stdout);
497}
498
130fadbb 499#ifdef USING_MMAP
21341cfd 500/* Allocate SIZE bytes of anonymous memory, preferably near PREF,
825b6926
ZW
501 (if non-null). The ifdef structure here is intended to cause a
502 compile error unless exactly one of the HAVE_* is defined. */
cb2ec151 503
21341cfd
AS
504static inline char *
505alloc_anon (pref, size)
005537df 506 char *pref ATTRIBUTE_UNUSED;
21341cfd
AS
507 size_t size;
508{
825b6926
ZW
509#ifdef HAVE_MMAP_ANON
510 char *page = (char *) mmap (pref, size, PROT_READ | PROT_WRITE,
511 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
512#endif
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);
21341cfd 516#endif
825b6926
ZW
517
518 if (page == (char *) MAP_FAILED)
005537df 519 {
1f978f5f 520 perror ("virtual memory exhausted");
bd0f0717 521 exit (FATAL_EXIT_CODE);
005537df 522 }
21341cfd 523
3277221c
MM
524 /* Remember that we allocated this memory. */
525 G.bytes_mapped += size;
526
21341cfd
AS
527 return page;
528}
130fadbb
RH
529#endif
530#ifdef USING_MALLOC_PAGE_GROUPS
531/* Compute the index for this page into the page group. */
532
533static inline size_t
534page_group_index (allocation, page)
535 char *allocation, *page;
536{
c4f2c499 537 return (size_t) (page - allocation) >> G.lg_pagesize;
130fadbb
RH
538}
539
540/* Set and clear the in_use bit for this page in the page group. */
541
542static inline void
543set_page_group_in_use (group, page)
544 page_group *group;
545 char *page;
546{
547 group->in_use |= 1 << page_group_index (group->allocation, page);
548}
549
550static inline void
551clear_page_group_in_use (group, page)
552 page_group *group;
553 char *page;
554{
555 group->in_use &= ~(1 << page_group_index (group->allocation, page));
556}
557#endif
21341cfd
AS
558
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. */
cb2ec151 562
21341cfd
AS
563static inline struct page_entry *
564alloc_page (order)
565 unsigned order;
566{
567 struct page_entry *entry, *p, **pp;
568 char *page;
569 size_t num_objects;
570 size_t bitmap_size;
571 size_t page_entry_size;
572 size_t entry_size;
130fadbb
RH
573#ifdef USING_MALLOC_PAGE_GROUPS
574 page_group *group;
575#endif
21341cfd
AS
576
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;
2be510b8 580 entry_size = num_objects * OBJECT_SIZE (order);
ca79429a
RH
581 if (entry_size < G.pagesize)
582 entry_size = G.pagesize;
21341cfd
AS
583
584 entry = NULL;
585 page = NULL;
586
587 /* Check the list of free pages for one we can use. */
bd0f0717 588 for (pp = &G.free_pages, p = *pp; p; pp = &p->next, p = *pp)
21341cfd
AS
589 if (p->bytes == entry_size)
590 break;
591
592 if (p != NULL)
593 {
dc297297 594 /* Recycle the allocated memory from this page ... */
21341cfd
AS
595 *pp = p->next;
596 page = p->page;
bd0f0717 597
130fadbb
RH
598#ifdef USING_MALLOC_PAGE_GROUPS
599 group = p->group;
600#endif
bd0f0717 601
21341cfd
AS
602 /* ... and, if possible, the page entry itself. */
603 if (p->order == order)
604 {
605 entry = p;
606 memset (entry, 0, page_entry_size);
607 }
608 else
609 free (p);
610 }
825b6926 611#ifdef USING_MMAP
054f5e69 612 else if (entry_size == G.pagesize)
21341cfd 613 {
054f5e69
ZW
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;
618 int i;
619
ca79429a 620 page = alloc_anon (NULL, G.pagesize * GGC_QUIRE_SIZE);
bd0f0717 621
054f5e69
ZW
622 /* This loop counts down so that the chain will be in ascending
623 memory order. */
624 for (i = GGC_QUIRE_SIZE - 1; i >= 1; i--)
625 {
ca79429a
RH
626 e = (struct page_entry *) xcalloc (1, page_entry_size);
627 e->order = order;
628 e->bytes = G.pagesize;
629 e->page = page + (i << G.lg_pagesize);
054f5e69
ZW
630 e->next = f;
631 f = e;
632 }
bd0f0717 633
054f5e69 634 G.free_pages = f;
21341cfd 635 }
054f5e69
ZW
636 else
637 page = alloc_anon (NULL, entry_size);
130fadbb
RH
638#endif
639#ifdef USING_MALLOC_PAGE_GROUPS
640 else
641 {
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. */
645
646 char *allocation, *a, *enda;
647 size_t alloc_size, head_slop, tail_slop;
648 int multiple_pages = (entry_size == G.pagesize);
649
650 if (multiple_pages)
651 alloc_size = GGC_QUIRE_SIZE * G.pagesize;
652 else
653 alloc_size = entry_size + G.pagesize - 1;
654 allocation = xmalloc (alloc_size);
655
c4f2c499 656 page = (char *) (((size_t) allocation + G.pagesize - 1) & -G.pagesize);
130fadbb
RH
657 head_slop = page - allocation;
658 if (multiple_pages)
659 tail_slop = ((size_t) allocation + alloc_size) & (G.pagesize - 1);
660 else
661 tail_slop = alloc_size - entry_size - head_slop;
662 enda = allocation + alloc_size - tail_slop;
663
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;
669 else
670 {
671 /* We magically got an aligned allocation. Too bad, we have
672 to waste a page anyway. */
673 if (tail_slop == 0)
674 {
675 enda -= G.pagesize;
676 tail_slop += G.pagesize;
677 }
678 if (tail_slop < sizeof (page_group))
679 abort ();
680 group = (page_group *)enda;
681 tail_slop -= sizeof (page_group);
682 }
683
684 /* Remember that we allocated this memory. */
685 group->next = G.page_groups;
686 group->allocation = allocation;
687 group->alloc_size = alloc_size;
688 group->in_use = 0;
689 G.page_groups = group;
690 G.bytes_mapped += alloc_size;
691
692 /* If we allocated multiple pages, put the rest on the free list. */
693 if (multiple_pages)
694 {
695 struct page_entry *e, *f = G.free_pages;
696 for (a = enda - G.pagesize; a != page; a -= G.pagesize)
697 {
698 e = (struct page_entry *) xcalloc (1, page_entry_size);
699 e->order = order;
700 e->bytes = G.pagesize;
701 e->page = a;
702 e->group = group;
703 e->next = f;
704 f = e;
705 }
706 G.free_pages = f;
707 }
708 }
709#endif
21341cfd
AS
710
711 if (entry == NULL)
712 entry = (struct page_entry *) xcalloc (1, page_entry_size);
713
714 entry->bytes = entry_size;
715 entry->page = page;
716 entry->context_depth = G.context_depth;
717 entry->order = order;
718 entry->num_free_objects = num_objects;
719 entry->next_bit_hint = 1;
720
130fadbb
RH
721#ifdef USING_MALLOC_PAGE_GROUPS
722 entry->group = group;
723 set_page_group_in_use (group, page);
724#endif
725
21341cfd
AS
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);
730
731 set_page_table_entry (page, entry);
732
733 if (GGC_DEBUG_LEVEL >= 2)
734 fprintf (G.debug_file,
8a951190
AJ
735 "Allocating page at %p, object size=%lu, data %p-%p\n",
736 (PTR) entry, (unsigned long) OBJECT_SIZE (order), page,
bd0f0717 737 page + entry_size - 1);
21341cfd
AS
738
739 return entry;
740}
741
cb2ec151 742/* For a page that is no longer needed, put it on the free page list. */
21341cfd 743
21341cfd
AS
744static inline void
745free_page (entry)
746 page_entry *entry;
747{
748 if (GGC_DEBUG_LEVEL >= 2)
749 fprintf (G.debug_file,
683eb0e9 750 "Deallocating page at %p, data %p-%p\n", (PTR) entry,
21341cfd
AS
751 entry->page, entry->page + entry->bytes - 1);
752
753 set_page_table_entry (entry->page, NULL);
754
130fadbb
RH
755#ifdef USING_MALLOC_PAGE_GROUPS
756 clear_page_group_in_use (entry->group, entry->page);
757#endif
758
21341cfd
AS
759 entry->next = G.free_pages;
760 G.free_pages = entry;
761}
762
cb2ec151 763/* Release the free page cache to the system. */
21341cfd 764
4934cc53 765static void
21341cfd
AS
766release_pages ()
767{
825b6926 768#ifdef USING_MMAP
130fadbb 769 page_entry *p, *next;
21341cfd
AS
770 char *start;
771 size_t len;
772
054f5e69 773 /* Gather up adjacent pages so they are unmapped together. */
21341cfd 774 p = G.free_pages;
21341cfd
AS
775
776 while (p)
777 {
054f5e69 778 start = p->page;
21341cfd 779 next = p->next;
054f5e69 780 len = p->bytes;
21341cfd
AS
781 free (p);
782 p = next;
21341cfd 783
054f5e69
ZW
784 while (p && p->page == start + len)
785 {
786 next = p->next;
787 len += p->bytes;
788 free (p);
789 p = next;
790 }
791
792 munmap (start, len);
793 G.bytes_mapped -= len;
794 }
005537df 795
21341cfd 796 G.free_pages = NULL;
130fadbb
RH
797#endif
798#ifdef USING_MALLOC_PAGE_GROUPS
799 page_entry **pp, *p;
800 page_group **gp, *g;
801
802 /* Remove all pages from free page groups from the list. */
803 pp = &G.free_pages;
804 while ((p = *pp) != NULL)
805 if (p->group->in_use == 0)
806 {
807 *pp = p->next;
808 free (p);
809 }
810 else
811 pp = &p->next;
812
813 /* Remove all free page groups, and release the storage. */
814 gp = &G.page_groups;
815 while ((g = *gp) != NULL)
816 if (g->in_use == 0)
817 {
818 *gp = g->next;
819 G.bytes_mapped -= g->alloc_size;
820 free (g->allocation);
821 }
822 else
823 gp = &g->next;
824#endif
21341cfd
AS
825}
826
21341cfd 827/* This table provides a fast way to determine ceil(log_2(size)) for
9fd51e67 828 allocation requests. The minimum allocation size is eight bytes. */
cb2ec151 829
2be510b8 830static unsigned char size_lookup[257] =
9fd51e67
ZW
831{
832 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
21341cfd
AS
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,
848 8
849};
850
851/* Allocate a chunk of memory of SIZE bytes. If ZERO is non-zero, the
852 memory is zeroed; otherwise, its contents are undefined. */
cb2ec151 853
005537df 854void *
f8a83ee3 855ggc_alloc (size)
21341cfd 856 size_t size;
21341cfd
AS
857{
858 unsigned order, word, bit, object_offset;
859 struct page_entry *entry;
860 void *result;
861
862 if (size <= 256)
863 order = size_lookup[size];
864 else
865 {
866 order = 9;
2be510b8 867 while (size > OBJECT_SIZE (order))
21341cfd
AS
868 order++;
869 }
870
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];
874
875 /* If there is no page for this object size, or all pages in this
876 context are full, allocate a new page. */
4934cc53 877 if (entry == NULL || entry->num_free_objects == 0)
21341cfd
AS
878 {
879 struct page_entry *new_entry;
880 new_entry = alloc_page (order);
881
882 /* If this is the only entry, it's also the tail. */
883 if (entry == NULL)
884 G.page_tails[order] = new_entry;
885
886 /* Put new pages at the head of the page list. */
887 new_entry->next = entry;
888 entry = new_entry;
889 G.pages[order] = new_entry;
890
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;
894 word = 0;
895 bit = 0;
896 object_offset = 0;
897 }
898 else
899 {
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;
907
908 /* If the hint didn't work, scan the bitmap from the beginning. */
909 if ((entry->in_use_p[word] >> bit) & 1)
910 {
911 word = bit = 0;
912 while (~entry->in_use_p[word] == 0)
913 ++word;
914 while ((entry->in_use_p[word] >> bit) & 1)
915 ++bit;
916 hint = word * HOST_BITS_PER_LONG + bit;
917 }
918
919 /* Next time, try the next bit. */
920 entry->next_bit_hint = hint + 1;
921
2be510b8 922 object_offset = hint * OBJECT_SIZE (order);
21341cfd
AS
923 }
924
925 /* Set the in-use bit. */
926 entry->in_use_p[word] |= ((unsigned long) 1 << bit);
927
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)
935 {
936 G.pages[order] = entry->next;
937 entry->next = NULL;
938 G.page_tails[order]->next = entry;
939 G.page_tails[order] = entry;
940 }
941
942 /* Calculate the object's address. */
943 result = entry->page + object_offset;
944
945#ifdef GGC_POISON
f8a83ee3
ZW
946 /* `Poison' the entire allocated object, including any padding at
947 the end. */
2be510b8 948 memset (result, 0xaf, OBJECT_SIZE (order));
21341cfd 949#endif
cb2ec151 950
21341cfd
AS
951 /* Keep track of how many bytes are being allocated. This
952 information is used in deciding when to collect. */
2be510b8 953 G.allocated += OBJECT_SIZE (order);
21341cfd
AS
954
955 if (GGC_DEBUG_LEVEL >= 3)
956 fprintf (G.debug_file,
8a951190
AJ
957 "Allocating object, requested size=%lu, actual=%lu at %p on %p\n",
958 (unsigned long) size, (unsigned long) OBJECT_SIZE (order), result,
959 (PTR) entry);
21341cfd
AS
960
961 return result;
962}
963
cb2ec151 964/* If P is not marked, marks it and return false. Otherwise return true.
21341cfd
AS
965 P must have been allocated by the GC allocator; it mustn't point to
966 static objects, stack variables, or memory allocated with malloc. */
cb2ec151 967
005537df
RH
968int
969ggc_set_mark (p)
3cce094d 970 const void *p;
21341cfd
AS
971{
972 page_entry *entry;
973 unsigned bit, word;
974 unsigned long mask;
975
976 /* Look up the page on which the object is alloced. If the object
977 wasn't allocated by the collector, we'll probably die. */
74c937ca 978 entry = lookup_page_table_entry (p);
21341cfd
AS
979#ifdef ENABLE_CHECKING
980 if (entry == NULL)
981 abort ();
982#endif
983
984 /* Calculate the index of the object on the page; this is its bit
985 position in the in_use_p bitmap. */
2be510b8 986 bit = (((const char *) p) - entry->page) / OBJECT_SIZE (entry->order);
21341cfd
AS
987 word = bit / HOST_BITS_PER_LONG;
988 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
989
dc297297 990 /* If the bit was previously set, skip it. */
21341cfd
AS
991 if (entry->in_use_p[word] & mask)
992 return 1;
993
994 /* Otherwise set it, and decrement the free object count. */
995 entry->in_use_p[word] |= mask;
996 entry->num_free_objects -= 1;
997
21341cfd
AS
998 if (GGC_DEBUG_LEVEL >= 4)
999 fprintf (G.debug_file, "Marking %p\n", p);
1000
1001 return 0;
1002}
1003
4c160717
RK
1004/* Return 1 if P has been marked, zero otherwise.
1005 P must have been allocated by the GC allocator; it mustn't point to
1006 static objects, stack variables, or memory allocated with malloc. */
1007
1008int
1009ggc_marked_p (p)
1010 const void *p;
1011{
1012 page_entry *entry;
1013 unsigned bit, word;
1014 unsigned long mask;
1015
1016 /* Look up the page on which the object is alloced. If the object
1017 wasn't allocated by the collector, we'll probably die. */
1018 entry = lookup_page_table_entry (p);
1019#ifdef ENABLE_CHECKING
1020 if (entry == NULL)
1021 abort ();
1022#endif
1023
1024 /* Calculate the index of the object on the page; this is its bit
1025 position in the in_use_p bitmap. */
1026 bit = (((const char *) p) - entry->page) / OBJECT_SIZE (entry->order);
1027 word = bit / HOST_BITS_PER_LONG;
1028 mask = (unsigned long) 1 << (bit % HOST_BITS_PER_LONG);
1029
a4b5b2ae 1030 return (entry->in_use_p[word] & mask) != 0;
4c160717
RK
1031}
1032
cb2ec151
RH
1033/* Return the size of the gc-able object P. */
1034
3277221c
MM
1035size_t
1036ggc_get_size (p)
3cce094d 1037 const void *p;
3277221c
MM
1038{
1039 page_entry *pe = lookup_page_table_entry (p);
2be510b8 1040 return OBJECT_SIZE (pe->order);
3277221c 1041}
21341cfd
AS
1042\f
1043/* Initialize the ggc-mmap allocator. */
cb2ec151 1044
21341cfd
AS
1045void
1046init_ggc ()
1047{
2be510b8
MM
1048 unsigned order;
1049
21341cfd
AS
1050 G.pagesize = getpagesize();
1051 G.lg_pagesize = exact_log2 (G.pagesize);
1052
825b6926 1053#ifdef HAVE_MMAP_DEV_ZERO
21341cfd
AS
1054 G.dev_zero_fd = open ("/dev/zero", O_RDONLY);
1055 if (G.dev_zero_fd == -1)
1056 abort ();
1057#endif
1058
1059#if 0
1060 G.debug_file = fopen ("ggc-mmap.debug", "w");
1061#else
1062 G.debug_file = stdout;
1063#endif
1064
a70261ee 1065 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
21341cfd 1066
825b6926 1067#ifdef USING_MMAP
1b3e1423
RH
1068 /* StunOS has an amazing off-by-one error for the first mmap allocation
1069 after fiddling with RLIMIT_STACK. The result, as hard as it is to
1070 believe, is an unaligned page allocation, which would cause us to
1071 hork badly if we tried to use it. */
1072 {
1073 char *p = alloc_anon (NULL, G.pagesize);
825b6926 1074 struct page_entry *e;
1b3e1423
RH
1075 if ((size_t)p & (G.pagesize - 1))
1076 {
1077 /* How losing. Discard this one and try another. If we still
1078 can't get something useful, give up. */
1079
1080 p = alloc_anon (NULL, G.pagesize);
1081 if ((size_t)p & (G.pagesize - 1))
1082 abort ();
1083 }
825b6926 1084
dc297297 1085 /* We have a good page, might as well hold onto it... */
825b6926
ZW
1086 e = (struct page_entry *) xcalloc (1, sizeof (struct page_entry));
1087 e->bytes = G.pagesize;
1088 e->page = p;
1089 e->next = G.free_pages;
1090 G.free_pages = e;
1b3e1423
RH
1091 }
1092#endif
2be510b8
MM
1093
1094 /* Initialize the object size table. */
1095 for (order = 0; order < HOST_BITS_PER_PTR; ++order)
1096 object_size_table[order] = (size_t) 1 << order;
1097 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
b1095f9c
MM
1098 {
1099 size_t s = extra_order_size_table[order - HOST_BITS_PER_PTR];
1100
1101 /* If S is not a multiple of the MAX_ALIGNMENT, then round it up
1102 so that we're sure of getting aligned memory. */
1103 s = CEIL (s, MAX_ALIGNMENT) * MAX_ALIGNMENT;
1104 object_size_table[order] = s;
1105 }
2be510b8
MM
1106
1107 /* Initialize the objects-per-page table. */
1108 for (order = 0; order < NUM_ORDERS; ++order)
1109 {
1110 objects_per_page_table[order] = G.pagesize / OBJECT_SIZE (order);
1111 if (objects_per_page_table[order] == 0)
1112 objects_per_page_table[order] = 1;
1113 }
1114
1115 /* Reset the size_lookup array to put appropriately sized objects in
1116 the special orders. All objects bigger than the previous power
1117 of two, but no greater than the special size, should go in the
1118 new order. */
1119 for (order = HOST_BITS_PER_PTR; order < NUM_ORDERS; ++order)
1120 {
1121 int o;
1122 int i;
1123
1124 o = size_lookup[OBJECT_SIZE (order)];
1125 for (i = OBJECT_SIZE (order); size_lookup [i] == o; --i)
1126 size_lookup[i] = order;
1127 }
21341cfd
AS
1128}
1129
cb2ec151
RH
1130/* Increment the `GC context'. Objects allocated in an outer context
1131 are never freed, eliminating the need to register their roots. */
21341cfd
AS
1132
1133void
1134ggc_push_context ()
1135{
1136 ++G.context_depth;
1137
1138 /* Die on wrap. */
1139 if (G.context_depth == 0)
1140 abort ();
1141}
1142
4934cc53
MM
1143/* Merge the SAVE_IN_USE_P and IN_USE_P arrays in P so that IN_USE_P
1144 reflects reality. Recalculate NUM_FREE_OBJECTS as well. */
1145
1146static void
1147ggc_recalculate_in_use_p (p)
1148 page_entry *p;
1149{
1150 unsigned int i;
1151 size_t num_objects;
1152
1153 /* Because the past-the-end bit in in_use_p is always set, we
1154 pretend there is one additional object. */
1155 num_objects = OBJECTS_PER_PAGE (p->order) + 1;
1156
1157 /* Reset the free object count. */
1158 p->num_free_objects = num_objects;
1159
1160 /* Combine the IN_USE_P and SAVE_IN_USE_P arrays. */
1161 for (i = 0;
2be510b8
MM
1162 i < CEIL (BITMAP_SIZE (num_objects),
1163 sizeof (*p->in_use_p));
4934cc53
MM
1164 ++i)
1165 {
1166 unsigned long j;
1167
1168 /* Something is in use if it is marked, or if it was in use in a
1169 context further down the context stack. */
1170 p->in_use_p[i] |= p->save_in_use_p[i];
1171
1172 /* Decrement the free object count for every object allocated. */
1173 for (j = p->in_use_p[i]; j; j >>= 1)
1174 p->num_free_objects -= (j & 1);
1175 }
1176
1177 if (p->num_free_objects >= num_objects)
1178 abort ();
1179}
1180
cb2ec151
RH
1181/* Decrement the `GC context'. All objects allocated since the
1182 previous ggc_push_context are migrated to the outer context. */
21341cfd
AS
1183
1184void
1185ggc_pop_context ()
1186{
1187 unsigned order, depth;
1188
1189 depth = --G.context_depth;
1190
1191 /* Any remaining pages in the popped context are lowered to the new
1192 current context; i.e. objects allocated in the popped context and
1193 left over are imported into the previous context. */
2be510b8 1194 for (order = 2; order < NUM_ORDERS; order++)
21341cfd 1195 {
21341cfd
AS
1196 page_entry *p;
1197
1198 for (p = G.pages[order]; p != NULL; p = p->next)
1199 {
1200 if (p->context_depth > depth)
4934cc53 1201 p->context_depth = depth;
21341cfd
AS
1202
1203 /* If this page is now in the topmost context, and we'd
1204 saved its allocation state, restore it. */
1205 else if (p->context_depth == depth && p->save_in_use_p)
1206 {
4934cc53 1207 ggc_recalculate_in_use_p (p);
21341cfd
AS
1208 free (p->save_in_use_p);
1209 p->save_in_use_p = 0;
21341cfd
AS
1210 }
1211 }
1212 }
1213}
21341cfd 1214\f
cb2ec151
RH
1215/* Unmark all objects. */
1216
21341cfd
AS
1217static inline void
1218clear_marks ()
1219{
1220 unsigned order;
1221
2be510b8 1222 for (order = 2; order < NUM_ORDERS; order++)
21341cfd
AS
1223 {
1224 size_t num_objects = OBJECTS_PER_PAGE (order);
4934cc53 1225 size_t bitmap_size = BITMAP_SIZE (num_objects + 1);
21341cfd
AS
1226 page_entry *p;
1227
1228 for (p = G.pages[order]; p != NULL; p = p->next)
1229 {
1230#ifdef ENABLE_CHECKING
1231 /* The data should be page-aligned. */
1232 if ((size_t) p->page & (G.pagesize - 1))
1233 abort ();
1234#endif
1235
1236 /* Pages that aren't in the topmost context are not collected;
1237 nevertheless, we need their in-use bit vectors to store GC
1238 marks. So, back them up first. */
4934cc53 1239 if (p->context_depth < G.context_depth)
21341cfd 1240 {
4934cc53
MM
1241 if (! p->save_in_use_p)
1242 p->save_in_use_p = xmalloc (bitmap_size);
21341cfd 1243 memcpy (p->save_in_use_p, p->in_use_p, bitmap_size);
21341cfd
AS
1244 }
1245
1246 /* Reset reset the number of free objects and clear the
1247 in-use bits. These will be adjusted by mark_obj. */
1248 p->num_free_objects = num_objects;
1249 memset (p->in_use_p, 0, bitmap_size);
1250
1251 /* Make sure the one-past-the-end bit is always set. */
1252 p->in_use_p[num_objects / HOST_BITS_PER_LONG]
1253 = ((unsigned long) 1 << (num_objects % HOST_BITS_PER_LONG));
1254 }
1255 }
1256}
1257
cb2ec151
RH
1258/* Free all empty pages. Partially empty pages need no attention
1259 because the `mark' bit doubles as an `unused' bit. */
1260
21341cfd
AS
1261static inline void
1262sweep_pages ()
1263{
1264 unsigned order;
1265
2be510b8 1266 for (order = 2; order < NUM_ORDERS; order++)
21341cfd
AS
1267 {
1268 /* The last page-entry to consider, regardless of entries
1269 placed at the end of the list. */
1270 page_entry * const last = G.page_tails[order];
1271
1272 size_t num_objects = OBJECTS_PER_PAGE (order);
054f5e69 1273 size_t live_objects;
21341cfd
AS
1274 page_entry *p, *previous;
1275 int done;
1276
1277 p = G.pages[order];
1278 if (p == NULL)
1279 continue;
1280
1281 previous = NULL;
1282 do
1283 {
1284 page_entry *next = p->next;
1285
1286 /* Loop until all entries have been examined. */
1287 done = (p == last);
1288
054f5e69
ZW
1289 /* Add all live objects on this page to the count of
1290 allocated memory. */
1291 live_objects = num_objects - p->num_free_objects;
1292
2be510b8 1293 G.allocated += OBJECT_SIZE (order) * live_objects;
054f5e69 1294
21341cfd
AS
1295 /* Only objects on pages in the topmost context should get
1296 collected. */
1297 if (p->context_depth < G.context_depth)
1298 ;
1299
1300 /* Remove the page if it's empty. */
054f5e69 1301 else if (live_objects == 0)
21341cfd
AS
1302 {
1303 if (! previous)
1304 G.pages[order] = next;
1305 else
1306 previous->next = next;
1307
1308 /* Are we removing the last element? */
1309 if (p == G.page_tails[order])
1310 G.page_tails[order] = previous;
1311 free_page (p);
1312 p = previous;
1313 }
1314
1315 /* If the page is full, move it to the end. */
1316 else if (p->num_free_objects == 0)
1317 {
1318 /* Don't move it if it's already at the end. */
1319 if (p != G.page_tails[order])
1320 {
1321 /* Move p to the end of the list. */
1322 p->next = NULL;
1323 G.page_tails[order]->next = p;
1324
1325 /* Update the tail pointer... */
1326 G.page_tails[order] = p;
1327
1328 /* ... and the head pointer, if necessary. */
1329 if (! previous)
1330 G.pages[order] = next;
1331 else
1332 previous->next = next;
1333 p = previous;
1334 }
1335 }
1336
1337 /* If we've fallen through to here, it's a page in the
1338 topmost context that is neither full nor empty. Such a
1339 page must precede pages at lesser context depth in the
1340 list, so move it to the head. */
1341 else if (p != G.pages[order])
1342 {
1343 previous->next = p->next;
1344 p->next = G.pages[order];
1345 G.pages[order] = p;
1346 /* Are we moving the last element? */
1347 if (G.page_tails[order] == p)
1348 G.page_tails[order] = previous;
1349 p = previous;
1350 }
1351
1352 previous = p;
1353 p = next;
1354 }
1355 while (! done);
4934cc53
MM
1356
1357 /* Now, restore the in_use_p vectors for any pages from contexts
1358 other than the current one. */
1359 for (p = G.pages[order]; p; p = p->next)
1360 if (p->context_depth != G.context_depth)
1361 ggc_recalculate_in_use_p (p);
21341cfd
AS
1362 }
1363}
1364
1365#ifdef GGC_POISON
cb2ec151
RH
1366/* Clobber all free objects. */
1367
21341cfd
AS
1368static inline void
1369poison_pages ()
1370{
1371 unsigned order;
1372
2be510b8 1373 for (order = 2; order < NUM_ORDERS; order++)
21341cfd
AS
1374 {
1375 size_t num_objects = OBJECTS_PER_PAGE (order);
2be510b8 1376 size_t size = OBJECT_SIZE (order);
21341cfd
AS
1377 page_entry *p;
1378
1379 for (p = G.pages[order]; p != NULL; p = p->next)
1380 {
1381 size_t i;
c831fdea
MM
1382
1383 if (p->context_depth != G.context_depth)
1384 /* Since we don't do any collection for pages in pushed
1385 contexts, there's no need to do any poisoning. And
1386 besides, the IN_USE_P array isn't valid until we pop
1387 contexts. */
1388 continue;
1389
21341cfd
AS
1390 for (i = 0; i < num_objects; i++)
1391 {
1392 size_t word, bit;
1393 word = i / HOST_BITS_PER_LONG;
1394 bit = i % HOST_BITS_PER_LONG;
1395 if (((p->in_use_p[word] >> bit) & 1) == 0)
cb2ec151 1396 memset (p->page + i * size, 0xa5, size);
21341cfd
AS
1397 }
1398 }
1399 }
1400}
1401#endif
1402
cb2ec151
RH
1403/* Top level mark-and-sweep routine. */
1404
21341cfd
AS
1405void
1406ggc_collect ()
1407{
21341cfd
AS
1408 /* Avoid frequent unnecessary work by skipping collection if the
1409 total allocations haven't expanded much since the last
1410 collection. */
1411#ifndef GGC_ALWAYS_COLLECT
1412 if (G.allocated < GGC_MIN_EXPAND_FOR_GC * G.allocated_last_gc)
1413 return;
1414#endif
1415
2a9a326b 1416 timevar_push (TV_GC);
21341cfd 1417 if (!quiet_flag)
b9bfacf0 1418 fprintf (stderr, " {GC %luk -> ", (unsigned long) G.allocated / 1024);
21341cfd 1419
054f5e69
ZW
1420 /* Zero the total allocated bytes. This will be recalculated in the
1421 sweep phase. */
21341cfd
AS
1422 G.allocated = 0;
1423
1424 /* Release the pages we freed the last time we collected, but didn't
1425 reuse in the interim. */
1426 release_pages ();
1427
1428 clear_marks ();
1429 ggc_mark_roots ();
21341cfd
AS
1430
1431#ifdef GGC_POISON
1432 poison_pages ();
1433#endif
1434
cb2ec151
RH
1435 sweep_pages ();
1436
21341cfd 1437 G.allocated_last_gc = G.allocated;
a70261ee
RH
1438 if (G.allocated_last_gc < GGC_MIN_LAST_ALLOCATED)
1439 G.allocated_last_gc = GGC_MIN_LAST_ALLOCATED;
21341cfd 1440
2a9a326b 1441 timevar_pop (TV_GC);
21341cfd 1442
21341cfd 1443 if (!quiet_flag)
2a9a326b 1444 fprintf (stderr, "%luk}", (unsigned long) G.allocated / 1024);
21341cfd 1445}
3277221c
MM
1446
1447/* Print allocation statistics. */
fba0bfd4
ZW
1448#define SCALE(x) ((unsigned long) ((x) < 1024*10 \
1449 ? (x) \
1450 : ((x) < 1024*1024*10 \
1451 ? (x) / 1024 \
1452 : (x) / (1024*1024))))
1453#define LABEL(x) ((x) < 1024*10 ? ' ' : ((x) < 1024*1024*10 ? 'k' : 'M'))
3277221c
MM
1454
1455void
fba0bfd4 1456ggc_print_statistics ()
3277221c
MM
1457{
1458 struct ggc_statistics stats;
4934cc53 1459 unsigned int i;
fba0bfd4 1460 size_t total_overhead = 0;
3277221c
MM
1461
1462 /* Clear the statistics. */
d219c7f1 1463 memset (&stats, 0, sizeof (stats));
3277221c
MM
1464
1465 /* Make sure collection will really occur. */
1466 G.allocated_last_gc = 0;
1467
1468 /* Collect and print the statistics common across collectors. */
fba0bfd4 1469 ggc_print_common_statistics (stderr, &stats);
3277221c 1470
4934cc53
MM
1471 /* Release free pages so that we will not count the bytes allocated
1472 there as part of the total allocated memory. */
1473 release_pages ();
1474
3277221c
MM
1475 /* Collect some information about the various sizes of
1476 allocation. */
fba0bfd4 1477 fprintf (stderr, "\n%-5s %10s %10s %10s\n",
9fd51e67 1478 "Size", "Allocated", "Used", "Overhead");
2be510b8 1479 for (i = 0; i < NUM_ORDERS; ++i)
3277221c
MM
1480 {
1481 page_entry *p;
1482 size_t allocated;
1483 size_t in_use;
fba0bfd4 1484 size_t overhead;
3277221c
MM
1485
1486 /* Skip empty entries. */
1487 if (!G.pages[i])
1488 continue;
1489
fba0bfd4 1490 overhead = allocated = in_use = 0;
3277221c
MM
1491
1492 /* Figure out the total number of bytes allocated for objects of
fba0bfd4
ZW
1493 this size, and how many of them are actually in use. Also figure
1494 out how much memory the page table is using. */
3277221c
MM
1495 for (p = G.pages[i]; p; p = p->next)
1496 {
1497 allocated += p->bytes;
1498 in_use +=
2be510b8 1499 (OBJECTS_PER_PAGE (i) - p->num_free_objects) * OBJECT_SIZE (i);
fba0bfd4
ZW
1500
1501 overhead += (sizeof (page_entry) - sizeof (long)
1502 + BITMAP_SIZE (OBJECTS_PER_PAGE (i) + 1));
3277221c 1503 }
8a951190
AJ
1504 fprintf (stderr, "%-5lu %10lu%c %10lu%c %10lu%c\n",
1505 (unsigned long) OBJECT_SIZE (i),
fba0bfd4
ZW
1506 SCALE (allocated), LABEL (allocated),
1507 SCALE (in_use), LABEL (in_use),
1508 SCALE (overhead), LABEL (overhead));
1509 total_overhead += overhead;
3277221c 1510 }
8a951190 1511 fprintf (stderr, "%-5s %10lu%c %10lu%c %10lu%c\n", "Total",
fba0bfd4
ZW
1512 SCALE (G.bytes_mapped), LABEL (G.bytes_mapped),
1513 SCALE (G.allocated), LABEL(G.allocated),
1514 SCALE (total_overhead), LABEL (total_overhead));
3277221c 1515}
This page took 0.830467 seconds and 5 git commands to generate.