aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-10-03 20:21:58 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-10-03 20:21:58 +0200
commit4b3fcb1b0187b55ddaedfc87a39c518fb9cac365 (patch)
tree016346cedc4fabd8315aadfb94700b4bc47ad45c
parent1e3531e5d972231a353ea47a0605a94d6adc2a2f (diff)
Use the bit ceiling to resize when extending arrays
-rw-r--r--include/array.h4
1 files changed, 2 insertions, 2 deletions
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 <stdbit.h>
#include <stddef.h>
#include <string.h>
@@ -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)); \