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

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Wed Oct 3 04:43:33 CEST 2012

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.



More information about the sheepdog mailing list