UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 /** 29 * \file array_list.h 30 * \brief Array list implementation. 31 * \details Also provides several low-level functions for custom array list implementations. 32 * \author Mike Becker 33 * \author Olaf Wintermann 34 * \copyright 2-Clause BSD License 35 */ 36 37 38 #ifndef UCX_ARRAY_LIST_H 39 #define UCX_ARRAY_LIST_H 40 41 #include "list.h" 42 43 #ifdef __cplusplus 44 extern "C" { 45 #endif 46 47 /** 48 * The maximum item size in an array list that fits into stack buffer when swapped. 49 */ 50 extern unsigned cx_array_swap_sbo_size; 51 52 /** 53 * Declares variables for an array that can be used with the convenience macros. 54 * 55 * @see cx_array_simple_add() 56 * @see cx_array_simple_copy() 57 * @see cx_array_initialize() 58 */ 59 #define CX_ARRAY_DECLARE(type, name) \ 60 type * name; \ 61 size_t name##_size; \ 62 size_t name##_capacity 63 64 /** 65 * Initializes an array declared with CX_ARRAY_DECLARE(). 66 * 67 * The memory for the array is allocated with stdlib malloc(). 68 * @param array the array 69 * @param capacity the initial capacity 70 */ 71 #define cx_array_initialize(array, capacity) \ 72 array##_capacity = capacity; \ 73 array##_size = 0; \ 74 array = malloc(sizeof(array[0]) * capacity) 75 76 /** 77 * Defines a reallocation mechanism for arrays. 78 */ 79 struct cx_array_reallocator_s { 80 /** 81 * Reallocates space for the given array. 82 * 83 * Implementations are not required to free the original array. 84 * This allows reallocation of static memory by allocating heap memory 85 * and copying the array contents. The information in the custom fields of 86 * the referenced allocator can be used to track the state of the memory 87 * or to transport other additional data. 88 * 89 * @param array the array to reallocate 90 * @param capacity the new capacity (number of elements) 91 * @param elem_size the size of each element 92 * @param alloc a reference to this allocator 93 * @return a pointer to the reallocated memory or \c NULL on failure 94 */ 95 void *(*realloc)( 96 void *array, 97 size_t capacity, 98 size_t elem_size, 99 struct cx_array_reallocator_s *alloc 100 ); 101 102 /** 103 * Custom data pointer. 104 */ 105 void *ptr1; 106 /** 107 * Custom data pointer. 108 */ 109 void *ptr2; 110 /** 111 * Custom data integer. 112 */ 113 size_t int1; 114 /** 115 * Custom data integer. 116 */ 117 size_t int2; 118 }; 119 120 /** 121 * A default stdlib-based array reallocator. 122 */ 123 extern struct cx_array_reallocator_s *cx_array_default_reallocator; 124 125 /** 126 * Return codes for array functions. 127 */ 128 enum cx_array_result { 129 CX_ARRAY_SUCCESS, 130 CX_ARRAY_REALLOC_NOT_SUPPORTED, 131 CX_ARRAY_REALLOC_FAILED, 132 }; 133 134 /** 135 * Copies elements from one array to another. 136 * 137 * The elements are copied to the \p target array at the specified \p index, 138 * overwriting possible elements. The \p index does not need to be in range of 139 * the current array \p size. If the new index plus the number of elements added 140 * would extend the array's size, and \p capacity is not \c NULL, the remaining 141 * capacity is used. 142 * 143 * If the capacity is insufficient to hold the new data, a reallocation 144 * attempt is made, unless the \p reallocator is set to \c NULL, in which case 145 * this function ultimately returns a failure. 146 * 147 * @param target a pointer to the target array 148 * @param size a pointer to the size of the target array 149 * @param capacity a pointer to the target array's capacity - 150 * \c NULL if only the size shall be used to bound the array 151 * @param index the index where the copied elements shall be placed 152 * @param src the source array 153 * @param elem_size the size of one element 154 * @param elem_count the number of elements to copy 155 * @param reallocator the array reallocator to use, or \c NULL 156 * if reallocation shall not happen 157 * @return zero on success, non-zero error code on failure 158 */ 159 enum cx_array_result cx_array_copy( 160 void **target, 161 size_t *size, 162 size_t *capacity, 163 size_t index, 164 void const *src, 165 size_t elem_size, 166 size_t elem_count, 167 struct cx_array_reallocator_s *reallocator 168 ) __attribute__((__nonnull__(1, 2, 5))); 169 170 /** 171 * Convenience macro that uses cx_array_copy() with a default layout and the default reallocator. 172 * 173 * @param array the name of the array (NOT a pointer to the array) 174 * @param index the index where the copied elements shall be placed 175 * @param src the source array 176 * @param count the number of elements to copy 177 */ 178 #define cx_array_simple_copy(array, index, src, count) \ 179 cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \ 180 index, src, sizeof((array)[0]), count, cx_array_default_reallocator) 181 182 /** 183 * Adds an element to an array with the possibility of allocating more space. 184 * 185 * The element \p elem is added to the end of the \p target array which containing 186 * \p size elements, already. The \p capacity must not be \c NULL and point a 187 * variable holding the current maximum number of elements the array can hold. 188 * 189 * If the capacity is insufficient to hold the new element, and the optional 190 * \p reallocator is not \c NULL, an attempt increase the \p capacity is made 191 * and the new capacity is written back. 192 * 193 * @param target a pointer to the target array 194 * @param size a pointer to the size of the target array 195 * @param capacity a pointer to the target array's capacity - must not be \c NULL 196 * @param elem_size the size of one element 197 * @param elem a pointer to the element to add 198 * @param reallocator the array reallocator to use, or \c NULL if reallocation shall not happen 199 * @return zero on success, non-zero error code on failure 200 */ 201 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ 202 cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator) 203 204 /** 205 * Convenience macro that uses cx_array_add() with a default layout and the default reallocator. 206 * 207 * @param array the name of the array (NOT a pointer to the array) 208 * @param elem the element to add (NOT a pointer, address is automatically taken) 209 */ 210 #define cx_array_simple_add(array, elem) \ 211 cx_array_simple_copy(array, array##_size, &(elem), 1) 212 213 /** 214 * Swaps two array elements. 215 * 216 * @param arr the array 217 * @param elem_size the element size 218 * @param idx1 index of first element 219 * @param idx2 index of second element 220 */ 221 void cx_array_swap( 222 void *arr, 223 size_t elem_size, 224 size_t idx1, 225 size_t idx2 226 ) __attribute__((__nonnull__)); 227 228 /** 229 * Allocates an array list for storing elements with \p elem_size bytes each. 230 * 231 * If \p elem_size is CX_STORE_POINTERS, the created list will be created as if 232 * cxListStorePointers() was called immediately after creation and the compare 233 * function will be automatically set to cx_cmp_ptr(), if none is given. 234 * 235 * @param allocator the allocator for allocating the list memory 236 * (if \c NULL the cxDefaultAllocator will be used) 237 * @param comparator the comparator for the elements 238 * (if \c NULL, and the list is not storing pointers, sort and find 239 * functions will not work) 240 * @param elem_size the size of each element in bytes 241 * @param initial_capacity the initial number of elements the array can store 242 * @return the created list 243 */ 244 CxList *cxArrayListCreate( 245 CxAllocator const *allocator, 246 cx_compare_func comparator, 247 size_t elem_size, 248 size_t initial_capacity 249 ); 250 251 /** 252 * Allocates an array list for storing elements with \p elem_size bytes each. 253 * 254 * The list will use the cxDefaultAllocator and \em NO compare function. 255 * If you want to call functions that need a compare function, you have to 256 * set it immediately after creation or use cxArrayListCreate(). 257 * 258 * If \p elem_size is CX_STORE_POINTERS, the created list will be created as if 259 * cxListStorePointers() was called immediately after creation and the compare 260 * function will be automatically set to cx_cmp_ptr(). 261 * 262 * @param elem_size the size of each element in bytes 263 * @param initial_capacity the initial number of elements the array can store 264 * @return the created list 265 */ 266 #define cxArrayListCreateSimple(elem_size, initial_capacity) \ 267 cxArrayListCreate(NULL, NULL, elem_size, initial_capacity) 268 269 #ifdef __cplusplus 270 } // extern "C" 271 #endif 272 273 #endif // UCX_ARRAY_LIST_H 274