git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: "Jonathan Nieder" <jrnieder@gmail.com>,
	"Joachim Schmitz" <jojo@schmitz-digital.de>,
	"René Scharfe" <rene.scharfe@lsrfire.ath.cx>,
	git@vger.kernel.org
Subject: [PATCHv2 6/8] submodule: simplify memory handling in config parsing
Date: Wed, 23 Jan 2013 01:26:42 -0500	[thread overview]
Message-ID: <20130123062642.GF5036@sigill.intra.peff.net> (raw)
In-Reply-To: <20130123062132.GA2038@sigill.intra.peff.net>

We keep a strbuf for the name of the submodule, even though
we only ever add one string to it. Let's just use xmemdupz
instead, which is slightly more efficient and makes it
easier to follow what is going on.

Unfortunately, we still end up having to deal with some
memory ownership issues in some code branches, as we have to
allocate the string in order to do a string list lookup, and
then only sometimes want to hand ownership of that string
over to the string_list. Still, making that explicit in the
code (as opposed to sometimes detaching the strbuf, and then
always releasing it) makes it a little more obvious what is
going on.

Signed-off-by: Jeff King <peff@peff.net>
---
 submodule.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)

diff --git a/submodule.c b/submodule.c
index 25413de..9ba1496 100644
--- a/submodule.c
+++ b/submodule.c
@@ -127,7 +127,6 @@ int parse_submodule_config_option(const char *var, const char *value)
 int parse_submodule_config_option(const char *var, const char *value)
 {
 	struct string_list_item *config;
-	struct strbuf submodname = STRBUF_INIT;
 	const char *name, *key;
 	int namelen;
 
@@ -135,37 +134,36 @@ int parse_submodule_config_option(const char *var, const char *value)
 		return 0;
 
 	if (!strcmp(key, "path")) {
-		strbuf_add(&submodname, name, namelen);
 		config = unsorted_string_list_lookup(&config_name_for_path, value);
 		if (config)
 			free(config->util);
 		else
 			config = string_list_append(&config_name_for_path, xstrdup(value));
-		config->util = strbuf_detach(&submodname, NULL);
-		strbuf_release(&submodname);
+		config->util = xmemdupz(name, namelen);
 	} else if (!strcmp(key, "fetchrecursesubmodules")) {
-		strbuf_add(&submodname, name, namelen);
-		config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
+		char *name_cstr = xmemdupz(name, namelen);
+		config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
 		if (!config)
-			config = string_list_append(&config_fetch_recurse_submodules_for_name,
-						    strbuf_detach(&submodname, NULL));
+			config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
+		else
+			free(name_cstr);
 		config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
-		strbuf_release(&submodname);
 	} else if (!strcmp(key, "ignore")) {
+		char *name_cstr;
+
 		if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
 		    strcmp(value, "all") && strcmp(value, "none")) {
 			warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
 			return 0;
 		}
 
-		strbuf_add(&submodname, name, namelen);
-		config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
-		if (config)
+		name_cstr = xmemdupz(name, namelen);
+		config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
+		if (config) {
 			free(config->util);
-		else
-			config = string_list_append(&config_ignore_for_name,
-						    strbuf_detach(&submodname, NULL));
-		strbuf_release(&submodname);
+			free(name_cstr);
+		} else
+			config = string_list_append(&config_ignore_for_name, name_cstr);
 		config->util = xstrdup(value);
 		return 0;
 	}
-- 
1.8.0.2.15.g815dc66

  parent reply	other threads:[~2013-01-23  6:27 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-13 17:42 [PATCH] archive-tar: fix sanity check in config parsing René Scharfe
2013-01-13 20:00 ` Jeff King
2013-01-14  8:17   ` Joachim Schmitz
2013-01-14 12:44     ` Jeff King
2013-01-14 14:58       ` Jeff King
2013-01-14 15:00         ` [PATCH 1/6] config: add helper function for parsing key names Jeff King
2013-01-14 18:08           ` Junio C Hamano
2013-01-15 16:04             ` Jeff King
2013-01-15 17:07               ` Junio C Hamano
2013-01-18 20:53                 ` Junio C Hamano
2013-01-23  6:21                   ` [PATCHv2 0/8] config key-parsing cleanups Jeff King
2013-01-23  6:23                     ` [PATCHv2 1/8] config: add helper function for parsing key names Jeff King
2013-01-23  6:23                     ` [PATCHv2 2/8] archive-tar: use parse_config_key when parsing config Jeff King
2013-01-23  6:24                     ` [PATCHv2 3/8] convert some config callbacks to parse_config_key Jeff King
2013-01-23  6:25                     ` [PATCHv2 4/8] userdiff: drop parse_driver function Jeff King
2013-01-23  6:25                     ` [PATCHv2 5/8] submodule: use parse_config_key when parsing config Jeff King
2013-01-23 20:45                       ` Jens Lehmann
2013-01-23  6:26                     ` Jeff King [this message]
2013-01-23 20:51                       ` [PATCHv2 6/8] submodule: simplify memory handling in config parsing Jens Lehmann
2013-01-23  6:27                     ` [PATCHv2 7/8] help: use parse_config_key for man config Jeff King
2013-01-23  6:27                     ` [PATCHv2 8/8] reflog: use parse_config_key in config callback Jeff King
2013-01-23  7:04                       ` Junio C Hamano
2013-01-23  7:27                     ` [PATCHv2 0/8] config key-parsing cleanups Jonathan Nieder
2013-01-14 15:02         ` [PATCH 2/6] archive-tar: use match_config_key when parsing config Jeff King
2013-01-14 15:03         ` [PATCH 3/6] convert some config callbacks to match_config_key Jeff King
2013-01-14 16:55           ` Jonathan Nieder
2013-01-14 17:06             ` Jeff King
2013-01-14 18:05               ` Jeff King
2013-01-14 15:04         ` [PATCH 4/6] userdiff: drop parse_driver function Jeff King
2013-01-14 15:04         ` [PATCH 5/6] submodule: use match_config_key when parsing config Jeff King
2013-01-14 15:07         ` [PATCH 6/6] submodule: simplify memory handling in config parsing 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

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

  git send-email \
    --in-reply-to=20130123062642.GF5036@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jojo@schmitz-digital.de \
    --cc=jrnieder@gmail.com \
    --cc=rene.scharfe@lsrfire.ath.cx \
    /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).