summaryrefslogtreecommitdiffstats
path: root/mem.h
diff options
context:
space:
mode:
Diffstat (limited to 'mem.h')
-rw-r--r--mem.h45
1 files changed, 45 insertions, 0 deletions
diff --git a/mem.h b/mem.h
new file mode 100644
index 0000000..852de0a
--- /dev/null
+++ b/mem.h
@@ -0,0 +1,45 @@
+#ifndef MEM_H
+#define MEM_H
+
+#include <err.h>
+#include <stdlib.h>
+#include <syslog.h>
+
+#define MALLOC(s) xmalloc((s), __FILE__, __LINE__)
+#define CALLOC(n, s) xcalloc((n), (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))) {
+ syslog(LOG_ERR, "malloc: %s (line %d)", file, line);
+ exit(1);
+ }
+ return p;
+}
+
+static inline void *xcalloc(size_t n, size_t s, const char *file, int line)
+{
+ void *p;
+
+ if (!(p = calloc(n, s))) {
+ syslog(LOG_ERR, "calloc: %s (line %d)", file, line);
+ exit(1);
+ }
+ return p;
+}
+
+static inline void *xrealloc(void *ptr, size_t s, const char *file, int line)
+{
+ void *p;
+
+ if (!(p = realloc(ptr, s))) {
+ syslog(LOG_ERR, "realloc: %s (line %d)", file, line);
+ exit(1);
+ }
+ return p;
+}
+
+#endif /* MEM_H */