Cleanup: update ifdef wrapper name
[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.
19 */
20
21#include <babeltrace/compiler.h>
22#include <babeltrace/format.h>
3122e6f0 23#include <babeltrace/types.h>
6ee5115e 24#include <errno.h>
c054553d
MD
25
26static
f6625916 27struct definition *_variant_definition_new(struct declaration *declaration,
05c749e5 28 struct definition_scope *parent_scope,
98df1c9f
MD
29 GQuark field_name, int index,
30 const char *root_name);
c054553d 31static
e1151715 32void _variant_definition_free(struct definition *definition);
c054553d 33
c5e74408 34int variant_rw(struct stream_pos *ppos, struct definition *definition)
c054553d 35{
d11e9c49 36 struct definition_variant *variant_definition =
e1151715 37 container_of(definition, struct definition_variant, p);
b1a2f580 38 struct definition *field;
c054553d 39
d11e9c49 40 field = variant_get_current_field(variant_definition);
b1a2f580 41 return generic_rw(ppos, field);
c054553d
MD
42}
43
44static
d60cb676 45void _untagged_variant_declaration_free(struct declaration *declaration)
c054553d 46{
d60cb676
MD
47 struct declaration_untagged_variant *untagged_variant_declaration =
48 container_of(declaration, struct declaration_untagged_variant, p);
c054553d
MD
49 unsigned long i;
50
d60cb676
MD
51 free_declaration_scope(untagged_variant_declaration->scope);
52 g_hash_table_destroy(untagged_variant_declaration->fields_by_tag);
c054553d 53
d60cb676 54 for (i = 0; i < untagged_variant_declaration->fields->len; i++) {
f6625916 55 struct declaration_field *declaration_field =
d60cb676 56 &g_array_index(untagged_variant_declaration->fields,
f6625916
MD
57 struct declaration_field, i);
58 declaration_unref(declaration_field->declaration);
c054553d 59 }
d60cb676
MD
60 g_array_free(untagged_variant_declaration->fields, true);
61 g_free(untagged_variant_declaration);
c054553d
MD
62}
63
ab4cf058 64struct declaration_untagged_variant *untagged_variant_declaration_new(
f6625916 65 struct declaration_scope *parent_scope)
c054553d 66{
d60cb676 67 struct declaration_untagged_variant *untagged_variant_declaration;
f6625916 68 struct declaration *declaration;
c054553d 69
d60cb676
MD
70 untagged_variant_declaration = g_new(struct declaration_untagged_variant, 1);
71 declaration = &untagged_variant_declaration->p;
72 untagged_variant_declaration->fields_by_tag = g_hash_table_new(g_direct_hash,
e19c3d69 73 g_direct_equal);
d60cb676 74 untagged_variant_declaration->fields = g_array_sized_new(FALSE, TRUE,
f6625916 75 sizeof(struct declaration_field),
e19c3d69 76 DEFAULT_NR_STRUCT_FIELDS);
d60cb676
MD
77 untagged_variant_declaration->scope = new_declaration_scope(parent_scope);
78 declaration->id = CTF_TYPE_UNTAGGED_VARIANT;
f6625916 79 declaration->alignment = 1;
d60cb676
MD
80 declaration->declaration_free = _untagged_variant_declaration_free;
81 declaration->definition_new = NULL;
82 declaration->definition_free = NULL;
83 declaration->ref = 1;
84 return untagged_variant_declaration;
85}
86
87static
88void _variant_declaration_free(struct declaration *declaration)
89{
90 struct declaration_variant *variant_declaration =
91 container_of(declaration, struct declaration_variant, p);
92
93 _untagged_variant_declaration_free(&variant_declaration->untagged_variant->p);
94 g_array_free(variant_declaration->tag_name, TRUE);
95}
96
97struct declaration_variant *
98 variant_declaration_new(struct declaration_untagged_variant *untagged_variant, const char *tag)
99{
100 struct declaration_variant *variant_declaration;
101 struct declaration *declaration;
102
103 variant_declaration = g_new(struct declaration_variant, 1);
104 declaration = &variant_declaration->p;
105 variant_declaration->untagged_variant = untagged_variant;
07fdca67 106 declaration_ref(&untagged_variant->p);
d60cb676
MD
107 variant_declaration->tag_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
108 append_scope_path(tag, variant_declaration->tag_name);
109 declaration->id = CTF_TYPE_VARIANT;
110 declaration->alignment = 1;
f6625916
MD
111 declaration->declaration_free = _variant_declaration_free;
112 declaration->definition_new = _variant_definition_new;
113 declaration->definition_free = _variant_definition_free;
114 declaration->ref = 1;
115 return variant_declaration;
c054553d
MD
116}
117
d60cb676
MD
118/*
119 * tag_instance is assumed to be an enumeration.
120 * Returns 0 if OK, < 0 if error.
121 */
122static
123int check_enum_tag(struct definition_variant *variant,
124 struct definition *enum_tag)
125{
126 struct definition_enum *_enum =
127 container_of(enum_tag, struct definition_enum, p);
128 struct declaration_enum *enum_declaration = _enum->declaration;
129 int missing_field = 0;
130 unsigned long i;
131
132 /*
133 * Strictly speaking, each enumerator must map to a field of the
134 * variant. However, we are even stricter here by requiring that each
135 * variant choice map to an enumerator too. We then validate that the
136 * number of enumerators equals the number of variant choices.
137 */
138 if (variant->declaration->untagged_variant->fields->len != enum_get_nr_enumerators(enum_declaration))
139 return -EPERM;
140
141 for (i = 0; i < variant->declaration->untagged_variant->fields->len; i++) {
142 struct declaration_field *field_declaration =
143 &g_array_index(variant->declaration->untagged_variant->fields,
144 struct declaration_field, i);
145 if (!enum_quark_to_range_set(enum_declaration, field_declaration->name)) {
146 missing_field = 1;
147 break;
148 }
149 }
150 if (missing_field)
151 return -EPERM;
152
153 /*
154 * Check the enumeration: it must map each value to one and only one
155 * enumerator tag.
156 * TODO: we should also check that each range map to one and only one
157 * tag. For the moment, we will simply check this dynamically in
158 * variant_declaration_get_current_field().
159 */
160 return 0;
161}
162
163
164
c054553d 165static
e1151715 166struct definition *
f6625916 167 _variant_definition_new(struct declaration *declaration,
05c749e5 168 struct definition_scope *parent_scope,
98df1c9f
MD
169 GQuark field_name, int index,
170 const char *root_name)
c054553d 171{
f6625916
MD
172 struct declaration_variant *variant_declaration =
173 container_of(declaration, struct declaration_variant, p);
e1151715 174 struct definition_variant *variant;
ac88af75 175 unsigned long i;
98df1c9f 176 int ret;
c054553d 177
e1151715 178 variant = g_new(struct definition_variant, 1);
f6625916
MD
179 declaration_ref(&variant_declaration->p);
180 variant->p.declaration = declaration;
181 variant->declaration = variant_declaration;
c054553d 182 variant->p.ref = 1;
98df1c9f
MD
183 /*
184 * Use INT_MAX order to ensure that all fields of the parent
185 * scope are seen as being prior to this scope.
186 */
187 variant->p.index = root_name ? INT_MAX : index;
b1a2f580 188 variant->p.name = field_name;
98df1c9f 189 variant->p.path = new_definition_path(parent_scope, field_name, root_name);
a35173fe 190 variant->p.scope = new_definition_scope(parent_scope, field_name, root_name);
98df1c9f
MD
191
192 ret = register_field_definition(field_name, &variant->p,
193 parent_scope);
194 assert(!ret);
195
a35173fe
MD
196 variant->enum_tag = lookup_path_definition(variant->p.scope->scope_path,
197 variant_declaration->tag_name,
198 parent_scope);
d60cb676
MD
199
200 if (!variant->enum_tag
201 || check_enum_tag(variant, variant->enum_tag) < 0)
202 goto error;
203 definition_ref(variant->enum_tag);
b1a2f580
MD
204 variant->fields = g_ptr_array_sized_new(variant_declaration->untagged_variant->fields->len);
205 g_ptr_array_set_size(variant->fields, variant_declaration->untagged_variant->fields->len);
d60cb676 206 for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
f6625916 207 struct declaration_field *declaration_field =
d60cb676 208 &g_array_index(variant_declaration->untagged_variant->fields,
f6625916 209 struct declaration_field, i);
b1a2f580
MD
210 struct definition **field =
211 (struct definition **) &g_ptr_array_index(variant->fields, i);
ac88af75 212
05c749e5
MD
213 /*
214 * All child definition are at index 0, because they are
215 * various choices of the same field.
216 */
b1a2f580 217 *field = declaration_field->declaration->definition_new(declaration_field->declaration,
a35173fe 218 variant->p.scope,
98df1c9f
MD
219 declaration_field->name, 0, NULL);
220 if (!*field)
221 goto error;
ac88af75 222 }
c054553d
MD
223 variant->current_field = NULL;
224 return &variant->p;
d60cb676 225error:
a35173fe 226 free_definition_scope(variant->p.scope);
d60cb676
MD
227 declaration_unref(&variant_declaration->p);
228 g_free(variant);
229 return NULL;
c054553d
MD
230}
231
232static
e1151715 233void _variant_definition_free(struct definition *definition)
c054553d 234{
e1151715
MD
235 struct definition_variant *variant =
236 container_of(definition, struct definition_variant, p);
c054553d
MD
237 unsigned long i;
238
d60cb676 239 assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
c054553d 240 for (i = 0; i < variant->fields->len; i++) {
b1a2f580
MD
241 struct definition *field = g_ptr_array_index(variant->fields, i);
242 definition_unref(field);
c054553d 243 }
d60cb676 244 definition_unref(variant->enum_tag);
a35173fe 245 free_definition_scope(variant->p.scope);
f6625916 246 declaration_unref(variant->p.declaration);
c054553d
MD
247 g_free(variant);
248}
249
d60cb676
MD
250void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
251 const char *field_name,
252 struct declaration *field_declaration)
c054553d 253{
f6625916 254 struct declaration_field *field;
c054553d
MD
255 unsigned long index;
256
d60cb676
MD
257 g_array_set_size(untagged_variant_declaration->fields, untagged_variant_declaration->fields->len + 1);
258 index = untagged_variant_declaration->fields->len - 1; /* last field (new) */
259 field = &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
260 field->name = g_quark_from_string(field_name);
261 declaration_ref(field_declaration);
262 field->declaration = field_declaration;
c054553d 263 /* Keep index in hash rather than pointer, because array can relocate */
d60cb676 264 g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
c054553d
MD
265 (gpointer) (unsigned long) field->name,
266 (gpointer) index);
267 /*
268 * Alignment of variant is based on the alignment of its currently
269 * selected choice, so we leave variant alignment as-is (statically
270 * speaking).
271 */
272}
273
f6625916 274struct declaration_field *
d60cb676 275untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag)
c054553d 276{
43f9090c
SM
277 gpointer index;
278 gboolean found;
c054553d 279
43f9090c
SM
280 found = g_hash_table_lookup_extended(
281 untagged_variant_declaration->fields_by_tag,
282 (gconstpointer) (unsigned long) tag, NULL, &index);
283
284 if (!found) {
285 return NULL;
286 }
287
288 return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, (unsigned long)index);
c054553d
MD
289}
290
291/*
292 * field returned only valid as long as the field structure is not appended to.
293 */
b1a2f580 294struct definition *variant_get_current_field(struct definition_variant *variant)
c054553d 295{
e1151715
MD
296 struct definition_enum *_enum =
297 container_of(variant->enum_tag, struct definition_enum, p);
f6625916 298 struct declaration_variant *variant_declaration = variant->declaration;
c054553d
MD
299 unsigned long index;
300 GArray *tag_array;
301 GQuark tag;
302
303 tag_array = _enum->value;
304 /*
305 * The 1 to 1 mapping from enumeration to value should have been already
306 * checked. (see TODO above)
307 */
308 assert(tag_array->len == 1);
309 tag = g_array_index(tag_array, GQuark, 0);
d60cb676 310 index = (unsigned long) g_hash_table_lookup(variant_declaration->untagged_variant->fields_by_tag,
c054553d 311 (gconstpointer) (unsigned long) tag);
b1a2f580 312 variant->current_field = g_ptr_array_index(variant->fields, index);
96354dac 313 return variant->current_field;
c054553d 314}
This page took 0.061783 seconds and 4 git commands to generate.