diff options
Diffstat (limited to 'src/shared.h')
| -rw-r--r-- | src/shared.h | 92 |
1 files changed, 33 insertions, 59 deletions
diff --git a/src/shared.h b/src/shared.h index 66d1af3..adaad4a 100644 --- a/src/shared.h +++ b/src/shared.h | |||
| @@ -1,11 +1,9 @@ | |||
| 1 | #ifndef __VXGG_REWRITE___SHARED_H___3880294315821___ | 1 | #ifndef __VXGG_REWRITE___SHARED_H___3880294315821___ |
| 2 | #define __VXGG_REWRITE___SHARED_H___3880294315821___ | 2 | #define __VXGG_REWRITE___SHARED_H___3880294315821___ 1 |
| 3 | 3 | ||
| 4 | #include <stddef.h> | 4 | #include <stddef.h> |
| 5 | 5 | ||
| 6 | #define STATIC_ARRAY_LEN(arr) (sizeof((arr))/sizeof((arr)[0])) | 6 | #define STATIC_ARRAY_LEN(arr) (sizeof((arr))/sizeof((arr)[0])) |
| 7 | #define FALSE 0 | ||
| 8 | #define TRUE 1 | ||
| 9 | 7 | ||
| 10 | typedef int (*gcallback)(void*); // Generic callback signature | 8 | typedef int (*gcallback)(void*); // Generic callback signature |
| 11 | typedef void (*fcallback)(void*); // free()-like callback signature | 9 | typedef void (*fcallback)(void*); // free()-like callback signature |
| @@ -44,82 +42,58 @@ typedef void (*fcallback)(void*); // free()-like callback signature | |||
| 44 | 42 | ||
| 45 | // `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` | 43 | // `malloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` |
| 46 | void * xmalloc(size_t size); | 44 | void * xmalloc(size_t size); |
| 47 | |||
| 48 | // `calloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` | 45 | // `calloc()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` |
| 49 | void * xcalloc(size_t nmemb, size_t size); | 46 | void * xcalloc(size_t nmemb, size_t size); |
| 50 | |||
| 51 | // `reallocarray()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` | 47 | // `reallocarray()` with error checking. Calls `exit()` or `abort()` on error, depending on the value of `___VXGG___XALLOC_EXIT_ON_ERROR___` |
| 52 | void * xreallocarray(void *ptr, size_t nmemb, size_t size); | 48 | void * xreallocarray(void *ptr, size_t nmemb, size_t size); |
| 53 | |||
| 54 | // Read the entire contents of a file descriptor into a malloc()'ed buffer | 49 | // Read the entire contents of a file descriptor into a malloc()'ed buffer |
| 55 | int rwbuf(char **str, unsigned long int initsize, int fd); | 50 | int rwbuf(char **str, unsigned long int initsize, int fd); |
| 56 | |||
| 57 | // Write the entire contents of a buffer into a file descriptor | 51 | // Write the entire contents of a buffer into a file descriptor |
| 58 | int wwbuf(int fd, const unsigned char *buf, int len); | 52 | int wwbuf(int fd, const unsigned char *buf, int len); |
| 59 | |||
| 60 | // `dirname()` reimplementation that returns a malloc()'ed string. According to the `x___` naming scheme, exits/aborts on alloc error. | 53 | // `dirname()` reimplementation that returns a malloc()'ed string. According to the `x___` naming scheme, exits/aborts on alloc error. |
| 61 | char * xdirname(const char * const path); | 54 | char * xdirname(const char * const path); |
| 62 | 55 | ||
| 63 | 56 | ||
| 64 | 57 | ||
| 65 | // Cleanup callback. Should act like `free()`, in that it doesn't crash if the pointer it's given is null | 58 | // A locally defined structure designed for easier function cleanup |
| 66 | typedef fcallback cleanup_callback; | ||
| 67 | |||
| 68 | // Cleanup struct. Stores a STATICALLY defined array of callbacks and void* arguments | ||
| 69 | typedef struct cl { | 59 | typedef struct cl { |
| 70 | cleanup_callback *funcs; // Actual type: cleanup_callback funcs[] | 60 | fcallback *callbacks; // Actual Type: fcallback callbacks[] |
| 71 | void **args; // Actual type: void *args[] | 61 | void * *arguments; // Actual Type: void *arguments[] |
| 72 | 62 | int size; | |
| 73 | int size; | 63 | int used; |
| 74 | int used; | ||
| 75 | } cleanup; | 64 | } cleanup; |
| 76 | 65 | ||
| 77 | // 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 | 66 | int cleanup_init(cleanup *loc, fcallback callbacks[], void *arguments[], int size); |
| 78 | int cleanup_init(cleanup * const loc, int size, cleanup_callback funcs[], void *args[]); | 67 | int cleanup_register(cleanup *loc, fcallback cb, void *arg); |
| 79 | 68 | int cleanup_cndregister(cleanup *loc, fcallback cb, void *arg, unsigned char flag); | |
| 80 | // Register a cleanup callback for a given cleanup object | 69 | int cleanup_clear(cleanup *loc); |
| 81 | int cleanup_register(cleanup * const loc, cleanup_callback cb, void *arg); | 70 | int cleanup_fire(cleanup *loc); |
| 82 | 71 | int cleanup_cndfire(cleanup *loc, unsigned char flag); | |
| 83 | // Register a cleanup callback, if and only if `flag == 0` | ||
| 84 | int cleanup_cndregister(cleanup * const loc, unsigned char flag, cleanup_callback cb, void *arg); | ||
| 85 | |||
| 86 | // Clear the contents of a cleanup stack | ||
| 87 | int cleanup_clear(cleanup * const loc); | ||
| 88 | |||
| 89 | // Get the top callback without removing it from the cleanup stack | ||
| 90 | cleanup_callback cleanup_peekf(cleanup * const loc); | ||
| 91 | |||
| 92 | // Get and remove the top callback from the cleanup stack. Does not return the argument for the given callback | ||
| 93 | cleanup_callback cleanup_popf(cleanup * const loc); | ||
| 94 | |||
| 95 | // Get the top argument without removing it from the cleanup stack | ||
| 96 | void * cleanup_peeka(cleanup * const loc); | ||
| 97 | |||
| 98 | // Get and remove the top argument from the cleanup stack. Does not return the callback it was to be fed into | ||
| 99 | void * cleanup_popa(cleanup * const loc); | ||
| 100 | |||
| 101 | // Fire all the callbacks in the cleanup stack | ||
| 102 | int cleanup_fire(cleanup * const loc); | ||
| 103 | 72 | ||
| 104 | /* Cleanup environment creator. Automatically defines the variables `__CLEANUP`, `__CLEANUP_FUNCS`, and `__CLEANUP_ARGS` and initializes | ||
| 105 | // via `cleanup_init()` using these variables. */ | ||
| 106 | #define cleanup_CREATE(size) \ | 73 | #define cleanup_CREATE(size) \ |
| 107 | cleanup __CLEANUP; \ | 74 | cleanup __CLEANUP; \ |
| 108 | cleanup_callback __CLEANUP_FUNCS[(size)]; \ | 75 | fcallback __CLEANUP_FUNCS[(size)]; \ |
| 109 | void *__CLEANUP_ARGS[(size)]; \ | 76 | void *__CLEANUP_ARGS[(size)]; \ |
| 110 | unsigned char __FLAG = 0; \ | 77 | unsigned char __FLAG = 0; \ |
| 111 | cleanup_init(&__CLEANUP, (size), __CLEANUP_FUNCS, __CLEANUP_ARGS) | 78 | cleanup_init(&__CLEANUP, __CLEANUP_FUNCS, __CLEANUP_ARGS, (size)) |
| 112 | 79 | ||
| 113 | #define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg)) | 80 | #define cleanup_REGISTER(cb, arg) cleanup_register(&__CLEANUP, (cb), (arg)) |
| 114 | #define cleanup_CNDREGISTER(cb, arg) cleanup_cndregister(&__CLEANUP, __FLAG, (cb), (arg)) | 81 | #define cleanup_CNDREGISTER(cb, arg) cleanup_cndregister(&__CLEANUP, (cb), (arg), __FLAG) |
| 115 | #define cleanup_CLEAR() cleanup_clear(&__CLEANUP) | 82 | #define cleanup_CLEAR() cleanup_clear(&__CLEANUP) |
| 116 | #define cleanup_PEEKF() cleanup_peekf(&__CLEANUP) | 83 | #define cleanup_FIRE() cleanup_fire(&__CLEANUP) |
| 117 | #define cleanup_POPF() cleanup_popf(&__CLEANUP) | 84 | #define cleanup_CNDFIRE() cleanup_cndfire(&__CLEANUP, __FLAG) |
| 118 | #define cleanup_PEEKA() cleanup_peeka(&__CLEANUP) | 85 | #define cleanup_MARK() (__FLAG = 1) |
| 119 | #define cleanup_POPA() cleanup_popa(&__CLEANUP) | 86 | #define cleanup_UNMARK() (__FLAG = 0) |
| 120 | #define cleanup_FIRE() cleanup_fire(&__CLEANUP) | 87 | #define cleanup_ERRORFLAGGED (__FLAG != 0) |
| 121 | #define cleanup_MARK() (__FLAG = 1) | 88 | #define cleanup_CNDEXEC(code) while(!cleanup_ERRORFLAGGED) {code; break;} |
| 122 | #define cleanup_UNMARK() (__FLAG = 0) | 89 | |
| 123 | #define cleanup_ERRORFLAGGED (__FLAG != 0) | 90 | |
| 91 | |||
| 92 | // Generic task to be executed by a consumer | ||
| 93 | typedef struct task task; | ||
| 94 | // A queue of tasks | ||
| 95 | typedef struct taskqueue taskqueue; | ||
| 96 | // A concurrent queue of tasks, basically a threadpool tasks can be dispatched to | ||
| 97 | typedef struct ctqueue ctqueue; | ||
| 124 | 98 | ||
| 125 | #endif \ No newline at end of file | 99 | #endif \ No newline at end of file |
