Unify reference counting using a common bt_object base
[babeltrace.git] / formats / ctf / ir / trace.c
index 158f2b8bbc2e6d13ea8555ecb95a8cff0011d306..f96ce87cd95adbaf268cf92bf1bf41ec54d4b25f 100644 (file)
 #include <babeltrace/ctf-ir/visitor-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/compiler.h>
-#include <babeltrace/objects.h>
+#include <babeltrace/values.h>
+#include <babeltrace/ref.h>
 
 #define DEFAULT_IDENTIFIER_SIZE 128
 #define DEFAULT_METADATA_STRING_SIZE 4096
 
 static
-void bt_ctf_trace_destroy(struct bt_ctf_ref *ref);
+void bt_ctf_trace_destroy(struct bt_object *obj);
 static
 int init_trace_packet_header(struct bt_ctf_trace *trace);
 static
@@ -70,7 +71,7 @@ static
 void put_stream_class(struct bt_ctf_stream_class *stream_class)
 {
        (void) bt_ctf_stream_class_set_trace(stream_class, NULL);
-       bt_ctf_stream_class_put(stream_class);
+       bt_put(stream_class);
 }
 
 struct bt_ctf_trace *bt_ctf_trace_create(void)
@@ -83,47 +84,41 @@ struct bt_ctf_trace *bt_ctf_trace_create(void)
        }
 
        bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE);
-       bt_ctf_ref_init(&trace->ref_count);
+       bt_object_init(trace, bt_ctf_trace_destroy);
        trace->clocks = g_ptr_array_new_with_free_func(
-               (GDestroyNotify) bt_ctf_clock_put);
+               (GDestroyNotify) bt_put);
        trace->streams = g_ptr_array_new_with_free_func(
-               (GDestroyNotify) bt_ctf_stream_put);
+               (GDestroyNotify) bt_put);
        trace->stream_classes = g_ptr_array_new_with_free_func(
                (GDestroyNotify) put_stream_class);
        if (!trace->clocks || !trace->stream_classes || !trace->streams) {
-               goto error_destroy;
+               goto error;
        }
 
        /* Generate a trace UUID */
        uuid_generate(trace->uuid);
        if (init_trace_packet_header(trace)) {
-               goto error_destroy;
+               goto error;
        }
 
        /* Create the environment array object */
        trace->environment = bt_ctf_attributes_create();
        if (!trace->environment) {
-               goto error_destroy;
+               goto error;
        }
 
        return trace;
 
-error_destroy:
-       bt_ctf_trace_destroy(&trace->ref_count);
-       trace = NULL;
 error:
+       BT_PUT(trace);
        return trace;
 }
 
-void bt_ctf_trace_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_trace_destroy(struct bt_object *obj)
 {
        struct bt_ctf_trace *trace;
 
-       if (!ref) {
-               return;
-       }
-
-       trace = container_of(ref, struct bt_ctf_trace, ref_count);
+       trace = container_of(obj, struct bt_ctf_trace, base);
        if (trace->environment) {
                bt_ctf_attributes_destroy(trace->environment);
        }
@@ -140,7 +135,7 @@ void bt_ctf_trace_destroy(struct bt_ctf_ref *ref)
                g_ptr_array_free(trace->stream_classes, TRUE);
        }
 
-       bt_ctf_field_type_put(trace->packet_header_type);
+       bt_put(trace->packet_header_type);
        g_free(trace);
 }
 
@@ -156,11 +151,6 @@ struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace,
                goto error;
        }
 
-       stream = bt_ctf_stream_create(stream_class, trace);
-       if (!stream) {
-               goto error;
-       }
-
        for (i = 0; i < trace->stream_classes->len; i++) {
                if (trace->stream_classes->pdata[i] == stream_class) {
                        stream_class_found = 1;
@@ -174,23 +164,28 @@ struct bt_ctf_stream *bt_ctf_trace_create_stream(struct bt_ctf_trace *trace,
                }
        }
 
-       bt_ctf_stream_get(stream);
+       stream = bt_ctf_stream_create(stream_class, trace);
+       if (!stream) {
+               goto error;
+       }
+
+       bt_get(stream);
        g_ptr_array_add(trace->streams, stream);
 
        return stream;
 error:
-       bt_ctf_stream_put(stream);
-       return NULL;
+        BT_PUT(stream);
+       return stream;
 }
 
 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
