src.ctf.fs: make ctf_fs_ds_file_group_create return a ctf_fs_ds_file_group::UP
authorSimon Marchi <simon.marchi@efficios.com>
Thu, 7 Dec 2023 20:33:44 +0000 (20:33 +0000)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 17 Apr 2024 17:57:53 +0000 (13:57 -0400)
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 <simon.marchi@efficios.com>
Change-Id: Ic89ec8edc5d7dd779c836beb962dcad7e45ab996
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8245
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12284

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 d2d31471a7c9d095e19005a17ff2f0f4aea362bb..15a7a368709957c563a7e1ac84dd1ce0ee2a3bf3 100644 (file)
@@ -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;
index 0b6a95afadf8d98a23e0b48c745fb7015da4e97d..54c484a61a96ad54d9e47ac8b70d7eb2ed98dc55 100644 (file)
@@ -7,6 +7,8 @@
 #ifndef CTF_FS_DS_FILE_H
 #define CTF_FS_DS_FILE_H
 
+#include <memory>
+
 #include <glib.h>
 #include <stdio.h>
 
@@ -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<ctf_fs_ds_file_group, ctf_fs_ds_file_group_deleter>;
+
     /*
      * 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);
 
index 70ac423696244861bea03781ec859596841f324f..7b2e6af3379c81c05bdca887cc76652184a8eba1 100644 (file)
@@ -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);
This page took 0.027896 seconds and 4 git commands to generate.