Fix: ir: bt_ctf_field_copy(): copy payload_set
[babeltrace.git] / formats / ctf / ir / event-fields.c
index 2c6ccf6f1acd1067fdd0d1fc5e0faf058b27a006..f524d5dbb8ce2fef86b935a5d4e69226e4c1999d 100644 (file)
@@ -76,32 +76,32 @@ static
 void bt_ctf_field_string_destroy(struct bt_ctf_field *);
 
 static
-int bt_ctf_field_generic_validate(struct bt_ctf_field *field);
+int bt_ctf_field_generic_validate(struct bt_ctf_field *);
 static
-int bt_ctf_field_structure_validate(struct bt_ctf_field *field);
+int bt_ctf_field_structure_validate(struct bt_ctf_field *);
 static
-int bt_ctf_field_variant_validate(struct bt_ctf_field *field);
+int bt_ctf_field_variant_validate(struct bt_ctf_field *);
 static
-int bt_ctf_field_enumeration_validate(struct bt_ctf_field *field);
+int bt_ctf_field_enumeration_validate(struct bt_ctf_field *);
 static
-int bt_ctf_field_array_validate(struct bt_ctf_field *field);
+int bt_ctf_field_array_validate(struct bt_ctf_field *);
 static
-int bt_ctf_field_sequence_validate(struct bt_ctf_field *field);
+int bt_ctf_field_sequence_validate(struct bt_ctf_field *);
 
 static
-int bt_ctf_field_generic_reset(struct bt_ctf_field *field);
+int bt_ctf_field_generic_reset(struct bt_ctf_field *);
 static
-int bt_ctf_field_structure_reset(struct bt_ctf_field *field);
+int bt_ctf_field_structure_reset(struct bt_ctf_field *);
 static
-int bt_ctf_field_variant_reset(struct bt_ctf_field *field);
+int bt_ctf_field_variant_reset(struct bt_ctf_field *);
 static
-int bt_ctf_field_enumeration_reset(struct bt_ctf_field *field);
+int bt_ctf_field_enumeration_reset(struct bt_ctf_field *);
 static
-int bt_ctf_field_array_reset(struct bt_ctf_field *field);
+int bt_ctf_field_array_reset(struct bt_ctf_field *);
 static
-int bt_ctf_field_sequence_reset(struct bt_ctf_field *field);
+int bt_ctf_field_sequence_reset(struct bt_ctf_field *);
 static
-int bt_ctf_field_string_reset(struct bt_ctf_field *field);
+int bt_ctf_field_string_reset(struct bt_ctf_field *);
 
 static
 int bt_ctf_field_integer_serialize(struct bt_ctf_field *,
@@ -128,11 +128,29 @@ 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);
 
 static