-               const char *name, struct bt_object *value)
+               const char *name, struct bt_value *value)
 {
        int ret = 0;
 
        if (!trace || !name || !value ||
                bt_ctf_validate_identifier(name) ||
-               !(bt_object_is_integer(value) || bt_object_is_string(value))) {
+               !(bt_value_is_integer(value) || bt_value_is_string(value))) {
                ret = -1;
                goto end;
        }
@@ -207,17 +202,17 @@ int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
                 *
                 * The object passed is frozen like all other attributes.
                 */
-               struct bt_object *attribute =
+               struct bt_value *attribute =
                        bt_ctf_attributes_get_field_value_by_name(
                                trace->environment, name);
 
                if (attribute) {
-                       BT_OBJECT_PUT(attribute);
+                       BT_PUT(attribute);
                        ret = -1;
                        goto end;
                }
 
-               bt_object_freeze(value);
+               bt_value_freeze(value);
        }
 
        ret = bt_ctf_attributes_set_field_value(trace->environment, name,
@@ -231,7 +226,7 @@ int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
                const char *name, const char *value)
 {
        int ret = 0;
-       struct bt_object *env_value_string_obj = NULL;
+       struct bt_value *env_value_string_obj = NULL;
 
        if (!trace || !name || !value) {
                ret = -1;
@@ -243,18 +238,18 @@ int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
                 * New environment fields may be added to a frozen trace,
                 * but existing fields may not be changed.
                 */
-               struct bt_object *attribute =
+               struct bt_value *attribute =
                        bt_ctf_attributes_get_field_value_by_name(
                                trace->environment, name);
 
                if (attribute) {
-                       BT_OBJECT_PUT(attribute);
+                       BT_PUT(attribute);
                        ret = -1;
                        goto end;
                }
        }
 
-       env_value_string_obj = bt_object_string_create_init(value);
+       env_value_string_obj = bt_value_string_create_init(value);
 
        if (!env_value_string_obj) {
                ret = -1;
@@ -262,14 +257,13 @@ int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
        }
 
        if (trace->frozen) {
-               bt_object_freeze(env_value_string_obj);
+               bt_value_freeze(env_value_string_obj);
        }
        ret = bt_ctf_trace_set_environment_field(trace, name,
                env_value_string_obj);
 
 end:
-       BT_OBJECT_PUT(env_value_string_obj);
-
+       BT_PUT(env_value_string_obj);
        return ret;
 }
 
@@ -277,7 +271,7 @@ int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
                const char *name, int64_t value)
 {
        int ret = 0;
-       struct bt_object *env_value_integer_obj = NULL;
+       struct bt_value *env_value_integer_obj = NULL;
 
        if (!trace || !name) {
                ret = -1;
@@ -289,18 +283,18 @@ int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
                 * New environment fields may be added to a frozen trace,
                 * but existing fields may not be changed.
                 */
-               struct bt_object *attribute =
+               struct bt_value *attribute =
                        bt_ctf_attributes_get_field_value_by_name(
                                trace->environment, name);
 
                if (attribute) {
-                       BT_OBJECT_PUT(attribute);
+                       BT_PUT(attribute);
                        ret = -1;
                        goto end;
                }
        }
 
-       env_value_integer_obj = bt_object_integer_create_init(value);
+       env_value_integer_obj = bt_value_integer_create_init(value);
        if (!env_value_integer_obj) {
                ret = -1;
                goto end;
@@ -309,11 +303,10 @@ int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
        ret = bt_ctf_trace_set_environment_field(trace, name,
                env_value_integer_obj);
        if (trace->frozen) {
-               bt_object_freeze(env_value_integer_obj);
+               bt_value_freeze(env_value_integer_obj);
        }
 end:
-       BT_OBJECT_PUT(env_value_integer_obj);
-
+       BT_PUT(env_value_integer_obj);
        return ret;
 }
 
@@ -348,10 +341,10 @@ end:
        return ret;
 }
 
