diff options
| author | @syxhe <https://t.me/syxhe> | 2025-03-28 17:23:55 -0500 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-03-28 17:23:55 -0500 |
| commit | 97713275dc87ea1d0afa4fd6bc49f695ad40efc0 (patch) | |
| tree | 3bd1d8c14639808a66b3a5b6e7a27f6a7261cce5 /src/shared.c | |
| parent | cfdd25cdd8efabaa43446036e0ff0c326c001f8f (diff) | |
Create xalloc function and wrappers, fix XALLOC_EXIT usages to comply with ISO C99
Diffstat (limited to 'src/shared.c')
| -rw-r--r-- | src/shared.c | 66 |
1 files changed, 50 insertions, 16 deletions
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 @@ | |||
| 6 | #include <errno.h> | 6 | #include <errno.h> |
| 7 | #include <error.h> | 7 | #include <error.h> |
| 8 | 8 | ||
| 9 | void* xcalloc(size_t nmemb, size_t size) { | 9 | static enum XALLOC_TYPE { |
| 10 | void *mem = calloc(nmemb, size); | 10 | XALLOC_INVAL, // Default when unset |
| 11 | if(!mem) | 11 | |
| 12 | XALLOC_EXIT("<xcalloc> Could not allocate memory"); | 12 | XALLOC_MALLOC, |
| 13 | 13 | XALLOC_CALLOC, | |
| 14 | return mem; | 14 | XALLOC_REALLOC, |
| 15 | |||
| 16 | XALLOC_2BIG // Out of range | ||
| 17 | }; | ||
| 18 | |||
| 19 | void * xalloc(size_t nmemb, size_t size, enum XALLOC_TYPE actype, void *ptr) { | ||
| 20 | if(actype <= XALLOC_INVAL || actype >= XALLOC_2BIG) | ||
| 21 | RETURNWERR(EINVAL, NULL); | ||
| 22 | |||
| 23 | void *mem = NULL; | ||
| 24 | switch(actype) { | ||
| 25 | case XALLOC_MALLOC: | ||
| 26 | mem = malloc(nmemb * size); | ||
| 27 | break; | ||
| 28 | |||
| 29 | case XALLOC_CALLOC: | ||
| 30 | mem = calloc(nmemb, size); | ||
| 31 | break; | ||
| 32 | |||
| 33 | case XALLOC_REALLOC: | ||
| 34 | mem = realloc(ptr, nmemb * size); | ||
| 35 | break; | ||
| 36 | |||
| 37 | default: | ||
| 38 | XALLOC_EXIT("<xalloc> An unknown alloc type was passed, which shouldn't be possible", ); | ||
| 39 | } | ||
| 40 | |||
| 41 | if(!mem) | ||
| 42 | XALLOC_EXIT("<xalloc> Could not allocate memory", ); | ||
| 43 | |||
| 44 | return mem; | ||
| 15 | } | 45 | } |
| 16 | 46 | ||
| 17 | void* xreallocarray(void *ptr, size_t nmemb, size_t size) { | 47 | void * xmalloc(size_t size) { |
| 18 | void *mem = reallocarray(ptr, nmemb, size); | 48 | return xalloc(size, 1, XALLOC_MALLOC, NULL); |
| 19 | if(mem == NULL) | 49 | } |
| 20 | XALLOC_EXIT("<xreallocarray> Could not allocate memory"); | 50 | |
| 51 | void * xcalloc(size_t nmemb, size_t size) { | ||
| 52 | return xalloc(nmemb, size, XALLOC_CALLOC, NULL); | ||
| 53 | } | ||
| 21 | 54 | ||
| 22 | return mem; | 55 | void * xreallocarray(void *ptr, size_t nmemb, size_t size) { |
| 56 | return xalloc(nmemb, size, XALLOC_REALLOC, ptr); | ||
| 23 | } | 57 | } |
| 24 | 58 | ||
| 25 | int rwbuf(char **str, unsigned long int initsize, int fd) { | 59 | int rwbuf(char **str, unsigned long int initsize, int fd) { |
| @@ -108,7 +142,7 @@ char *xdirname(const char * const path) { | |||
| 108 | if(!path) { // Path being null is a special case which should return super early, before anything else | 142 | if(!path) { // Path being null is a special case which should return super early, before anything else |
| 109 | tmp = strdup("."); | 143 | tmp = strdup("."); |
| 110 | if(!tmp) | 144 | if(!tmp) |
| 111 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result \"NULL\""); | 145 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result \"NULL\"", ); |
| 112 | 146 | ||
| 113 | return tmp; | 147 | return tmp; |
| 114 | } | 148 | } |
| @@ -120,7 +154,7 @@ char *xdirname(const char * const path) { | |||
| 120 | 154 | ||
| 121 | if(flag) { | 155 | if(flag) { |
| 122 | if(!tmp) | 156 | if(!tmp) |
| 123 | XALLOC_EXIT("<xdirname> could not strdup a set path result"); | 157 | XALLOC_EXIT("<xdirname> could not strdup a set path result", ); |
| 124 | 158 | ||
| 125 | return tmp; | 159 | return tmp; |
| 126 | } | 160 | } |
| @@ -162,14 +196,14 @@ char *xdirname(const char * const path) { | |||
| 162 | free(tmp); | 196 | free(tmp); |
| 163 | tmp = strdup("."); | 197 | tmp = strdup("."); |
| 164 | if(!tmp) | 198 | if(!tmp) |
| 165 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result"); | 199 | XALLOC_EXIT("<xdirname> could not strdup \".\" for set path result", ); |
| 166 | return tmp; | 200 | return tmp; |
| 167 | } | 201 | } |
| 168 | if(count == 1) { | 202 | if(count == 1) { |
| 169 | free(tmp); | 203 | free(tmp); |
| 170 | tmp = strdup("/"); | 204 | tmp = strdup("/"); |
| 171 | if(!tmp) | 205 | if(!tmp) |
| 172 | XALLOC_EXIT("<xdirname> could not strdup \"/\" for set path result"); | 206 | XALLOC_EXIT("<xdirname> could not strdup \"/\" for set path result", ); |
| 173 | 207 | ||
| 174 | return tmp; | 208 | return tmp; |
| 175 | } | 209 | } |
| @@ -183,7 +217,7 @@ char *xdirname(const char * const path) { | |||
| 183 | 217 | ||
| 184 | char * const actual = strdup(tmp); | 218 | char * const actual = strdup(tmp); |
| 185 | if(!actual) | 219 | if(!actual) |
| 186 | XALLOC_EXIT("<xdirname> could not strdup tmp string to make a shorter end string"); | 220 | XALLOC_EXIT("<xdirname> could not strdup tmp string to make a shorter end string", ); |
| 187 | free(tmp); | 221 | free(tmp); |
| 188 | 222 | ||
| 189 | return actual; | 223 | return actual; |
