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