57 */ |
57 */ |
58 #define cx_array_declare(type, name) \ |
58 #define cx_array_declare(type, name) \ |
59 type * name; \ |
59 type * name; \ |
60 size_t name##_size; \ |
60 size_t name##_size; \ |
61 size_t name##_capacity; |
61 size_t name##_capacity; |
|
62 |
|
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); |
62 |
74 |
63 /** |
75 /** |
64 * Defines a reallocation mechanism for arrays. |
76 * Defines a reallocation mechanism for arrays. |
65 */ |
77 */ |
66 struct cx_array_reallocator_s { |
78 struct cx_array_reallocator_s { |
179 * |
191 * |
180 * @param target a pointer to the target array |
192 * @param target a pointer to the target array |
181 * @param size a pointer to the size of the target array |
193 * @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 |
194 * @param capacity a pointer to the target array's capacity - must not be \c NULL |
183 * @param elem_size the size of one element |
195 * @param elem_size the size of one element |
184 * @param elem the element to add |
196 * @param elem a pointer to the element to add |
185 * @param reallocator the array reallocator to use, or \c NULL if reallocation shall not happen |
197 * @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 |
198 * @return zero on success, non-zero error code on failure |
187 */ |
199 */ |
188 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ |
200 #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) |
201 cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator) |
190 |
202 |
191 /** |
203 /** |
192 * Convenience macro that uses cx_array_add() with a default layout and the default reallocator. |
204 * Convenience macro that uses cx_array_add() with a default layout and the default reallocator. |
193 * |
205 * |
194 * @param array the name of the array (NOT a pointer to the array) |
206 * @param array the name of the array (NOT a pointer to the array) |
195 * @param elem the element to add |
207 * @param elem the element to add (NOT a pointer, address is automatically taken) |
196 */ |
208 */ |
197 #define cx_array_simple_add(array, elem) \ |
209 #define cx_array_simple_add(array, elem) \ |
198 cx_array_simple_copy(array, array##_size, elem, 1) |
210 cx_array_simple_copy(array, array##_size, &(elem), 1) |
199 |
211 |
200 /** |
212 /** |
201 * Swaps two array elements. |
213 * Swaps two array elements. |
202 * |
214 * |
203 * @param arr the array |
215 * @param arr the array |