summaryrefslogtreecommitdiff
path: root/xmalloc.h
diff options
context:
space:
mode:
authorJosé Fonseca <jrfonseca@users.sourceforge.net>2003-07-03 18:25:53 +0000
committerJosé Fonseca <jrfonseca@users.sourceforge.net>2003-07-03 18:25:53 +0000
commit15ff73b9843c79c1709f4a521aaa09285105f8bc (patch)
tree207ebddf035d06172746ef77695f4c3de64f357f /xmalloc.h
parent2867d16bc9bae9764904efbf44cd5131fca1bc9b (diff)
Exit with an error code immediately whenever an error.
Extensive use of xmalloc and friends.
Diffstat (limited to 'xmalloc.h')
-rw-r--r--xmalloc.h56
1 files changed, 56 insertions, 0 deletions
diff --git a/xmalloc.h b/xmalloc.h
new file mode 100644
index 0000000..881bcad
--- /dev/null
+++ b/xmalloc.h
@@ -0,0 +1,56 @@
+/**
+ * \file xmalloc.c
+ * Allocate space or die.
+ *
+ * \author Adapted from fetchmail's xmalloc.c by José Fonseca.
+ */
+
+#ifndef _XMALLOC_H
+#define _XMALLOC_H
+
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "main.h"
+
+
+static inline
+void *xmalloc(size_t size)
+{
+ void *ptr;
+
+ ptr = malloc(size);
+ if (ptr == NULL)
+ {
+ perror(NULL);
+ exit(EX_OSERR);
+ }
+ return(ptr);
+}
+
+static inline
+void *xrealloc(void *ptr, size_t size)
+{
+ if (ptr == 0)
+ return xmalloc(size);
+ ptr = realloc(ptr, size);
+ if (ptr == NULL)
+ {
+ perror(NULL);
+ exit(EX_OSERR);
+ }
+ return ptr;
+}
+
+static inline
+char *xstrdup(const char *s)
+{
+ char *p;
+ p = (char *)xmalloc(strlen(s) + 1);
+ strcpy(p, s);
+ return p;
+}
+
+#endif