#include "vec.h" #include "utils.h" #include #include 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; }