aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/emalloc.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/emalloc.h')
-rw-r--r--src/util/emalloc.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/util/emalloc.h b/src/util/emalloc.h
new file mode 100644
index 0000000..7c4266e
--- /dev/null
+++ b/src/util/emalloc.h
@@ -0,0 +1,23 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdlib.h>
+
+#define emalloc(...) enmalloc(1, __VA_ARGS__)
+#define ecalloc(...) encalloc(1, __VA_ARGS__)
+
+static inline void *
+enmalloc(int status, size_t n)
+{
+ void *ptr = malloc(n);
+ if (!ptr)
+ enprintf(status, "malloc: out of memory\n");
+ return ptr;
+}
+
+static inline void *
+encalloc(int status, size_t n, size_t m)
+{
+ void *ptr = calloc(n, m);
+ if (!ptr)
+ enprintf(status, "malloc: out of memory\n");
+ return ptr;
+}