Implement bt_ctf_field deep copy
[babeltrace.git] / formats / ctf / ir / event-fields.c
index 17616a51be4f21baa3b7faa1d0f6098d8724926a..e49a17be9b147f732b36e58b739cccef6e33893a 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * event-fields.c
  *
- * Babeltrace CTF Writer
+ * Babeltrace CTF IR - Event Fields
  *
- * Copyright 2013 EfficiOS Inc.
+ * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
  * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
@@ -88,6 +88,21 @@ int bt_ctf_field_array_validate(struct bt_ctf_field *field);
 static
 int bt_ctf_field_sequence_validate(struct bt_ctf_field *field);
 
+static
+int bt_ctf_field_generic_reset(struct bt_ctf_field *field);
+static
+int bt_ctf_field_structure_reset(struct bt_ctf_field *field);
+static
+int bt_ctf_field_variant_reset(struct bt_ctf_field *field);
+static
+int bt_ctf_field_enumeration_reset(struct bt_ctf_field *field);
+static
+int bt_ctf_field_array_reset(struct bt_ctf_field *field);
+static
+int bt_ctf_field_sequence_reset(struct bt_ctf_field *field);
+static
+int bt_ctf_field_string_reset(struct bt_ctf_field *field);
+
 static
 int bt_ctf_field_integer_serialize(struct bt_ctf_field *,
                struct ctf_stream_pos *);
@@ -113,6 +128,24 @@ static
 int bt_ctf_field_string_serialize(struct bt_ctf_field *,
                struct ctf_stream_pos *);
 
+static
+int bt_ctf_field_integer_copy(struct bt_ctf_field *, struct bt_ctf_field *);
+static
+int bt_ctf_field_enumeration_copy(struct bt_ctf_field *, struct bt_ctf_field *);
+static
+int bt_ctf_field_floating_point_copy(struct bt_ctf_field *,
+               struct bt_ctf_field *);
+static
+int bt_ctf_field_structure_copy(struct bt_ctf_field *, struct bt_ctf_field *);
+static
+int bt_ctf_field_variant_copy(struct bt_ctf_field *, struct bt_ctf_field *);
+static
+int bt_ctf_field_array_copy(struct bt_ctf_field *, struct bt_ctf_field *);
+static
+int bt_ctf_field_sequence_copy(struct bt_ctf_field *, struct bt_ctf_field *);
+static
+int bt_ctf_field_string_copy(struct bt_ctf_field *, struct bt_ctf_field *);
+
 static
 int increase_packet_size(struct ctf_stream_pos *pos);
 
@@ -155,6 +188,18 @@ int (*field_validate_funcs[])(struct bt_ctf_field *) = {
        [CTF_TYPE_STRING] = bt_ctf_field_generic_validate,
 };
 
+static
+int (*field_reset_funcs[])(struct bt_ctf_field *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_generic_reset,
+       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_reset,
+       [CTF_TYPE_FLOAT] = bt_ctf_field_generic_reset,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_reset,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_reset,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_array_reset,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_reset,
+       [CTF_TYPE_STRING] = bt_ctf_field_string_reset,
+};
+
 static
 int (*field_serialize_funcs[])(struct bt_ctf_field *,
                struct ctf_stream_pos *) = {
@@ -169,6 +214,18 @@ int (*field_serialize_funcs[])(struct bt_ctf_field *,
        [CTF_TYPE_STRING] = bt_ctf_field_string_serialize,
 };
 
+static
+int (*field_copy_funcs[])(struct bt_ctf_field *, struct bt_ctf_field *) = {
+       [CTF_TYPE_INTEGER] = bt_ctf_field_integer_copy,
+       [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_copy,
+       [CTF_TYPE_FLOAT] = bt_ctf_field_floating_point_copy,
+       [CTF_TYPE_STRUCT] = bt_ctf_field_structure_copy,
+       [CTF_TYPE_VARIANT] = bt_ctf_field_variant_copy,
+       [CTF_TYPE_ARRAY] = bt_ctf_field_array_copy,
+       [CTF_TYPE_SEQUENCE] = bt_ctf_field_sequence_copy,
+       [CTF_TYPE_STRING] = bt_ctf_field_string_copy,
+};
+
 struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type)
 {
        struct bt_ctf_field *field = NULL;
@@ -212,6 +269,42 @@ void bt_ctf_field_put(struct bt_ctf_field *field)
        }
 }
 
+struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field)
+{
+       struct bt_ctf_field_type *ret = NULL;
+
+       if (!field) {
+               goto end;
+       }
+
+       ret = field->type;
+       bt_ctf_field_type_get(ret);
+end:
+       return ret;
+}
+
+struct bt_ctf_field *bt_ctf_field_sequence_get_length(
+               struct bt_ctf_field *field)
+{
+       struct bt_ctf_field *ret = NULL;
+       struct bt_ctf_field_sequence *sequence;
+
+       if (!field) {
+               goto end;
+       }
+
+       if (bt_ctf_field_type_get_type_id(field->type) !=
+               CTF_TYPE_SEQUENCE) {
+               goto end;
+       }
+
+       sequence = container_of(field, struct bt_ctf_field_sequence, parent);
+       ret = sequence->length;
+       bt_ctf_field_get(ret);
+end:
+       return ret;
+}
+
 int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field,
                struct bt_ctf_field *length_field)
 {
@@ -233,6 +326,7 @@ int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field,
 
        length_type = container_of(length_field->type,
                struct bt_ctf_field_type_integer, parent);
+       /* The length field must be unsigned */
        if (length_type->declaration.signedness) {
                ret = -1;
                goto end;
@@ -268,8 +362,7 @@ struct bt_ctf_field *bt_ctf_field_structure_get_field(
        struct bt_ctf_field *new_field = NULL;
        GQuark field_quark;
        struct bt_ctf_field_structure *structure;
-       struct bt_ctf_field_type_structure *structure_type;
-       struct bt_ctf_field_type *field_type;
+       struct bt_ctf_field_type *field_type = NULL;
        size_t index;
 
        if (!field || !name ||
@@ -280,9 +373,9 @@ struct bt_ctf_field *bt_ctf_field_structure_get_field(
 
        field_quark = g_quark_from_string(name);
        structure = container_of(field, struct bt_ctf_field_structure, parent);
-       structure_type = container_of(field->type,
-               struct bt_ctf_field_type_structure, parent);
-       field_type = bt_ctf_field_type_structure_get_type(structure_type, name);
+       field_type =
+               bt_ctf_field_type_structure_get_field_type_by_name(field->type,
+               name);
        if (!g_hash_table_lookup_extended(structure->field_name_to_index,
                GUINT_TO_POINTER(field_quark), NULL, (gpointer *)&index)) {
                goto error;
@@ -302,9 +395,65 @@ struct bt_ctf_field *bt_ctf_field_structure_get_field(
 end:
        bt_ctf_field_get(new_field);
 error:
+       if (field_type) {
+               bt_ctf_field_type_put(field_type);
+       }
        return new_field;
 }
 
+struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index(
+               struct bt_ctf_field *field, int index)
+{
+       int ret;
+       const char *field_name;
+       struct bt_ctf_field_structure *structure;
+       struct bt_ctf_field_type *structure_type;
+       struct bt_ctf_field_type *field_type = NULL;
+       struct bt_ctf_field *ret_field = NULL;
+
+       if (!field ||
+               bt_ctf_field_type_get_type_id(field->type) != CTF_TYPE_STRUCT) {
+               goto end;
+       }
+
+       structure = container_of(field, struct bt_ctf_field_structure, parent);
+       if (index >= structure->fields->len) {
+               goto error;
+       }
+
+       ret_field = structure->fields->pdata[index];
+       if (ret_field) {
+               goto end;
+       }
+
+       /* Field has not been instanciated yet, create it */
+       structure_type = bt_ctf_field_get_type(field);
+       if (!structure_type) {
+               goto error;
+       }
+
+       ret = bt_ctf_field_type_structure_get_field(structure_type,
+               &field_name, &field_type, index);
+       bt_ctf_field_type_put(structure_type);
+       if (ret) {
+               goto error;
+       }
+
+       ret_field = bt_ctf_field_create(field_type);
+       if (!ret_field) {
+               goto error;
+       }
+
+       structure->fields->pdata[index] = ret_field;
+end:
+       bt_ctf_field_get(ret_field);
+error:
+       if (field_type) {
+               bt_ctf_field_type_put(field_type);
+       }
+       return ret_field;
+}
+
 BT_HIDDEN
 int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
                const char *name, struct bt_ctf_field *value)
@@ -312,8 +461,7 @@ int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
        int ret = 0;
        GQuark field_quark;
        struct bt_ctf_field_structure *structure;
-       struct bt_ctf_field_type_structure *structure_type;
-       struct bt_ctf_field_type *expected_field_type;
+       struct bt_ctf_field_type *expected_field_type = NULL;
        size_t index;
 
        if (!field || !name || !value ||
@@ -325,10 +473,9 @@ int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
 
        field_quark = g_quark_from_string(name);
        structure = container_of(field, struct bt_ctf_field_structure, parent);
-       structure_type = container_of(field->type,
-               struct bt_ctf_field_type_structure, parent);
-       expected_field_type = bt_ctf_field_type_structure_get_type(
-               structure_type, name);
+       expected_field_type =
+               bt_ctf_field_type_structure_get_field_type_by_name(field->type,
+               name);
        if (expected_field_type != value->type) {
                ret = -1;
                goto end;
@@ -346,6 +493,9 @@ int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
        structure->fields->pdata[index] = value;
        bt_ctf_field_get(value);
 end:
+       if (expected_field_type) {
+               bt_ctf_field_type_put(expected_field_type);
+       }
        return ret;
 }
 
@@ -353,9 +503,8 @@ struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field,
                uint64_t index)
 {
        struct bt_ctf_field *new_field = NULL;
+       struct bt_ctf_field_type *field_type = NULL;
        struct bt_ctf_field_array *array;
-       struct bt_ctf_field_type_array *array_type;
-       struct bt_ctf_field_type *field_type;
 
        if (!field || bt_ctf_field_type_get_type_id(field->type) !=
                CTF_TYPE_ARRAY) {
@@ -367,9 +516,7 @@ struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field,
                goto end;
        }
 
-       array_type = container_of(field->type, struct bt_ctf_field_type_array,
-               parent);
-       field_type = bt_ctf_field_type_array_get_element_type(array_type);
+       field_type = bt_ctf_field_type_array_get_element_type(field->type);
        if (array->elements->pdata[(size_t)index]) {
                new_field = array->elements->pdata[(size_t)index];
                goto end;
@@ -379,6 +526,9 @@ struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field,
        bt_ctf_field_get(new_field);
        array->elements->pdata[(size_t)index] = new_field;
 end:
+       if (field_type) {
+               bt_ctf_field_type_put(field_type);
+       }
        return new_field;
 }
 
@@ -386,9 +536,8 @@ struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
                uint64_t index)
 {
        struct bt_ctf_field *new_field = NULL;
+       struct bt_ctf_field_type *field_type = NULL;
        struct bt_ctf_field_sequence *sequence;
-       struct bt_ctf_field_type_sequence *sequence_type;
-       struct bt_ctf_field_type *field_type;
 
        if (!field || bt_ctf_field_type_get_type_id(field->type) !=
                CTF_TYPE_SEQUENCE) {
@@ -400,9 +549,7 @@ struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
                goto end;
        }
 
-       sequence_type = container_of(field->type,
-               struct bt_ctf_field_type_sequence, parent);
-       field_type = bt_ctf_field_type_sequence_get_element_type(sequence_type);
+       field_type = bt_ctf_field_type_sequence_get_element_type(field->type);
        if (sequence->elements->pdata[(size_t)index]) {
                new_field = sequence->elements->pdata[(size_t)index];
                goto end;
@@ -412,6 +559,9 @@ struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
        bt_ctf_field_get(new_field);
        sequence->elements->pdata[(size_t)index] = new_field;
 end:
+       if (field_type) {
+               bt_ctf_field_type_put(field_type);
+       }
        return new_field;
 }
 
@@ -445,13 +595,13 @@ struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field,
        tag_enum_integer = container_of(tag_enum, struct bt_ctf_field_integer,
                parent);
 
-       if (!bt_ctf_field_validate(variant->tag)) {
+       if (bt_ctf_field_validate(tag_field) < 0) {
                goto end;
        }
 
        tag_enum_value = tag_enum_integer->definition.value._signed;
-       field_type = bt_ctf_field_type_variant_get_field_type(variant_type,
-               tag_enum_value);
+       field_type = bt_ctf_field_type_variant_get_field_type_signed(
+               variant_type, tag_enum_value);
        if (!field_type) {
                goto end;
        }
@@ -478,7 +628,8 @@ struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
        struct bt_ctf_field *container = NULL;
        struct bt_ctf_field_enumeration *enumeration;
 
-       if (!field) {
+       if (!field || bt_ctf_field_type_get_type_id(field->type) !=
+               CTF_TYPE_ENUM) {
                goto end;
        }
 
@@ -498,6 +649,89 @@ end:
        return container;
 }
 
+const char *bt_ctf_field_enumeration_get_mapping_name(
+       struct bt_ctf_field *field)
+{
+       int ret;
+       const char *name = NULL;
+       struct bt_ctf_field *container = NULL;
+       struct bt_ctf_field_type *container_type = NULL;
+       struct bt_ctf_field_type_integer *integer_type = NULL;
+       struct bt_ctf_field_type_enumeration *enumeration_type = NULL;
+
+       container = bt_ctf_field_enumeration_get_container(field);
+       if (!container) {
+               goto end;
+       }
+
+       container_type = bt_ctf_field_get_type(container);
+       if (!container_type) {
+               goto error_put_container;
+       }
+
+       integer_type = container_of(container_type,
+               struct bt_ctf_field_type_integer, parent);
+       enumeration_type = container_of(field->type,
+               struct bt_ctf_field_type_enumeration, parent);
+
+       if (!integer_type->declaration.signedness) {
+               uint64_t value;
+               ret = bt_ctf_field_unsigned_integer_get_value(container,
+                     &value);
+               if (ret) {
+                       goto error_put_container_type;
+               }
+
+               name = bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
+                       enumeration_type, value);
+       } else {
+               int64_t value;
+               ret = bt_ctf_field_signed_integer_get_value(container,
+                     &value);
+               if (ret) {
+                       goto error_put_container_type;
+               }
+
+               name = bt_ctf_field_type_enumeration_get_mapping_name_signed(
+                       enumeration_type, value);
+       }
+
+error_put_container_type:
+       bt_ctf_field_type_put(container_type);
+error_put_container:
+       bt_ctf_field_put(container);
+end:
+       return name;
+}
+
+int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field *field,
+               int64_t *value)
+{
+       int ret = 0;
+       struct bt_ctf_field_integer *integer;
+       struct bt_ctf_field_type_integer *integer_type;
+
+       if (!field || !value || !field->payload_set ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_INTEGER) {
+               ret = -1;
+               goto end;
+       }
+
+       integer_type = container_of(field->type,
+               struct bt_ctf_field_type_integer, parent);
+       if (!integer_type->declaration.signedness) {
+               ret = -1;
+               goto end;
+       }
+
+       integer = container_of(field,
+               struct bt_ctf_field_integer, parent);
+       *value = integer->definition.value._signed;
+end:
+       return ret;
+}
+
 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field,
                int64_t value)
 {
@@ -536,6 +770,34 @@ end:
        return ret;
 }
 
+int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field *field,
+               uint64_t *value)
+{
+       int ret = 0;
+       struct bt_ctf_field_integer *integer;
+       struct bt_ctf_field_type_integer *integer_type;
+
+       if (!field || !value || !field->payload_set ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_INTEGER) {
+               ret = -1;
+               goto end;
+       }
+
+       integer_type = container_of(field->type,
+               struct bt_ctf_field_type_integer, parent);
+       if (integer_type->declaration.signedness) {
+               ret = -1;
+               goto end;
+       }
+
+       integer = container_of(field,
+               struct bt_ctf_field_integer, parent);
+       *value = integer->definition.value._unsigned;
+end:
+       return ret;
+}
+
 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field,
                uint64_t value)
 {
@@ -573,6 +835,26 @@ end:
        return ret;
 }
 
+int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field,
+               double *value)
+{
+       int ret = 0;
+       struct bt_ctf_field_floating_point *floating_point;
+
+       if (!field || !value || !field->payload_set ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_FLOAT) {
+               ret = -1;
+               goto end;
+       }
+
+       floating_point = container_of(field,
+               struct bt_ctf_field_floating_point, parent);
+       *value = floating_point->definition.value;
+end:
+       return ret;
+}
+
 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field,
                double value)
 {
@@ -593,6 +875,24 @@ end:
        return ret;
 }
 
