ctf.fs: bt_ctf_notif_iter_create(): assert() that all medops exist
[babeltrace.git] / formats / ctf / ir / field-types.c
index 4f2bc03449bd438c5d3f648907aab4c66c9d6dac..02a47b366d900cb0d5e1aa29019ab5a00558d480 100644 (file)
@@ -30,8 +30,8 @@
 #include <babeltrace/ctf-ir/field-path-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/ref.h>
-#include <babeltrace/ctf-ir/clock.h>
-#include <babeltrace/ctf-ir/clock-internal.h>
+#include <babeltrace/ctf-ir/clock-class.h>
+#include <babeltrace/ctf-ir/clock-class-internal.h>
 #include <babeltrace/ctf-writer/writer-internal.h>
 #include <babeltrace/object-internal.h>
 #include <babeltrace/ref.h>
@@ -445,6 +445,71 @@ end:
        return ret;
 }
 
+static
+struct enumeration_mapping *get_enumeration_mapping(
+               struct bt_ctf_field_type *type, int index)
+{
+       struct enumeration_mapping *mapping = NULL;
+       struct bt_ctf_field_type_enumeration *enumeration;
+
+       enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
+               parent);
+       if (index >= enumeration->entries->len) {
+               goto end;
+       }
+
+       mapping = g_ptr_array_index(enumeration->entries, index);
+end:
+       return mapping;
+}
+
+/*
+ * Note: This algorithm is O(n^2) vs number of enumeration mappings.
+ * Only used when freezing an enumeration.
+ */
+static
+void set_enumeration_range_overlap(
+               struct bt_ctf_field_type *type)
+{
+       int i, j, len;
+       struct bt_ctf_field_type *container_type;
+       struct bt_ctf_field_type_enumeration *enumeration_type;
+       int is_signed;
+
+       enumeration_type = container_of(type,
+                       struct bt_ctf_field_type_enumeration, parent);
+
+       len = enumeration_type->entries->len;
+       container_type = enumeration_type->container;
+       is_signed = bt_ctf_field_type_integer_get_signed(container_type);
+
+       for (i = 0; i < len; i++) {
+               for (j = i + 1; j < len; j++) {
+                       struct enumeration_mapping *mapping[2];
+
+                       mapping[0] = get_enumeration_mapping(type, i);
+                       mapping[1] = get_enumeration_mapping(type, j);
+                       if (is_signed) {
+                               if (mapping[0]->range_start._signed
+                                                       <= mapping[1]->range_end._signed
+                                               && mapping[0]->range_end._signed
+                                                       >= mapping[1]->range_start._signed) {
+                                       enumeration_type->has_overlapping_ranges = true;
+                                       return;
+                               }
+                       } else {
+                               if (mapping[0]->range_start._unsigned
+                                                       <= mapping[1]->range_end._unsigned
+                                               && mapping[0]->range_end._unsigned
+                                                       >= mapping[1]->range_start._unsigned) {
+                                       enumeration_type->has_overlapping_ranges = true;
+                                       return;
+                               }
+                       }
+               }
+       }
+}
+
 static
 int bt_ctf_field_type_enumeration_validate(struct bt_ctf_field_type *type)
 {
@@ -557,6 +622,43 @@ end:
        return ret;
 }
 
+static
+bool bt_ctf_field_type_enumeration_has_overlapping_ranges(
+               struct bt_ctf_field_type_enumeration *enumeration_type)
+{
+       if (!enumeration_type->parent.frozen) {
+               set_enumeration_range_overlap(&enumeration_type->parent);
+       }
+       return enumeration_type->has_overlapping_ranges;
+}
+
+static
+int bt_ctf_field_type_enumeration_get_mapping_name(
+               struct bt_ctf_field_type *enum_field_type,
+               int index,
+               const char **mapping_name)
+{
+       int ret = 0;
+       struct enumeration_mapping *mapping;
+
+       if (!enum_field_type || index < 0) {
+               ret = -1;
+               goto end;
+       }
+
+       mapping = get_enumeration_mapping(enum_field_type, index);
+       if (!mapping) {
+               ret = -1;
+               goto end;
+       }
+
+       if (mapping_name) {
+               *mapping_name = g_quark_to_string(mapping->string);
+       }
+end:
+       return ret;
+}
+
 static
 int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type)
 {
@@ -574,6 +676,12 @@ int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type)
                goto end;
        }
 
