Subject: [PATCH 18/21] strbuf: allow "buf" to point to the middle of the allocated buffer

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 15 Dec 2010 22:02:53 +0700

This allows offseting buf forward by neglen bytes. This patch makes
sure that reallocation works even when buf is not at the top of
allocated region.

Many functions are not aware that there could be data before buf. Use
with care.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 strbuf.c |   36 ++++++++++++++++++++++++++----------
 strbuf.h |   10 ++++++----
 2 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index bc3a080..12cda6f 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -28,8 +28,10 @@ char strbuf_slopbuf[1];
 
 void strbuf_init(struct strbuf *sb, size_t hint)
 {
-	sb->alloc = sb->len = 0;
-	sb->buf = strbuf_slopbuf;
+	sb->alloc  = 0;
+	sb->len	   = 0;
+	sb->neglen = 0;
+	sb->buf	   = strbuf_slopbuf;
 	if (hint)
 		strbuf_grow(sb, hint);
 }
@@ -37,7 +39,7 @@ void strbuf_init(struct strbuf *sb, size_t hint)
 void strbuf_release(struct strbuf *sb)
 {
 	if (sb->alloc) {
-		free(sb->buf);
+		free(sb->buf - sb->neglen);
 		strbuf_init(sb, 0);
 	}
 }
@@ -45,6 +47,8 @@ void strbuf_release(struct strbuf *sb)
 char *strbuf_detach(struct strbuf *sb, size_t *sz)
 {
 	char *res = sb->alloc ? sb->buf : NULL;
+	if (sb->neglen)
+		die("shift buf back before detaching");
 	if (sz)
 		*sz = sb->len;
 	strbuf_init(sb, 0);
@@ -54,20 +58,23 @@ char *strbuf_detach(struct strbuf *sb, size_t *sz)
 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
 {
 	strbuf_release(sb);
-	sb->buf   = buf;
-	sb->len   = len;
-	sb->alloc = alloc;
+	sb->buf    = buf;
+	sb->len    = len;
+	sb->neglen = 0;
+	sb->alloc  = alloc;
 	strbuf_grow(sb, 0);
 	sb->buf[sb->len] = '\0';
 }
 
 void strbuf_grow(struct strbuf *sb, size_t extra)
 {
-	if (sb->len + extra + 1 <= sb->len)
+	char *buf = !sb->alloc ? NULL : sb->buf - sb->neglen;
+	int len = sb->neglen + sb->len;
+
+	if (len + extra + 1 <= len)
 		die("you want to use way too much memory");
-	if (!sb->alloc)
-		sb->buf = NULL;
-	ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
+	ALLOC_GROW(buf, len + extra + 1, sb->alloc);
+	sb->buf = buf + sb->neglen;
 }
 
 void strbuf_trim(struct strbuf *sb)
@@ -402,3 +409,12 @@ int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
 	strbuf_splice(sb, 0, 0, "refs/heads/", 11);
 	return check_ref_format(sb->buf);
 }
+
+void strbuf_offset(struct strbuf *sb, int offset)
+{
+	if (offset > sb->len || offset < sb->neglen)
+		die("you cannot offset out of allocated buffer");
+	sb->buf	   += offset;
+	sb->neglen += offset;
+	sb->len	   -= offset;
+}
diff --git a/strbuf.h b/strbuf.h
index fac2dbc..3c3b146 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -43,11 +43,11 @@
 extern char strbuf_slopbuf[];
 struct strbuf {
 	size_t alloc;
-	size_t len;
+	size_t len, neglen;
 	char *buf;
 };
 
-#define STRBUF_INIT  { 0, 0, strbuf_slopbuf }
+#define STRBUF_INIT  { 0, 0, 0, strbuf_slopbuf }
 
 /*----- strbuf life cycle -----*/
 extern void strbuf_init(struct strbuf *, size_t);
@@ -62,7 +62,7 @@ static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
 
 /*----- strbuf size related -----*/
 static inline size_t strbuf_avail(const struct strbuf *sb) {
-	return sb->alloc ? sb->alloc - sb->len - 1 : 0;
+	return sb->alloc ? sb->alloc - sb->len - sb->neglen - 1 : 0;
 }
 
 extern void strbuf_grow(struct strbuf *, size_t);
@@ -70,7 +70,7 @@ extern void strbuf_grow(struct strbuf *, size_t);
 static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
 	if (!sb->alloc)
 		strbuf_grow(sb, 0);
-	assert(len < sb->alloc);
+	assert(sb->neglen + len < sb->alloc);
 	sb->len = len;
 	sb->buf[len] = '\0';
 }
@@ -136,4 +136,6 @@ extern int launch_editor(const char *path, struct strbuf *buffer, const char *co
 extern int strbuf_branchname(struct strbuf *sb, const char *name);
 extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
 
+extern void strbuf_offset(struct strbuf *sb, int offset);
+
 #endif /* STRBUF_H */
-- 
1.7.3.3.476.g10a82