aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2017-01-23 22:47:20 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2017-01-23 22:47:39 +0100
commit973a4b765629a85d16f438f6832b1a2bb54af7e7 (patch)
tree4ba235666b32889c731c1fb2a2440f6a90326895
parent74b00a921a214a7ea097fd97a239fde7f25aae48 (diff)
downloadliberty-973a4b765629a85d16f438f6832b1a2bb54af7e7.tar.gz
liberty-973a4b765629a85d16f438f6832b1a2bb54af7e7.tar.xz
liberty-973a4b765629a85d16f438f6832b1a2bb54af7e7.zip
Add ARRAY convenience macros
Because dynamically allocated arrays in C are a pain.
-rw-r--r--liberty.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/liberty.c b/liberty.c
index 0e19c31..c564bfd 100644
--- a/liberty.c
+++ b/liberty.c
@@ -301,6 +301,23 @@ xstrndup (const char *s, size_t n)
return copy;
}
+// --- Simple array support ----------------------------------------------------
+
+// The most basic helper macros to make working with arrays not suck
+
+#define ARRAY(type, name) type *name; size_t name ## _len, name ## _size;
+#define ARRAY_INIT_SIZED(a, n) \
+ BLOCK_START \
+ (a) = xcalloc (sizeof *(a), (a ## _size) = (n)); \
+ (a ## _len) = 0; \
+ BLOCK_END
+#define ARRAY_INIT(a) ARRAY_INIT_SIZED (a, 16)
+#define ARRAY_RESERVE(a, n) \
+ BLOCK_START \
+ while ((a ## _size) - (a ## _len) < n) \
+ (a) = xreallocarray ((a), sizeof *(a), (a ## _size) <<= 1); \
+ BLOCK_END
+
// --- Double-linked list helpers ----------------------------------------------
#define LIST_HEADER(type) \