Skip to content

Commit

Permalink
config_format: yaml: Let flb_kv own variant memory
Browse files Browse the repository at this point in the history
Add a new flag to `flb_kv` so it is possible to know if its `val` is
pointing to a `cfl_variant` struct. This allows the `flb_kv` struct to
keep ownership of the variant and properly release it on config reload.

Signed-off-by: Thiago Padilha <[email protected]>
  • Loading branch information
tchrono committed May 27, 2024
1 parent 4a29f10 commit 6afb673
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions include/fluent-bit/flb_kv.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
struct flb_kv {
flb_sds_t key;
flb_sds_t val;
/* set to `1` if `val` is a `cfl_variant` pointer` */
/* the correct thing would changing `val` to be an union, but it would require
* too much refactoring since `val` is referenced all over the code */
int is_variant;
struct mk_list _head;
};

Expand Down
1 change: 0 additions & 1 deletion src/flb_config_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,6 @@ int flb_config_map_set(struct mk_list *properties, struct mk_list *map, void *co
else if (m->type == FLB_CONFIG_MAP_VARIANT) {
m_variant = (struct cfl_variant **) (base + m->offset);
*m_variant = (struct cfl_variant *)kv->val;
kv->val = NULL; /* ownership now belongs to the plugin */
}
else if (m->type >= FLB_CONFIG_MAP_CLIST ||
m->type <= FLB_CONFIG_MAP_SLIST_4) {
Expand Down
8 changes: 7 additions & 1 deletion src/flb_kv.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_log.h>
#include <fluent-bit/flb_mem.h>
#include <cfl/cfl.h>

void flb_kv_init(struct mk_list *list)
{
Expand Down Expand Up @@ -83,7 +84,12 @@ void flb_kv_item_destroy(struct flb_kv *kv)
}

if (kv->val) {
flb_sds_destroy(kv->val);
if (kv->is_variant) {
cfl_variant_destroy((struct cfl_variant *)kv->val);
}
else {
flb_sds_destroy(kv->val);
}
}

mk_list_del(&kv->_head);
Expand Down
3 changes: 1 addition & 2 deletions src/flb_processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,6 @@ static int load_from_config_format_group(struct flb_processor *proc, int type, s
return -1;
}

/* ownership moved to the processor instance */
pair->val = NULL;
cfl_kvpair_destroy(pair);
}
}
Expand Down Expand Up @@ -926,6 +924,7 @@ int flb_processor_instance_set_property(struct flb_processor_instance *ins,
* the variant to the plugin, which will then own its memory. After this happens,
* kv->val must be set to NULL */
kv->val = (void *)v;
kv->is_variant = 1;
}
}

Expand Down

0 comments on commit 6afb673

Please sign in to comment.