git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tay Ray Chuan <rctay89@gmail.com>
To: "Git Mailing List" <git@vger.kernel.org>
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Shawn O. Pearce" <spearce@spearce.org>
Subject: [PATCH v2 2/4] xdiff/xprepare: refactor abort cleanups
Date: Thu,  7 Jul 2011 00:38:55 +0800	[thread overview]
Message-ID: <1309970337-6016-3-git-send-email-rctay89@gmail.com> (raw)
In-Reply-To: <1309970337-6016-2-git-send-email-rctay89@gmail.com>

Group free()'s that are called when a malloc() fails in
xdl_prepare_ctx(), making for more readable code.

Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
 xdiff/xprepare.c |   94 +++++++++++++++++++----------------------------------
 1 files changed, 34 insertions(+), 60 deletions(-)

diff --git a/xdiff/xprepare.c b/xdiff/xprepare.c
index 783631a..6168327 100644
--- a/xdiff/xprepare.c
+++ b/xdiff/xprepare.c
@@ -143,24 +143,15 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
 	char *rchg;
 	long *rindex;
 
-	if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) {
-
-		return -1;
-	}
-	if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) {
-
-		xdl_cha_free(&xdf->rcha);
-		return -1;
-	}
+	if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0)
+		goto abort_rcha;
+	if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *))))
+		goto abort_recs;
 
 	hbits = xdl_hashbits((unsigned int) narec);
 	hsize = 1 << hbits;
-	if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) {
-
-		xdl_free(recs);
-		xdl_cha_free(&xdf->rcha);
-		return -1;
-	}
+	if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *))))
+		goto abort_rhash;
 	memset(rhash, 0, hsize * sizeof(xrecord_t *));
 
 	nrec = 0;
@@ -175,63 +166,30 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
 			hav = xdl_hash_record(&cur, top, xpp->flags);
 			if (nrec >= narec) {
 				narec *= 2;
-				if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) {
-
-					xdl_free(rhash);
-					xdl_free(recs);
-					xdl_cha_free(&xdf->rcha);
-					return -1;
-				}
+				if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *))))
+					goto abort_rrecs;
 				recs = rrecs;
 			}
-			if (!(crec = xdl_cha_alloc(&xdf->rcha))) {
-
-				xdl_free(rhash);
-				xdl_free(recs);
-				xdl_cha_free(&xdf->rcha);
-				return -1;
-			}
+			if (!(crec = xdl_cha_alloc(&xdf->rcha)))
+				goto abort_crec;
 			crec->ptr = prev;
 			crec->size = (long) (cur - prev);
 			crec->ha = hav;
 			recs[nrec++] = crec;
 
-			if (xdl_classify_record(cf, rhash, hbits, crec) < 0) {
-
-				xdl_free(rhash);
-				xdl_free(recs);
-				xdl_cha_free(&xdf->rcha);
-				return -1;
-			}
+			if (xdl_classify_record(cf, rhash, hbits, crec) < 0)
+				goto abort_classify;
 		}
 	}
 
-	if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) {
-
-		xdl_free(rhash);
-		xdl_free(recs);
-		xdl_cha_free(&xdf->rcha);
-		return -1;
-	}
+	if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char))))
+		goto abort_rchg;
 	memset(rchg, 0, (nrec + 2) * sizeof(char));
 
-	if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long)))) {
-
-		xdl_free(rchg);
-		xdl_free(rhash);
-		xdl_free(recs);
-		xdl_cha_free(&xdf->rcha);
-		return -1;
-	}
-	if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long)))) {
-
-		xdl_free(rindex);
-		xdl_free(rchg);
-		xdl_free(rhash);
-		xdl_free(recs);
-		xdl_cha_free(&xdf->rcha);
-		return -1;
-	}
+	if (!(rindex = (long *) xdl_malloc((nrec + 1) * sizeof(long))))
+		goto abort_rindex;
+	if (!(ha = (unsigned long *) xdl_malloc((nrec + 1) * sizeof(unsigned long))))
+		goto abort_ha;
 
 	xdf->nrec = nrec;
 	xdf->recs = recs;
@@ -245,6 +203,22 @@ static int xdl_prepare_ctx(mmfile_t *mf, long narec, xpparam_t const *xpp,
 	xdf->dend = nrec - 1;
 
 	return 0;
+
+abort_ha:
+	xdl_free(rindex);
+abort_rindex:
+	xdl_free(rchg);
+abort_rchg:
+abort_classify:
+abort_crec:
+abort_rrecs:
+	xdl_free(rhash);
+abort_rhash:
+	xdl_free(recs);
+abort_recs:
+	xdl_cha_free(&xdf->rcha);
+abort_rcha:
+	return -1;
 }
 
 
-- 
1.7.3.4.721.g4666.dirty

  reply	other threads:[~2011-07-06 16:39 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   ` Tay Ray Chuan [this message]
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
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=1309970337-6016-3-git-send-email-rctay89@gmail.com \
    --to=rctay89@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.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).