Mon, 18 Apr 2022 16:29:14 +0200
simplify destructor signature (but loads more responsibility onto the user)
src/cx/allocator.h | file | annotate | diff | comparison | revisions | |
src/cx/linked_list.h | file | annotate | diff | comparison | revisions | |
src/cx/list.h | file | annotate | diff | comparison | revisions | |
src/list.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/cx/allocator.h Mon Apr 18 15:59:09 2022 +0200 1.2 +++ b/src/cx/allocator.h Mon Apr 18 16:29:14 2022 +0200 1.3 @@ -108,13 +108,13 @@ 1.4 * Function pointer type for destructor functions. 1.5 * 1.6 * A destructor function deallocates possible contents and MAY free the memory 1.7 - * pointed to by \p memory. 1.8 + * pointed to by \p memory. Read the documentation of the respective function 1.9 + * pointer to learn if a destructor SHALL, MAY, or MUST NOT free the memory in that 1.10 + * particular implementation. 1.11 * 1.12 * @param memory a pointer to the object to destruct 1.13 - * @return \p memory if it has NOT been free'd by this destructor, otherwise \c NULL 1.14 */ 1.15 -typedef void *(*cx_destructor_func)(void *memory) 1.16 - __attribute__((__nonnull__, __warn_unused_result__)); 1.17 +typedef void (*cx_destructor_func)(void *memory) __attribute__((__nonnull__)); 1.18 1.19 /** 1.20 * Allocate \p n bytes of memory.
2.1 --- a/src/cx/linked_list.h Mon Apr 18 15:59:09 2022 +0200 2.2 +++ b/src/cx/linked_list.h Mon Apr 18 16:29:14 2022 +0200 2.3 @@ -48,7 +48,8 @@ 2.4 /** 2.5 * Allocates a linked list for storing elements with \p item_size bytes each. 2.6 * 2.7 - * Elements added to the list are copied. 2.8 + * @remark Elements added to the list are copied, therefore a possible cx_list_s.content_destructor 2.9 + * MUST NOT free the memory pointed to by its argument. 2.10 * 2.11 * @param allocator the allocator for allocating the list nodes 2.12 * @param comparator the comparator for the elements 2.13 @@ -66,6 +67,9 @@ 2.14 * 2.15 * If you want to store the elements directly in this list, use cxLinkedListCreate(). 2.16 * 2.17 + * @remark Since only pointers are stored in this list, a possible cx_list_s.content_destructor 2.18 + * MAY free the memory pointed to by its argument in order to prevent memory leaks. 2.19 + * 2.20 * @param allocator the allocator for allocating the list nodes 2.21 * @param comparator the comparator for the elements 2.22 * @return the created list 2.23 @@ -78,6 +82,9 @@ 2.24 /** 2.25 * Creates a linked list using the data from an array. 2.26 * 2.27 + * @remark Elements added to the list are copied, therefore a possible cx_list_s.content_destructor 2.28 + * MUST NOT free the memory pointed to by its argument. 2.29 + * 2.30 * @param allocator the allocator for allocating the list nodes 2.31 * @param comparator the comparator for the elements 2.32 * @param item_size the size of one item in the array
3.1 --- a/src/cx/list.h Mon Apr 18 15:59:09 2022 +0200 3.2 +++ b/src/cx/list.h Mon Apr 18 16:29:14 2022 +0200 3.3 @@ -72,6 +72,9 @@ 3.4 CxAllocator const *allocator; 3.5 /** 3.6 * An optional destructor for the list contents. 3.7 + * 3.8 + * @attention Read the documentation of the particular list implementation 3.9 + * whether this destructor shall only destroy the contents or also free the memory. 3.10 */ 3.11 cx_destructor_func content_destructor; 3.12 /**
4.1 --- a/src/list.c Mon Apr 18 15:59:09 2022 +0200 4.2 +++ b/src/list.c Mon Apr 18 16:29:14 2022 +0200 4.3 @@ -32,7 +32,7 @@ 4.4 if (list->content_destructor != NULL) { 4.5 CxIterator iter = cxListBegin(list); 4.6 cx_foreach(void*, elem, iter) { 4.7 - cxFree(list->allocator, list->content_destructor(elem)); 4.8 + list->content_destructor(elem); 4.9 } 4.10 } 4.11 list->cl->destructor(list);