Skip to content
A transparent buffer implementation for C
C Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Lightweight buffer library


buf.c lets you use regular pointers as expandable buffers. It uses an approach, inspired by sds, of storing metadata before the pointer value.


Declare a buffer by declaring a pointer to an item type. For example, a char * can be a buffer, or a struct foo *. Below, TYPE stands for the type that you declared for the buffer.

Initialize an empty buffer by setting it to NULL.

void buf_truncate(TYPE *);

Set the length of the buffer to zero, but don't free the buffer's memory.

void buf_free(TYPE **);

Release allocated memory for the buffer, and mark it as empty buffer (set to NULL).

size_t buf_len(TYPE *);

Get the length of the data in the buffer.

bool buf_alloc(TYPE **, size_t size);

Ensure that the buffer has enough space for some data. Return true for success, false for failure.

bool buf_put(TYPE **, const void *data, size_t size);

Put some data in the buffer. Return true on success, false on failure.

bool buf_append(TYPE **, const void *data, size_t size);

Write the given data at the end of the buffer, expanding the buffer if necessary. Return true on success, false on failure.

Similar projects

stb's stretchy_buffer does the same thing



You can’t perform that action at this time.