aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Packard via Newlib <newlib@sourceware.org>2020-08-13 17:19:02 -0700
committerCorinna Vinschen <corinna@vinschen.de>2020-08-17 11:43:55 +0200
commit8a7ec55c535cddd74d45a2f6fb644ecded114de8 (patch)
treea174c521143b21a82f2c8274e14601fde41a0b66
parentce4044adeebfdc60714d3a35f67ba536edb55612 (diff)
downloadnewlib-8a7ec55c535cddd74d45a2f6fb644ecded114de8.zip
newlib-8a7ec55c535cddd74d45a2f6fb644ecded114de8.tar.gz
newlib-8a7ec55c535cddd74d45a2f6fb644ecded114de8.tar.bz2
libm/stdlib: Realloc when shrinking by 2* or more
This reduces memory usage when reallocating objects much smaller. Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r--newlib/libc/stdlib/nano-mallocr.c6
1 files changed, 3 insertions, 3 deletions
diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c
index 3970753..6ba0eb7 100644
--- a/newlib/libc/stdlib/nano-mallocr.c
+++ b/newlib/libc/stdlib/nano-mallocr.c
@@ -476,15 +476,15 @@ void * nano_realloc(RARG void * ptr, malloc_size_t size)
return NULL;
}
- /* TODO: There is chance to shrink the chunk if newly requested
- * size is much small */
old_size = nano_malloc_usable_size(RCALL ptr);
- if (old_size >= size)
+ if (size <= old_size && (old_size >> 1) < size)
return ptr;
mem = nano_malloc(RCALL size);
if (mem != NULL)
{
+ if (old_size > size)
+ old_size = size;
memcpy(mem, ptr, old_size);
nano_free(RCALL ptr);
}