From 97713275dc87ea1d0afa4fd6bc49f695ad40efc0 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Fri, 28 Mar 2025 17:23:55 -0500 Subject: Create xalloc function and wrappers, fix XALLOC_EXIT usages to comply with ISO C99 --- src/shared.c | 66 +++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 16 deletions(-) (limited to 'src/shared.c') diff --git a/src/shared.c b/src/shared.c index cc11fe1..771c44f 100644 --- a/src/shared.c +++ b/src/shared.c @@ -6,20 +6,54 @@ #include #include -void* xcalloc(size_t nmemb, size_t size) { - void *mem = calloc(nmemb, size); - if(!mem) - XALLOC_EXIT(" Could not allocate memory"); - - return mem; +static enum XALLOC_TYPE { + XALLOC_INVAL, // Default when unset + + XALLOC_MALLOC, + XALLOC_CALLOC, + XALLOC_REALLOC, + + XALLOC_2BIG // Out of range +}; + +void * xalloc(size_t nmemb, size_t size, enum XALLOC_TYPE actype, void *ptr) { + if(actype <= XALLOC_INVAL || actype >= XALLOC_2BIG) + RETURNWERR(EINVAL, NULL); + + void *mem = NULL; + switch(actype) { + case XALLOC_MALLOC: + mem = malloc(nmemb * size); + break; + + case XALLOC_CALLOC: + mem = calloc(nmemb, size); + break; + + case XALLOC_REALLOC: + mem = realloc(ptr, nmemb * size); + break; + + default: + XALLOC_EXIT(" An unknown alloc type was passed, which shouldn't be possible", ); + } + + if(!mem) + XALLOC_EXIT(" Could not allocate memory", ); + + return mem; } -void* xreallocarray(void *ptr, size_t nmemb, size_t size) { - void *mem = reallocarray(ptr, nmemb, size); - if(mem == NULL) - XALLOC_EXIT(" Could not allocate memory"); +void * xmalloc(size_t size) { + return xalloc(size, 1, XALLOC_MALLOC, NULL); +} + +void * xcalloc(size_t nmemb, size_t size) { + return xalloc(nmemb, size, XALLOC_CALLOC, NULL); +} - return mem; +void * xreallocarray(void *ptr, size_t nmemb, size_t size) { + return xalloc(nmemb, size, XALLOC_REALLOC, ptr); } int rwbuf(char **str, unsigned long int initsize, int fd) { @@ -108,7 +142,7 @@ char *xdirname(const char * const path) { if(!path) { // Path being null is a special case which should return super early, before anything else tmp = strdup("."); if(!tmp) - XALLOC_EXIT(" could not strdup \".\" for set path result \"NULL\""); + XALLOC_EXIT(" could not strdup \".\" for set path result \"NULL\"", ); return tmp; } @@ -120,7 +154,7 @@ char *xdirname(const char * const path) { if(flag) { if(!tmp) - XALLOC_EXIT(" could not strdup a set path result"); + XALLOC_EXIT(" could not strdup a set path result", ); return tmp; } @@ -162,14 +196,14 @@ char *xdirname(const char * const path) { free(tmp); tmp = strdup("."); if(!tmp) - XALLOC_EXIT(" could not strdup \".\" for set path result"); + XALLOC_EXIT(" could not strdup \".\" for set path result", ); return tmp; } if(count == 1) { free(tmp); tmp = strdup("/"); if(!tmp) - XALLOC_EXIT(" could not strdup \"/\" for set path result"); + XALLOC_EXIT(" could not strdup \"/\" for set path result", ); return tmp; } @@ -183,7 +217,7 @@ char *xdirname(const char * const path) { char * const actual = strdup(tmp); if(!actual) - XALLOC_EXIT(" could not strdup tmp string to make a shorter end string"); + XALLOC_EXIT(" could not strdup tmp string to make a shorter end string", ); free(tmp); return actual; -- cgit v1.2.3