diff options
author | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-06-28 18:32:56 +0300 |
---|---|---|
committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-06-28 18:38:43 +0300 |
commit | 2fc4f728643f78577c26e548619bc7db90a7c6ae (patch) | |
tree | 760d2ae310737b0ff39e37aa896d182f7df58c51 /src/vec.h | |
parent | d65c2461d26bf485b88276984bbc33054d5bce20 (diff) | |
download | ejit-2fc4f728643f78577c26e548619bc7db90a7c6ae.tar.gz ejit-2fc4f728643f78577c26e548619bc7db90a7c6ae.zip |
implement enough for fibonacci
Diffstat (limited to 'src/vec.h')
-rw-r--r-- | src/vec.h | 17 |
1 files changed, 13 insertions, 4 deletions
@@ -33,11 +33,12 @@ struct vec { static inline struct vec vec_create(size_t ns) { + const size_t s = 8; return (struct vec) { .n = 0, - .s = 1, + .s = s, .ns = ns, - .buf = malloc(ns), + .buf = malloc(s * ns), }; } @@ -98,8 +99,16 @@ static inline void vec_reserve(struct vec *v, size_t n) return; v->n = n; - v->s = n; - v->buf = realloc(v->buf, v->s * v->ns); + if (v->s < v->n) { + v->s *= 2; + v->buf = realloc(v->buf, v->s * v->ns); + } +} + +static inline void vec_shrink(struct vec *v, size_t n) +{ + assert(v->n >= n); + v->n = n; } #endif /* VEC_H */ |