Be more widely accepting for missing variant/enum fields
[babeltrace.git] / types / variant.c
CommitLineData
c054553d 1/*
ab4cf058 2 * variant.c
c054553d
MD
3 *
4 * BabelTrace - Variant Type Converter
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
c054553d
MD
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
c462e188
MD
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
c054553d
MD
27 */
28
29#include <babeltrace/compiler.h>
30#include <babeltrace/format.h>
3122e6f0 31#include <babeltrace/types.h>
6ee5115e 32#include <errno.h>
c054553d
MD
33
34static
ecc54f11 35struct bt_definition *_variant_definition_new(struct bt_declaration *declaration,
05c749e5 36 struct definition_scope *parent_scope,
98df1c9f
MD
37 GQuark field_name, int index,
38 const char *root_name);
c054553d 39static
0d69b916 40void _variant_definition_free(struct bt_definition *definition);
c054553d 41
0d69b916 42int bt_variant_rw(struct bt_stream_pos *ppos, struct bt_definition *definition)
c054553d 43{
d11e9c49 44 struct definition_variant *variant_definition =
e1151715 45 container_of(definition, struct definition_variant, p);
0d69b916 46 struct bt_definition *field;
c054553d 47
ebae302b 48 field = bt_variant_get_current_field(variant_definition);
b1a2f580 49 return generic_rw(ppos, field);
c054553d
MD
50}
51
52static
ecc54f11 53void _untagged_variant_declaration_free(struct bt_declaration *declaration)
c054553d 54{
d60cb676
MD
55 struct declaration_untagged_variant *untagged_variant_declaration =
56 container_of(declaration, struct declaration_untagged_variant, p);
c054553d
MD
57 unsigned long i;
58
becd02a1 59 bt_free_declaration_scope(untagged_variant_declaration->scope);
d60cb676 60 g_hash_table_destroy(untagged_variant_declaration->fields_by_tag);
c054553d 61
d60cb676 62 for (i = 0; i < untagged_variant_declaration->fields->len; i++) {
f6625916 63 struct declaration_field *declaration_field =
d60cb676 64 &g_array_index(untagged_variant_declaration->fields,
f6625916 65 struct declaration_field, i);
e6b4b4f4 66 bt_declaration_unref(declaration_field->declaration);
c054553d 67 }
d60cb676
MD
68 g_array_free(untagged_variant_declaration->fields, true);
69 g_free(untagged_variant_declaration);
c054553d
MD
70}
71
becd02a1 72struct declaration_untagged_variant *bt_untagged_bt_variant_declaration_new(
f6625916 73 struct declaration_scope *parent_scope)
c054553d 74{
d60cb676 75 struct declaration_untagged_variant *untagged_variant_declaration;
ecc54f11 76 struct bt_declaration *declaration;
c054553d 77
d60cb676
MD
78 untagged_variant_declaration = g_new(struct declaration_untagged_variant, 1);
79 declaration = &untagged_variant_declaration->p;
80 untagged_variant_declaration->fields_by_tag = g_hash_table_new(g_direct_hash,
e19c3d69 81 g_direct_equal);
d60cb676 82 untagged_variant_declaration->fields = g_array_sized_new(FALSE, TRUE,
f6625916 83 sizeof(struct declaration_field),
e19c3d69 84 DEFAULT_NR_STRUCT_FIELDS);
becd02a1 85 untagged_variant_declaration->scope = bt_new_declaration_scope(parent_scope);
d60cb676 86 declaration->id = CTF_TYPE_UNTAGGED_VARIANT;
f6625916 87 declaration->alignment = 1;
d60cb676
MD
88 declaration->declaration_free = _untagged_variant_declaration_free;
89 declaration->definition_new = NULL;
90 declaration->definition_free = NULL;
91 declaration->ref = 1;
92 return untagged_variant_declaration;
93}
94
95static
ecc54f11 96void _variant_declaration_free(struct bt_declaration *declaration)
d60cb676
MD
97{
98 struct declaration_variant *variant_declaration =
99 container_of(declaration, struct declaration_variant, p);
100
e6b4b4f4 101 bt_declaration_unref(&variant_declaration->untagged_variant->p);
d60cb676 102 g_array_free(variant_declaration->tag_name, TRUE);
15d4fe3c 103 g_free(variant_declaration);
d60cb676
MD
104}
105
106struct declaration_variant *
becd02a1 107 bt_variant_declaration_new(struct declaration_untagged_variant *untagged_variant, const char *tag)
d60cb676
MD
108{
109 struct declaration_variant *variant_declaration;
ecc54f11 110 struct bt_declaration *declaration;
d60cb676
MD
111
112 variant_declaration = g_new(struct declaration_variant, 1);
113 declaration = &variant_declaration->p;
114 variant_declaration->untagged_variant = untagged_variant;
e6b4b4f4 115 bt_declaration_ref(&untagged_variant->p);
d60cb676 116 variant_declaration->tag_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
dd0365d9 117 bt_append_scope_path(tag, variant_declaration->tag_name);
d60cb676
MD
118 declaration->id = CTF_TYPE_VARIANT;
119 declaration->alignment = 1;
f6625916
MD
120 declaration->declaration_free = _variant_declaration_free;
121 declaration->definition_new = _variant_definition_new;
122 declaration->definition_free = _variant_definition_free;
123 declaration->ref = 1;
124 return variant_declaration;
c054553d
MD
125}
126
127static
0d69b916 128struct bt_definition *
ecc54f11 129 _variant_definition_new(struct bt_declaration *declaration,
05c749e5 130 struct definition_scope *parent_scope,
98df1c9f
MD
131 GQuark field_name, int index,
132 const char *root_name)
c054553d 133{
f6625916
MD
134 struct declaration_variant *variant_declaration =
135 container_of(declaration, struct declaration_variant, p);
e1151715 136 struct definition_variant *variant;
ac88af75 137 unsigned long i;
98df1c9f 138 int ret;
c054553d 139
e1151715 140 variant = g_new(struct definition_variant, 1);
e6b4b4f4 141 bt_declaration_ref(&variant_declaration->p);
f6625916
MD
142 variant->p.declaration = declaration;
143 variant->declaration = variant_declaration;
c054553d 144 variant->p.ref = 1;
98df1c9f
MD
145 /*
146 * Use INT_MAX order to ensure that all fields of the parent
147 * scope are seen as being prior to this scope.
148 */
149 variant->p.index = root_name ? INT_MAX : index;
b1a2f580 150 variant->p.name = field_name;
2b77e6a6
JD
151 variant->p.path = bt_new_definition_path(parent_scope, field_name, root_name);
152 variant->p.scope = bt_new_definition_scope(parent_scope, field_name, root_name);
98df1c9f 153
2b77e6a6 154 ret = bt_register_field_definition(field_name, &variant->p,
98df1c9f
MD
155 parent_scope);
156 assert(!ret);
157
2b77e6a6 158 variant->enum_tag = bt_lookup_path_definition(variant->p.scope->scope_path,
a35173fe
MD
159 variant_declaration->tag_name,
160 parent_scope);
d60cb676 161
d80f0837 162 if (!variant->enum_tag)
d60cb676 163 goto error;
13fad8b6 164 bt_definition_ref(variant->enum_tag);
b1a2f580
MD
165 variant->fields = g_ptr_array_sized_new(variant_declaration->untagged_variant->fields->len);
166 g_ptr_array_set_size(variant->fields, variant_declaration->untagged_variant->fields->len);
d60cb676 167 for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
f6625916 168 struct declaration_field *declaration_field =
d60cb676 169 &g_array_index(variant_declaration->untagged_variant->fields,
f6625916 170 struct declaration_field, i);
0d69b916
JD
171 struct bt_definition **field =
172 (struct bt_definition **) &g_ptr_array_index(variant->fields, i);
ac88af75 173
05c749e5
MD
174 /*
175 * All child definition are at index 0, because they are
176 * various choices of the same field.
177 */
b1a2f580 178 *field = declaration_field->declaration->definition_new(declaration_field->declaration,
a35173fe 179 variant->p.scope,
98df1c9f
MD
180 declaration_field->name, 0, NULL);
181 if (!*field)
182 goto error;
ac88af75 183 }
c054553d
MD
184 variant->current_field = NULL;
185 return &variant->p;
d60cb676 186error:
2b77e6a6 187 bt_free_definition_scope(variant->p.scope);
e6b4b4f4 188 bt_declaration_unref(&variant_declaration->p);
d60cb676
MD
189 g_free(variant);
190 return NULL;
c054553d
MD
191}
192
193static
0d69b916 194void _variant_definition_free(struct bt_definition *definition)
c054553d 195{
e1151715
MD
196 struct definition_variant *variant =
197 container_of(definition, struct definition_variant, p);
c054553d
MD
198 unsigned long i;
199
d60cb676 200 assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
c054553d 201 for (i = 0; i < variant->fields->len; i++) {
0d69b916 202 struct bt_definition *field = g_ptr_array_index(variant->fields, i);
13fad8b6 203 bt_definition_unref(field);
c054553d 204 }
13fad8b6 205 bt_definition_unref(variant->enum_tag);
2b77e6a6 206 bt_free_definition_scope(variant->p.scope);
e6b4b4f4 207 bt_declaration_unref(variant->p.declaration);
15d4fe3c 208 g_ptr_array_free(variant->fields, TRUE);
c054553d
MD
209 g_free(variant);
210}
211
becd02a1 212void bt_untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
d60cb676 213 const char *field_name,
ecc54f11 214 struct bt_declaration *field_declaration)
c054553d 215{
f6625916 216 struct declaration_field *field;
c054553d
MD
217 unsigned long index;
218
d60cb676
MD
219 g_array_set_size(untagged_variant_declaration->fields, untagged_variant_declaration->fields->len + 1);
220 index = untagged_variant_declaration->fields->len - 1; /* last field (new) */
221 field = &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
222 field->name = g_quark_from_string(field_name);
e6b4b4f4 223 bt_declaration_ref(field_declaration);
d60cb676 224 field->declaration = field_declaration;
c054553d 225 /* Keep index in hash rather than pointer, because array can relocate */
d60cb676 226 g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
c054553d
MD
227 (gpointer) (unsigned long) field->name,
228 (gpointer) index);
229 /*
230 * Alignment of variant is based on the alignment of its currently
231 * selected choice, so we leave variant alignment as-is (statically
232 * speaking).
233 */
234}
235
f6625916 236struct declaration_field *
becd02a1 237bt_untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag)
c054553d 238{
43f9090c
SM
239 gpointer index;
240 gboolean found;
c054553d 241
43f9090c
SM
242 found = g_hash_table_lookup_extended(
243 untagged_variant_declaration->fields_by_tag,
244 (gconstpointer) (unsigned long) tag, NULL, &index);
245
246 if (!found) {
247 return NULL;
248 }
249
250 return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, (unsigned long)index);
c054553d
MD
251}
252
253/*
254 * field returned only valid as long as the field structure is not appended to.
255 */
0d69b916 256struct bt_definition *bt_variant_get_current_field(struct definition_variant *variant)
c054553d 257{
e1151715
MD
258 struct definition_enum *_enum =
259 container_of(variant->enum_tag, struct definition_enum, p);
f6625916 260 struct declaration_variant *variant_declaration = variant->declaration;
c054553d
MD
261 unsigned long index;
262 GArray *tag_array;
263 GQuark tag;
264
265 tag_array = _enum->value;
266 /*
267 * The 1 to 1 mapping from enumeration to value should have been already
268 * checked. (see TODO above)
269 */
270 assert(tag_array->len == 1);
271 tag = g_array_index(tag_array, GQuark, 0);
d60cb676 272 index = (unsigned long) g_hash_table_lookup(variant_declaration->untagged_variant->fields_by_tag,
c054553d 273 (gconstpointer) (unsigned long) tag);
b1a2f580 274 variant->current_field = g_ptr_array_index(variant->fields, index);
96354dac 275 return variant->current_field;
c054553d 276}
This page took 0.041382 seconds and 4 git commands to generate.