git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/2] fixup! 8e00b48 don't allocate struct wt_status_state dynamically

From
Yy@imag.fr <y@imag.fr>
Date
Jun 14, 2012, 08:20 UTC
Message-ID
<20099.6449254037$1339662032@news.gmane.org>
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
Previous: Matthieu MoyNext: Junio C Hamano
Message 3 of 8 in “What's cooking in git.git (Jun 2012, #03; Wed, 13)”
  1. Junio C HamanoJun 13, 2012
  2. lk/more-helpful-status-hints (was Re: What's cooking in git.git (Jun 2012, #03; Wed, 13))Matthieu Moy, Jun 14, 2012
  3. 1/2 fixup! 8e00b48 don't allocate struct wt_status_state dynamicallyy@imag.fr, Jun 14, 2012
  4. Junio C HamanoJun 14, 2012
  5. 1/2 fixup! 8e00b48 don't allocate struct wt_status_state dynamicallyy@imag.fr, Jun 14, 2012
  6. 2/2 fixup! 38388c8 reword message when splitting a commit.y@imag.fr, Jun 14, 2012
  7. 2/2 fixup! 38388c8 reword message when splitting a commit.y@imag.fr, Jun 14, 2012
  8. Matthieu MoyJun 14, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.