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