src.ctf.fs: make ctf_fs_ds_index::entries a vector of ctf_fs_ds_index_entry::UP
authorSimon Marchi <simon.marchi@efficios.com>
Tue, 26 Jul 2022 21:05:49 +0000 (17:05 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 23 Aug 2022 16:06:16 +0000 (12:06 -0400)
Change-Id: If56c5ab6c35b6d93bf38b264f384496d2d06f4eb
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8254
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
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
src/plugins/ctf/fs-src/query.cpp

index f0c7da634bab65412a57b8d006045c0345a7b6ec..273c6b706af74392235421fb7f96f088dbd5b51f 100644 (file)
@@ -368,7 +368,7 @@ static enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data
     enum ctf_msg_iter_medium_status status;
 
     /* If we have gone through all index entries, we are done. */
-    if (data->next_index_entry_index >= data->ds_file_group->index->entries->len) {
+    if (data->next_index_entry_index >= data->ds_file_group->index->entries.size()) {
         status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
         goto end;
     }
@@ -377,8 +377,7 @@ static enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data
      * Otherwise, look up the next index entry / packet and prepare it
      *  for reading.
      */
-    index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
-        data->ds_file_group->index->entries, data->next_index_entry_index);
+    index_entry = data->ds_file_group->index->entries[data->next_index_entry_index].get();
 
     status = ctf_fs_ds_group_medops_set_file(data, index_entry);
     if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
@@ -414,7 +413,7 @@ enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_data_create(
     BT_ASSERT(self_msg_iter);
     BT_ASSERT(ds_file_group);
     BT_ASSERT(ds_file_group->index);
-    BT_ASSERT(ds_file_group->index->entries->len > 0);
+    BT_ASSERT(!ds_file_group->index->entries.empty());
 
     ctf_fs_ds_group_medops_data *data = new ctf_fs_ds_group_medops_data {logCfg};
     data->ds_file_group = ds_file_group;
@@ -448,11 +447,6 @@ struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops = {
     .borrow_stream = medop_group_borrow_stream,
 };
 
-static void ctf_fs_ds_index_entry_destroy(ctf_fs_ds_index_entry *entry)
-{
-    delete entry;
-}
-
 static ctf_fs_ds_index_entry::UP ctf_fs_ds_index_entry_create(const bt2_common::DataLen offset,
                                                               const bt2_common::DataLen packetSize)
 {
@@ -653,8 +647,7 @@ static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_f
 
         prev_index_entry = index_entry.get();
 
-        /* Give ownership of `index_entry` to `index->entries`. */
-        g_ptr_array_add(index->entries, index_entry.release());
+        index->entries.emplace_back(std::move(index_entry));
     }
 
     /* Validate that the index addresses the complete stream. */
@@ -796,7 +789,7 @@ static ctf_fs_ds_index::UP build_index_from_stream_file(struct ctf_fs_ds_file *d
             goto error;
         }
 
-        g_ptr_array_add(index->entries, index_entry.release());
+        index->entries.emplace_back(std::move(index_entry));
 
         currentPacketOffset += currentPacketSize;
         BT_CLOGD("Seeking to next packet: current-packet-offset-bytes=%llu, "
@@ -874,21 +867,7 @@ end:
 BT_HIDDEN
 ctf_fs_ds_index::UP ctf_fs_ds_index_create(const bt2_common::LogCfg& logCfg)
 {
-    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_CLOGE("Failed to allocate index entries.");
-        goto error;
-    }
-
-    goto end;
-
-error:
-    index.reset();
-
-end:
-    return index;
+    return ctf_fs_ds_index::UP {new ctf_fs_ds_index};
 }
 
 BT_HIDDEN
@@ -915,10 +894,6 @@ void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
         return;
     }
 
-    if (index->entries) {
-        g_ptr_array_free(index->entries, TRUE);
-    }
-
     delete index;
 }
 
index 91ded77f54442c47cefb7b955660d226d0858720..d7564851b20aae26fbce486b1f343e92e01d0592 100644 (file)
@@ -121,8 +121,7 @@ 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;
+    std::vector<ctf_fs_ds_index_entry::UP> entries;
 };
 
 struct ctf_fs_ds_file_group_deleter
index 3f946eec9e40c4d39d9165b529e4d7bf77cb12c1..a1a7ff2db437407f22d7174f15924dfadbf29c56 100644 (file)
@@ -441,24 +441,6 @@ end:
     return ret;
 }
 
