From 4b3fcb1b0187b55ddaedfc87a39c518fb9cac365 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 3 Oct 2024 20:21:58 +0200 Subject: Use the bit ceiling to resize when extending arrays --- include/array.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/array.h b/include/array.h index de45a65..11d8180 100644 --- a/include/array.h +++ b/include/array.h @@ -1,6 +1,7 @@ #ifndef MLIB_ARRAY_H #define MLIB_ARRAY_H +#include #include #include @@ -41,12 +42,11 @@ _mlib_array_hdr(void *p, ptrdiff_t align) (*(p))[hdr->len++] = (x); \ } while (false) -/* TODO: Make the resizing not bad */ #define array_extend(p, xs, n) \ do { \ _mlib_arr_hdr_t *hdr = _mlib_array_hdr((p), alignof(typeof(*(p)))); \ if (hdr->len + (n) <= hdr->cap) { \ - (p) = array_resz((p), hdr->len * 2 + (n)); \ + (p) = array_resz((p), stdc_bit_ceil((size_t)hdr->len + (n))); \ hdr = _mlib_array_hdr((p), alignof(typeof(*(p)))); \ } \ memcpy(&(p)[hdr->len], (xs), (n) * sizeof(*xs)); \ -- cgit v1.2.3