|
| 1 | +#ifndef PYAWAITABLE_ARRAY_H |
| 2 | +#define PYAWAITABLE_ARRAY_H |
| 3 | + |
| 4 | +#include <Python.h> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +#define pyawaitable_array_DEFAULT_SIZE 16 |
| 8 | + |
| 9 | +/* |
| 10 | + * Deallocator for items on a pyawaitable_array structure. A NULL pointer |
| 11 | + * will never be given to the deallocator. |
| 12 | + */ |
| 13 | +typedef void (*pyawaitable_array_deallocator)(void *); |
| 14 | + |
| 15 | +/* |
| 16 | + * Internal only dynamic array for CPython. |
| 17 | + */ |
| 18 | +typedef struct |
| 19 | +{ |
| 20 | + /* |
| 21 | + * The actual items in the dynamic array. |
| 22 | + * Don't access this field publicly to get |
| 23 | + * items--use pyawaitable_array_GET_ITEM() instead. |
| 24 | + */ |
| 25 | + void **items; |
| 26 | + /* |
| 27 | + * The length of the actual items array allocation. |
| 28 | + */ |
| 29 | + Py_ssize_t capacity; |
| 30 | + /* |
| 31 | + * The number of items in the array. |
| 32 | + * Don't use this field publicly--use pyawaitable_array_LENGTH() |
| 33 | + */ |
| 34 | + Py_ssize_t length; |
| 35 | + /* |
| 36 | + * The deallocator, set by one of the initializer functions. |
| 37 | + * This may be NULL. |
| 38 | + */ |
| 39 | + pyawaitable_array_deallocator deallocator; |
| 40 | +} pyawaitable_array; |
| 41 | + |
| 42 | + |
| 43 | +/* Zero out the array */ |
| 44 | +static inline void |
| 45 | +pyawaitable_array_ZERO(pyawaitable_array *array) |
| 46 | +{ |
| 47 | + assert(array != NULL); |
| 48 | + array->deallocator = NULL; |
| 49 | + array->items = NULL; |
| 50 | + array->length = 0; |
| 51 | + array->capacity = 0; |
| 52 | +} |
| 53 | + |
| 54 | +static inline void |
| 55 | +pyawaitable_array_ASSERT_VALID(pyawaitable_array *array) |
| 56 | +{ |
| 57 | + assert(array != NULL); |
| 58 | + assert(array->items != NULL); |
| 59 | +} |
| 60 | + |
| 61 | +static inline void |
| 62 | +pyawaitable_array_ASSERT_INDEX(pyawaitable_array *array, Py_ssize_t index) |
| 63 | +{ |
| 64 | + // Ensure the index is valid |
| 65 | + assert(index < array->length); |
| 66 | + assert(index >= 0); |
| 67 | +} |
| 68 | + |
| 69 | +/* |
| 70 | + * Initialize a dynamic array with an initial size and deallocator. |
| 71 | + * |
| 72 | + * If the deallocator is NULL, then nothing happens to items upon |
| 73 | + * removal and upon array clearing. |
| 74 | + * |
| 75 | + * Returns -1 upon failure, 0 otherwise. |
| 76 | + */ |
| 77 | +int |
| 78 | +pyawaitable_array_init_with_size( |
| 79 | + pyawaitable_array *array, |
| 80 | + pyawaitable_array_deallocator deallocator, |
| 81 | + Py_ssize_t initial |
| 82 | +); |
| 83 | + |
| 84 | +/* |
| 85 | + * Append to the array. |
| 86 | + * |
| 87 | + * Returns -1 upon failure, 0 otherwise. |
| 88 | + * If this fails, the deallocator is not ran on the item. |
| 89 | + */ |
| 90 | +int pyawaitable_array_append(pyawaitable_array *array, void *item); |
| 91 | + |
| 92 | +/* |
| 93 | + * Insert an item at the target index. The index |
| 94 | + * must currently be a valid index in the array. |
| 95 | + * |
| 96 | + * Returns -1 upon failure, 0 otherwise. |
| 97 | + * If this fails, the deallocator is not ran on the item. |
| 98 | + */ |
| 99 | +int |
| 100 | +pyawaitable_array_insert( |
| 101 | + pyawaitable_array *array, |
| 102 | + Py_ssize_t index, |
| 103 | + void *item |
| 104 | +); |
| 105 | + |
| 106 | +/* Remove all items from the array. */ |
| 107 | +void |
| 108 | +pyawaitable_array_clear_items(pyawaitable_array *array); |
| 109 | + |
| 110 | +/* |
| 111 | + * Clear all the fields on the array. |
| 112 | + * |
| 113 | + * Note that this does *not* free the actual dynamic array |
| 114 | + * structure--use pyawaitable_array_Free() for that. |
| 115 | + * |
| 116 | + * It's safe to call pyawaitable_array_init() or init_with_size() again |
| 117 | + * on the array after calling this. |
| 118 | + */ |
| 119 | +void pyawaitable_array_clear(pyawaitable_array *array); |
| 120 | + |
| 121 | +/* |
| 122 | + * Set a value at index in the array. |
| 123 | + * |
| 124 | + * If an item already exists at the target index, the deallocator |
| 125 | + * is called on it, if the array has one set. |
| 126 | + * |
| 127 | + * This cannot fail. |
| 128 | + */ |
| 129 | +void |
| 130 | +pyawaitable_array_set(pyawaitable_array *array, Py_ssize_t index, void *item); |
| 131 | + |
| 132 | +/* |
| 133 | + * Remove the item at the index, and call the deallocator on it (if the array |
| 134 | + * has one set). |
| 135 | + * |
| 136 | + * This cannot fail. |
| 137 | + */ |
| 138 | +void |
| 139 | +pyawaitable_array_remove(pyawaitable_array *array, Py_ssize_t index); |
| 140 | + |
| 141 | +/* |
| 142 | + * Remove the item at the index *without* deallocating it, and |
| 143 | + * return the item. |
| 144 | + * |
| 145 | + * This cannot fail. |
| 146 | + */ |
| 147 | +void * |
| 148 | +pyawaitable_array_pop(pyawaitable_array *array, Py_ssize_t index); |
| 149 | + |
| 150 | +/* |
| 151 | + * Clear all the fields on a dynamic array, and then |
| 152 | + * free the dynamic array structure itself. |
| 153 | + * |
| 154 | + * The array must have been created by pyawaitable_array_new() |
| 155 | + */ |
| 156 | +static inline void |
| 157 | +pyawaitable_array_free(pyawaitable_array *array) |
| 158 | +{ |
| 159 | + pyawaitable_array_ASSERT_VALID(array); |
| 160 | + pyawaitable_array_clear(array); |
| 161 | + PyMem_RawFree(array); |
| 162 | +} |
| 163 | + |
| 164 | +/* |
| 165 | + * Equivalent to pyawaitable_array_init_with_size() with a default size of 16. |
| 166 | + * |
| 167 | + * Returns -1 upon failure, 0 otherwise. |
| 168 | + */ |
| 169 | +static inline int |
| 170 | +pyawaitable_array_init( |
| 171 | + pyawaitable_array *array, |
| 172 | + pyawaitable_array_deallocator deallocator |
| 173 | +) |
| 174 | +{ |
| 175 | + return pyawaitable_array_init_with_size( |
| 176 | + array, |
| 177 | + deallocator, |
| 178 | + pyawaitable_array_DEFAULT_SIZE |
| 179 | + ); |
| 180 | +} |
| 181 | + |
| 182 | +/* |
| 183 | + * Allocate and create a new dynamic array on the heap. |
| 184 | + * |
| 185 | + * The returned pointer should be freed with pyawaitable_array_free() |
| 186 | + * If this function fails, it returns NULL. |
| 187 | + */ |
| 188 | +static inline pyawaitable_array * |
| 189 | +pyawaitable_array_new_with_size( |
| 190 | + pyawaitable_array_deallocator deallocator, |
| 191 | + Py_ssize_t initial |
| 192 | +) |
| 193 | +{ |
| 194 | + pyawaitable_array *array = PyMem_Malloc(sizeof(pyawaitable_array)); |
| 195 | + if (array == NULL) |
| 196 | + { |
| 197 | + return NULL; |
| 198 | + } |
| 199 | + |
| 200 | + if (pyawaitable_array_init_with_size(array, deallocator, initial) < 0) |
| 201 | + { |
| 202 | + PyMem_Free(array); |
| 203 | + return NULL; |
| 204 | + } |
| 205 | + |
| 206 | + pyawaitable_array_ASSERT_VALID(array); // Sanity check |
| 207 | + return array; |
| 208 | +} |
| 209 | + |
| 210 | +/* |
| 211 | + * Equivalent to pyawaitable_array_new_with_size() with a size of 16. |
| 212 | + * |
| 213 | + * The returned array must be freed with pyawaitable_array_free(). |
| 214 | + * Returns NULL on failure. |
| 215 | + */ |
| 216 | +static inline pyawaitable_array * |
| 217 | +pyawaitable_array_new(pyawaitable_array_deallocator deallocator) |
| 218 | +{ |
| 219 | + return pyawaitable_array_new_with_size( |
| 220 | + deallocator, |
| 221 | + pyawaitable_array_DEFAULT_SIZE |
| 222 | + ); |
| 223 | +} |
| 224 | + |
| 225 | +/* |
| 226 | + * Get an item from the array. This cannot fail. |
| 227 | + * |
| 228 | + * If the index is not valid, this is undefined behavior. |
| 229 | + */ |
| 230 | +static inline void * |
| 231 | +pyawaitable_array_GET_ITEM(pyawaitable_array *array, Py_ssize_t index) |
| 232 | +{ |
| 233 | + pyawaitable_array_ASSERT_VALID(array); |
| 234 | + pyawaitable_array_ASSERT_INDEX(array, index); |
| 235 | + return array->items[index]; |
| 236 | +} |
| 237 | + |
| 238 | +/* |
| 239 | + * Get the length of the array. This cannot fail. |
| 240 | + */ |
| 241 | +static inline Py_ssize_t |
| 242 | +pyawaitable_array_LENGTH(pyawaitable_array *array) |
| 243 | +{ |
| 244 | + pyawaitable_array_ASSERT_VALID(array); |
| 245 | + return array->length; |
| 246 | +} |
| 247 | + |
| 248 | +/* |
| 249 | + * Pop the item at the end the array. |
| 250 | + * This function cannot fail. |
| 251 | + */ |
| 252 | +static inline void * |
| 253 | +pyawaitable_array_pop_top(pyawaitable_array *array) |
| 254 | +{ |
| 255 | + return pyawaitable_array_pop(array, pyawaitable_array_LENGTH(array) - 1); |
| 256 | +} |
| 257 | + |
| 258 | +#endif |
0 commit comments