4a8abcb1b406b40962a9a44d9cd3783e3c0b8170
[babeltrace.git] / types / variant.c
1 /*
2 * variant.c
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>
21 #include <errno.h>
22
23 static
24 struct definition *_variant_definition_new(struct declaration *declaration,
25 struct definition_scope *parent_scope,
26 GQuark field_name, int index);
27 static
28 void _variant_definition_free(struct definition *definition);
29
30 void variant_copy(struct stream_pos *dest, const struct format *fdest,
31 struct stream_pos *src, const struct format *fsrc,
32 struct definition *definition)
33 {
34 struct definition_variant *variant =
35 container_of(definition, struct definition_variant, p);
36 struct declaration_variant *variant_declaration = variant->declaration;
37 struct field *field;
38 struct declaration *field_declaration;
39
40 fsrc->variant_begin(src, variant_declaration);
41 fdest->variant_begin(dest, variant_declaration);
42
43 field = variant_get_current_field(variant);
44 field_declaration = field->definition->declaration;
45 field_declaration->copy(dest, fdest, src, fsrc, field->definition);
46
47 fsrc->variant_end(src, variant_declaration);
48 fdest->variant_end(dest, variant_declaration);
49 }
50
51 static
52 void _variant_declaration_free(struct declaration *declaration)
53 {
54 struct declaration_variant *variant_declaration =
55 container_of(declaration, struct declaration_variant, p);
56 unsigned long i;
57
58 free_declaration_scope(variant_declaration->scope);
59 g_hash_table_destroy(variant_declaration->fields_by_tag);
60
61 for (i = 0; i < variant_declaration->fields->len; i++) {
62 struct declaration_field *declaration_field =
63 &g_array_index(variant_declaration->fields,
64 struct declaration_field, i);
65 declaration_unref(declaration_field->declaration);
66 }
67 g_array_free(variant_declaration->fields, true);
68 g_free(variant_declaration);
69 }
70
71 struct declaration_variant *variant_declaration_new(const char *name,
72 struct declaration_scope *parent_scope)
73 {
74 struct declaration_variant *variant_declaration;
75 struct declaration *declaration;
76
77 variant_declaration = g_new(struct declaration_variant, 1);
78 declaration = &variant_declaration->p;
79 variant_declaration->fields_by_tag = g_hash_table_new(g_direct_hash,
80 g_direct_equal);
81 variant_declaration->fields = g_array_sized_new(FALSE, TRUE,
82 sizeof(struct declaration_field),
83 DEFAULT_NR_STRUCT_FIELDS);
84 variant_declaration->scope = new_declaration_scope(parent_scope);
85 declaration->id = CTF_TYPE_VARIANT;
86 declaration->name = g_quark_from_string(name);
87 declaration->alignment = 1;
88 declaration->copy = variant_copy;
89 declaration->declaration_free = _variant_declaration_free;
90 declaration->definition_new = _variant_definition_new;
91 declaration->definition_free = _variant_definition_free;
92 declaration->ref = 1;
93 return variant_declaration;
94 }
95
96 static
97 struct definition *
98 _variant_definition_new(struct declaration *declaration,
99 struct definition_scope *parent_scope,
100 GQuark field_name, int index)
101 {
102 struct declaration_variant *variant_declaration =
103 container_of(declaration, struct declaration_variant, p);
104 struct definition_variant *variant;
105 unsigned long i;
106
107 variant = g_new(struct definition_variant, 1);
108 declaration_ref(&variant_declaration->p);
109 variant->p.declaration = declaration;
110 variant->declaration = variant_declaration;
111 variant->p.ref = 1;
112 variant->p.index = index;
113 variant->scope = new_definition_scope(parent_scope, field_name);
114 variant->fields = g_array_sized_new(FALSE, TRUE,
115 sizeof(struct field),
116 DEFAULT_NR_STRUCT_FIELDS);
117 g_array_set_size(variant->fields, variant_declaration->fields->len);
118 for (i = 0; i < variant_declaration->fields->len; i++) {
119 struct declaration_field *declaration_field =
120 &g_array_index(variant_declaration->fields,
121 struct declaration_field, i);
122 struct field *field = &g_array_index(variant->fields,
123 struct field, i);
124
125 field->name = declaration_field->name;
126 /*
127 * All child definition are at index 0, because they are
128 * various choices of the same field.
129 */
130 field->definition =
131 declaration_field->declaration->definition_new(declaration_field->declaration,
132 variant->scope,
133 field->name, 0);
134 }
135 variant->current_field = NULL;
136 return &variant->p;
137 }
138
139 static
140 void _variant_definition_free(struct definition *definition)
141 {
142 struct definition_variant *variant =
143 container_of(definition, struct definition_variant, p);
144 unsigned long i;
145
146 assert(variant->fields->len == variant->declaration->fields->len);
147 for (i = 0; i < variant->fields->len; i++) {
148 struct field *field = &g_array_index(variant->fields,
149 struct field, i);
150 definition_unref(field->definition);
151 }
152 free_definition_scope(variant->scope);
153 declaration_unref(variant->p.declaration);
154 g_free(variant);
155 }
156
157 void variant_declaration_add_field(struct declaration_variant *variant_declaration,
158 const char *tag_name,
159 struct declaration *tag_declaration)
160 {
161 struct declaration_field *field;
162 unsigned long index;
163
164 g_array_set_size(variant_declaration->fields, variant_declaration->fields->len + 1);
165 index = variant_declaration->fields->len - 1; /* last field (new) */
166 field = &g_array_index(variant_declaration->fields, struct declaration_field, index);
167 field->name = g_quark_from_string(tag_name);
168 declaration_ref(tag_declaration);
169 field->declaration = tag_declaration;
170 /* Keep index in hash rather than pointer, because array can relocate */
171 g_hash_table_insert(variant_declaration->fields_by_tag,
172 (gpointer) (unsigned long) field->name,
173 (gpointer) index);
174 /*
175 * Alignment of variant is based on the alignment of its currently
176 * selected choice, so we leave variant alignment as-is (statically
177 * speaking).
178 */
179 }
180
181 struct declaration_field *
182 struct_declaration_get_field_from_tag(struct declaration_variant *variant_declaration, GQuark tag)
183 {
184 unsigned long index;
185
186 index = (unsigned long) g_hash_table_lookup(variant_declaration->fields_by_tag,
187 (gconstpointer) (unsigned long) tag);
188 return &g_array_index(variant_declaration->fields, struct declaration_field, index);
189 }
190
191 /*
192 * tag_instance is assumed to be an enumeration.
193 */
194 int variant_definition_set_tag(struct definition_variant *variant,
195 struct definition *enum_tag)
196 {
197 struct definition_enum *_enum =
198 container_of(variant->enum_tag, struct definition_enum, p);
199 struct declaration_enum *enum_declaration = _enum->declaration;
200 int missing_field = 0;
201 unsigned long i;
202
203 /*
204 * Strictly speaking, each enumerator must map to a field of the
205 * variant. However, we are even stricter here by requiring that each
206 * variant choice map to an enumerator too. We then validate that the
207 * number of enumerators equals the number of variant choices.
208 */
209 if (variant->declaration->fields->len != enum_get_nr_enumerators(enum_declaration))
210 return -EPERM;
211
212 for (i = 0; i < variant->declaration->fields->len; i++) {
213 struct declaration_field *field_declaration =
214 &g_array_index(variant->declaration->fields,
215 struct declaration_field, i);
216 if (!enum_quark_to_range_set(enum_declaration, field_declaration->name)) {
217 missing_field = 1;
218 break;
219 }
220 }
221 if (missing_field)
222 return -EPERM;
223
224 /*
225 * Check the enumeration: it must map each value to one and only one
226 * enumerator tag.
227 * TODO: we should also check that each range map to one and only one
228 * tag. For the moment, we will simply check this dynamically in
229 * variant_declaration_get_current_field().
230 */
231
232 /* Set the enum tag field */
233 variant->enum_tag = enum_tag;
234 return 0;
235 }
236
237 /*
238 * field returned only valid as long as the field structure is not appended to.
239 */
240 struct field *variant_get_current_field(struct definition_variant *variant)
241 {
242 struct definition_enum *_enum =
243 container_of(variant->enum_tag, struct definition_enum, p);
244 struct declaration_variant *variant_declaration = variant->declaration;
245 unsigned long index;
246 GArray *tag_array;
247 GQuark tag;
248
249 tag_array = _enum->value;
250 /*
251 * The 1 to 1 mapping from enumeration to value should have been already
252 * checked. (see TODO above)
253 */
254 assert(tag_array->len == 1);
255 tag = g_array_index(tag_array, GQuark, 0);
256 index = (unsigned long) g_hash_table_lookup(variant_declaration->fields_by_tag,
257 (gconstpointer) (unsigned long) tag);
258 variant->current_field = &g_array_index(variant->fields, struct field, index);
259 return variant->current_field;
260 }
This page took 0.033388 seconds and 3 git commands to generate.