660fb5817d93cfb244894e85c48a6e810b9bd964
[babeltrace.git] / types / variant.c
1 /*
2 * variant.c
3 *
4 * BabelTrace - Variant Type Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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>
23 #include <babeltrace/types.h>
24 #include <errno.h>
25
26 static
27 struct definition *_variant_definition_new(struct declaration *declaration,
28 struct definition_scope *parent_scope,
29 GQuark field_name, int index,
30 const char *root_name);
31 static
32 void _variant_definition_free(struct definition *definition);
33
34 int variant_rw(struct stream_pos *ppos, struct definition *definition)
35 {
36 struct definition_variant *variant_definition =
37 container_of(definition, struct definition_variant, p);
38 struct definition *field;
39
40 field = variant_get_current_field(variant_definition);
41 return generic_rw(ppos, field);
42 }
43
44 static
45 void _untagged_variant_declaration_free(struct declaration *declaration)
46 {
47 struct declaration_untagged_variant *untagged_variant_declaration =
48 container_of(declaration, struct declaration_untagged_variant, p);
49 unsigned long i;
50
51 free_declaration_scope(untagged_variant_declaration->scope);
52 g_hash_table_destroy(untagged_variant_declaration->fields_by_tag);
53
54 for (i = 0; i < untagged_variant_declaration->fields->len; i++) {
55 struct declaration_field *declaration_field =
56 &g_array_index(untagged_variant_declaration->fields,
57 struct declaration_field, i);
58 declaration_unref(declaration_field->declaration);
59 }
60 g_array_free(untagged_variant_declaration->fields, true);
61 g_free(untagged_variant_declaration);
62 }
63
64 struct declaration_untagged_variant *untagged_variant_declaration_new(
65 struct declaration_scope *parent_scope)
66 {
67 struct declaration_untagged_variant *untagged_variant_declaration;
68 struct declaration *declaration;
69
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,
73 g_direct_equal);
74 untagged_variant_declaration->fields = g_array_sized_new(FALSE, TRUE,
75 sizeof(struct declaration_field),
76 DEFAULT_NR_STRUCT_FIELDS);
77 untagged_variant_declaration->scope = new_declaration_scope(parent_scope);
78 declaration->id = CTF_TYPE_UNTAGGED_VARIANT;
79 declaration->alignment = 1;
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
93 declaration_unref(&variant_declaration->untagged_variant->p);
94 g_array_free(variant_declaration->tag_name, TRUE);
95 g_free(variant_declaration);
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;
107 declaration_ref(&untagged_variant->p);
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;
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;
117 }
118
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
166 static
167 struct definition *
168 _variant_definition_new(struct declaration *declaration,
169 struct definition_scope *parent_scope,
170 GQuark field_name, int index,
171 const char *root_name)
172 {
173 struct declaration_variant *variant_declaration =
174 container_of(declaration, struct declaration_variant, p);
175 struct definition_variant *variant;
176 unsigned long i;
177 int ret;
178
179 variant = g_new(struct definition_variant, 1);
180 declaration_ref(&variant_declaration->p);
181 variant->p.declaration = declaration;
182 variant->declaration = variant_declaration;
183 variant->p.ref = 1;
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;
189 variant->p.name = field_name;
190 variant->p.path = new_definition_path(parent_scope, field_name, root_name);
191 variant->p.scope = new_definition_scope(parent_scope, field_name, root_name);
192
193 ret = register_field_definition(field_name, &variant->p,
194 parent_scope);
195 assert(!ret);
196
197 variant->enum_tag = lookup_path_definition(variant->p.scope->scope_path,
198 variant_declaration->tag_name,
199 parent_scope);
200
201 if (!variant->enum_tag
202 || check_enum_tag(variant, variant->enum_tag) < 0)
203 goto error;
204 definition_ref(variant->enum_tag);
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);
207 for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
208 struct declaration_field *declaration_field =
209 &g_array_index(variant_declaration->untagged_variant->fields,
210 struct declaration_field, i);
211 struct definition **field =
212 (struct definition **) &g_ptr_array_index(variant->fields, i);
213
214 /*
215 * All child definition are at index 0, because they are
216 * various choices of the same field.
217 */
218 *field = declaration_field->declaration->definition_new(declaration_field->declaration,
219 variant->p.scope,
220 declaration_field->name, 0, NULL);
221 if (!*field)
222 goto error;
223 }
224 variant->current_field = NULL;
225 return &variant->p;
226 error:
227 free_definition_scope(variant->p.scope);
228 declaration_unref(&variant_declaration->p);
229 g_free(variant);
230 return NULL;
231 }
232
233 static
234 void _variant_definition_free(struct definition *definition)
235 {
236 struct definition_variant *variant =
237 container_of(definition, struct definition_variant, p);
238 unsigned long i;
239
240 assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
241 for (i = 0; i < variant->fields->len; i++) {
242 struct definition *field = g_ptr_array_index(variant->fields, i);
243 definition_unref(field);
244 }
245 definition_unref(variant->enum_tag);
246 free_definition_scope(variant->p.scope);
247 declaration_unref(variant->p.declaration);
248 g_ptr_array_free(variant->fields, TRUE);
249 g_free(variant);
250 }
251
252 void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
253 const char *field_name,
254 struct declaration *field_declaration)
255 {
256 struct declaration_field *field;
257 unsigned long index;
258
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;
265 /* Keep index in hash rather than pointer, because array can relocate */
266 g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
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
276 struct declaration_field *
277 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag)
278 {
279 gpointer index;
280 gboolean found;
281
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);
291 }
292
293 /*
294 * field returned only valid as long as the field structure is not appended to.
295 */
296 struct definition *variant_get_current_field(struct definition_variant *variant)
297 {
298 struct definition_enum *_enum =
299 container_of(variant->enum_tag, struct definition_enum, p);
300 struct declaration_variant *variant_declaration = variant->declaration;
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);
312 index = (unsigned long) g_hash_table_lookup(variant_declaration->untagged_variant->fields_by_tag,
313 (gconstpointer) (unsigned long) tag);
314 variant->current_field = g_ptr_array_index(variant->fields, index);
315 return variant->current_field;
316 }
This page took 0.053957 seconds and 3 git commands to generate.