blob: dd93116bb69f7b52f02c847eb618ad274344114b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#include <stdlib.h>
#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);
}
|