diff options
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 */ |