diff -r 33e7b6ebf403 -r a89857072ace src/cx/list.h --- a/src/cx/list.h Tue Feb 15 19:07:14 2022 +0100 +++ b/src/cx/list.h Tue Feb 15 19:41:48 2022 +0100 @@ -71,6 +71,14 @@ */ CxAllocator *allocator; /** + * A mandatory destructor for the list structure. + */ + cx_destructor_func list_destructor; + /** + * An optional destructor for the list contents. + */ + cx_destructor_func content_destructor; + /** * The comparator function for the elements. */ CxListComparator cmpfunc; @@ -86,6 +94,16 @@ * The capacity of the list (maximum number of elements). */ size_t capacity; + /** + * Flag indicating whether cxListDestroy() shall free the list structure, + * even if cx_list_s.list_destructor did not do that. + */ + bool autofree; + /** + * Flag indicating whether cxListDestroy() shall free each list element, + * even if cx_list_s.content_destructor did not do that. + */ + bool autofree_contents; }; /** @@ -364,6 +382,21 @@ return list->cl->compare(list, other); } +/** + * Calls the list's destructor function for every element. + * If CxList.autofree_content is \c true, the elements are automatically free'd + * unless the content destructor function did not already do that. + * Similarly, if CxList.autofree is \c true, the list structure is free'd, unless + * the list destructor function did not already do that. + * + * This function itself is a destructor function for the CxList. + * + * @param list the list which contents shall be destroyed + * @return \p list if the list structure has been free'd during the process + */ +__attribute__((__nonnull__)) +CxList *cxListDestroy(CxList *list); + #ifdef __cplusplus } /* extern "C" */ #endif