- value = container_of(obj, struct bt_clock_value, base);
- BT_LOGD("Destroying clock value: addr=%p, clock-class-addr=%p, "
- "clock-class-name=\"%s\"", obj, value->clock_class,
- bt_clock_class_get_name(value->clock_class));
- bt_put(value->clock_class);
- g_free(value);
-}
-
-static
-void set_ns_from_epoch(struct bt_clock_value *clock_value)
-{
- struct bt_clock_class *clock_class = clock_value->clock_class;
- int64_t diff;
- int64_t s_ns;
- uint64_t u_ns;
- uint64_t cycles;
-
- /* Initialize nanosecond timestamp to clock's offset in seconds */
- if (clock_class->offset_s <= (INT64_MIN / 1000000000) ||
- clock_class->offset_s >= (INT64_MAX / 1000000000)) {
- /*
- * Overflow: offset in seconds converted to nanoseconds
- * is outside the int64_t range.
- */
- clock_value->ns_from_epoch_overflows = true;
- goto end;
- }
-
- clock_value->ns_from_epoch = clock_class->offset_s * (int64_t) 1000000000;
-
- /* Add offset in cycles */
- if (clock_class->offset < 0) {
- cycles = (uint64_t) (-clock_class->offset);
- } else {
- cycles = (uint64_t) clock_class->offset;
- }
-
- u_ns = ns_from_value(clock_class->frequency, cycles);
-
- if (u_ns == -1ULL || u_ns >= INT64_MAX) {
- /*
- * Overflow: offset in cycles converted to nanoseconds
- * is outside the int64_t range.
- */
- clock_value->ns_from_epoch_overflows = true;
- goto end;
- }
-
- s_ns = (int64_t) u_ns;
- assert(s_ns >= 0);
-
- if (clock_class->offset < 0) {
- if (clock_value->ns_from_epoch >= 0) {
- /*
- * Offset in cycles is negative so it must also
- * be negative once converted to nanoseconds.
- */
- s_ns = -s_ns;
- goto offset_ok;
- }
-
- diff = clock_value->ns_from_epoch - INT64_MIN;
-
- if (s_ns >= diff) {
- /*
- * Overflow: current timestamp in nanoseconds
- * plus the offset in cycles converted to
- * nanoseconds is outside the int64_t range.
- */
- clock_value->ns_from_epoch_overflows = true;
- goto end;
- }
-
- /*
- * Offset in cycles is negative so it must also be
- * negative once converted to nanoseconds.
- */
- s_ns = -s_ns;
- } else {
- if (clock_value->ns_from_epoch <= 0) {
- goto offset_ok;
- }
-
- diff = INT64_MAX - clock_value->ns_from_epoch;
-
- if (s_ns >= diff) {
- /*
- * Overflow: current timestamp in nanoseconds
- * plus the offset in cycles converted to
- * nanoseconds is outside the int64_t range.
- */
- clock_value->ns_from_epoch_overflows = true;
- goto end;
- }
- }
-
-offset_ok:
- clock_value->ns_from_epoch += s_ns;
-
- /* Add clock value (cycles) */
- u_ns = ns_from_value(clock_class->frequency, clock_value->value);
-
- if (u_ns == -1ULL || u_ns >= INT64_MAX) {
- /*
- * Overflow: value converted to nanoseconds is outside
- * the int64_t range.
- */
- clock_value->ns_from_epoch_overflows = true;
- goto end;
- }
-
- s_ns = (int64_t) u_ns;
- assert(s_ns >= 0);
-
- /* Clock value (cycles) is always positive */
- if (clock_value->ns_from_epoch <= 0) {
- goto value_ok;
- }
-
- diff = INT64_MAX - clock_value->ns_from_epoch;
-
- if (s_ns >= diff) {
- /*
- * Overflow: current timestamp in nanoseconds plus the
- * clock value converted to nanoseconds is outside the
- * int64_t range.
- */
- clock_value->ns_from_epoch_overflows = true;
- goto end;
- }
-
-value_ok:
- clock_value->ns_from_epoch += s_ns;
-
-end:
- if (clock_value->ns_from_epoch_overflows) {
- clock_value->ns_from_epoch = 0;
- }
-}
-
-struct bt_clock_value *bt_clock_value_create(
- struct bt_clock_class *clock_class, uint64_t value)
-{
- struct bt_clock_value *ret = NULL;
-
- BT_LOGD("Creating clock value object: clock-class-addr=%p, "
- "clock-class-name=\"%s\", value=%" PRIu64, clock_class,
- bt_clock_class_get_name(clock_class), value);
-
- if (!clock_class) {
- BT_LOGW_STR("Invalid parameter: clock class is NULL.");
- goto end;
- }
-
- ret = g_new0(struct bt_clock_value, 1);
- if (!ret) {
- BT_LOGE_STR("Failed to allocate one clock value.");
- goto end;
- }
-
- bt_object_init(ret, bt_clock_value_destroy);
- ret->clock_class = bt_get(clock_class);
- ret->value = value;
- set_ns_from_epoch(ret);
- bt_clock_class_freeze(clock_class);
- BT_LOGD("Created clock value object: clock-value-addr=%p, "
- "clock-class-addr=%p, clock-class-name=\"%s\", "
- "ns-from-epoch=%" PRId64 ", ns-from-epoch-overflows=%d",
- ret, clock_class, bt_clock_class_get_name(clock_class),
- ret->ns_from_epoch, ret->ns_from_epoch_overflows);
-
-end:
- return ret;