summaryrefslogtreecommitdiff
path: root/bittorrent/epoll_server/utils
diff options
context:
space:
mode:
Diffstat (limited to 'bittorrent/epoll_server/utils')
-rw-r--r--bittorrent/epoll_server/utils/xalloc.c31
-rw-r--r--bittorrent/epoll_server/utils/xalloc.h32
2 files changed, 63 insertions, 0 deletions
diff --git a/bittorrent/epoll_server/utils/xalloc.c b/bittorrent/epoll_server/utils/xalloc.c
new file mode 100644
index 0000000..e4dc6b8
--- /dev/null
+++ b/bittorrent/epoll_server/utils/xalloc.c
@@ -0,0 +1,31 @@
+#include "xalloc.h"
+
+#include <err.h>
+#include <stdlib.h>
+
+void *xmalloc(size_t size)
+{
+ void *res = malloc(size);
+ if (!res)
+ err(EXIT_FAILURE, "Impossible to malloc");
+
+ return res;
+}
+
+void *xcalloc(size_t nmemb, size_t size)
+{
+ void *res = calloc(nmemb, size);
+ if (!res)
+ err(EXIT_FAILURE, "Impossible to calloc");
+
+ return res;
+}
+
+void *xrealloc(void *ptr, size_t size)
+{
+ void *res = realloc(ptr, size);
+ if (!res)
+ err(EXIT_FAILURE, "Impossible to realloc");
+
+ return res;
+}
diff --git a/bittorrent/epoll_server/utils/xalloc.h b/bittorrent/epoll_server/utils/xalloc.h
new file mode 100644
index 0000000..e7655b8
--- /dev/null
+++ b/bittorrent/epoll_server/utils/xalloc.h
@@ -0,0 +1,32 @@
+#ifndef XALLOC_H
+#define XALLOC_H
+
+#include <stddef.h>
+
+/**
+** \brief Malloc wrapper that exits on failure.
+**
+** \param size The size to malloc.
+** \return The malloc return.
+*/
+void *xmalloc(size_t size);
+
+/**
+** \brief Calloc wrapper that exits on failure.
+**
+** \param nmemb The number of elements.
+** \param size The size of an element.
+** \return The calloc return.
+*/
+void *xcalloc(size_t nmemb, size_t size);
+
+/**
+** \brief Realloc wrapper that exits on failure.
+**
+** \param ptr The mem pointer.
+** \param size The size to realloc.
+** \return The realloc return.
+*/
+void *xrealloc(void *ptr, size_t size);
+
+#endif /* !XALLOC_H */