src.ctf.fs: make ctf_fs_ds_index_create return a unique_ptr
authorSimon Marchi <simon.marchi@efficios.com>
Mon, 4 Dec 2023 20:20:20 +0000 (20:20 +0000)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 17 Apr 2024 17:57:53 +0000 (13:57 -0400)
Introduce ctf_fs_ds_index::UP, a unique_ptr with a deleter that calls
ctf_fs_ds_index_destroy.  Make ctf_fs_ds_index_create (and the
other functions creating an index in data-stream-file.cpp) return it.

Change-Id: Id04124aeaff3384e566a43fbbd6c1fedd2bf9bc3
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8251
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12289

src/plugins/ctf/fs-src/data-stream-file.cpp
src/plugins/ctf/fs-src/data-stream-file.hpp
src/plugins/ctf/fs-src/fs.cpp

index 21f474240ad88d3bb7f45be58fe9440621639e75..d0cf562be8bea84e946650f2e33a5998434eb9a5 100644 (file)
@@ -460,9 +460,9 @@ static int convert_cycles_to_ns(struct ctf_clock_class *clock_class, uint64_t cy
                                                   clock_class->offset_cycles, ns);
 }
 
-static struct ctf_fs_ds_index *build_index_from_idx_file(struct ctf_fs_ds_file *ds_file,
-                                                         struct ctf_fs_ds_file_info *file_info,
-                                                         struct ctf_msg_iter *msg_iter)
+static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_file,
+                                                     struct ctf_fs_ds_file_info *file_info,
+                                                     struct ctf_msg_iter *msg_iter)
 {
     int ret;
     gchar *directory = NULL;
@@ -473,7 +473,7 @@ static struct ctf_fs_ds_index *build_index_from_idx_file(struct ctf_fs_ds_file *
     gsize filesize;
     const char *mmap_begin = NULL, *file_pos = NULL;
     const struct ctf_packet_index_file_hdr *header = NULL;
-    struct ctf_fs_ds_index *index = NULL;
+    ctf_fs_ds_index::UP index;
     struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL;
     auto totalPacketsSize = bt2c::DataLen::fromBytes(0);
     size_t file_index_entry_size;
@@ -679,9 +679,8 @@ end:
     }
     return index;
 error:
-    ctf_fs_ds_index_destroy(index);
+    index.reset();
     ctf_fs_ds_index_entry_destroy(index_entry);
-    index = NULL;
     goto end;
 }
 
@@ -730,12 +729,12 @@ end:
     return ret;
 }
 
-static struct ctf_fs_ds_index *build_index_from_stream_file(struct ctf_fs_ds_file *ds_file,
-                                                            struct ctf_fs_ds_file_info *file_info,
-                                                            struct ctf_msg_iter *msg_iter)
+static ctf_fs_ds_index::UP build_index_from_stream_file(struct ctf_fs_ds_file *ds_file,
+                                                        struct ctf_fs_ds_file_info *file_info,
+                                                        struct ctf_msg_iter *msg_iter)
 {
     int ret;
-    struct ctf_fs_ds_index *index = NULL;
+    ctf_fs_ds_index::UP index;
     enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
     auto currentPacketOffset = bt2c::DataLen::fromBytes(0);
 
@@ -818,8 +817,7 @@ end:
     return index;
 
 error:
-    ctf_fs_ds_index_destroy(index);
-    index = NULL;
+    index.reset();
     goto end;
 }
 
@@ -862,13 +860,11 @@ end:
     return ds_file;
 }
 
-struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file,
-                                                   struct ctf_fs_ds_file_info *file_info,
-                                                   struct ctf_msg_iter *msg_iter)
+ctf_fs_ds_index::UP ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file,
+                                               struct ctf_fs_ds_file_info *file_info,
+                                               struct ctf_msg_iter *msg_iter)
 {
-    struct ctf_fs_ds_index *index;
-
-    index = build_index_from_idx_file(ds_file, file_info, msg_iter);
+    auto index = build_index_from_idx_file(ds_file, file_info, msg_iter);
     if (index) {
         goto end;
     }
@@ -880,9 +876,10 @@ end:
     return index;
 }
 
