commit 8b59c3bef0ee384f595b9af9bd74fe1b6521843c
parent 8530066451c94cbbb5a5a9046a6e6f2fc0ad3239
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Mon, 13 Dec 2021 20:21:18 -0600
array: add function to add count zero bytes to array
Diffstat:
2 files changed, 26 insertions(+), 0 deletions(-)
diff --git a/array.c b/array.c
@@ -25,3 +25,25 @@ _array_add(void **data, size_t *len, size_t *cap, void *new, size_t size, size_t
return *len;
}
+
+// should only be called when size is 1
+int
+_array_zero(void **data, size_t *len, size_t *cap, size_t count)
+{
+ bool need_realloc;
+ while (*cap < *len + count) {
+ need_realloc = true;
+ *cap = *cap ? *cap * 2 : 1;
+ }
+
+ if (need_realloc) {
+ *data = realloc(*data, *cap);
+ if (*data == NULL)
+ return -1;
+ }
+
+ memset((char *)*data + *len, 0, count);
+ *len += count;
+
+ return *len;
+}
diff --git a/array.h b/array.h
@@ -4,4 +4,8 @@
#define array_push(arr, new, count) \
_array_add((void **) &(arr->data), &(arr->len), &(arr->cap), new, sizeof(*(arr->data)), count);
+#define array_zero(arr, count) \
+ _array_zero((void **) &(arr->data), &(arr->len), &(arr->cap), count);
+
int _array_add(void **data, size_t *len, size_t *cap, void *new, size_t size, size_t count);
+int _array_zero(void **data, size_t *len, size_t *cap, size_t count);