Allocate structure fields on creation
[babeltrace.git] / lib / ctf-ir / fields.c
index c1553547f6b4fdf67e0bd366533ec2a3e35e0cb9..95c19fd11a643c8031957f941164540a3e88f0f3 100644 (file)
@@ -39,8 +39,6 @@
 #include <babeltrace/align-internal.h>
 #include <inttypes.h>
 
-#define PACKET_LEN_INCREMENT   (getpagesize() * 8 * CHAR_BIT)
-
 static
 struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *);
 static
@@ -513,11 +511,11 @@ end:
 struct bt_ctf_field *bt_ctf_field_structure_get_field_by_name(
                struct bt_ctf_field *field, const char *name)
 {
-       struct bt_ctf_field *new_field = NULL;
+       struct bt_ctf_field *ret = NULL;
        GQuark field_quark;
        struct bt_ctf_field_structure *structure;
-       struct bt_ctf_field_type *field_type = NULL;
        size_t index;
+       GHashTable *field_name_to_index;
 
        if (!field) {
                BT_LOGW_STR("Invalid parameter: field is NULL.");
@@ -538,61 +536,31 @@ struct bt_ctf_field *bt_ctf_field_structure_get_field_by_name(
                goto error;
        }
 
+       field_name_to_index =
+               container_of(field->type, struct bt_ctf_field_type_structure,
+                       parent)->field_name_to_index;
        field_quark = g_quark_from_string(name);
        structure = container_of(field, struct bt_ctf_field_structure, parent);
-       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,
+       if (!g_hash_table_lookup_extended(field_name_to_index,
                        GUINT_TO_POINTER(field_quark),
                        NULL, (gpointer *)&index)) {
-               BT_LOGW("Invalid parameter: no such field in structure field's type: "
-                       "struct-field-addr=%p, struct-ft-addr=%p, "
-                       "field-ft-addr=%p, name=\"%s\"",
-                       field, field->type, field_type, name);
-               goto error;
-       }
-
-       if (structure->fields->pdata[index]) {
-               new_field = structure->fields->pdata[index];
-               goto end;
-       }
-
-       /* We don't want to modify this field if it's frozen */
-       if (field->frozen) {
-               BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                       field);
-               goto end;
-       }
-
-       new_field = bt_ctf_field_create(field_type);
-       if (!new_field) {
-               BT_LOGW("Cannot create field: "
-                       "struct-field-addr=%p, struct-ft-addr=%p, "
-                       "field-ft-addr=%p, name=\"%s\"",
-                       field, field->type, field_type, name);
+               BT_LOGV("Invalid parameter: no such field in structure field's type: "
+                       "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"",
+                       field, field->type, name);
                goto error;
        }
 
-       structure->fields->pdata[index] = new_field;
-end:
-       bt_get(new_field);
+       ret = bt_get(structure->fields->pdata[index]);
+       assert(ret);
 error:
-       if (field_type) {
-               bt_put(field_type);
-       }
-       return new_field;
+       return ret;
 }
 
 struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index(
                struct bt_ctf_field *field, uint64_t 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;
+       struct bt_ctf_field *ret = NULL;
 
        if (!field) {
                BT_LOGW_STR("Invalid parameter: field is NULL.");
@@ -613,46 +581,15 @@ struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index(
                BT_LOGW("Invalid parameter: index is out of bounds: "
                        "addr=%p, index=%" PRIu64 ", count=%u",
                        field, index, structure->fields->len);
-               goto error;
-       }
-
-       ret_field = structure->fields->pdata[index];
-       if (ret_field) {
                goto end;
        }
 
-       /* We don't want to modify this field if it's frozen */
-       if (field->frozen) {
-               BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                       field);
-               goto end;
-       }
-
-       /* Field has not been instanciated yet, create it */
-       structure_type = bt_ctf_field_get_type(field);
-       assert(structure_type);
-       ret = bt_ctf_field_type_structure_get_field(structure_type,
-               &field_name, &field_type, index);
-       assert(ret == 0);
-       bt_put(structure_type);
-       ret_field = bt_ctf_field_create(field_type);
-       if (!ret_field) {
-               BT_LOGW("Cannot create field: "
-                       "struct-field-addr=%p, struct-ft-addr=%p, "
-                       "field-ft-addr=%p, index=%" PRIu64,
-                       field, field->type, field_type, index);
-               goto error;
-       }
-
-       structure->fields->pdata[index] = ret_field;
+       ret = bt_get(structure->fields->pdata[index]);
 end:
-       bt_get(ret_field);
-error:
-       bt_put(field_type);
-       return ret_field;
+       return ret;
 }
 
