From fe2e19c47f25132d9a54d2e00cc284d70221c824 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Thu, 7 Dec 2023 20:33:44 +0000 Subject: [PATCH] src.ctf.fs: make ctf_fs_ds_file_group_create return a ctf_fs_ds_file_group::UP Introduced ctf_fs_ds_file_group::UP, a unique_ptr type with a deleter that calls ctf_fs_ds_file_group_destroy. Change ctf_fs_ds_file_group_create to return that type. Signed-off-by: Simon Marchi Change-Id: Ic89ec8edc5d7dd779c836beb962dcad7e45ab996 Reviewed-on: https://review.lttng.org/c/babeltrace/+/8245 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/12284 --- src/plugins/ctf/fs-src/data-stream-file.cpp | 19 ++++++----- src/plugins/ctf/fs-src/data-stream-file.hpp | 17 +++++++--- src/plugins/ctf/fs-src/fs.cpp | 35 +++++++++++---------- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/src/plugins/ctf/fs-src/data-stream-file.cpp b/src/plugins/ctf/fs-src/data-stream-file.cpp index d2d31471..15a7a368 100644 --- a/src/plugins/ctf/fs-src/data-stream-file.cpp +++ b/src/plugins/ctf/fs-src/data-stream-file.cpp @@ -971,12 +971,18 @@ void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group) delete ds_file_group; } -struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create(struct ctf_fs_trace *ctf_fs_trace, - struct ctf_stream_class *sc, - uint64_t stream_instance_id, - struct ctf_fs_ds_index *index) +void ctf_fs_ds_file_group_deleter::operator()(ctf_fs_ds_file_group *group) noexcept { - ctf_fs_ds_file_group *ds_file_group = new ctf_fs_ds_file_group; + ctf_fs_ds_file_group_destroy(group); +} + +ctf_fs_ds_file_group::UP ctf_fs_ds_file_group_create(struct ctf_fs_trace *ctf_fs_trace, + struct ctf_stream_class *sc, + uint64_t stream_instance_id, + struct ctf_fs_ds_index *index) +{ + ctf_fs_ds_file_group::UP ds_file_group {new ctf_fs_ds_file_group}; + ds_file_group->ds_file_infos = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_fs_ds_file_info_destroy); if (!ds_file_group->ds_file_infos) { @@ -992,9 +998,8 @@ struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create(struct ctf_fs_trace *ct goto end; error: - ctf_fs_ds_file_group_destroy(ds_file_group); + ds_file_group.reset(); ctf_fs_ds_index_destroy(index); - ds_file_group = NULL; end: return ds_file_group; diff --git a/src/plugins/ctf/fs-src/data-stream-file.hpp b/src/plugins/ctf/fs-src/data-stream-file.hpp index 0b6a95af..54c484a6 100644 --- a/src/plugins/ctf/fs-src/data-stream-file.hpp +++ b/src/plugins/ctf/fs-src/data-stream-file.hpp @@ -7,6 +7,8 @@ #ifndef CTF_FS_DS_FILE_H #define CTF_FS_DS_FILE_H +#include + #include #include @@ -107,8 +109,15 @@ struct ctf_fs_ds_index GPtrArray *entries = nullptr; }; +struct ctf_fs_ds_file_group_deleter +{ + void operator()(struct ctf_fs_ds_file_group *group) noexcept; +}; + struct ctf_fs_ds_file_group { + using UP = std::unique_ptr; + /* * Array of struct ctf_fs_ds_file_info, owned by this. * @@ -155,10 +164,10 @@ void ctf_fs_ds_file_info_destroy(struct ctf_fs_ds_file_info *ds_file_info); struct ctf_fs_ds_file_info *ctf_fs_ds_file_info_create(const char *path, int64_t begin_ns); -struct ctf_fs_ds_file_group *ctf_fs_ds_file_group_create(struct ctf_fs_trace *ctf_fs_trace, - struct ctf_stream_class *sc, - uint64_t stream_instance_id, - struct ctf_fs_ds_index *index); +ctf_fs_ds_file_group::UP ctf_fs_ds_file_group_create(struct ctf_fs_trace *ctf_fs_trace, + struct ctf_stream_class *sc, + uint64_t stream_instance_id, + struct ctf_fs_ds_index *index); void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group); diff --git a/src/plugins/ctf/fs-src/fs.cpp b/src/plugins/ctf/fs-src/fs.cpp index 70ac4236..7b2e6af3 100644 --- a/src/plugins/ctf/fs-src/fs.cpp +++ b/src/plugins/ctf/fs-src/fs.cpp @@ -553,7 +553,7 @@ static int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, const int64_t stream_instance_id = -1; int64_t begin_ns = -1; struct ctf_fs_ds_file_group *ds_file_group = NULL; - bool add_group = false; + ctf_fs_ds_file_group::UP new_ds_file_group; int ret; size_t i; struct ctf_fs_ds_file *ds_file = NULL; @@ -639,17 +639,17 @@ static int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, const * there's no timestamp to order the file within its * group. */ - ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, sc, UINT64_C(-1), index); + new_ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, sc, UINT64_C(-1), index); + /* Ownership of index is transferred. */ index = NULL; - if (!ds_file_group) { + if (!new_ds_file_group) { goto error; } - ds_file_group_insert_ds_file_info_sorted(ds_file_group, BT_MOVE_REF(ds_file_info)); - - add_group = true; + ds_file_group_insert_ds_file_info_sorted(new_ds_file_group.get(), + BT_MOVE_REF(ds_file_info)); goto end; } @@ -669,14 +669,15 @@ static int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, const } if (!ds_file_group) { - ds_file_group = ctf_fs_ds_file_group_create(ctf_fs_trace, sc, stream_instance_id, index); + new_ds_file_group = + ctf_fs_ds_file_group_create(ctf_fs_trace, sc, stream_instance_id, index); /* Ownership of index is transferred. */ index = NULL; - if (!ds_file_group) { + if (!new_ds_file_group) { goto error; } - add_group = true; + ds_file_group = new_ds_file_group.get(); } else { merge_ctf_fs_ds_indexes(ds_file_group->index, index); } @@ -686,13 +687,11 @@ static int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, const goto end; error: - ctf_fs_ds_file_group_destroy(ds_file_group); - ds_file_group = NULL; ret = -1; end: - if (add_group && ds_file_group) { - g_ptr_array_add(ctf_fs_trace->ds_file_groups, ds_file_group); + if (new_ds_file_group) { + g_ptr_array_add(ctf_fs_trace->ds_file_groups, new_ds_file_group.release()); } ctf_fs_ds_file_destroy(ds_file); @@ -1110,15 +1109,19 @@ static int merge_matching_ctf_fs_ds_file_groups(struct ctf_fs_trace *dest_trace, goto end; } - dest_group = ctf_fs_ds_file_group_create(dest_trace, sc, src_group->stream_id, index); + auto new_dest_group = + ctf_fs_ds_file_group_create(dest_trace, sc, src_group->stream_id, index); + /* Ownership of index is transferred. */ index = NULL; - if (!dest_group) { + if (!new_dest_group) { ret = -1; goto end; } - g_ptr_array_add(dest_trace->ds_file_groups, dest_group); + dest_group = new_dest_group.get(); + + g_ptr_array_add(dest_trace->ds_file_groups, new_dest_group.release()); } BT_ASSERT(dest_group); -- 2.34.1