Add missing permission notice in each source file
[babeltrace.git] / types / variant.c
index 96b665082442fcef2c4acfd0f145315e431c1ff3..55b4b1f058820c0c7089202dc029478d31a0e96b 100644 (file)
@@ -3,7 +3,9 @@
  *
  * BabelTrace - Variant Type Converter
  *
- * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  *
  * 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
@@ -87,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);
+       declaration_unref(&variant_declaration->untagged_variant->p);
        g_array_free(variant_declaration->tag_name, TRUE);
+       g_free(variant_declaration);
 }
 
 struct declaration_variant *
@@ -100,6 +112,7 @@ 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);
        variant_declaration->tag_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
        append_scope_path(tag, variant_declaration->tag_name);
        declaration->id = CTF_TYPE_VARIANT;
@@ -183,15 +196,15 @@ struct definition *
        variant->p.index = root_name ? INT_MAX : index;
        variant->p.name = field_name;
        variant->p.path = new_definition_path(parent_scope, field_name, root_name);
-       variant->scope = new_definition_scope(parent_scope, field_name, root_name);
+       variant->p.scope = new_definition_scope(parent_scope, field_name, root_name);
 
        ret = register_field_definition(field_name, &variant->p,
                                        parent_scope);
        assert(!ret);
 
-       variant->enum_tag = lookup_definition(variant->scope->scope_path,
-                                             variant_declaration->tag_name,
-                                             parent_scope);
+       variant->enum_tag = lookup_path_definition(variant->p.scope->scope_path,
+                                                  variant_declaration->tag_name,
+                                                  parent_scope);
                                              
        if (!variant->enum_tag
            || check_enum_tag(variant, variant->enum_tag) < 0)
@@ -211,7 +224,7 @@ struct definition *
                 * various choices of the same field.
                 */
                *field = declaration_field->declaration->definition_new(declaration_field->declaration,
-                                                 variant->scope,
+                                                 variant->p.scope,
                                                  declaration_field->name, 0, NULL);
                if (!*field)
                        goto error;
@@ -219,7 +232,7 @@ struct definition *
        variant->current_field = NULL;
        return &variant->p;
 error:
-       free_definition_scope(variant->scope);
+       free_definition_scope(variant->p.scope);
        declaration_unref(&variant_declaration->p);
        g_free(variant);
        return NULL;
@@ -238,8 +251,9 @@ void _variant_definition_free(struct definition *definition)
                definition_unref(field);
        }
        definition_unref(variant->enum_tag);
-       free_definition_scope(variant->scope);
+       free_definition_scope(variant->p.scope);
        declaration_unref(variant->p.declaration);
+       g_ptr_array_free(variant->fields, TRUE);
        g_free(variant);
 }
 
@@ -270,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.024728 seconds and 4 git commands to generate.