+static
+void update_clock_state(uint64_t *state,
+ struct bt_ctf_field *value_field)
+{
+ struct bt_ctf_field_type *value_type = NULL;
+ uint64_t requested_new_value;
+ uint64_t requested_new_value_mask;
+ uint64_t cur_value_masked;
+ int requested_new_value_size;
+ int ret;
+
+ value_type = bt_ctf_field_get_type(value_field);
+ assert(value_type);
+
+ requested_new_value_size =
+ bt_ctf_field_type_integer_get_size(value_type);
+ assert(requested_new_value_size > 0);
+
+ ret = bt_ctf_field_unsigned_integer_get_value(value_field,
+ &requested_new_value);
+ assert(!ret);
+
+ /*
+ * Special case for a 64-bit new value, which is the limit
+ * of a clock value as of this version: overwrite the
+ * current value directly.
+ */
+ if (requested_new_value_size == 64) {
+ *state = requested_new_value;
+ goto end;
+ }
+
+ requested_new_value_mask = (1ULL << requested_new_value_size) - 1;
+ cur_value_masked = *state & requested_new_value_mask;
+
+ if (requested_new_value < cur_value_masked) {
+ /*
+ * It looks like a wrap happened on the number of bits
+ * of the requested new value. Assume that the clock
+ * value wrapped only one time.
+ */
+ *state += requested_new_value_mask + 1;
+ }
+
+ /* Clear the low bits of the current clock value. */
+ *state &= ~requested_new_value_mask;
+
+ /* Set the low bits of the current clock value. */
+ *state |= requested_new_value;
+end:
+ bt_put(value_type);
+}
+
+static
+enum bt_ctf_btr_status update_clock(struct bt_ctf_notif_iter *notit,
+ struct bt_ctf_field_type *int_field_type,
+ struct bt_ctf_field *int_field)
+{
+ gboolean clock_found;
+ uint64_t *clock_state;
+ enum bt_ctf_btr_status ret = BT_CTF_BTR_STATUS_OK;
+ struct bt_ctf_clock *clock = bt_ctf_field_type_integer_get_mapped_clock(
+ int_field_type);
+
+ if (likely(!clock)) {
+ goto end_no_clock;
+ }
+
+ clock_found = g_hash_table_lookup_extended(notit->clock_states,
+ clock, NULL, (gpointer) &clock_state);
+ if (unlikely(!clock_found)) {
+ const char *clock_name = bt_ctf_clock_get_name(clock);
+
+ PERR("Unknown clock %s mapped to integer encountered in stream\n",
+ clock_name ? : "NULL");
+ ret = BT_CTF_BTR_STATUS_ERROR;
+ goto end;
+ }
+
+ if (unlikely(!clock_state)) {
+ clock_state = g_new0(uint64_t, 1);
+ if (!clock_state) {
+ ret = BT_CTF_BTR_STATUS_ENOMEM;
+ goto end;
+ }
+ g_hash_table_insert(notit->clock_states, bt_get(clock),
+ clock_state);
+ }
+
+ /* Update the clock's state. */
+ update_clock_state(clock_state, int_field);
+end:
+ bt_put(clock);
+end_no_clock:
+ return ret;
+}
+