summaryrefslogtreecommitdiff
path: root/src/vec.cc
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-02-10 09:32:35 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-02-10 09:32:35 +0100
commitccab86b21825ebc41f0887ea633ea28906b32a8e (patch)
tree60f378c3629c139f412d41a85142d1b48518e54f /src/vec.cc
parent86c09e193ed66420da48a7fd62678286b89dace2 (diff)
adding vector and eventloop implementation
Diffstat (limited to 'src/vec.cc')
-rw-r--r--src/vec.cc83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/vec.cc b/src/vec.cc
new file mode 100644
index 0000000..d43bc52
--- /dev/null
+++ b/src/vec.cc
@@ -0,0 +1,83 @@
+#include "vec.h"
+#include "utils.h"
+#include <cstring>
+#include <mimalloc.h>
+
+static inline usize wayc_raw_vec_next_cap(usize curr_cap, usize grow_to) {
+ if (curr_cap == 0)
+ return WAYC_VEC_INITIAL;
+
+ usize next_cap = curr_cap * WAYC_VEC_GROWTH;
+ if (next_cap < grow_to)
+ return grow_to;
+
+ return next_cap;
+}
+
+static inline bool wayc_raw_vec_needs_grow(raw_vec_s *vec) {
+ wayc_notnull(vec);
+ wayc_assert(vec->len <= vec->cap);
+ return vec->len == vec->cap;
+}
+
+static inline usize wayc_raw_vec_bytes(usize size, usize value) {
+ return value * size;
+}
+
+static inline u8 *wayc_raw_vec_at(raw_vec_s *vec, usize index) {
+ wayc_notnull(vec);
+ u8 *at = vec->ptr + wayc_raw_vec_bytes(vec->size, index);
+ return at;
+}
+
+static void wayc_raw_vec_grow(raw_vec_s *vec, usize grow_to) {
+ wayc_notnull(vec);
+ wayc_assert(wayc_raw_vec_needs_grow(vec));
+
+ usize curr_bytes = wayc_raw_vec_bytes(vec->size, vec->cap);
+ usize next_cap = wayc_raw_vec_next_cap(vec->cap, grow_to);
+ usize next_bytes = wayc_raw_vec_bytes(vec->size, next_cap);
+
+ u8 *next_ptr = (u8 *)mi_malloc(next_bytes);
+ memcpy(next_ptr, vec->ptr, curr_bytes);
+ mi_free(vec->ptr);
+
+ vec->ptr = next_ptr;
+ vec->cap = next_cap;
+}
+
+void wayc_raw_vec_push(raw_vec_s *vec, const u8 *at) {
+ wayc_notnull(vec);
+ wayc_notnull(at);
+
+ if (wayc_raw_vec_needs_grow(vec))
+ wayc_raw_vec_grow(vec, vec->len + 1);
+
+ u8 *dst = wayc_raw_vec_at(vec, vec->len);
+ memcpy(dst, at, vec->size);
+ vec->len++;
+}
+
+bool wayc_raw_vec_pop(raw_vec_s *vec, u8 *out) {
+ wayc_notnull(vec);
+ wayc_notnull(out);
+
+ if (vec->len == 0)
+ return false;
+
+ u8 *at = wayc_raw_vec_at(vec, vec->len - 1);
+ memcpy(out, at, vec->size);
+ vec->len--;
+ return true;
+}
+
+void wayc_raw_vec_deinit(raw_vec_s *vec) {
+ wayc_notnull(vec);
+ if (vec->ptr == nullptr)
+ return;
+
+ mi_free(vec->ptr);
+ vec->ptr = nullptr;
+ vec->len = 0;
+ vec->cap = 0;
+} \ No newline at end of file