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

[PATCH 1/4] strbuf_split_buf(): use ALLOC_GROW()

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Nov 4, 2012, 06:46 UTC
Message-ID
<1352011614-29334-2-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1352011614-29334-1-git-send-email-mhagger@alum.mit.edu>

Use ALLOC_GROW() rather than inline code to manage memory in strbuf_split_buf(). Rename "pos" to "nr" because it better describes the use of the variable and it better conforms to the "ALLOC_GROW" idiom.

Also, instead of adding a sentinal NULL value after each entry is added to the list, only add it once after all of the entries have been added.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 strbuf.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)
diff --git a/strbuf.c b/strbuf.c
index 4b9e30c..5256c2a 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -108,33 +108,30 @@ void strbuf_ltrim(struct strbuf *sb)
 
 struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int max)
 {
-	int alloc = 2, pos = 0;
+	struct strbuf **ret = NULL;
+	size_t nr = 0, alloc = 0;
 	const char *n, *p;
-	struct strbuf **ret;
 	struct strbuf *t;
 
-	ret = xcalloc(alloc, sizeof(struct strbuf *));
 	p = n = str;
 	while (n < str + slen) {
 		int len;
-		if (max <= 0 || pos + 1 < max)
+		if (max <= 0 || nr + 1 < max)
 			n = memchr(n, delim, slen - (n - str));
 		else
 			n = NULL;
-		if (pos + 1 >= alloc) {
-			alloc = alloc * 2;
-			ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
-		}
 		if (!n)
 			n = str + slen - 1;
 		len = n - p + 1;
 		t = xmalloc(sizeof(struct strbuf));
 		strbuf_init(t, len);
 		strbuf_add(t, p, len);
-		ret[pos] = t;
-		ret[++pos] = NULL;
+		ALLOC_GROW(ret, nr + 2, alloc);
+		ret[nr++] = t;
 		p = ++n;
 	}
+	ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
+	ret[nr] = NULL;
 	return ret;
 }
 
-- 
1.8.0
Previous: Michael HaggertyNext: Jeff King
Message 2 of 9 in “Simplify and document strbuf_split() functions”
  1. 0/4 Simplify and document strbuf_split() functionsMichael Haggerty, Nov 4, 2012
  2. 1/4 strbuf_split_buf(): use ALLOC_GROW()Michael Haggerty, Nov 4, 2012
  3. Jeff KingNov 4, 2012
  4. Michael HaggertyNov 6, 2012
  5. Jeff KingNov 8, 2012
  6. 2/4 strbuf_split_buf(): simplify iterationMichael Haggerty, Nov 4, 2012
  7. 3/4 strbuf_split*(): rename "delim" parameter to "terminator"Michael Haggerty, Nov 4, 2012
  8. 4/4 strbuf_split*(): document functionsMichael Haggerty, Nov 4, 2012
  9. Jeff KingNov 4, 2012

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.