| 46 /** |
46 /** |
| 47 * The maximum item size in an array list that fits into |
47 * The maximum item size in an array list that fits into |
| 48 * a stack buffer when swapped. |
48 * a stack buffer when swapped. |
| 49 */ |
49 */ |
| 50 CX_EXPORT extern const unsigned cx_array_swap_sbo_size; |
50 CX_EXPORT extern const unsigned cx_array_swap_sbo_size; |
| |
51 |
| |
52 #define CX_ARRAY(type, name) \ |
| |
53 struct { \ |
| |
54 type *data; \ |
| |
55 size_t size; \ |
| |
56 size_t capacity; \ |
| |
57 } name |
| |
58 |
| |
59 typedef struct cx_array_s { |
| |
60 void *data; |
| |
61 size_t size; |
| |
62 size_t capacity; |
| |
63 } CxArray; |
| |
64 |
| |
65 cx_attr_nonnull |
| |
66 CX_EXPORT int cx_array_init_(CxArray *array, const CxAllocator *allocator, size_t elem_size, size_t capacity); |
| |
67 |
| |
68 #define cx_array_init(array, capacity) cx_array_init_((CxArray*)&array, cxDefaultAllocator, sizeof((array).data[0]), capacity) |
| |
69 |
| |
70 #define cx_array_init_a(array, allocator, capacity) cx_array_init_((CxArray*)&array, allocator, sizeof((array).data[0]), capacity) |
| |
71 |
| |
72 cx_attr_nonnull |
| |
73 CX_EXPORT int cx_array_add_(CxArray *array, const CxAllocator *allocator, size_t elem_size, void *element); |
| |
74 |
| |
75 #define cx_array_add(array, element) cx_array_add_((CxArray*)&array, cxDefaultAllocator, sizeof((array).data[0]), element) |
| |
76 |
| |
77 #define cx_array_add_a(array, allocator, element) cx_array_add_((CxArray*)&array, cxDefaultAllocator, sizeof((array).data[0]), element) |
| |
78 |
| |
79 cx_attr_nonnull |
| |
80 CX_EXPORT void cx_array_free_(const CxAllocator *allocator, CxArray *array); |
| |
81 |
| |
82 #define cx_array_free(array) cx_array_free_(cxDefaultAllocator, (CxArray*)&array) |
| |
83 |
| |
84 #define cx_array_free_a(allocator, array) cx_array_free_(allocator, (CxArray*)&array) |
| 51 |
85 |
| 52 /** |
86 /** |
| 53 * Declares variables for an array that can be used with the convenience macros. |
87 * Declares variables for an array that can be used with the convenience macros. |
| 54 * |
88 * |
| 55 * @par Examples |
89 * @par Examples |
| 370 * @see cx_array_simple_reserve_a() |
404 * @see cx_array_simple_reserve_a() |
| 371 */ |
405 */ |
| 372 #define cx_array_simple_reserve(array, count) \ |
406 #define cx_array_simple_reserve(array, count) \ |
| 373 cx_array_simple_reserve_a(NULL, array, count) |
407 cx_array_simple_reserve_a(NULL, array, count) |
| 374 |
408 |
| 375 /** |
|
| 376 * Adds an element to an array with the possibility of allocating more space. |
|
| 377 * |
|
| 378 * The element @p elem is added to the end of the @p target array which contains |
|
| 379 * @p size elements, already. The @p capacity must point to a variable denoting |
|
| 380 * the current maximum number of elements the array can hold. |
|
| 381 * |
|
| 382 * If the capacity is insufficient to hold the new element, an attempt to |
|
| 383 * increase the @p capacity is made and the new capacity is written back. |
|
| 384 * |
|
| 385 * The \@ SIZE_TYPE is flexible and can be any unsigned integer type. |
|
| 386 * It is important, however, that @p size and @p capacity are pointers to |
|
| 387 * variables of the same type. |
|
| 388 * |
|
| 389 * @param target (@c void**) a pointer to the target array |
|
| 390 * @param size (@c SIZE_TYPE*) a pointer to the size of the target array |
|
| 391 * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array |
|
| 392 * @param elem_size (@c size_t) the size of one element |
|
| 393 * @param elem (@c void*) a pointer to the element to add |
|
| 394 * @param reallocator (@c CxArrayReallocator*) the array reallocator to use |
|
| 395 * @retval zero success |
|
| 396 * @retval non-zero failure |
|
| 397 */ |
|
| 398 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ |
|
| 399 cx_array_copy((void**)(target), size, capacity, sizeof(*(size)), \ |
|
| 400 *(size), elem, elem_size, 1, reallocator) |
|
| 401 |
409 |
| 402 /** |
410 /** |
| 403 * Convenience macro that uses cx_array_add() with a default layout and |
411 * Convenience macro that uses cx_array_add() with a default layout and |
| 404 * the specified reallocator. |
412 * the specified reallocator. |
| 405 * |
413 * |