Merge branch 'jn/strbuf-doc-re-reuse'
authorJunio C Hamano <gitster@pobox.com>
Sat, 7 Oct 2017 07:27:53 +0000 (16:27 +0900)
committerJunio C Hamano <gitster@pobox.com>
Sat, 7 Oct 2017 07:27:53 +0000 (16:27 +0900)
* jn/strbuf-doc-re-reuse:
strbuf doc: reuse after strbuf_release is fine

1  2 
strbuf.h
diff --combined strbuf.h
index 7496cb8ec5a1f8baeda90f8653b2fcbbe2527390,80112a8c264a02caf0ffd813557969249ddf25ac..0a74acb2369ac26cb56b05a953171330c3abe652
+++ b/strbuf.h
@@@ -68,7 -68,7 +68,7 @@@ struct strbuf 
  };
  
  extern char strbuf_slopbuf[];
 -#define STRBUF_INIT  { 0, 0, strbuf_slopbuf }
 +#define STRBUF_INIT  { .alloc = 0, .len = 0, .buf = strbuf_slopbuf }
  
  /**
   * Life Cycle Functions
  extern void strbuf_init(struct strbuf *, size_t);
  
  /**
-  * Release a string buffer and the memory it used. You should not use the
-  * string buffer after using this function, unless you initialize it again.
+  * Release a string buffer and the memory it used. After this call, the
+  * strbuf points to an empty string that does not need to be free()ed, as
+  * if it had been set to `STRBUF_INIT` and never modified.
+  *
+  * To clear a strbuf in preparation for further use without the overhead
+  * of free()ing and malloc()ing again, use strbuf_reset() instead.
   */
  extern void strbuf_release(struct strbuf *);
  
@@@ -91,6 -95,9 +95,9 @@@
   * Detach the string from the strbuf and returns it; you now own the
   * storage the string occupies and it is your responsibility from then on
   * to release it with `free(3)` when you are done with it.
+  *
+  * The strbuf that previously held the string is reset to `STRBUF_INIT` so
+  * it can be reused after calling this function.
   */
  extern char *strbuf_detach(struct strbuf *, size_t *);
  
@@@ -147,10 -154,7 +154,10 @@@ static inline void strbuf_setlen(struc
        if (len > (sb->alloc ? sb->alloc - 1 : 0))
                die("BUG: strbuf_setlen() beyond buffer");
        sb->len = len;
 -      sb->buf[len] = '\0';
 +      if (sb->buf != strbuf_slopbuf)
 +              sb->buf[len] = '\0';
 +      else
 +              assert(!strbuf_slopbuf[0]);
  }
  
  /**