aboutsummaryrefslogtreecommitdiff
path: root/vec.c
diff options
context:
space:
mode:
authorMartin Kagamino Lehoux <martin@lehoux.net>2025-01-31 08:02:09 +0100
committerMartin Kagamino Lehoux <martin@lehoux.net>2025-01-31 08:02:09 +0100
commit8a9e6b1bd3be1041792311a86d8aac68b659e891 (patch)
tree2a62729da17b2e6669c3d71a162525ff37e42c6a /vec.c
feat: First version, failing json parse string
Diffstat (limited to 'vec.c')
-rw-r--r--vec.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/vec.c b/vec.c
new file mode 100644
index 0000000..6962d03
--- /dev/null
+++ b/vec.c
@@ -0,0 +1,34 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "vec.h"
+
+Vec *_Vec_new(Arena *a, int size, ptrdiff_t cap, int align) {
+ Vec *v = new (a, Vec, 1);
+ v->data = alloc(a, cap, size, align);
+ v->size = size;
+ v->align = align;
+ v->cap = cap;
+ v->len = 0;
+ return v;
+}
+
+Vec *Vec_push(Arena *a, Vec *v, void *data) {
+ // printf("push: dest=%p, src=%p, size=%d\n", v->data + v->len * v->size,
+ // data,
+ // v->size);
+ if (v->len == v->cap) {
+ void *cpy = alloc(a, v->cap * 2, v->size, v->align);
+ memcpy(cpy, v->data, v->len * v->size);
+ v->cap *= 2;
+ }
+ memcpy(v->data + v->len * v->size, data, v->size);
+ v->len++;
+ return v;
+}
+
+void *_Vec_get(Vec *v, int pos) {
+ // printf("get: length=%lu, cap=%lu, pos=%d\n", v->len, v->cap, pos);
+ // printf("get: start=%p, asked=%p\n", v->data, v->data + v->size * pos);
+ return v->data + v->size * pos;
+}