-/* Replace by g_ptr_array_insert when we depend on glib >= 2.40. */
-static void array_insert(GPtrArray *array, gpointer element, size_t pos)
-{
-    size_t original_array_len = array->len;
-
-    /* Allocate an unused element at the end of the array. */
-    g_ptr_array_add(array, NULL);
-
-    /* If we are not inserting at the end, move the elements by one. */
-    if (pos < original_array_len) {
-        memmove(&(array->pdata[pos + 1]), &(array->pdata[pos]),
-                (original_array_len - pos) * sizeof(gpointer));
-    }
-
-    /* Insert the value. */
-    array->pdata[pos] = element;
-}
-
 /*
  * Insert ds_file_info in ds_file_group's list of ds_file_infos at the right
  * place to keep it sorted.
@@ -513,16 +495,12 @@ static bool ds_index_entries_equal(const struct ctf_fs_ds_index_entry *left,
  */
 
 static void ds_index_insert_ds_index_entry_sorted(struct ctf_fs_ds_index *index,
-                                                  struct ctf_fs_ds_index_entry *entry)
+                                                  ctf_fs_ds_index_entry::UP entry)
 {
-    guint i;
-    struct ctf_fs_ds_index_entry *other_entry = NULL;
-
     /* Find the spot where to insert this index entry. */
-    for (i = 0; i < index->entries->len; i++) {
-        other_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, i);
-
-        if (entry->timestamp_begin_ns <= other_entry->timestamp_begin_ns) {
+    auto otherEntry = index->entries.begin();
+    for (; otherEntry != index->entries.end(); ++otherEntry) {
+        if (entry->timestamp_begin_ns <= (*otherEntry)->timestamp_begin_ns) {
             break;
         }
     }
@@ -534,27 +512,16 @@ static void ds_index_insert_ds_index_entry_sorted(struct ctf_fs_ds_index *index,
      * snapshots of the same trace.  We then want the index to contain
      * a reference to only one copy of that packet.
      */
-    if (i == index->entries->len || !ds_index_entries_equal(entry, other_entry)) {
-        array_insert(index->entries, entry, i);
-    } else {
-        delete entry;
+    if (otherEntry == index->entries.end() ||
+        !ds_index_entries_equal(entry.get(), otherEntry->get())) {
+        index->entries.insert(otherEntry, std::move(entry));
     }
 }
 
 static void merge_ctf_fs_ds_indexes(struct ctf_fs_ds_index *dest, ctf_fs_ds_index::UP src)
 {
-    guint i;
-
-    for (i = 0; i < src->entries->len; i++) {
-        struct ctf_fs_ds_index_entry *entry =
-            (struct ctf_fs_ds_index_entry *) g_ptr_array_index(src->entries, i);
-
-        /*
-               * Ownership of the ctf_fs_ds_index_entry is transferred to
-               * ds_index_insert_ds_index_entry_sorted.
-               */
-        g_ptr_array_index(src->entries, i) = NULL;
-        ds_index_insert_ds_index_entry_sorted(dest, entry);
+    for (ctf_fs_ds_index_entry::UP& entry : src->entries) {
+        ds_index_insert_ds_index_entry_sorted(dest, std::move(entry));
     }
 }
 
@@ -1295,27 +1262,22 @@ static int fix_index_lttng_event_after_packet_bug(struct ctf_fs_trace *trace)
     const bt2_common::LogCfg& logCfg = trace->logCfg;
 
     for (ctf_fs_ds_file_group::UP& ds_file_group : trace->ds_file_groups) {
-        guint entry_i;
         struct ctf_clock_class *default_cc;
-        struct ctf_fs_ds_index_entry *last_entry;
         struct ctf_fs_ds_index *index;
 
         BT_ASSERT(ds_file_group);
         index = ds_file_group->index.get();
 
         BT_ASSERT(index);
-        BT_ASSERT(index->entries);
-        BT_ASSERT(index->entries->len > 0);
+        BT_ASSERT(!index->entries.empty());
 
         /*
          * Iterate over all entries but the last one. The last one is
          * fixed differently after.
          */
-        for (entry_i = 0; entry_i < index->entries->len - 1; entry_i++) {
-            struct ctf_fs_ds_index_entry *curr_entry, *next_entry;
-
-            curr_entry = (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_i);
-            next_entry = (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_i + 1);
+        for (size_t entry_i = 0; entry_i < index->entries.size() - 1; ++entry_i) {
+            ctf_fs_ds_index_entry *curr_entry = index->entries[entry_i].get();
+            ctf_fs_ds_index_entry *next_entry = index->entries[entry_i + 1].get();
 
             /*
              * 1. Set the current index entry `end` timestamp to
@@ -1329,8 +1291,7 @@ static int fix_index_lttng_event_after_packet_bug(struct ctf_fs_trace *trace)
          * 2. Fix the last entry by decoding the last event of the last
          * packet.
          */
-        last_entry =
-            (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, index->entries->len - 1);
+        ctf_fs_ds_index_entry *last_entry = index->entries.back().get();
         BT_ASSERT(last_entry);
 
         BT_ASSERT(ds_file_group->sc->default_clock_class);
@@ -1374,13 +1335,11 @@ static int fix_index_barectf_event_before_packet_bug(struct ctf_fs_trace *trace)
     const bt2_common::LogCfg& logCfg = trace->logCfg;
 
     for (ctf_fs_ds_file_group::UP& ds_file_group : trace->ds_file_groups) {
-        guint entry_i;
         struct ctf_clock_class *default_cc;
         struct ctf_fs_ds_index *index = ds_file_group->index.get();
 
         BT_ASSERT(index);
-        BT_ASSERT(index->entries);
-        BT_ASSERT(index->entries->len > 0);
+        BT_ASSERT(!index->entries.empty());
 
         BT_ASSERT(ds_file_group->sc->default_clock_class);
         default_cc = ds_file_group->sc->default_clock_class;
@@ -1389,11 +1348,9 @@ static int fix_index_barectf_event_before_packet_bug(struct ctf_fs_trace *trace)
          * 1. Iterate over the index, starting from the second entry
          * (index = 1).
          */
-        for (entry_i = 1; entry_i < index->entries->len; entry_i++) {
-            ctf_fs_ds_index_entry *prev_entry =
-                (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_i - 1);
-            ctf_fs_ds_index_entry *curr_entry =
-                (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_i);
+        for (size_t entry_i = 1; entry_i < index->entries.size(); ++entry_i) {
+            ctf_fs_ds_index_entry *prev_entry = index->entries[entry_i - 1].get();
+            ctf_fs_ds_index_entry *curr_entry = index->entries[entry_i].get();
             /*
              * 2. Set the current entry `begin` timestamp to the
              * timestamp of the first event of the current packet.
@@ -1441,7 +1398,6 @@ static int fix_index_lttng_crash_quirk(struct ctf_fs_trace *trace)
     const bt2_common::LogCfg& logCfg = trace->logCfg;
 
     for (ctf_fs_ds_file_group::UP& ds_file_group : trace->ds_file_groups) {
-        guint entry_idx;
         struct ctf_clock_class *default_cc;
         struct ctf_fs_ds_index *index;
 
@@ -1452,11 +1408,9 @@ static int fix_index_lttng_crash_quirk(struct ctf_fs_trace *trace)
         default_cc = ds_file_group->sc->default_clock_class;
 
         BT_ASSERT(index);
-        BT_ASSERT(index->entries);
-        BT_ASSERT(index->entries->len > 0);
+        BT_ASSERT(!index->entries.empty());
 
-        ctf_fs_ds_index_entry *last_entry =
-            (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, index->entries->len - 1);
+        ctf_fs_ds_index_entry *last_entry = index->entries.back().get();
         BT_ASSERT(last_entry);
 
         /* 1. Fix the last entry first. */
@@ -1475,11 +1429,9 @@ static int fix_index_lttng_crash_quirk(struct ctf_fs_trace *trace)
         }
 
         /* Iterate over all entries but the last one. */
-        for (entry_idx = 0; entry_idx < index->entries->len - 1; entry_idx++) {
-            ctf_fs_ds_index_entry *curr_entry =
-                (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_idx);
-            ctf_fs_ds_index_entry *next_entry =
-                (ctf_fs_ds_index_entry *) g_ptr_array_index(index->entries, entry_idx + 1);
+        for (size_t entry_idx = 0; entry_idx < index->entries.size() - 1; ++entry_idx) {
+            ctf_fs_ds_index_entry *curr_entry = index->entries[entry_idx].get();
+            ctf_fs_ds_index_entry *next_entry = index->entries[entry_idx + 1].get();
 
             if (curr_entry->timestamp_end == 0 && curr_entry->timestamp_begin != 0) {
                 /*
index 0d850a9ffbc10c93532e3dc93795bdcf5f58c02d..fe2a7059b90ea23def6b95c0bb59632a36ecd46b 100644 (file)
@@ -121,16 +121,13 @@ static void populate_stream_info(struct ctf_fs_ds_file_group *group, bt2::MapVal
      * of the last index entry.
      */
     BT_ASSERT(group->index);
-    BT_ASSERT(group->index->entries);
-    BT_ASSERT(group->index->entries->len > 0);
+    BT_ASSERT(!group->index->entries.empty());
 
     /* First entry. */
-    ctf_fs_ds_index_entry *first_ds_index_entry =
-        (struct ctf_fs_ds_index_entry *) g_ptr_array_index(group->index->entries, 0);
+    ctf_fs_ds_index_entry *first_ds_index_entry = group->index->entries.front().get();
 
     /* Last entry. */
-    ctf_fs_ds_index_entry *last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
-        group->index->entries, group->index->entries->len - 1);
+    ctf_fs_ds_index_entry *last_ds_index_entry = group->index->entries.back().get();
 
     stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns;
     stream_range->end_ns = last_ds_index_entry->timestamp_end_ns;
This page took 0.037007 seconds and 5 git commands to generate.