aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-02-13 17:21:56 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-02-13 17:22:26 +0100
commit15e93b55266186c159688e3b54bead26da206ee2 (patch)
treeea80ee4419f9c19a14fc016c29beff38f4698896 /src/common
parent5a419deabba93d9fea3f6af2c921a76d2dc9927c (diff)
Make unreachable() useful when debugging, and move common macros
Diffstat (limited to 'src/common')
-rw-r--r--src/common/macros.h27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/common/macros.h b/src/common/macros.h
new file mode 100644
index 0000000..6daf08f
--- /dev/null
+++ b/src/common/macros.h
@@ -0,0 +1,27 @@
+#ifndef AHOY_COMMON_MACROS_H
+#define AHOY_COMMON_MACROS_H
+
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#define MAX(x, y) ((x) > (y) ? (x) : (y))
+
+#define lengthof(a) (sizeof(a) / sizeof(*(a)))
+#define memeq(x, y, n) (!memcmp(x, y, n))
+#define streq(x, y) (!strcmp(x, y))
+#define u8eq(x, y) (!u8cmp(x, y))
+
+#if DEBUG || !defined(unreachable)
+# if DEBUG
+# include "cerr.h"
+# ifdef unreachable
+# undef unreachable
+# endif
+# define unreachable() \
+ diex("%s:%d: hit unreachable in %s()", __FILE__, __LINE__, __func__)
+# elifdef __clang__
+# define unreachable() __builtin_unreachable()
+# else
+# include <stddef.h>
+# endif
+#endif
+
+#endif /* !AHOY_COMMON_MACROS_H */