Skip to content

Commit

Permalink
lib: cfl: Allow arrays/kvlists to be externally owned
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tchrono committed May 28, 2024
1 parent b82b55e commit 8ac0c00
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/cfl/include/cfl/cfl_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/cfl/include/cfl/cfl_kvlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions lib/cfl/src/cfl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions lib/cfl/src/cfl_kvlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 12 additions & 2 deletions lib/cfl/src/cfl_variant.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8ac0c00

Please sign in to comment.