[PATCH 3/4] libm/stdlib: Realloc when shrinking by 2* or more
Corinna Vinschen
vinschen@redhat.com
Wed Aug 12 08:08:29 GMT 2020
On Aug 11 16:05, Keith Packard via Newlib wrote:
> This reduces memory usage when reallocating objects much smaller.
>
> Signed-off-by: Keith Packard <keithp@keithp.com>
> ---
> newlib/libc/stdlib/nano-mallocr.c | 4 +---
> 1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c
> index cef23977e..83234c618 100644
> --- a/newlib/libc/stdlib/nano-mallocr.c
> +++ b/newlib/libc/stdlib/nano-mallocr.c
> @@ -476,10 +476,8 @@ 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);
> --
> 2.28.0
Ok, so this patch introduces the need for the conditional I complained
about in the previous patch. IMO, the conditional should be moved
into this patch to introduce it together with the change it requires.
Thanks,
Corinna
--
Corinna Vinschen
Cygwin Maintainer
Red Hat
More information about the Newlib
mailing list