From b04989164df973eaf6565d1c9bf2505215a54604 Mon Sep 17 00:00:00 2001
From: Thomas Voss <mail@thomasvoss.com>
Date: Fri, 10 May 2024 23:47:31 +0200
Subject: Add daextend() to the new dynarr system

---
 include/dynarr.h      | 21 ++++++++-------------
 lib/dynarr/daextend.c | 22 ++++++++++++++++++++++
 lib/dynarr/dapush.c   | 22 ----------------------
 3 files changed, 30 insertions(+), 35 deletions(-)
 create mode 100644 lib/dynarr/daextend.c
 delete mode 100644 lib/dynarr/dapush.c

diff --git a/include/dynarr.h b/include/dynarr.h
index 52cacc5..0a7e2c6 100644
--- a/include/dynarr.h
+++ b/include/dynarr.h
@@ -14,11 +14,15 @@
 		void *ctx;                                                             \
 	}
 
-void *dapush(void *, void *, size_t, size_t);
+void *daextend(void *, void *, size_t, size_t, size_t);
 
-#define dapush(da, ...)                                                        \
-	dapush((da), ((typeof(__VA_ARGS__)[1]){__VA_ARGS__}), sizeof(__VA_ARGS__), \
-	       alignof(__VA_ARGS__))
+#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)                                                          \
 	do {                                                                       \
@@ -28,15 +32,6 @@ void *dapush(void *, void *, size_t, size_t);
 		}                                                                      \
 	} while (false)
 
-#define DAPUSH(da, x)                                                          \
-	do {                                                                       \
-		if ((da)->len >= (da)->cap) {                                          \
-			(da)->cap = (da)->cap ? (da)->cap * 2 : 1;                         \
-			(da)->buf = bufalloc((da)->buf, (da)->cap, sizeof(*(da)->buf));    \
-		}                                                                      \
-		(da)->buf[(da)->len++] = (x);                                          \
-	} while (false)
-
 #define DAEXTEND(da, xs, n)                                                    \
 	do {                                                                       \
 		if ((da)->len + (n) >= (da)->cap) {                                    \
diff --git a/lib/dynarr/daextend.c b/lib/dynarr/daextend.c
new file mode 100644
index 0000000..efa96d5
--- /dev/null
+++ b/lib/dynarr/daextend.c
@@ -0,0 +1,22 @@
+#include <errno.h>
+#include <stdbit.h>
+#include <stdint.h>
+#include <string.h>
+
+#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/dapush.c b/lib/dynarr/dapush.c
deleted file mode 100644
index 11fde1b..0000000
--- a/lib/dynarr/dapush.c
+++ /dev/null
@@ -1,22 +0,0 @@
-#include <errno.h>
-#include <stdbit.h>
-#include <stdint.h>
-#include <string.h>
-
-#include "dynarr.h"
-
-void *
-(dapush)(void *da, void *x, size_t sz, size_t align)
-{
-	dynarr(uint8_t) cpy;
-	memcpy(&cpy, da, sizeof(cpy));
-
-	if (++cpy.len > 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 - sz, x, sz);
-	return memcpy(da, &cpy, sizeof(cpy));
-}
-- 
cgit v1.2.3