Lucas De Marchi | 780a4e9 | 2016-08-10 14:20:32 -0300 | [diff] [blame] | 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 */ |
Yauheni Kaliuta | b34819b | 2016-11-09 08:52:26 +0200 | [diff] [blame^] | 22 | static inline char *scratchbuf_str(struct scratchbuf *buf) |
Lucas De Marchi | 780a4e9 | 2016-08-10 14:20:32 -0300 | [diff] [blame] | 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 | } |