summaryrefslogtreecommitdiffstats
path: root/mem.h
diff options
context:
space:
mode:
Diffstat (limited to 'mem.h')
-rw-r--r--mem.h28
1 files changed, 28 insertions, 0 deletions
diff --git a/mem.h b/mem.h
new file mode 100644
index 0000000..032740c
--- /dev/null
+++ b/mem.h
@@ -0,0 +1,28 @@
+#ifndef MEM_H
+#define MEM_H
+
+#include <err.h>
+#include <stdlib.h>
+
+#define MALLOC(s) xmalloc((s), __FILE__, __LINE__)
+#define REALLOC(p, s) xrealloc((p), (s), __FILE__, __LINE__)
+
+static inline void *xmalloc(size_t s, const char *file, int line)
+{
+ void *p;
+
+ if (!(p = malloc(s)))
+ err(1, "%s:%d: malloc", file, line);
+ return p;
+}
+
+static inline void *xrealloc(void *ptr, size_t s, const char *file, int line)
+{
+ void *p;
+
+ if (!(p = realloc(ptr, s)))
+ err(1, "%s:%d: realloc", file, line);
+ return p;
+}
+
+#endif /* MEM_H */