From 21c168bf02bbab8b473873f0822d68859a025c24 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Wed, 22 Oct 2025 00:41:19 -0500 Subject: Fix threadpool after ripping out cleanup function suite --- src/shared.c | 148 ----------------------------------------------------------- 1 file changed, 148 deletions(-) (limited to 'src/shared.c') diff --git a/src/shared.c b/src/shared.c index 9a147eb..1310ef2 100644 --- a/src/shared.c +++ b/src/shared.c @@ -46,26 +46,12 @@ typedef int (*gcallback)(void*); //!< Generic callback signature typedef void (*fcallback)(void*); //!< free()-like callback signature -/** - * @brief A locally defined structure designed for easier function cleanup - * - */ -typedef struct cl { - fcallback *callbacks; //!< An array of free()-like callbacks. Actual Type: fcallback callbacks[] - void * *arguments; //!< An array of void pointers. Actual Type: void *arguments[] - int size; //!< The size of each array - int used; //!< The current number of used elements in each array -} cleanup; -// While the cleanup thing is useful, it's also a complete fucking mess. Swtich to error gotos asap - #include #include #include #include #include -#include - /** * @brief Read the entire contents of a file descriptor into a malloc()'ed buffer * @@ -241,138 +227,4 @@ char * vxdirname(const char * const path) { return actual; } - -/** - * @brief Initialize a cleanup object - * - * @param loc The cleanup object to be initialized - * @param callbacks An array of free()-like callbacks. Must be `size` elements long - * @param arguments An array of void pointers. Must be `size` elements long - * @param size The number of elements the callbacks and arguments array are long - * @retval (int)[-1, 0] Returns 0 on success, -1 on error - */ -int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size) { - if(!loc || !callbacks || !arguments || size <= 0) ERRRET(EINVAL, -1); - - loc->callbacks = callbacks; - loc->arguments = arguments; - loc->size = size; - loc->used = 0; - - return 0; -} - -/** - * @brief Register a new callback and argument onto a cleanup stack - * - * @param loc The cleanup object to modify - * @param cb A free()-like callback to run - * @param arg A piece of data for the callback to run - * @retval (int)[-1, 0] Returns 0 on success, -1 on error - */ -int cleanup_register(cleanup *loc, fcallback cb, void *arg) { - if(!loc || !cb) ERRRET(EINVAL, -1); - if(loc->used >= loc->size || loc->used < 0) ERRRET(ENOMEM, -1); - - loc->callbacks[loc->used] = cb; - loc->arguments[loc->used] = arg; - loc->used++; - - return 0; -} - -/** - * @brief Conditionally register a callback and argument - * - * @param loc The cleanup object to modify - * @param cb A free()-like callback to run - * @param arg A piece of data for the callback to run - * @param flag Whether or not the register should take place. Will not run if `flag` is non-zero - * @retval (int)[-1, 0] Returns 0 on success or skip, -1 on error - */ -int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag) { - if(flag) return 0; - return cleanup_register(loc, cb, arg); -} - -/** - * @brief Clear a cleanup object - * @attention Does not free any registered callbacks or arguments, just marks them as available space - * - * @param loc The cleanup object to modify - * @retval (int)[-1, 0] Returns 0 on success, -1 on error - */ -int cleanup_clear(cleanup *loc) { - if(!loc) ERRRET(EINVAL, -1); - loc->used = 0; - return 0; -} - -/** - * @brief Fires all the registered callbacks and arguments in a cleanup object in FIFO (stack) order - * - * @param loc The cleanup object to fire - * @retval (int)[-1, 0] Returns 0 on success, -1 on error - */ -int cleanup_fire(cleanup *loc) { - if(!loc) ERRRET(EINVAL, -1); - - for(int i = (loc->used - 1); i >= 0; i--) { - if(loc->callbacks[i] == NULL) { - error(0, EINVAL, "cleanup_fire: refusing to run null callback..."); - continue; - } - - loc->callbacks[i](loc->arguments[i]); - } - cleanup_clear(loc); - - return 0; -} - -/** - * @brief Conditionally fires a cleanup object - * - * @param loc The cleanup object in question - * @param flag Whether the object should be fired. Will skip firing if non-zero - * @retval (int)[-1, 0] Returns 0 on success, -1 on error - */ -int cleanup_cndfire(cleanup *loc, unsigned char flag) { - if(flag) - return cleanup_fire(loc); - return 0; -} - -/** - * @brief Initializes a set of variables suitable for use in the cleanup macros - * @param size The number of elements long each array should be - */ -#define cleanup_CREATE(size) \ -cleanup __CLEANUP; \ -fcallback __CLEANUP_FUNCS[(size)]; \ -void *__CLEANUP_ARGS[(size)]; \ -unsigned char __FLAG = 0; \ -cleanup_init(&__CLEANUP, __CLEANUP_FUNCS, __CLEANUP_ARGS, (size)) - -//! Register a callback-argument pair using the local cleanup object -#define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg)) -//! Conditionally register a callback-argument pair using the local cleanup object -#define cleanup_CNDREGISTER(cb, arg) cleanup_cndregister(&__CLEANUP, (cb), (arg), __FLAG) -//! Clean the local cleanup object -#define cleanup_CLEAR() cleanup_clear(&__CLEANUP) -//! Fire the local cleanup object -#define cleanup_FIRE() cleanup_fire(&__CLEANUP) -//! Conditionally fire the local cleanup object -#define cleanup_CNDFIRE() cleanup_cndfire(&__CLEANUP, __FLAG) -//! Set the local cleanup flag to a non-zero number -#define cleanup_MARK() (__FLAG = 1) -//! Set the local cleanup flag to zero -#define cleanup_UNMARK() (__FLAG = 0) -//! Check if the local cleanup flag is non-zero -#define cleanup_ERRORFLAGGED (__FLAG != 0) -//! Conditionally execute some `code` if the local cleanup flag has not been marked -#define cleanup_CNDEXEC(code) while(!cleanup_ERRORFLAGGED) {code; break;} -//! Conditionally fire the local cleanup object and return `ret` -#define cleanup_CNDFIRERET(ret) do {cleanup_CNDFIRE(); return ret;} while (0) - #endif \ No newline at end of file -- cgit v1.2.3