git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phil Hord <hordp@cisco.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Tay Ray Chuan <rctay89@gmail.com>,
	Git Mailing List <git@vger.kernel.org>,
	"Shawn O. Pearce" <spearce@spearce.org>
Subject: Re: [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups
Date: Wed, 06 Jul 2011 23:55:29 -0400	[thread overview]
Message-ID: <4E152E31.4020105@cisco.com> (raw)
In-Reply-To: <7vaacrhtp3.fsf@alter.siamese.dyndns.org>



On 07/06/2011 07:31 PM, Junio C Hamano wrote:
> Tay Ray Chuan <rctay89@gmail.com> writes:
>
>> On Thu, Jul 7, 2011 at 1:57 AM, Junio C Hamano <gitster@pobox.com> wrote:
>>> Tay Ray Chuan <rctay89@gmail.com> writes:
>>>
>>>> Group free()'s that are called when a malloc() fails in
>>>> xdl_prepare_ctx(), making for more readable code.
>>> Nicer, but I wonder if we can get away with a single label that lets us
>>> abort with freeing whatever we have allocated by making sure that the
>>> various pointer fields and variables are initialized to NULL, which may
>>> lead to even more readable code.
>> Pardon my dullness. Do you mean to check if the various fields are set
>> to NULL upon allocation, and free()'ing them if so?
> What I meant was that, instead of doing this:
>
>     func() {
> 	if (somefunc(&A, ...) < 0)
> 		goto failA;
>         ... do something ...
> 	B = someotheralloc();
>         if (!B)
>         	goto failB;
>         ... do something ...
> 	C = yetanotheralloc();
>         if (!C)
>         	goto failC;
> 	... do things using A, B, C ...
>
> 	return 0;
>     failC:
>     	free(B);
>     failB:
> 	free(A);
>     failA:
>         return -1;
>     }
>
> it would be easier to follow if you did:
>
>     func() {
> 	A = B = C = NULL;
> 	if (somefunc(&A, ...) < 0)
> 		goto fail;
>         ... do something ...
> 	B = someotheralloc();
>         if (!B)
>         	goto fail;
>         ... do something ...
> 	C = yetanotheralloc();
>         if (!C)
>         	goto fail;
> 	... do things using A, B, C ...
>
>     fail:
>         free(B);
>     	free(A);
>         return -1;
>     }
>
> Especially when you have more than 2 such fail labels.


Because 'free(NULL)' is handled sanely (as a no-op).
http://pubs.opengroup.org/onlinepubs/009695399/functions/free.html

I haven't looked, but I assume xdl_cha_free does the same thing.  I only
assume this because Junio seems to imply it, though.

Phil

  reply	other threads:[~2011-07-07  4:05 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-06 16:38 [PATCH v2 0/4] prepare for histogram diff Tay Ray Chuan
2011-07-06 16:38 ` [PATCH v2 1/4] xdiff/xprepare: use memset() Tay Ray Chuan
2011-07-06 16:38   ` [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups Tay Ray Chuan
2011-07-06 16:38     ` [PATCH v2 3/4] xdiff/xpatience: factor out fall-back-diff function Tay Ray Chuan
2011-07-06 16:38       ` [PATCH v2 4/4] t4033-diff-patience: factor out tests Tay Ray Chuan
2011-07-06 18:04         ` Junio C Hamano
2011-07-06 17:57     ` [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups Junio C Hamano
2011-07-06 18:10       ` Tay Ray Chuan
2011-07-06 23:31         ` Junio C Hamano
2011-07-07  3:55           ` Phil Hord [this message]
2011-07-07  4:05             ` Tay Ray Chuan
2011-07-07  4:23 ` [PATCH v3 0/4] prepare for histogram diff Tay Ray Chuan
2011-07-07  4:23   ` [PATCH v3 1/4] xdiff/xprepare: use memset() Tay Ray Chuan
2011-07-07  4:23     ` [PATCH v3 2/4] xdiff/xprepare: refactor abort cleanups Tay Ray Chuan
2011-07-07  4:23       ` [PATCH v3 3/4] xdiff/xpatience: factor out fall-back-diff function Tay Ray Chuan
2011-07-07  4:23         ` [PATCH v3 4/4] t4033-diff-patience: factor out tests Tay Ray Chuan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4E152E31.4020105@cisco.com \
    --to=hordp@cisco.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=rctay89@gmail.com \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).