summaryrefslogtreecommitdiff
path: root/misc/util.h
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-08-17 21:37:15 -0300
committerWerner Almesberger <werner@almesberger.net>2016-08-17 21:37:15 -0300
commit30d665115a8f7d72f659a1e77b6eb14e727fbe57 (patch)
treef0ff40e1a6b654571745dc5c289a8ed6cfd103ca /misc/util.h
parentae27fe967c09bf0d685952dc7a943d1535eb6af4 (diff)
downloadeeshow-30d665115a8f7d72f659a1e77b6eb14e727fbe57.tar.gz
eeshow-30d665115a8f7d72f659a1e77b6eb14e727fbe57.tar.bz2
eeshow-30d665115a8f7d72f659a1e77b6eb14e727fbe57.zip
eeshow/: util.h and diag.c go to misc/
Diffstat (limited to 'misc/util.h')
-rw-r--r--misc/util.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/misc/util.h b/misc/util.h
new file mode 100644
index 0000000..d0c5610
--- /dev/null
+++ b/misc/util.h
@@ -0,0 +1,53 @@
+/*
+ * misc/util.h - Common utility functions
+ *
+ * Written 2016 by Werner Almesberger
+ * Copyright 2016 Werner Almesberger
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+
+#ifndef MISC_UTIL_H
+#define MISC_UTIL_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+#define alloc_size(s) \
+ ({ void *alloc_size_tmp = malloc(s); \
+ if (!alloc_size_tmp) { \
+ perror("malloc"); \
+ exit(1); \
+ } \
+ alloc_size_tmp; })
+
+#define alloc_type(t) ((t *) alloc_size(sizeof(t)))
+#define alloc_type_n(t, n) ((t *) alloc_size(sizeof(t) * (n)))
+
+#define stralloc(s) \
+ ({ char *stralloc_tmp = strdup(s); \
+ if (!stralloc_tmp) { \
+ perror("strdup"); \
+ exit(1); \
+ } \
+ stralloc_tmp; })
+
+
+#define ARRAY_ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
+#define ARRAY_END(a) ((a) + ARRAY_ELEMENTS(a))
+
+
+#define swap(a, b) \
+ ({ typeof(a) _tmp = (a); a = (b); b = _tmp; })
+
+
+#define unsupported(s) \
+ fprintf(stderr, __FILE__ ":%d: unsupported: " s "\n", __LINE__)
+
+#endif /* !MISC_UTIL_H */