git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: y@imag.fr
To: git@vger.kernel.org, gitster@pobox.com
Cc: Matthieu Moy <Matthieu.Moy@imag.fr>
Subject: [PATCH 1/2] fixup! 8e00b48 don't allocate struct wt_status_state dynamically
Date: Thu, 14 Jun 2012 10:20:13 +0200	[thread overview]
Message-ID: <1339662014-30173-1-git-send-email-y> (raw)
In-Reply-To: <7vk3zag6jg.fsf@alter.siamese.dyndns.org>

From: Matthieu Moy <Matthieu.Moy@imag.fr>

The common

void function() {
	struct wt_status_state *state = calloc(...);
	...
	free(state);
}

is essentially a less efficient, and more error prone way of allocating a
variable on the stack (plus, the calloc should have been a xcalloc).
Replace it with an on-stack variable.

While we're there, also replace the individual initializations of fields
with memset(..., 0, ...).

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---

(BTW, I didn't find a way to have both --autosquash-compliant and
meaningfull titles)

 wt-status.c | 49 +++++++++++++++++++++----------------------------
 1 file changed, 21 insertions(+), 28 deletions(-)

diff --git a/wt-status.c b/wt-status.c
index ed28b4f..e65716d 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -932,49 +932,42 @@ static void show_bisect_in_progress(struct wt_status *s,
 static void wt_status_print_state(struct wt_status *s)
 {
 	const char *state_color = color(WT_STATUS_IN_PROGRESS, s);
-	struct wt_status_state *state = calloc(1, sizeof(*state));
+	struct wt_status_state state;
 	struct stat st;
 
-	state->merge_in_progress = 0;
-	state->am_in_progress = 0;
-	state->am_empty_patch = 0;
-	state->rebase_in_progress = 0;
-	state->rebase_interactive_in_progress = 0;
-	state->cherry_pick_in_progress = 0;
-	state->bisect_in_progress = 0;
+	memset(&state, 0, sizeof(state));
 
 	if (!stat(git_path("MERGE_HEAD"), &st)) {
-		state->merge_in_progress = 1;
+		state.merge_in_progress = 1;
 	} else if (!stat(git_path("rebase-apply"), &st)) {
 		if (!stat(git_path("rebase-apply/applying"), &st)) {
-			state->am_in_progress = 1;
+			state.am_in_progress = 1;
 			if (!stat(git_path("rebase-apply/patch"), &st) && !st.st_size)
-				state->am_empty_patch = 1;
+				state.am_empty_patch = 1;
 		} else {
-			state->rebase_in_progress = 1;
+			state.rebase_in_progress = 1;
 		}
 	} else if (!stat(git_path("rebase-merge"), &st)) {
 		if (!stat(git_path("rebase-merge/interactive"), &st))
-			state->rebase_interactive_in_progress = 1;
+			state.rebase_interactive_in_progress = 1;
 		else
-			state->rebase_in_progress = 1;
+			state.rebase_in_progress = 1;
 	} else if (!stat(git_path("CHERRY_PICK_HEAD"), &st)) {
-		state->cherry_pick_in_progress = 1;
+		state.cherry_pick_in_progress = 1;
 	}
 	if (!stat(git_path("BISECT_LOG"), &st))
-		state->bisect_in_progress = 1;
-
-	if (state->merge_in_progress)
-		show_merge_in_progress(s, state, state_color);
-	else if (state->am_in_progress)
-		show_am_in_progress(s, state, state_color);
-	else if (state->rebase_in_progress || state->rebase_interactive_in_progress)
-		show_rebase_in_progress(s, state, state_color);
-	else if (state->cherry_pick_in_progress)
-		show_cherry_pick_in_progress(s, state, state_color);
-	if (state->bisect_in_progress)
-		show_bisect_in_progress(s, state, state_color);
-	free(state);
+		state.bisect_in_progress = 1;
+
+	if (state.merge_in_progress)
+		show_merge_in_progress(s, &state, state_color);
+	else if (state.am_in_progress)
+		show_am_in_progress(s, &state, state_color);
+	else if (state.rebase_in_progress || state.rebase_interactive_in_progress)
+		show_rebase_in_progress(s, &state, state_color);
+	else if (state.cherry_pick_in_progress)
+		show_cherry_pick_in_progress(s, &state, state_color);
+	if (state.bisect_in_progress)
+		show_bisect_in_progress(s, &state, state_color);
 }
 
 void wt_status_print(struct wt_status *s)
-- 
1.7.11.rc0.57.g84a04c7


  parent reply	other threads:[~2012-06-14  8:20 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-13 23:01 What's cooking in git.git (Jun 2012, #03; Wed, 13) Junio C Hamano
2012-06-14  8:18 ` lk/more-helpful-status-hints (was Re: What's cooking in git.git (Jun 2012, #03; Wed, 13)) Matthieu Moy
2012-06-14  8:20 ` [PATCH 1/2] fixup! 8e00b48 don't allocate struct wt_status_state dynamically y
2012-06-14 17:13   ` Junio C Hamano
2012-06-14  8:20 ` y [this message]
2012-06-14  8:20   ` [PATCH 2/2] fixup! 38388c8 reword message when splitting a commit y
2012-06-14  8:20   ` y
2012-06-14  8:23   ` [PATCH 1/2] fixup! 8e00b48 don't allocate struct wt_status_state dynamically Matthieu Moy

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=1339662014-30173-1-git-send-email-y \
    --to=y@imag.fr \
    --cc=Matthieu.Moy@imag.fr \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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).