summaryrefslogtreecommitdiff
path: root/src/shared.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared.h')
-rw-r--r--src/shared.h31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/shared.h b/src/shared.h
index 7034255..9e7eaa8 100644
--- a/src/shared.h
+++ b/src/shared.h
@@ -69,14 +69,31 @@ typedef struct cl {
69 int used; 69 int used;
70} cleanup; 70} cleanup;
71 71
72// Initialize a local cleanup stack. `loc`, `funcs` and `args` need to be locally defined, non allocated arrays, and must be at least `size` elements large
72int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]); 73int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]);
74
75// Register a cleanup callback for a given cleanup object
73int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg); 76int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg);
77
78// Register a cleanup callback, if and only if `flag == 0`
74int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg); 79int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg);
80
81// Clear the contents of a cleanup stack
75int cleanup_clear(cleanup * const loc); 82int cleanup_clear(cleanup * const loc);
83
84// Get the top callback without removing it from the cleanup stack
76cleanup_callback cleanup_peekf(cleanup * const loc); 85cleanup_callback cleanup_peekf(cleanup * const loc);
86
87// Get and remove the top callback from the cleanup stack. Does not return the argument for the given callback
77cleanup_callback cleanup_popf(cleanup * const loc); 88cleanup_callback cleanup_popf(cleanup * const loc);
89
90// Get the top argument without removing it from the cleanup stack
78void * cleanup_peeka(cleanup * const loc); 91void * cleanup_peeka(cleanup * const loc);
92
93// Get and remove the top argument from the cleanup stack. Does not return the callback it was to be fed into
79void * cleanup_popa(cleanup * const loc); 94void * cleanup_popa(cleanup * const loc);
95
96// Fire all the callbacks in the cleanup stack
80int cleanup_fire(cleanup * const loc); 97int cleanup_fire(cleanup * const loc);
81 98
82/* Cleanup environment creator. Automatically defines the variables `__CLEANUP`, `__CLEANUP_FUNCS`, and `__CLEANUP_ARGS` and initializes 99/* Cleanup environment creator. Automatically defines the variables `__CLEANUP`, `__CLEANUP_FUNCS`, and `__CLEANUP_ARGS` and initializes
@@ -85,9 +102,19 @@ int cleanup_fire(cleanup * const loc);
85cleanup __CLEANUP; \ 102cleanup __CLEANUP; \
86cleanup_callback __CLEANUP_FUNCS[(size)]; \ 103cleanup_callback __CLEANUP_FUNCS[(size)]; \
87void *__CLEANUP_ARGS[(size)]; \ 104void *__CLEANUP_ARGS[(size)]; \
105unsigned char __FLAG = 0; \
88cleanup_init(&__CLEANUP, (size), __CLEANUP_FUNCS, __CLEANUP_ARGS) 106cleanup_init(&__CLEANUP, (size), __CLEANUP_FUNCS, __CLEANUP_ARGS)
89 107
90#define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg)) 108#define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg))
91#define cleanup_CNDREGISTER(flag, cb, arg) cleanup_cndregister(&__CLEANUP, (flag), (cb), (arg)) 109#define cleanup_CNDREGISTER(cb, arg) cleanup_cndregister(&__CLEANUP, __FLAG, (cb), (arg))
110#define cleanup_CLEAR() cleanup_clear(&__CLEANUP)
111#define cleanup_PEEKF() cleanup_peekf(&__CLEANUP)
112#define cleanup_POPF() cleanup_popf(&__CLEANUP)
113#define cleanup_PEEKA() cleanup_peeka(&__CLEANUP)
114#define cleanup_POPA() cleanup_popa(&__CLEANUP)
115#define cleanup_FIRE() cleanup_fire(&__CLEANUP)
116#define cleanup_MARK() (__FLAG = 1)
117#define cleanup_UNMARK() (__FLAG = 0)
118#define cleanup_ERRORFLAGGED (__FLAG != 0)
92 119
93#endif \ No newline at end of file 120#endif \ No newline at end of file