1.1 --- a/src/cx/array_list.h Sun Feb 18 12:24:04 2024 +0100 1.2 +++ b/src/cx/array_list.h Sun Feb 18 13:01:09 2024 +0100 1.3 @@ -50,14 +50,25 @@ 1.4 extern unsigned cx_array_swap_sbo_size; 1.5 1.6 /** 1.7 + * Declares variables for an array that can be used with the convenience macros. 1.8 + * 1.9 + * @see cx_array_simple_add() 1.10 + * @see cx_array_simple_copy() 1.11 + */ 1.12 +#define cx_array_declare(type, name) \ 1.13 + type * name; \ 1.14 + size_t name##_size; \ 1.15 + size_t name##_capacity; 1.16 + 1.17 +/** 1.18 * Defines a reallocation mechanism for arrays. 1.19 */ 1.20 struct cx_array_reallocator_s { 1.21 /** 1.22 - * Re-allocates space for the given array. 1.23 + * Reallocates space for the given array. 1.24 * 1.25 * Implementations are not required to free the original array. 1.26 - * This allows re-allocation of static memory by allocating heap memory 1.27 + * This allows reallocation of static memory by allocating heap memory 1.28 * and copying the array contents. The information in the custom fields of 1.29 * the referenced allocator can be used to track the state of the memory 1.30 * or to transport other additional data. 1.31 @@ -120,7 +131,7 @@ 1.32 * attempt is made, unless the \p reallocator is set to \c NULL, in which case 1.33 * this function ultimately returns a failure. 1.34 * 1.35 - * @param target the target array 1.36 + * @param target a pointer to the target array 1.37 * @param size a pointer to the size of the target array 1.38 * @param capacity a pointer to the target array's capacity - 1.39 * \c NULL if only the size shall be used to bound the array 1.40 @@ -128,8 +139,8 @@ 1.41 * @param src the source array 1.42 * @param elem_size the size of one element 1.43 * @param elem_count the number of elements to copy 1.44 - * @param reallocator the array re-allocator to use, or \c NULL 1.45 - * if re-allocation shall not happen 1.46 + * @param reallocator the array reallocator to use, or \c NULL 1.47 + * if reallocation shall not happen 1.48 * @return zero on success, non-zero error code on failure 1.49 */ 1.50 enum cx_array_result cx_array_copy( 1.51 @@ -144,6 +155,18 @@ 1.52 ) __attribute__((__nonnull__(1, 2, 5))); 1.53 1.54 /** 1.55 + * Convenience macro that uses cx_array_copy() with a default layout and the default reallocator. 1.56 + * 1.57 + * @param array the name of the array (NOT a pointer to the array) 1.58 + * @param index the index where the copied elements shall be placed 1.59 + * @param src the source array 1.60 + * @param count the number of elements to copy 1.61 + */ 1.62 +#define cx_array_simple_copy(array, index, src, count) \ 1.63 + cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \ 1.64 + index, src, sizeof((array)[0]), count, cx_array_default_reallocator) 1.65 + 1.66 +/** 1.67 * Adds an element to an array with the possibility of allocating more space. 1.68 * 1.69 * The element \p elem is added to the end of the \p target array which containing 1.70 @@ -154,19 +177,27 @@ 1.71 * \p reallocator is not \c NULL, an attempt increase the \p capacity is made 1.72 * and the new capacity is written back. 1.73 * 1.74 - * @param target the target array 1.75 + * @param target a pointer to the target array 1.76 * @param size a pointer to the size of the target array 1.77 * @param capacity a pointer to the target array's capacity - must not be \c NULL 1.78 * @param elem_size the size of one element 1.79 * @param elem the element to add 1.80 - * @param reallocator the array re-allocator to use, or \c NULL 1.81 - * if re-allocation shall not happen 1.82 + * @param reallocator the array reallocator to use, or \c NULL if reallocation shall not happen 1.83 * @return zero on success, non-zero error code on failure 1.84 */ 1.85 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ 1.86 cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator) 1.87 1.88 /** 1.89 + * Convenience macro that uses cx_array_add() with a default layout and the default reallocator. 1.90 + * 1.91 + * @param array the name of the array (NOT a pointer to the array) 1.92 + * @param elem the element to add 1.93 + */ 1.94 +#define cx_array_simple_add(array, elem) \ 1.95 + cx_array_simple_copy(array, array##_size, elem, 1) 1.96 + 1.97 +/** 1.98 * Swaps two array elements. 1.99 * 1.100 * @param arr the array