From ee3a6cab09c8acaf6706b3710f5652e9be43b57e Mon Sep 17 00:00:00 2001 From: Mariusz Tkaczyk Date: Wed, 25 Sep 2024 13:16:10 +0200 Subject: mdadm: add xmalloc.h Move memory declaration helpers outside mdadm.h. They seems to be useful so keep them but include separatelly. Rework them to not reffer to Name[] declared internally in mdadm/mdmon. This is first step to start decomplexing mdadm.h. Signed-off-by: Mariusz Tkaczyk --- xmalloc.c | 59 ++++++++++++++++++++++++++--------------------------------- 1 file changed, 26 insertions(+), 33 deletions(-) (limited to 'xmalloc.c') diff --git a/xmalloc.c b/xmalloc.c index 8b3f78a6..dac30eb6 100644 --- a/xmalloc.c +++ b/xmalloc.c @@ -21,64 +21,57 @@ * Email: */ -#include "mdadm.h" -/*#include -#include -#include -#include -#include -#include -#include -*/ +#include "xmalloc.h" + +#include +#include +#include +#include + +static void *exit_memory_alloc_failure(void) +{ + fprintf(stderr, "Memory allocation failure - aborting\n"); + + /* TODO: replace with MDADM_STATUS_MEM_FAIL */ + exit(1); +} void *xmalloc(size_t len) { void *rv = malloc(len); - char *msg; - int n; + if (rv) return rv; - msg = ": memory allocation failure - aborting\n"; - n = write(2, Name, strlen(Name)); - n += write(2, msg, strlen(msg)); - exit(4+!!n); + + return exit_memory_alloc_failure(); } void *xrealloc(void *ptr, size_t len) { void *rv = realloc(ptr, len); - char *msg; - int n; + if (rv) return rv; - msg = ": memory allocation failure - aborting\n"; - n = write(2, Name, strlen(Name)); - n += write(2, msg, strlen(msg)); - exit(4+!!n); + + return exit_memory_alloc_failure(); } void *xcalloc(size_t num, size_t size) { void *rv = calloc(num, size); - char *msg; - int n; + if (rv) return rv; - msg = ": memory allocation failure - aborting\n"; - n = write(2, Name, strlen(Name)); - n += write(2, msg, strlen(msg)); - exit(4+!!n); + + return exit_memory_alloc_failure(); } char *xstrdup(const char *str) { char *rv = strdup(str); - char *msg; - int n; + if (rv) return rv; - msg = ": memory allocation failure - aborting\n"; - n = write(2, Name, strlen(Name)); - n += write(2, msg, strlen(msg)); - exit(4+!!n); + + return exit_memory_alloc_failure(); } -- cgit v1.2.3