-struct bt_object *bt_ctf_trace_get_environment_field_value(
+struct bt_value *bt_ctf_trace_get_environment_field_value(
                struct bt_ctf_trace *trace, int index)
 {
-       struct bt_object *ret = NULL;
+       struct bt_value *ret = NULL;
 
        if (!trace) {
                goto end;
@@ -363,10 +356,10 @@ end:
        return ret;
 }
 
-struct bt_object *bt_ctf_trace_get_environment_field_value_by_name(
+struct bt_value *bt_ctf_trace_get_environment_field_value_by_name(
                struct bt_ctf_trace *trace, const char *name)
 {
-       struct bt_object *ret = NULL;
+       struct bt_value *ret = NULL;
 
        if (!trace || !name) {
                goto end;
@@ -397,7 +390,7 @@ int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace,
                goto end;
        }
 
-       bt_ctf_clock_get(clock);
+       bt_get(clock);
        g_ptr_array_add(trace->clocks, clock);
 end:
        return ret;
@@ -426,7 +419,7 @@ struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace,
        }
 
        clock = g_ptr_array_index(trace->clocks, index);
-       bt_ctf_clock_get(clock);
+       bt_get(clock);
 end:
        return clock;
 }
@@ -484,7 +477,7 @@ int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
                goto end;
        }
 
-       bt_ctf_stream_class_get(stream_class);
+       bt_get(stream_class);
        g_ptr_array_add(trace->stream_classes, stream_class);
 
        /*
@@ -538,7 +531,35 @@ struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
        }
 
        stream_class = g_ptr_array_index(trace->stream_classes, index);
-       bt_ctf_stream_class_get(stream_class);
+       bt_get(stream_class);
+end:
+       return stream_class;
+}
+
+struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
+               struct bt_ctf_trace *trace, uint32_t id)
+{
+       int i;
+       struct bt_ctf_stream_class *stream_class = NULL;
+
+       if (!trace) {
+               goto end;
+       }
+
+       for (i = 0; i < trace->stream_classes->len; ++i) {
+               struct bt_ctf_stream_class *stream_class_candidate;
+
+               stream_class_candidate =
+                       g_ptr_array_index(trace->stream_classes, i);
+
+               if (bt_ctf_stream_class_get_id(stream_class_candidate) ==
+                               (int64_t) id) {
+                       stream_class = stream_class_candidate;
+                       bt_get(stream_class);
+                       goto end;
+               }
+       }
+
 end:
        return stream_class;
 }
@@ -564,7 +585,7 @@ struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name(
 
                if (!strcmp(cur_clk_name, name)) {
                        clock = cur_clk;
-                       bt_ctf_clock_get(clock);
+                       bt_get(clock);
                        goto end;
                }
        }
@@ -645,7 +666,7 @@ void append_env_metadata(struct bt_ctf_trace *trace,
        g_string_append(context->string, "env {\n");
 
        for (i = 0; i < env_size; ++i) {
-               struct bt_object *env_field_value_obj = NULL;
+               struct bt_value *env_field_value_obj = NULL;
                const char *entry_name;
 
                entry_name = bt_ctf_attributes_get_field_name(
@@ -657,13 +678,13 @@ void append_env_metadata(struct bt_ctf_trace *trace,
                        goto loop_next;
                }
 
-               switch (bt_object_get_type(env_field_value_obj)) {
-               case BT_OBJECT_TYPE_INTEGER:
+               switch (bt_value_get_type(env_field_value_obj)) {
+               case BT_VALUE_TYPE_INTEGER:
                {
                        int ret;
                        int64_t int_value;
 
-                       ret = bt_object_integer_get(env_field_value_obj,
+                       ret = bt_value_integer_get(env_field_value_obj,
                                &int_value);
 
                        if (ret) {
@@ -675,13 +696,13 @@ void append_env_metadata(struct bt_ctf_trace *trace,
                                int_value);
                        break;
                }
-               case BT_OBJECT_TYPE_STRING:
+               case BT_VALUE_TYPE_STRING:
                {
                        int ret;
                        const char *str_value;
                        char *escaped_str = NULL;
 
-                       ret = bt_object_string_get(env_field_value_obj,
+                       ret = bt_value_string_get(env_field_value_obj,
                                &str_value);
 
                        if (ret) {
@@ -705,7 +726,7 @@ void append_env_metadata(struct bt_ctf_trace *trace,
                }
 
 loop_next:
-               BT_OBJECT_PUT(env_field_value_obj);
+               BT_PUT(env_field_value_obj);
        }
 
        g_string_append(context->string, "};\n\n");
@@ -827,7 +848,7 @@ struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
                goto end;
        }
 
-       bt_ctf_field_type_get(trace->packet_header_type);
+       bt_get(trace->packet_header_type);
        field_type = trace->packet_header_type;
 end:
        return field_type;
@@ -850,8 +871,8 @@ int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
                goto end;
        }
 
-       bt_ctf_field_type_get(packet_header_type);
-       bt_ctf_field_type_put(trace->packet_header_type);
+       bt_get(packet_header_type);
+       bt_put(trace->packet_header_type);
        trace->packet_header_type = packet_header_type;
 end:
        return ret;
@@ -859,20 +880,13 @@ end:
 
 void bt_ctf_trace_get(struct bt_ctf_trace *trace)
 {
-       if (!trace) {
-               return;
-       }
-
-       bt_ctf_ref_get(&trace->ref_count);
+       bt_get(trace);
 }
 
 void bt_ctf_trace_put(struct bt_ctf_trace *trace)
 {
-       if (!trace) {
-               return;
-       }
+       bt_put(trace);
 
-       bt_ctf_ref_put(&trace->ref_count, bt_ctf_trace_destroy);
 }
 
 BT_HIDDEN
@@ -952,12 +966,11 @@ int init_trace_packet_header(struct bt_ctf_trace *trace)
                goto end;
        }
 end:
-       bt_ctf_field_type_put(uuid_array_type);
-       bt_ctf_field_type_put(_uint32_t);
-       bt_ctf_field_type_put(_uint8_t);
-       bt_ctf_field_put(magic);
-       bt_ctf_field_put(uuid_array);
-       bt_ctf_field_type_put(trace_packet_header_type);
-
+       bt_put(uuid_array_type);
+       bt_put(_uint32_t);
+       bt_put(_uint8_t);
+       bt_put(magic);
+       bt_put(uuid_array);
+       bt_put(trace_packet_header_type);
        return ret;
 }
This page took 0.031013 seconds and 4 git commands to generate.