From 587ccc983cf36fb9e00aa10ac32b58d0205aa96a Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 11 May 2024 01:58:35 +0200 Subject: Go back to macros only, but they work now! --- include/dynarr.h | 47 ++++++++++++++++++++++++++--------------------- lib/dynarr/daextend.c | 22 ---------------------- lib/dynarr/dagrow.c | 17 ----------------- 3 files changed, 26 insertions(+), 60 deletions(-) delete mode 100644 lib/dynarr/daextend.c delete mode 100644 lib/dynarr/dagrow.c diff --git a/include/dynarr.h b/include/dynarr.h index 31d1e10..09dd41b 100644 --- a/include/dynarr.h +++ b/include/dynarr.h @@ -1,6 +1,7 @@ #ifndef MLIB_DYNARR_H #define MLIB_DYNARR_H +#include #include #include "_alloc_fn.h" @@ -14,31 +15,35 @@ void *ctx; \ } -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), \ - alignof(x))) - -#define daextend(da, xs, n) \ - ((typeof((da)->buf))daextend((da), (xs), (n), sizeof(*(xs)), \ - alignof(*(xs)))) - -#define dagrow(da, n) \ - ((typeof((da)->buf))dagrow((da), (n), sizeof(*(da)->buf), \ - alignof(*(da)->buf))) +#define DAPUSH(da, x) \ + do { \ + if (++(da)->len > (da)->cap) { \ + size_t ncap = stdc_bit_ceil((da)->len); \ + (da)->buf = (da)->alloc((da)->ctx, (da)->buf, (da)->cap, ncap, \ + sizeof(*(da)->buf), alignof(*(da)->buf)); \ + (da)->cap = ncap; \ + } \ + (da)->buf[(da)->len - 1] = (x); \ + } while (false) #define DAEXTEND(da, xs, n) \ do { \ - if ((da)->len + (n) >= (da)->cap) { \ - do \ - (da)->cap = (da)->cap ? (da)->cap * 2 : 1; \ - while ((da)->len + (n) >= (da)->cap); \ - (da)->buf = bufalloc((da)->buf, (da)->cap, sizeof(*(da)->buf)); \ + if (((da)->len += (n)) > (da)->cap) { \ + size_t ncap = stdc_bit_ceil((da)->len); \ + (da)->buf = (da)->alloc((da)->ctx, (da)->buf, (da)->cap, ncap, \ + sizeof(*(da)->buf), alignof(*(da)->buf)); \ + (da)->cap = ncap; \ + } \ + memcpy((da)->buf + (da)->len - (n), (xs), (n) * sizeof(*(da)->buf)); \ + } while (false) + +#define DAGROW(da, n) \ + do { \ + if ((n) > (da)->cap) { \ + (da)->buf = (da)->alloc((da)->ctx, (da)->buf, (da)->cap, (n), \ + sizeof(*(da)->buf), alignof(*(da)->buf)); \ + (da)->cap = (n); \ } \ - memcpy((da)->buf + (da)->len, (xs), (n)); \ - (da)->len += (n); \ } while (false) #define DAPOP(da) ((da)->buf[--(da)->len]) diff --git a/lib/dynarr/daextend.c b/lib/dynarr/daextend.c deleted file mode 100644 index efa96d5..0000000 --- a/lib/dynarr/daextend.c +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -#include -#include - -#include "dynarr.h" - -void * -(daextend)(void *da, void *xs, size_t n, size_t sz, size_t align) -{ - dynarr(uint8_t) cpy; - memcpy(&cpy, da, sizeof(cpy)); - - if ((cpy.len += n) > cpy.cap) { - size_t ncap = stdc_bit_ceil(cpy.len); - cpy.buf = cpy.alloc(cpy.ctx, cpy.buf, cpy.cap, ncap, sz, align); - cpy.cap = ncap; - } - - memcpy(cpy.buf + cpy.len * sz - n * sz, xs, n * sz); - return memcpy(da, &cpy, sizeof(cpy)); -} diff --git a/lib/dynarr/dagrow.c b/lib/dynarr/dagrow.c deleted file mode 100644 index 8273efe..0000000 --- a/lib/dynarr/dagrow.c +++ /dev/null @@ -1,17 +0,0 @@ -#include - -#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)); -} -- cgit v1.2.3