+       if (bt_ctf_field_type_enumeration_has_overlapping_ranges(
+                       variant->tag)) {
+               ret = -1;
+               goto end;
+       }
+
        tag_mappings_count =
                bt_ctf_field_type_enumeration_get_mapping_count(
                        (struct bt_ctf_field_type *) variant->tag);
@@ -585,12 +693,11 @@ int bt_ctf_field_type_variant_validate(struct bt_ctf_field_type *type)
 
        for (i = 0; i < tag_mappings_count; ++i) {
                const char *label;
-               int64_t range_start, range_end;
                struct bt_ctf_field_type *ft;
 
-               ret = bt_ctf_field_type_enumeration_get_mapping(
+               ret = bt_ctf_field_type_enumeration_get_mapping_name(
                        (struct bt_ctf_field_type *) variant->tag,
-                       i, &label, &range_start, &range_end);
+                       i, &label);
                if (ret) {
                        goto end;
                }
@@ -820,39 +927,329 @@ end:
        return ret;
 }
 
-struct bt_ctf_clock *bt_ctf_field_type_integer_get_mapped_clock(
+struct bt_ctf_clock_class *bt_ctf_field_type_integer_get_mapped_clock_class(
                struct bt_ctf_field_type *type)
 {
        struct bt_ctf_field_type_integer *integer;
-       struct bt_ctf_clock *clock = NULL;
+       struct bt_ctf_clock_class *clock_class = NULL;
 
        if (!type) {
                goto end;
        }
 
        integer = container_of(type, struct bt_ctf_field_type_integer, parent);
-       clock = integer->mapped_clock;
-       bt_get(clock);
+       clock_class = integer->mapped_clock;
+       bt_get(clock_class);
 end:
-       return clock;
+       return clock_class;
 }
 
-int bt_ctf_field_type_integer_set_mapped_clock(
+int bt_ctf_field_type_integer_set_mapped_clock_class(
                struct bt_ctf_field_type *type,
-               struct bt_ctf_clock *clock)
+               struct bt_ctf_clock_class *clock_class)
 {
        struct bt_ctf_field_type_integer *integer;
        int ret = 0;
 
-       if (!type || type->frozen) {
+       if (!type || type->frozen || !bt_ctf_clock_class_is_valid(clock_class)) {
                ret = -1;
                goto end;
        }
 
        integer = container_of(type, struct bt_ctf_field_type_integer, parent);
        bt_put(integer->mapped_clock);
-       bt_get(clock);
-       integer->mapped_clock = clock;
+       integer->mapped_clock = bt_get(clock_class);
+end:
+       return ret;
+}
+
+static
+void bt_ctf_field_type_enum_iter_destroy(struct bt_object *obj)
+{
+       struct bt_ctf_field_type_enumeration_mapping_iterator *iter =
+               container_of(obj,
+                       struct bt_ctf_field_type_enumeration_mapping_iterator,
+                       base);
+
+       bt_put(&iter->enumeration_type->parent);
+       g_free(iter);
+}
+
+static
+struct bt_ctf_field_type_enumeration_mapping_iterator *
+bt_ctf_field_type_enumeration_find_mappings_type(
+               struct bt_ctf_field_type *type,
+               enum bt_ctf_field_type_enumeration_mapping_iterator_type iterator_type)
+{
+       struct bt_ctf_field_type_enumeration *enumeration_type;
+       struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
+
+       if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) {
+               goto end;
+       }
+
+       enumeration_type = container_of(type,
+               struct bt_ctf_field_type_enumeration, parent);
+       iter = g_new0(struct bt_ctf_field_type_enumeration_mapping_iterator, 1);
+       if (!iter) {
+               goto end;
+       }
+
+       bt_object_init(&iter->base, bt_ctf_field_type_enum_iter_destroy);
+       bt_get(type);
+       iter->enumeration_type = enumeration_type;
+       iter->index = -1;
+       iter->type = iterator_type;
+end:
+       return iter;
+}
+
+struct bt_ctf_field_type_enumeration_mapping_iterator *
+bt_ctf_field_type_enumeration_find_mappings_by_name(
+               struct bt_ctf_field_type *type, const char *name)
+{
+       struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
+
+       iter = bt_ctf_field_type_enumeration_find_mappings_type(
+                       type, ITERATOR_BY_NAME);
+       if (!iter) {
+               goto error;
+       }
+
+       iter->u.name_quark = g_quark_try_string(name);
+       if (!iter->u.name_quark) {
+               goto error;
+       }
+
+       /* Advance iterator to first entry, or leave index at -1. */
+       if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
+               /* No entry found. */
+               goto error;
+       }
+
+       return iter;
+error:
+       bt_put(iter);
+       return NULL;
+}
+
+int bt_ctf_field_type_enumeration_mapping_iterator_next(
+               struct bt_ctf_field_type_enumeration_mapping_iterator *iter)
+{
+       struct bt_ctf_field_type_enumeration *enumeration;
+       struct bt_ctf_field_type *type;
+       int i, ret = 0, len;
+
+       enumeration = iter->enumeration_type;
+       type = &enumeration->parent;
+       len = enumeration->entries->len;
+       for (i = iter->index + 1; i < len; i++) {
+               struct enumeration_mapping *mapping =
+                       get_enumeration_mapping(type, i);
+
+               switch (iter->type) {
+               case ITERATOR_BY_NAME:
+                       if (mapping->string == iter->u.name_quark) {
+                               iter->index = i;
+                               goto end;
+                       }
+                       break;
+               case ITERATOR_BY_SIGNED_VALUE:
+               {
+                       int64_t value = iter->u.signed_value;
+
+                       if (value >= mapping->range_start._signed &&
+                                       value <= mapping->range_end._signed) {
+                               iter->index = i;
+                               goto end;
+                       }
+                       break;
+               }
+               case ITERATOR_BY_UNSIGNED_VALUE:
+               {
+                       uint64_t value = iter->u.unsigned_value;
+
+                       if (value >= mapping->range_start._unsigned &&
+                                       value <= mapping->range_end._unsigned) {
+                               iter->index = i;
+                               goto end;
+                       }
+                       break;
+               }
+               default:
+                       abort();
+               }
+       }
+
+       ret = -1;
+end:
+       return ret;
+}
+
+struct bt_ctf_field_type_enumeration_mapping_iterator *
+bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
+               struct bt_ctf_field_type *type, int64_t value)
+{
+       struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
+
+       iter = bt_ctf_field_type_enumeration_find_mappings_type(
+                       type, ITERATOR_BY_SIGNED_VALUE);
+       if (!iter) {
+               goto error;
+       }
+
+       if (bt_ctf_field_type_integer_get_signed(
+                       iter->enumeration_type->container) != 1) {
+               goto error;
+       }
+       iter->u.signed_value = value;
+
+       /* Advance iterator to first entry, or leave index at -1. */
+       if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
+               /* No entry found. */
+               goto error;
+       }
+
+       return iter;
+error:
+       bt_put(iter);
+       return NULL;
+}
+
+struct bt_ctf_field_type_enumeration_mapping_iterator *
+bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
+               struct bt_ctf_field_type *type, uint64_t value)
+{
+       struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
+
+       iter = bt_ctf_field_type_enumeration_find_mappings_type(
+                       type, ITERATOR_BY_UNSIGNED_VALUE);
+       if (!iter) {
+               goto error;
+       }
+
+       if (bt_ctf_field_type_integer_get_signed(
+                       iter->enumeration_type->container) != 0) {
+               goto error;
+       }
+       iter->u.unsigned_value = value;
+
+       /* Advance iterator to first entry, or leave index at -1. */
+       if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter)) {
+               /* No entry found. */
+               goto error;
+       }
+
+       return iter;
+error:
+       bt_put(iter);
+       return NULL;
+}
+
+int bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
+               struct bt_ctf_field_type_enumeration_mapping_iterator *iter,
+               const char **mapping_name, int64_t *range_begin,
+               int64_t *range_end)
+{
+       int ret = 0;
+
+       if (!iter) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_enumeration_get_mapping_signed(
+                       &iter->enumeration_type->parent, iter->index,
+                       mapping_name, range_begin, range_end);
+end:
+       return ret;
+}
+
+int bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(
+               struct bt_ctf_field_type_enumeration_mapping_iterator *iter,
+               const char **mapping_name, uint64_t *range_begin,
+               uint64_t *range_end)
+{
+       int ret = 0;
+
+       if (!iter) {
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_enumeration_get_mapping_unsigned(
+                       &iter->enumeration_type->parent, iter->index,
+                       mapping_name, range_begin, range_end);
+end:
+       return ret;
+}
+
+int bt_ctf_field_type_enumeration_get_mapping_signed(
+               struct bt_ctf_field_type *enum_field_type,
+               int index,
+               const char **mapping_name, int64_t *range_begin,
+               int64_t *range_end)
+{
+       int ret = 0;
+       struct enumeration_mapping *mapping;
+
+       if (!enum_field_type || index < 0) {
+               ret = -1;
+               goto end;
+       }
+
+       mapping = get_enumeration_mapping(enum_field_type, index);
+       if (!mapping) {
+               ret = -1;
+               goto end;
+       }
+
+       if (mapping_name) {
+               *mapping_name = g_quark_to_string(mapping->string);
+       }
+
+       if (range_begin) {
+               *range_begin = mapping->range_start._signed;
+       }
+
+       if (range_end) {
+               *range_end = mapping->range_end._signed;
+       }
+end:
+       return ret;
+}
+
+int bt_ctf_field_type_enumeration_get_mapping_unsigned(
+               struct bt_ctf_field_type *enum_field_type,
+               int index,
+               const char **mapping_name, uint64_t *range_begin,
+               uint64_t *range_end)
+{
+       int ret = 0;
+       struct enumeration_mapping *mapping;
+
+       if (!enum_field_type || index < 0) {
+               ret = -1;
+               goto end;
+       }
+
+       mapping = get_enumeration_mapping(enum_field_type, index);
+       if (!mapping) {
+               ret = -1;
+               goto end;
+       }
+
+       if (mapping_name) {
+               *mapping_name = g_quark_to_string(mapping->string);
+       }
+
+       if (range_begin) {
+               *range_begin = mapping->range_start._unsigned;
+       }
+
+       if (range_end) {
+               *range_end = mapping->range_end._unsigned;
+       }
 end:
        return ret;
 }
@@ -918,7 +1315,6 @@ int bt_ctf_field_type_enumeration_add_mapping(
        GQuark mapping_name;
        struct enumeration_mapping *mapping;
        struct bt_ctf_field_type_enumeration *enumeration;
-       struct range_overlap_query query;
        char *escaped_string;
 
        if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM) ||
@@ -939,31 +1335,19 @@ int bt_ctf_field_type_enumeration_add_mapping(
                goto end;
        }
 
-       mapping_name = g_quark_from_string(escaped_string);
-       query = (struct range_overlap_query) {
-               .range_start._signed = range_start,
-               .range_end._signed = range_end,
-               .mapping_name = mapping_name,
-               .overlaps = 0 };
-       enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
-               parent);
-
-       /* Check that the range does not overlap with one already present */
-       g_ptr_array_foreach(enumeration->entries, check_ranges_overlap, &query);
-       if (query.overlaps) {
-               ret = -1;
-               goto error_free;
-       }
-
        mapping = g_new(struct enumeration_mapping, 1);
        if (!mapping) {
                ret = -1;
                goto error_free;
        }
