path: root/strbuf.h
diff options
authorPierre Habouzit <>2007-09-27 10:58:23 (GMT)
committerJunio C Hamano <>2007-09-29 09:13:33 (GMT)
commitb315c5c08139c0d3c1e4867a305334e29da01d07 (patch)
treefd4b122c7dd87e06a642a191b678b426d35e5a5b /strbuf.h
parent690b61f5f13db05aa4ad0efc422bd01aef3c1367 (diff)
strbuf change: be sure ->buf is never ever NULL.
For that purpose, the ->buf is always initialized with a char * buf living in the strbuf module. It is made a char * so that we can sloppily accept things that perform: sb->buf[0] = '\0', and because you can't pass "" as an initializer for ->buf without making gcc unhappy for very good reasons. strbuf_init/_detach/_grow have been fixed to trust ->alloc and not ->buf anymore. as a consequence strbuf_detach is _mandatory_ to detach a buffer, copying ->buf isn't an option anymore, if ->buf is going to escape from the scope, and eventually be free'd. API changes: * strbuf_setlen now always works, so just make strbuf_reset a convenience macro. * strbuf_detatch takes a size_t* optional argument (meaning it can be NULL) to copy the buffer's len, as it was needed for this refactor to make the code more readable, and working like the callers. Signed-off-by: Pierre Habouzit <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'strbuf.h')
1 files changed, 5 insertions, 5 deletions
diff --git a/strbuf.h b/strbuf.h
index 5657e3d..6deec78 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -10,8 +10,7 @@
* 1. the ->buf member is always malloc-ed, hence strbuf's can be used to
* build complex strings/buffers whose final size isn't easily known.
- * It is legal to copy the ->buf pointer away. Though if you want to reuse
- * the strbuf after that, setting ->buf to NULL isn't legal.
+ * It is NOT legal to copy the ->buf pointer away.
* `strbuf_detach' is the operation that detachs a buffer from its shell
* while keeping the shell valid wrt its invariants.
@@ -41,19 +40,19 @@
#include <assert.h>
+extern char strbuf_slopbuf[];
struct strbuf {
size_t alloc;
size_t len;
char *buf;
-#define STRBUF_INIT { 0, 0, NULL }
+#define STRBUF_INIT { 0, 0, strbuf_slopbuf }
/*----- strbuf life cycle -----*/
extern void strbuf_init(struct strbuf *, size_t);
extern void strbuf_release(struct strbuf *);
-extern void strbuf_reset(struct strbuf *);
-extern char *strbuf_detach(struct strbuf *);
+extern char *strbuf_detach(struct strbuf *, size_t *);
extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
struct strbuf tmp = *a;
@@ -75,6 +74,7 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
sb->len = len;
sb->buf[len] = '\0';
+#define strbuf_reset(sb) strbuf_setlen(sb, 0)
/*----- content related -----*/
extern void strbuf_rtrim(struct strbuf *);