bug-bash
[Top][All Lists]
Advanced

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

Re: unnecessary {de,}alloc wrappers?


From: Ondrej Oprala
Subject: Re: unnecessary {de,}alloc wrappers?
Date: Tue, 01 Apr 2014 19:36:23 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0

On 04/01/2014 05:43 PM, Eric Blake wrote:
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).
Hmm, is that still up-to-date information?
Doing
$ git grep -B2 '\bfree\(.*;'
on glibc's and gnulib's upstream repo shows no occurences of code such as "if (foobar) free(foobar);". Sure there are conditional free()-s in the code but the condition usually evaluates a wholly different variable, a combination of them or a function call (often + assignment), which is quite common.

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.


That's possible. It might also have been an attempt at further optimization - just calling malloc if pointer == NULL, instead going through an additional realloc call.

Either way, I find both of these code snippets very redundant.

Thanks,
Ondrej.





reply via email to

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