-
+       mapping_name = g_quark_from_string(escaped_string);
        *mapping = (struct enumeration_mapping) {
                .range_start._signed = range_start,
-               .range_end._signed = range_end, .string = mapping_name};
+               .range_end._signed = range_end,
+               .string =  mapping_name,
+       };
+       enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
+               parent);
        g_ptr_array_add(enumeration->entries, mapping);
        g_ptr_array_sort(enumeration->entries,
                (GCompareFunc)compare_enumeration_mappings_signed);
@@ -981,7 +1365,6 @@ int bt_ctf_field_type_enumeration_add_mapping_unsigned(
        GQuark mapping_name;
        struct enumeration_mapping *mapping;
        struct bt_ctf_field_type_enumeration *enumeration;
-       struct range_overlap_query query;
        char *escaped_string;
 
        if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM) ||
@@ -1002,32 +1385,19 @@ int bt_ctf_field_type_enumeration_add_mapping_unsigned(
                goto end;
        }
 
-       mapping_name = g_quark_from_string(escaped_string);
-       query = (struct range_overlap_query) {
-               .range_start._unsigned = range_start,
-               .range_end._unsigned = range_end,
-               .mapping_name = mapping_name,
-               .overlaps = 0 };
-       enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
-               parent);
-
-       /* Check that the range does not overlap with one already present */
-       g_ptr_array_foreach(enumeration->entries, check_ranges_overlap_unsigned,
-               &query);
-       if (query.overlaps) {
-               ret = -1;
-               goto error_free;
-       }
-
        mapping = g_new(struct enumeration_mapping, 1);
        if (!mapping) {
                ret = -1;
                goto error_free;
        }
