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