-int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
+int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field,
                const char *name, struct bt_ctf_field *value)
 {
        int ret = 0;
@@ -660,6 +597,7 @@ int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
        struct bt_ctf_field_structure *structure;
        struct bt_ctf_field_type *expected_field_type = NULL;
        size_t index;
+       GHashTable *field_name_to_index;
 
        if (!field) {
                BT_LOGW_STR("Invalid parameter: structure field is NULL.");
@@ -704,22 +642,21 @@ int bt_ctf_field_structure_set_field(struct bt_ctf_field *field,
                goto end;
        }
 
-       if (!g_hash_table_lookup_extended(structure->field_name_to_index,
-                       GUINT_TO_POINTER(field_quark), NULL, (gpointer *) &index)) {
-               BT_LOGW("Invalid parameter: no such field in structure field's type: "
+       field_name_to_index =
+               container_of(field->type, struct bt_ctf_field_type_structure,
+                       parent)->field_name_to_index;
+       if (!g_hash_table_lookup_extended(field_name_to_index,
+                       GUINT_TO_POINTER(field_quark), NULL,
+                       (gpointer *) &index)) {
+               BT_LOGV("Invalid parameter: no such field in structure field's type: "
                        "struct-field-addr=%p, struct-ft-addr=%p, "
                        "field-ft-addr=%p, name=\"%s\"",
                        field, field->type, value->type, name);
                ret = -1;
                goto end;
        }
-
-       if (structure->fields->pdata[index]) {
-               bt_put(structure->fields->pdata[index]);
-       }
-
-       structure->fields->pdata[index] = value;
        bt_get(value);
+       BT_MOVE(structure->fields->pdata[index], value);
 end:
        if (expected_field_type) {
                bt_put(expected_field_type);
@@ -764,8 +701,14 @@ struct bt_ctf_field *bt_ctf_field_array_get_field(struct bt_ctf_field *field,
 
        /* We don't want to modify this field if it's frozen */
        if (field->frozen) {
-               BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                       field);
+               /*
+                * Not logging a warning here because the user could
+                * legitimately check if a array field is set with
+                * this function: if the preconditions are satisfied,
+                * a NULL return value means this.
+                */
+               BT_LOGV("Not creating a field because array field is frozen: "
+                       "array-field-addr=%p, index=%" PRIu64, field, index);
                goto end;
        }
 
@@ -804,7 +747,7 @@ struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
 
        sequence = container_of(field, struct bt_ctf_field_sequence, parent);
        if (!sequence->elements) {
-               BT_LOGW("Sequence field's elements do not exist: addr=%p",
+               BT_LOGV("Sequence field's elements do not exist: addr=%p",
                        field);
                goto end;
        }
@@ -824,8 +767,14 @@ struct bt_ctf_field *bt_ctf_field_sequence_get_field(struct bt_ctf_field *field,
 
        /* We don't want to modify this field if it's frozen */
        if (field->frozen) {
-               BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                       field);
+               /*
+                * Not logging a warning here because the user could
+                * legitimately check if a sequence field is set with
+                * this function: if the preconditions are satisfied,
+                * a NULL return value means this.
+                */
+               BT_LOGV("Not creating a field because sequence field is frozen: "
+                       "sequence-field-addr=%p, index=%" PRIu64, field, index);
                goto end;
        }
 
@@ -927,8 +876,15 @@ struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field,
 
        /* We don't want to modify this field if it's frozen */
        if (field->frozen) {
-               BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                       field);
+               /*
+                * Not logging a warning here because the user could
+                * legitimately check if a variant field is set with
+                * this function: if the preconditions are satisfied,
+                * a NULL return value means this.
+                */
+               BT_LOGV("Not creating a field because variant field is frozen: "
+                       "variant-field-addr=%p, tag-field-addr=%p",
+                       field, tag_field);
                goto end;
        }
 
@@ -1048,8 +1004,15 @@ struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
        if (!enumeration->payload) {
                /* We don't want to modify this field if it's frozen */
                if (field->frozen) {
-                       BT_LOGW("Invalid parameter: field is frozen: addr=%p",
-                               field);
+                       /*
+                        * Not logging a warning here because the user
+                        * could legitimately check if an enumeration's
+                        * container field is set with this function: if
+                        * the preconditions are satisfied, a NULL
+                        * return value means this.
+                        */
+                       BT_LOGV("Not creating a field because enumeration field is frozen: "
+                               "enum-field-addr=%p", field);
                        goto end;
                }
 
@@ -1228,7 +1191,7 @@ int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field *field,
        }
 
        integer->payload.signd = value;
-       integer->parent.payload_set = 1;
+       integer->parent.payload_set = true;
 end:
        return ret;
 }
@@ -1338,7 +1301,7 @@ int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field *field,
        }
 
        integer->payload.unsignd = value;
-       integer->parent.payload_set = 1;
+       integer->parent.payload_set = true;
 end:
        return ret;
 }
@@ -1416,7 +1379,7 @@ int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field,
        floating_point = container_of(field, struct bt_ctf_field_floating_point,
                parent);
        floating_point->payload = value;
-       floating_point->parent.payload_set = 1;
+       floating_point->parent.payload_set = true;
 end:
        return ret;
 }
@@ -1494,7 +1457,7 @@ int bt_ctf_field_string_set_value(struct bt_ctf_field *field,
                string->payload = g_string_new(value);
        }
 
-       string->parent.payload_set = 1;
+       string->parent.payload_set = true;
 end:
        return ret;
 }
