From ac710b333e9c8f49874c83668c4dfc8fa747b0f4 Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Thu, 24 Nov 2016 09:45:35 +0100 Subject: [PATCH] Accidentally omitted from version 1.211 --- library/stdlib_realloc.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/library/stdlib_realloc.c b/library/stdlib_realloc.c index beb633b..4461ad7 100644 --- a/library/stdlib_realloc.c +++ b/library/stdlib_realloc.c @@ -76,6 +76,7 @@ __realloc(void *ptr,size_t size,const char * file,int line) #endif /* UNIX_PATH_SEMANTICS */ else { + size_t old_size; struct MemoryNode * mn; BOOL reallocate; @@ -108,29 +109,23 @@ __realloc(void *ptr,size_t size,const char * file,int line) } #endif /* __MEM_DEBUG */ - if(mn == NULL || mn->mn_NeverFree) + if(mn == NULL || FLAG_IS_SET(mn->mn_Size, MN_SIZE_NEVERFREE)) { SHOWMSG("cannot free this chunk"); goto out; } + old_size = GET_MN_SIZE(mn); + /* Don't do anything unless the size of the allocation has really changed. */ #if defined(__MEM_DEBUG) { - reallocate = (mn->mn_Size != size); + reallocate = (old_size != size); } #else { - size_t rounded_allocation_size; - - /* Round the total allocation size to the operating system - granularity. */ - rounded_allocation_size = __get_allocation_size(size); - - assert( rounded_allocation_size >= size ); - - if(rounded_allocation_size > mn->mn_Size) + if(size > old_size) { /* Allocation size should grow. */ reallocate = TRUE; @@ -143,7 +138,7 @@ __realloc(void *ptr,size_t size,const char * file,int line) allocation. We also take into account that the actual size of the allocation is affected by a certain operating system imposed granularity. */ - reallocate = (rounded_allocation_size < mn->mn_Size && rounded_allocation_size <= mn->mn_Size / 2); + reallocate = (size < old_size && size <= old_size / 2); } } #endif /* __MEM_DEBUG */ @@ -152,7 +147,7 @@ __realloc(void *ptr,size_t size,const char * file,int line) { void * new_ptr; - D(("realloc() size has changed; old=%ld, new=%ld",mn->mn_Size,size)); + D(("realloc() size has changed; old=%ld, new=%ld",old_size,size)); /* We allocate the new memory chunk before we attempt to replace the old. */ @@ -164,8 +159,8 @@ __realloc(void *ptr,size_t size,const char * file,int line) } /* Copy the contents of the old allocation to the new buffer. */ - if(size > mn->mn_Size) - size = mn->mn_Size; + if(size > old_size) + size = old_size; memmove(new_ptr,ptr,size); @@ -177,7 +172,7 @@ __realloc(void *ptr,size_t size,const char * file,int line) } else { - D(("size didn't actually change that much (%ld -> %ld); returning memory block as is.",mn->mn_Size,size)); + D(("size didn't actually change that much (%ld -> %ld); returning memory block as is.",old_size,size)); /* No change in size. */ result = ptr;