-struct ctf_fs_ds_index *ctf_fs_ds_index_create(const bt2c::Logger& logger)
+ctf_fs_ds_index::UP ctf_fs_ds_index_create(const bt2c::Logger& logger)
 {
-    ctf_fs_ds_index *index = new ctf_fs_ds_index;
+    ctf_fs_ds_index::UP index {new ctf_fs_ds_index};
+
     index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_fs_ds_index_entry_destroy);
     if (!index->entries) {
         BT_CPPLOGE_SPEC(logger, "Failed to allocate index entries.");
@@ -892,8 +889,8 @@ struct ctf_fs_ds_index *ctf_fs_ds_index_create(const bt2c::Logger& logger)
     goto end;
 
 error:
-    ctf_fs_ds_index_destroy(index);
-    index = NULL;
+    index.reset();
+
 end:
     return index;
 }
@@ -927,6 +924,11 @@ void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
     delete index;
 }
 
+void ctf_fs_ds_index_deleter::operator()(ctf_fs_ds_index * const index) noexcept
+{
+    ctf_fs_ds_index_destroy(index);
+}
+
 ctf_fs_ds_file_info::UP ctf_fs_ds_file_info_create(const char *path, int64_t begin_ns)
 {
     ctf_fs_ds_file_info::UP ds_file_info = bt2s::make_unique<ctf_fs_ds_file_info>();
index 139281794ab20fa7ce537e55f99cd1c9475e1cc8..a87877caf319e1d08bf7007cdf6730e310c5c181 100644 (file)
@@ -106,8 +106,15 @@ struct ctf_fs_ds_index_entry
     uint64_t packet_seq_num = 0;
 };
 
+struct ctf_fs_ds_index_deleter
+{
+    void operator()(struct ctf_fs_ds_index *index) noexcept;
+};
+
 struct ctf_fs_ds_index
 {
+    using UP = std::unique_ptr<ctf_fs_ds_index, ctf_fs_ds_index_deleter>;
+
     /* Array of pointer to struct ctf_fs_ds_index_entry. */
     GPtrArray *entries = nullptr;
 };
@@ -153,11 +160,11 @@ struct ctf_fs_ds_file *ctf_fs_ds_file_create(struct ctf_fs_trace *ctf_fs_trace,
 
 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *stream);
 
-struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file,
-                                                   struct ctf_fs_ds_file_info *ds_file_info,
-                                                   struct ctf_msg_iter *msg_iter);
+ctf_fs_ds_index::UP ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file,
+                                               struct ctf_fs_ds_file_info *ds_file_info,
+                                               struct ctf_msg_iter *msg_iter);
 
-struct ctf_fs_ds_index *ctf_fs_ds_index_create(const bt2c::Logger& logger);
+ctf_fs_ds_index::UP ctf_fs_ds_index_create(const bt2c::Logger& logger);
 
 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index);
 
index 7ad16003d0430378e820f412d947cdeb35b47095..2c8a440ce6bf4277ac47bb9d33c12b118e4bb9d9 100644 (file)
@@ -604,7 +604,7 @@ static int add_ds_file_to_ds_file_group(struct ctf_fs_trace *ctf_fs_trace, const
         goto error;
     }
 
-    index = ctf_fs_ds_file_build_index(ds_file, ds_file_info.get(), msg_iter);
+    index = ctf_fs_ds_file_build_index(ds_file, ds_file_info.get(), msg_iter).release();
     if (!index) {
         BT_CPPLOGE_APPEND_CAUSE_SPEC(ctf_fs_trace->logger, "Failed to index CTF stream file \'{}\'",
                                      ds_file->file->path->str);
@@ -1059,13 +1059,12 @@ static int merge_matching_ctf_fs_ds_file_groups(struct ctf_fs_trace *dest_trace,
          */
         if (!dest_group) {
             struct ctf_stream_class *sc;
-            struct ctf_fs_ds_index *index;
 
             sc = ctf_trace_class_borrow_stream_class_by_id(dest_trace->metadata->tc,
                                                            src_group->sc->id);
             BT_ASSERT(sc);
 
-            index = ctf_fs_ds_index_create(dest_trace->logger);
+            auto index = ctf_fs_ds_index_create(dest_trace->logger).release();
             if (!index) {
                 ret = -1;
                 goto end;
This page took 0.02842 seconds and 4 git commands to generate.