blob: 27ea9d9f6008e4ce6ec6098b5d49f3267c02b0ae [file] [log] [blame]
Lucas De Marchi780a4e92016-08-10 14:20:32 -03001#pragma once
2
3#include <stdbool.h>
4#include <stdlib.h>
5
6#include <shared/macro.h>
7
8/*
9 * Buffer abstract data type
10 */
11struct scratchbuf {
12 char *bytes;
13 size_t size;
14 bool need_free;
15};
16
17void scratchbuf_init(struct scratchbuf *buf, char *stackbuf, size_t size);
18int scratchbuf_alloc(struct scratchbuf *buf, size_t sz);
19void scratchbuf_release(struct scratchbuf *buf);
20
21/* Return a C string */
Yauheni Kaliutab34819b2016-11-09 08:52:26 +020022static inline char *scratchbuf_str(struct scratchbuf *buf)
Lucas De Marchi780a4e92016-08-10 14:20:32 -030023{
24 return buf->bytes;
25}
26
27#define SCRATCHBUF_INITIALIZER(buf_) { \
28 .bytes = buf_, \
29 .size = sizeof(buf_) + _array_size_chk(buf_), \
30 .need_free = false, \
31}