6 * membuf - simple linear memory buffer routines.
8 * It's common to want a linear memory buffer, where you can get memory on
9 * the end, and consume memory from the start. The details of actually
10 * when to enlarge or move the buffer are slightly nontrivial, so they're
14 * Author: Rusty Russell <rusty@rustcorp.com.au>
17 * #include <ccan/membuf/membuf.h>
21 * // Given "hello world" outputs helloworld
22 * // Given "hello there world" outputs hellothereworld
23 * int main(int argc, char *argv[])
25 * MEMBUF(char) charbuf;
27 * membuf_init(&charbuf, malloc(10), 10, membuf_realloc);
29 * for (int i = 1; i < argc; i++) {
30 * size_t len = strlen(argv[i]);
31 * memcpy(membuf_add(&charbuf, len), argv[i], len);
34 * // This is dumb, we could do all at once, but shows technique.
35 * while (membuf_num_elems(&charbuf) > 0)
36 * printf("%c", *(char *)membuf_consume(&charbuf, 1));
38 * free(membuf_cleanup(&charbuf));
42 int main(int argc, char *argv[])
44 /* Expect exactly one argument */
48 if (strcmp(argv[1], "depends") == 0) {
49 printf("ccan/tcon\n");