From 8ac0c00f6603e1875ae336c7df4dc19e367064d9 Mon Sep 17 00:00:00 2001 From: Thiago Padilha Date: Mon, 27 May 2024 12:28:52 -0300 Subject: [PATCH] lib: cfl: Allow arrays/kvlists to be externally owned When destroying arrays/kvlists, cfl_variant_destroy will check if the "referenced" flag is set, and call the appropriate destroy function when the values are externally referenced. Signed-off-by: Thiago Padilha --- lib/cfl/include/cfl/cfl_array.h | 1 + lib/cfl/include/cfl/cfl_kvlist.h | 1 + lib/cfl/src/cfl_array.c | 12 ++++++++++++ lib/cfl/src/cfl_kvlist.c | 20 ++++++++++++++++++++ lib/cfl/src/cfl_variant.c | 14 ++++++++++++-- 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/cfl/include/cfl/cfl_array.h b/lib/cfl/include/cfl/cfl_array.h index 3977df75e6b..00fb2449292 100644 --- a/lib/cfl/include/cfl/cfl_array.h +++ b/lib/cfl/include/cfl/cfl_array.h @@ -32,6 +32,7 @@ struct cfl_array { struct cfl_array *cfl_array_create(size_t slot_count); void cfl_array_destroy(struct cfl_array *array); +void cfl_array_destroy_referenced(struct cfl_array *array); static inline struct cfl_variant *cfl_array_fetch_by_index(struct cfl_array *array, size_t position) diff --git a/lib/cfl/include/cfl/cfl_kvlist.h b/lib/cfl/include/cfl/cfl_kvlist.h index ab25b162389..019981435e5 100644 --- a/lib/cfl/include/cfl/cfl_kvlist.h +++ b/lib/cfl/include/cfl/cfl_kvlist.h @@ -36,6 +36,7 @@ struct cfl_kvlist { }; struct cfl_kvlist *cfl_kvlist_create(); +void cfl_kvlist_destroy_referenced(struct cfl_kvlist *list); void cfl_kvlist_destroy(struct cfl_kvlist *list); int cfl_kvlist_insert_string(struct cfl_kvlist *list, diff --git a/lib/cfl/src/cfl_array.c b/lib/cfl/src/cfl_array.c index 916b053e4af..2cfe5fe794c 100644 --- a/lib/cfl/src/cfl_array.c +++ b/lib/cfl/src/cfl_array.c @@ -48,6 +48,18 @@ struct cfl_array *cfl_array_create(size_t slot_count) return array; } +void cfl_array_destroy_referenced(struct cfl_array *array) +{ + if (!array) { + return; + } + + if (array->entries != NULL) { + free(array->entries); + } + free(array); +} + void cfl_array_destroy(struct cfl_array *array) { size_t index; diff --git a/lib/cfl/src/cfl_kvlist.c b/lib/cfl/src/cfl_kvlist.c index 12c7711290c..53e402934e0 100644 --- a/lib/cfl/src/cfl_kvlist.c +++ b/lib/cfl/src/cfl_kvlist.c @@ -37,6 +37,26 @@ struct cfl_kvlist *cfl_kvlist_create() return list; } +void cfl_kvlist_destroy_referenced(struct cfl_kvlist *list) +{ + struct cfl_list *tmp; + struct cfl_list *head; + struct cfl_kvpair *pair; + + cfl_list_foreach_safe(head, tmp, &list->list) { + pair = cfl_list_entry(head, struct cfl_kvpair, _head); + + if (pair->key) { + cfl_sds_destroy(pair->key); + } + + cfl_list_del(&pair->_head); + free(pair); + } + + free(list); +} + void cfl_kvlist_destroy(struct cfl_kvlist *list) { struct cfl_list *tmp; diff --git a/lib/cfl/src/cfl_variant.c b/lib/cfl/src/cfl_variant.c index 1b688b847fe..36043d20c97 100644 --- a/lib/cfl/src/cfl_variant.c +++ b/lib/cfl/src/cfl_variant.c @@ -275,10 +275,20 @@ void cfl_variant_destroy(struct cfl_variant *instance) } } else if (instance->type == CFL_VARIANT_ARRAY) { - cfl_array_destroy(instance->data.as_array); + if (instance->referenced) { + cfl_array_destroy_referenced(instance->data.as_array); + } + else { + cfl_array_destroy(instance->data.as_array); + } } else if (instance->type == CFL_VARIANT_KVLIST) { - cfl_kvlist_destroy(instance->data.as_kvlist); + if (instance->referenced) { + cfl_kvlist_destroy_referenced(instance->data.as_kvlist); + } + else { + cfl_kvlist_destroy(instance->data.as_kvlist); + } } free(instance);