src.ctf.fs: make ctf_fs_ds_index::entries a vector of ctf_fs_ds_index_entry::UP
[babeltrace.git] / src / plugins / ctf / fs-src / fs.cpp
index b7d2d855f63afda0add191a4165459380f3d6f75..40e50085fa87aabf8543bcbf35a3518c38668195 100644 (file)
@@ -421,24 +421,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.
@@ -493,16 +475,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;
         }
     }
@@ -514,27 +492,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 (auto& entry : src->entries) {
+        ds_index_insert_ds_index_entry_sorted(dest, std::move(entry));
     }
 }
 
@@ -1059,7 +1026,7 @@ static int merge_matching_ctf_fs_ds_file_groups(struct ctf_fs_trace *dest_trace,
                                                            src_group->sc->id);
             BT_ASSERT(sc);
 
-            auto index = ctf_fs_ds_index_create(dest_trace->logger);
+            auto index = ctf_fs_ds_index_create();
             if (!index) {
                 ret = -1;
                 goto end;
@@ -1284,26 +1251,21 @@ static int fix_index_lttng_event_after_packet_bug(struct ctf_fs_trace *trace)
     int ret = 0;
 
     for (const auto& ds_file_group : trace->ds_file_groups) {
-        guint entry_i;
         struct ctf_clock_class *default_cc;
-        struct ctf_fs_ds_index_entry *last_entry;
 
         BT_ASSERT(ds_file_group);
         const auto 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
@@ -1317,8 +1279,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);
+        const auto last_entry = index->entries.back().get();
         BT_ASSERT(last_entry);
 
         BT_ASSERT(ds_file_group->sc->default_clock_class);
@@ -1362,13 +1323,11 @@ static int fix_index_barectf_event_before_packet_bug(struct ctf_fs_trace *trace)
     int ret = 0;
 
     for (const auto& ds_file_group : trace->ds_file_groups) {
-        guint entry_i;
         struct ctf_clock_class *default_cc;
         const auto 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;
@@ -1377,11 +1336,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.
@@ -1429,7 +1386,6 @@ static int fix_index_lttng_crash_quirk(struct ctf_fs_trace *trace)
     int ret = 0;
 
     for (const auto& ds_file_group : trace->ds_file_groups) {
-        guint entry_idx;
         struct ctf_clock_class *default_cc;
 
         BT_ASSERT(ds_file_group);
@@ -1439,11 +1395,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);
+        const auto last_entry = index->entries.back().get();
         BT_ASSERT(last_entry);
 
         /* 1. Fix the last entry first. */
@@ -1463,11 +1417,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) {
                 /*
This page took 0.032905 seconds and 4 git commands to generate.