[Bug libc/5071] New: realloc(): do not remap() if same number of pages
tomash dot brechko at gmail dot com
sourceware-bugzilla@sourceware.org
Wed Sep 26 09:42:00 GMT 2007
strace of the following program on Linux x86 (4096 bytes/page) shows that there
are lots of redundant calls to mremap() (old size and new size are equal). The
bug is present in glibc up to and including 2.6.1 (bug report Version field
doesn't have 2.6).
#include <stdlib.h>
int
main()
{
const size_t size = 256 * 1024 + 4050;
void *p;
int i;
p = malloc(size);
for (i = 0; i < 100; ++i)
p = realloc(p, size + i);
return 0;
}
The following patch fixes the problem (comment line is copied from the similar
check in _int_realloc()):
--- malloc/malloc.c-orig 2007-09-26 13:28:36.000000000 +0400
+++ malloc/malloc.c 2007-09-26 12:57:41.000000000 +0400
@@ -3503,6 +3503,10 @@ mremap_chunk(p, new_size) mchunkptr p; s
/* Note the extra SIZE_SZ overhead as in mmap_chunk(). */
new_size = (new_size + offset + SIZE_SZ + page_mask) & ~page_mask;
+ /* don't need to remap if still within same page */
+ if (size + offset == new_size)
+ return p;
+
cp = (char *)mremap((char *)p - offset, size + offset, new_size,
MREMAP_MAYMOVE);
--
Summary: realloc(): do not remap() if same number of pages
Product: glibc
Version: 2.4
Status: NEW
Severity: minor
Priority: P3
Component: libc
AssignedTo: drepper at redhat dot com
ReportedBy: tomash dot brechko at gmail dot com
CC: glibc-bugs at sources dot redhat dot com
GCC build triplet: i686-redhat-linux-gnu
GCC host triplet: i686-redhat-linux-gnu
GCC target triplet: i686-redhat-linux-gnu
http://sourceware.org/bugzilla/show_bug.cgi?id=5071
------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.
More information about the Glibc-bugs
mailing list