diff options
author | Hisham <hisham@gobolinux.org> | 2016-02-03 12:42:25 +0000 |
---|---|---|
committer | Hisham <hisham@gobolinux.org> | 2016-02-03 12:42:25 +0000 |
commit | 807640e49b0d8b67b5bf424db0c221812483f55d (patch) | |
tree | 8eff83b74cb369bb3feca6b259dd2907d15333ae | |
parent | 5c593fae4227651075cfc9445f17dad6ae0a0b47 (diff) |
Shorten the code using the err() function.
-rw-r--r-- | XAlloc.c | 29 |
1 files changed, 11 insertions, 18 deletions
@@ -5,22 +5,24 @@ #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif -#include <unistd.h> +#include <err.h> +#include <stdlib.h> #include <string.h> /*{ #include <stdlib.h> }*/ -static char oomMessage[] = "Out of memory!\n"; +static inline void fail() { + curs_set(1); + endwin(); + err(1, NULL); +} void* xMalloc(size_t size) { void* data = malloc(size); if (!data && size > 0) { - curs_set(1); - endwin(); - write(2, oomMessage, sizeof oomMessage - 1); - exit(1); + fail(); } return data; } @@ -28,10 +30,7 @@ void* xMalloc(size_t size) { void* xCalloc(size_t nmemb, size_t size) { void* data = calloc(nmemb, size); if (!data) { - curs_set(1); - endwin(); - write(2, oomMessage, sizeof oomMessage - 1); - exit(1); + fail(); } return data; } @@ -39,10 +38,7 @@ void* xCalloc(size_t nmemb, size_t size) { void* xRealloc(void* ptr, size_t size) { void* data = realloc(ptr, size); if (!data && size > 0) { - curs_set(1); - endwin(); - write(2, oomMessage, sizeof oomMessage - 1); - exit(1); + fail(); } return data; } @@ -50,10 +46,7 @@ void* xRealloc(void* ptr, size_t size) { char* xStrdup(const char* str) { char* data = strdup(str); if (!data && str) { - curs_set(1); - endwin(); - write(2, oomMessage, sizeof oomMessage - 1); - exit(1); + fail(); } return data; } |