Fix: correct name of bt_ctf_field_get_error in comments and typo in man page
[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 _untagged_variant_declaration_free(&variant_declaration->untagged_variant->p);
94 g_array_free(variant_declaration->tag_name, TRUE);
95 }
96
97 struct 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;
106 declaration_ref(&untagged_variant->p);
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;
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;
116 }
117
118 /*
119 * tag_instance is assumed to be an enumeration.
120 * Returns 0 if OK, < 0 if error.
121 */
122 static
123 int 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
165 static
166 struct definition *
167 _variant_definition_new(struct declaration *declaration,
168 struct definition_scope *parent_scope,
169 GQuark field_name, int index,
170 const char *root_name)
171 {
172 struct declaration_variant *variant_declaration =
173 container_of(declaration, struct declaration_variant, p);
174 struct definition_variant *variant;
175 unsigned long i;
176 int ret;
177
178 variant = g_new(struct definition_variant, 1);
179 declaration_ref(&variant_declaration->p);
180 variant->p.declaration = declaration;
181 variant->declaration = variant_declaration;
182 variant->p.ref = 1;
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;
188 variant->p.name = field_name;
189 variant->p.path = new_definition_path(parent_scope, field_name, root_name);
190 variant->p.scope = new_definition_scope(parent_scope, field_name, root_name);
191
192 ret = register_field_definition(field_name, &variant->p,
193 parent_scope);
194 assert(!ret);
195
196 variant->enum_tag = lookup_path_definition(variant->p.scope->scope_path,
197 variant_declaration->tag_name,
198 parent_scope);
199
200 if (!variant->enum_tag
201 || check_enum_tag(variant, variant->enum_tag) < 0)
202 goto error;
203 definition_ref(variant->enum_tag);
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);
206 for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
207 struct declaration_field *declaration_field =
208 &g_array_index(variant_declaration->untagged_variant->fields,
209 struct declaration_field, i);
210 struct definition **field =
211 (struct definition **) &g_ptr_array_index(variant->fields, i);
212
213 /*
214 * All child definition are at index 0, because they are
215 * various choices of the same field.
216 */
217 *field = declaration_field->declaration->definition_new(declaration_field->declaration,
218 variant->p.scope,
219 declaration_field->name, 0, NULL);
220 if (!*field)
221 goto error;
222 }
223 variant->current_field = NULL;
224 return &variant->p;
225 error:
226 free_definition_scope(variant->p.scope);
227 declaration_unref(&variant_declaration->p);
228 g_free(variant);
229 return NULL;
230 }
231
232 static
233 void _variant_definition_free(struct definition *definition)
234 {
235 struct definition_variant *variant =
236 container_of(definition, struct definition_variant, p);
237 unsigned long i;
238
239 assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
240 for (i = 0; i < variant->fields->len; i++) {
241 struct definition *field = g_ptr_array_index(variant->fields, i);
242 definition_unref(field);
243 }
244 definition_unref(variant->enum_tag);
245 free_definition_scope(variant->p.scope);
246 declaration_unref(variant->p.declaration);
247 g_free(variant);
248 }
249
250 void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
251 const char *field_name,
252 struct declaration *field_declaration)
253 {
254 struct declaration_field *field;
255 unsigned long index;
256
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;
263 /* Keep index in hash rather than pointer, because array can relocate */
264 g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
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
274 struct declaration_field *
275 untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag)
276 {
277 gpointer index;
278 gboolean found;
279
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);
289 }
290
291 /*
292 * field returned only valid as long as the field structure is not appended to.
293 */
294 struct definition *variant_get_current_field(struct definition_variant *variant)
295 {
296 struct definition_enum *_enum =
297 container_of(variant->enum_tag, struct definition_enum, p);
298 struct declaration_variant *variant_declaration = variant->declaration;
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);
310 index = (unsigned long) g_hash_table_lookup(variant_declaration->untagged_variant->fields_by_tag,
311 (gconstpointer) (unsigned long) tag);
312 variant->current_field = g_ptr_array_index(variant->fields, index);
313 return variant->current_field;
314 }
This page took 0.035352 seconds and 4 git commands to generate.