1 #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 */ 11 struct scratchbuf { 12 char *bytes; 13 size_t size; 14 bool need_free; 15 }; 16 17 void scratchbuf_init(struct scratchbuf *buf, char *stackbuf, size_t size); 18 int scratchbuf_alloc(struct scratchbuf *buf, size_t sz); 19 void scratchbuf_release(struct scratchbuf *buf); 20 21 /* Return a C string */ 22 static inline char *scratchbuf_str(struct scratchbuf *buf) 23 { 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 } 32