From b9bd6f743843a53de1396c43d1e308e51d40c5c4 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 17 Oct 2011 10:00:07 +0000 Subject: Reduce memory waste due to non-power-of-2 allocs This patch basically arranges for the allocation size of line_map buffers to be as close as possible to a power of two. This *significantly* decreases peak memory consumption as (macro) maps are numerous and stay live during all the compilation. The patch adds a new ggc_round_alloc_size interface to the ggc allocator. In each of the two main allocator implementations ('page' and 'zone') the function has been extracted from the main allocation function code and returns the actual size of the allocated memory region, thus giving a chance to the caller to maximize the amount of memory it actually uses from the allocated memory region. In the 'none' allocator implementation (that uses xmalloc) the ggc_round_alloc_size just returns the requested allocation size. Co-Authored-By: Dodji Seketeli From-SVN: r180086 --- gcc/ggc-zone.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'gcc/ggc-zone.c') diff --git a/gcc/ggc-zone.c b/gcc/ggc-zone.c index d0c1d79..79c8c03 100644 --- a/gcc/ggc-zone.c +++ b/gcc/ggc-zone.c @@ -1073,6 +1073,24 @@ free_chunk (char *ptr, size_t size, struct alloc_zone *zone) fprintf (G.debug_file, "Deallocating object, chunk=%p\n", (void *)chunk); } +/* For a given size of memory requested for allocation, return the + actual size that is going to be allocated. */ + +size_t +ggc_round_alloc_size (size_t requested_size) +{ + size_t size; + + /* Make sure that zero-sized allocations get a unique and freeable + pointer. */ + if (requested_size == 0) + size = MAX_ALIGNMENT; + else + size = (requested_size + MAX_ALIGNMENT - 1) & -MAX_ALIGNMENT; + + return size; +} + /* Allocate a chunk of memory of at least ORIG_SIZE bytes, in ZONE. */ void * @@ -1084,14 +1102,7 @@ ggc_internal_alloc_zone_stat (size_t orig_size, struct alloc_zone *zone struct small_page_entry *entry; struct alloc_chunk *chunk, **pp; void *result; - size_t size = orig_size; - - /* Make sure that zero-sized allocations get a unique and freeable - pointer. */ - if (size == 0) - size = MAX_ALIGNMENT; - else - size = (size + MAX_ALIGNMENT - 1) & -MAX_ALIGNMENT; + size_t size = ggc_alloced_size_for_request (orig_size); /* Try to allocate the object from several different sources. Each of these cases is responsible for setting RESULT and SIZE to -- cgit v1.1