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

Re: [PATCH 7/7] Avoid duplicating memory, and use xmemdupz instead of xstrdup.

From
Pierre Habouzit <madcoder@debian.org>
Date
Sep 20, 2007, 22:05 UTC
Message-ID
<20070920220506.GC17514@artemis.corp>
In-Reply-To
<1190241736-30449-8-git-send-email-madcoder@debian.org>
On mer, sep 19, 2007 at 10:42:16 +0000, Pierre Habouzit wrote:
Show 37 quoted lines
> Signed-off-by: Pierre Habouzit <madcoder@debian.org>
> ---
>  walker.c |   23 +++++++++--------------
>  1 files changed, 9 insertions(+), 14 deletions(-)
> 
> diff --git a/walker.c b/walker.c
> index 397b80d..0abdd64 100644
> --- a/walker.c
> +++ b/walker.c
> @@ -213,24 +213,19 @@ int walker_targets_stdin(char ***target, const char ***write_ref)
>  	struct strbuf buf;
>  	*target = NULL; *write_ref = NULL;
>  	strbuf_init(&buf, 0);
> -	while (1) {
> -		char *rf_one = NULL;
> -		char *tg_one;
> -
> -		if (strbuf_getline(&buf, stdin, '\n') == EOF)
> -			break;
> -		tg_one = buf.buf;
> -		rf_one = strchr(tg_one, '\t');
> -		if (rf_one)
> -			*rf_one++ = 0;
> +	while (strbuf_getline(&buf, stdin, '\n') != EOF) {
> +		char *rf_one = memchr(buf.buf, '\t', buf.len);
>  
>  		if (targets >= targets_alloc) {
> -			targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
> -			*target = xrealloc(*target, targets_alloc * sizeof(**target));
> +			ALLOC_GROW(target, targets, targets_alloc);
>  			*write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
>  		}
> -		(*target)[targets] = xstrdup(tg_one);
> -		(*write_ref)[targets] = rf_one ? xstrdup(rf_one) : NULL;
> +		if (rf_one) {
> +			(*write_ref)[targets] = xmemdupz(rf_one, buf.len - (rf_one - buf.buf));
> +		} else {
  As someone pointed to me off-list the above should be:
  +		if (rf_one) {
  +			(*write_ref)[targets] = xmemdupz(rf_one + 1, buf.len - (rf_one + 1 - buf.buf));
  +		} else {
  Or better:
  +		if (rf_one) {
  +			rf_one++; /* skip \t */
  +			(*write_ref)[targets] = xmemdupz(rf_one, buf.buf + buf.len - rf_one);
  +		} else {
  Which is definitely more readable.
-- 
·O·  Pierre Habouzit
··O                                                madcoder@debian.org
OOO                                                http://www.madism.org
Previous: Pierre HabouzitNext: Pierre Habouzit
Message 9 of 11 in “quote/strbuf series, take 3”
  1. Pierre HabouzitSep 19, 2007
  2. Junio C HamanoSep 20, 2007
  3. Christian CouderSep 20, 2007
  4. Pierre HabouzitSep 20, 2007
  5. 2/7 nfv?asprintf are broken without va_copy, workaround them.Pierre Habouzit, Sep 20, 2007
  6. Junio C HamanoSep 21, 2007
  7. Pierre HabouzitSep 21, 2007
  8. 4/7 sq_quote_argv and add_to_string rework with strbuf's.Pierre Habouzit, Sep 20, 2007
  9. Pierre HabouzitSep 20, 2007
  10. Pierre HabouzitSep 21, 2007
  11. 7/7 Pierre Habouzit, Sep 21, 2007

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.