+const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field)
+{
+       const char *ret = NULL;
+       struct bt_ctf_field_string *string;
+
+       if (!field || !field->payload_set ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_STRING) {
+               goto end;
+       }
+
+       string = container_of(field,
+               struct bt_ctf_field_string, parent);
+       ret = string->payload->str;
+end:
+       return ret;
+}
+
 int bt_ctf_field_string_set_value(struct bt_ctf_field *field,
                const char *value)
 {
@@ -608,10 +908,11 @@ int bt_ctf_field_string_set_value(struct bt_ctf_field *field,
 
        string = container_of(field, struct bt_ctf_field_string, parent);
        if (string->payload) {
-               g_string_free(string->payload, TRUE);
+               g_string_assign(string->payload, value);
+       } else {
+               string->payload = g_string_new(value);
        }
 
-       string->payload = g_string_new(value);
        string->parent.payload_set = 1;
 end:
        return ret;
@@ -639,6 +940,28 @@ end:
        return ret;
 }
 
+BT_HIDDEN
+int bt_ctf_field_reset(struct bt_ctf_field *field)
+{
+       int ret = 0;
+       enum ctf_type_id type_id;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       type_id = bt_ctf_field_type_get_type_id(field->type);
+       if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = field_reset_funcs[type_id](field);
+end:
+       return ret;
+}
+
 BT_HIDDEN
 int bt_ctf_field_serialize(struct bt_ctf_field *field,
                struct ctf_stream_pos *pos)
@@ -662,6 +985,40 @@ end:
        return ret;
 }
 
