summaryrefslogtreecommitdiff
path: root/macros.h
diff options
context:
space:
mode:
Diffstat (limited to 'macros.h')
-rw-r--r--macros.h50
1 files changed, 50 insertions, 0 deletions
diff --git a/macros.h b/macros.h
new file mode 100644
index 0000000..eb514b5
--- /dev/null
+++ b/macros.h
@@ -0,0 +1,50 @@
+#ifndef macros_h
+#define macros_h
+
+#undef assert
+#define assert(expr) if (!(expr)) { *(int *)0 = 0; }
+
+#define array_count(array) (sizeof(array)/sizeof(*(array)))
+
+#undef offsetof
+#define offsetof(type, element) ((u64)(&(((type *)0)->element)))
+
+#define min(a, b) (((a) < (b)) ? (a) : (b))
+#define max(a, b) (((a) > (b)) ? (a) : (b))
+
+#define clamp(a, x, b) \
+ (((x) < (a)) ? (a) : \
+ (((x) > (b)) ? (b) : (x)))
+
+#define swap(type, a, b) { type tmp = (a); (a) = (b); (b) = tmp; }
+
+#define kilobytes(n) n*1024
+#define megabytes(n) kilobytes(n)*1024
+#define gigabytes(n) megabytes(n)*1024
+
+/* NOTE(pryazha): Singly linked list */
+#define sllpush(first, last, node) \
+ ((first) == 0 ? \
+ ((first) = (last) = (node), (node)->next = 0) : \
+ ((last)->next = (node), (last) = (node), (node)->next = 0))
+#define sllpop(first, last) \
+ ((first) == (last) ? \
+ ((first) = (last) = 0) : \
+ ((first) = (first)->next))
+
+/* NOTE(pryazha): Doubly linked list */
+#define dllpush(first, last, node, next, prev) \
+ ((first) == 0 ? \
+ ((first) = (last) = (node), (node)->next = (node)->prev = 0) : \
+ ((node)->prev = (last), (last)->next = (node), (last) = (node), (node)->next = 0))
+#define dllpushback(first, last, node) dllpush(first, last, node, next, prev)
+#define dllpushfront(first, last, node) dllpush(last, first, node, prev, next)
+
+#define dllremove(first, last, node) \
+ ((first) == (node) ? \
+ ((first) == (last) ? (first) = (last) = 0 : \
+ ((first) = (first)->next, (first)->prev = 0)) : \
+ ((last) == (node) ? ((last) = (last)->prev, (last)->next = 0) : \
+ ((node)->next->prev = (node)->prev, (node)->prev->next = (node)->next)))
+
+#endif