From 610808a5902adad751a4acdbcc310803a51fed5d Mon Sep 17 00:00:00 2001 From: wrmr Date: Sat, 2 Nov 2024 19:33:08 -0500 Subject: very different document data structure --- buf.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 buf.c (limited to 'buf.c') diff --git a/buf.c b/buf.c new file mode 100644 index 0000000..dd93116 --- /dev/null +++ b/buf.c @@ -0,0 +1,28 @@ +#include +#include "buf.h" +#include "err.h" + +void buf_init(struct buf *b, size_t n) { + b->buf = calloc(1, n); + if (!b->buf) { + efatal("buf_init"); + } + b->cap = n; + b->sz = 0; +} + +void buf_grow(struct buf *b, size_t n) { + size_t sz = b->sz + n; + size_t c = b->cap; + if (sz > c) { + while (sz > c) c <<= 1; + char *p = realloc(b->buf, c); + if (!p) efatal("buf_grow"); + b->buf = p; + b->cap = c; + } +} + +void buf_free(struct buf *b) { + free(b->buf); +} -- cgit 1.4.1-2-gfad0