-
+       mapping_name = g_quark_from_string(escaped_string);
        *mapping = (struct enumeration_mapping) {
                .range_start._unsigned = range_start,
-               .range_end._unsigned = range_end, .string = mapping_name};
+               .range_end._unsigned = range_end,
+               .string = mapping_name,
+       };
+       enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
+               parent);
        g_ptr_array_add(enumeration->entries, mapping);
        g_ptr_array_sort(enumeration->entries,
                (GCompareFunc)compare_enumeration_mappings_unsigned);
@@ -1037,51 +1407,6 @@ end:
        return ret;
 }
 
-const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
-               struct bt_ctf_field_type_enumeration *enumeration_type,
-               uint64_t value)
-{
-       const char *name = NULL;
-       struct range_overlap_query query =
-               (struct range_overlap_query) {
-               .range_start._unsigned = value,
-               .range_end._unsigned = value,
-               .overlaps = 0 };
-
-       g_ptr_array_foreach(enumeration_type->entries,
-               check_ranges_overlap_unsigned,
-               &query);
-       if (!query.overlaps) {
-               goto end;
-       }
-
-       name = g_quark_to_string(query.mapping_name);
-end:
-       return name;
-}
-
-const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
-               struct bt_ctf_field_type_enumeration *enumeration_type,
-               int64_t value)
-{
-       const char *name = NULL;
-       struct range_overlap_query query =
-               (struct range_overlap_query) {
-               .range_start._signed = value,
-               .range_end._signed = value,
-               .overlaps = 0 };
-
-       g_ptr_array_foreach(enumeration_type->entries, check_ranges_overlap,
-               &query);
-       if (!query.overlaps) {
-               goto end;
-       }
-
-       name = g_quark_to_string(query.mapping_name);
-end:
-       return name;
-}
-
 int bt_ctf_field_type_enumeration_get_mapping_count(
                struct bt_ctf_field_type *type)
 {
@@ -1100,170 +1425,6 @@ end:
        return ret;
 }
 
