[sheepdog] Question about memory allocation functions in lib/util.c

Hitoshi Mitake h.mitake at gmail.com
Mon Oct 8 11:00:08 CEST 2012

On Wed, Oct 3, 2012 at 11:43 AM, MORITA Kazutaka
<morita.kazutaka at lab.ntt.co.jp> wrote:
> At Mon, 1 Oct 2012 22:46:34 +0900,
> Hitoshi Mitake wrote:
>> Hi,
>> I have a question about the memory allocation functions in lib/util.c
>> of sheepdog.
>> It seems that xmalloc(), xcalloc(), and xrealloc() try to allocate 1
>> byte area if first allocation fails
>> and the size of requested area is 0.
>> e.g. this is the head part of of xmalloc()
>> void *xmalloc(size_t size)
>> {
>>       void *ret = malloc(size);
>>       if (!ret && !size)
>>               ret = malloc(1);
>> ...
>> I couldn't understand the intention of this implementation.
>> What is the intention of these behaviours?
> It is necessary to handle the case where malloc(0) returns NULL.  Note
> that the result of malloc(0) is implementation-defined.  IIRC, Yuan
> borrows the code from another project and would know better.
> However, we are unlikely to support other compilers than GCC.  If you
> don't like the malloc(0) check, it looks okay to me to remove it from
> xmalloc and xrealloc.

Thanks for your description. I could understand the intention.

Now I think removing the malloc(1) shouldn't be done. Because this depends
on the implementation of libc, not compiler.

When sheepdog is ported on other OSes with libc which has different semantics
of malloc(), the malloc(1) is required.


More information about the sheepdog mailing list