@@ -1542,7 +1505,7 @@ int bt_ctf_field_string_append(struct bt_ctf_field *field,
                string_field->payload = g_string_new(value);
        }
 
-       string_field->parent.payload_set = 1;
+       string_field->parent.payload_set = true;
 
 end:
        return ret;
@@ -1603,7 +1566,7 @@ int bt_ctf_field_string_append_len(struct bt_ctf_field *field,
                        effective_length);
        }
 
-       string_field->parent.payload_set = 1;
+       string_field->parent.payload_set = true;
 
 end:
        return ret;
@@ -1814,7 +1777,8 @@ struct bt_ctf_field *bt_ctf_field_structure_create(
                struct bt_ctf_field_type_structure, parent);
        struct bt_ctf_field_structure *structure = g_new0(
                struct bt_ctf_field_structure, 1);
-       struct bt_ctf_field *field = NULL;
+       struct bt_ctf_field *ret = NULL;
+       size_t i;
 
        BT_LOGD("Creating structure field object: ft-addr=%p", type);
 
@@ -1823,16 +1787,33 @@ struct bt_ctf_field *bt_ctf_field_structure_create(
                goto end;
        }
 
-       structure->field_name_to_index = structure_type->field_name_to_index;
        structure->fields = g_ptr_array_new_with_free_func(
-               (GDestroyNotify)bt_ctf_field_put);
+               (GDestroyNotify) bt_ctf_field_put);
        g_ptr_array_set_size(structure->fields,
-               g_hash_table_size(structure->field_name_to_index));
-       field = &structure->parent;
-       BT_LOGD("Created structure field object: addr=%p, ft-addr=%p",
-               field, type);
+               structure_type->fields->len);
+
+       /* Create all fields contained by the structure field. */
+       for (i = 0; i < structure_type->fields->len; i++) {
+               struct bt_ctf_field *field;
+               struct structure_field *field_type =
+                       g_ptr_array_index(structure_type->fields, i);
+
+               field = bt_ctf_field_create(field_type->type);
+               if (!field) {
+                       BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu",
+                               g_quark_to_string(field_type->name), i);
+                       bt_ctf_field_structure_destroy(&structure->parent);
+                       goto end;
+               }
+
+               g_ptr_array_index(structure->fields, i) = field;
+       }
+
+       ret = &structure->parent;
+       BT_LOGD("Created structure field object: addr=%p, ft-addr=%p", ret,
+               type);
 end:
