Updated to properly use the MemoryNode changes.

This commit is contained in:
obarthel
2023-09-08 16:05:29 +02:00
parent b9463f442b
commit 842acf2eaa
+26 -21
View File
@@ -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;
}
/****************************************************************************/