+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(dst, 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 = 0, 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);
+
+ /* This field_name_to_index HT is owned by the structure field type */
+ struct_dst->field_name_to_index = struct_src->field_name_to_index;
+ g_ptr_array_set_size(struct_dst->fields, struct_src->fields->len);
+
+ for (i = 0; i < struct_src->fields->len; i++) {
+ struct bt_ctf_field *field =
+ g_ptr_array_index(struct_src->fields, i);
+ struct bt_ctf_field *field_copy = NULL;
+
+ if (field) {
+ field_copy = bt_ctf_field_copy(field);
+
+ if (!field_copy) {
+ ret = -1;
+ goto end;
+ }
+ }
+
+ g_ptr_array_index(struct_dst->fields, i) = 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);
+
+ g_ptr_array_set_size(array_dst->elements, array_src->elements->len);
+ for (i = 0; i < array_src->elements->len; i++) {
+ struct bt_ctf_field *field =
+ g_ptr_array_index(array_src->elements, i);
+ struct bt_ctf_field *field_copy = NULL;
+
+ if (field) {
+ field_copy = bt_ctf_field_copy(field);
+
+ if (!field_copy) {
+ ret = -1;
+ goto end;
+ }
+ }
+
+ g_ptr_array_index(array_dst->elements, i) = 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;
+ struct bt_ctf_field *src_length;
+ struct bt_ctf_field *dst_length;
+
+ sequence_src = container_of(src, struct bt_ctf_field_sequence, parent);
+ sequence_dst = container_of(dst, struct bt_ctf_field_sequence, parent);
+
+ src_length = bt_ctf_field_sequence_get_length(src);
+
+ if (!src_length) {
+ /* no length set yet: keep destination sequence empty */
+ goto end;
+ }
+
+ /* copy source length */
+ dst_length = bt_ctf_field_copy(src_length);
+ bt_ctf_field_put(src_length);
+
+ if (!dst_length) {
+ ret = -1;
+ goto end;
+ }
+
+ /* this will initialize the destination sequence's internal array */
+ ret = bt_ctf_field_sequence_set_length(dst, dst_length);
+ bt_ctf_field_put(dst_length);
+
+ if (ret) {
+ goto end;
+ }
+
+ assert(sequence_dst->elements->len == sequence_src->elements->len);
+
+ for (i = 0; i < sequence_src->elements->len; i++) {
+ struct bt_ctf_field *field =
+ g_ptr_array_index(sequence_src->elements, i);
+ struct bt_ctf_field *field_copy = NULL;
+
+ if (field) {
+ field_copy = bt_ctf_field_copy(field);
+
+ if (!field_copy) {
+ ret = -1;
+ goto end;
+ }
+ }
+
+ g_ptr_array_index(sequence_dst->elements, i) = 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;
+}
+