bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: unnecessary {de,}alloc wrappers?


From: Eric Blake
Subject: Re: unnecessary {de,}alloc wrappers?
Date: Tue, 01 Apr 2014 09:43:14 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0

On 04/01/2014 09:28 AM, Ondrej Oprala wrote:
> Hi,
>  Could someone please explain to me, why are xrealloc and xfree (or more
> specifically - their parts) from lib/malloc/xmalloc.c necessary?
> 
> e.g.
> if (string)
>     free(string);
> Why is this needed? AFAIK the C standard specifies free(NULL) is
> completely legal.

Bruno Haible argued that the cost of a redundant if conditional is
cheaper than the cost of a function call in profiling runs, at least in
the portions of gnulib where he used that idiom.  Personally, I like
getting rid of the redundant if (I think the extra CPU cycles spent are
in the noise, until given a profile dump that proves otherwise).

> Also:
>   temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
> Again, the C standard says that realloc(0, bytes) is equivalent to
> malloc(bytes).

There's no good reason for this one; probably just extremely old legacy
code back in the day when various libc were not compatible to the
restrictions added in later versions of the C standard.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]