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>
This commit is contained in:
Keith Packard via Newlib 2020-08-13 17:19:02 -07:00 committed by Corinna Vinschen
parent ce4044adee
commit 8a7ec55c53
1 changed files with 3 additions and 3 deletions

View File

@ -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);
}