+BT_HIDDEN
+struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
+{
+       int ret;
+       struct bt_ctf_field *copy = NULL;
+       enum ctf_type_id type_id;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       type_id = bt_ctf_field_type_get_type_id(field->type);
+       if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
+               ret = -1;
+               goto end;
+       }
+
+       copy = bt_ctf_field_create(field->type);
+       if (!copy) {
+               goto end;
+       }
+
+       bt_ctf_field_type_get(field->type);
+       copy->type = field->type;
+       ret = field_copy_funcs[type_id](field, copy);
+       if (ret) {
+               bt_ctf_field_put(copy);
+               copy = NULL;
+       }
+end:
+       return copy;
+}
+
 static
 struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type)
 {
@@ -1053,6 +1410,185 @@ end:
        return ret;
 }
 
+static
+int bt_ctf_field_generic_reset(struct bt_ctf_field *field)
+{
+       int ret = 0;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       field->payload_set = 0;
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_enumeration_reset(struct bt_ctf_field *field)
+{
+       int ret = 0;
+       struct bt_ctf_field_enumeration *enumeration;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       enumeration = container_of(field, struct bt_ctf_field_enumeration,
+               parent);
+       if (!enumeration->payload) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_reset(enumeration->payload);
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_structure_reset(struct bt_ctf_field *field)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field_structure *structure;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       structure = container_of(field, struct bt_ctf_field_structure, parent);
+       for (i = 0; i < structure->fields->len; i++) {
+               struct bt_ctf_field *member = structure->fields->pdata[i];
+
+               if (!member) {
+                       /*
+                        * Structure members are lazily initialized; skip if
+                        * this member has not been allocated yet.
+                        */
+                       continue;
+               }
+
+               ret = bt_ctf_field_reset(member);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_variant_reset(struct bt_ctf_field *field)
+{
+       int ret = 0;
+       struct bt_ctf_field_variant *variant;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       variant = container_of(field, struct bt_ctf_field_variant, parent);
+       if (variant->payload) {
+               ret = bt_ctf_field_reset(variant->payload);
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_array_reset(struct bt_ctf_field *field)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field_array *array;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       array = container_of(field, struct bt_ctf_field_array, parent);
+       for (i = 0; i < array->elements->len; i++) {
+               struct bt_ctf_field *member = array->elements->pdata[i];
+
+               if (!member) {
+                       /*
+                        * Array elements are lazily initialized; skip if
+                        * this member has not been allocated yet.
+                        */
+                       continue;
+               }
+
+               ret = bt_ctf_field_reset(member);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_sequence_reset(struct bt_ctf_field *field)
+{
+       size_t i;
+       int ret = 0;
+       struct bt_ctf_field_sequence *sequence;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       sequence = container_of(field, struct bt_ctf_field_sequence, parent);
+       for (i = 0; i < sequence->elements->len; i++) {
+               struct bt_ctf_field *member = sequence->elements->pdata[i];
+
+               if (!member) {
+                       /*
+                        * Sequence elements are lazily initialized; skip if
+                        * this member has not been allocated yet.
+                        */
+                       continue;
+               }
+
+               ret = bt_ctf_field_reset(member);
+               if (ret) {
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_string_reset(struct bt_ctf_field *field)
+{
+       int ret = 0;
+       struct bt_ctf_field_string *string;
+
+       if (!field) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_generic_reset(field);
+       if (ret) {
+               goto end;
+       }
+
+       string = container_of(field, struct bt_ctf_field_string, parent);
+       if (string->payload) {
+               g_string_truncate(string->payload, 0);
+       }
+end:
+       return ret;
+}
+
 static
 int bt_ctf_field_integer_serialize(struct bt_ctf_field *field,
                struct ctf_stream_pos *pos)
@@ -1229,6 +1765,194 @@ end:
        return ret;
 }
 
+static
+int bt_ctf_field_integer_copy(struct bt_ctf_field *src,
+               struct bt_ctf_field *dst)
+{
+       struct bt_ctf_field_integer *integer_src, *integer_dst;
+
+       integer_src = container_of(src, struct bt_ctf_field_integer, parent);
+       integer_dst = container_of(src, struct bt_ctf_field_integer, parent);
+
+       memcpy(&integer_dst->definition, &integer_src->definition,
+               sizeof(struct definition_integer));
+       return 0;
+}
+
+static
+int bt_ctf_field_enumeration_copy(struct bt_ctf_field *src,
+               struct bt_ctf_field *dst)
+{
+       int ret = 0;
+       struct bt_ctf_field_enumeration *enum_src, *enum_dst;
+
+       enum_src = container_of(src, struct bt_ctf_field_enumeration, parent);
+       enum_dst = container_of(dst, struct bt_ctf_field_enumeration, parent);
+
+       if (enum_src->payload) {
+               enum_dst->payload = bt_ctf_field_copy(enum_src->payload);
+               if (!enum_dst->payload) {
+                       ret = -1;
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_floating_point_copy(
+               struct bt_ctf_field *src, struct bt_ctf_field *dst)
+{
+       struct bt_ctf_field_floating_point *float_src, *float_dst;
+
+       float_src = container_of(src, struct bt_ctf_field_floating_point,
+               parent);
+       float_dst = container_of(dst, struct bt_ctf_field_floating_point,
+               parent);
+
+       memcpy(&float_dst->definition, &float_src->definition,
+               sizeof(struct definition_float));
+       memcpy(&float_dst->sign, &float_src->sign,
+               sizeof(struct definition_integer));
+       memcpy(&float_dst->mantissa, &float_src->mantissa,
+               sizeof(struct definition_integer));
+       memcpy(&float_dst->exp, &float_src->exp,
+               sizeof(struct definition_integer));
+       return 0;
+}
+
+static
+int bt_ctf_field_structure_copy(struct bt_ctf_field *src,
+               struct bt_ctf_field *dst)
+{
+       int ret, i;
+       struct bt_ctf_field_structure *struct_src, *struct_dst;
+
+       struct_src = container_of(src, struct bt_ctf_field_structure, parent);
+       struct_dst = container_of(dst, struct bt_ctf_field_structure, parent);
+
+       struct_dst->field_name_to_index = struct_src->field_name_to_index;
+       struct_dst->fields = g_ptr_array_sized_new(struct_src->fields->len);
+       if (!struct_dst->fields) {
+               ret = -1;
+               goto end;
+       }
+
+       for (i = 0; i < struct_src->fields->len; i++) {
+               struct bt_ctf_field *field_copy = bt_ctf_field_copy(
+                       g_ptr_array_index(struct_src->fields, i));
+
+               if (!field_copy) {
+                       ret = -1;
+                       goto end;
+               }
+               g_ptr_array_add(struct_dst->fields, field_copy);
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_variant_copy(struct bt_ctf_field *src,
+               struct bt_ctf_field *dst)
+{
+       int ret = 0;
+       struct bt_ctf_field_variant *variant_src, *variant_dst;
+
+       variant_src = container_of(src, struct bt_ctf_field_variant, parent);
+       variant_dst = container_of(dst, struct bt_ctf_field_variant, parent);
+
+       if (variant_src->tag) {
+               variant_dst->tag = bt_ctf_field_copy(variant_src->tag);
+               if (!variant_dst->tag) {
+                       ret = -1;
+                       goto end;
+               }
+       }
+       if (variant_src->payload) {
+               variant_dst->payload = bt_ctf_field_copy(variant_src->payload);
+               if (!variant_dst->payload) {
+                       ret = -1;
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_array_copy(struct bt_ctf_field *src,
+               struct bt_ctf_field *dst)
+{
+       int ret = 0, i;
+       struct bt_ctf_field_array *array_src, *array_dst;
+
+       array_src = container_of(src, struct bt_ctf_field_array, parent);
+       array_dst = container_of(dst, struct bt_ctf_field_array, parent);
+
+       array_dst->elements = g_ptr_array_sized_new(array_src->elements->len);
+       for (i = 0; i < array_src->elements->len; i++) {
+               struct bt_ctf_field *field_copy = bt_ctf_field_copy(
+                       g_ptr_array_index(array_src->elements, i));
+
+               if (!field_copy) {
+                       ret = -1;
+                       goto end;
+               }
+               g_ptr_array_add(array_dst->elements, field_copy);
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_sequence_copy(struct bt_ctf_field *src,
+               struct bt_ctf_field *dst)
+{
+       int ret = 0, i;
+       struct bt_ctf_field_sequence *sequence_src, *sequence_dst;
+
+       sequence_src = container_of(src, struct bt_ctf_field_sequence, parent);
+       sequence_dst = container_of(dst, struct bt_ctf_field_sequence, parent);
+
+       sequence_dst->elements = g_ptr_array_sized_new(
+               sequence_src->elements->len);
+       for (i = 0; i < sequence_src->elements->len; i++) {
+               struct bt_ctf_field *field_copy = bt_ctf_field_copy(
+                       g_ptr_array_index(sequence_src->elements, i));
+
+               if (!field_copy) {
+                       ret = -1;
+                       goto end;
+               }
+               g_ptr_array_add(sequence_dst->elements, field_copy);
+       }
+end:
+       return ret;
+}
+
+static
+int bt_ctf_field_string_copy(struct bt_ctf_field *src,
+               struct bt_ctf_field *dst)
+{
+       int ret = 0;
+       struct bt_ctf_field_string *string_src, *string_dst;
+
+       string_src = container_of(src, struct bt_ctf_field_string, parent);
+       string_dst = container_of(dst, struct bt_ctf_field_string, parent);
+
+       if (string_src->payload) {
+               string_dst->payload = g_string_new(string_src->payload->str);
+               if (!string_dst->payload) {
+                       ret = -1;
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+
 static
 int increase_packet_size(struct ctf_stream_pos *pos)
 {
This page took 0.034587 seconds and 4 git commands to generate.