From 842acf2eaa9716c7107e60ca4a9c43a185f41bbf Mon Sep 17 00:00:00 2001 From: obarthel Date: Fri, 8 Sep 2023 16:05:29 +0200 Subject: [PATCH] Updated to properly use the MemoryNode changes. --- library/stdlib_realloc.c | 47 ++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/library/stdlib_realloc.c b/library/stdlib_realloc.c index 137ed8a..050ad31 100644 --- a/library/stdlib_realloc.c +++ b/library/stdlib_realloc.c @@ -50,7 +50,7 @@ /****************************************************************************/ __static void * -__realloc(void *ptr,size_t size,const char * file,int line) +__realloc(void *ptr, size_t size, const char * file, int line) { void * result = NULL; BOOL locked = FALSE; @@ -62,18 +62,18 @@ __realloc(void *ptr,size_t size,const char * file,int line) assert( (int)size >= 0 ); - if(ptr == NULL) + if (ptr == NULL) { - D(("calling malloc(%ld)",size)); + D(("calling malloc(%ld)", size)); - result = __malloc(size,file,line); + result = __malloc(size, file, line); } #ifndef UNIX_PATH_SEMANTICS else if (size == 0) { D(("calling free(0x%08lx)",ptr)); - __free(ptr,file,line); + __free(ptr, file, line); } #endif /* UNIX_PATH_SEMANTICS */ else @@ -94,11 +94,12 @@ __realloc(void *ptr,size_t size,const char * file,int line) { /* If we managed to find the memory allocation, reallocate it. */ - if(mn == NULL) + if (mn == NULL) { SHOWMSG("allocation not found"); - kprintf("[%s] %s:%ld:Address for realloc(0x%08lx,%ld) not known.\n",__program_name,file,line,ptr,size); + kprintf("[%s] %s:%ld:Address for realloc(0x%08lx,%ld) not known.\n", + __program_name, file, line, ptr, size); /* Apparently, the address did not qualify for reallocation. */ @@ -111,13 +112,15 @@ __realloc(void *ptr,size_t size,const char * file,int line) } #endif /* __MEM_DEBUG */ - if(mn == NULL || FLAG_IS_SET(mn->mn_Size, MN_SIZE_NEVERFREE)) + if (mn == NULL || FLAG_IS_SET(mn->mn_Flags, MNF_NEVER_FREE)) { SHOWMSG("cannot free this chunk"); goto out; } - old_size = GET_MN_SIZE(mn); + assert( FLAG_IS_CLEAR(mn->mn_Flags, MNF_NEVER_FREE) ); + + old_size = mn->mn_AllocationSize; /* Don't do anything unless the size of the allocation has really changed. */ @@ -127,13 +130,14 @@ __realloc(void *ptr,size_t size,const char * file,int line) } #else { - if(size > old_size) + if (size > old_size) { /* Allocation size should grow. */ reallocate = TRUE; } else { + /* Optimization: If the block size shrinks by less than half the original allocation size, do not reallocate the block and do not copy over the contents of the old @@ -145,36 +149,37 @@ __realloc(void *ptr,size_t size,const char * file,int line) } #endif /* __MEM_DEBUG */ - if(reallocate) + if (reallocate) { void * new_ptr; - D(("realloc() size has changed; old=%ld, new=%ld",old_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. */ - new_ptr = __malloc(size,file,line); - if(new_ptr == NULL) + new_ptr = __malloc(size, file, line); + if (new_ptr == NULL) { SHOWMSG("could not reallocate memory"); goto out; } /* Copy the contents of the old allocation to the new buffer. */ - if(size > old_size) + if (size > old_size) size = old_size; - memmove(new_ptr,ptr,size); + memmove(new_ptr, ptr, size); /* Free the old allocation. Since we already know which memory node is associated with it, we don't call __free() here. */ - __free_memory_node(mn,file,line); + __free_memory_node(mn, file, line); result = new_ptr; } else { - D(("size didn't actually change that much (%ld -> %ld); returning memory block as is.",old_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; @@ -183,14 +188,14 @@ __realloc(void *ptr,size_t size,const char * file,int line) out: - if(locked) + if (locked) __memory_unlock(); - if(result == NULL) + if (result == NULL) SHOWMSG("ouch! realloc failed"); RETURN(result); - return(result); + return result; } /****************************************************************************/