Sun, 13 Nov 2022 13:22:03 +0100
proposal for a low level array copy
src/cx/array_list.h | file | annotate | diff | comparison | revisions |
1.1 --- a/src/cx/array_list.h Sun Nov 13 13:21:48 2022 +0100 1.2 +++ b/src/cx/array_list.h Sun Nov 13 13:22:03 2022 +0100 1.3 @@ -46,6 +46,72 @@ 1.4 #endif 1.5 1.6 /** 1.7 + * Defines a reallocation mechanism for arrays. 1.8 + */ 1.9 +struct cx_array_reallocator_s { 1.10 + /** 1.11 + * Re-allocates space for the given array. 1.12 + * 1.13 + * Implementations are not required to free the original array. 1.14 + * This allows re-allocation of static memory by allocating heap memory 1.15 + * and copying the array contents. The information in \p data can keep 1.16 + * track of the state of the memory or other additional allocator info. 1.17 + * 1.18 + * @param array the array to reallocate 1.19 + * @param capacity the new capacity (number of elements) 1.20 + * @param elem_size the size of each element 1.21 + * @param data additional data 1.22 + * @return a pointer to the reallocated memory or \c NULL on failure 1.23 + */ 1.24 + void *(*realloc)( 1.25 + void *array, 1.26 + size_t capacity, 1.27 + size_t elem_size, 1.28 + void *data 1.29 + ); 1.30 + 1.31 + /** 1.32 + * Additional data. 1.33 + */ 1.34 + void *data; 1.35 +}; 1.36 + 1.37 +/** 1.38 + * Copies elements from one array to another. 1.39 + * 1.40 + * The elements are copied to the \p target array at the specified \p index, 1.41 + * overwriting possible elements. The index must be in range of the current 1.42 + * array \p size. If the number of elements added would extend the array's size, 1.43 + * and \p capacity is not \c NULL, the remaining capacity is used. 1.44 + * 1.45 + * If the capacity is insufficient to hold the new data, a reallocation 1.46 + * attempt is made, unless the allocator is set to \c NULL, in which case 1.47 + * this function ultimately returns a failure. 1.48 + * 1.49 + * @param target the target array 1.50 + * @param size a pointer to the size of the target array 1.51 + * @param capacity a pointer to the target array's capacity - 1.52 + * \c NULL if only the size shall be used to bound the array 1.53 + * @param index the index where the copied elements shall be placed 1.54 + * @param src the source array 1.55 + * @param elem_size the size of one element 1.56 + * @param elem_count the number of elements to copy 1.57 + * @param reallocator the array re-allocator to use, or \c NULL 1.58 + * if re-allocation shall not happen 1.59 + * @return zero on success, non-zero on failure 1.60 + */ 1.61 +int cx_array_copy( 1.62 + void **target, 1.63 + size_t *size, 1.64 + size_t *capacity, 1.65 + size_t index, 1.66 + void const *src, 1.67 + size_t elem_size, 1.68 + size_t elem_count, 1.69 + struct cx_array_reallocator_s const *reallocator 1.70 +) __attribute__((__nonnull__(1, 2, 5))); 1.71 + 1.72 +/** 1.73 * Allocates an array list for storing elements with \p item_size bytes each. 1.74 * 1.75 * @param allocator the allocator for allocating the list memory