add convenience function to configure list memory management

Tue, 15 Feb 2022 19:48:25 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 15 Feb 2022 19:48:25 +0100
changeset 504
aaf89ce0cbf6
parent 503
a89857072ace
child 505
03e9151bea32

add convenience function to configure list memory management

src/cx/list.h file | annotate | diff | comparison | revisions
--- a/src/cx/list.h	Tue Feb 15 19:41:48 2022 +0100
+++ b/src/cx/list.h	Tue Feb 15 19:48:25 2022 +0100
@@ -193,6 +193,28 @@
 typedef struct cx_list_s CxList;
 
 /**
+ * Convenience function to configure the memory management for this a list.
+ * @param list the list to configure
+ * @param list_destructor an alternative list destructor to use (if \c NULL, the current destructor remains unchanged)
+ * @param content_destructor the content destructor to use (if \c NULL, no content destructor is used)
+ * @param list_autofree a flag indicating, if the list allocator shall free the list, if the destructor did not do that
+ * @param content_autofree a flag indicating, if the list allocator shall free an element,
+ * if the content destructor did not do that or no content destructor exists
+ */
+static inline void cxListMemoryMgmt(
+        CxList *list,
+        cx_destructor_func list_destructor,
+        cx_destructor_func content_destructor,
+        bool list_autofree,
+        bool content_autofree
+) {
+    if (list_destructor != NULL) list->list_destructor = list_destructor;
+    list->content_destructor = content_destructor;
+    list->autofree = list_autofree;
+    list->autofree_contents = content_autofree;
+}
+
+/**
  * Adds an item to the end of the list.
  *
  * @param list the list

mercurial