summaryrefslogtreecommitdiff
path: root/src/xmalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/xmalloc.c')
-rw-r--r--src/xmalloc.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/xmalloc.c b/src/xmalloc.c
new file mode 100644
index 0000000..8a08cb8
--- /dev/null
+++ b/src/xmalloc.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include "log.h"
+#include "xmalloc.h"
+
+void *xmalloc(size_t size)
+{
+ void *ptr = malloc(size);
+
+ if (ptr != NULL) {
+ log_debug("Allocated %zu bytes.\n", size);
+ return ptr;
+ } else {
+ fputs("Out of memory, exiting.", stderr);
+ exit(EXIT_FAILURE);
+ }
+}
+
+void *xcalloc(size_t nmemb, size_t size)
+{
+ void *ptr = calloc(nmemb, size);
+
+ if (ptr != NULL) {
+ log_debug("Allocated %zux%zu bytes.\n", nmemb, size);
+ return ptr;
+ } else {
+ fputs("Out of memory, exiting.", stderr);
+ exit(EXIT_FAILURE);
+ }
+}
+
+void *xrealloc(void *ptr, size_t size)
+{
+ ptr = realloc(ptr, size);
+
+ if (ptr != NULL) {
+ log_debug("Reallocated to %zu bytes.\n", size);
+ return ptr;
+ } else {
+ fputs("Out of memory, exiting.", stderr);
+ exit(EXIT_FAILURE);
+ }
+}