-static inline
-struct enumeration_mapping *get_enumeration_mapping(
-       struct bt_ctf_field_type *type, int index)
-{
-       struct enumeration_mapping *mapping = NULL;
-       struct bt_ctf_field_type_enumeration *enumeration;
-
-       enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
-               parent);
-       if (index >= enumeration->entries->len) {
-               goto end;
-       }
-
-       mapping = g_ptr_array_index(enumeration->entries, index);
-end:
-       return mapping;
-}
-
-int bt_ctf_field_type_enumeration_get_mapping(
-               struct bt_ctf_field_type *type, int index,
-               const char **string, int64_t *range_start, int64_t *range_end)
-{
-       struct enumeration_mapping *mapping;
-       int ret = 0;
-
-       if (!type || index < 0 || !string || !range_start || !range_end ||
-               (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) {
-               ret = -1;
-               goto end;
-       }
-
-       mapping = get_enumeration_mapping(type, index);
-       if (!mapping) {
-               ret = -1;
-               goto end;
-       }
-
-       *string = g_quark_to_string(mapping->string);
-       *range_start = mapping->range_start._signed;
-       *range_end = mapping->range_end._signed;
-end:
-       return ret;
-}
-
-int bt_ctf_field_type_enumeration_get_mapping_unsigned(
-               struct bt_ctf_field_type *type, int index,
-               const char **string, uint64_t *range_start, uint64_t *range_end)
-{
-       struct enumeration_mapping *mapping;
-       int ret = 0;
-
-       if (!type || index < 0 || !string || !range_start || !range_end ||
-               (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) {
-               ret = -1;
-               goto end;
-       }
-
-       mapping = get_enumeration_mapping(type, index);
-       if (!mapping) {
-               ret = -1;
-               goto end;
-       }
-
-       *string = g_quark_to_string(mapping->string);
-       *range_start = mapping->range_start._unsigned;
-       *range_end = mapping->range_end._unsigned;
-end:
-       return ret;
-}
-
-int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
-               struct bt_ctf_field_type *type, const char *name)
-{
-       GQuark name_quark;
-       struct bt_ctf_field_type_enumeration *enumeration;
-       int i, ret = 0;
-
-       if (!type || !name ||
-               (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) {
-               ret = -1;
-               goto end;
-       }
-
-       name_quark = g_quark_try_string(name);
-       if (!name_quark) {
-               ret = -1;
-               goto end;
-       }
-
-       enumeration = container_of(type,
-               struct bt_ctf_field_type_enumeration, parent);
-       for (i = 0; i < enumeration->entries->len; i++) {
-               struct enumeration_mapping *mapping =
-                       get_enumeration_mapping(type, i);
-
-               if (mapping->string == name_quark) {
-                       ret = i;
-                       goto end;
-               }
-       }
-
-       ret = -1;
-end:
-       return ret;
-}
-
-int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
-               struct bt_ctf_field_type *type, int64_t value)
-{
-       struct bt_ctf_field_type_enumeration *enumeration;
-       int i, ret = 0;
-
-       if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) {
-               ret = -1;
-               goto end;
-       }
-
-       enumeration = container_of(type,
-               struct bt_ctf_field_type_enumeration, parent);
-       for (i = 0; i < enumeration->entries->len; i++) {
-               struct enumeration_mapping *mapping =
-                       get_enumeration_mapping(type, i);
-
-               if (value >= mapping->range_start._signed &&
-                       value <= mapping->range_end._signed) {
-                       ret = i;
-                       goto end;
-               }
-       }
-
-       ret = -1;
-end:
-       return ret;
-}
-
-int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
-               struct bt_ctf_field_type *type, uint64_t value)
-{
-       struct bt_ctf_field_type_enumeration *enumeration;
-       int i, ret = 0;
-
-       if (!type || (type->declaration->id != BT_CTF_TYPE_ID_ENUM)) {
-               ret = -1;
-               goto end;
-       }
-
-       enumeration = container_of(type,
-               struct bt_ctf_field_type_enumeration, parent);
-       for (i = 0; i < enumeration->entries->len; i++) {
-               struct enumeration_mapping *mapping =
-                       get_enumeration_mapping(type, i);
-
-               if (value >= mapping->range_start._unsigned &&
-                       value <= mapping->range_end._unsigned) {
-                       ret = i;
-                       goto end;
-               }
-       }
-
-       ret = -1;
-end:
-       return ret;
-}
-
 struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void)
 {
        struct bt_ctf_field_type_floating_point *floating_point =
@@ -1410,9 +1571,14 @@ int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type,
        int ret = 0;
        struct bt_ctf_field_type_structure *structure;
 
+       /*
+        * TODO: check that `field_type` does not contain `type`,
+        *       recursively.
+        */
        if (!type || !field_type || type->frozen ||
                bt_ctf_validate_identifier(field_name) ||
-               (type->declaration->id != BT_CTF_TYPE_ID_STRUCT)) {
+               (type->declaration->id != BT_CTF_TYPE_ID_STRUCT) ||
+               type == field_type) {
                ret = -1;
                goto end;
        }
@@ -1615,9 +1781,14 @@ int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type,
        struct bt_ctf_field_type_variant *variant;
        GQuark field_name_quark = g_quark_from_string(field_name);
 
+       /*
+        * TODO: check that `field_type` does not contain `type`,
+        *       recursively.
+        */
        if (!type || !field_type || type->frozen ||
                bt_ctf_validate_identifier(field_name) ||
-               (type->declaration->id != BT_CTF_TYPE_ID_VARIANT)) {
+               (type->declaration->id != BT_CTF_TYPE_ID_VARIANT) ||
+               type == field_type) {
                ret = -1;
                goto end;
        }
@@ -1691,22 +1862,30 @@ struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag(
                struct bt_ctf_field_type *type,
                struct bt_ctf_field *tag)
 {
+       int ret;
        const char *enum_value;
        struct bt_ctf_field_type *field_type = NULL;
+       struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
 
        if (!type || !tag || type->declaration->id != BT_CTF_TYPE_ID_VARIANT) {
                goto end;
        }
 
-       enum_value = bt_ctf_field_enumeration_get_mapping_name(tag);
-       if (!enum_value) {
+       iter = bt_ctf_field_enumeration_get_mappings(tag);
+       if (!iter) {
+               goto end;
+       }
+
+       ret = bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter,
+               &enum_value, NULL, NULL);
+       if (ret) {
                goto end;
        }
 
-       /* Already increments field_type's reference count */
        field_type = bt_ctf_field_type_variant_get_field_type_by_name(
                type, enum_value);
 end:
+       bt_put(iter);
        return field_type;
 }
 
@@ -2140,6 +2319,13 @@ enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order(
                ret = integer->user_byte_order;
                break;
        }
+       case BT_CTF_TYPE_ID_ENUM:
+       {
+               struct bt_ctf_field_type_enumeration *enum_ft = container_of(
+                       type, struct bt_ctf_field_type_enumeration, parent);
+               ret = bt_ctf_field_type_get_byte_order(enum_ft->container);
+               break;
+       }
        case BT_CTF_TYPE_ID_FLOAT:
        {
                struct bt_ctf_field_type_floating_point *floating_point =
@@ -2281,7 +2467,9 @@ struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed(
        struct range_overlap_query query = {
                .range_start._signed = tag_value,
                .range_end._signed = tag_value,
-               .mapping_name = 0, .overlaps = 0};
+               .mapping_name = 0,
+               .overlaps = 0,
+       };
 
        g_ptr_array_foreach(variant->tag->entries, check_ranges_overlap,
                &query);
@@ -2313,7 +2501,9 @@ struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned(
        struct range_overlap_query query = {
                .range_start._unsigned = tag_value,
                .range_end._unsigned = tag_value,
-               .mapping_name = 0, .overlaps = 0};
+               .mapping_name = 0,
+               .overlaps = 0,
+       };
 
        g_ptr_array_foreach(variant->tag->entries,
                check_ranges_overlap_unsigned,
@@ -2372,7 +2562,6 @@ void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type *type,
        }
 }
 
-BT_HIDDEN
 struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *type)
 {
        struct bt_ctf_field_type *copy = NULL;
@@ -2710,7 +2899,7 @@ void bt_ctf_field_type_integer_freeze(struct bt_ctf_field_type *type)
                type, struct bt_ctf_field_type_integer, parent);
 
        if (integer_type->mapped_clock) {
-               bt_ctf_clock_freeze(integer_type->mapped_clock);
+               bt_ctf_clock_class_freeze(integer_type->mapped_clock);
        }
 
        generic_field_type_freeze(type);
@@ -2722,6 +2911,8 @@ void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type)
        struct bt_ctf_field_type_enumeration *enumeration_type = container_of(
                type, struct bt_ctf_field_type_enumeration, parent);
 
+       set_enumeration_range_overlap(type);
+
        generic_field_type_freeze(type);
        bt_ctf_field_type_freeze(enumeration_type->container);
 }
@@ -2845,7 +3036,7 @@ int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type,
                get_integer_base_string(integer->declaration.base),
                get_byte_order_string(integer->declaration.byte_order));
        if (integer->mapped_clock) {
-               const char *clock_name = bt_ctf_clock_get_name(
+               const char *clock_name = bt_ctf_clock_class_get_name(
                        integer->mapped_clock);
 
                if (!clock_name) {
@@ -3355,7 +3546,7 @@ struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy(
        for (i = 0; i < enumeration->entries->len; i++) {
                struct enumeration_mapping *mapping = g_ptr_array_index(
                        enumeration->entries, i);
-               struct enumeration_mappingcopy_mapping = g_new0(
+               struct enumeration_mapping *copy_mapping = g_new0(
                        struct enumeration_mapping, 1);
 
                if (!copy_mapping) {
This page took 0.034201 seconds and 4 git commands to generate.