summaryrefslogtreecommitdiff
path: root/src/shared.h
diff options
context:
space:
mode:
author@syxhe <https://t.me/syxhe>2025-04-21 17:31:37 -0500
committer@syxhe <https://t.me/syxhe>2025-04-21 17:31:37 -0500
commit9cf4667331b97f1123f4156273a46558e27c2d2d (patch)
tree091766ab0cb3345bc9e61a2387dc65ca7714569c /src/shared.h
parentd47f45a5e3e40b48131409071b119b442c78bffc (diff)
Start work on cqueue implementation, create cleanup set of functions
Diffstat (limited to 'src/shared.h')
-rw-r--r--src/shared.h32
1 files changed, 32 insertions, 0 deletions
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);
50// `dirname()` reimplementation that returns a malloc()'ed string. According to the `x___` naming scheme, exits/aborts on alloc error. 50// `dirname()` reimplementation that returns a malloc()'ed string. According to the `x___` naming scheme, exits/aborts on alloc error.
51char * xdirname(const char * const path); 51char * xdirname(const char * const path);
52 52
53
54
55// Cleanup callback. Should act like `free()`, in that it doesn't crash if the pointer it's given is null
56typedef void (*cleanup_callback)(void*);
57
58// Cleanup struct. Stores a STATICALLY defined array of callbacks and void* arguments
59typedef struct cl {
60 cleanup_callback *funcs;
61 void **args;
62
63 int size;
64 int used;
65} cleanup;
66
67int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]);
68int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg);
69int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg);
70int cleanup_clear(cleanup * const loc);
71cleanup_callback cleanup_peekf(cleanup * const loc);
72cleanup_callback cleanup_popf(cleanup * const loc);
73void * cleanup_peeka(cleanup * const loc);
74void * cleanup_popa(cleanup * const loc);
75int cleanup_fire(cleanup * const loc);
76
77/* Cleanup environment creator. Automatically defines the variables `__CLEANUP`, `__CLEANUP_FUNCS`, and `__CLEANUP_ARGS` and initializes
78// via `cleanup_init()` using these variables. */
79#define cleanup_create(size) \
80cleanup __CLEANUP; \
81cleanup_callback __CLEANUP_FUNCS[(size)]; \
82void *__CLEANUP_ARGS[(size)]; \
83cleanup_init(&__CLEANUP, (size), __CLEANUP_FUNCS, __CLEANUP_ARGS)
84
53#endif \ No newline at end of file 85#endif \ No newline at end of file