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/Makefile | 2 +- src/arena.c | 5 +++-- src/arena.h | 2 +- src/encryption.c | 2 +- src/ll.c | 2 +- src/shared.c | 66 ++++++++++++++++++++++++++++++++++++++++++-------------- src/shared.h | 7 ++++-- 7 files changed, 62 insertions(+), 24 deletions(-) diff --git a/src/Makefile b/src/Makefile index 4bddadf..4450727 100644 --- a/src/Makefile +++ b/src/Makefile @@ -5,7 +5,7 @@ SHELL = /usr/bin/env -S bash # RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto # RELEASE_LDFLAGS := -fuse-linker-plugin -flto=auto -CFLAGS = -Wall -Wextra -Wpedantic -pedantic-errors -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 $$(pkg-config --cflags libsodium) +CFLAGS = -std=c2x -Wall -Wextra -Wpedantic -pedantic-errors -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -O0 $$(pkg-config --cflags libsodium) LDLIBS += $$(pkg-config --libs-only-l libsodium) LDFLAGS += $$(pkg-config --libs-only-L libsodium) diff --git a/src/arena.c b/src/arena.c index 36a9ce7..62dbbf8 100644 --- a/src/arena.c +++ b/src/arena.c @@ -23,6 +23,7 @@ typedef struct arena { arenanode *current; // For keeping track of the largest possible thing that can be allocated to one node + // I don't know if I care to keep this or remove it. I'll see if it becomes a problem / is useful size_t node_memspace; } arena; @@ -49,7 +50,7 @@ arenanode * arenanode_init(size_t bytes) { arena * arena_init(size_t bytes) { if(!ISPOWOF2(MEM_ALIGN_BYTES)) - XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena"); + XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena", ); arena *a = VALLOC(1, sizeof(arena)); if(!a) @@ -73,7 +74,7 @@ arena * arena_init(size_t bytes) { void * arena_alloc(arena * const arena, size_t bytes) { if(!ISPOWOF2(MEM_ALIGN_BYTES)) - XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory"); + XALLOC_EXIT(" \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory", ); if(!arena) RETURNWERR(EINVAL, NULL); if(bytes > arena->node_memspace) diff --git a/src/arena.h b/src/arena.h index 1f57f01..e5be79a 100644 --- a/src/arena.h +++ b/src/arena.h @@ -15,7 +15,7 @@ static const size_t MEM_ALIGN_BYTES = (2 * sizeof(void*)); // Hey, the static ke #define MEM_ALIGN(x) ((x) + (((x) & (MEM_ALIGN_BYTES - 1)) != 0) * (MEM_ALIGN_BYTES - ((x) & (MEM_ALIGN_BYTES - 1)))) arena * arena_init(size_t bytes); -void* arena_alloc(arena * const arena, size_t bytes); +void * arena_alloc(arena * const arena, size_t bytes); int arena_free(arena **arena); int arena_clear(arena **arena); diff --git a/src/encryption.c b/src/encryption.c index 9b8715e..b75a119 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -206,7 +206,7 @@ void* xsodium_malloc(size_t size) { void *mem = sodium_malloc(size); if(mem == NULL) - XALLOC_EXIT(" could not allocate memory... Quitting"); + XALLOC_EXIT(" could not allocate memory... Quitting", ); return mem; } diff --git a/src/ll.c b/src/ll.c index 1fa8773..b02c275 100644 --- a/src/ll.c +++ b/src/ll.c @@ -119,7 +119,7 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char break; default: - XALLOC_EXIT(" got an invalid op token when it shouldn't be possible to recieve one"); + XALLOC_EXIT(" got an invalid op token when it shouldn't be possible to recieve one", ); // Technically not an alloc, but also there's no reason I can't reuse a perfectly good macro } 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; diff --git a/src/shared.h b/src/shared.h index b9e7bcd..718bbf2 100644 --- a/src/shared.h +++ b/src/shared.h @@ -29,10 +29,13 @@ exit(EXIT_FAILURE); /* Makes gcc happy */\ } while (0) -// `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` +// `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` +void * xmalloc(size_t size); + +// `calloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` void* xcalloc(size_t nmemb, size_t size); -// `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` +// `reallocarray()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` void* xreallocarray(void *ptr, size_t nmemb, size_t size); // Read the entire contents of a file descriptor into a malloc()'ed buffer -- cgit v1.2.3