Add strbuf_malloc() macro

This commit is contained in:
Andrew Bettison 2015-11-17 00:05:01 +10:30
parent 45b7d4f4c5
commit 8310d3cf19

View File

@ -131,6 +131,25 @@ typedef const struct strbuf *const_strbuf;
*/ */
#define SIZEOF_STRBUF (sizeof(struct strbuf)) #define SIZEOF_STRBUF (sizeof(struct strbuf))
/** Convenience macro for allocating a strbuf and its backing buffer on the
* heap using a single call to malloc(3).
*
* strbuf func1() {
* strbuf b = strbuf_malloc(1024);
* strbuf_puts(b, "some text");
* strbuf_puts(b, " some more text");
* return b;
* }
* strbuf func2() {
* strbuf b = func1();
* printf("%s\n", strbuf_str(b));
* free(b);
* }
*
* @author Andrew Bettison <andrew@servalproject.com>
*/
#define strbuf_malloc(size) strbuf_make(malloc(SIZEOF_STRBUF + (size)), SIZEOF_STRBUF + (size))
/** Convenience macro for allocating a strbuf and its backing buffer on the /** Convenience macro for allocating a strbuf and its backing buffer on the
* stack within the calling function. The returned strbuf is only valid for * stack within the calling function. The returned strbuf is only valid for
* the duration of the function, so it must not be returned. See alloca(3) for * the duration of the function, so it must not be returned. See alloca(3) for