diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-23 14:42:48 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-23 14:42:48 +0200 |
| commit | 034d197e7eb3749f9f5b347595abdcfe2d086965 (patch) | |
| tree | ba1237cdf7f9a98c39cc79b647baf89221de48be /vec.c | |
| parent | b997e31ac151679a516fd76da45bb36931a13e40 (diff) | |
fix(json): Reduce memory usage and fix memory bugs
Diffstat (limited to 'vec.c')
| -rw-r--r-- | vec.c | 17 |
1 files changed, 9 insertions, 8 deletions
@@ -4,14 +4,14 @@ #include "types.h" #include "vec.h" -Vec *_Vec_new(Arena *a, size tsize, size cap, size align) { +Vec _Vec_new(Arena *a, size tsize, size cap, size align) { assert(cap > 0); - Vec *v = new(a, Vec, 1); - v->tsize = tsize; - v->cap = cap; - v->align = align; - v->len = 0; - v->data = alloc(a, cap, tsize, align); + Vec v = {0}; + v.tsize = tsize; + v.cap = cap; + v.align = align; + v.len = 0; + v.data = alloc(a, cap, tsize, align); return v; } @@ -20,9 +20,10 @@ void Vec_push(Arena *a, Vec *v, void *data) { 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++; } -void *_Vec_get(Vec *v, size pos) { return v->data + v->tsize * pos; } +void *_Vec_get(Vec v, size pos) { return v.data + v.tsize * pos; } |
