diff options
Diffstat (limited to 'shared/scratchbuf.h')
-rw-r--r-- | shared/scratchbuf.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/shared/scratchbuf.h b/shared/scratchbuf.h new file mode 100644 index 0000000..27ea9d9 --- /dev/null +++ b/shared/scratchbuf.h @@ -0,0 +1,31 @@ +#pragma once + +#include <stdbool.h> +#include <stdlib.h> + +#include <shared/macro.h> + +/* + * Buffer abstract data type + */ +struct scratchbuf { + char *bytes; + size_t size; + bool need_free; +}; + +void scratchbuf_init(struct scratchbuf *buf, char *stackbuf, size_t size); +int scratchbuf_alloc(struct scratchbuf *buf, size_t sz); +void scratchbuf_release(struct scratchbuf *buf); + +/* Return a C string */ +static inline char *scratchbuf_str(struct scratchbuf *buf) +{ + return buf->bytes; +} + +#define SCRATCHBUF_INITIALIZER(buf_) { \ + .bytes = buf_, \ + .size = sizeof(buf_) + _array_size_chk(buf_), \ + .need_free = false, \ +} |