ctf: grow stored_values array when necessary
[babeltrace.git] / src / plugins / ctf / common / msg-iter / msg-iter.cpp
index e4190724dc152b734b6b7e4cb07d04de73c0e859..5f38ec6c0dd5ebdac1397e228086d07558d7840b 100644 (file)
@@ -394,7 +394,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 +423,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)
@@ -1672,7 +1672,6 @@ static inline enum ctf_msg_iter_status handle_state(struct ctf_msg_iter *msg_it)
     return status;
 }
 
-BT_HIDDEN
 void ctf_msg_iter_reset_for_next_stream_file(struct ctf_msg_iter *msg_it)
 {
     BT_ASSERT(msg_it);
@@ -1703,7 +1702,6 @@ void ctf_msg_iter_reset_for_next_stream_file(struct ctf_msg_iter *msg_it)
 /**
  * Resets the internal state of a CTF message iterator.
  */
-BT_HIDDEN
 void ctf_msg_iter_reset(struct ctf_msg_iter *msg_it)
 {
     ctf_msg_iter_reset_for_next_stream_file(msg_it);
@@ -1795,6 +1793,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)
 {
@@ -1803,13 +1815,14 @@ static enum bt_bfcr_status bfcr_unsigned_int_cb(uint64_t value, struct ctf_field
     enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
 
     bt_field *field = NULL;
-    ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
 
     BT_COMP_LOGT("Unsigned integer function called from BFCR: "
                  "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
                  "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
                  msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir, value);
 
+    ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
+
     if (G_LIKELY(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE)) {
         goto update_def_clock;
     }
@@ -1863,7 +1876,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)) {
@@ -1890,13 +1904,14 @@ static enum bt_bfcr_status bfcr_unsigned_int_char_cb(uint64_t value, struct ctf_
     bt_self_component *self_comp = msg_it->self_comp;
     enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
     bt_field *string_field = NULL;
-    ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
     char str[2] = {'\0', '\0'};
 
     BT_COMP_LOGT("Unsigned integer character function called from BFCR: "
                  "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
                  "fc-type=%d, fc-in-ir=%d, value=%" PRIu64,
                  msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir, value);
+
+    ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
     BT_ASSERT_DBG(int_fc->meaning == CTF_FIELD_CLASS_MEANING_NONE);
     BT_ASSERT_DBG(!int_fc->mapped_clock_class);
     BT_ASSERT_DBG(int_fc->storing_index < 0);
@@ -1938,16 +1953,18 @@ static enum bt_bfcr_status bfcr_signed_int_cb(int64_t value, struct ctf_field_cl
     enum bt_bfcr_status status = BT_BFCR_STATUS_OK;
     bt_field *field = NULL;
     ctf_msg_iter *msg_it = (ctf_msg_iter *) data;
-    ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
 
     BT_COMP_LOGT("Signed integer function called from BFCR: "
                  "msg-it-addr=%p, bfcr-addr=%p, fc-addr=%p, "
                  "fc-type=%d, fc-in-ir=%d, value=%" PRId64,
                  msg_it, msg_it->bfcr, fc, fc->type, fc->in_ir, value);
+
+    ctf_field_class_int *int_fc = ctf_field_class_as_int(fc);
     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;
     }
 
@@ -2190,7 +2207,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;
@@ -2241,7 +2259,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.
@@ -2576,7 +2594,6 @@ end:
     return msg;
 }
 
-BT_HIDDEN
 struct ctf_msg_iter *ctf_msg_iter_create(struct ctf_trace_class *tc, size_t max_request_sz,
                                          struct ctf_msg_iter_medium_ops medops, void *data,
                                          bt_logging_level log_level, bt_self_component *self_comp,
@@ -2902,7 +2919,6 @@ end:
     return status;
 }
 
-BT_HIDDEN
 enum ctf_msg_iter_status ctf_msg_iter_seek(struct ctf_msg_iter *msg_it, off_t offset)
 {
     enum ctf_msg_iter_status status = CTF_MSG_ITER_STATUS_OK;
@@ -2948,7 +2964,6 @@ end:
     return status;
 }
 
-BT_HIDDEN
 enum ctf_msg_iter_status
 ctf_msg_iter_curr_packet_first_event_clock_snapshot(struct ctf_msg_iter *msg_it,
                                                     uint64_t *first_clock_snapshot)
@@ -2957,7 +2972,6 @@ ctf_msg_iter_curr_packet_first_event_clock_snapshot(struct ctf_msg_iter *msg_it,
                                             first_clock_snapshot);
 }
 
-BT_HIDDEN
 enum ctf_msg_iter_status
 ctf_msg_iter_curr_packet_last_event_clock_snapshot(struct ctf_msg_iter *msg_it,
                                                    uint64_t *last_clock_snapshot)
@@ -2966,7 +2980,6 @@ ctf_msg_iter_curr_packet_last_event_clock_snapshot(struct ctf_msg_iter *msg_it,
                                             STATE_EMIT_MSG_PACKET_END_MULTI, last_clock_snapshot);
 }
 
-BT_HIDDEN
 enum ctf_msg_iter_status
 ctf_msg_iter_get_packet_properties(struct ctf_msg_iter *msg_it,
                                    struct ctf_msg_iter_packet_properties *props)
@@ -2993,7 +3006,6 @@ end:
     return status;
 }
 
-BT_HIDDEN
 void ctf_msg_iter_set_dry_run(struct ctf_msg_iter *msg_it, bool val)
 {
     msg_it->dry_run = val;
This page took 0.025701 seconds and 4 git commands to generate.