From 945312a2da5cb33b12d98ade374ef0a57ddef83a Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 12 Dec 2023 04:43:15 +0000 Subject: [PATCH] src.ctf.fs: make ctf_fs_component::port_data a vector Replace a GPtrArray with a vector of unique pointers of ctf_fs_port_data, simplifying memory management. Change-Id: I4f832f32b0ca893242cc3e61e8aaac495fe86003 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/8243 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/12283 --- src/plugins/ctf/fs-src/fs.cpp | 45 ++++++----------------------------- src/plugins/ctf/fs-src/fs.hpp | 5 ++-- 2 files changed, 10 insertions(+), 40 deletions(-) diff --git a/src/plugins/ctf/fs-src/fs.cpp b/src/plugins/ctf/fs-src/fs.cpp index dba30e83..70ac4236 100644 --- a/src/plugins/ctf/fs-src/fs.cpp +++ b/src/plugins/ctf/fs-src/fs.cpp @@ -15,6 +15,7 @@ #include "common/assert.h" #include "common/common.h" #include "common/uuid.h" +#include "cpp-common/bt2s/make-unique.hpp" #include "plugins/common/param-validation/param-validation.h" @@ -309,10 +310,6 @@ void ctf_fs_destroy(struct ctf_fs_component *ctf_fs) return; } - if (ctf_fs->port_data) { - g_ptr_array_free(ctf_fs->port_data, TRUE); - } - delete ctf_fs; } @@ -321,36 +318,9 @@ void ctf_fs_component_deleter::operator()(ctf_fs_component *comp) ctf_fs_destroy(comp); } -static void port_data_destroy(struct ctf_fs_port_data *port_data) -{ - if (!port_data) { - return; - } - - delete port_data; -} - -static void port_data_destroy_notifier(void *data) -{ - port_data_destroy((struct ctf_fs_port_data *) data); -} - ctf_fs_component::UP ctf_fs_component_create(const bt2c::Logger& parentLogger) { - ctf_fs_component::UP ctf_fs {new ctf_fs_component {parentLogger}}; - - ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy_notifier); - if (!ctf_fs->port_data) { - goto error; - } - - goto end; - -error: - ctf_fs.reset(); - -end: - return ctf_fs; + return ctf_fs_component::UP {new ctf_fs_component {parentLogger}}; } void ctf_fs_finalize(bt_self_component_source *component) @@ -408,7 +378,7 @@ static int create_one_port_for_trace(struct ctf_fs_component *ctf_fs, bt_self_component_source *self_comp_src) { int ret = 0; - struct ctf_fs_port_data *port_data = NULL; + ctf_fs_port_data::UP port_data; bt2c::GCharUP port_name = ctf_fs_make_port_name(ds_file_group); if (!port_name) { @@ -418,23 +388,22 @@ static int create_one_port_for_trace(struct ctf_fs_component *ctf_fs, BT_CPPLOGI_SPEC(ctf_fs->logger, "Creating one port named `{}`", port_name.get()); /* Create output port for this file */ - port_data = new ctf_fs_port_data; + port_data = bt2s::make_unique(); port_data->ctf_fs = ctf_fs; port_data->ds_file_group = ds_file_group; - ret = bt_self_component_source_add_output_port(self_comp_src, port_name.get(), port_data, NULL); + ret = bt_self_component_source_add_output_port(self_comp_src, port_name.get(), port_data.get(), + NULL); if (ret) { goto error; } - g_ptr_array_add(ctf_fs->port_data, port_data); - port_data = NULL; + ctf_fs->port_data.emplace_back(std::move(port_data)); goto end; error: ret = -1; end: - port_data_destroy(port_data); return ret; } diff --git a/src/plugins/ctf/fs-src/fs.hpp b/src/plugins/ctf/fs-src/fs.hpp index af790384..a270304a 100644 --- a/src/plugins/ctf/fs-src/fs.hpp +++ b/src/plugins/ctf/fs-src/fs.hpp @@ -91,6 +91,8 @@ struct ctf_fs_trace struct ctf_fs_port_data { + using UP = std::unique_ptr; + /* Weak, belongs to ctf_fs_trace */ struct ctf_fs_ds_file_group *ds_file_group = nullptr; @@ -114,8 +116,7 @@ struct ctf_fs_component bt2c::Logger logger; - /* Array of struct ctf_fs_port_data *, owned by this */ - GPtrArray *port_data = nullptr; + std::vector port_data; ctf_fs_trace::UP trace; -- 2.34.1