Sun, 18 Feb 2024 13:16:38 +0100
make cx_array_simple_add() automatically take the address of the element
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 */
38 #ifndef UCX_ARRAY_LIST_H
39 #define UCX_ARRAY_LIST_H
41 #include "list.h"
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
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;
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 */
58 #define cx_array_declare(type, name) \
59 type * name; \
60 size_t name##_size; \
61 size_t name##_capacity;
63 /**
64 * Initializes an array declared with cx_array_declare().
65 *
66 * The memory for the array is allocated with stdlib malloc().
67 * @param array the array
68 * @param capacity the initial capacity
69 */
70 #define cx_array_initialize(array, capacity) \
71 array##_capacity = capacity; \
72 array##_size = 0; \
73 array = malloc(sizeof(array[0]) * capacity);
75 /**
76 * Defines a reallocation mechanism for arrays.
77 */
78 struct cx_array_reallocator_s {
79 /**
80 * Reallocates space for the given array.
81 *
82 * Implementations are not required to free the original array.
83 * This allows reallocation of static memory by allocating heap memory
84 * and copying the array contents. The information in the custom fields of
85 * the referenced allocator can be used to track the state of the memory
86 * or to transport other additional data.
87 *
88 * @param array the array to reallocate
89 * @param capacity the new capacity (number of elements)
90 * @param elem_size the size of each element
91 * @param alloc a reference to this allocator
92 * @return a pointer to the reallocated memory or \c NULL on failure
93 */
94 void *(*realloc)(
95 void *array,
96 size_t capacity,
97 size_t elem_size,
98 struct cx_array_reallocator_s *alloc
99 );
101 /**
102 * Custom data pointer.
103 */
104 void *ptr1;
105 /**
106 * Custom data pointer.
107 */
108 void *ptr2;
109 /**
110 * Custom data integer.
111 */
112 size_t int1;
113 /**
114 * Custom data integer.
115 */
116 size_t int2;
117 };
119 /**
120 * A default stdlib-based array reallocator.
121 */
122 extern struct cx_array_reallocator_s *cx_array_default_reallocator;
124 /**
125 * Return codes for array functions.
126 */
127 enum cx_array_result {
128 CX_ARRAY_SUCCESS,
129 CX_ARRAY_REALLOC_NOT_SUPPORTED,
130 CX_ARRAY_REALLOC_FAILED,
131 };
133 /**
134 * Copies elements from one array to another.
135 *
136 * The elements are copied to the \p target array at the specified \p index,
137 * overwriting possible elements. The \p index does not need to be in range of
138 * the current array \p size. If the new index plus the number of elements added
139 * would extend the array's size, and \p capacity is not \c NULL, the remaining
140 * capacity is used.
141 *
142 * If the capacity is insufficient to hold the new data, a reallocation
143 * attempt is made, unless the \p reallocator is set to \c NULL, in which case
144 * this function ultimately returns a failure.
145 *
146 * @param target a pointer to the target array
147 * @param size a pointer to the size of the target array
148 * @param capacity a pointer to the target array's capacity -
149 * \c NULL if only the size shall be used to bound the array
150 * @param index the index where the copied elements shall be placed
151 * @param src the source array
152 * @param elem_size the size of one element
153 * @param elem_count the number of elements to copy
154 * @param reallocator the array reallocator to use, or \c NULL
155 * if reallocation shall not happen
156 * @return zero on success, non-zero error code on failure
157 */
158 enum cx_array_result cx_array_copy(
159 void **target,
160 size_t *size,
161 size_t *capacity,
162 size_t index,
163 void const *src,
164 size_t elem_size,
165 size_t elem_count,
166 struct cx_array_reallocator_s *reallocator
167 ) __attribute__((__nonnull__(1, 2, 5)));
169 /**
170 * Convenience macro that uses cx_array_copy() with a default layout and the default reallocator.
171 *
172 * @param array the name of the array (NOT a pointer to the array)
173 * @param index the index where the copied elements shall be placed
174 * @param src the source array
175 * @param count the number of elements to copy
176 */
177 #define cx_array_simple_copy(array, index, src, count) \
178 cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \
179 index, src, sizeof((array)[0]), count, cx_array_default_reallocator)
181 /**
182 * Adds an element to an array with the possibility of allocating more space.
183 *
184 * The element \p elem is added to the end of the \p target array which containing
185 * \p size elements, already. The \p capacity must not be \c NULL and point a
186 * variable holding the current maximum number of elements the array can hold.
187 *
188 * If the capacity is insufficient to hold the new element, and the optional
189 * \p reallocator is not \c NULL, an attempt increase the \p capacity is made
190 * and the new capacity is written back.
191 *
192 * @param target a pointer to the target array
193 * @param size a pointer to the size of the target array
194 * @param capacity a pointer to the target array's capacity - must not be \c NULL
195 * @param elem_size the size of one element
196 * @param elem a pointer to the element to add
197 * @param reallocator the array reallocator to use, or \c NULL if reallocation shall not happen
198 * @return zero on success, non-zero error code on failure
199 */
200 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \
201 cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator)
203 /**
204 * Convenience macro that uses cx_array_add() with a default layout and the default reallocator.
205 *
206 * @param array the name of the array (NOT a pointer to the array)
207 * @param elem the element to add (NOT a pointer, address is automatically taken)
208 */
209 #define cx_array_simple_add(array, elem) \
210 cx_array_simple_copy(array, array##_size, &(elem), 1)
212 /**
213 * Swaps two array elements.
214 *
215 * @param arr the array
216 * @param elem_size the element size
217 * @param idx1 index of first element
218 * @param idx2 index of second element
219 */
220 void cx_array_swap(
221 void *arr,
222 size_t elem_size,
223 size_t idx1,
224 size_t idx2
225 ) __attribute__((__nonnull__));
227 /**
228 * Allocates an array list for storing elements with \p item_size bytes each.
229 *
230 * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
231 * cxListStorePointers() was called immediately after creation and the compare
232 * function will be automatically set to cx_cmp_ptr(), if none is given.
233 *
234 * @param allocator the allocator for allocating the list memory
235 * (if \c NULL the cxDefaultAllocator will be used)
236 * @param comparator the comparator for the elements
237 * (if \c NULL, and the list is not storing pointers, sort and find
238 * functions will not work)
239 * @param item_size the size of each element in bytes
240 * @param initial_capacity the initial number of elements the array can store
241 * @return the created list
242 */
243 CxList *cxArrayListCreate(
244 CxAllocator const *allocator,
245 cx_compare_func comparator,
246 size_t item_size,
247 size_t initial_capacity
248 );
250 /**
251 * Allocates an array list for storing elements with \p item_size bytes each.
252 *
253 * The list will use the cxDefaultAllocator and \em NO compare function.
254 * If you want to call functions that need a compare function, you have to
255 * set it immediately after creation or use cxArrayListCreate().
256 *
257 * If \p item_size is CX_STORE_POINTERS, the created list will be created as if
258 * cxListStorePointers() was called immediately after creation and the compare
259 * function will be automatically set to cx_cmp_ptr().
260 *
261 * @param item_size the size of each element in bytes
262 * @param initial_capacity the initial number of elements the array can store
263 * @return the created list
264 */
265 #define cxArrayListCreateSimple(item_size, initial_capacity) \
266 cxArrayListCreate(NULL, NULL, item_size, initial_capacity)
268 #ifdef __cplusplus
269 } // extern "C"
270 #endif
272 #endif // UCX_ARRAY_LIST_H