From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 234E83858D37; Mon, 17 Aug 2020 09:45:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 234E83858D37 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: newlib-cvs@sourceware.org Subject: [newlib-cygwin] libm/stdlib: Realloc when shrinking by 2* or more X-Act-Checkin: newlib-cygwin X-Git-Author: Keith Packard via Newlib X-Git-Refname: refs/heads/master X-Git-Oldrev: ce4044adeebfdc60714d3a35f67ba536edb55612 X-Git-Newrev: 8a7ec55c535cddd74d45a2f6fb644ecded114de8 Message-Id: <20200817094510.234E83858D37@sourceware.org> Date: Mon, 17 Aug 2020 09:45:10 +0000 (GMT) X-BeenThere: newlib-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib GIT logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Aug 2020 09:45:10 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=8a7ec55c535cddd74d45a2f6fb644ecded114de8 commit 8a7ec55c535cddd74d45a2f6fb644ecded114de8 Author: Keith Packard via Newlib Date: Thu Aug 13 17:19:02 2020 -0700 libm/stdlib: Realloc when shrinking by 2* or more This reduces memory usage when reallocating objects much smaller. Signed-off-by: Keith Packard Diff: --- newlib/libc/stdlib/nano-mallocr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c index 3970753a9..6ba0eb74b 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); }