namespace the sequence functions
[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
f6625916 35struct definition *_variant_definition_new(struct declaration *declaration,
05c749e5 36 struct definition_scope *parent_scope,
98df1c9f
MD
37 GQuark field_name, int index,
38 const char *root_name);
c054553d 39static
e1151715 40void _variant_definition_free(struct definition *definition);
c054553d 41
c5e74408 42int variant_rw(struct stream_pos *ppos, struct definition *definition)
c054553d 43{
d11e9c49 44 struct definition_variant *variant_definition =
e1151715 45 container_of(definition, struct definition_variant, p);
b1a2f580 46 struct definition *field;
c054553d 47
d11e9c49 48 field = variant_get_current_field(variant_definition);
b1a2f580 49 return generic_rw(ppos, field);
c054553d
MD
50}
51
52static
d60cb676 53void _untagged_variant_declaration_free(struct 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
d60cb676
MD
59 free_declaration_scope(untagged_variant_declaration->scope);
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
ab4cf058 72struct declaration_untagged_variant *untagged_variant_declaration_new(
f6625916 73 struct declaration_scope *parent_scope)
c054553d 74{
d60cb676 75 struct declaration_untagged_variant *untagged_variant_declaration;
f6625916 76 struct 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);
d60cb676
MD
85 untagged_variant_declaration->scope = new_declaration_scope(parent_scope);
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
96void _variant_declaration_free(struct declaration *declaration)
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 *
107 variant_declaration_new(struct declaration_untagged_variant *untagged_variant, const char *tag)
108{
109 struct declaration_variant *variant_declaration;
110 struct declaration *declaration;
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
d60cb676
MD
127/*
128 * tag_instance is assumed to be an enumeration.
129 * Returns 0 if OK, < 0 if error.
130 */
131static
132int check_enum_tag(struct definition_variant *variant,
133 struct definition *enum_tag)
134{
135 struct definition_enum *_enum =
136 container_of(enum_tag, struct definition_enum, p);
137 struct declaration_enum *enum_declaration = _enum->declaration;
138 int missing_field = 0;
139 unsigned long i;
140
141 /*
142 * Strictly speaking, each enumerator must map to a field of the
143 * variant. However, we are even stricter here by requiring that each
144 * variant choice map to an enumerator too. We then validate that the
145 * number of enumerators equals the number of variant choices.
146 */
2399b6f4 147 if (variant->declaration->untagged_variant->fields->len != bt_enum_get_nr_enumerators(enum_declaration))
d60cb676
MD
148 return -EPERM;
149
150 for (i = 0; i < variant->declaration->untagged_variant->fields->len; i++) {
151 struct declaration_field *field_declaration =
152 &g_array_index(variant->declaration->untagged_variant->fields,
153 struct declaration_field, i);
2399b6f4 154 if (!bt_enum_quark_to_range_set(enum_declaration, field_declaration->name)) {
d60cb676
MD
155 missing_field = 1;
156 break;
157 }
158 }
159 if (missing_field)
160 return -EPERM;
161
162 /*
163 * Check the enumeration: it must map each value to one and only one
164 * enumerator tag.
165 * TODO: we should also check that each range map to one and only one
166 * tag. For the moment, we will simply check this dynamically in
167 * variant_declaration_get_current_field().
168 */
169 return 0;
170}
171
172
173
c054553d 174static
e1151715 175struct definition *
f6625916 176 _variant_definition_new(struct declaration *declaration,
05c749e5 177 struct definition_scope *parent_scope,
98df1c9f
MD
178 GQuark field_name, int index,
179 const char *root_name)
c054553d 180{
f6625916
MD
181 struct declaration_variant *variant_declaration =
182 container_of(declaration, struct declaration_variant, p);
e1151715 183 struct definition_variant *variant;
ac88af75 184 unsigned long i;
98df1c9f 185 int ret;
c054553d 186
e1151715 187 variant = g_new(struct definition_variant, 1);
e6b4b4f4 188 bt_declaration_ref(&variant_declaration->p);
f6625916
MD
189 variant->p.declaration = declaration;
190 variant->declaration = variant_declaration;
c054553d 191 variant->p.ref = 1;
98df1c9f
MD
192 /*
193 * Use INT_MAX order to ensure that all fields of the parent
194 * scope are seen as being prior to this scope.
195 */
196 variant->p.index = root_name ? INT_MAX : index;
b1a2f580 197 variant->p.name = field_name;
98df1c9f 198 variant->p.path = new_definition_path(parent_scope, field_name, root_name);
a35173fe 199 variant->p.scope = new_definition_scope(parent_scope, field_name, root_name);
98df1c9f
MD
200
201 ret = register_field_definition(field_name, &variant->p,
202 parent_scope);
203 assert(!ret);
204
a35173fe
MD
205 variant->enum_tag = lookup_path_definition(variant->p.scope->scope_path,
206 variant_declaration->tag_name,
207 parent_scope);
d60cb676
MD
208
209 if (!variant->enum_tag
210 || check_enum_tag(variant, variant->enum_tag) < 0)
211 goto error;
13fad8b6 212 bt_definition_ref(variant->enum_tag);
b1a2f580
MD
213 variant->fields = g_ptr_array_sized_new(variant_declaration->untagged_variant->fields->len);
214 g_ptr_array_set_size(variant->fields, variant_declaration->untagged_variant->fields->len);
d60cb676 215 for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
f6625916 216 struct declaration_field *declaration_field =
d60cb676 217 &g_array_index(variant_declaration->untagged_variant->fields,
f6625916 218 struct declaration_field, i);
b1a2f580
MD
219 struct definition **field =
220 (struct definition **) &g_ptr_array_index(variant->fields, i);
ac88af75 221
05c749e5
MD
222 /*
223 * All child definition are at index 0, because they are
224 * various choices of the same field.
225 */
b1a2f580 226 *field = declaration_field->declaration->definition_new(declaration_field->declaration,
a35173fe 227 variant->p.scope,
98df1c9f
MD
228 declaration_field->name, 0, NULL);
229 if (!*field)
230 goto error;
ac88af75 231 }
c054553d
MD
232 variant->current_field = NULL;
233 return &variant->p;
d60cb676 234error:
a35173fe 235 free_definition_scope(variant->p.scope);
e6b4b4f4 236 bt_declaration_unref(&variant_declaration->p);
d60cb676
MD
237 g_free(variant);
238 return NULL;
c054553d
MD
239}
240
241static
e1151715 242void _variant_definition_free(struct definition *definition)
c054553d 243{
e1151715
MD
244 struct definition_variant *variant =
245 container_of(definition, struct definition_variant, p);
c054553d
MD
246 unsigned long i;
247
d60cb676 248 assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
c054553d 249 for (i = 0; i < variant->fields->len; i++) {
b1a2f580 250 struct definition *field = g_ptr_array_index(variant->fields, i);
13fad8b6 251 bt_definition_unref(field);
c054553d 252 }
13fad8b6 253 bt_definition_unref(variant->enum_tag);
a35173fe 254 free_definition_scope(variant->p.scope);
e6b4b4f4 255 bt_declaration_unref(variant->p.declaration);
15d4fe3c 256 g_ptr_array_free(variant->fields, TRUE);
c054553d
MD
257 g_free(variant);
258}
259
d60cb676
MD
260void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
261 const char *field_name,
262 struct declaration *field_declaration)
c054553d 263{
f6625916 264 struct declaration_field *field;
c054553d
MD
265 unsigned long index;
266
d60cb676
MD
267 g_array_set_size(untagged_variant_declaration->fields, untagged_variant_declaration->fields->len + 1);
268 index = untagged_variant_declaration->fields->len - 1; /* last field (new) */
269 field = &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
270 field->name = g_quark_from_string(field_name);
e6b4b4f4 271 bt_declaration_ref(field_declaration);
d60cb676 272 field->declaration = field_declaration;
c054553d 273 /* Keep index in hash rather than pointer, because array can relocate */
d60cb676 274 g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
c054553d
MD
275 (gpointer) (unsigned long) field->name,
276 (gpointer) index);
277 /*
278 * Alignment of variant is based on the alignment of its currently
279 * selected choice, so we leave variant alignment as-is (statically
280 * speaking).
281 */
282}
283
f6625916 284struct declaration_field *
d60cb676 285untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag)
c054553d 286{
43f9090c
SM
287 gpointer index;
288 gboolean found;
c054553d 289
43f9090c
SM
290 found = g_hash_table_lookup_extended(
291 untagged_variant_declaration->fields_by_tag,
292 (gconstpointer) (unsigned long) tag, NULL, &index);
293
294 if (!found) {
295 return NULL;
296 }
297
298 return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, (unsigned long)index);
c054553d
MD
299}
300
301/*
302 * field returned only valid as long as the field structure is not appended to.
303 */
b1a2f580 304struct definition *variant_get_current_field(struct definition_variant *variant)
c054553d 305{
e1151715
MD
306 struct definition_enum *_enum =
307 container_of(variant->enum_tag, struct definition_enum, p);
f6625916 308 struct declaration_variant *variant_declaration = variant->declaration;
c054553d
MD
309 unsigned long index;
310 GArray *tag_array;
311 GQuark tag;
312
313 tag_array = _enum->value;
314 /*
315 * The 1 to 1 mapping from enumeration to value should have been already
316 * checked. (see TODO above)
317 */
318 assert(tag_array->len == 1);
319 tag = g_array_index(tag_array, GQuark, 0);
d60cb676 320 index = (unsigned long) g_hash_table_lookup(variant_declaration->untagged_variant->fields_by_tag,
c054553d 321 (gconstpointer) (unsigned long) tag);
b1a2f580 322 variant->current_field = g_ptr_array_index(variant->fields, index);
96354dac 323 return variant->current_field;
c054553d 324}
This page took 0.041311 seconds and 4 git commands to generate.