git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pierre Habouzit <madcoder@debian.org>
To: git@vger.kernel.org
Cc: Pierre Habouzit <madcoder@debian.org>
Subject: [PATCH] Add strbuf_fread, use it in fast-import.c.
Date: Tue,  4 Sep 2007 10:48:00 +0200	[thread overview]
Message-ID: <11888956801971-git-send-email-madcoder@debian.org> (raw)
In-Reply-To: <buobqcjrycl.fsf@dhapc248.dev.necel.com>

  This is a good proof of concept that combined with strbuf_detach it often
helps improving readability a lot, while remaining as efficient as before.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 fast-import.c |   26 ++++++++++----------------
 strbuf.c      |   12 ++++++++++++
 strbuf.h      |    3 +++
 3 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/fast-import.c b/fast-import.c
index ab3927b..2d8daeb 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1638,20 +1638,15 @@ static void cmd_mark(void)
 
 static void *cmd_data (size_t *size)
 {
-	size_t length;
-	char *buffer;
+	struct strbuf buffer = STRBUF_INIT;
 
 	if (prefixcmp(command_buf.buf, "data "))
 		die("Expected 'data n' command, found: %s", command_buf.buf);
 
 	if (!prefixcmp(command_buf.buf + 5, "<<")) {
 		char *term = xstrdup(command_buf.buf + 5 + 2);
-		size_t sz = 8192, term_len = command_buf.len - 5 - 2;
-		length = 0;
-		buffer = xmalloc(sz);
+		size_t term_len = command_buf.len - 5 - 2;
 
-		/* XXX possible memory leak ? */
-		strbuf_detach(&command_buf);
 		for (;;) {
 			read_line(&command_buf, stdin, '\n');
 			if (command_buf.eof)
@@ -1659,19 +1654,18 @@ static void *cmd_data (size_t *size)
 			if (term_len == command_buf.len
 				&& !strcmp(term, command_buf.buf))
 				break;
-			ALLOC_GROW(buffer, length + command_buf.len + 1, sz);
-			memcpy(buffer + length, command_buf.buf, command_buf.len);
-			length += command_buf.len;
-			buffer[length++] = '\n';
+			strbuf_addbuf(&buffer, &command_buf);
+			strbuf_addch(&buffer, '\n');
 		}
 		free(term);
 	}
 	else {
-		size_t n = 0;
+		size_t n = 0, length;
+
 		length = strtoul(command_buf.buf + 5, NULL, 10);
-		buffer = xmalloc(length);
+
 		while (n < length) {
-			size_t s = fread(buffer + n, 1, length - n, stdin);
+			size_t s = strbuf_fread(&buffer, length - n, stdin);
 			if (!s && feof(stdin))
 				die("EOF in data (%lu bytes remaining)",
 					(unsigned long)(length - n));
@@ -1680,8 +1674,8 @@ static void *cmd_data (size_t *size)
 	}
 
 	skip_optional_lf();
-	*size = length;
-	return buffer;
+	*size = buffer.len;
+	return strbuf_detach(&buffer);
 }
 
 static int validate_raw_date(const char *src, char *result, int maxlen)
diff --git a/strbuf.c b/strbuf.c
index 86d5965..693fbcf 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -58,6 +58,18 @@ void strbuf_addvf(struct strbuf *sb, const char *fmt, va_list ap)
 	sb->buf[sb->len] = '\0';
 }
 
