git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>, "Jeff King" <peff@peff.net>,
	"Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Subject: [RFC PATCH v2 4/4] branch: use ref_sorting_release()
Date: Wed, 20 Oct 2021 15:24:36 +0200	[thread overview]
Message-ID: <RFC-patch-v2-4.4-f7d87aea384-20211020T131627Z-avarab@gmail.com> (raw)
In-Reply-To: <RFC-cover-v2-0.4-00000000000-20211020T131627Z-avarab@gmail.com>

Use a ref_sorting_release() in branch.c to free the memory from the
ref_sorting_options(). This plugs the final in-tree memory leak of
that API.

In the preceding commit the "sorting" variable was left in the
cmd_branch() scope, even though that wasn't needed anymore. Move it to
the "else if (list)" scope instead. We can also move the "struct
string_list" only used for that branch to be declared in that block

That "struct ref_sorting" does not need to be "static" (and isn't
re-used). The "ref_sorting_options()" will return a valid one, we
don't need to make it "static" to have it zero'd out. That it was
static was another artifact of the pre-image of the preceding commit.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/branch.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 69211f8dd1c..3963c592f56 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -69,7 +69,6 @@ static const char *color_branch_slots[] = {
 	[BRANCH_COLOR_WORKTREE] = "worktree",
 };
 
-static struct string_list output = STRING_LIST_INIT_DUP;
 static unsigned int colopts;
 
 define_list_config_array(color_branch_slots);
@@ -406,7 +405,8 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
 	return strbuf_detach(&fmt, NULL);
 }
 
-static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, struct ref_format *format)
+static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting,
+			   struct ref_format *format, struct string_list *output)
 {
 	int i;
 	struct ref_array array;
@@ -448,7 +448,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
 		if (column_active(colopts)) {
 			assert(!filter->verbose && "--column and --verbose are incompatible");
 			 /* format to a string_list to let print_columns() do its job */
-			string_list_append(&output, out.buf);
+			string_list_append(output, out.buf);
 		} else {
 			fwrite(out.buf, 1, out.len, stdout);
 			putchar('\n');
@@ -623,7 +623,6 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	enum branch_track track;
 	struct ref_filter filter;
 	int icase = 0;
-	static struct ref_sorting *sorting;
 	struct string_list sorting_options = STRING_LIST_INIT_DUP;
 	struct ref_format format = REF_FORMAT_INIT;
 
@@ -737,6 +736,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		print_current_branch_name();
 		return 0;
 	} else if (list) {
+		struct ref_sorting *sorting;
+		static struct string_list output = STRING_LIST_INIT_DUP;
+
 		/*  git branch --list also shows HEAD when it is detached */
 		if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
 			filter.kind |= FILTER_REFS_DETACHED_HEAD;
@@ -752,9 +754,10 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
 		ref_sorting_set_sort_flags_all(
 			sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1);
-		print_ref_list(&filter, sorting, &format);
+		print_ref_list(&filter, sorting, &format, &output);
 		print_columns(&output, colopts, NULL);
 		string_list_clear(&output, 0);
+		ref_sorting_release(sorting);
 		return 0;
 	} else if (edit_description) {
 		const char *branch_name;
-- 
2.33.1.1338.g20da966911a


  parent reply	other threads:[~2021-10-20 13:24 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-18 18:32 [PATCH] for-each-ref: delay parsing of --sort=<atom> options Junio C Hamano
2021-10-19 22:18 ` Jeff King
2021-10-20  2:20   ` Jeff King
2021-10-20 12:30     ` Ævar Arnfjörð Bjarmason
2021-10-20 13:24       ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Ævar Arnfjörð Bjarmason
2021-10-20 13:24         ` [RFC PATCH v2 1/4] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
2021-10-20 14:37           ` Junio C Hamano
2021-10-20 13:24         ` [RFC PATCH v2 2/4] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 14:39           ` Junio C Hamano
2021-10-20 13:24         ` [RFC PATCH v2 3/4] for-each-ref: delay parsing of --sort=<atom> options Ævar Arnfjörð Bjarmason
2021-10-20 13:24         ` Ævar Arnfjörð Bjarmason [this message]
2021-10-20 14:43         ` [RFC PATCH v2 0/4] for-each-ref: delay parsing of --sort=<atom> options + linux-leaks fixes Junio C Hamano
2021-10-20 18:27         ` [PATCH 0/3] ref-filter: add a ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 18:27           ` [PATCH 1/3] tag: use a "goto cleanup" pattern, leak less memory Ævar Arnfjörð Bjarmason
2021-10-20 18:27           ` [PATCH 2/3] ref-filter API user: add and use a ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 18:27           ` [PATCH 3/3] branch: use ref_sorting_release() Ævar Arnfjörð Bjarmason
2021-10-20 13:58   ` [PATCH] for-each-ref: delay parsing of --sort=<atom> options Junio C Hamano
2021-10-20 20:48     ` Jeff King
2021-10-20 21:32       ` Junio C Hamano
2021-10-21 14:54         ` Jeff King

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=RFC-patch-v2-4.4-f7d87aea384-20211020T131627Z-avarab@gmail.com \
    --to=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).