2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "FIELD-CLASSES"
26 #include <babeltrace/lib-logging-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/trace-ir/private-field-classes.h>
30 #include <babeltrace/trace-ir/field-classes-internal.h>
31 #include <babeltrace/trace-ir/field-path-internal.h>
32 #include <babeltrace/trace-ir/fields-internal.h>
33 #include <babeltrace/trace-ir/private-fields.h>
34 #include <babeltrace/trace-ir/fields.h>
35 #include <babeltrace/trace-ir/utils-internal.h>
36 #include <babeltrace/object.h>
37 #include <babeltrace/trace-ir/clock-class.h>
38 #include <babeltrace/trace-ir/clock-class-internal.h>
39 #include <babeltrace/object-internal.h>
40 #include <babeltrace/object.h>
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>
49 enum bt_field_class_type
bt_field_class_get_type(struct bt_field_class
*fc
)
51 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
56 void init_field_class(struct bt_field_class
*fc
, enum bt_field_class_type type
,
57 bt_object_release_func release_func
)
60 BT_ASSERT(bt_field_class_has_known_type(fc
));
61 BT_ASSERT(release_func
);
62 bt_object_init_shared(&fc
->base
, release_func
);
67 void init_integer_field_class(struct bt_field_class_integer
*fc
,
68 enum bt_field_class_type type
,
69 bt_object_release_func release_func
)
71 init_field_class((void *) fc
, type
, release_func
);
73 fc
->base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
77 void destroy_integer_field_class(struct bt_object
*obj
)
80 BT_LIB_LOGD("Destroying integer field class object: %!+F", obj
);
85 struct bt_field_class
*create_integer_field_class(enum bt_field_class_type type
)
87 struct bt_field_class_integer
*int_fc
= NULL
;
89 BT_LOGD("Creating default integer field class object: type=%s",
90 bt_common_field_class_type_string(type
));
91 int_fc
= g_new0(struct bt_field_class_integer
, 1);
93 BT_LOGE_STR("Failed to allocate one integer field class.");
97 init_integer_field_class(int_fc
, type
, destroy_integer_field_class
);
98 BT_LIB_LOGD("Created integer field class object: %!+F", int_fc
);
102 BT_OBJECT_PUT_REF_AND_RESET(int_fc
);
105 return (void *) int_fc
;
108 struct bt_private_field_class
*
109 bt_private_field_class_unsigned_integer_create(void)
111 return (void *) create_integer_field_class(
112 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
);
115 struct bt_private_field_class
*bt_private_field_class_signed_integer_create(void)
117 return (void *) create_integer_field_class(
118 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
);
121 uint64_t bt_field_class_integer_get_field_value_range(
122 struct bt_field_class
*fc
)
124 struct bt_field_class_integer
*int_fc
= (void *) fc
;
126 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
127 BT_ASSERT_PRE_FC_IS_INT(fc
, "Field class");
128 return int_fc
->range
;
133 bool size_is_valid_for_enumeration_field_class(struct bt_field_class
*fc
,
140 void bt_private_field_class_integer_set_field_value_range(
141 struct bt_private_field_class
*priv_fc
, uint64_t size
)
143 struct bt_field_class
*fc
= (void *) priv_fc
;
144 struct bt_field_class_integer
*int_fc
= (void *) fc
;
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,
150 "Unsupported size for integer field class's field value range "
151 "(maximum is 64): size=%" PRIu64
, size
);
153 int_fc
->common
.type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
154 int_fc
->common
.type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
155 size_is_valid_for_enumeration_field_class(fc
, size
),
156 "Invalid field value range for enumeration field class: "
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
;
160 BT_LIB_LOGV("Set integer field class's field value range: %!+F", fc
);
163 enum bt_field_class_integer_preferred_display_base
164 bt_field_class_integer_get_preferred_display_base(struct bt_field_class
*fc
)
166 struct bt_field_class_integer
*int_fc
= (void *) fc
;
168 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
169 BT_ASSERT_PRE_FC_IS_INT(fc
, "Field class");
173 void bt_private_field_class_integer_set_preferred_display_base(
174 struct bt_private_field_class
*priv_fc
,
175 enum bt_field_class_integer_preferred_display_base base
)
177 struct bt_field_class
*fc
= (void *) priv_fc
;
178 struct bt_field_class_integer
*int_fc
= (void *) fc
;
180 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
181 BT_ASSERT_PRE_FC_IS_INT(fc
, "Field class");
182 BT_ASSERT_PRE_FC_HOT(fc
, "Field class");
184 BT_LIB_LOGV("Set integer field class's preferred display base: %!+F", fc
);
188 void finalize_enumeration_field_class_mapping(
189 struct bt_field_class_enumeration_mapping
*mapping
)
193 if (mapping
->label
) {
194 g_string_free(mapping
->label
, TRUE
);
197 if (mapping
->ranges
) {
198 g_array_free(mapping
->ranges
, TRUE
);
203 void destroy_enumeration_field_class(struct bt_object
*obj
)
205 struct bt_field_class_enumeration
*fc
= (void *) obj
;
208 BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc
);
213 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
214 finalize_enumeration_field_class_mapping(
215 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, i
));
218 g_array_free(fc
->mappings
, TRUE
);
223 g_ptr_array_free(fc
->label_buf
, TRUE
);
224 fc
->label_buf
= NULL
;
231 struct bt_field_class
*create_enumeration_field_class(enum bt_field_class_type type
)
233 struct bt_field_class_enumeration
*enum_fc
= NULL
;
235 BT_LOGD("Creating default enumeration field class object: type=%s",
236 bt_common_field_class_type_string(type
));
237 enum_fc
= g_new0(struct bt_field_class_enumeration
, 1);
239 BT_LOGE_STR("Failed to allocate one enumeration field class.");
243 init_integer_field_class((void *) enum_fc
, type
,
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.");
252 enum_fc
->label_buf
= g_ptr_array_new();
253 if (!enum_fc
->label_buf
) {
254 BT_LOGE_STR("Failed to allocate a GArray.");
258 BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc
);
262 BT_OBJECT_PUT_REF_AND_RESET(enum_fc
);
265 return (void *) enum_fc
;
268 struct bt_private_field_class
*
269 bt_private_field_class_unsigned_enumeration_create(void)
271 return (void *) create_enumeration_field_class(
272 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
275 struct bt_private_field_class
*
276 bt_private_field_class_signed_enumeration_create(void)
278 return (void *) create_enumeration_field_class(
279 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
282 uint64_t bt_field_class_enumeration_get_mapping_count(struct bt_field_class
*fc
)
284 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
286 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
287 BT_ASSERT_PRE_FC_IS_ENUM(fc
, "Field class");
288 return (uint64_t) enum_fc
->mappings
->len
;
291 void bt_field_class_unsigned_enumeration_borrow_mapping_by_index(
292 struct bt_field_class
*fc
, uint64_t index
,
294 struct bt_field_class_unsigned_enumeration_mapping_ranges
**ranges
)
296 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
297 struct bt_field_class_enumeration_mapping
*mapping
;
299 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
300 BT_ASSERT_PRE_NON_NULL(name
, "Name (output)");
301 BT_ASSERT_PRE_NON_NULL(ranges
, "Ranges (output)");
302 BT_ASSERT_PRE_VALID_INDEX(index
, enum_fc
->mappings
->len
);
303 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
305 mapping
= BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, index
);
306 *name
= mapping
->label
->str
;
307 *ranges
= (void *) mapping
;
310 void bt_field_class_signed_enumeration_borrow_mapping_by_index(
311 struct bt_field_class
*fc
, uint64_t index
,
313 struct bt_field_class_signed_enumeration_mapping_ranges
**ranges
)
315 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
316 struct bt_field_class_enumeration_mapping
*mapping
;
318 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
319 BT_ASSERT_PRE_NON_NULL(name
, "Name (output)");
320 BT_ASSERT_PRE_NON_NULL(ranges
, "Ranges (output)");
321 BT_ASSERT_PRE_VALID_INDEX(index
, enum_fc
->mappings
->len
);
322 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
324 mapping
= BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, index
);
325 *name
= mapping
->label
->str
;
326 *ranges
= (void *) mapping
;
330 uint64_t get_enumeration_field_class_mapping_range_count(
331 struct bt_field_class_enumeration_mapping
*mapping
)
333 BT_ASSERT_PRE_NON_NULL(mapping
, "Ranges");
334 return (uint64_t) mapping
->ranges
->len
;
337 uint64_t bt_field_class_unsigned_enumeration_mapping_ranges_get_range_count(
338 struct bt_field_class_unsigned_enumeration_mapping_ranges
*ranges
)
340 return get_enumeration_field_class_mapping_range_count((void *) ranges
);
343 uint64_t bt_field_class_signed_enumeration_mapping_ranges_get_range_count(
344 struct bt_field_class_signed_enumeration_mapping_ranges
*ranges
)
346 return get_enumeration_field_class_mapping_range_count((void *) ranges
);
350 void get_enumeration_field_class_mapping_range_at_index(
351 struct bt_field_class_enumeration_mapping
*mapping
,
352 uint64_t index
, uint64_t *lower
, uint64_t *upper
)
354 struct bt_field_class_enumeration_mapping_range
*range
;
356 BT_ASSERT_PRE_NON_NULL(mapping
, "Ranges");
357 BT_ASSERT_PRE_NON_NULL(lower
, "Range's lower (output)");
358 BT_ASSERT_PRE_NON_NULL(upper
, "Range's upper (output)");
359 BT_ASSERT_PRE_VALID_INDEX(index
, mapping
->ranges
->len
);
360 range
= BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping
, index
);
361 *lower
= range
->lower
.u
;
362 *upper
= range
->upper
.u
;
365 void bt_field_class_unsigned_enumeration_mapping_ranges_get_range_by_index(
366 struct bt_field_class_unsigned_enumeration_mapping_ranges
*ranges
,
367 uint64_t index
, uint64_t *lower
, uint64_t *upper
)
369 get_enumeration_field_class_mapping_range_at_index((void *) ranges
,
370 index
, lower
, upper
);
373 void bt_field_class_signed_enumeration_mapping_ranges_get_range_by_index(
374 struct bt_field_class_unsigned_enumeration_mapping_ranges
*ranges
,
375 uint64_t index
, int64_t *lower
, int64_t *upper
)
377 get_enumeration_field_class_mapping_range_at_index((void *) ranges
,
378 index
, (uint64_t *) lower
, (uint64_t *) upper
);
383 int bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
384 struct bt_field_class
*fc
, uint64_t value
,
385 bt_field_class_enumeration_mapping_label_array
*label_array
,
388 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
391 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
392 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
393 BT_ASSERT_PRE_NON_NULL(count
, "Count (output)");
394 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
396 g_ptr_array_set_size(enum_fc
->label_buf
, 0);
398 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
400 struct bt_field_class_enumeration_mapping
*mapping
=
401 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
403 for (j
= 0; j
< mapping
->ranges
->len
; j
++) {
404 struct bt_field_class_enumeration_mapping_range
*range
=
405 BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(
408 if (value
>= range
->lower
.u
&&
409 value
<= range
->upper
.u
) {
410 g_ptr_array_add(enum_fc
->label_buf
,
411 mapping
->label
->str
);
417 *label_array
= (void *) enum_fc
->label_buf
->pdata
;
418 *count
= (uint64_t) enum_fc
->label_buf
->len
;
422 int bt_field_class_signed_enumeration_get_mapping_labels_by_value(
423 struct bt_field_class
*fc
, int64_t value
,
424 bt_field_class_enumeration_mapping_label_array
*label_array
,
427 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
430 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
431 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
432 BT_ASSERT_PRE_NON_NULL(count
, "Count (output)");
433 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
435 g_ptr_array_set_size(enum_fc
->label_buf
, 0);
437 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
439 struct bt_field_class_enumeration_mapping
*mapping
=
440 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
442 for (j
= 0; j
< mapping
->ranges
->len
; j
++) {
443 struct bt_field_class_enumeration_mapping_range
*range
=
444 BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(
447 if (value
>= range
->lower
.i
&&
448 value
<= range
->upper
.i
) {
449 g_ptr_array_add(enum_fc
->label_buf
,
450 mapping
->label
->str
);
456 *label_array
= (void *) enum_fc
->label_buf
->pdata
;
457 *count
= (uint64_t) enum_fc
->label_buf
->len
;
462 int add_mapping_to_enumeration_field_class(struct bt_field_class
*fc
,
463 const char *label
, uint64_t lower
, uint64_t upper
)
467 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
468 struct bt_field_class_enumeration_mapping
*mapping
= NULL
;
469 struct bt_field_class_enumeration_mapping_range
*range
;
472 BT_ASSERT_PRE_NON_NULL(label
, "Label");
474 /* Find existing mapping identified by this label */
475 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
476 struct bt_field_class_enumeration_mapping
*mapping_candidate
=
477 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
479 if (strcmp(mapping_candidate
->label
->str
, label
) == 0) {
480 mapping
= mapping_candidate
;
486 /* Create new mapping for this label */
487 g_array_set_size(enum_fc
->mappings
, enum_fc
->mappings
->len
+ 1);
488 mapping
= BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
,
489 enum_fc
->mappings
->len
- 1);
490 mapping
->ranges
= g_array_new(FALSE
, TRUE
,
491 sizeof(struct bt_field_class_enumeration_mapping_range
));
492 if (!mapping
->ranges
) {
493 finalize_enumeration_field_class_mapping(mapping
);
494 g_array_set_size(enum_fc
->mappings
,
495 enum_fc
->mappings
->len
- 1);
500 mapping
->label
= g_string_new(label
);
501 if (!mapping
->label
) {
502 finalize_enumeration_field_class_mapping(mapping
);
503 g_array_set_size(enum_fc
->mappings
,
504 enum_fc
->mappings
->len
- 1);
512 g_array_set_size(mapping
->ranges
, mapping
->ranges
->len
+ 1);
513 range
= BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping
,
514 mapping
->ranges
->len
- 1);
515 range
->lower
.u
= lower
;
516 range
->upper
.u
= upper
;
517 BT_LIB_LOGV("Added mapping to enumeration field class: "
518 "%![fc-]+F, label=\"%s\", lower-unsigned=%" PRIu64
", "
519 "upper-unsigned=%" PRIu64
, fc
, label
, lower
, upper
);
525 int bt_private_field_class_unsigned_enumeration_map_range(
526 struct bt_private_field_class
*priv_fc
, const char *label
,
527 uint64_t range_lower
, uint64_t range_upper
)
529 struct bt_field_class
*fc
= (void *) priv_fc
;
530 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
532 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
533 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
535 BT_ASSERT_PRE(range_lower
<= range_upper
,
536 "Range's upper bound is less than lower bound: "
537 "upper=%" PRIu64
", lower=%" PRIu64
,
538 range_lower
, range_upper
);
539 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc
->common
.range
,
541 "Range's lower bound is outside the enumeration field class's value range: "
542 "%![fc-]+F, lower=%" PRIu64
, fc
, range_lower
);
543 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc
->common
.range
,
545 "Range's upper bound is outside the enumeration field class's value range: "
546 "%![fc-]+F, upper=%" PRIu64
, fc
, range_upper
);
547 return add_mapping_to_enumeration_field_class(fc
, label
, range_lower
,
551 int bt_private_field_class_signed_enumeration_map_range(
552 struct bt_private_field_class
*priv_fc
, const char *label
,
553 int64_t range_lower
, int64_t range_upper
)
555 struct bt_field_class
*fc
= (void *) priv_fc
;
556 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
558 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
559 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
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
,
567 "Range's lower bound is outside the enumeration field class's value range: "
568 "%![fc-]+F, lower=%" PRId64
, fc
, range_lower
);
569 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc
->common
.range
,
571 "Range's upper bound is outside the enumeration field class's value range: "
572 "%![fc-]+F, upper=%" PRId64
, fc
, range_upper
);
573 return add_mapping_to_enumeration_field_class(fc
, label
, range_lower
,
578 void destroy_real_field_class(struct bt_object
*obj
)
581 BT_LIB_LOGD("Destroying real field class object: %!+F", obj
);
585 struct bt_private_field_class
*bt_private_field_class_real_create(void)
587 struct bt_field_class_real
*real_fc
= NULL
;
589 BT_LOGD_STR("Creating default real field class object.");
590 real_fc
= g_new0(struct bt_field_class_real
, 1);
592 BT_LOGE_STR("Failed to allocate one real field class.");
596 init_field_class((void *) real_fc
, BT_FIELD_CLASS_TYPE_REAL
,
597 destroy_real_field_class
);
598 BT_LIB_LOGD("Created real field class object: %!+F", real_fc
);
602 BT_OBJECT_PUT_REF_AND_RESET(real_fc
);
605 return (void *) real_fc
;
608 bt_bool
bt_field_class_real_is_single_precision(struct bt_field_class
*fc
)
610 struct bt_field_class_real
*real_fc
= (void *) fc
;
612 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
613 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_REAL
, "Field class");
614 return real_fc
->is_single_precision
;
617 void bt_private_field_class_real_set_is_single_precision(
618 struct bt_private_field_class
*priv_fc
,
619 bt_bool is_single_precision
)
621 struct bt_field_class
*fc
= (void *) priv_fc
;
622 struct bt_field_class_real
*real_fc
= (void *) fc
;
624 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
625 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_REAL
, "Field class");
626 BT_ASSERT_PRE_FC_HOT(fc
, "Field class");
627 real_fc
->is_single_precision
= (bool) is_single_precision
;
628 BT_LIB_LOGV("Set real field class's \"is single precision\" property: "
633 int init_named_field_classes_container(
634 struct bt_field_class_named_field_class_container
*fc
,
635 enum bt_field_class_type type
, bt_object_release_func release_func
)
639 init_field_class((void *) fc
, type
, release_func
);
640 fc
->named_fcs
= g_array_new(FALSE
, TRUE
,
641 sizeof(struct bt_named_field_class
));
642 if (!fc
->named_fcs
) {
643 BT_LOGE_STR("Failed to allocate a GArray.");
648 fc
->name_to_index
= g_hash_table_new(g_str_hash
, g_str_equal
);
649 if (!fc
->name_to_index
) {
650 BT_LOGE_STR("Failed to allocate a GHashTable.");
660 void finalize_named_field_class(struct bt_named_field_class
*named_fc
)
663 BT_LIB_LOGD("Finalizing named field class: "
664 "addr=%p, name=\"%s\", %![fc-]+F",
665 named_fc
, named_fc
->name
? named_fc
->name
->str
: NULL
,
668 if (named_fc
->name
) {
669 g_string_free(named_fc
->name
, TRUE
);
672 BT_LOGD_STR("Putting named field class's field class.");
673 BT_OBJECT_PUT_REF_AND_RESET(named_fc
->fc
);
677 void finalize_named_field_classes_container(
678 struct bt_field_class_named_field_class_container
*fc
)
685 for (i
= 0; i
< fc
->named_fcs
->len
; i
++) {
686 finalize_named_field_class(
687 &g_array_index(fc
->named_fcs
,
688 struct bt_named_field_class
, i
));
691 g_array_free(fc
->named_fcs
, TRUE
);
694 if (fc
->name_to_index
) {
695 g_hash_table_destroy(fc
->name_to_index
);
700 void destroy_structure_field_class(struct bt_object
*obj
)
703 BT_LIB_LOGD("Destroying string field class object: %!+F", obj
);
704 finalize_named_field_classes_container((void *) obj
);
708 struct bt_private_field_class
*bt_private_field_class_structure_create(void)
711 struct bt_field_class_structure
*struct_fc
= NULL
;
713 BT_LOGD_STR("Creating default structure field class object.");
714 struct_fc
= g_new0(struct bt_field_class_structure
, 1);
716 BT_LOGE_STR("Failed to allocate one structure field class.");
720 ret
= init_named_field_classes_container((void *) struct_fc
,
721 BT_FIELD_CLASS_TYPE_STRUCTURE
, destroy_structure_field_class
);
726 BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc
);
730 BT_OBJECT_PUT_REF_AND_RESET(struct_fc
);
733 return (void *) struct_fc
;
737 int append_named_field_class_to_container_field_class(
738 struct bt_field_class_named_field_class_container
*container_fc
,
739 const char *name
, struct bt_field_class
*fc
)
742 struct bt_named_field_class
*named_fc
;
745 BT_ASSERT(container_fc
);
746 BT_ASSERT_PRE_FC_HOT(container_fc
, "Field class");
747 BT_ASSERT_PRE_NON_NULL(name
, "Name");
748 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
749 BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc
->name_to_index
,
751 "Duplicate member/option name in structure/variant field class: "
752 "%![container-fc-]+F, name=\"%s\"", container_fc
, name
);
753 name_str
= g_string_new(name
);
755 BT_LOGE_STR("Failed to allocate a GString.");
760 g_array_set_size(container_fc
->named_fcs
,
761 container_fc
->named_fcs
->len
+ 1);
762 named_fc
= &g_array_index(container_fc
->named_fcs
,
763 struct bt_named_field_class
, container_fc
->named_fcs
->len
- 1);
764 named_fc
->name
= name_str
;
765 named_fc
->fc
= bt_object_get_ref(fc
);
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
);
774 int bt_private_field_class_structure_append_member(
775 struct bt_private_field_class
*priv_fc
,
776 const char *name
, struct bt_private_field_class
*member_fc
)
778 struct bt_field_class
*fc
= (void *) priv_fc
;
780 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
781 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field class");
782 return append_named_field_class_to_container_field_class((void *) fc
,
783 name
, (void *) member_fc
);
786 uint64_t bt_field_class_structure_get_member_count(struct bt_field_class
*fc
)
788 struct bt_field_class_structure
*struct_fc
= (void *) fc
;
790 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
791 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field class");
792 return (uint64_t) struct_fc
->common
.named_fcs
->len
;
796 void borrow_named_field_class_from_container_field_class_at_index(
797 struct bt_field_class_named_field_class_container
*fc
,
798 uint64_t index
, const char **name
,
799 struct bt_field_class
**out_fc
)
801 struct bt_named_field_class
*named_fc
;
804 BT_ASSERT_PRE_NON_NULL(name
, "Name");
805 BT_ASSERT_PRE_NON_NULL(out_fc
, "Field class (output)");
806 BT_ASSERT_PRE_VALID_INDEX(index
, fc
->named_fcs
->len
);
807 named_fc
= BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc
, index
);
808 *name
= named_fc
->name
->str
;
809 *out_fc
= named_fc
->fc
;
812 void bt_field_class_structure_borrow_member_by_index(
813 struct bt_field_class
*fc
, uint64_t index
,
814 const char **name
, struct bt_field_class
**out_fc
)
816 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
817 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field class");
818 borrow_named_field_class_from_container_field_class_at_index((void *) fc
,
819 index
, name
, out_fc
);
822 void bt_private_field_class_structure_borrow_member_by_index(
823 struct bt_private_field_class
*fc
, uint64_t index
,
824 const char **name
, struct bt_private_field_class
**out_fc
)
826 bt_field_class_structure_borrow_member_by_index((void *) fc
,
827 index
, name
, (void *) out_fc
);
831 struct bt_field_class
*borrow_field_class_from_container_field_class_by_name(
832 struct bt_field_class_named_field_class_container
*fc
,
835 struct bt_field_class
*ret_fc
= NULL
;
836 struct bt_named_field_class
*named_fc
;
841 BT_ASSERT_PRE_NON_NULL(name
, "Name");
842 if (!g_hash_table_lookup_extended(fc
->name_to_index
, name
, &orig_key
,
847 named_fc
= BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc
,
848 GPOINTER_TO_UINT(value
));
849 ret_fc
= named_fc
->fc
;
855 struct bt_field_class
*bt_field_class_structure_borrow_member_field_class_by_name(
856 struct bt_field_class
*fc
, const char *name
)
858 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
859 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field class");
860 return borrow_field_class_from_container_field_class_by_name((void *) fc
,
864 struct bt_private_field_class
*
865 bt_private_field_class_structure_borrow_member_field_class_by_name(
866 struct bt_private_field_class
*fc
, const char *name
)
868 return (void *) bt_field_class_structure_borrow_member_field_class_by_name(
873 void destroy_variant_field_class(struct bt_object
*obj
)
875 struct bt_field_class_variant
*fc
= (void *) obj
;
878 BT_LIB_LOGD("Destroying variant field class object: %!+F", fc
);
879 finalize_named_field_classes_container((void *) fc
);
880 BT_LOGD_STR("Putting selector field path.");
881 BT_OBJECT_PUT_REF_AND_RESET(fc
->selector_field_path
);
882 BT_LOGD_STR("Putting selector field class.");
883 BT_OBJECT_PUT_REF_AND_RESET(fc
->selector_fc
);
887 struct bt_private_field_class
*bt_private_field_class_variant_create(void)
890 struct bt_field_class_variant
*var_fc
= NULL
;
892 BT_LOGD_STR("Creating default variant field class object.");
893 var_fc
= g_new0(struct bt_field_class_variant
, 1);
895 BT_LOGE_STR("Failed to allocate one variant field class.");
899 ret
= init_named_field_classes_container((void *) var_fc
,
900 BT_FIELD_CLASS_TYPE_VARIANT
, destroy_variant_field_class
);
905 BT_LIB_LOGD("Created variant field class object: %!+F", var_fc
);
909 BT_OBJECT_PUT_REF_AND_RESET(var_fc
);
912 return (void *) var_fc
;
915 int bt_private_field_class_variant_set_selector_field_class(
916 struct bt_private_field_class
*priv_fc
,
917 struct bt_private_field_class
*selector_fc
)
919 struct bt_field_class
*fc
= (void *) priv_fc
;
920 struct bt_field_class_variant
*var_fc
= (void *) fc
;
922 BT_ASSERT_PRE_NON_NULL(fc
, "Variant field class");
923 BT_ASSERT_PRE_NON_NULL(selector_fc
, "Selector field class");
924 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
, "Field class");
925 BT_ASSERT_PRE_FC_IS_ENUM(selector_fc
, "Selector field class");
926 BT_ASSERT_PRE_FC_HOT(fc
, "Variant field class");
927 var_fc
->selector_fc
= bt_object_get_ref(selector_fc
);
928 bt_field_class_freeze((void *) selector_fc
);
932 int bt_private_field_class_variant_append_private_option(
933 struct bt_private_field_class
*priv_fc
,
934 const char *name
, struct bt_private_field_class
*option_fc
)
936 struct bt_field_class
*fc
= (void *) priv_fc
;
938 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
939 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
, "Field class");
940 return append_named_field_class_to_container_field_class((void *) fc
,
941 name
, (void *) option_fc
);
944 struct bt_field_class
*bt_field_class_variant_borrow_option_field_class_by_name(
945 struct bt_field_class
*fc
, const char *name
)
947 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
948 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
, "Field class");
949 return borrow_field_class_from_container_field_class_by_name((void *) fc
,
953 struct bt_private_field_class
*
954 bt_private_field_class_variant_borrow_option_field_class_by_name(
955 struct bt_private_field_class
*fc
, const char *name
)
957 return (void *) bt_field_class_variant_borrow_option_field_class_by_name(
961 uint64_t bt_field_class_variant_get_option_count(struct bt_field_class
*fc
)
963 struct bt_field_class_variant
*var_fc
= (void *) fc
;
965 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
966 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
, "Field class");
967 return (uint64_t) var_fc
->common
.named_fcs
->len
;
970 void bt_field_class_variant_borrow_option_by_index(
971 struct bt_field_class
*fc
, uint64_t index
,
972 const char **name
, struct bt_field_class
**out_fc
)
974 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
975 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
, "Field class");
976 borrow_named_field_class_from_container_field_class_at_index((void *) fc
,
977 index
, name
, out_fc
);
980 void bt_private_field_class_variant_borrow_option_by_index(
981 struct bt_private_field_class
*fc
, uint64_t index
,
982 const char **name
, struct bt_private_field_class
**out_fc
)
984 bt_field_class_variant_borrow_option_by_index((void *) fc
,
985 index
, name
, (void *) out_fc
);
988 struct bt_field_path
*bt_field_class_variant_borrow_selector_field_path(
989 struct bt_field_class
*fc
)
991 struct bt_field_class_variant
*var_fc
= (void *) fc
;
993 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
994 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
,
996 return var_fc
->selector_field_path
;
1000 void init_array_field_class(struct bt_field_class_array
*fc
,
1001 enum bt_field_class_type type
, bt_object_release_func release_func
,
1002 struct bt_field_class
*element_fc
)
1004 BT_ASSERT(element_fc
);
1005 init_field_class((void *) fc
, type
, release_func
);
1006 fc
->element_fc
= bt_object_get_ref(element_fc
);
1007 bt_field_class_freeze(element_fc
);
1011 void finalize_array_field_class(struct bt_field_class_array
*array_fc
)
1013 BT_ASSERT(array_fc
);
1014 BT_LOGD_STR("Putting element field class.");
1015 BT_OBJECT_PUT_REF_AND_RESET(array_fc
->element_fc
);
1019 void destroy_static_array_field_class(struct bt_object
*obj
)
1022 BT_LIB_LOGD("Destroying static array field class object: %!+F", obj
);
1023 finalize_array_field_class((void *) obj
);
1027 struct bt_private_field_class
*
1028 bt_private_field_class_static_array_create(
1029 struct bt_private_field_class
*priv_element_fc
,
1032 struct bt_field_class
*element_fc
= (void *) priv_element_fc
;
1033 struct bt_field_class_static_array
*array_fc
= NULL
;
1035 BT_ASSERT_PRE_NON_NULL(element_fc
, "Element field class");
1036 BT_LOGD_STR("Creating default static array field class object.");
1037 array_fc
= g_new0(struct bt_field_class_static_array
, 1);
1039 BT_LOGE_STR("Failed to allocate one static array field class.");
1043 init_array_field_class((void *) array_fc
, BT_FIELD_CLASS_TYPE_STATIC_ARRAY
,
1044 destroy_static_array_field_class
, element_fc
);
1045 array_fc
->length
= length
;
1046 BT_LIB_LOGD("Created static array field class object: %!+F", array_fc
);
1050 BT_OBJECT_PUT_REF_AND_RESET(array_fc
);
1053 return (void *) array_fc
;
1056 struct bt_field_class
*bt_field_class_array_borrow_element_field_class(
1057 struct bt_field_class
*fc
)
1059 struct bt_field_class_array
*array_fc
= (void *) fc
;
1061 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1062 BT_ASSERT_PRE_FC_IS_ARRAY(fc
, "Field class");
1063 return array_fc
->element_fc
;
1066 struct bt_private_field_class
*
1067 bt_private_field_class_array_borrow_element_field_class(
1068 struct bt_private_field_class
*fc
)
1070 return (void *) bt_field_class_array_borrow_element_field_class(
1074 uint64_t bt_field_class_static_array_get_length(struct bt_field_class
*fc
)
1076 struct bt_field_class_static_array
*array_fc
= (void *) fc
;
1078 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1079 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STATIC_ARRAY
,
1081 return (uint64_t) array_fc
->length
;
1085 void destroy_dynamic_array_field_class(struct bt_object
*obj
)
1087 struct bt_field_class_dynamic_array
*fc
= (void *) obj
;
1090 BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc
);
1091 finalize_array_field_class((void *) fc
);
1092 BT_LOGD_STR("Putting length field path.");
1093 BT_OBJECT_PUT_REF_AND_RESET(fc
->length_field_path
);
1094 BT_LOGD_STR("Putting length field class.");
1095 BT_OBJECT_PUT_REF_AND_RESET(fc
->length_fc
);
1099 struct bt_private_field_class
*bt_private_field_class_dynamic_array_create(
1100 struct bt_private_field_class
*priv_element_fc
)
1102 struct bt_field_class
*element_fc
= (void *) priv_element_fc
;
1103 struct bt_field_class_dynamic_array
*array_fc
= NULL
;
1105 BT_ASSERT_PRE_NON_NULL(element_fc
, "Element field class");
1106 BT_LOGD_STR("Creating default dynamic array field class object.");
1107 array_fc
= g_new0(struct bt_field_class_dynamic_array
, 1);
1109 BT_LOGE_STR("Failed to allocate one dynamic array field class.");
1113 init_array_field_class((void *) array_fc
, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
,
1114 destroy_dynamic_array_field_class
, element_fc
);
1115 BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc
);
1119 BT_OBJECT_PUT_REF_AND_RESET(array_fc
);
1122 return (void *) array_fc
;
1125 int bt_private_field_class_dynamic_array_set_length_field_class(
1126 struct bt_private_field_class
*priv_fc
,
1127 struct bt_private_field_class
*priv_length_fc
)
1129 struct bt_field_class
*fc
= (void *) priv_fc
;
1130 struct bt_field_class
*length_fc
= (void *) priv_length_fc
;
1131 struct bt_field_class_dynamic_array
*array_fc
= (void *) fc
;
1133 BT_ASSERT_PRE_NON_NULL(fc
, "Dynamic array field class");
1134 BT_ASSERT_PRE_NON_NULL(length_fc
, "Length field class");
1135 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
,
1137 BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc
, "Length field class");
1138 BT_ASSERT_PRE_FC_HOT(fc
, "Dynamic array field class");
1139 array_fc
->length_fc
= bt_object_get_ref(length_fc
);
1140 bt_field_class_freeze(length_fc
);
1144 struct bt_field_path
*bt_field_class_dynamic_array_borrow_length_field_path(
1145 struct bt_field_class
*fc
)
1147 struct bt_field_class_dynamic_array
*seq_fc
= (void *) fc
;
1149 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1150 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
,
1152 return seq_fc
->length_field_path
;
1156 void destroy_string_field_class(struct bt_object
*obj
)
1159 BT_LIB_LOGD("Destroying string field class object: %!+F", obj
);
1163 struct bt_private_field_class
*bt_private_field_class_string_create(void)
1165 struct bt_field_class_string
*string_fc
= NULL
;
1167 BT_LOGD_STR("Creating default string field class object.");
1168 string_fc
= g_new0(struct bt_field_class_string
, 1);
1170 BT_LOGE_STR("Failed to allocate one string field class.");
1174 init_field_class((void *) string_fc
, BT_FIELD_CLASS_TYPE_STRING
,
1175 destroy_string_field_class
);
1176 BT_LIB_LOGD("Created string field class object: %!+F", string_fc
);
1180 BT_OBJECT_PUT_REF_AND_RESET(string_fc
);
1183 return (void *) string_fc
;
1187 void _bt_field_class_freeze(struct bt_field_class
*fc
)
1190 * Element/member/option field classes are frozen when added to
1198 void _bt_field_class_make_part_of_trace(struct bt_field_class
*fc
)
1201 BT_ASSERT_PRE(!fc
->part_of_trace
,
1202 "Field class is already part of a trace: %!+F", fc
);
1203 fc
->part_of_trace
= true;
1206 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1207 case BT_FIELD_CLASS_TYPE_VARIANT
:
1209 struct bt_field_class_named_field_class_container
*container_fc
=
1213 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
1214 struct bt_named_field_class
*named_fc
=
1215 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
1218 bt_field_class_make_part_of_trace(named_fc
->fc
);
1223 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1224 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1226 struct bt_field_class_array
*array_fc
= (void *) fc
;
1228 bt_field_class_make_part_of_trace(array_fc
->element_fc
);