mirror of
https://github.com/adtools/clib2.git
synced 2025-12-08 14:59:05 +00:00
- In libunix.a malloc(), calloc() and realloc() no longer treat a
request to allocate 0 bytes as an error, returning NULL. They all return a 1 byte memory chunk initialized to 0 instead. git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@15064 87f5fb63-7c3d-0410-a384-fd976d0f7a62
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: stdlib_realloc.c,v 1.6 2005-03-18 12:38:24 obarthel Exp $
|
||||
* $Id: stdlib_realloc.c,v 1.7 2005-11-20 17:00:22 obarthel Exp $
|
||||
*
|
||||
* :ts=4
|
||||
*
|
||||
@ -65,18 +65,20 @@ __realloc(void *ptr,size_t size,const char * file,int line)
|
||||
|
||||
result = __malloc(size,file,line);
|
||||
}
|
||||
#ifndef UNIX_PATH_SEMANTICS
|
||||
else if (size == 0)
|
||||
{
|
||||
D(("calling free(0x%08lx)",ptr));
|
||||
|
||||
__free(ptr,file,line);
|
||||
}
|
||||
#endif /* UNIX_PATH_SEMANTICS */
|
||||
else
|
||||
{
|
||||
struct MemoryNode * mn;
|
||||
BOOL reallocate;
|
||||
|
||||
assert( ptr != NULL && size > 0 );
|
||||
assert( ptr != NULL );
|
||||
|
||||
/* Try to find the allocation in the list. */
|
||||
mn = __find_memory_node(ptr);
|
||||
@ -84,8 +86,7 @@ __realloc(void *ptr,size_t size,const char * file,int line)
|
||||
#ifdef __MEM_DEBUG
|
||||
{
|
||||
/* If we managed to find the memory allocation,
|
||||
* reallocate it.
|
||||
*/
|
||||
reallocate it. */
|
||||
if(mn == NULL)
|
||||
{
|
||||
SHOWMSG("allocation not found");
|
||||
@ -93,9 +94,7 @@ __realloc(void *ptr,size_t size,const char * file,int line)
|
||||
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.
|
||||
*/
|
||||
__set_errno(ENOMEM);
|
||||
reallocation. */
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
@ -108,14 +107,11 @@ __realloc(void *ptr,size_t size,const char * file,int line)
|
||||
if(mn == NULL || mn->mn_NeverFree)
|
||||
{
|
||||
SHOWMSG("cannot free this chunk");
|
||||
|
||||
__set_errno(ENOMEM);
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Don't do anything unless the size of the allocation has really
|
||||
* changed.
|
||||
*/
|
||||
/* Don't do anything unless the size of the allocation
|
||||
has really changed. */
|
||||
#if defined(__MEM_DEBUG)
|
||||
{
|
||||
reallocate = (mn->mn_Size != size);
|
||||
@ -132,19 +128,17 @@ __realloc(void *ptr,size_t size,const char * file,int line)
|
||||
size_t rounded_allocation_size;
|
||||
|
||||
/* Round the total allocation size to the operating system
|
||||
* granularity.
|
||||
*/
|
||||
granularity. */
|
||||
rounded_allocation_size = __get_allocation_size(size);
|
||||
|
||||
assert( rounded_allocation_size >= size );
|
||||
|
||||
/* 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
|
||||
* allocation. We also take into account that the
|
||||
* actual size of the allocation is affected by a
|
||||
* certain operating system imposed granularity.
|
||||
*/
|
||||
original allocation size, do not reallocate the
|
||||
block and do not copy over the contents of the old
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -162,8 +156,6 @@ __realloc(void *ptr,size_t size,const char * file,int line)
|
||||
if(new_ptr == NULL)
|
||||
{
|
||||
SHOWMSG("could not reallocate memory");
|
||||
|
||||
__set_errno(ENOMEM);
|
||||
goto out;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user