From c05e140522070881b7beecc1a74249d3f6d6643c Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Mon, 4 Dec 2023 21:13:53 +0000 Subject: [PATCH] src.ctf.fs: make ctf_fs_ds_index_entry_create return a unique_ptr Introduce ctf_fs_ds_index_entry::UP and make ctf_fs_ds_index_entry_create return that. Modify a few functions in data-stream-file.cpp that deal with this. Signed-off-by: Simon Marchi Change-Id: I39598d860327cff5af32804f75b68b58e4ee0821 Reviewed-on: https://review.lttng.org/c/babeltrace/+/8253 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/12291 --- src/plugins/ctf/fs-src/data-stream-file.cpp | 23 +++++++++------------ src/plugins/ctf/fs-src/data-stream-file.hpp | 2 ++ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/plugins/ctf/fs-src/data-stream-file.cpp b/src/plugins/ctf/fs-src/data-stream-file.cpp index 11dee041..63140e45 100644 --- a/src/plugins/ctf/fs-src/data-stream-file.cpp +++ b/src/plugins/ctf/fs-src/data-stream-file.cpp @@ -443,10 +443,10 @@ static void ctf_fs_ds_index_entry_destroy(ctf_fs_ds_index_entry *entry) delete entry; } -static struct ctf_fs_ds_index_entry *ctf_fs_ds_index_entry_create(const bt2c::DataLen offset, - const bt2c::DataLen packetSize) +static ctf_fs_ds_index_entry::UP ctf_fs_ds_index_entry_create(const bt2c::DataLen offset, + const bt2c::DataLen packetSize) { - ctf_fs_ds_index_entry *entry = new ctf_fs_ds_index_entry {offset, packetSize}; + ctf_fs_ds_index_entry::UP entry = bt2s::make_unique(offset, packetSize); entry->packet_seq_num = UINT64_MAX; @@ -474,7 +474,8 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f const char *mmap_begin = NULL, *file_pos = NULL; const struct ctf_packet_index_file_hdr *header = NULL; ctf_fs_ds_index::UP index; - struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL; + ctf_fs_ds_index_entry::UP index_entry; + ctf_fs_ds_index_entry *prev_index_entry = NULL; auto totalPacketsSize = bt2c::DataLen::fromBytes(0); size_t file_index_entry_size; size_t file_entry_count; @@ -652,11 +653,10 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f totalPacketsSize += packetSize; file_pos += file_index_entry_size; - prev_index_entry = index_entry; + prev_index_entry = index_entry.get(); /* Give ownership of `index_entry` to `index->entries`. */ - g_ptr_array_add(index->entries, index_entry); - index_entry = NULL; + g_ptr_array_add(index->entries, index_entry.release()); } /* Validate that the index addresses the complete stream. */ @@ -680,7 +680,6 @@ end: return index; error: index.reset(); - ctf_fs_ds_index_entry_destroy(index_entry); goto end; } @@ -786,8 +785,7 @@ static ctf_fs_ds_index::UP build_index_from_stream_file(struct ctf_fs_ds_file *d goto error; } - const auto index_entry = - ctf_fs_ds_index_entry_create(currentPacketOffset, currentPacketSize); + auto index_entry = ctf_fs_ds_index_entry_create(currentPacketOffset, currentPacketSize); if (!index_entry) { BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file->logger, "Failed to create a ctf_fs_ds_index_entry."); @@ -797,13 +795,12 @@ static ctf_fs_ds_index::UP build_index_from_stream_file(struct ctf_fs_ds_file *d /* Set path to stream file. */ index_entry->path = file_info->path.c_str(); - ret = init_index_entry(index_entry, ds_file, &props); + ret = init_index_entry(index_entry.get(), ds_file, &props); if (ret) { - ctf_fs_ds_index_entry_destroy(index_entry); goto error; } - g_ptr_array_add(index->entries, index_entry); + g_ptr_array_add(index->entries, index_entry.release()); currentPacketOffset += currentPacketSize; BT_CPPLOGD_SPEC(ds_file->logger, diff --git a/src/plugins/ctf/fs-src/data-stream-file.hpp b/src/plugins/ctf/fs-src/data-stream-file.hpp index 9e8696e4..df0c0c78 100644 --- a/src/plugins/ctf/fs-src/data-stream-file.hpp +++ b/src/plugins/ctf/fs-src/data-stream-file.hpp @@ -72,6 +72,8 @@ struct ctf_fs_ds_file struct ctf_fs_ds_index_entry { + using UP = std::unique_ptr; + explicit ctf_fs_ds_index_entry(const bt2c::DataLen offsetParam, const bt2c::DataLen packetSizeParam) noexcept : offset(offsetParam), -- 2.34.1