aboutsummaryrefslogtreecommitdiff
path: root/vec.c
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-07-23 14:42:48 +0200
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-07-23 14:42:48 +0200
commit034d197e7eb3749f9f5b347595abdcfe2d086965 (patch)
treeba1237cdf7f9a98c39cc79b647baf89221de48be /vec.c
parentb997e31ac151679a516fd76da45bb36931a13e40 (diff)
fix(json): Reduce memory usage and fix memory bugs
Diffstat (limited to 'vec.c')
-rw-r--r--vec.c17
1 files changed, 9 insertions, 8 deletions
diff --git a/vec.c b/vec.c
index e91147e..3dfbf31 100644
--- a/vec.c
+++ b/vec.c
@@ -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; }