From c704a8a382c231e066a7fbf0402a33455c40b8f5 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 21 Apr 2025 21:51:54 -0500 Subject: Rework dlinkedlist functions to use VALLOC macro instead of only xalloc --- src/arena.h | 6 ------ src/ll-internal.c | 7 ++++++- src/ll.c | 18 +++++++++++++++--- src/ll.h | 18 ++---------------- src/main.c | 13 ------------- src/shared.h | 5 +++++ src/threadpool.c | 1 - 7 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/arena.h b/src/arena.h index e65b041..1c1c576 100644 --- a/src/arena.h +++ b/src/arena.h @@ -3,12 +3,6 @@ #include -// Whether to use `XALLOC` or normal `malloc` functions for acquiring new memory in an arena. `> 0` -// enables XALLOC functionality -#define ___VXGG___USE_XALLOC_FOR_ARENAS___ 1 - -#define VALLOC(nmemb, size) ((___VXGG___USE_XALLOC_FOR_ARENAS___ > 0) ? xmalloc((nmemb) * (size)) : malloc((nmemb) * (size))) - // Normal arena. Can expand in size beyond original allocation typedef struct arena arena; diff --git a/src/ll-internal.c b/src/ll-internal.c index 1a4fe3c..e56302e 100644 --- a/src/ll-internal.c +++ b/src/ll-internal.c @@ -1,8 +1,13 @@ #include "ll-internal.h" #include "shared.h" +#include + dllnode * dllnode_init(void *data, dll_freecb fcb) { - dllnode *n = xcalloc(1, sizeof(*n)); + dllnode *n = VALLOC(1, sizeof(*n)); + if(!n) + return NULL; + n->data = data; n->freecb = fcb; n->prev = NULL; diff --git a/src/ll.c b/src/ll.c index 285203d..76a79f9 100644 --- a/src/ll.c +++ b/src/ll.c @@ -8,7 +8,10 @@ #include dlinkedlist * dlinkedlist_init(void) { - dlinkedlist *ll = xcalloc(1, sizeof(*ll)); + dlinkedlist *ll = VALLOC(1, sizeof(*ll)); + if(!ll) + return NULL; + ll->end = NULL; ll->start = NULL; ll->size = 0; @@ -50,6 +53,8 @@ int dlinkedlist_handlefirstnode(dlinkedlist * const ll, void *data, dll_freecb f // Need to handle adding the first element differently than other opers, so do it here dllnode *n = dllnode_init(data, fcb); + if(!n) + return -1; // dllnode_init already sets n.prev and n.next to null, so no need to do so again ll->end = n; @@ -64,10 +69,15 @@ int dlinkedlist_xxxend(dlinkedlist * const ll, void *data, dll_freecb fcb, char RETURNWERR(EINVAL, 1); if(op != 'a' && op != 'p') RETURNWERR(EINVAL, 1); - if(dlinkedlist_handlefirstnode(ll, data, fcb) == 0) + + int handleret; + if((handleret = dlinkedlist_handlefirstnode(ll, data, fcb)) == 0) return 0; dllnode *n = dllnode_init(data, fcb); + if(!n || handleret < 0) + return -1; + switch (op) { case 'a': n->prev = (ll->end); @@ -135,6 +145,8 @@ int dlinkedlist_insert(dlinkedlist * const ll, void *data, dll_freecb fcb, int i // If you're inserting at index 2, the new node becomes index 2, and the previous node at index 2 moves to index 3 dllnode *new = dllnode_init(data, fcb); + if(!new) + return -1; dllnode *current = dlinkedlist_getnode(ll, index); if(!current) XALLOC_EXIT(" somehow managed to pull a null node from dlinkedlist_getnode(... , ... , ... , %d)", , (index)); @@ -156,7 +168,7 @@ int dlinkedlist_remove(dlinkedlist * const ll, int index) { dllnode *current = dlinkedlist_getnode(ll, index); if(!current) - abort(); + return -1; if(index == 0) { ll->start = current->next; diff --git a/src/ll.h b/src/ll.h index 4b5a2df..154cfe2 100644 --- a/src/ll.h +++ b/src/ll.h @@ -1,25 +1,11 @@ #ifndef __VXGG_REWRITE___LL_H___305861098005___ #define __VXGG_REWRITE___LL_H___305861098005___ -/*////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WA // -// RNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING // -// BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WA // -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*//* -// // -// dlinkedlist functions should be considered X_ALLOC functions, as they call xcalloc to initialize new nodes. This is not // -// reflected in their naming, which could change later, but as of now it is something to keep in mind. This means ABSOLUETLY NO // -// USE OF DLINKEDLIST FUNCTIONS INSIDE ENCRYPTION FUNCTIONS, OR ANYTHING THAT SHOULD BE CONSIDERED ATOMIC OR SEMI-ATOMIC. IF // -// XCALLOC ERRORS FOR ANY REASON, AND YOU'RE DOING SOMETHING WITH THESE IN AN (SEMI)ATOMIC FUNCTION, YOU WILL BREAK SHIT // -// // -////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*//* -// BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WA // -// RNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING // -// BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WARNING BIG WA // -//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/ +// Notice: dlinked functions are no longer necessarily xalloc, but MAY be IF the `___VXGG___USE_XALLOC_FOR_VALLOC___` is greater than 0 /* TODO: Implement a way to register a set of alloc functions to a linked list so I can give it arenas for memory allocation // instead of just xcalloc */ + // I don't know if I care to do this right now. I might be able to hack it in but it would be a fugly hack, something as bad if not worse than that weird callback thing I was doing with the ALWAYS_CHECK_LIBSODIUM macro typedef void (*dll_freecb)(void*); typedef struct dlinked dlinkedlist; diff --git a/src/main.c b/src/main.c index 3779206..2fcf8ab 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,3 @@ -#define _GNU_SOURCE - #include "arena.h" #include "ll.h" #include "encryption.h" @@ -9,18 +7,7 @@ #include #include -#include - -#include -#include - -#include -int lol(const struct dirent *node) {return 1;} -int printnames(void *data) { - printf("%s\n", (data) ? ((struct dirent *)data)->d_name : "null"); - return 0; -} int main() { error(1, ENOTSUP, "No main file lol"); return 0; diff --git a/src/shared.h b/src/shared.h index 3b0a5f7..7034255 100644 --- a/src/shared.h +++ b/src/shared.h @@ -29,6 +29,11 @@ exit(EXIT_FAILURE); /* Makes gcc happy */\ } while (0) +// Whether to use `XALLOC` or normal `malloc` functions for acquiring new memory via the `VALLOC()` macro. `> 0` +// enables XALLOC functionality +#define ___VXGG___USE_XALLOC_FOR_VALLOC___ 0 +#define VALLOC(nmemb, size) ((___VXGG___USE_XALLOC_FOR_VALLOC___ > 0) ? xmalloc((nmemb) * (size)) : malloc((nmemb) * (size))) + // Error macro that gcc will not complain about #define ERROR(status, errnum, format, ...) do {error((status), (errnum), (format)__VA_ARGS__); exit((status));} while (0) diff --git a/src/threadpool.c b/src/threadpool.c index 808dd5d..56dcd6b 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -4,7 +4,6 @@ #include "ll.h" -#include #include #include #include -- cgit v1.2.3