diff options
Diffstat (limited to 'src/shared.h')
| -rw-r--r-- | src/shared.h | 31 |
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 | ||
| 72 | int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]); | 73 | int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]); |
| 74 | |||
| 75 | // Register a cleanup callback for a given cleanup object | ||
| 73 | int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg); | 76 | int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg); |
| 77 | |||
| 78 | // Register a cleanup callback, if and only if `flag == 0` | ||
| 74 | int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg); | 79 | int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg); |
| 80 | |||
| 81 | // Clear the contents of a cleanup stack | ||
| 75 | int cleanup_clear(cleanup * const loc); | 82 | int cleanup_clear(cleanup * const loc); |
| 83 | |||
| 84 | // Get the top callback without removing it from the cleanup stack | ||
| 76 | cleanup_callback cleanup_peekf(cleanup * const loc); | 85 | cleanup_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 | ||
| 77 | cleanup_callback cleanup_popf(cleanup * const loc); | 88 | cleanup_callback cleanup_popf(cleanup * const loc); |
| 89 | |||
| 90 | // Get the top argument without removing it from the cleanup stack | ||
| 78 | void * cleanup_peeka(cleanup * const loc); | 91 | void * 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 | ||
| 79 | void * cleanup_popa(cleanup * const loc); | 94 | void * cleanup_popa(cleanup * const loc); |
| 95 | |||
| 96 | // Fire all the callbacks in the cleanup stack | ||
| 80 | int cleanup_fire(cleanup * const loc); | 97 | int 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); | |||
| 85 | cleanup __CLEANUP; \ | 102 | cleanup __CLEANUP; \ |
| 86 | cleanup_callback __CLEANUP_FUNCS[(size)]; \ | 103 | cleanup_callback __CLEANUP_FUNCS[(size)]; \ |
| 87 | void *__CLEANUP_ARGS[(size)]; \ | 104 | void *__CLEANUP_ARGS[(size)]; \ |
| 105 | unsigned char __FLAG = 0; \ | ||
| 88 | cleanup_init(&__CLEANUP, (size), __CLEANUP_FUNCS, __CLEANUP_ARGS) | 106 | cleanup_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 |
