summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Weiner <hannes@cmpxchg.org>2009-07-31 19:07:38 +0200
committerJohannes Weiner <hannes@cmpxchg.org>2009-07-31 19:09:47 +0200
commit3c2c0e28f6cc8f5600c13157b8aea40e4cd27d43 (patch)
treeb8034399837b7be23c80b6e0798dd04367fa4400
parent8fe3cf595692f2c7f2622645798934e41d19cdae (diff)
sheep: non-failing memory allocation functions
-rw-r--r--include/sheep/util.h11
-rw-r--r--sheep/util.c33
2 files changed, 44 insertions, 0 deletions
diff --git a/include/sheep/util.h b/include/sheep/util.h
new file mode 100644
index 0000000..f4b4fe9
--- /dev/null
+++ b/include/sheep/util.h
@@ -0,0 +1,11 @@
+#ifndef _SHEEP_UTIL_H
+#define _SHEEP_UTIL_H
+
+#include <stddef.h>
+
+void *sheep_malloc(size_t);
+void *sheep_zalloc(size_t);
+void *sheep_realloc(void *, size_t);
+char *sheep_strdup(const char *);
+
+#endif /* _SHEEP_UTIL_H */
diff --git a/sheep/util.c b/sheep/util.c
new file mode 100644
index 0000000..beefe4c
--- /dev/null
+++ b/sheep/util.c
@@ -0,0 +1,33 @@
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <sheep/util.h>
+
+static void *massert(void *mem)
+{
+ if (mem)
+ return mem;
+ fprintf(stderr, "sheep: out of memory\n");
+ abort();
+}
+
+void *sheep_malloc(size_t size)
+{
+ return massert(malloc(size));
+}
+
+void *sheep_zalloc(size_t size)
+{
+ return massert(calloc(1, size));
+}
+
+void *sheep_realloc(void *mem, size_t size)
+{
+ return massert(realloc(mem, size));
+}
+
+char *sheep_strdup(const char *str)
+{
+ return massert(strdup(str));
+}