ir: consolidate reference counting functions
[babeltrace.git] / formats / ctf / ir / event.c
index d8def6252464919cea9790dcc4972bc33b1e4138..6cb9f2d5c33685b3d08c90fe12b299bcf2d44d4a 100644 (file)
 #include <babeltrace/ctf-ir/stream-class-internal.h>
 #include <babeltrace/ctf-ir/trace-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
+#include <babeltrace/ctf-ir/common-internal.h>
+#include <babeltrace/ctf-ir/ref.h>
+#include <babeltrace/ctf-ir/attributes-internal.h>
 #include <babeltrace/compiler.h>
 
 static
-void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_event_class_destroy(struct bt_ref *ref);
 static
-void bt_ctf_event_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_event_destroy(struct bt_ref *ref);
 static
 int set_integer_field_value(struct bt_ctf_field *field, uint64_t value);
 
@@ -60,7 +63,7 @@ struct bt_ctf_event_class *bt_ctf_event_class_create(const char *name)
                goto error;
        }
 
-       bt_ctf_ref_init(&event_class->ref_count);
+       bt_ctf_base_init(event_class, bt_ctf_event_class_destroy);
        event_class->fields = bt_ctf_field_type_structure_create();
        if (!event_class->fields) {
                goto error;
@@ -137,7 +140,7 @@ end:
 int64_t bt_ctf_event_class_get_id(struct bt_ctf_event_class *event_class)
 {
        struct bt_object *obj = NULL;
-       int64_t ret;
+       int64_t ret = 0;
 
        if (!event_class) {
                ret = -1;
@@ -509,26 +512,39 @@ end:
 
 void bt_ctf_event_class_get(struct bt_ctf_event_class *event_class)
 {
-       if (!event_class) {
-               return;
-       }
-
-       bt_ctf_ref_get(&event_class->ref_count);
+       bt_ctf_get(event_class);
 }
 
 void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class)
 {
-       if (!event_class) {
-               return;
+       bt_ctf_put(event_class);
+}
+
+BT_HIDDEN
+int bt_ctf_event_class_set_stream_id(struct bt_ctf_event_class *event_class,
+               uint32_t stream_id)
+{
+       int ret = 0;
+       struct bt_object *obj;
+
+       obj = bt_object_integer_create_init(stream_id);
+
+       if (!obj) {
+               ret = -1;
+               goto end;
        }
 
-       bt_ctf_ref_put(&event_class->ref_count, bt_ctf_event_class_destroy);
+       ret = bt_ctf_attributes_set_field_value(event_class->attributes,
+               "stream_id", obj);
+
+end:
+       BT_OBJECT_PUT(obj);
+
+       return ret;
 }
 
 struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
 {
-       int ret;
-       struct bt_object *obj = NULL;
        struct bt_ctf_event *event = NULL;
 
        if (!event_class) {
@@ -545,27 +561,12 @@ struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class)
                goto end;
        }
        assert(event_class->stream_class->event_header_type);
-
-       /* set "stream_id" attribute now that we know its value */
-       obj = bt_object_integer_create_init(event_class->stream_class->id);
-       if (!obj) {
-               goto end;
-       }
-
-       ret = bt_ctf_attributes_set_field_value(event_class->attributes,
-               "stream_id", obj);
-       BT_OBJECT_PUT(obj);
-
-       if (ret) {
-               goto end;
-       }
-
        event = g_new0(struct bt_ctf_event, 1);
        if (!event) {
                goto end;
        }
 
-       bt_ctf_ref_init(&event->ref_count);
+       bt_ctf_base_init(event, bt_ctf_event_destroy);
        bt_ctf_event_class_get(event_class);
        bt_ctf_event_class_freeze(event_class);
        event->event_class = event_class;
@@ -596,7 +597,7 @@ end:
        return event;
 error_destroy:
        if (event) {
-               bt_ctf_event_destroy(&event->ref_count);
+               bt_ctf_event_destroy(&event->base.ref_count);
        }
 
        return NULL;
@@ -616,6 +617,22 @@ end:
        return event_class;
 }
 
+struct bt_ctf_stream *bt_ctf_event_get_stream(struct bt_ctf_event *event)
+{
+       struct bt_ctf_stream *stream = NULL;
+
+       if (!event) {
+               goto end;
+       }
+
+       stream = event->stream;
+       if (stream) {
+               bt_ctf_stream_get(stream);
+       }
+end:
+       return stream;
+}
+
 struct bt_ctf_clock *bt_ctf_event_get_clock(struct bt_ctf_event *event)
 {
        struct bt_ctf_clock *clock = NULL;
@@ -681,6 +698,54 @@ end:
        return ret;
 }
 
+struct bt_ctf_field *bt_ctf_event_get_payload_field(struct bt_ctf_event *event)
+{
+       struct bt_ctf_field *payload = NULL;
+
+       if (!event || !event->fields_payload) {
+               goto end;
+       }
+
+       payload = event->fields_payload;
+       bt_ctf_field_get(payload);
+end:
+       return payload;
+}
+
+int bt_ctf_event_set_payload_field(struct bt_ctf_event *event,
+               struct bt_ctf_field *payload)
+{
+       int ret = 0;
+       struct bt_ctf_field_type *payload_type = NULL;
+
+       if (!event || !payload) {
+               ret = -1;
+               goto end;
+       }
+
+       payload_type = bt_ctf_field_get_type(payload);
+       if (!payload_type) {
+               ret = -1;
+               goto end;
+       }
+
+       if (bt_ctf_field_type_get_type_id(payload_type) != CTF_TYPE_STRUCT) {
+               ret = -1;
+               goto end;
+       }
+
+       bt_ctf_field_get(payload);
+       if (event->fields_payload) {
+               bt_ctf_field_put(event->fields_payload);
+       }
+       event->fields_payload = payload;
+
+end:
+       if (payload_type) {
+               bt_ctf_field_type_put(payload_type);
+       }
+       return ret;
+}
 
 struct bt_ctf_field *bt_ctf_event_get_payload(struct bt_ctf_event *event,
                const char *name)
@@ -717,7 +782,7 @@ end:
        return field;
 }
 
-struct bt_ctf_field *bt_ctf_event_get_event_header(
+struct bt_ctf_field *bt_ctf_event_get_header(
                struct bt_ctf_event *event)
 {
        struct bt_ctf_field *header = NULL;
@@ -732,7 +797,7 @@ end:
        return header;
 }
 
-int bt_ctf_event_set_event_header(struct bt_ctf_event *event,
+int bt_ctf_event_set_header(struct bt_ctf_event *event,
                struct bt_ctf_field *header)
 {
        int ret = 0;
@@ -813,26 +878,19 @@ end:
 
 void bt_ctf_event_get(struct bt_ctf_event *event)
 {
-       if (!event) {
-               return;
-       }
-
-       bt_ctf_ref_get(&event->ref_count);
+       bt_ctf_get(event);
 }
 
 void bt_ctf_event_put(struct bt_ctf_event *event)
 {
-       if (!event) {
-               return;
-       }
-
-       bt_ctf_ref_put(&event->ref_count, bt_ctf_event_destroy);
+       bt_ctf_put(event);
 }
 
 static
-void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_event_class_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_event_class *event_class;
+       struct bt_ctf_base *base;
 
        if (!ref) {
                return;
@@ -842,7 +900,8 @@ void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref)
         * Don't call put() on the stream class. See comment in
         * bt_ctf_event_class_set_stream_class for explanation.
         */
-       event_class = container_of(ref, struct bt_ctf_event_class, ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       event_class = container_of(base, struct bt_ctf_event_class, base);
        if (event_class->attributes) {
                bt_ctf_attributes_destroy(event_class->attributes);
        }
@@ -856,16 +915,17 @@ void bt_ctf_event_class_destroy(struct bt_ctf_ref *ref)
 }
 
 static
-void bt_ctf_event_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_event_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_event *event;
+       struct bt_ctf_base *base;
 
        if (!ref) {
                return;
        }
 
-       event = container_of(ref, struct bt_ctf_event,
-               ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       event = container_of(base, struct bt_ctf_event, base);
        if (event->event_class) {
                bt_ctf_event_class_put(event->event_class);
        }
@@ -1185,3 +1245,93 @@ end:
        }
        return ret;
 }
+
+BT_HIDDEN
+int bt_ctf_event_set_stream(struct bt_ctf_event *event,
+               struct bt_ctf_stream *stream)
+{
+       int ret = 0;
+
+       if (!event) {
+               ret = -1;
+               goto end;
+       }
+
+       if (event->stream && stream) {
+               /* Already attached to a stream */
+               ret = -1;
+               goto end;
+       }
+
+       event->stream = stream;
+end:
+       return ret;
+}
+
+struct bt_ctf_event *bt_ctf_event_copy(struct bt_ctf_event *event)
+{
+       struct bt_ctf_event *copy = NULL;
+
+       if (!event) {
+               goto error;
+       }
+
+       copy = g_new0(struct bt_ctf_event, 1);
+       if (!copy) {
+               goto error;
+       }
+
+       bt_ctf_base_init(copy, bt_ctf_event_destroy);
+       copy->event_class = event->event_class;
+       bt_ctf_event_class_get(copy->event_class);
+       copy->stream = event->stream;
+
+       if (event->event_header) {
+               copy->event_header = bt_ctf_field_copy(event->event_header);
+
+               if (!copy->event_header) {
+                       goto error;
+               }
+       }
+
+       if (event->context_payload) {
+               copy->context_payload = bt_ctf_field_copy(
+                       event->context_payload);
+
+               if (!copy->context_payload) {
+                       goto error;
+               }
+       }
+
+       if (event->fields_payload) {
+               copy->fields_payload = bt_ctf_field_copy(event->fields_payload);
+
+               if (!copy->fields_payload) {
+                       goto error;
+               }
+       }
+
+       return copy;
+
+error:
+       if (copy) {
+               if (copy->event_class) {
+                       bt_ctf_event_class_put(copy->event_class);
+               }
+
+               if (copy->event_header) {
+                       bt_ctf_field_put(copy->event_header);
+               }
+
+               if (copy->context_payload) {
+                       bt_ctf_field_put(copy->context_payload);
+               }
+
+               if (copy->fields_payload) {
+                       bt_ctf_field_put(copy->fields_payload);
+               }
+       }
+
+       g_free(copy);
+       return NULL;
+}
This page took 0.026635 seconds and 4 git commands to generate.