Handle negative time and offset from Epoch
[babeltrace.git] / formats / ctf / ir / clock.c
index 195f83716bf3840c79f98fa15861a1783d80c34b..bf2fbda5b2627b8cffb9584650ff1a81921b2afa 100644 (file)
  */
 
 #include <babeltrace/ctf-ir/clock-internal.h>
+#include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ref.h>
 #include <babeltrace/ctf-writer/writer-internal.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);
 
 BT_HIDDEN
 struct bt_ctf_clock *_bt_ctf_clock_create(void)
@@ -46,7 +49,7 @@ struct bt_ctf_clock *_bt_ctf_clock_create(void)
 
        clock->precision = 1;
        clock->frequency = 1000000000;
-       bt_ctf_ref_init(&clock->ref_count);
+       bt_object_init(clock, bt_ctf_clock_destroy);
 end:
        return clock;
 }
@@ -57,20 +60,21 @@ int bt_ctf_clock_set_name(struct bt_ctf_clock *clock,
 {
        int ret = 0;
 
-       if (validate_identifier(name)) {
+       if (bt_ctf_validate_identifier(name)) {
                ret = -1;
                goto end;
        }
 
        if (clock->name) {
-               g_string_free(clock->name, TRUE);
+               g_string_assign(clock->name, name);
+       } else {
+               clock->name = g_string_new(name);
+               if (!clock->name) {
+                       ret = -1;
+                       goto end;
+               }
        }
 
-       clock->name = g_string_new(name);
-       if (!clock->name) {
-               ret = -1;
-               goto end;
-       }
 end:
        return ret;
 }
@@ -87,20 +91,19 @@ struct bt_ctf_clock *bt_ctf_clock_create(const char *name)
 
        ret = bt_ctf_clock_set_name(clock, name);
        if (ret) {
-               goto error_destroy;
+               goto error;
        }
 
-       ret = babeltrace_uuid_generate(clock->uuid);
+       ret = bt_uuid_generate(clock->uuid);
        if (ret) {
-               goto error_destroy;
+               goto error;
        }
 
        clock->uuid_set = 1;
        return clock;
-error_destroy:
-       bt_ctf_clock_destroy(&clock->ref_count);
 error:
-       return NULL;
+       BT_PUT(clock);
+       return clock;
 }
 
 const char *bt_ctf_clock_get_name(struct bt_ctf_clock *clock)
@@ -203,20 +206,21 @@ end:
        return ret;
 }
 
-uint64_t bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock)
+int bt_ctf_clock_get_offset_s(struct bt_ctf_clock *clock, int64_t *offset_s)
 {
-       uint64_t ret = -1ULL;
+       int ret = 0;
 
-       if (!clock) {
+       if (!clock || !offset_s) {
+               ret = -1;
                goto end;
        }
 
-       ret = clock->offset_s;
+       *offset_s = clock->offset_s;
 end:
        return ret;
 }
 
-int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, uint64_t offset_s)
+int bt_ctf_clock_set_offset_s(struct bt_ctf_clock *clock, int64_t offset_s)
 {
        int ret = 0;
 
@@ -230,20 +234,21 @@ end:
        return ret;
 }
 
-uint64_t bt_ctf_clock_get_offset(struct bt_ctf_clock *clock)
+int bt_ctf_clock_get_offset(struct bt_ctf_clock *clock, int64_t *offset)
 {
-       uint64_t ret = -1ULL;
+       int ret = 0;
 
-       if (!clock) {
+       if (!clock || !offset) {
+               ret = -1;
                goto end;
        }
 
-       ret = clock->offset;
+       *offset = clock->offset;
 end:
        return ret;
 }
 
-int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, uint64_t offset)
+int bt_ctf_clock_set_offset(struct bt_ctf_clock *clock, int64_t offset)
 {
        int ret = 0;
 
@@ -313,20 +318,21 @@ end:
        return ret;
 }
 
-uint64_t bt_ctf_clock_get_time(struct bt_ctf_clock *clock)
+int bt_ctf_clock_get_time(struct bt_ctf_clock *clock, int64_t *time)
 {
-       uint64_t ret = -1ULL;
+       int ret = 0;
 
-       if (!clock) {
+       if (!clock || !time) {
+               ret = -1;
                goto end;
        }
 
-       ret = clock->time;
+       *time = clock->time;
 end:
        return ret;
 }
 
-int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, uint64_t time)
+int bt_ctf_clock_set_time(struct bt_ctf_clock *clock, int64_t time)
 {
        int ret = 0;
 
@@ -343,20 +349,12 @@ end:
 
 void bt_ctf_clock_get(struct bt_ctf_clock *clock)
 {
-       if (!clock) {
-               return;
-       }
-
-       bt_ctf_ref_get(&clock->ref_count);
+       bt_get(clock);
 }
 
 void bt_ctf_clock_put(struct bt_ctf_clock *clock)
 {
-       if (!clock) {
-               return;
-       }
-
-       bt_ctf_ref_put(&clock->ref_count, bt_ctf_clock_destroy);
+       bt_put(clock);
 }
 
 BT_HIDDEN
@@ -389,7 +387,7 @@ void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
                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) {
+       if (clock->description) {
                g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
                        clock->description->str);
        }
@@ -408,15 +406,11 @@ void bt_ctf_clock_serialize(struct bt_ctf_clock *clock,
 }
 
 static
-void bt_ctf_clock_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_clock_destroy(struct bt_object *obj)
 {
        struct bt_ctf_clock *clock;
 
-       if (!ref) {
-               return;
-       }
-
-       clock = container_of(ref, struct bt_ctf_clock, ref_count);
+       clock = container_of(obj, struct bt_ctf_clock, base);
        if (clock->name) {
                g_string_free(clock->name, TRUE);
        }
This page took 0.026866 seconds and 4 git commands to generate.