Browse Source Download (without any required ccan dependencies)

Module:

membuf

Summary:

simple linear memory buffer routines.

Author:

Rusty Russell <rusty@rustcorp.com.au>

Dependencies:

Description:

It's common to want a linear memory buffer, where you can get memory on the end, and consume memory from the start. The details of actually when to enlarge or move the buffer are slightly nontrivial, so they're encapsulated here.

Example:

#include <ccan/membuf/membuf.h>
#include <string.h>
#include <stdio.h>

// Given "hello world" outputs helloworld
// Given "hello there world" outputs hellothereworld
int main(int argc, char *argv[])
{
     MEMBUF(char) charbuf;

     membuf_init(&charbuf, malloc(10), 10, membuf_realloc);

     for (int i = 1; i < argc; i++)
             strcpy(membuf_add(&charbuf, strlen(argv[i])), argv[i]);

     // This is dumb, we could do all at once, but shows technique.
     while (membuf_num_elems(&charbuf) > 0)
             printf("%c", *(char *)membuf_consume(&charbuf, 1));
     printf("\n");
     return 0;
}

License:

BSD-MIT