-       return field;
+       return ret;
 }
 
 static
@@ -2214,7 +2195,7 @@ int bt_ctf_field_sequence_validate(struct bt_ctf_field *field)
                if (ret) {
                        BT_LOGW("Invalid sequence field's element field: "
                                "sequence-field-addr=%p, field-addr=%p, "
-                               "index=%" PRId64, field, elem_field, i);
+                               "index=%zu", field, elem_field, i);
                        goto end;
                }
        }
@@ -2233,7 +2214,7 @@ int bt_ctf_field_generic_reset(struct bt_ctf_field *field)
                goto end;
        }
 
-       field->payload_set = 0;
+       field->payload_set = false;
 end:
        return ret;
 }
@@ -2352,7 +2333,7 @@ int bt_ctf_field_array_reset(struct bt_ctf_field *field)
                if (ret) {
                        BT_LOGE("Failed to reset array field's field: "
                                "array-field-addr=%p, field-addr=%p, "
-                               "index=%" PRId64, field, member, i);
+                               "index=%zu", field, member, i);
                        goto end;
                }
        }
@@ -2389,7 +2370,7 @@ int bt_ctf_field_sequence_reset(struct bt_ctf_field *field)
                if (ret) {
                        BT_LOGE("Failed to reset sequence field's field: "
                                "sequence-field-addr=%p, field-addr=%p, "
-                               "index=%" PRId64, field, member, i);
+                               "index=%zu", field, member, i);
                        goto end;
                }
        }
@@ -2544,25 +2525,33 @@ int bt_ctf_field_structure_serialize(struct bt_ctf_field *field,
        for (i = 0; i < structure->fields->len; i++) {
                struct bt_ctf_field *member = g_ptr_array_index(
                        structure->fields, i);
+               const char *field_name = NULL;
+
+               if (BT_LOG_ON_WARN) {
+                       ret = bt_ctf_field_type_structure_get_field(
+                               field->type, &field_name, NULL, i);
+                       assert(ret == 0);
+               }
 
                BT_LOGV("Serializing structure field's field: pos-offset=%" PRId64 ", "
                        "field-addr=%p, index=%" PRId64,
                        pos->offset, member, i);
+
+               if (!member) {
+                       BT_LOGW("Cannot serialize structure field's field: field is not set: "
+                               "struct-field-addr=%p, "
+                               "field-name=\"%s\", index=%" PRId64,
+                               field, field_name, i);
+                       ret = -1;
+                       goto end;
+               }
+
                ret = bt_ctf_field_serialize(member, pos, native_byte_order);
                if (ret) {
-                       int this_ret;
-                       const char *name;
-                       struct bt_ctf_field_type *structure_type =
-                                       bt_ctf_field_get_type(field);
-
-                       this_ret = bt_ctf_field_type_structure_get_field(
-                               structure_type, &name, NULL, i);
-                       assert(this_ret == 0);
                        BT_LOGW("Cannot serialize structure field's field: "
                                "struct-field-addr=%p, field-addr=%p, "
                                "field-name=\"%s\", index=%" PRId64,
-                               field, member, name, i);
-                       bt_put(structure_type);
+                               field->type, member, field_name, i);
                        break;
                }
        }
