66c8f59f7051a0378360a2b947e61f6062bb11cb
4 * BabelTrace - Variant Type Converter
6 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/compiler.h>
20 #include <babeltrace/format.h>
24 struct definition
*_variant_definition_new(struct type
*type
,
25 struct definition_scope
*parent_scope
);
27 void _variant_definition_free(struct definition
*definition
);
29 void variant_copy(struct stream_pos
*dest
, const struct format
*fdest
,
30 struct stream_pos
*src
, const struct format
*fsrc
,
31 struct definition
*definition
)
33 struct definition_variant
*variant
=
34 container_of(definition
, struct definition_variant
, p
);
35 struct type_variant
*variant_type
= variant
->type
;
37 struct type
*field_type
;
39 fsrc
->variant_begin(src
, variant_type
);
40 fdest
->variant_begin(dest
, variant_type
);
42 field
= variant_get_current_field(variant
);
43 field_type
= field
->definition
->type
;
44 field_type
->copy(dest
, fdest
, src
, fsrc
, field
->definition
);
46 fsrc
->variant_end(src
, variant_type
);
47 fdest
->variant_end(dest
, variant_type
);
51 void _variant_type_free(struct type
*type
)
53 struct type_variant
*variant_type
=
54 container_of(type
, struct type_variant
, p
);
57 free_type_scope(variant_type
->scope
);
58 g_hash_table_destroy(variant_type
->fields_by_tag
);
60 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
61 struct type_field
*type_field
=
62 &g_array_index(variant_type
->fields
,
63 struct type_field
, i
);
64 type_unref(type_field
->type
);
66 g_array_free(variant_type
->fields
, true);
70 struct type_variant
*variant_type_new(const char *name
,
71 struct type_scope
*parent_scope
)
73 struct type_variant
*variant_type
;
76 variant_type
= g_new(struct type_variant
, 1);
77 type
= &variant_type
->p
;
78 variant_type
->fields_by_tag
= g_hash_table_new(g_direct_hash
,
80 variant_type
->fields
= g_array_sized_new(FALSE
, TRUE
,
81 sizeof(struct type_field
),
82 DEFAULT_NR_STRUCT_FIELDS
);
83 variant_type
->scope
= new_type_scope(parent_scope
);
84 type
->id
= CTF_TYPE_VARIANT
;
85 type
->name
= g_quark_from_string(name
);
87 type
->copy
= variant_copy
;
88 type
->type_free
= _variant_type_free
;
89 type
->definition_new
= _variant_definition_new
;
90 type
->definition_free
= _variant_definition_free
;
97 _variant_definition_new(struct type
*type
,
98 struct definition_scope
*parent_scope
)
100 struct type_variant
*variant_type
=
101 container_of(type
, struct type_variant
, p
);
102 struct definition_variant
*variant
;
105 variant
= g_new(struct definition_variant
, 1);
106 type_ref(&variant_type
->p
);
107 variant
->p
.type
= type
;
108 variant
->type
= variant_type
;
110 variant
->scope
= new_definition_scope(parent_scope
);
111 variant
->fields
= g_array_sized_new(FALSE
, TRUE
,
112 sizeof(struct field
),
113 DEFAULT_NR_STRUCT_FIELDS
);
114 g_array_set_size(variant
->fields
, variant_type
->fields
->len
);
115 for (i
= 0; i
< variant_type
->fields
->len
; i
++) {
116 struct type_field
*type_field
=
117 &g_array_index(variant_type
->fields
,
118 struct type_field
, i
);
119 struct field
*field
= &g_array_index(variant
->fields
,
122 field
->name
= type_field
->name
;
124 type_field
->type
->definition_new(type_field
->type
,
127 variant
->current_field
= NULL
;
132 void _variant_definition_free(struct definition
*definition
)
134 struct definition_variant
*variant
=
135 container_of(definition
, struct definition_variant
, p
);
138 assert(variant
->fields
->len
== variant
->type
->fields
->len
);
139 for (i
= 0; i
< variant
->fields
->len
; i
++) {
140 struct field
*field
= &g_array_index(variant
->fields
,
142 definition_unref(field
->definition
);
144 free_definition_scope(variant
->scope
);
145 type_unref(variant
->p
.type
);
149 void variant_type_add_field(struct type_variant
*variant_type
,
150 const char *tag_name
,
151 struct type
*tag_type
)
153 struct type_field
*field
;
156 g_array_set_size(variant_type
->fields
, variant_type
->fields
->len
+ 1);
157 index
= variant_type
->fields
->len
- 1; /* last field (new) */
158 field
= &g_array_index(variant_type
->fields
, struct type_field
, index
);
159 field
->name
= g_quark_from_string(tag_name
);
161 field
->type
= tag_type
;
162 /* Keep index in hash rather than pointer, because array can relocate */
163 g_hash_table_insert(variant_type
->fields_by_tag
,
164 (gpointer
) (unsigned long) field
->name
,
167 * Alignment of variant is based on the alignment of its currently
168 * selected choice, so we leave variant alignment as-is (statically
174 struct_type_get_field_from_tag(struct type_variant
*variant_type
, GQuark tag
)
178 index
= (unsigned long) g_hash_table_lookup(variant_type
->fields_by_tag
,
179 (gconstpointer
) (unsigned long) tag
);
180 return &g_array_index(variant_type
->fields
, struct type_field
, index
);
184 * tag_instance is assumed to be an enumeration.
186 int variant_definition_set_tag(struct definition_variant
*variant
,
187 struct definition
*enum_tag
)
189 struct definition_enum
*_enum
=
190 container_of(variant
->enum_tag
, struct definition_enum
, p
);
191 struct type_enum
*enum_type
= _enum
->type
;
192 int missing_field
= 0;
196 * Strictly speaking, each enumerator must map to a field of the
197 * variant. However, we are even stricter here by requiring that each
198 * variant choice map to an enumerator too. We then validate that the
199 * number of enumerators equals the number of variant choices.
201 if (variant
->type
->fields
->len
!= enum_get_nr_enumerators(enum_type
))
204 for (i
= 0; i
< variant
->type
->fields
->len
; i
++) {
205 struct type_field
*field_type
=
206 &g_array_index(variant
->type
->fields
,
207 struct type_field
, i
);
208 if (!enum_quark_to_range_set(enum_type
, field_type
->name
)) {
217 * Check the enumeration: it must map each value to one and only one
219 * TODO: we should also check that each range map to one and only one
220 * tag. For the moment, we will simply check this dynamically in
221 * variant_type_get_current_field().
224 /* Set the enum tag field */
225 variant
->enum_tag
= enum_tag
;
230 * field returned only valid as long as the field structure is not appended to.
232 struct field
*variant_get_current_field(struct definition_variant
*variant
)
234 struct definition_enum
*_enum
=
235 container_of(variant
->enum_tag
, struct definition_enum
, p
);
236 struct type_variant
*variant_type
= variant
->type
;
241 tag_array
= _enum
->value
;
243 * The 1 to 1 mapping from enumeration to value should have been already
244 * checked. (see TODO above)
246 assert(tag_array
->len
== 1);
247 tag
= g_array_index(tag_array
, GQuark
, 0);
248 index
= (unsigned long) g_hash_table_lookup(variant_type
->fields_by_tag
,
249 (gconstpointer
) (unsigned long) tag
);
250 variant
->current_field
= &g_array_index(variant
->fields
, struct field
, index
);
251 return variant
->current_field
;
This page took 0.033144 seconds and 3 git commands to generate.