Sort includes in C++ files
[babeltrace.git] / src / plugins / ctf / common / msg-iter / msg-iter.cpp
index eed758535a24c95bbd249791907ad02edf025c8d..38305c84455fed8dcbb2cf667c095d0a320085df 100644 (file)
@@ -7,25 +7,27 @@
  * Babeltrace - CTF message iterator
  */
 
+#include <glib.h>
+#include <inttypes.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <babeltrace2/babeltrace.h>
+
 #define BT_COMP_LOG_SELF_COMP (msg_it->self_comp)
 #define BT_LOG_OUTPUT_LEVEL   (msg_it->log_level)
 #define BT_LOG_TAG            "PLUGIN/CTF/MSG-ITER"
 #include "logging/comp-logging.h"
 
-#include <stdint.h>
-#include <inttypes.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <stdbool.h>
 #include "common/assert.h"
-#include <string.h>
-#include <babeltrace2/babeltrace.h>
 #include "common/common.h"
-#include <glib.h>
-#include <stdlib.h>
 
-#include "msg-iter.hpp"
 #include "../bfcr/bfcr.hpp"
+#include "msg-iter.hpp"
 
 struct ctf_msg_iter;
 
@@ -394,7 +396,7 @@ static void stack_push(struct stack *stack, bt_field *base)
         g_array_set_size(stack->entries, stack->size + 1);
     }
 
-    entry = &g_array_index(stack->entries, struct stack_entry, stack->size);
+    entry = &bt_g_array_index(stack->entries, struct stack_entry, stack->size);
     entry->base = base;
     entry->index = 0;
     stack->size++;
@@ -423,7 +425,7 @@ static inline struct stack_entry *stack_top(struct stack *stack)
 {
     BT_ASSERT_DBG(stack);
     BT_ASSERT_DBG(stack_size(stack));
-    return &g_array_index(stack->entries, struct stack_entry, stack->size - 1);
+    return &bt_g_array_index(stack->entries, struct stack_entry, stack->size - 1);
 }
 
 static inline bool stack_empty(struct stack *stack)
@@ -1793,6 +1795,20 @@ end:
                  msg_it->default_clock_snapshot);
 }
 
+/*
+ * Ensure the message iterator's `stored_values` array is large enough to
+ * accomodate `storing_index`.
+ *
+ * We may need more slots in the array than initially allocated if more
+ * metadata arrives along the way.
+ */
+static void ensure_stored_values_size(ctf_msg_iter *msg_it, uint64_t storing_index)
+{
+    if (G_UNLIKELY(storing_index >= msg_it->stored_values->len)) {
+        g_array_set_size(msg_it->stored_values, msg_it->meta.tc->stored_value_count);
+    }
+}
+
 static enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value, struct ctf_field_class *fc,
                                                 void *data)
 {
@@ -1862,7 +1878,8 @@ update_def_clock:
     }
 
     if (G_UNLIKELY(int_fc->storing_index >= 0)) {
-        g_array_index(msg_it->stored_values, uint64_t, (uint64_t) int_fc->storing_index) = value;
+        ensure_stored_values_size(msg_it, int_fc->storing_index);
+        bt_g_array_index(msg_it->stored_values, uint64_t, (uint64_t) int_fc->storing_index) = value;
     }
 
     if (G_UNLIKELY(!fc->in_ir || msg_it->dry_run)) {
@@ -1948,7 +1965,8 @@ static enum bt_bfcr_status bfcr_signed_int_cb(int64_t value, struct ctf_field_cl
     BT_ASSERT_DBG(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
 
     if (G_UNLIKELY(int_fc->storing_index >= 0)) {
-        g_array_index(msg_it->stored_values, uint64_t, (uint64_t) int_fc->storing_index) =
+        ensure_stored_values_size(msg_it, int_fc->storing_index);
+        bt_g_array_index(msg_it->stored_values, uint64_t, (uint64_t) int_fc->storing_index) =
             (uint64_t) value;
     }
 
@@ -2191,7 +2209,8 @@ static int64_t bfcr_get_sequence_length_cb(struct ctf_field_class *fc, void *dat
     int64_t length;
     int ret;
 
-    length = (uint64_t) g_array_index(msg_it->stored_values, uint64_t, seq_fc->stored_length_index);
+    length =
+        (uint64_t) bt_g_array_index(msg_it->stored_values, uint64_t, seq_fc->stored_length_index);
 
     if (G_UNLIKELY(msg_it->dry_run)) {
         goto end;
@@ -2242,7 +2261,7 @@ bfcr_borrow_variant_selected_field_class_cb(struct ctf_field_class *fc, void *da
     } tag;
 
     /* Get variant's tag */
-    tag.u = g_array_index(msg_it->stored_values, uint64_t, var_fc->stored_tag_index);
+    tag.u = bt_g_array_index(msg_it->stored_values, uint64_t, var_fc->stored_tag_index);
 
     /*
      * Check each range to find the selected option's index.
This page took 0.024882 seconds and 4 git commands to generate.