@@ -2679,7 +2668,7 @@ int bt_ctf_field_string_serialize(struct bt_ctf_field *field,
 
                ret = bt_ctf_field_unsigned_integer_set_value(character, chr);
                if (ret) {
-                       BT_LOGE("Cannot set character field's value: "
+                       BT_LOGW("Cannot set character field's value: "
                                "pos-offset=%" PRId64 ", field-addr=%p, "
                                "index=%" PRId64 ", char-int=%" PRIu64,
                                pos->offset, character, i, chr);
@@ -2693,7 +2682,7 @@ int bt_ctf_field_string_serialize(struct bt_ctf_field *field,
                ret = bt_ctf_field_integer_serialize(character, pos,
                        native_byte_order);
                if (ret) {
-                       BT_LOGE_STR("Cannot serialize character field.");
+                       BT_LOGW_STR("Cannot serialize character field.");
                        goto end;
                }
        }
@@ -2775,8 +2764,6 @@ int bt_ctf_field_structure_copy(struct bt_ctf_field *src,
        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++) {
@@ -2785,7 +2772,7 @@ int bt_ctf_field_structure_copy(struct bt_ctf_field *src,
                struct bt_ctf_field *field_copy = NULL;
 
                if (field) {
-                       BT_LOGD("Copying structure field's field: src-field-addr=%p",
+                       BT_LOGD("Copying structure field's field: src-field-addr=%p"
                                "index=%" PRId64, field, i);
                        field_copy = bt_ctf_field_copy(field);
                        if (!field_copy) {
@@ -2797,7 +2784,7 @@ int bt_ctf_field_structure_copy(struct bt_ctf_field *src,
                        }
                }
 
-               g_ptr_array_index(struct_dst->fields, i) = field_copy;
+               BT_MOVE(g_ptr_array_index(struct_dst->fields, i), field_copy);
        }
 
        BT_LOGD_STR("Copied structure field.");
@@ -2992,8 +2979,8 @@ int increase_packet_size(struct bt_ctf_stream_pos *pos)
                pos->offset, pos->packet_size);
        ret = munmap_align(pos->base_mma);
        if (ret) {
-               BT_LOGE("Failed to perform an aligned memory unmapping: "
-                       "ret=%d, errno=%d", ret, errno);
+               BT_LOGE_ERRNO("Failed to perform an aligned memory unmapping",
+                       ": ret=%d", ret);
                goto end;
        }
 
@@ -3003,8 +2990,8 @@ int increase_packet_size(struct bt_ctf_stream_pos *pos)
                        pos->packet_size / CHAR_BIT);
        } while (ret == EINTR);
        if (ret) {
-               BT_LOGE("Failed to preallocate memory space: ret=%d, errno=%d",
-                       ret, errno);
+               BT_LOGE_ERRNO("Failed to preallocate memory space",
+                       ": ret=%d", ret);
                errno = EINTR;
                ret = -1;
                goto end;
@@ -3013,14 +3000,16 @@ int increase_packet_size(struct bt_ctf_stream_pos *pos)
        pos->base_mma = mmap_align(pos->packet_size / CHAR_BIT, pos->prot,
                pos->flags, pos->fd, pos->mmap_offset);
        if (pos->base_mma == MAP_FAILED) {
-               BT_LOGE("Failed to perform an aligned memory mapping: "
-                       "ret=%d, errno=%d", ret, errno);
+               BT_LOGE_ERRNO("Failed to perform an aligned memory mapping",
+                       ": ret=%d", ret);
                ret = -1;
        }
 
        BT_LOGV("Increased packet size: pos-offset=%" PRId64 ", "
                "new-packet-size=%" PRIu64,
                pos->offset, pos->packet_size);
+       assert(pos->packet_size % 8 == 0);
+
 end:
        return ret;
 }
@@ -3028,7 +3017,7 @@ end:
 static
 void generic_field_freeze(struct bt_ctf_field *field)
 {
-       field->frozen = 1;
+       field->frozen = true;
 }
 
 static
This page took 0.046041 seconds and 4 git commands to generate.