summaryrefslogtreecommitdiff
path: root/libcpp/line-map.c
diff options
context:
space:
mode:
authordodji <dodji@138bc75d-0d04-0410-961f-82ee72b054a4>2011-10-17 10:00:07 +0000
committerdodji <dodji@138bc75d-0d04-0410-961f-82ee72b054a4>2011-10-17 10:00:07 +0000
commit1ae3520e0d4d2a5693961919636474de0a4faef6 (patch)
tree0b0e309c095d0459230339edc0343cafe8f0c4c8 /libcpp/line-map.c
parente77b8253aa187580c9e185461ed28c207e693126 (diff)
downloadgcc-1ae3520e0d4d2a5693961919636474de0a4faef6.tar.gz
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. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@180086 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libcpp/line-map.c')
-rw-r--r--libcpp/line-map.c39
1 files changed, 33 insertions, 6 deletions
diff --git a/libcpp/line-map.c b/libcpp/line-map.c
index 9086b3e3065..87b8bfe8a0f 100644
--- a/libcpp/line-map.c
+++ b/libcpp/line-map.c
@@ -92,16 +92,43 @@ new_linemap (struct line_maps *set,
if (LINEMAPS_USED (set, macro_map_p) == LINEMAPS_ALLOCATED (set, macro_map_p))
{
/* We ran out of allocated line maps. Let's allocate more. */
+ unsigned alloc_size;
line_map_realloc reallocator
= set->reallocator ? set->reallocator : xrealloc;
+ line_map_round_alloc_size_func round_alloc_size =
+ set->round_alloc_size;
+
+ /* We are going to execute some dance to try to reduce the
+ overhead of the memory allocator, in case we are using the
+ ggc-page.c one.
+
+ The actual size of memory we are going to get back from the
+ allocator is the smallest power of 2 that is greater than the
+ size we requested. So let's consider that size then. */
+
+ alloc_size =
+ (2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256)
+ * sizeof (struct line_map);
+
+ /* Get the actual size of memory that is going to be allocated
+ by the allocator. */
+ alloc_size = round_alloc_size (alloc_size);
+
+ /* Now alloc_size contains the exact memory size we would get if
+ we have asked for the initial alloc_size amount of memory.
+ Let's get back to the number of macro map that amounts
+ to. */
LINEMAPS_ALLOCATED (set, macro_map_p) =
- 2 * LINEMAPS_ALLOCATED (set, macro_map_p) + 256;
- LINEMAPS_MAPS (set, macro_map_p)
- = (struct line_map *) (*reallocator) (LINEMAPS_MAPS (set, macro_map_p),
- LINEMAPS_ALLOCATED (set,
- macro_map_p)
- * sizeof (struct line_map));
+ alloc_size / (sizeof (struct line_map));
+
+ /* And now let's really do the re-allocation. */
+ LINEMAPS_MAPS (set, macro_map_p) =
+ (struct line_map *) (*reallocator)
+ (LINEMAPS_MAPS (set, macro_map_p),
+ (LINEMAPS_ALLOCATED (set, macro_map_p)
+ * sizeof (struct line_map)));
+
result =
&LINEMAPS_MAPS (set, macro_map_p)[LINEMAPS_USED (set, macro_map_p)];
memset (result, 0,