ctf.fs: bt_ctf_notif_iter_create(): assert() that all medops exist
[babeltrace.git] / formats / ctf / writer / clock.c
index 1146e005a3bcb43ea646d0165acece436a658626..84125f2229bd6e4ef6100a9f1c69a7d10f906458 100644 (file)
@@ -1,9 +1,10 @@
 /*
  * clock.c
  *
- * Babeltrace CTF Writer
+ * Babeltrace CTF IR - Clock
  *
- * Copyright 2013 EfficiOS Inc.
+ * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
  *
  * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
  * SOFTWARE.
  */
 
-#include <babeltrace/ctf-writer/clock.h>
 #include <babeltrace/ctf-writer/clock-internal.h>
-#include <babeltrace/ctf-writer/writer-internal.h>
+#include <babeltrace/ctf-ir/clock-class.h>
+#include <babeltrace/ctf-ir/clock-class-internal.h>
+#include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ref.h>
+#include <babeltrace/object-internal.h>
 #include <babeltrace/compiler.h>
 #include <inttypes.h>
 
 static
-void bt_ctf_clock_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_clock_destroy(struct bt_object *obj);
 
 struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
 {
        struct bt_ctf_clock *clock = NULL;
 
-       if (validate_identifier(name)) {
+       if (!name) {
                goto error;
        }
 
        clock = g_new0(struct bt_ctf_clock, 1);
+
        if (!clock) {
                goto error;
        }
 
-       clock->name = g_string_new(name);
-       if (!clock->name) {
-               goto error_destroy;
-       }
-
-       clock->description = g_string_new(NULL);
-       if (!clock->description) {
-               goto error_destroy;
+       bt_object_init(clock, bt_ctf_clock_destroy);
+       clock->value = 0;
+       clock->clock_class = bt_ctf_clock_class_create(name);
+       if (!clock->clock_class) {
+               goto error;
        }
-
-       clock->precision = 1;
-       clock->frequency = 1000000000;
-       uuid_generate(clock->uuid);
-       bt_ctf_ref_init(&clock->ref_count);
        return clock;
-error_destroy:
-       bt_ctf_clock_destroy(&clock->ref_count);
+
 error:
-       clock = NULL;
+       BT_PUT(clock);
        return clock;
 }
 
+const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
+{
+       const char *name = NULL;
+
+       if (clock) {
+               name = bt_ctf_clock_class_get_name(clock->clock_class);
+       }
+
+       return name;
+}
+
+const char *bt_ctf_clock_get_description(struct bt_ctf_clock *clock)
+{
+       const char *description = NULL;
+
+       if (clock) {
+               description = bt_ctf_clock_class_get_description(
+                       clock->clock_class);
+       }
+
+       return description;
+}
+
 int bt_ctf_clock_set_description(struct bt_ctf_clock *clock, const char *desc)
 {
-       int ret = 0;
+       int ret = -1;
 
-       if (!clock || !desc || clock->frozen) {
-               ret = -1;
-               goto end;
+       if (clock) {
+               ret = bt_ctf_clock_class_set_description(clock->clock_class,
+                       desc);
        }
 
-       clock->description = g_string_assign(clock->description, desc);
-       ret = clock->description ? 0 : -1;
-end:
        return ret;
 }
 
+uint64_t bt_ctf_clock_get_frequency(struct bt_ctf_clock *clock)
+{
+       uint64_t freq = -1ULL;
+
+       if (clock) {
+               freq = bt_ctf_clock_class_get_frequency(clock->clock_class);
+       }
+
+       return freq;
+}
+
 int bt_ctf_clock_set_frequency(struct bt_ctf_clock *clock, uint64_t freq)
 {
-       int ret = 0;
+       int ret = -1;
 
-       if (!clock || clock->frozen) {
-               ret = -1;
-               goto end;
+       if (clock) {
+               ret = bt_ctf_clock_class_set_frequency(clock->clock_class,
+                       freq);
        }
 
-       clock->frequency = freq;
-end:
        return ret;
 }
 
+uint64_t bt_ctf_clock_get_precision(struct bt_ctf_clock *clock)
+{
+       uint64_t precision = -1ULL;
+
+       if (clock) {
+               precision = bt_ctf_clock_class_get_precision(
+                       clock->clock_class);
+       }
+
+       return precision;
+}
+
 int bt_ctf_clock_set_precision(struct bt_ctf_clock *clock, uint64_t precision)
 {
-       int ret = 0;
+       int ret = -1;
 
-       if (!clock || clock->frozen) {
-               ret = -1;
-               goto end;
+       if (clock) {
+               ret = bt_ctf_clock_class_set_precision(clock->clock_class,
+                       precision);
        }
 
-       clock->precision = precision;
-end:
        return ret;
 }
 
-int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
+int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s)
 {
-       int ret = 0;
+       int ret = -1;
 
-       if (!clock || clock->frozen) {
-               ret = -1;
-               goto end;
+       if (clock) {
+               ret = bt_ctf_clock_class_get_offset_s(clock->clock_class,
+                       offset_s);
        }
 
-       clock->offset_s = offset_s;
-end:
        return ret;
 }
 
-int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
+int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s)
 {
-       int ret = 0;
+       int ret = -1;
 
-       if (!clock || clock->frozen) {
-               ret = -1;
-               goto end;
+       if (clock) {
+               ret = bt_ctf_clock_class_set_offset_s(clock->clock_class,
+                       offset_s);
        }
 
-       clock->offset = offset;
-end:
        return ret;
 }
 
-int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
+int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset)
 {
-       int ret = 0;
+       int ret = -1;
 
-       if (!clock || clock->frozen) {
-               ret = -1;
-               goto end;
+       if (clock) {
+               ret = bt_ctf_clock_class_get_offset_cycles(clock->clock_class,
+                       offset);
        }
 
-       clock->absolute = !!is_absolute;
-end:
        return ret;
 }
 
-int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
+int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset)
 {
-       int ret = 0;
+       int ret = -1;
 
-       /* Timestamps are strictly monotonic */
-       if (!clock || time < clock->time) {
-               ret = -1;
-               goto end;
+       if (clock) {
+               ret = bt_ctf_clock_class_set_offset_cycles(clock->clock_class,
+                       offset);
        }
 
-       clock->time = time;
-end:
        return ret;
 }
 
-void bt_ctf_clock_get(struct bt_ctf_clock *clock)
+int bt_ctf_clock_get_is_absolute(struct bt_ctf_clock *clock)
 {
-       if (!clock) {
-               return;
+       int is_absolute = -1;
+
+       if (clock) {
+               is_absolute = bt_ctf_clock_class_get_is_absolute(
+                       clock->clock_class);
        }
 
-       bt_ctf_ref_get(&clock->ref_count);
+       return is_absolute;
 }
 
-void bt_ctf_clock_put(struct bt_ctf_clock *clock)
+int bt_ctf_clock_set_is_absolute(struct bt_ctf_clock *clock, int is_absolute)
 {
-       if (!clock) {
-               return;
+       int ret = -1;
+
+       if (clock) {
+               ret = bt_ctf_clock_class_set_is_absolute(clock->clock_class,
+                       is_absolute);
        }
 
-       bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
+       return ret;
 }
 
-BT_HIDDEN
-void bt_ctf_clock_freeze(struct bt_ctf_clock *clock)
+const unsigned char *bt_ctf_clock_get_uuid(struct bt_ctf_clock *clock)
 {
-       if (!clock) {
-               return;
+       const unsigned char *uuid = NULL;
+
+       if (clock) {
+               uuid = bt_ctf_clock_class_get_uuid(clock->clock_class);
        }
 
-       clock->frozen = 1;
+       return uuid;
 }
 
-BT_HIDDEN
-void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
-               struct metadata_context *context)
+int bt_ctf_clock_set_uuid(struct bt_ctf_clock *clock, const unsigned char *uuid)
 {
-       unsigned char *uuid;
+       int ret = -1;
+
+       if (clock) {
+               ret = bt_ctf_clock_class_set_uuid(clock->clock_class, uuid);
+       }
+
+       return ret;
+}
+
+int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
+{
+       int ret = 0;
+       int64_t value;
+
+       if (!clock) {
+               ret = -1;
+               goto end;
+       }
 
-       if (!clock || !context) {
-               return;
+       /* Common case where cycles are actually nanoseconds */
+       if (clock->clock_class->frequency == 1000000000) {
+               value = time;
+       } else {
+               value = (uint64_t) (((double) time *
+                       (double) clock->clock_class->frequency) / 1e9);
        }
 
-       uuid = clock->uuid;
-       g_string_append(context->string, "clock {\n");
-       g_string_append_printf(context->string, "\tname = %s;\n",
-               clock->name->str);
-       g_string_append_printf(context->string,
-               "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
-               uuid[0], uuid[1], uuid[2], uuid[3],
-               uuid[4], uuid[5], uuid[6], uuid[7],
-               uuid[8], uuid[9], uuid[10], uuid[11],
-               uuid[12], uuid[13], uuid[14], uuid[15]);
-       if (clock->description->len) {
-               g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
-                       clock->description->str);
+       if (clock->value > value) {
+               /* Timestamps must be strictly monotonic. */
+               ret = -1;
+               goto end;
        }
 
-       g_string_append_printf(context->string, "\tfreq = %" PRIu64 ";\n",
-               clock->frequency);
-       g_string_append_printf(context->string, "\tprecision = %" PRIu64 ";\n",
-               clock->precision);
-       g_string_append_printf(context->string, "\toffset_s = %" PRIu64 ";\n",
-               clock->offset_s);
-       g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
-               clock->offset);
-       g_string_append_printf(context->string, "\tabsolute = %s;\n",
-               clock->absolute ? "TRUE" : "FALSE");
-       g_string_append(context->string, "};\n\n");
+       clock->value = value;
+end:
+       return ret;
 }
 
-BT_HIDDEN
-uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
+void bt_ctf_clock_get(struct bt_ctf_clock *clock)
 {
-       return clock ? clock->time : 0;
+       bt_get(clock);
 }
 
-static
-void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_clock_put(struct bt_ctf_clock *clock)
 {
-       struct bt_ctf_clock *clock;
+       bt_put(clock);
+}
 
-       if (!ref) {
-               return;
-       }
+BT_HIDDEN
+int bt_ctf_clock_get_value(struct bt_ctf_clock *clock, uint64_t *value)
+{
+       int ret = 0;
 
-       clock = container_of(ref, struct bt_ctf_clock, ref_count);
-       if (clock->name) {
-               g_string_free(clock->name, TRUE);
+       if (!clock || !value) {
+               ret = -1;
+               goto end;
        }
 
-       if (clock->description) {
-               g_string_free(clock->description, TRUE);
-       }
+       *value = clock->value;
+end:
+       return ret;
+}
+
+static
+void bt_ctf_clock_destroy(struct bt_object *obj)
+{
+       struct bt_ctf_clock *clock;
 
+       clock = container_of(obj, struct bt_ctf_clock, base);
+       bt_put(clock->clock_class);
        g_free(clock);
 }
This page took 0.041448 seconds and 4 git commands to generate.