diff -r 949908c97474 -r 2be8fe3d5a2d src/cx/array_list.h --- a/src/cx/array_list.h Wed Jan 24 22:19:05 2024 +0100 +++ b/src/cx/array_list.h Thu Jan 25 22:01:12 2024 +0100 @@ -96,7 +96,7 @@ /** * A default stdlib-based array reallocator. */ -extern struct cx_array_reallocator_s cx_array_default_reallocator; +extern struct cx_array_reallocator_s *cx_array_default_reallocator; /** * Return codes for cx_array_copy(). @@ -117,7 +117,7 @@ * capacity is used. * * If the capacity is insufficient to hold the new data, a reallocation - * attempt is made, unless the allocator is set to \c NULL, in which case + * attempt is made, unless the \p reallocator is set to \c NULL, in which case * this function ultimately returns a failure. * * @param target the target array @@ -143,6 +143,28 @@ struct cx_array_reallocator_s *reallocator ) __attribute__((__nonnull__(1, 2, 5))); +/** + * Adds an element to an array with the possibility of allocating more space. + * + * The element \p elem is added to the end of the \p target array which containing + * \p size elements, already. The \p capacity must not be \c NULL and point a + * variable holding the current maximum number of elements the array can hold. + * + * If the capacity is insufficient to hold the new element, and the optional + * \p reallocator is not \c NULL, an attempt increase the \p capacity is made + * and the new capacity is written back. + * + * @param target the target array + * @param size a pointer to the size of the target array + * @param capacity a pointer to the target array's capacity - must not be \c NULL + * @param elem_size the size of one element + * @param elem the element to add + * @param reallocator the array re-allocator to use, or \c NULL + * if re-allocation shall not happen + * @return zero on success, non-zero error code on failure + */ +#define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ + cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator) /** * Swaps two array elements.