aboutsummaryrefslogtreecommitdiffstats
path: root/src/macros.h
diff options
context:
space:
mode:
authorMattias Andrée <maandree@member.fsf.org>2016-01-04 18:16:45 +0100
committerMattias Andrée <maandree@member.fsf.org>2016-01-04 18:16:45 +0100
commit453f44a8ac3100bb6c1f2fd33d92ac68a1bcca92 (patch)
tree9ff6c365f5b17a8ca3b05bb70245922618a5e94f /src/macros.h
parentinitialise partitions (diff)
downloadradharc-453f44a8ac3100bb6c1f2fd33d92ac68a1bcca92.tar.gz
radharc-453f44a8ac3100bb6c1f2fd33d92ac68a1bcca92.tar.bz2
radharc-453f44a8ac3100bb6c1f2fd33d92ac68a1bcca92.tar.xz
macros
Signed-off-by: Mattias Andrée <maandree@member.fsf.org>
Diffstat (limited to 'src/macros.h')
-rw-r--r--src/macros.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/macros.h b/src/macros.h
new file mode 100644
index 0000000..298a41a
--- /dev/null
+++ b/src/macros.h
@@ -0,0 +1,57 @@
+/**
+ * Copyright © 2016 Mattias Andrée <maandree@member.fsf.org>
+ *
+ * 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include <string.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+
+
+#define try(...) do { if (!(__VA_ARGS__)) goto fail; } while (0)
+#define t(...) do { if (__VA_ARGS__) goto fail; } while (0)
+#define CLEANUP(...) do { int cleanup__ = errno; __VA_ARGS__; errno = cleanup__; } while (0)
+
+#define xstrdup(outp, ...) \
+do { \
+ const char *xstrdup__ = (__VA_ARGS__); \
+ if (xstrdup__) \
+ try (*(outp) = strdup(xstrdup__)); \
+ else \
+ *(outp) = NULL; \
+} while (0)
+
+#define xpread(fd, buf, len, off) t (pread(fd, buf, len, off) < (ssize_t)(len))
+#define xpwrite(fd, buf, len, off) t (pwrite(fd, buf, len, off) < (ssize_t)(len))
+#define xread(fd, buf, len) t (read(fd, buf, len) < (ssize_t)(len))
+#define xwrite(fd, buf, len) t (write(fd, buf, len) < (ssize_t)(len))
+
+#define xcalloc(outp, num) try (*(outp) = calloc(num, sizeof(**(outp))))
+#define xmalloc(outp, num) try (*(outp) = malloc((num) * sizeof(**(outp))))
+#define xrealloc(outp, num) \
+do { \
+ size_t n__ = (num); \
+ void *new__ = realloc(*(outp), n__ * sizeof(**(outp))); \
+ t (n__ && !new__); \
+ *(outp) = new__; \
+} while (0)
+
+#define SHRINK(outp, num) \
+do { \
+ void *new__ = realloc(*(outp), (num) * sizeof(**(outp))); \
+ if (new__) *(outp) = new__; \
+} while (0)
+