* - darr_ensure_i
* - darr_ensure_i_mt
* - darr_free
+ * - darr_free_free
+ * - darr_free_func
* - darr_insert
* - darr_insert_mt
* - darr_insertz
} \
} while (0)
+/**
+ * Free memory allocated for the dynamic array `A`, calling `darr_free` for
+ * each element of the array first.
+ *
+ * Args:
+ * A: The dynamic array, can be NULL.
+ */
+#define darr_free_free(A) \
+ do { \
+ for (uint __i = 0; __i < darr_len(A); __i++) \
+ if ((A)[__i]) { \
+ struct darr_metadata *__meta = \
+ _darr_meta((A)[__i]); \
+ XFREE(__meta->mtype, __meta); \
+ } \
+ darr_free(A); \
+ } while (0)
+
+/**
+ * Free memory allocated for the dynamic array `A`, calling `F` routine
+ * for each element of the array first.
+ *
+ * Args:
+ * A: The dynamic array, can be NULL.
+ * F: The function to call for each element.
+ */
+
+#define darr_free_func(A, F) \
+ do { \
+ for (uint __i = 0; __i < darr_len(A); __i++) { \
+ F((A)[__i]); \
+ } \
+ darr_free(A); \
+ } while (0)
+
/**
* Make sure that there is room in the dynamic array `A` to add `C` elements.
*
* [x] - darr_foreach_i
* [x] - darr_foreach_p
* [x] - darr_free
+ * [x] - darr_free_free
+ * [x] - darr_free_func
* [x] - darr_insert
* [ ] - darr_insertz
* [x] - darr_insert_n
uint addlen = strlen(add);
char *da1 = NULL;
char *da2 = NULL;
+ const char **strings = NULL;
+ uint sum = 0;
assert(darr_strlen(da1) == 0);
da1 = darr_in_strcatf(da1, "0123456789: %08x", 0xDEADBEEF);
assert(!strcmp("0123456789: deadbeef", da1));
darr_free(da1);
+
+ sum = 0;
+ *darr_append(strings) = "1";
+ *darr_append(strings) = "2";
+ *darr_append(strings) = "3";
+#define adder(x) (sum += atoi(x))
+ darr_free_func(strings, adder);
+ assert(sum == 6);
+ assert(strings == NULL);
+
+ sum = 0;
+ darr_free_func(strings, adder);
+ assert(sum == 0);
+ assert(strings == NULL);
+
+ *darr_append(strings) = NULL;
+ *darr_append(strings) = darr_strdup("2");
+ *darr_append(strings) = darr_strdup("3");
+ darr_free_free(strings);
+ assert(strings == NULL);
+
+ darr_free_free(strings);
+ assert(strings == NULL);
}
int main(int argc, char **argv)