-struct bt_ctf_field *(*field_create_funcs[])(
+struct bt_ctf_field *(* const field_create_funcs[])(
                struct bt_ctf_field_type *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_integer_create,
        [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_create,
@@ -146,7 +164,7 @@ struct bt_ctf_field *(*field_create_funcs[])(
 };
 
 static
-void (*field_destroy_funcs[])(struct bt_ctf_field *) = {
+void (* const field_destroy_funcs[])(struct bt_ctf_field *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_integer_destroy,
        [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_destroy,
        [CTF_TYPE_FLOAT] =
@@ -159,7 +177,7 @@ void (*field_destroy_funcs[])(struct bt_ctf_field *) = {
 };
 
 static
-int (*field_validate_funcs[])(struct bt_ctf_field *) = {
+int (* const field_validate_funcs[])(struct bt_ctf_field *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_generic_validate,
        [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_validate,
        [CTF_TYPE_FLOAT] = bt_ctf_field_generic_validate,
@@ -171,7 +189,7 @@ int (*field_validate_funcs[])(struct bt_ctf_field *) = {
 };
 
 static
-int (*field_reset_funcs[])(struct bt_ctf_field *) = {
+int (* const 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,
@@ -183,7 +201,7 @@ int (*field_reset_funcs[])(struct bt_ctf_field *) = {
 };
 
 static
-int (*field_serialize_funcs[])(struct bt_ctf_field *,
+int (* const field_serialize_funcs[])(struct bt_ctf_field *,
                struct ctf_stream_pos *) = {
        [CTF_TYPE_INTEGER] = bt_ctf_field_integer_serialize,
        [CTF_TYPE_ENUM] = bt_ctf_field_enumeration_serialize,
@@ -196,6 +214,19 @@ int (*field_serialize_funcs[])(struct bt_ctf_field *,
        [CTF_TYPE_STRING] = bt_ctf_field_string_serialize,
 };
 
+static
+int (* const 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;
@@ -888,6 +919,72 @@ end:
        return ret;
 }
 
+int bt_ctf_field_string_append(struct bt_ctf_field *field,
+               const char *value)
+{
+       int ret = 0;
+       struct bt_ctf_field_string *string_field;
+
+       if (!field || !value ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_STRING) {
+               ret = -1;
+               goto end;
+       }
+
+       string_field = container_of(field, struct bt_ctf_field_string, parent);
+
+       if (string_field->payload) {
+               g_string_append(string_field->payload, value);
+       } else {
+               string_field->payload = g_string_new(value);
+       }
+
+       string_field->parent.payload_set = 1;
+
+end:
+       return ret;
+}
+
+int bt_ctf_field_string_append_len(struct bt_ctf_field *field,
+               const char *value, unsigned int length)
+{
+       int i;
+       int ret = 0;
+       unsigned int effective_length = length;
+       struct bt_ctf_field_string *string_field;
+
+       if (!field || !value ||
+               bt_ctf_field_type_get_type_id(field->type) !=
+                       CTF_TYPE_STRING) {
+               ret = -1;
+               goto end;
+       }
+
+       string_field = container_of(field, struct bt_ctf_field_string, parent);
+
+       /* make sure no null bytes are appended */
+       for (i = 0; i < length; ++i) {
+               if (value[i] == '\0') {
+                       effective_length = i;
+                       break;
+               }
+       }
+
+       if (string_field->payload) {
+               g_string_append_len(string_field->payload, value,
+                       effective_length);
+       } else {
+               string_field->payload = g_string_new_len(value,
+                       effective_length);
+       }
+
+       string_field->parent.payload_set = 1;
+
+end:
+       return ret;
+}
+
 BT_HIDDEN
 int bt_ctf_field_validate(struct bt_ctf_field *field)
 {
@@ -955,6 +1052,37 @@ 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) {
+               goto end;
+       }
+
+       type_id = bt_ctf_field_type_get_type_id(field->type);
+       if (type_id <= CTF_TYPE_UNKNOWN || type_id >= NR_CTF_TYPES) {
+               goto end;
+       }
+
+       copy = bt_ctf_field_create(field->type);
+       if (!copy) {
+               goto end;
+       }
+
+       copy->payload_set = field->payload_set;
+       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)
 {
@@ -1227,7 +1355,9 @@ void bt_ctf_field_string_destroy(struct bt_ctf_field *field)
        }
 
        string = container_of(field, struct bt_ctf_field_string, parent);
-       g_string_free(string->payload, TRUE);
+       if (string->payload) {
+               g_string_free(string->payload, TRUE);
+       }
        g_free(string);
 }
 
@@ -1701,6 +1831,191 @@ 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(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_copy = bt_ctf_field_copy(
+                       g_ptr_array_index(struct_src->fields, i));
+
+               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_copy = bt_ctf_field_copy(
+                       g_ptr_array_index(array_src->elements, i));
+
+               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;
+
+       sequence_src = container_of(src, struct bt_ctf_field_sequence, parent);
+       sequence_dst = container_of(dst, struct bt_ctf_field_sequence, parent);
+
+       g_ptr_array_set_size(sequence_dst->elements,
+               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_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;
+}
+
 static
 int increase_packet_size(struct ctf_stream_pos *pos)
 {
This page took 0.028149 seconds and 4 git commands to generate.