ir: consolidate reference counting functions
[babeltrace.git] / formats / ctf / ir / event-fields.c
index 257ad523cc8ae20918c7d33d1b149d6b23f6042c..2fb783c31ccbd51dacd0ff027f365501c5a3acf2 100644 (file)
@@ -29,6 +29,8 @@
 #include <babeltrace/ctf-writer/event-fields.h>
 #include <babeltrace/ctf-ir/event-fields-internal.h>
 #include <babeltrace/ctf-ir/event-types-internal.h>
+#include <babeltrace/ctf-ir/common-internal.h>
+#include <babeltrace/ctf-ir/ref.h>
 #include <babeltrace/compiler.h>
 
 #define PACKET_LEN_INCREMENT   (getpagesize() * 8 * CHAR_BIT)
@@ -57,7 +59,7 @@ static
 struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *);
 
 static
-void bt_ctf_field_destroy(struct bt_ctf_ref *);
+void bt_ctf_field_destroy(struct bt_ref *);
 static
 void bt_ctf_field_integer_destroy(struct bt_ctf_field *);
 static
@@ -250,7 +252,7 @@ struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type)
        /* The type's declaration can't change after this point */
        bt_ctf_field_type_freeze(type);
        bt_ctf_field_type_get(type);
-       bt_ctf_ref_init(&field->ref_count);
+       bt_ctf_base_init(field, bt_ctf_field_destroy);
        field->type = type;
 error:
        return field;
@@ -258,16 +260,12 @@ error:
 
 void bt_ctf_field_get(struct bt_ctf_field *field)
 {
-       if (field) {
-               bt_ctf_ref_get(&field->ref_count);
-       }
+       bt_ctf_get(field);
 }
 
 void bt_ctf_field_put(struct bt_ctf_field *field)
 {
-       if (field) {
-               bt_ctf_ref_put(&field->ref_count, bt_ctf_field_destroy);
-       }
+       bt_ctf_put(field);
 }
 
 struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field)
@@ -652,6 +650,31 @@ end:
        return new_field;
 }
 
+struct bt_ctf_field *bt_ctf_field_variant_get_current_field(
+               struct bt_ctf_field *variant_field)
+{
+       struct bt_ctf_field *current_field = NULL;
+       struct bt_ctf_field_variant *variant;
+
+       if (!variant_field ||
+               bt_ctf_field_type_get_type_id(variant_field->type) !=
+                       CTF_TYPE_VARIANT) {
+               goto end;
+       }
+
+       variant = container_of(variant_field, struct bt_ctf_field_variant,
+               parent);
+
+       if (variant->payload) {
+               current_field = variant->payload;
+               bt_ctf_field_get(current_field);
+               goto end;
+       }
+
+end:
+       return current_field;
+}
+
 struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
        struct bt_ctf_field *field)
 {
@@ -1081,7 +1104,6 @@ end:
        return ret;
 }
 
-BT_HIDDEN
 struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
 {
        int ret;
@@ -1251,9 +1273,10 @@ struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type)
 }
 
 static
-void bt_ctf_field_destroy(struct bt_ctf_ref *ref)
+void bt_ctf_field_destroy(struct bt_ref *ref)
 {
        struct bt_ctf_field *field;
+       struct bt_ctf_base *base;
        struct bt_ctf_field_type *type;
        enum ctf_type_id type_id;
 
@@ -1261,7 +1284,8 @@ void bt_ctf_field_destroy(struct bt_ctf_ref *ref)
                return;
        }
 
-       field = container_of(ref, struct bt_ctf_field, ref_count);
+       base = container_of(ref, struct bt_ctf_base, ref_count);
+       field = container_of(base, struct bt_ctf_field, base);
        type = field->type;
        type_id = bt_ctf_field_type_get_type_id(type);
        if (type_id <= CTF_TYPE_UNKNOWN ||
@@ -1370,7 +1394,9 @@ void bt_ctf_field_sequence_destroy(struct bt_ctf_field *field)
        }
 
        sequence = container_of(field, struct bt_ctf_field_sequence, parent);
-       g_ptr_array_free(sequence->elements, TRUE);
+       if (sequence->elements) {
+               g_ptr_array_free(sequence->elements, TRUE);
+       }
        bt_ctf_field_put(sequence->length);
        g_free(sequence);
 }
This page took 0.02383 seconds and 4 git commands to generate.