# HG changeset patch # User Mike Becker # Date 1732369532 -3600 # Node ID 8aa57a7fecc41918997aa9576b103995b17223aa # Parent 15b3ca7ee33f93ca1ca86bbaeb0142f6e97efb60 improve consistency for allocator arguments - fixes #485 diff -r 15b3ca7ee33f -r 8aa57a7fecc4 CHANGELOG --- a/CHANGELOG Mon Nov 18 22:05:42 2024 +0100 +++ b/CHANGELOG Sat Nov 23 14:45:32 2024 +0100 @@ -12,6 +12,8 @@ * adds runtime constants to read out the actual SBO sizes * adds improved version of UCX 2 Test framework (now a self-contained header) * adds cx_nmemb() utility function to common.h + * changes all functions, for which there is no dedicated xyz_a variant, + to accept NULL as allocator argument (in which case a default allocator will be used) * moves cx_compare_func typedef to compare.h * moves cx_szmul() to common.h * moves stream copy functions to new streams.h diff -r 15b3ca7ee33f -r 8aa57a7fecc4 src/buffer.c --- a/src/buffer.c Mon Nov 18 22:05:42 2024 +0100 +++ b/src/buffer.c Sat Nov 23 14:45:32 2024 +0100 @@ -38,7 +38,9 @@ const CxAllocator *allocator, int flags ) { - if (allocator == NULL) allocator = cxDefaultAllocator; + if (allocator == NULL) { + allocator = cxDefaultAllocator; + } buffer->allocator = allocator; buffer->flags = flags; if (!space) { @@ -75,6 +77,9 @@ const CxAllocator *allocator, int flags ) { + if (allocator == NULL) { + allocator = cxDefaultAllocator; + } CxBuffer *buf = cxMalloc(allocator, sizeof(CxBuffer)); if (buf == NULL) return NULL; if (0 == cxBufferInit(buf, space, capacity, allocator, flags)) { diff -r 15b3ca7ee33f -r 8aa57a7fecc4 src/cx/array_list.h --- a/src/cx/array_list.h Mon Nov 18 22:05:42 2024 +0100 +++ b/src/cx/array_list.h Sat Nov 23 14:45:32 2024 +0100 @@ -420,7 +420,7 @@ * function will be automatically set to cx_cmp_ptr(), if none is given. * * @param allocator the allocator for allocating the list memory - * (if \c NULL the cxDefaultAllocator will be used) + * (if \c NULL, a default stdlib allocator will be used) * @param comparator the comparator for the elements * (if \c NULL, and the list is not storing pointers, sort and find * functions will not work) diff -r 15b3ca7ee33f -r 8aa57a7fecc4 src/cx/buffer.h --- a/src/cx/buffer.h Mon Nov 18 22:05:42 2024 +0100 +++ b/src/cx/buffer.h Sat Nov 23 14:45:32 2024 +0100 @@ -151,7 +151,8 @@ * new memory * @param capacity the capacity of the buffer * @param allocator the allocator this buffer shall use for automatic - * memory management. If \c NULL, the default heap allocator will be used. + * memory management + * (if \c NULL, a default stdlib allocator will be used) * @param flags buffer features (see cx_buffer_s.flags) * @return zero on success, non-zero if a required allocation failed */ @@ -201,7 +202,8 @@ * new memory * @param capacity the capacity of the buffer * @param allocator the allocator to use for allocating the structure and the automatic - * memory management within the buffer. If \c NULL, the default heap allocator will be used. + * memory management within the buffer + * (if \c NULL, a default stdlib allocator will be used) * @param flags buffer features (see cx_buffer_s.flags) * @return a pointer to the buffer on success, \c NULL if a required allocation failed */ diff -r 15b3ca7ee33f -r 8aa57a7fecc4 src/cx/hash_map.h --- a/src/cx/hash_map.h Mon Nov 18 22:05:42 2024 +0100 +++ b/src/cx/hash_map.h Sat Nov 23 14:45:32 2024 +0100 @@ -77,11 +77,11 @@ * In other words, when the iterator is finished, \c index==size . * * @param allocator the allocator to use + * (if \c NULL, a default stdlib allocator will be used) * @param itemsize the size of one element * @param buckets the initial number of buckets in this hash map * @return a pointer to the new hash map */ -cx_attr_nonnull cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxMapDestroy, 1) diff -r 15b3ca7ee33f -r 8aa57a7fecc4 src/cx/linked_list.h --- a/src/cx/linked_list.h Mon Nov 18 22:05:42 2024 +0100 +++ b/src/cx/linked_list.h Sat Nov 23 14:45:32 2024 +0100 @@ -57,7 +57,7 @@ * function will be automatically set to cx_cmp_ptr(), if none is given. * * @param allocator the allocator for allocating the list nodes - * (if \c NULL the cxDefaultAllocator will be used) + * (if \c NULL, a default stdlib allocator will be used) * @param comparator the comparator for the elements * (if \c NULL, and the list is not storing pointers, sort and find * functions will not work) diff -r 15b3ca7ee33f -r 8aa57a7fecc4 src/cx/tree.h --- a/src/cx/tree.h Mon Nov 18 22:05:42 2024 +0100 +++ b/src/cx/tree.h Sat Nov 23 14:45:32 2024 +0100 @@ -954,6 +954,7 @@ * will free the nodes with the allocator's free() method. * * @param allocator the allocator that shall be used + * (if \c NULL, a default stdlib allocator will be used) * @param create_func a function that creates new nodes * @param search_func a function that compares two nodes * @param search_data_func a function that compares a node with data @@ -967,7 +968,7 @@ * @see cxTreeCreateSimple() * @see cxTreeCreateWrapped() */ -cx_attr_nonnull +cx_attr_nonnull_arg(2, 3, 4) cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxTreeDestroy, 1) @@ -1016,6 +1017,7 @@ * tree, you need to specify those functions afterwards. * * @param allocator the allocator that was used for nodes of the wrapped tree + * (if \c NULL, a default stdlib allocator is assumed) * @param root the root node of the tree that shall be wrapped * @param loc_parent offset in the node struct for the parent pointer * @param loc_children offset in the node struct for the children linked list @@ -1026,7 +1028,7 @@ * @return the new tree * @see cxTreeCreate() */ -cx_attr_nonnull +cx_attr_nonnull_arg(2) cx_attr_nodiscard cx_attr_malloc cx_attr_dealloc(cxTreeDestroy, 1) diff -r 15b3ca7ee33f -r 8aa57a7fecc4 src/hash_map.c --- a/src/hash_map.c Mon Nov 18 22:05:42 2024 +0100 +++ b/src/hash_map.c Sat Nov 23 14:45:32 2024 +0100 @@ -392,6 +392,10 @@ size_t itemsize, size_t buckets ) { + if (allocator == NULL) { + allocator = cxDefaultAllocator; + } + if (buckets == 0) { // implementation defined default buckets = 16; diff -r 15b3ca7ee33f -r 8aa57a7fecc4 src/tree.c --- a/src/tree.c Mon Nov 18 22:05:42 2024 +0100 +++ b/src/tree.c Sat Nov 23 14:45:32 2024 +0100 @@ -808,6 +808,13 @@ ptrdiff_t loc_prev, ptrdiff_t loc_next ) { + if (allocator == NULL) { + allocator = cxDefaultAllocator; + } + assert(create_func != NULL); + assert(search_func != NULL); + assert(search_data_func != NULL); + CxTree *tree = cxMalloc(allocator, sizeof(CxTree)); if (tree == NULL) return NULL; @@ -839,6 +846,11 @@ ptrdiff_t loc_prev, ptrdiff_t loc_next ) { + if (allocator == NULL) { + allocator = cxDefaultAllocator; + } + assert(root != NULL); + CxTree *tree = cxMalloc(allocator, sizeof(CxTree)); if (tree == NULL) return NULL;