From: Pierre Habouzit <madcoder@debian.org>
To: gitster@pobox.com
Cc: git@vger.kernel.org
Subject: Re: [PATCH 7/7] Avoid duplicating memory, and use xmemdupz instead of xstrdup.
Date: Fri, 21 Sep 2007 00:05:06 +0200 [thread overview]
Message-ID: <20070920220506.GC17514@artemis.corp> (raw)
In-Reply-To: <1190241736-30449-8-git-send-email-madcoder@debian.org>
[-- Attachment #1: Type: text/plain, Size: 1954 bytes --]
On mer, sep 19, 2007 at 10:42:16 +0000, Pierre Habouzit wrote:
> 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
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
next prev parent reply other threads:[~2007-09-20 22:05 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-19 22:42 quote/strbuf series, take 3 Pierre Habouzit
[not found] ` <1190241736-30449-2-git-send-email-madcoder@debian.org>
[not found] ` <1190241736-30449-3-git-send-email-madcoder@debian.org>
2007-09-20 4:27 ` [PATCH 2/7] nfv?asprintf are broken without va_copy, workaround them Junio C Hamano
2007-09-20 4:53 ` Christian Couder
2007-09-20 8:27 ` Pierre Habouzit
2007-09-20 8:43 ` [SUPERSEDES PATCH " Pierre Habouzit
2007-09-21 6:17 ` Junio C Hamano
2007-09-21 7:02 ` Pierre Habouzit
2007-09-20 8:44 ` [SUPERSEDES PATCH 4/7] sq_quote_argv and add_to_string rework with strbuf's Pierre Habouzit
[not found] ` <1190241736-30449-4-git-send-email-madcoder@debian.org>
[not found] ` <1190241736-30449-5-git-send-email-madcoder@debian.org>
[not found] ` <1190241736-30449-6-git-send-email-madcoder@debian.org>
[not found] ` <1190241736-30449-7-git-send-email-madcoder@debian.org>
[not found] ` <1190241736-30449-8-git-send-email-madcoder@debian.org>
2007-09-20 22:05 ` Pierre Habouzit [this message]
2007-09-21 7:03 ` [PATCH 7/7] Avoid duplicating memory, and use xmemdupz instead of xstrdup Pierre Habouzit
2007-09-21 7:39 ` [DON'T MERGE PATCH 7/7] Pierre Habouzit
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=20070920220506.GC17514@artemis.corp \
--to=madcoder@debian.org \
--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).