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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Pierre Habouzit <madcoder@xxxxxxxxxx>
---
 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 {
+			(*write_ref)[targets] = NULL;
+		}
+		(*target)[targets] = strbuf_detach(&buf);
 		targets++;
 	}
 	strbuf_release(&buf);
-- 
1.5.3.2.1036.gccf7

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux