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 * strcpy(membuf_add(&charbuf, strlen(argv[i])), argv[i]);
32 * // This is dumb, we could do all at once, but shows technique.
33 * while (membuf_num_elems(&charbuf) > 0)
34 * printf("%c", *(char *)membuf_consume(&charbuf, 1));
39 int main(int argc, char *argv[])
41 /* Expect exactly one argument */
45 if (strcmp(argv[1], "depends") == 0) {
46 printf("ccan/tcon\n");