mirror of
git://sourceware.org/git/newlib-cygwin.git
synced 2025-02-15 05:29:10 +08:00
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:
parent
fd43311e52
commit
b53ce64154
@ -476,15 +476,15 @@ void * nano_realloc(RARG void * ptr, malloc_size_t size)
|
|||||||
return NULL;
|
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);
|
old_size = nano_malloc_usable_size(RCALL ptr);
|
||||||
if (old_size >= size)
|
if (size <= old_size && (old_size >> 1) < size)
|
||||||
return ptr;
|
return ptr;
|
||||||
|
|
||||||
mem = nano_malloc(RCALL size);
|
mem = nano_malloc(RCALL size);
|
||||||
if (mem != NULL)
|
if (mem != NULL)
|
||||||
{
|
{
|
||||||
|
if (old_size > size)
|
||||||
|
old_size = size;
|
||||||
memcpy(mem, ptr, old_size);
|
memcpy(mem, ptr, old_size);
|
||||||
nano_free(RCALL ptr);
|
nano_free(RCALL ptr);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user