+size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) {
+	size_t res;
+
+	strbuf_ensure(sb, size);
+	res = fread(sb->buf + sb->len, 1, size, f);
+	if (res > 0) {
+		sb->len += res;
+		sb->buf[sb->len] = '\0';
+	}
+	return res;
+}
+
 void read_line(struct strbuf *sb, FILE *fp, int term) {
 	int ch;
 	if (feof(fp)) {
diff --git a/strbuf.h b/strbuf.h
index ee72419..20c5255 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -42,6 +42,9 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...) {
 	va_end(ap);
 }
 
+
+extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
+
 extern void read_line(struct strbuf *, FILE *, int);
 
 #endif /* STRBUF_H */
-- 
1.5.3

  parent reply	other threads:[~2007-09-04  8:48 UTC|newest]

Thread overview: 90+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-02 22:42 strbuf API Pierre Habouzit
2007-09-03  5:43 ` Johan Herland
2007-09-03  8:46   ` Pierre Habouzit
2007-09-04  1:52     ` Miles Bader
2007-09-04  8:47       ` strbuf new semantics, let's give it a try Pierre Habouzit
2007-09-04  8:47       ` [PATCH] Rework strbuf API and semantics Pierre Habouzit
2007-09-04 11:11         ` Johannes Schindelin
2007-09-04 11:53           ` Pierre Habouzit
2007-09-04 13:34             ` Andreas Ericsson
2007-09-04 14:01             ` Pierre Habouzit
2007-09-04 15:44               ` Johannes Schindelin
2007-09-04 16:18                 ` Pierre Habouzit
2007-09-04 17:18                   ` Wincent Colaiuta
2007-09-04 14:01             ` [PATCH] Simplify strbuf uses in fast-import.c using the proper functions Pierre Habouzit
2007-09-04 23:46               ` René Scharfe
2007-09-04 23:46               ` René Scharfe
2007-09-05  7:48                 ` Pierre Habouzit
2007-09-05  8:05                   ` Junio C Hamano
2007-09-05  8:57                     ` Pierre Habouzit
2007-09-05 19:18                       ` [PATCH] Rework strbuf API and semantics Pierre Habouzit
2007-09-05 19:18                         ` [PATCH] Simplify strbuf uses in archive-tar.c using the proper functions Pierre Habouzit
2007-09-05 19:18                           ` [PATCH] Simplify strbuf uses in fast-import.c " Pierre Habouzit
2007-09-05 19:18                             ` [PATCH] Use proper strbuf API, and also simplify cmd_data code Pierre Habouzit
2007-09-05 19:18                               ` [PATCH] Simplify write_tree using strbuf's Pierre Habouzit
2007-09-05 19:18                                 ` [PATCH] Further strbuf re-engineering Pierre Habouzit
2007-09-05 19:18                                   ` [PATCH] Eradicate yet-another-buffer implementation in buitin-rerere.c Pierre Habouzit
2007-09-05 19:18                                     ` [PATCH] More strbuf uses in cache-tree.c Pierre Habouzit
2007-09-19  8:05                                   ` [PATCH] Further strbuf re-engineering Junio C Hamano
2007-09-05 19:21                             ` [PATCH] Simplify strbuf uses in fast-import.c using the proper functions Pierre Habouzit
2007-09-19  8:06                           ` [PATCH] Simplify strbuf uses in archive-tar.c " Junio C Hamano
2007-09-19  8:36                             ` Pierre Habouzit
2007-09-06  9:31                         ` [PATCH] Rework strbuf API and semantics Junio C Hamano
2007-09-06  9:49                           ` Pierre Habouzit
2007-09-06 10:03                         ` Junio C Hamano
2007-09-06 10:22                           ` Pierre Habouzit
2007-09-04 14:01             ` [PATCH] Use proper strbuf API, and also simplify cmd_data code Pierre Habouzit
2007-09-05  4:44             ` [PATCH] Rework strbuf API and semantics Miles Bader
2007-09-04  8:48       ` Pierre Habouzit [this message]
2007-09-03  8:32 ` strbuf API Matthieu Moy
2007-09-03  8:49   ` Pierre Habouzit
2007-09-03  9:02     ` Matthieu Moy
2007-09-03  9:18     ` Junio C Hamano
2007-09-03 11:53       ` Pierre Habouzit
2007-09-03 12:29       ` Johannes Schindelin
2007-09-06 11:20 ` strbuf new API, take 2 for inclusion Pierre Habouzit
2007-09-06 11:20   ` [PATCH 1/7] Rework strbuf API and semantics Pierre Habouzit
2007-09-06 11:20     ` [PATCH 2/7] Simplify strbuf uses in archive-tar.c using the proper functions Pierre Habouzit
2007-09-06 11:20       ` [PATCH 3/7] Use proper strbuf API, and also simplify cmd_data code Pierre Habouzit
2007-09-06 11:20         ` [PATCH 4/7] Simplify write_tree using strbuf's Pierre Habouzit
2007-09-06 11:20           ` [PATCH 5/7] Further strbuf re-engineering Pierre Habouzit
2007-09-06 11:20             ` [PATCH 6/7] Eradicate yet-another-buffer implementation in buitin-rerere.c Pierre Habouzit
2007-09-06 11:20               ` [PATCH 7/7] More strbuf uses in cache-tree.c Pierre Habouzit
2007-09-06 14:05               ` [PATCH 6/7] Eradicate yet-another-buffer implementation in buitin-rerere.c Johannes Schindelin
2007-09-06 17:17                 ` Pierre Habouzit
2007-09-06 20:16                   ` David Kastrup
2007-09-06 20:54                     ` Pierre Habouzit
2007-09-07  8:03                   ` Junio C Hamano
2007-09-07  9:02                     ` Pierre Habouzit
2007-09-06 17:59       ` [PATCH 2/7] Simplify strbuf uses in archive-tar.c using the proper functions Kristian Høgsberg
2007-09-06 18:08         ` Pierre Habouzit
2007-09-06 18:18           ` Kristian Høgsberg
2007-09-06 18:27             ` Pierre Habouzit
2007-09-06 22:54               ` René Scharfe
2007-09-06 14:09     ` [PATCH 1/7] Rework strbuf API and semantics Johannes Schindelin
2007-09-06 14:21       ` Jeff King
2007-09-06 14:44         ` David Kastrup
2007-09-06 14:50           ` Jeff King
2007-09-06 15:06             ` David Kastrup
2007-09-06 15:36               ` Jeff King
2007-09-06 15:53                 ` David Kastrup
2007-09-06 15:45               ` Johannes Sixt
2007-09-06 14:43       ` David Kastrup
2007-09-06 14:52         ` Jeff King
2007-09-06 17:49     ` Kristian Høgsberg
2007-09-06 12:58   ` strbuf new API, take 2 for inclusion Jeff King
2007-09-06 17:15     ` Pierre Habouzit
2007-09-06 17:16       ` Jeff King
2007-09-06 17:19         ` Pierre Habouzit
2007-09-08 11:53 ` Use strbufs in commit.c (pretty printing) Pierre Habouzit
2007-09-08 11:53   ` [PATCH 1/3] Add strbuf_rtrim (to remove trailing spaces) Pierre Habouzit
2007-09-08 11:53     ` [PATCH 2/3] Change semantics of interpolate to work like snprintf Pierre Habouzit
2007-09-08 11:53       ` [PATCH 3/3] Rework pretty_print_commit to use strbufs instead of custom buffers Pierre Habouzit
2007-09-08 11:59         ` David Kastrup
2007-09-08 12:17           ` Pierre Habouzit
2007-09-08 12:28           ` Pierre Habouzit
2007-09-08 18:40         ` René Scharfe
2007-09-08 18:49           ` Pierre Habouzit
2007-09-08 16:18     ` [PATCH 1/3] Add strbuf_rtrim (to remove trailing spaces) René Scharfe
2007-09-08 22:53       ` Pierre Habouzit
2007-09-08 23:44         ` 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=11888956801971-git-send-email-madcoder@debian.org \
    --to=madcoder@debian.org \
    --cc=git@vger.kernel.org \
    /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).