From 9cf4667331b97f1123f4156273a46558e27c2d2d Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 21 Apr 2025 17:31:37 -0500 Subject: Start work on cqueue implementation, create cleanup set of functions --- src/shared.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/shared.h') diff --git a/src/shared.h b/src/shared.h index b7fe7c3..1a629e6 100644 --- a/src/shared.h +++ b/src/shared.h @@ -50,4 +50,36 @@ int wwbuf(int fd, const unsigned char *buf, int len); // `dirname()` reimplementation that returns a malloc()'ed string. According to the `x___` naming scheme, exits/aborts on alloc error. char * xdirname(const char * const path); + + +// Cleanup callback. Should act like `free()`, in that it doesn't crash if the pointer it's given is null +typedef void (*cleanup_callback)(void*); + +// Cleanup struct. Stores a STATICALLY defined array of callbacks and void* arguments +typedef struct cl { + cleanup_callback *funcs; + void **args; + + int size; + int used; +} cleanup; + +int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]); +int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg); +int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg); +int cleanup_clear(cleanup * const loc); +cleanup_callback cleanup_peekf(cleanup * const loc); +cleanup_callback cleanup_popf(cleanup * const loc); +void * cleanup_peeka(cleanup * const loc); +void * cleanup_popa(cleanup * const loc); +int cleanup_fire(cleanup * const loc); + +/* Cleanup environment creator. Automatically defines the variables `__CLEANUP`, `__CLEANUP_FUNCS`, and `__CLEANUP_ARGS` and initializes +// via `cleanup_init()` using these variables. */ +#define cleanup_create(size) \ +cleanup __CLEANUP; \ +cleanup_callback __CLEANUP_FUNCS[(size)]; \ +void *__CLEANUP_ARGS[(size)]; \ +cleanup_init(&__CLEANUP, (size), __CLEANUP_FUNCS, __CLEANUP_ARGS) + #endif \ No newline at end of file -- cgit v1.2.3