namespace the struct functions
[babeltrace.git] / types / variant.c
index bb4faa17093a87ec0266f7f08349922037105e1b..47c821ecd9fb6b1619499eb7a018004175b376a9 100644 (file)
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
  */
 
 #include <babeltrace/compiler.h>
 #include <babeltrace/format.h>
+#include <babeltrace/types.h>
 #include <errno.h>
 
 static
@@ -54,7 +63,7 @@ void _untagged_variant_declaration_free(struct declaration *declaration)
                struct declaration_field *declaration_field =
                        &g_array_index(untagged_variant_declaration->fields,
                                       struct declaration_field, i);
-               declaration_unref(declaration_field->declaration);
+               bt_declaration_unref(declaration_field->declaration);
        }
        g_array_free(untagged_variant_declaration->fields, true);
        g_free(untagged_variant_declaration);
@@ -89,8 +98,9 @@ void _variant_declaration_free(struct declaration *declaration)
        struct declaration_variant *variant_declaration =
                container_of(declaration, struct declaration_variant, p);
 
-       _untagged_variant_declaration_free(&variant_declaration->untagged_variant->p);
+       bt_declaration_unref(&variant_declaration->untagged_variant->p);
        g_array_free(variant_declaration->tag_name, TRUE);
+       g_free(variant_declaration);
 }
 
 struct declaration_variant *
@@ -102,9 +112,9 @@ struct declaration_variant *
        variant_declaration = g_new(struct declaration_variant, 1);
        declaration = &variant_declaration->p;
        variant_declaration->untagged_variant = untagged_variant;
-       declaration_ref(&untagged_variant->p);
+       bt_declaration_ref(&untagged_variant->p);
        variant_declaration->tag_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
-       append_scope_path(tag, variant_declaration->tag_name);
+       bt_append_scope_path(tag, variant_declaration->tag_name);
        declaration->id = CTF_TYPE_VARIANT;
        declaration->alignment = 1;
        declaration->declaration_free = _variant_declaration_free;
@@ -134,14 +144,14 @@ int check_enum_tag(struct definition_variant *variant,
         * variant choice map to an enumerator too. We then validate that the
         * number of enumerators equals the number of variant choices.
         */
-       if (variant->declaration->untagged_variant->fields->len != enum_get_nr_enumerators(enum_declaration))
+       if (variant->declaration->untagged_variant->fields->len != bt_enum_get_nr_enumerators(enum_declaration))
                return -EPERM;
 
        for (i = 0; i < variant->declaration->untagged_variant->fields->len; i++) {
                struct declaration_field *field_declaration =
                        &g_array_index(variant->declaration->untagged_variant->fields,
                                       struct declaration_field, i);
-               if (!enum_quark_to_range_set(enum_declaration, field_declaration->name)) {
+               if (!bt_enum_quark_to_range_set(enum_declaration, field_declaration->name)) {
                        missing_field = 1;
                        break;
                }
@@ -175,7 +185,7 @@ struct definition *
        int ret;
 
        variant = g_new(struct definition_variant, 1);
-       declaration_ref(&variant_declaration->p);
+       bt_declaration_ref(&variant_declaration->p);
        variant->p.declaration = declaration;
        variant->declaration = variant_declaration;
        variant->p.ref = 1;
@@ -199,7 +209,7 @@ struct definition *
        if (!variant->enum_tag
            || check_enum_tag(variant, variant->enum_tag) < 0)
                goto error;
-       definition_ref(variant->enum_tag);
+       bt_definition_ref(variant->enum_tag);
        variant->fields = g_ptr_array_sized_new(variant_declaration->untagged_variant->fields->len);
        g_ptr_array_set_size(variant->fields, variant_declaration->untagged_variant->fields->len);
        for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
@@ -223,7 +233,7 @@ struct definition *
        return &variant->p;
 error:
        free_definition_scope(variant->p.scope);
-       declaration_unref(&variant_declaration->p);
+       bt_declaration_unref(&variant_declaration->p);
        g_free(variant);
        return NULL;
 }
@@ -238,11 +248,12 @@ void _variant_definition_free(struct definition *definition)
        assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
        for (i = 0; i < variant->fields->len; i++) {
                struct definition *field = g_ptr_array_index(variant->fields, i);
-               definition_unref(field);
+               bt_definition_unref(field);
        }
-       definition_unref(variant->enum_tag);
+       bt_definition_unref(variant->enum_tag);
        free_definition_scope(variant->p.scope);
-       declaration_unref(variant->p.declaration);
+       bt_declaration_unref(variant->p.declaration);
+       g_ptr_array_free(variant->fields, TRUE);
        g_free(variant);
 }
 
@@ -257,7 +268,7 @@ void untagged_variant_declaration_add_field(struct declaration_untagged_variant
        index = untagged_variant_declaration->fields->len - 1;  /* last field (new) */
        field = &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
        field->name = g_quark_from_string(field_name);
-       declaration_ref(field_declaration);
+       bt_declaration_ref(field_declaration);
        field->declaration = field_declaration;
        /* Keep index in hash rather than pointer, because array can relocate */
        g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
@@ -273,11 +284,18 @@ void untagged_variant_declaration_add_field(struct declaration_untagged_variant
 struct declaration_field *
 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag)
 {
-       unsigned long index;
+       gpointer index;
+       gboolean found;
 
-       index = (unsigned long) g_hash_table_lookup(untagged_variant_declaration->fields_by_tag,
-                                                   (gconstpointer) (unsigned long) tag);
-       return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
+       found = g_hash_table_lookup_extended(
+                               untagged_variant_declaration->fields_by_tag,
+                               (gconstpointer) (unsigned long) tag, NULL, &index);
+
+       if (!found) {
+               return NULL;
+       }
+
+       return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, (unsigned long)index);
 }
 
 /*
This page took 0.025646 seconds and 4 git commands to generate.