Fix: Make sure we have all the metadata streams before adding new traces
[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);
a88cb1c7
MD
49 if (!field) {
50 return -EINVAL;
51 }
b1a2f580 52 return generic_rw(ppos, field);
c054553d
MD
53}
54
55static
ecc54f11 56void _untagged_variant_declaration_free(struct bt_declaration *declaration)
c054553d 57{
d60cb676
MD
58 struct declaration_untagged_variant *untagged_variant_declaration =
59 container_of(declaration, struct declaration_untagged_variant, p);
c054553d
MD
60 unsigned long i;
61
becd02a1 62 bt_free_declaration_scope(untagged_variant_declaration->scope);
d60cb676 63 g_hash_table_destroy(untagged_variant_declaration->fields_by_tag);
c054553d 64
d60cb676 65 for (i = 0; i < untagged_variant_declaration->fields->len; i++) {
f6625916 66 struct declaration_field *declaration_field =
d60cb676 67 &g_array_index(untagged_variant_declaration->fields,
f6625916 68 struct declaration_field, i);
e6b4b4f4 69 bt_declaration_unref(declaration_field->declaration);
c054553d 70 }
d60cb676
MD
71 g_array_free(untagged_variant_declaration->fields, true);
72 g_free(untagged_variant_declaration);
c054553d
MD
73}
74
becd02a1 75struct declaration_untagged_variant *bt_untagged_bt_variant_declaration_new(
f6625916 76 struct declaration_scope *parent_scope)
c054553d 77{
d60cb676 78 struct declaration_untagged_variant *untagged_variant_declaration;
ecc54f11 79 struct bt_declaration *declaration;
c054553d 80
d60cb676
MD
81 untagged_variant_declaration = g_new(struct declaration_untagged_variant, 1);
82 declaration = &untagged_variant_declaration->p;
83 untagged_variant_declaration->fields_by_tag = g_hash_table_new(g_direct_hash,
e19c3d69 84 g_direct_equal);
d60cb676 85 untagged_variant_declaration->fields = g_array_sized_new(FALSE, TRUE,
f6625916 86 sizeof(struct declaration_field),
e19c3d69 87 DEFAULT_NR_STRUCT_FIELDS);
becd02a1 88 untagged_variant_declaration->scope = bt_new_declaration_scope(parent_scope);
d60cb676 89 declaration->id = CTF_TYPE_UNTAGGED_VARIANT;
f6625916 90 declaration->alignment = 1;
d60cb676
MD
91 declaration->declaration_free = _untagged_variant_declaration_free;
92 declaration->definition_new = NULL;
93 declaration->definition_free = NULL;
94 declaration->ref = 1;
95 return untagged_variant_declaration;
96}
97
98static
ecc54f11 99void _variant_declaration_free(struct bt_declaration *declaration)
d60cb676
MD
100{
101 struct declaration_variant *variant_declaration =
102 container_of(declaration, struct declaration_variant, p);
103
e6b4b4f4 104 bt_declaration_unref(&variant_declaration->untagged_variant->p);
d60cb676 105 g_array_free(variant_declaration->tag_name, TRUE);
15d4fe3c 106 g_free(variant_declaration);
d60cb676
MD
107}
108
109struct declaration_variant *
becd02a1 110 bt_variant_declaration_new(struct declaration_untagged_variant *untagged_variant, const char *tag)
d60cb676
MD
111{
112 struct declaration_variant *variant_declaration;
ecc54f11 113 struct bt_declaration *declaration;
d60cb676
MD
114
115 variant_declaration = g_new(struct declaration_variant, 1);
116 declaration = &variant_declaration->p;
117 variant_declaration->untagged_variant = untagged_variant;
e6b4b4f4 118 bt_declaration_ref(&untagged_variant->p);
d60cb676 119 variant_declaration->tag_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
dd0365d9 120 bt_append_scope_path(tag, variant_declaration->tag_name);
d60cb676
MD
121 declaration->id = CTF_TYPE_VARIANT;
122 declaration->alignment = 1;
f6625916
MD
123 declaration->declaration_free = _variant_declaration_free;
124 declaration->definition_new = _variant_definition_new;
125 declaration->definition_free = _variant_definition_free;
126 declaration->ref = 1;
127 return variant_declaration;
c054553d
MD
128}
129
130static
0d69b916 131struct bt_definition *
ecc54f11 132 _variant_definition_new(struct bt_declaration *declaration,
05c749e5 133 struct definition_scope *parent_scope,
98df1c9f
MD
134 GQuark field_name, int index,
135 const char *root_name)
c054553d 136{
f6625916
MD
137 struct declaration_variant *variant_declaration =
138 container_of(declaration, struct declaration_variant, p);
e1151715 139 struct definition_variant *variant;
ac88af75 140 unsigned long i;
98df1c9f 141 int ret;
c054553d 142
e1151715 143 variant = g_new(struct definition_variant, 1);
e6b4b4f4 144 bt_declaration_ref(&variant_declaration->p);
f6625916
MD
145 variant->p.declaration = declaration;
146 variant->declaration = variant_declaration;
c054553d 147 variant->p.ref = 1;
98df1c9f
MD
148 /*
149 * Use INT_MAX order to ensure that all fields of the parent
150 * scope are seen as being prior to this scope.
151 */
152 variant->p.index = root_name ? INT_MAX : index;
b1a2f580 153 variant->p.name = field_name;
2b77e6a6
JD
154 variant->p.path = bt_new_definition_path(parent_scope, field_name, root_name);
155 variant->p.scope = bt_new_definition_scope(parent_scope, field_name, root_name);
98df1c9f 156
2b77e6a6 157 ret = bt_register_field_definition(field_name, &variant->p,
98df1c9f
MD
158 parent_scope);
159 assert(!ret);
160
2b77e6a6 161 variant->enum_tag = bt_lookup_path_definition(variant->p.scope->scope_path,
a35173fe
MD
162 variant_declaration->tag_name,
163 parent_scope);
d60cb676 164
d80f0837 165 if (!variant->enum_tag)
d60cb676 166 goto error;
13fad8b6 167 bt_definition_ref(variant->enum_tag);
b1a2f580
MD
168 variant->fields = g_ptr_array_sized_new(variant_declaration->untagged_variant->fields->len);
169 g_ptr_array_set_size(variant->fields, variant_declaration->untagged_variant->fields->len);
d60cb676 170 for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
f6625916 171 struct declaration_field *declaration_field =
d60cb676 172 &g_array_index(variant_declaration->untagged_variant->fields,
f6625916 173 struct declaration_field, i);
0d69b916
JD
174 struct bt_definition **field =
175 (struct bt_definition **) &g_ptr_array_index(variant->fields, i);
ac88af75 176
05c749e5
MD
177 /*
178 * All child definition are at index 0, because they are
179 * various choices of the same field.
180 */
b1a2f580 181 *field = declaration_field->declaration->definition_new(declaration_field->declaration,
a35173fe 182 variant->p.scope,
98df1c9f
MD
183 declaration_field->name, 0, NULL);
184 if (!*field)
185 goto error;
ac88af75 186 }
c054553d
MD
187 variant->current_field = NULL;
188 return &variant->p;
d60cb676 189error:
2b77e6a6 190 bt_free_definition_scope(variant->p.scope);
e6b4b4f4 191 bt_declaration_unref(&variant_declaration->p);
d60cb676
MD
192 g_free(variant);
193 return NULL;
c054553d
MD
194}
195
196static
0d69b916 197void _variant_definition_free(struct bt_definition *definition)
c054553d 198{
e1151715
MD
199 struct definition_variant *variant =
200 container_of(definition, struct definition_variant, p);
c054553d
MD
201 unsigned long i;
202
d60cb676 203 assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
c054553d 204 for (i = 0; i < variant->fields->len; i++) {
0d69b916 205 struct bt_definition *field = g_ptr_array_index(variant->fields, i);
13fad8b6 206 bt_definition_unref(field);
c054553d 207 }
13fad8b6 208 bt_definition_unref(variant->enum_tag);
2b77e6a6 209 bt_free_definition_scope(variant->p.scope);
e6b4b4f4 210 bt_declaration_unref(variant->p.declaration);
15d4fe3c 211 g_ptr_array_free(variant->fields, TRUE);
c054553d
MD
212 g_free(variant);
213}
214
becd02a1 215void bt_untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
d60cb676 216 const char *field_name,
ecc54f11 217 struct bt_declaration *field_declaration)
c054553d 218{
f6625916 219 struct declaration_field *field;
c054553d
MD
220 unsigned long index;
221
d60cb676
MD
222 g_array_set_size(untagged_variant_declaration->fields, untagged_variant_declaration->fields->len + 1);
223 index = untagged_variant_declaration->fields->len - 1; /* last field (new) */
224 field = &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
225 field->name = g_quark_from_string(field_name);
e6b4b4f4 226 bt_declaration_ref(field_declaration);
d60cb676 227 field->declaration = field_declaration;
c054553d 228 /* Keep index in hash rather than pointer, because array can relocate */
d60cb676 229 g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
c054553d
MD
230 (gpointer) (unsigned long) field->name,
231 (gpointer) index);
232 /*
233 * Alignment of variant is based on the alignment of its currently
234 * selected choice, so we leave variant alignment as-is (statically
235 * speaking).
236 */
237}
238
f6625916 239struct declaration_field *
becd02a1 240bt_untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag)
c054553d 241{
43f9090c
SM
242 gpointer index;
243 gboolean found;
c054553d 244
43f9090c
SM
245 found = g_hash_table_lookup_extended(
246 untagged_variant_declaration->fields_by_tag,
247 (gconstpointer) (unsigned long) tag, NULL, &index);
248
249 if (!found) {
250 return NULL;
251 }
252
253 return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, (unsigned long)index);
c054553d
MD
254}
255
256/*
257 * field returned only valid as long as the field structure is not appended to.
258 */
0d69b916 259struct bt_definition *bt_variant_get_current_field(struct definition_variant *variant)
c054553d 260{
e1151715
MD
261 struct definition_enum *_enum =
262 container_of(variant->enum_tag, struct definition_enum, p);
f6625916 263 struct declaration_variant *variant_declaration = variant->declaration;
c054553d
MD
264 unsigned long index;
265 GArray *tag_array;
266 GQuark tag;
a88cb1c7 267 gpointer orig_key, value;
c054553d
MD
268
269 tag_array = _enum->value;
a88cb1c7
MD
270 if (!tag_array) {
271 /* Enumeration has unknown tag. */
272 fprintf(stderr, "[error] Enumeration used for variant has unknown tag.\n");
273 return NULL;
274 }
c054553d
MD
275 /*
276 * The 1 to 1 mapping from enumeration to value should have been already
277 * checked. (see TODO above)
278 */
279 assert(tag_array->len == 1);
280 tag = g_array_index(tag_array, GQuark, 0);
a88cb1c7
MD
281 if (!g_hash_table_lookup_extended(variant_declaration->untagged_variant->fields_by_tag,
282 (gconstpointer) (unsigned long) tag,
283 &orig_key,
284 &value)) {
285 /* Cannot find matching field. */
286 fprintf(stderr, "[error] Cannot find matching field for enum field \"%s\" in variant.\n",
287 g_quark_to_string(tag));
288 return NULL;
289 }
290 index = (unsigned long) value;
b1a2f580 291 variant->current_field = g_ptr_array_index(variant->fields, index);
96354dac 292 return variant->current_field;
c054553d 293}
This page took 0.067776 seconds and 4 git commands to generate.