diff options
-rw-r--r-- | include/dynarr.h | 11 | ||||
-rw-r--r-- | lib/dynarr/dagrow.c | 17 |
2 files changed, 21 insertions, 7 deletions
diff --git a/include/dynarr.h b/include/dynarr.h index 0a7e2c6..31d1e10 100644 --- a/include/dynarr.h +++ b/include/dynarr.h @@ -15,6 +15,7 @@ } void *daextend(void *, void *, size_t, size_t, size_t); +void *dagrow(void *, size_t, size_t, size_t); #define dapush(da, x) \ ((typeof((da)->buf))(daextend)((da), (typeof(x)[1]){(x)}, 1, sizeof(x), \ @@ -24,13 +25,9 @@ void *daextend(void *, void *, size_t, size_t, size_t); ((typeof((da)->buf))daextend((da), (xs), (n), sizeof(*(xs)), \ alignof(*(xs)))) -#define DAGROW(da, n) \ - do { \ - if ((n) > (a)->cap) { \ - (a)->cap = (n); \ - (a)->buf = bufalloc((a)->buf, (a)->cap, sizeof(*(a)->buf)); \ - } \ - } while (false) +#define dagrow(da, n) \ + ((typeof((da)->buf))dagrow((da), (n), sizeof(*(da)->buf), \ + alignof(*(da)->buf))) #define DAEXTEND(da, xs, n) \ do { \ diff --git a/lib/dynarr/dagrow.c b/lib/dynarr/dagrow.c new file mode 100644 index 0000000..8273efe --- /dev/null +++ b/lib/dynarr/dagrow.c @@ -0,0 +1,17 @@ +#include <stdint.h> + +#include "dynarr.h" + +void * +(dagrow)(void *da, size_t n, size_t elemsz, size_t align) +{ + dynarr(uint8_t) cpy; + memcpy(&cpy, da, sizeof(cpy)); + + if (n > cpy.cap) { + cpy.buf = cpy.alloc(cpy.ctx, cpy.buf, cpy.cap, n, elemsz, align); + cpy.cap = n; + } + + return memcpy(da, &cpy, sizeof(cpy)); +} |