src/cx/array_list.h

changeset 818
2be8fe3d5a2d
parent 817
949908c97474
child 819
5da2ead43077
     1.1 --- a/src/cx/array_list.h	Wed Jan 24 22:19:05 2024 +0100
     1.2 +++ b/src/cx/array_list.h	Thu Jan 25 22:01:12 2024 +0100
     1.3 @@ -96,7 +96,7 @@
     1.4  /**
     1.5   * A default stdlib-based array reallocator.
     1.6   */
     1.7 -extern struct cx_array_reallocator_s cx_array_default_reallocator;
     1.8 +extern struct cx_array_reallocator_s *cx_array_default_reallocator;
     1.9  
    1.10  /**
    1.11   * Return codes for cx_array_copy().
    1.12 @@ -117,7 +117,7 @@
    1.13   * capacity is used.
    1.14   *
    1.15   * If the capacity is insufficient to hold the new data, a reallocation
    1.16 - * attempt is made, unless the allocator is set to \c NULL, in which case
    1.17 + * attempt is made, unless the \p reallocator is set to \c NULL, in which case
    1.18   * this function ultimately returns a failure.
    1.19   *
    1.20   * @param target the target array
    1.21 @@ -143,6 +143,28 @@
    1.22          struct cx_array_reallocator_s *reallocator
    1.23  ) __attribute__((__nonnull__(1, 2, 5)));
    1.24  
    1.25 +/**
    1.26 + * Adds an element to an array with the possibility of allocating more space.
    1.27 + *
    1.28 + * The element \p elem is added to the end of the \p target array which containing
    1.29 + * \p size elements, already. The \p capacity must not be \c NULL and point a
    1.30 + * variable holding the current maximum number of elements the array can hold.
    1.31 + *
    1.32 + * If the capacity is insufficient to hold the new element, and the optional
    1.33 + * \p reallocator is not \c NULL, an attempt increase the \p capacity is made
    1.34 + * and the new capacity is written back.
    1.35 + *
    1.36 + * @param target 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 - must not be \c NULL
    1.39 + * @param elem_size the size of one element
    1.40 + * @param elem the element to add
    1.41 + * @param reallocator the array re-allocator to use, or \c NULL
    1.42 + * if re-allocation shall not happen
    1.43 + * @return zero on success, non-zero error code on failure
    1.44 + */
    1.45 +#define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \
    1.46 +    cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator)
    1.47  
    1.48  /**
    1.49   * Swaps two array elements.

mercurial