From b3bd3df103a5a75d267b2b79e85558768b1dc4bb Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Sun, 23 Mar 2025 22:11:15 -0500 Subject: Fix a whole bunch of shit, create an arena implementation --- src/shared.c | 115 ++++++++++++----------------------------------------------- 1 file changed, 23 insertions(+), 92 deletions(-) (limited to 'src/shared.c') diff --git a/src/shared.c b/src/shared.c index d67e362..72ede56 100644 --- a/src/shared.c +++ b/src/shared.c @@ -1,45 +1,23 @@ #include "shared.h" -#include #include #include #include #include #include -#include void* xcalloc(size_t nmemb, size_t size) { void *mem = calloc(nmemb, size); - - if(!mem) { - #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 - error(1, errno, " Could not allocate memory"); - #else - exit(EXIT_FAILURE); - #endif - #endif - - abort(); - } + 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) { - #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 - error(1, errno, " Could not allocate memory"); - #else - exit(EXIT_FAILURE); - #endif - #endif - - abort(); - } + if(mem == NULL) + XALLOC_EXIT(" Could not allocate memory"); return mem; } @@ -63,9 +41,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) { tmp = realloc(lstr, csize * sizeof(char)); if(!tmp) { - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + if(___VXGG___VERBOSE_ERRORS___) error(0, errno, "Could not reallocate enough space for lstr"); - #endif + free(lstr); lstr = NULL; // Need to set this because of the break bytesread = -100; @@ -75,9 +53,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) { } } if(bytesread < 0 && bytesread != -100) { - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + if(___VXGG___VERBOSE_ERRORS___) error(0, errno, "Ran into a read() error"); - #endif + free(lstr); lstr = NULL; } @@ -85,9 +63,9 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) { if(lstr) { tmp = realloc(lstr, csize - ccap + 1); if(!tmp) { - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 + if(___VXGG___VERBOSE_ERRORS___) error(0, errno, "Could not shrink lstr after reading buffer"); - #endif + free(lstr); bytesread = -100; } @@ -127,16 +105,8 @@ char *xdirname(const char * const path) { char *tmp = NULL; if(!path) { // Path being null is a special case which should return super early, before anything else tmp = strdup("."); - if(!tmp) { - #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 - error(1, errno, " could not strdup \".\" for set path result \"NULL\""); - #else - exit(EXIT_FAILURE); - #endif - #endif - abort(); - } + if(!tmp) + XALLOC_EXIT(" could not strdup \".\" for set path result \"NULL\""); return tmp; } @@ -147,16 +117,9 @@ char *xdirname(const char * const path) { if(strcmp(path, "..") == 0 && !flag) {tmp = strdup("."); flag++;} if(flag) { - if(!tmp) { - #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 - error(1, errno, " could not strdup a set path result"); - #else - exit(EXIT_FAILURE); - #endif - #endif - abort(); - } + if(!tmp) + XALLOC_EXIT(" could not strdup a set path result"); + return tmp; } @@ -173,16 +136,8 @@ char *xdirname(const char * const path) { // Get a temp copy of the path for manipulation purposes tmp = strdup(path); - if(!tmp) { - #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 - error(1, errno, " could not strdup the given path \"%s\" for internal manipulation", path); - #else - exit(EXIT_FAILURE); - #endif - #endif - abort(); - } + if(!tmp) + XALLOC_EXIT(" could not strdup the given path \"%s\" for internal manipulation", , path); // If there's a trailing '/', delete it size_t pathlen = strlen(path); @@ -204,31 +159,15 @@ char *xdirname(const char * const path) { if(count == 0) { free(tmp); tmp = strdup("."); - if(!tmp) { - #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 - error(1, errno, " could not strdup \".\" for set path result"); - #else - exit(EXIT_FAILURE); - #endif - #endif - abort(); - } + if(!tmp) + XALLOC_EXIT(" could not strdup \".\" for set path result"); return tmp; } if(count == 1) { free(tmp); tmp = strdup("/"); - if(!tmp) { - #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 - error(1, errno, " could not strdup \"/\" for set path result"); - #else - exit(EXIT_FAILURE); - #endif - #endif - abort(); - } + if(!tmp) + XALLOC_EXIT(" could not strdup \"/\" for set path result"); return tmp; } @@ -241,16 +180,8 @@ char *xdirname(const char * const path) { } char * const actual = strdup(tmp); - if(!actual) { - #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0 - #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0 - error(1, errno, " could not strdup tmp string to make a shorter end string"); - #else - exit(EXIT_FAILURE); - #endif - #endif - abort(); - } + if(!actual) + XALLOC_EXIT(" could not strdup tmp string to make a shorter end string"); free(tmp); return actual; -- cgit v1.2.3