ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | Submodules | LICENSE

Commit: 2a81d093bcb14cd758e890f672ec2cfc47bb3533
Parent: 6ea31f2e4d09488ecd8af6f277356327a2ecb233
Author: Randy Palamar
Date:   Wed,  2 Apr 2025 05:56:08 -0600

util: rework da_push_ as da_reserve allowing for bulk reservations

Diffstat:
Mutil.c | 22++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/util.c b/util.c @@ -74,27 +74,33 @@ alloc_(Arena *a, iz len, iz align, iz count) } enum { DA_INITIAL_CAP = 8 }; +#define da_reserve(a, s, n) \ + (s)->data = da_reserve_((a), (s)->data, &(s)->capacity, (s)->count + n, \ + _Alignof(typeof(*(s)->data)), sizeof(*(s)->data)) #define da_push(a, s) \ - ((s)->count == (s)->capacity \ - ? (s)->data = da_push_((a), (s)->data, &(s)->capacity, \ - _Alignof(typeof(*(s)->data)), sizeof(*(s)->data)), \ - (s)->data + (s)->count++ \ + ((s)->count == (s)->capacity \ + ? da_reserve(a, s, 1), \ + (s)->data + (s)->count++ \ : (s)->data + (s)->count++) static void * -da_push_(Arena *a, void *data, iz *capacity, iz align, iz size) +da_reserve_(Arena *a, void *data, iz *capacity, iz needed, iz align, iz size) { iz cap = *capacity; + + /* NOTE(rnp): handle both 0 initialized DAs and DAs that need to be moved (they started + * on the stack or someone allocated something in the middle of the arena during usage) */ if (!data || a->beg != (u8 *)data + cap * size) { void *copy = alloc_(a, size, align, cap); if (data) mem_copy(copy, data, cap * size); data = copy; } - iz extend = cap ? cap : DA_INITIAL_CAP; - alloc_(a, size, align, extend); - *capacity = cap + extend; + if (!cap) cap = DA_INITIAL_CAP; + while (cap < needed) cap *= 2; + alloc_(a, size, align, cap - *capacity); + *capacity = cap; return data; }