diff options
Diffstat (limited to 'src/std/vec.c')
| -rw-r--r-- | src/std/vec.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/std/vec.c b/src/std/vec.c new file mode 100644 index 0000000..077e64a --- /dev/null +++ b/src/std/vec.c @@ -0,0 +1,27 @@ +#include <string.h> + +#include "assert.h" +#include "types.h" +#include "vec.h" + +Vec _Vec_new(Arena *a, size tsize, size cap, size align) { + assert(cap > 0, "negative cap"); + Vec v = {0}; + v.tsize = tsize; + v.cap = cap; + v.align = align; + v.len = 0; + v.data = alloc(a, cap, tsize, align); + return v; +} + +void Vec_push(Arena *a, Vec *v, void *data) { + if (v->len == v->cap) { + void *cpy = alloc(a, v->cap * 2, v->tsize, v->align); + memcpy(cpy, v->data, v->len * v->tsize); + v->cap *= 2; + v->data = cpy; + } + memcpy(v->data + v->len * v->tsize, data, v->tsize); + v->len++; +} |
