Sun, 18 Feb 2024 13:01:09 +0100
add convenience macros for cx_array_*
CHANGELOG | file | annotate | diff | comparison | revisions | |
docs/src/features.md | file | annotate | diff | comparison | revisions | |
src/cx/array_list.h | file | annotate | diff | comparison | revisions | |
tests/test_list.c | file | annotate | diff | comparison | revisions |
1.1 --- a/CHANGELOG Sun Feb 18 12:24:04 2024 +0100 1.2 +++ b/CHANGELOG Sun Feb 18 13:01:09 2024 +0100 1.3 @@ -3,6 +3,7 @@ 1.4 * adds tree.h 1.5 * adds cx_array_default_reallocator 1.6 * adds cx_array_add() 1.7 + * adds cx_array_declare() and cx_array_simple_*() convenience macros 1.8 * adds cx_linked_list_find_node() 1.9 * adds cxListFindRemove() 1.10 * adds cxBufferReset()
2.1 --- a/docs/src/features.md Sun Feb 18 12:24:04 2024 +0100 2.2 +++ b/docs/src/features.md Sun Feb 18 13:01:09 2024 +0100 2.3 @@ -318,6 +318,8 @@ 2.4 2.5 If you just want to add one single element to an existing array, you can use the macro `cx_array_add()`. 2.6 In that case, since the element is added to the end of the array, the `capacity` argument is mandatory. 2.7 +You can use `cx_array_declare()` to declare the necessary fields within a structure and then use the 2.8 +`cx_array_simple_*()` convenience macros to reduce code overhead. 2.9 2.10 ## Map 2.11
3.1 --- a/src/cx/array_list.h Sun Feb 18 12:24:04 2024 +0100 3.2 +++ b/src/cx/array_list.h Sun Feb 18 13:01:09 2024 +0100 3.3 @@ -50,14 +50,25 @@ 3.4 extern unsigned cx_array_swap_sbo_size; 3.5 3.6 /** 3.7 + * Declares variables for an array that can be used with the convenience macros. 3.8 + * 3.9 + * @see cx_array_simple_add() 3.10 + * @see cx_array_simple_copy() 3.11 + */ 3.12 +#define cx_array_declare(type, name) \ 3.13 + type * name; \ 3.14 + size_t name##_size; \ 3.15 + size_t name##_capacity; 3.16 + 3.17 +/** 3.18 * Defines a reallocation mechanism for arrays. 3.19 */ 3.20 struct cx_array_reallocator_s { 3.21 /** 3.22 - * Re-allocates space for the given array. 3.23 + * Reallocates space for the given array. 3.24 * 3.25 * Implementations are not required to free the original array. 3.26 - * This allows re-allocation of static memory by allocating heap memory 3.27 + * This allows reallocation of static memory by allocating heap memory 3.28 * and copying the array contents. The information in the custom fields of 3.29 * the referenced allocator can be used to track the state of the memory 3.30 * or to transport other additional data. 3.31 @@ -120,7 +131,7 @@ 3.32 * attempt is made, unless the \p reallocator is set to \c NULL, in which case 3.33 * this function ultimately returns a failure. 3.34 * 3.35 - * @param target the target array 3.36 + * @param target a pointer to the target array 3.37 * @param size a pointer to the size of the target array 3.38 * @param capacity a pointer to the target array's capacity - 3.39 * \c NULL if only the size shall be used to bound the array 3.40 @@ -128,8 +139,8 @@ 3.41 * @param src the source array 3.42 * @param elem_size the size of one element 3.43 * @param elem_count the number of elements to copy 3.44 - * @param reallocator the array re-allocator to use, or \c NULL 3.45 - * if re-allocation shall not happen 3.46 + * @param reallocator the array reallocator to use, or \c NULL 3.47 + * if reallocation shall not happen 3.48 * @return zero on success, non-zero error code on failure 3.49 */ 3.50 enum cx_array_result cx_array_copy( 3.51 @@ -144,6 +155,18 @@ 3.52 ) __attribute__((__nonnull__(1, 2, 5))); 3.53 3.54 /** 3.55 + * Convenience macro that uses cx_array_copy() with a default layout and the default reallocator. 3.56 + * 3.57 + * @param array the name of the array (NOT a pointer to the array) 3.58 + * @param index the index where the copied elements shall be placed 3.59 + * @param src the source array 3.60 + * @param count the number of elements to copy 3.61 + */ 3.62 +#define cx_array_simple_copy(array, index, src, count) \ 3.63 + cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \ 3.64 + index, src, sizeof((array)[0]), count, cx_array_default_reallocator) 3.65 + 3.66 +/** 3.67 * Adds an element to an array with the possibility of allocating more space. 3.68 * 3.69 * The element \p elem is added to the end of the \p target array which containing 3.70 @@ -154,19 +177,27 @@ 3.71 * \p reallocator is not \c NULL, an attempt increase the \p capacity is made 3.72 * and the new capacity is written back. 3.73 * 3.74 - * @param target the target array 3.75 + * @param target a pointer to the target array 3.76 * @param size a pointer to the size of the target array 3.77 * @param capacity a pointer to the target array's capacity - must not be \c NULL 3.78 * @param elem_size the size of one element 3.79 * @param elem the element to add 3.80 - * @param reallocator the array re-allocator to use, or \c NULL 3.81 - * if re-allocation shall not happen 3.82 + * @param reallocator the array reallocator to use, or \c NULL if reallocation shall not happen 3.83 * @return zero on success, non-zero error code on failure 3.84 */ 3.85 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \ 3.86 cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator) 3.87 3.88 /** 3.89 + * Convenience macro that uses cx_array_add() with a default layout and the default reallocator. 3.90 + * 3.91 + * @param array the name of the array (NOT a pointer to the array) 3.92 + * @param elem the element to add 3.93 + */ 3.94 +#define cx_array_simple_add(array, elem) \ 3.95 + cx_array_simple_copy(array, array##_size, elem, 1) 3.96 + 3.97 +/** 3.98 * Swaps two array elements. 3.99 * 3.100 * @param arr the array
4.1 --- a/tests/test_list.c Sun Feb 18 12:24:04 2024 +0100 4.2 +++ b/tests/test_list.c Sun Feb 18 13:01:09 2024 +0100 4.3 @@ -41,14 +41,15 @@ 4.4 int *stackarray = stackspace; 4.5 size_t stackarray_size = 3; 4.6 size_t stackarray_capacity = 5; 4.7 - int *heaparray = calloc(5, sizeof(int)); 4.8 + cx_array_declare(int, heaparray); 4.9 + heaparray = calloc(5, sizeof(int)); 4.10 heaparray[0] = 2; 4.11 heaparray[1] = 3; 4.12 heaparray[2] = 5; 4.13 heaparray[3] = 7; 4.14 heaparray[4] = 11; 4.15 - size_t heaparray_size = 3; 4.16 - size_t heaparray_capacity = 5; 4.17 + heaparray_size = 3; 4.18 + heaparray_capacity = 5; 4.19 int elem = 8, elem2 = 47; 4.20 enum cx_array_result result; 4.21 CX_TEST_DO { 4.22 @@ -73,7 +74,7 @@ 4.23 CX_TEST_ASSERT(stackarray_size == 5); 4.24 CX_TEST_ASSERT(stackarray_capacity == 5); 4.25 4.26 - result = cx_array_add(&heaparray, &heaparray_size, &heaparray_capacity, sizeof(int), &elem, cx_array_default_reallocator); 4.27 + result = cx_array_simple_add(heaparray, &elem); 4.28 CX_TEST_ASSERT(result == CX_ARRAY_SUCCESS); 4.29 CX_TEST_ASSERT(heaparray[0] == 2); 4.30 CX_TEST_ASSERT(heaparray[1] == 3); 4.31 @@ -84,7 +85,7 @@ 4.32 CX_TEST_ASSERT(heaparray_capacity == 5); 4.33 4.34 heaparray_size = 5; 4.35 - result = cx_array_add(&heaparray, &heaparray_size, &heaparray_capacity, sizeof(int), &elem2, cx_array_default_reallocator); 4.36 + result = cx_array_simple_add(heaparray, &elem2); 4.37 CX_TEST_ASSERT(result == CX_ARRAY_SUCCESS); 4.38 CX_TEST_ASSERT(heaparray[0] == 2); 4.39 CX_TEST_ASSERT(heaparray[1] == 3);