summaryrefslogtreecommitdiffstats
path: root/src/macro.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/macro.h')
-rw-r--r--src/macro.h104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/macro.h b/src/macro.h
new file mode 100644
index 0000000..d664c3e
--- /dev/null
+++ b/src/macro.h
@@ -0,0 +1,104 @@
+#ifndef foomacrohfoo
+#define foomacrohfoo
+
+#include <stdio.h>
+#include <assert.h>
+
+#ifdef __GNUC__
+#define SA_PRETTY_FUNCTION __PRETTY_FUNCTION__
+#else
+#define SA_PRETTY_FUNCTION ""
+#endif
+
+#define sa_return_if_fail(expr) \
+ do { \
+ if (!(expr)) { \
+ fprintf(stderr, "%s: Assertion <%s> failed.\n", SA_PRETTY_FUNCTION, #expr ); \
+ return; \
+ } \
+ } while(0)
+
+#define sa_return_val_if_fail(expr, val) \
+ do { \
+ if (!(expr)) { \
+ fprintf(stderr, "%s: Assertion <%s> failed.\n", SA_PRETTY_FUNCTION, #expr ); \
+ return (val); \
+ } \
+ } while(0)
+
+#define sa_return_if_fail_mutex(m, expr) \
+ do { \
+ if (!(expr)) { \
+ fprintf(stderr, "%s: Assertion <%s> failed.\n", SA_PRETTY_FUNCTION, #expr ); \
+ sa_mutex_unlock(m); \
+ return; \
+ } \
+ } while(0)
+
+#define sa_return_val_if_fail_mutex(m, expr, val) \
+ do { \
+ if (!(expr)) { \
+ \
+ fprintf(stderr, "%s: Assertion <%s> failed.\n", SA_PRETTY_FUNCTION, #expr ); \
+ sa_mutex_unlock(m); \
+ return (val); \
+ } \
+ } while(0)
+
+#define sa_assert assert
+
+/* An assert which guarantees side effects of x */
+#ifdef NDEBUG
+#define sa_assert_se(x) x
+#else
+#define sa_assert_se(x) sa_assert(x)
+#endif
+
+#define sa_assert_not_reached() sa_assert(!"Should not be reached.")
+
+#define sa_assert_success(x) do { \
+ int _r = (x); \
+ sa_assert(_r == 0); \
+ } while(0)
+
+#define SA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
+
+#ifndef MAX
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#endif
+
+#ifndef MIN
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#endif
+
+#ifndef CLAMP
+#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
+#endif
+
+#ifndef FALSE
+#define FALSE (0)
+#endif
+
+#ifndef TRUE
+#define TRUE (!FALSE)
+#endif
+
+#define SA_PTR_TO_UINT(p) ((unsigned int) (unsigned long) (p))
+#define SA_UINT_TO_PTR(u) ((void*) (unsigned long) (u))
+
+#define SA_PTR_TO_UINT32(p) ((uint32_t) SA_PTR_TO_UINT(p))
+#define SA_UINT32_TO_PTR(u) SA_UINT_TO_PTR((uint32_t) u)
+
+#define SA_PTR_TO_INT(p) ((int) SA_PTR_TO_UINT(p))
+#define SA_INT_TO_PTR(u) SA_UINT_TO_PTR((int) u)
+
+#define SA_PTR_TO_INT32(p) ((int32_t) SA_PTR_TO_UINT(p))
+#define SA_INT32_TO_PTR(u) SA_UINT_TO_PTR((int32_t) u)
+
+static inline size_t sa_align(size_t l) {
+ return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
+}
+
+#define SA_ALIGN(x) (sa_align(x))
+
+#endif