1
0
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:
Olaf Barthel
2005-11-20 17:00:22 +00:00
parent fe29857567
commit 0459ea1fc9
7 changed files with 59 additions and 87 deletions

View File

@ -1,5 +1,5 @@
/*
* $Id: stdlib_malloc.c,v 1.14 2005-10-27 08:58:41 obarthel Exp $
* $Id: stdlib_malloc.c,v 1.15 2005-11-20 17:00:22 obarthel Exp $
*
* :ts=4
*
@ -99,16 +99,28 @@ __allocate_memory(size_t size,BOOL never_free,const char * UNUSED unused_file,in
struct MemoryNode * mn;
size_t allocation_size;
void * result = NULL;
size_t original_size;
assert( size > 0 );
#if defined(UNIX_PATH_SEMANTICS)
{
original_size = size;
/* The libunix.a flavour accepts zero length memory allocations
and quietly turns them into 1 byte allocations. */
if(size == 0)
size++;
}
#endif /* UNIX_PATH_SEMANTICS */
__memory_lock();
/* Zero length allocations are by default rejected. */
if(size == 0)
goto out;
if(__free_memory_threshold > 0 && AvailMem(MEMF_ANY|MEMF_LARGEST) < __free_memory_threshold)
{
SHOWMSG("not enough free memory available to safely proceed with allocation");
__set_errno(ENOMEM);
goto out;
}
@ -137,8 +149,6 @@ __allocate_memory(size_t size,BOOL never_free,const char * UNUSED unused_file,in
if(mn == NULL)
{
SHOWMSG("not enough memory");
__set_errno(ENOMEM);
goto out;
}
@ -195,6 +205,16 @@ __allocate_memory(size_t size,BOOL never_free,const char * UNUSED unused_file,in
}
#endif /* __MEM_DEBUG */
#if defined(UNIX_PATH_SEMANTICS)
{
/* Zero length memory allocations in libunix.a quietly
return one byte of memory each, and that byte is
set to '\0'. */
if(original_size == 0)
*(char *)result = '\0';
}
#endif /* UNIX_PATH_SEMANTICS */
assert( (((ULONG)result) & 3) == 0 );
out:
@ -232,29 +252,9 @@ __malloc(size_t size,const char * file,int line)
}
#endif /* __MEM_DEBUG */
if(size == 0)
{
#ifdef __MEM_DEBUG
{
kprintf("[%s] ",__program_name);
if(file != NULL)
kprintf("%s:%ld:",file,line);
kprintf("malloc(0) called.\n");
}
#endif /* __MEM_DEBUG */
goto out;
}
assert( (int)size > 0 );
/* Allocate memory which can be put through realloc() and free(). */
result = __allocate_memory(size,FALSE,file,line);
out:
return(result);
}