Commit | Line | Data |
---|---|---|
5cd6d0e5 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
5cd6d0e5 PP |
3 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
4 | * | |
5cd6d0e5 PP |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
24 | #define BT_LOG_TAG "FIELD-CLASSES" | |
25 | #include <babeltrace/lib-logging-internal.h> | |
26 | ||
27 | #include <babeltrace/assert-pre-internal.h> | |
c6bd8523 PP |
28 | #include <babeltrace/trace-ir/field-class.h> |
29 | #include <babeltrace/trace-ir/field-class-const.h> | |
30 | #include <babeltrace/trace-ir/field-class-internal.h> | |
5cd6d0e5 | 31 | #include <babeltrace/trace-ir/field-path-internal.h> |
c6bd8523 PP |
32 | #include <babeltrace/trace-ir/field-internal.h> |
33 | #include <babeltrace/trace-ir/field-const.h> | |
34 | #include <babeltrace/trace-ir/field.h> | |
5cd6d0e5 | 35 | #include <babeltrace/trace-ir/utils-internal.h> |
65300d60 | 36 | #include <babeltrace/object.h> |
5cd6d0e5 PP |
37 | #include <babeltrace/trace-ir/clock-class.h> |
38 | #include <babeltrace/trace-ir/clock-class-internal.h> | |
39 | #include <babeltrace/object-internal.h> | |
65300d60 | 40 | #include <babeltrace/object.h> |
5cd6d0e5 PP |
41 | #include <babeltrace/compiler-internal.h> |
42 | #include <babeltrace/endian-internal.h> | |
43 | #include <babeltrace/assert-internal.h> | |
44 | #include <babeltrace/compat/glib-internal.h> | |
45 | #include <float.h> | |
46 | #include <inttypes.h> | |
47 | #include <stdlib.h> | |
48 | ||
40f4ba76 PP |
49 | enum bt_field_class_type bt_field_class_get_type( |
50 | const struct bt_field_class *fc) | |
5cd6d0e5 PP |
51 | { |
52 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 53 | return fc->type; |
5cd6d0e5 PP |
54 | } |
55 | ||
56 | static | |
864cad70 | 57 | void init_field_class(struct bt_field_class *fc, enum bt_field_class_type type, |
5cd6d0e5 PP |
58 | bt_object_release_func release_func) |
59 | { | |
60 | BT_ASSERT(fc); | |
864cad70 | 61 | BT_ASSERT(bt_field_class_has_known_type(fc)); |
5cd6d0e5 PP |
62 | BT_ASSERT(release_func); |
63 | bt_object_init_shared(&fc->base, release_func); | |
864cad70 | 64 | fc->type = type; |
5cd6d0e5 PP |
65 | } |
66 | ||
67 | static | |
864cad70 PP |
68 | void init_integer_field_class(struct bt_field_class_integer *fc, |
69 | enum bt_field_class_type type, | |
5cd6d0e5 PP |
70 | bt_object_release_func release_func) |
71 | { | |
864cad70 | 72 | init_field_class((void *) fc, type, release_func); |
5cd6d0e5 PP |
73 | fc->range = 64; |
74 | fc->base = BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL; | |
75 | } | |
76 | ||
77 | static | |
78 | void destroy_integer_field_class(struct bt_object *obj) | |
79 | { | |
80 | BT_ASSERT(obj); | |
e5be10ef | 81 | BT_LIB_LOGD("Destroying integer field class object: %!+F", obj); |
5cd6d0e5 PP |
82 | g_free(obj); |
83 | } | |
84 | ||
85 | static inline | |
864cad70 | 86 | struct bt_field_class *create_integer_field_class(enum bt_field_class_type type) |
5cd6d0e5 PP |
87 | { |
88 | struct bt_field_class_integer *int_fc = NULL; | |
89 | ||
e5be10ef | 90 | BT_LOGD("Creating default integer field class object: type=%s", |
864cad70 | 91 | bt_common_field_class_type_string(type)); |
5cd6d0e5 PP |
92 | int_fc = g_new0(struct bt_field_class_integer, 1); |
93 | if (!int_fc) { | |
e5be10ef | 94 | BT_LOGE_STR("Failed to allocate one integer field class."); |
5cd6d0e5 PP |
95 | goto error; |
96 | } | |
97 | ||
864cad70 | 98 | init_integer_field_class(int_fc, type, destroy_integer_field_class); |
e5be10ef | 99 | BT_LIB_LOGD("Created integer field class object: %!+F", int_fc); |
5cd6d0e5 PP |
100 | goto end; |
101 | ||
102 | error: | |
65300d60 | 103 | BT_OBJECT_PUT_REF_AND_RESET(int_fc); |
5cd6d0e5 PP |
104 | |
105 | end: | |
106 | return (void *) int_fc; | |
107 | } | |
108 | ||
40f4ba76 PP |
109 | struct bt_field_class * |
110 | bt_field_class_unsigned_integer_create(void) | |
5cd6d0e5 | 111 | { |
40f4ba76 | 112 | return create_integer_field_class( |
e5be10ef | 113 | BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER); |
5cd6d0e5 PP |
114 | } |
115 | ||
40f4ba76 | 116 | struct bt_field_class *bt_field_class_signed_integer_create(void) |
5cd6d0e5 | 117 | { |
40f4ba76 | 118 | return create_integer_field_class( |
e5be10ef | 119 | BT_FIELD_CLASS_TYPE_SIGNED_INTEGER); |
5cd6d0e5 PP |
120 | } |
121 | ||
122 | uint64_t bt_field_class_integer_get_field_value_range( | |
40f4ba76 | 123 | const struct bt_field_class *fc) |
5cd6d0e5 | 124 | { |
40f4ba76 | 125 | const struct bt_field_class_integer *int_fc = (const void *) fc; |
5cd6d0e5 PP |
126 | |
127 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
128 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
129 | return int_fc->range; | |
130 | } | |
131 | ||
132 | BT_ASSERT_PRE_FUNC | |
133 | static | |
134 | bool size_is_valid_for_enumeration_field_class(struct bt_field_class *fc, | |
135 | uint64_t size) | |
136 | { | |
137 | // TODO | |
138 | return true; | |
139 | } | |
140 | ||
40f4ba76 PP |
141 | void bt_field_class_integer_set_field_value_range( |
142 | struct bt_field_class *fc, uint64_t size) | |
5cd6d0e5 PP |
143 | { |
144 | struct bt_field_class_integer *int_fc = (void *) fc; | |
145 | ||
146 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
147 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
148 | BT_ASSERT_PRE_FC_HOT(fc, "Field class"); | |
149 | BT_ASSERT_PRE(size <= 64, | |
e5be10ef | 150 | "Unsupported size for integer field class's field value range " |
5cd6d0e5 | 151 | "(maximum is 64): size=%" PRIu64, size); |
864cad70 PP |
152 | BT_ASSERT_PRE( |
153 | int_fc->common.type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || | |
154 | int_fc->common.type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || | |
5cd6d0e5 | 155 | size_is_valid_for_enumeration_field_class(fc, size), |
e5be10ef | 156 | "Invalid field value range for enumeration field class: " |
5cd6d0e5 PP |
157 | "at least one of the current mapping ranges contains values " |
158 | "which are outside this range: %!+F, size=%" PRIu64, fc, size); | |
159 | int_fc->range = size; | |
e5be10ef | 160 | BT_LIB_LOGV("Set integer field class's field value range: %!+F", fc); |
5cd6d0e5 PP |
161 | } |
162 | ||
163 | enum bt_field_class_integer_preferred_display_base | |
40f4ba76 | 164 | bt_field_class_integer_get_preferred_display_base(const struct bt_field_class *fc) |
5cd6d0e5 | 165 | { |
40f4ba76 | 166 | const struct bt_field_class_integer *int_fc = (const void *) fc; |
5cd6d0e5 PP |
167 | |
168 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
169 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
170 | return int_fc->base; | |
171 | } | |
172 | ||
40f4ba76 PP |
173 | void bt_field_class_integer_set_preferred_display_base( |
174 | struct bt_field_class *fc, | |
5cd6d0e5 PP |
175 | enum bt_field_class_integer_preferred_display_base base) |
176 | { | |
177 | struct bt_field_class_integer *int_fc = (void *) fc; | |
178 | ||
179 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
180 | BT_ASSERT_PRE_FC_IS_INT(fc, "Field class"); | |
181 | BT_ASSERT_PRE_FC_HOT(fc, "Field class"); | |
182 | int_fc->base = base; | |
e5be10ef | 183 | BT_LIB_LOGV("Set integer field class's preferred display base: %!+F", fc); |
5cd6d0e5 PP |
184 | } |
185 | ||
186 | static | |
187 | void finalize_enumeration_field_class_mapping( | |
188 | struct bt_field_class_enumeration_mapping *mapping) | |
189 | { | |
190 | BT_ASSERT(mapping); | |
191 | ||
192 | if (mapping->label) { | |
193 | g_string_free(mapping->label, TRUE); | |
194 | } | |
195 | ||
196 | if (mapping->ranges) { | |
197 | g_array_free(mapping->ranges, TRUE); | |
198 | } | |
199 | } | |
200 | ||
201 | static | |
202 | void destroy_enumeration_field_class(struct bt_object *obj) | |
203 | { | |
204 | struct bt_field_class_enumeration *fc = (void *) obj; | |
205 | ||
206 | BT_ASSERT(fc); | |
e5be10ef | 207 | BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc); |
5cd6d0e5 PP |
208 | |
209 | if (fc->mappings) { | |
210 | uint64_t i; | |
211 | ||
212 | for (i = 0; i < fc->mappings->len; i++) { | |
213 | finalize_enumeration_field_class_mapping( | |
214 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, i)); | |
215 | } | |
216 | ||
217 | g_array_free(fc->mappings, TRUE); | |
238b7404 | 218 | fc->mappings = NULL; |
5cd6d0e5 PP |
219 | } |
220 | ||
221 | if (fc->label_buf) { | |
222 | g_ptr_array_free(fc->label_buf, TRUE); | |
238b7404 | 223 | fc->label_buf = NULL; |
5cd6d0e5 PP |
224 | } |
225 | ||
226 | g_free(fc); | |
227 | } | |
228 | ||
229 | static | |
40f4ba76 PP |
230 | struct bt_field_class *create_enumeration_field_class( |
231 | enum bt_field_class_type type) | |
5cd6d0e5 PP |
232 | { |
233 | struct bt_field_class_enumeration *enum_fc = NULL; | |
234 | ||
e5be10ef | 235 | BT_LOGD("Creating default enumeration field class object: type=%s", |
864cad70 | 236 | bt_common_field_class_type_string(type)); |
5cd6d0e5 PP |
237 | enum_fc = g_new0(struct bt_field_class_enumeration, 1); |
238 | if (!enum_fc) { | |
e5be10ef | 239 | BT_LOGE_STR("Failed to allocate one enumeration field class."); |
5cd6d0e5 PP |
240 | goto error; |
241 | } | |
242 | ||
864cad70 | 243 | init_integer_field_class((void *) enum_fc, type, |
5cd6d0e5 PP |
244 | destroy_enumeration_field_class); |
245 | enum_fc->mappings = g_array_new(FALSE, TRUE, | |
246 | sizeof(struct bt_field_class_enumeration_mapping)); | |
247 | if (!enum_fc->mappings) { | |
248 | BT_LOGE_STR("Failed to allocate a GArray."); | |
249 | goto error; | |
250 | } | |
251 | ||
252 | enum_fc->label_buf = g_ptr_array_new(); | |
253 | if (!enum_fc->label_buf) { | |
254 | BT_LOGE_STR("Failed to allocate a GArray."); | |
255 | goto error; | |
256 | } | |
257 | ||
e5be10ef | 258 | BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc); |
5cd6d0e5 PP |
259 | goto end; |
260 | ||
261 | error: | |
65300d60 | 262 | BT_OBJECT_PUT_REF_AND_RESET(enum_fc); |
5cd6d0e5 PP |
263 | |
264 | end: | |
265 | return (void *) enum_fc; | |
266 | } | |
267 | ||
40f4ba76 | 268 | struct bt_field_class *bt_field_class_unsigned_enumeration_create(void) |
5cd6d0e5 | 269 | { |
40f4ba76 | 270 | return create_enumeration_field_class( |
864cad70 | 271 | BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION); |
5cd6d0e5 PP |
272 | } |
273 | ||
40f4ba76 | 274 | struct bt_field_class *bt_field_class_signed_enumeration_create(void) |
5cd6d0e5 | 275 | { |
40f4ba76 | 276 | return create_enumeration_field_class( |
864cad70 | 277 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION); |
5cd6d0e5 PP |
278 | } |
279 | ||
40f4ba76 PP |
280 | uint64_t bt_field_class_enumeration_get_mapping_count( |
281 | const struct bt_field_class *fc) | |
5cd6d0e5 | 282 | { |
40f4ba76 | 283 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
5cd6d0e5 PP |
284 | |
285 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
286 | BT_ASSERT_PRE_FC_IS_ENUM(fc, "Field class"); | |
287 | return (uint64_t) enum_fc->mappings->len; | |
288 | } | |
289 | ||
40f4ba76 PP |
290 | void bt_field_class_unsigned_enumeration_borrow_mapping_by_index_const( |
291 | const struct bt_field_class *fc, uint64_t index, | |
5cd6d0e5 | 292 | const char **name, |
40f4ba76 | 293 | const struct bt_field_class_unsigned_enumeration_mapping_ranges **ranges) |
5cd6d0e5 | 294 | { |
40f4ba76 PP |
295 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
296 | const struct bt_field_class_enumeration_mapping *mapping; | |
5cd6d0e5 PP |
297 | |
298 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
299 | BT_ASSERT_PRE_NON_NULL(name, "Name (output)"); | |
300 | BT_ASSERT_PRE_NON_NULL(ranges, "Ranges (output)"); | |
301 | BT_ASSERT_PRE_VALID_INDEX(index, enum_fc->mappings->len); | |
864cad70 | 302 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
5cd6d0e5 PP |
303 | "Field class"); |
304 | mapping = BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); | |
305 | *name = mapping->label->str; | |
306 | *ranges = (void *) mapping; | |
307 | } | |
308 | ||
40f4ba76 PP |
309 | void bt_field_class_signed_enumeration_borrow_mapping_by_index_const( |
310 | const struct bt_field_class *fc, uint64_t index, | |
5cd6d0e5 | 311 | const char **name, |
40f4ba76 | 312 | const struct bt_field_class_signed_enumeration_mapping_ranges **ranges) |
5cd6d0e5 | 313 | { |
40f4ba76 PP |
314 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
315 | const struct bt_field_class_enumeration_mapping *mapping; | |
5cd6d0e5 PP |
316 | |
317 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
318 | BT_ASSERT_PRE_NON_NULL(name, "Name (output)"); | |
319 | BT_ASSERT_PRE_NON_NULL(ranges, "Ranges (output)"); | |
320 | BT_ASSERT_PRE_VALID_INDEX(index, enum_fc->mappings->len); | |
864cad70 | 321 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
5cd6d0e5 PP |
322 | "Field class"); |
323 | mapping = BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc, index); | |
324 | *name = mapping->label->str; | |
325 | *ranges = (void *) mapping; | |
326 | } | |
327 | ||
328 | static inline | |
329 | uint64_t get_enumeration_field_class_mapping_range_count( | |
40f4ba76 | 330 | const struct bt_field_class_enumeration_mapping *mapping) |
5cd6d0e5 PP |
331 | { |
332 | BT_ASSERT_PRE_NON_NULL(mapping, "Ranges"); | |
333 | return (uint64_t) mapping->ranges->len; | |
334 | } | |
335 | ||
336 | uint64_t bt_field_class_unsigned_enumeration_mapping_ranges_get_range_count( | |
40f4ba76 | 337 | const struct bt_field_class_unsigned_enumeration_mapping_ranges *ranges) |
5cd6d0e5 | 338 | { |
40f4ba76 PP |
339 | return get_enumeration_field_class_mapping_range_count( |
340 | (const void *) ranges); | |
5cd6d0e5 PP |
341 | } |
342 | ||
343 | uint64_t bt_field_class_signed_enumeration_mapping_ranges_get_range_count( | |
40f4ba76 | 344 | const struct bt_field_class_signed_enumeration_mapping_ranges *ranges) |
5cd6d0e5 | 345 | { |
40f4ba76 PP |
346 | return get_enumeration_field_class_mapping_range_count( |
347 | (const void *) ranges); | |
5cd6d0e5 PP |
348 | } |
349 | ||
350 | static inline | |
351 | void get_enumeration_field_class_mapping_range_at_index( | |
40f4ba76 | 352 | const struct bt_field_class_enumeration_mapping *mapping, |
5cd6d0e5 PP |
353 | uint64_t index, uint64_t *lower, uint64_t *upper) |
354 | { | |
40f4ba76 | 355 | const struct bt_field_class_enumeration_mapping_range *range; |
5cd6d0e5 PP |
356 | |
357 | BT_ASSERT_PRE_NON_NULL(mapping, "Ranges"); | |
358 | BT_ASSERT_PRE_NON_NULL(lower, "Range's lower (output)"); | |
359 | BT_ASSERT_PRE_NON_NULL(upper, "Range's upper (output)"); | |
360 | BT_ASSERT_PRE_VALID_INDEX(index, mapping->ranges->len); | |
361 | range = BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping, index); | |
362 | *lower = range->lower.u; | |
363 | *upper = range->upper.u; | |
364 | } | |
365 | ||
366 | void bt_field_class_unsigned_enumeration_mapping_ranges_get_range_by_index( | |
40f4ba76 | 367 | const struct bt_field_class_unsigned_enumeration_mapping_ranges *ranges, |
5cd6d0e5 PP |
368 | uint64_t index, uint64_t *lower, uint64_t *upper) |
369 | { | |
40f4ba76 PP |
370 | get_enumeration_field_class_mapping_range_at_index( |
371 | (const void *) ranges, index, lower, upper); | |
5cd6d0e5 PP |
372 | } |
373 | ||
374 | void bt_field_class_signed_enumeration_mapping_ranges_get_range_by_index( | |
40f4ba76 | 375 | const struct bt_field_class_unsigned_enumeration_mapping_ranges *ranges, |
5cd6d0e5 PP |
376 | uint64_t index, int64_t *lower, int64_t *upper) |
377 | { | |
40f4ba76 PP |
378 | get_enumeration_field_class_mapping_range_at_index( |
379 | (const void *) ranges, index, | |
380 | (uint64_t *) lower, (uint64_t *) upper); | |
5cd6d0e5 PP |
381 | } |
382 | ||
383 | ||
384 | ||
385 | int bt_field_class_unsigned_enumeration_get_mapping_labels_by_value( | |
40f4ba76 | 386 | const struct bt_field_class *fc, uint64_t value, |
5cd6d0e5 PP |
387 | bt_field_class_enumeration_mapping_label_array *label_array, |
388 | uint64_t *count) | |
389 | { | |
40f4ba76 | 390 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
5cd6d0e5 PP |
391 | uint64_t i; |
392 | ||
393 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
394 | BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)"); | |
395 | BT_ASSERT_PRE_NON_NULL(count, "Count (output)"); | |
864cad70 | 396 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
5cd6d0e5 PP |
397 | "Field class"); |
398 | g_ptr_array_set_size(enum_fc->label_buf, 0); | |
399 | ||
400 | for (i = 0; i < enum_fc->mappings->len; i++) { | |
401 | uint64_t j; | |
40f4ba76 | 402 | const struct bt_field_class_enumeration_mapping *mapping = |
5cd6d0e5 PP |
403 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
404 | ||
405 | for (j = 0; j < mapping->ranges->len; j++) { | |
40f4ba76 | 406 | const struct bt_field_class_enumeration_mapping_range *range = |
5cd6d0e5 PP |
407 | BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX( |
408 | mapping, j); | |
409 | ||
410 | if (value >= range->lower.u && | |
411 | value <= range->upper.u) { | |
412 | g_ptr_array_add(enum_fc->label_buf, | |
413 | mapping->label->str); | |
414 | break; | |
415 | } | |
416 | } | |
417 | } | |
418 | ||
419 | *label_array = (void *) enum_fc->label_buf->pdata; | |
420 | *count = (uint64_t) enum_fc->label_buf->len; | |
421 | return 0; | |
422 | } | |
423 | ||
424 | int bt_field_class_signed_enumeration_get_mapping_labels_by_value( | |
40f4ba76 | 425 | const struct bt_field_class *fc, int64_t value, |
5cd6d0e5 PP |
426 | bt_field_class_enumeration_mapping_label_array *label_array, |
427 | uint64_t *count) | |
428 | { | |
40f4ba76 | 429 | const struct bt_field_class_enumeration *enum_fc = (const void *) fc; |
5cd6d0e5 PP |
430 | uint64_t i; |
431 | ||
432 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
433 | BT_ASSERT_PRE_NON_NULL(label_array, "Label array (output)"); | |
434 | BT_ASSERT_PRE_NON_NULL(count, "Count (output)"); | |
864cad70 | 435 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
5cd6d0e5 PP |
436 | "Field class"); |
437 | g_ptr_array_set_size(enum_fc->label_buf, 0); | |
438 | ||
439 | for (i = 0; i < enum_fc->mappings->len; i++) { | |
440 | uint64_t j; | |
40f4ba76 | 441 | const struct bt_field_class_enumeration_mapping *mapping = |
5cd6d0e5 PP |
442 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); |
443 | ||
444 | for (j = 0; j < mapping->ranges->len; j++) { | |
40f4ba76 | 445 | const struct bt_field_class_enumeration_mapping_range *range = |
5cd6d0e5 PP |
446 | BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX( |
447 | mapping, j); | |
448 | ||
449 | if (value >= range->lower.i && | |
450 | value <= range->upper.i) { | |
451 | g_ptr_array_add(enum_fc->label_buf, | |
452 | mapping->label->str); | |
453 | break; | |
454 | } | |
455 | } | |
456 | } | |
457 | ||
458 | *label_array = (void *) enum_fc->label_buf->pdata; | |
459 | *count = (uint64_t) enum_fc->label_buf->len; | |
460 | return 0; | |
461 | } | |
462 | ||
463 | static inline | |
464 | int add_mapping_to_enumeration_field_class(struct bt_field_class *fc, | |
465 | const char *label, uint64_t lower, uint64_t upper) | |
466 | { | |
467 | int ret = 0; | |
468 | uint64_t i; | |
469 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
470 | struct bt_field_class_enumeration_mapping *mapping = NULL; | |
471 | struct bt_field_class_enumeration_mapping_range *range; | |
472 | ||
473 | BT_ASSERT(fc); | |
474 | BT_ASSERT_PRE_NON_NULL(label, "Label"); | |
475 | ||
476 | /* Find existing mapping identified by this label */ | |
477 | for (i = 0; i < enum_fc->mappings->len; i++) { | |
478 | struct bt_field_class_enumeration_mapping *mapping_candidate = | |
479 | BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, i); | |
480 | ||
481 | if (strcmp(mapping_candidate->label->str, label) == 0) { | |
482 | mapping = mapping_candidate; | |
483 | break; | |
484 | } | |
485 | } | |
486 | ||
487 | if (!mapping) { | |
488 | /* Create new mapping for this label */ | |
489 | g_array_set_size(enum_fc->mappings, enum_fc->mappings->len + 1); | |
490 | mapping = BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc, | |
491 | enum_fc->mappings->len - 1); | |
492 | mapping->ranges = g_array_new(FALSE, TRUE, | |
493 | sizeof(struct bt_field_class_enumeration_mapping_range)); | |
494 | if (!mapping->ranges) { | |
495 | finalize_enumeration_field_class_mapping(mapping); | |
496 | g_array_set_size(enum_fc->mappings, | |
497 | enum_fc->mappings->len - 1); | |
498 | ret = -1; | |
499 | goto end; | |
500 | } | |
501 | ||
502 | mapping->label = g_string_new(label); | |
503 | if (!mapping->label) { | |
504 | finalize_enumeration_field_class_mapping(mapping); | |
505 | g_array_set_size(enum_fc->mappings, | |
506 | enum_fc->mappings->len - 1); | |
507 | ret = -1; | |
508 | goto end; | |
509 | } | |
510 | } | |
511 | ||
512 | /* Add range */ | |
513 | BT_ASSERT(mapping); | |
514 | g_array_set_size(mapping->ranges, mapping->ranges->len + 1); | |
515 | range = BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping, | |
516 | mapping->ranges->len - 1); | |
517 | range->lower.u = lower; | |
518 | range->upper.u = upper; | |
e5be10ef | 519 | BT_LIB_LOGV("Added mapping to enumeration field class: " |
5cd6d0e5 PP |
520 | "%![fc-]+F, label=\"%s\", lower-unsigned=%" PRIu64 ", " |
521 | "upper-unsigned=%" PRIu64, fc, label, lower, upper); | |
522 | ||
523 | end: | |
524 | return ret; | |
525 | } | |
526 | ||
40f4ba76 PP |
527 | int bt_field_class_unsigned_enumeration_map_range( |
528 | struct bt_field_class *fc, const char *label, | |
5cd6d0e5 PP |
529 | uint64_t range_lower, uint64_t range_upper) |
530 | { | |
531 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
532 | ||
533 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 534 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, |
5cd6d0e5 PP |
535 | "Field class"); |
536 | BT_ASSERT_PRE(range_lower <= range_upper, | |
537 | "Range's upper bound is less than lower bound: " | |
538 | "upper=%" PRIu64 ", lower=%" PRIu64, | |
539 | range_lower, range_upper); | |
540 | BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc->common.range, | |
541 | range_lower), | |
e5be10ef | 542 | "Range's lower bound is outside the enumeration field class's value range: " |
5cd6d0e5 PP |
543 | "%![fc-]+F, lower=%" PRIu64, fc, range_lower); |
544 | BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc->common.range, | |
545 | range_upper), | |
e5be10ef | 546 | "Range's upper bound is outside the enumeration field class's value range: " |
5cd6d0e5 PP |
547 | "%![fc-]+F, upper=%" PRIu64, fc, range_upper); |
548 | return add_mapping_to_enumeration_field_class(fc, label, range_lower, | |
549 | range_upper); | |
550 | } | |
551 | ||
40f4ba76 PP |
552 | int bt_field_class_signed_enumeration_map_range( |
553 | struct bt_field_class *fc, const char *label, | |
5cd6d0e5 PP |
554 | int64_t range_lower, int64_t range_upper) |
555 | { | |
556 | struct bt_field_class_enumeration *enum_fc = (void *) fc; | |
557 | ||
558 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 559 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, |
5cd6d0e5 PP |
560 | "Field class"); |
561 | BT_ASSERT_PRE(range_lower <= range_upper, | |
562 | "Range's upper bound is less than lower bound: " | |
563 | "upper=%" PRId64 ", lower=%" PRId64, | |
564 | range_lower, range_upper); | |
565 | BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc->common.range, | |
566 | range_lower), | |
e5be10ef | 567 | "Range's lower bound is outside the enumeration field class's value range: " |
5cd6d0e5 PP |
568 | "%![fc-]+F, lower=%" PRId64, fc, range_lower); |
569 | BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc->common.range, | |
570 | range_upper), | |
e5be10ef | 571 | "Range's upper bound is outside the enumeration field class's value range: " |
5cd6d0e5 PP |
572 | "%![fc-]+F, upper=%" PRId64, fc, range_upper); |
573 | return add_mapping_to_enumeration_field_class(fc, label, range_lower, | |
574 | range_upper); | |
575 | } | |
576 | ||
577 | static | |
578 | void destroy_real_field_class(struct bt_object *obj) | |
579 | { | |
580 | BT_ASSERT(obj); | |
e5be10ef | 581 | BT_LIB_LOGD("Destroying real field class object: %!+F", obj); |
5cd6d0e5 PP |
582 | g_free(obj); |
583 | } | |
584 | ||
40f4ba76 | 585 | struct bt_field_class *bt_field_class_real_create(void) |
5cd6d0e5 PP |
586 | { |
587 | struct bt_field_class_real *real_fc = NULL; | |
588 | ||
e5be10ef | 589 | BT_LOGD_STR("Creating default real field class object."); |
5cd6d0e5 PP |
590 | real_fc = g_new0(struct bt_field_class_real, 1); |
591 | if (!real_fc) { | |
e5be10ef | 592 | BT_LOGE_STR("Failed to allocate one real field class."); |
5cd6d0e5 PP |
593 | goto error; |
594 | } | |
595 | ||
864cad70 | 596 | init_field_class((void *) real_fc, BT_FIELD_CLASS_TYPE_REAL, |
5cd6d0e5 | 597 | destroy_real_field_class); |
e5be10ef | 598 | BT_LIB_LOGD("Created real field class object: %!+F", real_fc); |
5cd6d0e5 PP |
599 | goto end; |
600 | ||
601 | error: | |
65300d60 | 602 | BT_OBJECT_PUT_REF_AND_RESET(real_fc); |
5cd6d0e5 PP |
603 | |
604 | end: | |
605 | return (void *) real_fc; | |
606 | } | |
607 | ||
40f4ba76 | 608 | bt_bool bt_field_class_real_is_single_precision(const struct bt_field_class *fc) |
5cd6d0e5 | 609 | { |
40f4ba76 | 610 | const struct bt_field_class_real *real_fc = (const void *) fc; |
5cd6d0e5 PP |
611 | |
612 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 613 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class"); |
5cd6d0e5 PP |
614 | return real_fc->is_single_precision; |
615 | } | |
616 | ||
40f4ba76 | 617 | void bt_field_class_real_set_is_single_precision(struct bt_field_class *fc, |
5cd6d0e5 PP |
618 | bt_bool is_single_precision) |
619 | { | |
620 | struct bt_field_class_real *real_fc = (void *) fc; | |
621 | ||
622 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 623 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_REAL, "Field class"); |
5cd6d0e5 PP |
624 | BT_ASSERT_PRE_FC_HOT(fc, "Field class"); |
625 | real_fc->is_single_precision = (bool) is_single_precision; | |
e5be10ef | 626 | BT_LIB_LOGV("Set real field class's \"is single precision\" property: " |
5cd6d0e5 | 627 | "%!+F", fc); |
5cd6d0e5 PP |
628 | } |
629 | ||
630 | static | |
631 | int init_named_field_classes_container( | |
632 | struct bt_field_class_named_field_class_container *fc, | |
40f4ba76 PP |
633 | enum bt_field_class_type type, |
634 | bt_object_release_func release_func) | |
5cd6d0e5 PP |
635 | { |
636 | int ret = 0; | |
637 | ||
864cad70 | 638 | init_field_class((void *) fc, type, release_func); |
5cd6d0e5 PP |
639 | fc->named_fcs = g_array_new(FALSE, TRUE, |
640 | sizeof(struct bt_named_field_class)); | |
641 | if (!fc->named_fcs) { | |
642 | BT_LOGE_STR("Failed to allocate a GArray."); | |
643 | ret = -1; | |
644 | goto end; | |
645 | } | |
646 | ||
647 | fc->name_to_index = g_hash_table_new(g_str_hash, g_str_equal); | |
648 | if (!fc->name_to_index) { | |
649 | BT_LOGE_STR("Failed to allocate a GHashTable."); | |
650 | ret = -1; | |
651 | goto end; | |
652 | } | |
653 | ||
654 | end: | |
655 | return ret; | |
656 | } | |
657 | ||
658 | static | |
659 | void finalize_named_field_class(struct bt_named_field_class *named_fc) | |
660 | { | |
661 | BT_ASSERT(named_fc); | |
e5be10ef | 662 | BT_LIB_LOGD("Finalizing named field class: " |
5cd6d0e5 PP |
663 | "addr=%p, name=\"%s\", %![fc-]+F", |
664 | named_fc, named_fc->name ? named_fc->name->str : NULL, | |
665 | named_fc->fc); | |
666 | ||
667 | if (named_fc->name) { | |
668 | g_string_free(named_fc->name, TRUE); | |
669 | } | |
670 | ||
e5be10ef | 671 | BT_LOGD_STR("Putting named field class's field class."); |
238b7404 | 672 | BT_OBJECT_PUT_REF_AND_RESET(named_fc->fc); |
5cd6d0e5 PP |
673 | } |
674 | ||
675 | static | |
676 | void finalize_named_field_classes_container( | |
677 | struct bt_field_class_named_field_class_container *fc) | |
678 | { | |
679 | uint64_t i; | |
680 | ||
681 | BT_ASSERT(fc); | |
682 | ||
683 | if (fc->named_fcs) { | |
684 | for (i = 0; i < fc->named_fcs->len; i++) { | |
685 | finalize_named_field_class( | |
686 | &g_array_index(fc->named_fcs, | |
687 | struct bt_named_field_class, i)); | |
688 | } | |
689 | ||
690 | g_array_free(fc->named_fcs, TRUE); | |
691 | } | |
692 | ||
693 | if (fc->name_to_index) { | |
694 | g_hash_table_destroy(fc->name_to_index); | |
695 | } | |
696 | } | |
697 | ||
698 | static | |
699 | void destroy_structure_field_class(struct bt_object *obj) | |
700 | { | |
701 | BT_ASSERT(obj); | |
e5be10ef | 702 | BT_LIB_LOGD("Destroying string field class object: %!+F", obj); |
5cd6d0e5 PP |
703 | finalize_named_field_classes_container((void *) obj); |
704 | g_free(obj); | |
705 | } | |
706 | ||
40f4ba76 | 707 | struct bt_field_class *bt_field_class_structure_create(void) |
5cd6d0e5 PP |
708 | { |
709 | int ret; | |
710 | struct bt_field_class_structure *struct_fc = NULL; | |
711 | ||
e5be10ef | 712 | BT_LOGD_STR("Creating default structure field class object."); |
5cd6d0e5 PP |
713 | struct_fc = g_new0(struct bt_field_class_structure, 1); |
714 | if (!struct_fc) { | |
e5be10ef | 715 | BT_LOGE_STR("Failed to allocate one structure field class."); |
5cd6d0e5 PP |
716 | goto error; |
717 | } | |
718 | ||
719 | ret = init_named_field_classes_container((void *) struct_fc, | |
864cad70 | 720 | BT_FIELD_CLASS_TYPE_STRUCTURE, destroy_structure_field_class); |
5cd6d0e5 PP |
721 | if (ret) { |
722 | goto error; | |
723 | } | |
724 | ||
e5be10ef | 725 | BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc); |
5cd6d0e5 PP |
726 | goto end; |
727 | ||
728 | error: | |
65300d60 | 729 | BT_OBJECT_PUT_REF_AND_RESET(struct_fc); |
5cd6d0e5 PP |
730 | |
731 | end: | |
732 | return (void *) struct_fc; | |
733 | } | |
734 | ||
735 | static | |
736 | int append_named_field_class_to_container_field_class( | |
737 | struct bt_field_class_named_field_class_container *container_fc, | |
738 | const char *name, struct bt_field_class *fc) | |
739 | { | |
740 | int ret = 0; | |
741 | struct bt_named_field_class *named_fc; | |
742 | GString *name_str; | |
743 | ||
744 | BT_ASSERT(container_fc); | |
745 | BT_ASSERT_PRE_FC_HOT(container_fc, "Field class"); | |
746 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
747 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
748 | BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc->name_to_index, | |
749 | name), | |
e5be10ef | 750 | "Duplicate member/option name in structure/variant field class: " |
5cd6d0e5 PP |
751 | "%![container-fc-]+F, name=\"%s\"", container_fc, name); |
752 | name_str = g_string_new(name); | |
753 | if (!name_str) { | |
754 | BT_LOGE_STR("Failed to allocate a GString."); | |
755 | ret = -1; | |
756 | goto end; | |
757 | } | |
758 | ||
759 | g_array_set_size(container_fc->named_fcs, | |
760 | container_fc->named_fcs->len + 1); | |
761 | named_fc = &g_array_index(container_fc->named_fcs, | |
762 | struct bt_named_field_class, container_fc->named_fcs->len - 1); | |
763 | named_fc->name = name_str; | |
398454ed PP |
764 | named_fc->fc = fc; |
765 | bt_object_get_no_null_check(fc); | |
5cd6d0e5 PP |
766 | g_hash_table_insert(container_fc->name_to_index, named_fc->name->str, |
767 | GUINT_TO_POINTER(container_fc->named_fcs->len - 1)); | |
768 | bt_field_class_freeze(fc); | |
769 | ||
770 | end: | |
771 | return ret; | |
772 | } | |
773 | ||
40f4ba76 PP |
774 | int bt_field_class_structure_append_member(struct bt_field_class *fc, |
775 | const char *name, struct bt_field_class *member_fc) | |
5cd6d0e5 | 776 | { |
e5be10ef | 777 | |
5cd6d0e5 | 778 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
864cad70 | 779 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class"); |
5cd6d0e5 | 780 | return append_named_field_class_to_container_field_class((void *) fc, |
40f4ba76 | 781 | name, member_fc); |
5cd6d0e5 PP |
782 | } |
783 | ||
40f4ba76 PP |
784 | uint64_t bt_field_class_structure_get_member_count( |
785 | const struct bt_field_class *fc) | |
5cd6d0e5 PP |
786 | { |
787 | struct bt_field_class_structure *struct_fc = (void *) fc; | |
788 | ||
789 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 790 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class"); |
5cd6d0e5 PP |
791 | return (uint64_t) struct_fc->common.named_fcs->len; |
792 | } | |
793 | ||
794 | static | |
40f4ba76 PP |
795 | void borrow_named_field_class_from_container_field_class_at_index_const( |
796 | const struct bt_field_class_named_field_class_container *fc, | |
5cd6d0e5 | 797 | uint64_t index, const char **name, |
40f4ba76 | 798 | const struct bt_field_class **out_fc) |
5cd6d0e5 | 799 | { |
40f4ba76 | 800 | const struct bt_named_field_class *named_fc; |
5cd6d0e5 PP |
801 | |
802 | BT_ASSERT(fc); | |
803 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
804 | BT_ASSERT_PRE_NON_NULL(out_fc, "Field class (output)"); | |
805 | BT_ASSERT_PRE_VALID_INDEX(index, fc->named_fcs->len); | |
806 | named_fc = BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, index); | |
807 | *name = named_fc->name->str; | |
808 | *out_fc = named_fc->fc; | |
809 | } | |
810 | ||
40f4ba76 PP |
811 | void bt_field_class_structure_borrow_member_by_index_const( |
812 | const struct bt_field_class *fc, uint64_t index, | |
813 | const char **name, const struct bt_field_class **out_fc) | |
5cd6d0e5 PP |
814 | { |
815 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 816 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class"); |
40f4ba76 PP |
817 | borrow_named_field_class_from_container_field_class_at_index_const( |
818 | (void *) fc, index, name, out_fc); | |
e5be10ef PP |
819 | } |
820 | ||
5cd6d0e5 | 821 | static |
40f4ba76 PP |
822 | const struct bt_field_class * |
823 | borrow_field_class_from_container_field_class_by_name_const( | |
824 | const struct bt_field_class_named_field_class_container *fc, | |
5cd6d0e5 PP |
825 | const char *name) |
826 | { | |
40f4ba76 PP |
827 | const struct bt_field_class *ret_fc = NULL; |
828 | const struct bt_named_field_class *named_fc; | |
5cd6d0e5 PP |
829 | gpointer orig_key; |
830 | gpointer value; | |
831 | ||
832 | BT_ASSERT(fc); | |
833 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
834 | if (!g_hash_table_lookup_extended(fc->name_to_index, name, &orig_key, | |
835 | &value)) { | |
836 | goto end; | |
837 | } | |
838 | ||
839 | named_fc = BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc, | |
840 | GPOINTER_TO_UINT(value)); | |
841 | ret_fc = named_fc->fc; | |
842 | ||
843 | end: | |
844 | return ret_fc; | |
845 | } | |
846 | ||
40f4ba76 PP |
847 | const struct bt_field_class * |
848 | bt_field_class_structure_borrow_member_field_class_by_name( | |
849 | const struct bt_field_class *fc, const char *name) | |
5cd6d0e5 PP |
850 | { |
851 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 852 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STRUCTURE, "Field class"); |
40f4ba76 PP |
853 | return borrow_field_class_from_container_field_class_by_name_const( |
854 | (void *) fc, name); | |
e5be10ef PP |
855 | } |
856 | ||
5cd6d0e5 PP |
857 | static |
858 | void destroy_variant_field_class(struct bt_object *obj) | |
859 | { | |
860 | struct bt_field_class_variant *fc = (void *) obj; | |
861 | ||
862 | BT_ASSERT(fc); | |
e5be10ef | 863 | BT_LIB_LOGD("Destroying variant field class object: %!+F", fc); |
5cd6d0e5 PP |
864 | finalize_named_field_classes_container((void *) fc); |
865 | BT_LOGD_STR("Putting selector field path."); | |
238b7404 PP |
866 | BT_OBJECT_PUT_REF_AND_RESET(fc->selector_field_path); |
867 | BT_LOGD_STR("Putting selector field class."); | |
868 | BT_OBJECT_PUT_REF_AND_RESET(fc->selector_fc); | |
5cd6d0e5 PP |
869 | g_free(fc); |
870 | } | |
871 | ||
40f4ba76 | 872 | struct bt_field_class *bt_field_class_variant_create(void) |
5cd6d0e5 PP |
873 | { |
874 | int ret; | |
875 | struct bt_field_class_variant *var_fc = NULL; | |
876 | ||
e5be10ef | 877 | BT_LOGD_STR("Creating default variant field class object."); |
5cd6d0e5 PP |
878 | var_fc = g_new0(struct bt_field_class_variant, 1); |
879 | if (!var_fc) { | |
e5be10ef | 880 | BT_LOGE_STR("Failed to allocate one variant field class."); |
5cd6d0e5 PP |
881 | goto error; |
882 | } | |
883 | ||
884 | ret = init_named_field_classes_container((void *) var_fc, | |
864cad70 | 885 | BT_FIELD_CLASS_TYPE_VARIANT, destroy_variant_field_class); |
5cd6d0e5 PP |
886 | if (ret) { |
887 | goto error; | |
888 | } | |
889 | ||
e5be10ef | 890 | BT_LIB_LOGD("Created variant field class object: %!+F", var_fc); |
5cd6d0e5 PP |
891 | goto end; |
892 | ||
893 | error: | |
65300d60 | 894 | BT_OBJECT_PUT_REF_AND_RESET(var_fc); |
5cd6d0e5 PP |
895 | |
896 | end: | |
897 | return (void *) var_fc; | |
898 | } | |
899 | ||
40f4ba76 PP |
900 | int bt_field_class_variant_set_selector_field_class( |
901 | struct bt_field_class *fc, | |
902 | struct bt_field_class *selector_fc) | |
5cd6d0e5 PP |
903 | { |
904 | struct bt_field_class_variant *var_fc = (void *) fc; | |
905 | ||
e5be10ef PP |
906 | BT_ASSERT_PRE_NON_NULL(fc, "Variant field class"); |
907 | BT_ASSERT_PRE_NON_NULL(selector_fc, "Selector field class"); | |
864cad70 | 908 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
e5be10ef PP |
909 | BT_ASSERT_PRE_FC_IS_ENUM(selector_fc, "Selector field class"); |
910 | BT_ASSERT_PRE_FC_HOT(fc, "Variant field class"); | |
40f4ba76 | 911 | var_fc->selector_fc = selector_fc; |
398454ed | 912 | bt_object_get_no_null_check(selector_fc); |
40f4ba76 | 913 | bt_field_class_freeze(selector_fc); |
5cd6d0e5 PP |
914 | return 0; |
915 | } | |
916 | ||
40f4ba76 PP |
917 | int bt_field_class_variant_append_option( |
918 | struct bt_field_class *fc, | |
919 | const char *name, struct bt_field_class *option_fc) | |
5cd6d0e5 | 920 | { |
e5be10ef | 921 | |
5cd6d0e5 | 922 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); |
864cad70 | 923 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
5cd6d0e5 | 924 | return append_named_field_class_to_container_field_class((void *) fc, |
40f4ba76 | 925 | name, option_fc); |
5cd6d0e5 PP |
926 | } |
927 | ||
40f4ba76 PP |
928 | const struct bt_field_class * |
929 | bt_field_class_variant_borrow_option_field_class_by_name_const( | |
930 | const struct bt_field_class *fc, const char *name) | |
5cd6d0e5 PP |
931 | { |
932 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 933 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
40f4ba76 | 934 | return borrow_field_class_from_container_field_class_by_name_const( |
e5be10ef PP |
935 | (void *) fc, name); |
936 | } | |
937 | ||
40f4ba76 | 938 | uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class *fc) |
5cd6d0e5 | 939 | { |
40f4ba76 | 940 | const struct bt_field_class_variant *var_fc = (const void *) fc; |
5cd6d0e5 PP |
941 | |
942 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 943 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
5cd6d0e5 PP |
944 | return (uint64_t) var_fc->common.named_fcs->len; |
945 | } | |
946 | ||
40f4ba76 PP |
947 | void bt_field_class_variant_borrow_option_by_index_const( |
948 | const struct bt_field_class *fc, uint64_t index, | |
949 | const char **name, const struct bt_field_class **out_fc) | |
5cd6d0e5 PP |
950 | { |
951 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 952 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, "Field class"); |
40f4ba76 PP |
953 | borrow_named_field_class_from_container_field_class_at_index_const( |
954 | (void *) fc, index, name, out_fc); | |
5cd6d0e5 PP |
955 | } |
956 | ||
40f4ba76 PP |
957 | const struct bt_field_path * |
958 | bt_field_class_variant_borrow_selector_field_path_const( | |
959 | const struct bt_field_class *fc) | |
e5be10ef | 960 | { |
40f4ba76 | 961 | const struct bt_field_class_variant *var_fc = (const void *) fc; |
5cd6d0e5 PP |
962 | |
963 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 964 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_VARIANT, |
5cd6d0e5 PP |
965 | "Field class"); |
966 | return var_fc->selector_field_path; | |
967 | } | |
968 | ||
969 | static | |
970 | void init_array_field_class(struct bt_field_class_array *fc, | |
864cad70 | 971 | enum bt_field_class_type type, bt_object_release_func release_func, |
5cd6d0e5 PP |
972 | struct bt_field_class *element_fc) |
973 | { | |
974 | BT_ASSERT(element_fc); | |
864cad70 | 975 | init_field_class((void *) fc, type, release_func); |
398454ed PP |
976 | fc->element_fc = element_fc; |
977 | bt_object_get_no_null_check(element_fc); | |
5cd6d0e5 PP |
978 | bt_field_class_freeze(element_fc); |
979 | } | |
980 | ||
981 | static | |
982 | void finalize_array_field_class(struct bt_field_class_array *array_fc) | |
983 | { | |
984 | BT_ASSERT(array_fc); | |
e5be10ef | 985 | BT_LOGD_STR("Putting element field class."); |
238b7404 | 986 | BT_OBJECT_PUT_REF_AND_RESET(array_fc->element_fc); |
5cd6d0e5 PP |
987 | } |
988 | ||
989 | static | |
990 | void destroy_static_array_field_class(struct bt_object *obj) | |
991 | { | |
992 | BT_ASSERT(obj); | |
e5be10ef | 993 | BT_LIB_LOGD("Destroying static array field class object: %!+F", obj); |
5cd6d0e5 PP |
994 | finalize_array_field_class((void *) obj); |
995 | g_free(obj); | |
996 | } | |
997 | ||
40f4ba76 PP |
998 | struct bt_field_class * |
999 | bt_field_class_static_array_create(struct bt_field_class *element_fc, | |
e5be10ef | 1000 | uint64_t length) |
5cd6d0e5 PP |
1001 | { |
1002 | struct bt_field_class_static_array *array_fc = NULL; | |
1003 | ||
e5be10ef PP |
1004 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
1005 | BT_LOGD_STR("Creating default static array field class object."); | |
5cd6d0e5 PP |
1006 | array_fc = g_new0(struct bt_field_class_static_array, 1); |
1007 | if (!array_fc) { | |
e5be10ef | 1008 | BT_LOGE_STR("Failed to allocate one static array field class."); |
5cd6d0e5 PP |
1009 | goto error; |
1010 | } | |
1011 | ||
864cad70 | 1012 | init_array_field_class((void *) array_fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, |
5cd6d0e5 PP |
1013 | destroy_static_array_field_class, element_fc); |
1014 | array_fc->length = length; | |
e5be10ef | 1015 | BT_LIB_LOGD("Created static array field class object: %!+F", array_fc); |
5cd6d0e5 PP |
1016 | goto end; |
1017 | ||
1018 | error: | |
65300d60 | 1019 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
5cd6d0e5 PP |
1020 | |
1021 | end: | |
1022 | return (void *) array_fc; | |
1023 | } | |
1024 | ||
40f4ba76 PP |
1025 | const struct bt_field_class * |
1026 | bt_field_class_array_borrow_element_field_class_const( | |
1027 | const struct bt_field_class *fc) | |
5cd6d0e5 | 1028 | { |
40f4ba76 | 1029 | const struct bt_field_class_array *array_fc = (const void *) fc; |
5cd6d0e5 PP |
1030 | |
1031 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
1032 | BT_ASSERT_PRE_FC_IS_ARRAY(fc, "Field class"); | |
1033 | return array_fc->element_fc; | |
1034 | } | |
1035 | ||
40f4ba76 | 1036 | uint64_t bt_field_class_static_array_get_length(const struct bt_field_class *fc) |
e5be10ef | 1037 | { |
40f4ba76 | 1038 | const struct bt_field_class_static_array *array_fc = (const void *) fc; |
5cd6d0e5 PP |
1039 | |
1040 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 1041 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_STATIC_ARRAY, |
5cd6d0e5 PP |
1042 | "Field class"); |
1043 | return (uint64_t) array_fc->length; | |
1044 | } | |
1045 | ||
1046 | static | |
1047 | void destroy_dynamic_array_field_class(struct bt_object *obj) | |
1048 | { | |
1049 | struct bt_field_class_dynamic_array *fc = (void *) obj; | |
1050 | ||
1051 | BT_ASSERT(fc); | |
e5be10ef | 1052 | BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc); |
5cd6d0e5 PP |
1053 | finalize_array_field_class((void *) fc); |
1054 | BT_LOGD_STR("Putting length field path."); | |
238b7404 PP |
1055 | BT_OBJECT_PUT_REF_AND_RESET(fc->length_field_path); |
1056 | BT_LOGD_STR("Putting length field class."); | |
1057 | BT_OBJECT_PUT_REF_AND_RESET(fc->length_fc); | |
5cd6d0e5 PP |
1058 | g_free(fc); |
1059 | } | |
1060 | ||
40f4ba76 PP |
1061 | struct bt_field_class *bt_field_class_dynamic_array_create( |
1062 | struct bt_field_class *element_fc) | |
5cd6d0e5 PP |
1063 | { |
1064 | struct bt_field_class_dynamic_array *array_fc = NULL; | |
1065 | ||
e5be10ef PP |
1066 | BT_ASSERT_PRE_NON_NULL(element_fc, "Element field class"); |
1067 | BT_LOGD_STR("Creating default dynamic array field class object."); | |
5cd6d0e5 PP |
1068 | array_fc = g_new0(struct bt_field_class_dynamic_array, 1); |
1069 | if (!array_fc) { | |
e5be10ef | 1070 | BT_LOGE_STR("Failed to allocate one dynamic array field class."); |
5cd6d0e5 PP |
1071 | goto error; |
1072 | } | |
1073 | ||
40f4ba76 PP |
1074 | init_array_field_class((void *) array_fc, |
1075 | BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, | |
5cd6d0e5 | 1076 | destroy_dynamic_array_field_class, element_fc); |
e5be10ef | 1077 | BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc); |
5cd6d0e5 PP |
1078 | goto end; |
1079 | ||
1080 | error: | |
65300d60 | 1081 | BT_OBJECT_PUT_REF_AND_RESET(array_fc); |
5cd6d0e5 PP |
1082 | |
1083 | end: | |
1084 | return (void *) array_fc; | |
1085 | } | |
1086 | ||
40f4ba76 PP |
1087 | int bt_field_class_dynamic_array_set_length_field_class( |
1088 | struct bt_field_class *fc, | |
1089 | struct bt_field_class *length_fc) | |
5cd6d0e5 PP |
1090 | { |
1091 | struct bt_field_class_dynamic_array *array_fc = (void *) fc; | |
1092 | ||
e5be10ef PP |
1093 | BT_ASSERT_PRE_NON_NULL(fc, "Dynamic array field class"); |
1094 | BT_ASSERT_PRE_NON_NULL(length_fc, "Length field class"); | |
864cad70 | 1095 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, |
5cd6d0e5 | 1096 | "Field class"); |
e5be10ef PP |
1097 | BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc, "Length field class"); |
1098 | BT_ASSERT_PRE_FC_HOT(fc, "Dynamic array field class"); | |
40f4ba76 | 1099 | array_fc->length_fc = length_fc; |
398454ed | 1100 | bt_object_get_no_null_check(length_fc); |
5cd6d0e5 PP |
1101 | bt_field_class_freeze(length_fc); |
1102 | return 0; | |
1103 | } | |
1104 | ||
40f4ba76 PP |
1105 | const struct bt_field_path * |
1106 | bt_field_class_dynamic_array_borrow_length_field_path_const( | |
1107 | const struct bt_field_class *fc) | |
5cd6d0e5 | 1108 | { |
40f4ba76 | 1109 | const struct bt_field_class_dynamic_array *seq_fc = (const void *) fc; |
5cd6d0e5 PP |
1110 | |
1111 | BT_ASSERT_PRE_NON_NULL(fc, "Field class"); | |
864cad70 | 1112 | BT_ASSERT_PRE_FC_HAS_ID(fc, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY, |
5cd6d0e5 PP |
1113 | "Field class"); |
1114 | return seq_fc->length_field_path; | |
1115 | } | |
1116 | ||
1117 | static | |
1118 | void destroy_string_field_class(struct bt_object *obj) | |
1119 | { | |
1120 | BT_ASSERT(obj); | |
e5be10ef | 1121 | BT_LIB_LOGD("Destroying string field class object: %!+F", obj); |
5cd6d0e5 PP |
1122 | g_free(obj); |
1123 | } | |
1124 | ||
40f4ba76 | 1125 | struct bt_field_class *bt_field_class_string_create(void) |
5cd6d0e5 PP |
1126 | { |
1127 | struct bt_field_class_string *string_fc = NULL; | |
1128 | ||
e5be10ef | 1129 | BT_LOGD_STR("Creating default string field class object."); |
5cd6d0e5 PP |
1130 | string_fc = g_new0(struct bt_field_class_string, 1); |
1131 | if (!string_fc) { | |
e5be10ef | 1132 | BT_LOGE_STR("Failed to allocate one string field class."); |
5cd6d0e5 PP |
1133 | goto error; |
1134 | } | |
1135 | ||
864cad70 | 1136 | init_field_class((void *) string_fc, BT_FIELD_CLASS_TYPE_STRING, |
5cd6d0e5 | 1137 | destroy_string_field_class); |
e5be10ef | 1138 | BT_LIB_LOGD("Created string field class object: %!+F", string_fc); |
5cd6d0e5 PP |
1139 | goto end; |
1140 | ||
1141 | error: | |
65300d60 | 1142 | BT_OBJECT_PUT_REF_AND_RESET(string_fc); |
5cd6d0e5 PP |
1143 | |
1144 | end: | |
1145 | return (void *) string_fc; | |
1146 | } | |
1147 | ||
1148 | BT_HIDDEN | |
40f4ba76 | 1149 | void _bt_field_class_freeze(const struct bt_field_class *fc) |
5cd6d0e5 PP |
1150 | { |
1151 | /* | |
1152 | * Element/member/option field classes are frozen when added to | |
1153 | * their owner. | |
1154 | */ | |
1155 | BT_ASSERT(fc); | |
40f4ba76 | 1156 | ((struct bt_field_class *) fc)->frozen = true; |
5cd6d0e5 PP |
1157 | } |
1158 | ||
1159 | BT_HIDDEN | |
862ca4ed | 1160 | void _bt_field_class_make_part_of_trace_class(const struct bt_field_class *c_fc) |
5cd6d0e5 | 1161 | { |
40f4ba76 PP |
1162 | struct bt_field_class *fc = (void *) c_fc; |
1163 | ||
5cd6d0e5 | 1164 | BT_ASSERT(fc); |
862ca4ed | 1165 | BT_ASSERT_PRE(!fc->part_of_trace_class, |
5cd6d0e5 | 1166 | "Field class is already part of a trace: %!+F", fc); |
862ca4ed | 1167 | fc->part_of_trace_class = true; |
5cd6d0e5 | 1168 | |
864cad70 PP |
1169 | switch (fc->type) { |
1170 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
1171 | case BT_FIELD_CLASS_TYPE_VARIANT: | |
5cd6d0e5 PP |
1172 | { |
1173 | struct bt_field_class_named_field_class_container *container_fc = | |
1174 | (void *) fc; | |
1175 | uint64_t i; | |
1176 | ||
1177 | for (i = 0; i < container_fc->named_fcs->len; i++) { | |
1178 | struct bt_named_field_class *named_fc = | |
1179 | BT_FIELD_CLASS_NAMED_FC_AT_INDEX( | |
1180 | container_fc, i); | |
1181 | ||
862ca4ed | 1182 | bt_field_class_make_part_of_trace_class(named_fc->fc); |
5cd6d0e5 PP |
1183 | } |
1184 | ||
1185 | break; | |
1186 | } | |
864cad70 PP |
1187 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
1188 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
5cd6d0e5 PP |
1189 | { |
1190 | struct bt_field_class_array *array_fc = (void *) fc; | |
1191 | ||
862ca4ed | 1192 | bt_field_class_make_part_of_trace_class(array_fc->element_fc); |
5cd6d0e5 PP |
1193 | break; |
1194 | } | |
1195 | default: | |
1196 | break; | |
1197 | } | |
1198 | } |