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
);
222 g_ptr_array_free(fc
->label_buf
, TRUE
);
229 struct bt_field_class
*create_enumeration_field_class(enum bt_field_class_type type
)
231 struct bt_field_class_enumeration
*enum_fc
= NULL
;
233 BT_LOGD("Creating default enumeration field class object: type=%s",
234 bt_common_field_class_type_string(type
));
235 enum_fc
= g_new0(struct bt_field_class_enumeration
, 1);
237 BT_LOGE_STR("Failed to allocate one enumeration field class.");
241 init_integer_field_class((void *) enum_fc
, type
,
242 destroy_enumeration_field_class
);
243 enum_fc
->mappings
= g_array_new(FALSE
, TRUE
,
244 sizeof(struct bt_field_class_enumeration_mapping
));
245 if (!enum_fc
->mappings
) {
246 BT_LOGE_STR("Failed to allocate a GArray.");
250 enum_fc
->label_buf
= g_ptr_array_new();
251 if (!enum_fc
->label_buf
) {
252 BT_LOGE_STR("Failed to allocate a GArray.");
256 BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc
);
260 BT_OBJECT_PUT_REF_AND_RESET(enum_fc
);
263 return (void *) enum_fc
;
266 struct bt_private_field_class
*
267 bt_private_field_class_unsigned_enumeration_create(void)
269 return (void *) create_enumeration_field_class(
270 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
273 struct bt_private_field_class
*
274 bt_private_field_class_signed_enumeration_create(void)
276 return (void *) create_enumeration_field_class(
277 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
280 uint64_t bt_field_class_enumeration_get_mapping_count(struct bt_field_class
*fc
)
282 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
284 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
285 BT_ASSERT_PRE_FC_IS_ENUM(fc
, "Field class");
286 return (uint64_t) enum_fc
->mappings
->len
;
289 void bt_field_class_unsigned_enumeration_borrow_mapping_by_index(
290 struct bt_field_class
*fc
, uint64_t index
,
292 struct bt_field_class_unsigned_enumeration_mapping_ranges
**ranges
)
294 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
295 struct bt_field_class_enumeration_mapping
*mapping
;
297 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
298 BT_ASSERT_PRE_NON_NULL(name
, "Name (output)");
299 BT_ASSERT_PRE_NON_NULL(ranges
, "Ranges (output)");
300 BT_ASSERT_PRE_VALID_INDEX(index
, enum_fc
->mappings
->len
);
301 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
303 mapping
= BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, index
);
304 *name
= mapping
->label
->str
;
305 *ranges
= (void *) mapping
;
308 void bt_field_class_signed_enumeration_borrow_mapping_by_index(
309 struct bt_field_class
*fc
, uint64_t index
,
311 struct bt_field_class_signed_enumeration_mapping_ranges
**ranges
)
313 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
314 struct bt_field_class_enumeration_mapping
*mapping
;
316 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
317 BT_ASSERT_PRE_NON_NULL(name
, "Name (output)");
318 BT_ASSERT_PRE_NON_NULL(ranges
, "Ranges (output)");
319 BT_ASSERT_PRE_VALID_INDEX(index
, enum_fc
->mappings
->len
);
320 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
322 mapping
= BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, index
);
323 *name
= mapping
->label
->str
;
324 *ranges
= (void *) mapping
;
328 uint64_t get_enumeration_field_class_mapping_range_count(
329 struct bt_field_class_enumeration_mapping
*mapping
)
331 BT_ASSERT_PRE_NON_NULL(mapping
, "Ranges");
332 return (uint64_t) mapping
->ranges
->len
;
335 uint64_t bt_field_class_unsigned_enumeration_mapping_ranges_get_range_count(
336 struct bt_field_class_unsigned_enumeration_mapping_ranges
*ranges
)
338 return get_enumeration_field_class_mapping_range_count((void *) ranges
);
341 uint64_t bt_field_class_signed_enumeration_mapping_ranges_get_range_count(
342 struct bt_field_class_signed_enumeration_mapping_ranges
*ranges
)
344 return get_enumeration_field_class_mapping_range_count((void *) ranges
);
348 void get_enumeration_field_class_mapping_range_at_index(
349 struct bt_field_class_enumeration_mapping
*mapping
,
350 uint64_t index
, uint64_t *lower
, uint64_t *upper
)
352 struct bt_field_class_enumeration_mapping_range
*range
;
354 BT_ASSERT_PRE_NON_NULL(mapping
, "Ranges");
355 BT_ASSERT_PRE_NON_NULL(lower
, "Range's lower (output)");
356 BT_ASSERT_PRE_NON_NULL(upper
, "Range's upper (output)");
357 BT_ASSERT_PRE_VALID_INDEX(index
, mapping
->ranges
->len
);
358 range
= BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping
, index
);
359 *lower
= range
->lower
.u
;
360 *upper
= range
->upper
.u
;
363 void bt_field_class_unsigned_enumeration_mapping_ranges_get_range_by_index(
364 struct bt_field_class_unsigned_enumeration_mapping_ranges
*ranges
,
365 uint64_t index
, uint64_t *lower
, uint64_t *upper
)
367 get_enumeration_field_class_mapping_range_at_index((void *) ranges
,
368 index
, lower
, upper
);
371 void bt_field_class_signed_enumeration_mapping_ranges_get_range_by_index(
372 struct bt_field_class_unsigned_enumeration_mapping_ranges
*ranges
,
373 uint64_t index
, int64_t *lower
, int64_t *upper
)
375 get_enumeration_field_class_mapping_range_at_index((void *) ranges
,
376 index
, (uint64_t *) lower
, (uint64_t *) upper
);
381 int bt_field_class_unsigned_enumeration_get_mapping_labels_by_value(
382 struct bt_field_class
*fc
, uint64_t value
,
383 bt_field_class_enumeration_mapping_label_array
*label_array
,
386 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
389 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
390 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
391 BT_ASSERT_PRE_NON_NULL(count
, "Count (output)");
392 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
394 g_ptr_array_set_size(enum_fc
->label_buf
, 0);
396 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
398 struct bt_field_class_enumeration_mapping
*mapping
=
399 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
401 for (j
= 0; j
< mapping
->ranges
->len
; j
++) {
402 struct bt_field_class_enumeration_mapping_range
*range
=
403 BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(
406 if (value
>= range
->lower
.u
&&
407 value
<= range
->upper
.u
) {
408 g_ptr_array_add(enum_fc
->label_buf
,
409 mapping
->label
->str
);
415 *label_array
= (void *) enum_fc
->label_buf
->pdata
;
416 *count
= (uint64_t) enum_fc
->label_buf
->len
;
420 int bt_field_class_signed_enumeration_get_mapping_labels_by_value(
421 struct bt_field_class
*fc
, int64_t value
,
422 bt_field_class_enumeration_mapping_label_array
*label_array
,
425 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
428 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
429 BT_ASSERT_PRE_NON_NULL(label_array
, "Label array (output)");
430 BT_ASSERT_PRE_NON_NULL(count
, "Count (output)");
431 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
433 g_ptr_array_set_size(enum_fc
->label_buf
, 0);
435 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
437 struct bt_field_class_enumeration_mapping
*mapping
=
438 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
440 for (j
= 0; j
< mapping
->ranges
->len
; j
++) {
441 struct bt_field_class_enumeration_mapping_range
*range
=
442 BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(
445 if (value
>= range
->lower
.i
&&
446 value
<= range
->upper
.i
) {
447 g_ptr_array_add(enum_fc
->label_buf
,
448 mapping
->label
->str
);
454 *label_array
= (void *) enum_fc
->label_buf
->pdata
;
455 *count
= (uint64_t) enum_fc
->label_buf
->len
;
460 int add_mapping_to_enumeration_field_class(struct bt_field_class
*fc
,
461 const char *label
, uint64_t lower
, uint64_t upper
)
465 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
466 struct bt_field_class_enumeration_mapping
*mapping
= NULL
;
467 struct bt_field_class_enumeration_mapping_range
*range
;
470 BT_ASSERT_PRE_NON_NULL(label
, "Label");
472 /* Find existing mapping identified by this label */
473 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
474 struct bt_field_class_enumeration_mapping
*mapping_candidate
=
475 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
477 if (strcmp(mapping_candidate
->label
->str
, label
) == 0) {
478 mapping
= mapping_candidate
;
484 /* Create new mapping for this label */
485 g_array_set_size(enum_fc
->mappings
, enum_fc
->mappings
->len
+ 1);
486 mapping
= BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
,
487 enum_fc
->mappings
->len
- 1);
488 mapping
->ranges
= g_array_new(FALSE
, TRUE
,
489 sizeof(struct bt_field_class_enumeration_mapping_range
));
490 if (!mapping
->ranges
) {
491 finalize_enumeration_field_class_mapping(mapping
);
492 g_array_set_size(enum_fc
->mappings
,
493 enum_fc
->mappings
->len
- 1);
498 mapping
->label
= g_string_new(label
);
499 if (!mapping
->label
) {
500 finalize_enumeration_field_class_mapping(mapping
);
501 g_array_set_size(enum_fc
->mappings
,
502 enum_fc
->mappings
->len
- 1);
510 g_array_set_size(mapping
->ranges
, mapping
->ranges
->len
+ 1);
511 range
= BT_FIELD_CLASS_ENUM_MAPPING_RANGE_AT_INDEX(mapping
,
512 mapping
->ranges
->len
- 1);
513 range
->lower
.u
= lower
;
514 range
->upper
.u
= upper
;
515 BT_LIB_LOGV("Added mapping to enumeration field class: "
516 "%![fc-]+F, label=\"%s\", lower-unsigned=%" PRIu64
", "
517 "upper-unsigned=%" PRIu64
, fc
, label
, lower
, upper
);
523 int bt_private_field_class_unsigned_enumeration_map_range(
524 struct bt_private_field_class
*priv_fc
, const char *label
,
525 uint64_t range_lower
, uint64_t range_upper
)
527 struct bt_field_class
*fc
= (void *) priv_fc
;
528 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
530 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
531 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
533 BT_ASSERT_PRE(range_lower
<= range_upper
,
534 "Range's upper bound is less than lower bound: "
535 "upper=%" PRIu64
", lower=%" PRIu64
,
536 range_lower
, range_upper
);
537 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc
->common
.range
,
539 "Range's lower bound is outside the enumeration field class's value range: "
540 "%![fc-]+F, lower=%" PRIu64
, fc
, range_lower
);
541 BT_ASSERT_PRE(bt_util_value_is_in_range_unsigned(enum_fc
->common
.range
,
543 "Range's upper bound is outside the enumeration field class's value range: "
544 "%![fc-]+F, upper=%" PRIu64
, fc
, range_upper
);
545 return add_mapping_to_enumeration_field_class(fc
, label
, range_lower
,
549 int bt_private_field_class_signed_enumeration_map_range(
550 struct bt_private_field_class
*priv_fc
, const char *label
,
551 int64_t range_lower
, int64_t range_upper
)
553 struct bt_field_class
*fc
= (void *) priv_fc
;
554 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
556 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
557 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
559 BT_ASSERT_PRE(range_lower
<= range_upper
,
560 "Range's upper bound is less than lower bound: "
561 "upper=%" PRId64
", lower=%" PRId64
,
562 range_lower
, range_upper
);
563 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc
->common
.range
,
565 "Range's lower bound is outside the enumeration field class's value range: "
566 "%![fc-]+F, lower=%" PRId64
, fc
, range_lower
);
567 BT_ASSERT_PRE(bt_util_value_is_in_range_signed(enum_fc
->common
.range
,
569 "Range's upper bound is outside the enumeration field class's value range: "
570 "%![fc-]+F, upper=%" PRId64
, fc
, range_upper
);
571 return add_mapping_to_enumeration_field_class(fc
, label
, range_lower
,
576 void destroy_real_field_class(struct bt_object
*obj
)
579 BT_LIB_LOGD("Destroying real field class object: %!+F", obj
);
583 struct bt_private_field_class
*bt_private_field_class_real_create(void)
585 struct bt_field_class_real
*real_fc
= NULL
;
587 BT_LOGD_STR("Creating default real field class object.");
588 real_fc
= g_new0(struct bt_field_class_real
, 1);
590 BT_LOGE_STR("Failed to allocate one real field class.");
594 init_field_class((void *) real_fc
, BT_FIELD_CLASS_TYPE_REAL
,
595 destroy_real_field_class
);
596 BT_LIB_LOGD("Created real field class object: %!+F", real_fc
);
600 BT_OBJECT_PUT_REF_AND_RESET(real_fc
);
603 return (void *) real_fc
;
606 bt_bool
bt_field_class_real_is_single_precision(struct bt_field_class
*fc
)
608 struct bt_field_class_real
*real_fc
= (void *) fc
;
610 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
611 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_REAL
, "Field class");
612 return real_fc
->is_single_precision
;
615 void bt_private_field_class_real_set_is_single_precision(
616 struct bt_private_field_class
*priv_fc
,
617 bt_bool is_single_precision
)
619 struct bt_field_class
*fc
= (void *) priv_fc
;
620 struct bt_field_class_real
*real_fc
= (void *) fc
;
622 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
623 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_REAL
, "Field class");
624 BT_ASSERT_PRE_FC_HOT(fc
, "Field class");
625 real_fc
->is_single_precision
= (bool) is_single_precision
;
626 BT_LIB_LOGV("Set real field class's \"is single precision\" property: "
631 int init_named_field_classes_container(
632 struct bt_field_class_named_field_class_container
*fc
,
633 enum bt_field_class_type type
, bt_object_release_func release_func
)
637 init_field_class((void *) fc
, type
, release_func
);
638 fc
->named_fcs
= g_array_new(FALSE
, TRUE
,
639 sizeof(struct bt_named_field_class
));
640 if (!fc
->named_fcs
) {
641 BT_LOGE_STR("Failed to allocate a GArray.");
646 fc
->name_to_index
= g_hash_table_new(g_str_hash
, g_str_equal
);
647 if (!fc
->name_to_index
) {
648 BT_LOGE_STR("Failed to allocate a GHashTable.");
658 void finalize_named_field_class(struct bt_named_field_class
*named_fc
)
661 BT_LIB_LOGD("Finalizing named field class: "
662 "addr=%p, name=\"%s\", %![fc-]+F",
663 named_fc
, named_fc
->name
? named_fc
->name
->str
: NULL
,
666 if (named_fc
->name
) {
667 g_string_free(named_fc
->name
, TRUE
);
670 BT_LOGD_STR("Putting named field class's field class.");
671 bt_object_put_ref(named_fc
->fc
);
675 void finalize_named_field_classes_container(
676 struct bt_field_class_named_field_class_container
*fc
)
683 for (i
= 0; i
< fc
->named_fcs
->len
; i
++) {
684 finalize_named_field_class(
685 &g_array_index(fc
->named_fcs
,
686 struct bt_named_field_class
, i
));
689 g_array_free(fc
->named_fcs
, TRUE
);
692 if (fc
->name_to_index
) {
693 g_hash_table_destroy(fc
->name_to_index
);
698 void destroy_structure_field_class(struct bt_object
*obj
)
701 BT_LIB_LOGD("Destroying string field class object: %!+F", obj
);
702 finalize_named_field_classes_container((void *) obj
);
706 struct bt_private_field_class
*bt_private_field_class_structure_create(void)
709 struct bt_field_class_structure
*struct_fc
= NULL
;
711 BT_LOGD_STR("Creating default structure field class object.");
712 struct_fc
= g_new0(struct bt_field_class_structure
, 1);
714 BT_LOGE_STR("Failed to allocate one structure field class.");
718 ret
= init_named_field_classes_container((void *) struct_fc
,
719 BT_FIELD_CLASS_TYPE_STRUCTURE
, destroy_structure_field_class
);
724 BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc
);
728 BT_OBJECT_PUT_REF_AND_RESET(struct_fc
);
731 return (void *) struct_fc
;
735 int append_named_field_class_to_container_field_class(
736 struct bt_field_class_named_field_class_container
*container_fc
,
737 const char *name
, struct bt_field_class
*fc
)
740 struct bt_named_field_class
*named_fc
;
743 BT_ASSERT(container_fc
);
744 BT_ASSERT_PRE_FC_HOT(container_fc
, "Field class");
745 BT_ASSERT_PRE_NON_NULL(name
, "Name");
746 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
747 BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc
->name_to_index
,
749 "Duplicate member/option name in structure/variant field class: "
750 "%![container-fc-]+F, name=\"%s\"", container_fc
, name
);
751 name_str
= g_string_new(name
);
753 BT_LOGE_STR("Failed to allocate a GString.");
758 g_array_set_size(container_fc
->named_fcs
,
759 container_fc
->named_fcs
->len
+ 1);
760 named_fc
= &g_array_index(container_fc
->named_fcs
,
761 struct bt_named_field_class
, container_fc
->named_fcs
->len
- 1);
762 named_fc
->name
= name_str
;
763 named_fc
->fc
= bt_object_get_ref(fc
);
764 g_hash_table_insert(container_fc
->name_to_index
, named_fc
->name
->str
,
765 GUINT_TO_POINTER(container_fc
->named_fcs
->len
- 1));
766 bt_field_class_freeze(fc
);
772 int bt_private_field_class_structure_append_member(
773 struct bt_private_field_class
*priv_fc
,
774 const char *name
, struct bt_private_field_class
*member_fc
)
776 struct bt_field_class
*fc
= (void *) priv_fc
;
778 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
779 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field class");
780 return append_named_field_class_to_container_field_class((void *) fc
,
781 name
, (void *) member_fc
);
784 uint64_t bt_field_class_structure_get_member_count(struct bt_field_class
*fc
)
786 struct bt_field_class_structure
*struct_fc
= (void *) fc
;
788 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
789 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field class");
790 return (uint64_t) struct_fc
->common
.named_fcs
->len
;
794 void borrow_named_field_class_from_container_field_class_at_index(
795 struct bt_field_class_named_field_class_container
*fc
,
796 uint64_t index
, const char **name
,
797 struct bt_field_class
**out_fc
)
799 struct bt_named_field_class
*named_fc
;
802 BT_ASSERT_PRE_NON_NULL(name
, "Name");
803 BT_ASSERT_PRE_NON_NULL(out_fc
, "Field class (output)");
804 BT_ASSERT_PRE_VALID_INDEX(index
, fc
->named_fcs
->len
);
805 named_fc
= BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc
, index
);
806 *name
= named_fc
->name
->str
;
807 *out_fc
= named_fc
->fc
;
810 void bt_field_class_structure_borrow_member_by_index(
811 struct bt_field_class
*fc
, uint64_t index
,
812 const char **name
, struct bt_field_class
**out_fc
)
814 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
815 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field class");
816 borrow_named_field_class_from_container_field_class_at_index((void *) fc
,
817 index
, name
, out_fc
);
820 void bt_private_field_class_structure_borrow_member_by_index(
821 struct bt_private_field_class
*fc
, uint64_t index
,
822 const char **name
, struct bt_private_field_class
**out_fc
)
824 bt_field_class_structure_borrow_member_by_index((void *) fc
,
825 index
, name
, (void *) out_fc
);
829 struct bt_field_class
*borrow_field_class_from_container_field_class_by_name(
830 struct bt_field_class_named_field_class_container
*fc
,
833 struct bt_field_class
*ret_fc
= NULL
;
834 struct bt_named_field_class
*named_fc
;
839 BT_ASSERT_PRE_NON_NULL(name
, "Name");
840 if (!g_hash_table_lookup_extended(fc
->name_to_index
, name
, &orig_key
,
845 named_fc
= BT_FIELD_CLASS_NAMED_FC_AT_INDEX(fc
,
846 GPOINTER_TO_UINT(value
));
847 ret_fc
= named_fc
->fc
;
853 struct bt_field_class
*bt_field_class_structure_borrow_member_field_class_by_name(
854 struct bt_field_class
*fc
, const char *name
)
856 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
857 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
, "Field class");
858 return borrow_field_class_from_container_field_class_by_name((void *) fc
,
862 struct bt_private_field_class
*
863 bt_private_field_class_structure_borrow_member_field_class_by_name(
864 struct bt_private_field_class
*fc
, const char *name
)
866 return (void *) bt_field_class_structure_borrow_member_field_class_by_name(
871 void destroy_variant_field_class(struct bt_object
*obj
)
873 struct bt_field_class_variant
*fc
= (void *) obj
;
876 BT_LIB_LOGD("Destroying variant field class object: %!+F", fc
);
877 finalize_named_field_classes_container((void *) fc
);
878 BT_LOGD_STR("Putting selector field path.");
879 bt_object_put_ref(fc
->selector_field_path
);
883 struct bt_private_field_class
*bt_private_field_class_variant_create(void)
886 struct bt_field_class_variant
*var_fc
= NULL
;
888 BT_LOGD_STR("Creating default variant field class object.");
889 var_fc
= g_new0(struct bt_field_class_variant
, 1);
891 BT_LOGE_STR("Failed to allocate one variant field class.");
895 ret
= init_named_field_classes_container((void *) var_fc
,
896 BT_FIELD_CLASS_TYPE_VARIANT
, destroy_variant_field_class
);
901 BT_LIB_LOGD("Created variant field class object: %!+F", var_fc
);
905 BT_OBJECT_PUT_REF_AND_RESET(var_fc
);
908 return (void *) var_fc
;
911 int bt_private_field_class_variant_set_selector_field_class(
912 struct bt_private_field_class
*priv_fc
,
913 struct bt_private_field_class
*selector_fc
)
915 struct bt_field_class
*fc
= (void *) priv_fc
;
916 struct bt_field_class_variant
*var_fc
= (void *) fc
;
918 BT_ASSERT_PRE_NON_NULL(fc
, "Variant field class");
919 BT_ASSERT_PRE_NON_NULL(selector_fc
, "Selector field class");
920 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
, "Field class");
921 BT_ASSERT_PRE_FC_IS_ENUM(selector_fc
, "Selector field class");
922 BT_ASSERT_PRE_FC_HOT(fc
, "Variant field class");
923 var_fc
->selector_fc
= (void *) selector_fc
;
924 bt_field_class_freeze((void *) selector_fc
);
928 int bt_private_field_class_variant_append_private_option(
929 struct bt_private_field_class
*priv_fc
,
930 const char *name
, struct bt_private_field_class
*option_fc
)
932 struct bt_field_class
*fc
= (void *) priv_fc
;
934 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
935 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
, "Field class");
936 return append_named_field_class_to_container_field_class((void *) fc
,
937 name
, (void *) option_fc
);
940 struct bt_field_class
*bt_field_class_variant_borrow_option_field_class_by_name(
941 struct bt_field_class
*fc
, const char *name
)
943 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
944 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
, "Field class");
945 return borrow_field_class_from_container_field_class_by_name((void *) fc
,
949 struct bt_private_field_class
*
950 bt_private_field_class_variant_borrow_option_field_class_by_name(
951 struct bt_private_field_class
*fc
, const char *name
)
953 return (void *) bt_field_class_variant_borrow_option_field_class_by_name(
957 uint64_t bt_field_class_variant_get_option_count(struct bt_field_class
*fc
)
959 struct bt_field_class_variant
*var_fc
= (void *) fc
;
961 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
962 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
, "Field class");
963 return (uint64_t) var_fc
->common
.named_fcs
->len
;
966 void bt_field_class_variant_borrow_option_by_index(
967 struct bt_field_class
*fc
, uint64_t index
,
968 const char **name
, struct bt_field_class
**out_fc
)
970 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
971 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
, "Field class");
972 borrow_named_field_class_from_container_field_class_at_index((void *) fc
,
973 index
, name
, out_fc
);
976 void bt_private_field_class_variant_borrow_option_by_index(
977 struct bt_private_field_class
*fc
, uint64_t index
,
978 const char **name
, struct bt_private_field_class
**out_fc
)
980 bt_field_class_variant_borrow_option_by_index((void *) fc
,
981 index
, name
, (void *) out_fc
);
984 struct bt_field_path
*bt_field_class_variant_borrow_selector_field_path(
985 struct bt_field_class
*fc
)
987 struct bt_field_class_variant
*var_fc
= (void *) fc
;
989 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
990 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_VARIANT
,
992 return var_fc
->selector_field_path
;
996 void init_array_field_class(struct bt_field_class_array
*fc
,
997 enum bt_field_class_type type
, bt_object_release_func release_func
,
998 struct bt_field_class
*element_fc
)
1000 BT_ASSERT(element_fc
);
1001 init_field_class((void *) fc
, type
, release_func
);
1002 fc
->element_fc
= bt_object_get_ref(element_fc
);
1003 bt_field_class_freeze(element_fc
);
1007 void finalize_array_field_class(struct bt_field_class_array
*array_fc
)
1009 BT_ASSERT(array_fc
);
1010 BT_LOGD_STR("Putting element field class.");
1011 bt_object_put_ref(array_fc
->element_fc
);
1015 void destroy_static_array_field_class(struct bt_object
*obj
)
1018 BT_LIB_LOGD("Destroying static array field class object: %!+F", obj
);
1019 finalize_array_field_class((void *) obj
);
1023 struct bt_private_field_class
*
1024 bt_private_field_class_static_array_create(
1025 struct bt_private_field_class
*priv_element_fc
,
1028 struct bt_field_class
*element_fc
= (void *) priv_element_fc
;
1029 struct bt_field_class_static_array
*array_fc
= NULL
;
1031 BT_ASSERT_PRE_NON_NULL(element_fc
, "Element field class");
1032 BT_LOGD_STR("Creating default static array field class object.");
1033 array_fc
= g_new0(struct bt_field_class_static_array
, 1);
1035 BT_LOGE_STR("Failed to allocate one static array field class.");
1039 init_array_field_class((void *) array_fc
, BT_FIELD_CLASS_TYPE_STATIC_ARRAY
,
1040 destroy_static_array_field_class
, element_fc
);
1041 array_fc
->length
= length
;
1042 BT_LIB_LOGD("Created static array field class object: %!+F", array_fc
);
1046 BT_OBJECT_PUT_REF_AND_RESET(array_fc
);
1049 return (void *) array_fc
;
1052 struct bt_field_class
*bt_field_class_array_borrow_element_field_class(
1053 struct bt_field_class
*fc
)
1055 struct bt_field_class_array
*array_fc
= (void *) fc
;
1057 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1058 BT_ASSERT_PRE_FC_IS_ARRAY(fc
, "Field class");
1059 return array_fc
->element_fc
;
1062 struct bt_private_field_class
*
1063 bt_private_field_class_array_borrow_element_field_class(
1064 struct bt_private_field_class
*fc
)
1066 return (void *) bt_field_class_array_borrow_element_field_class(
1070 uint64_t bt_field_class_static_array_get_length(struct bt_field_class
*fc
)
1072 struct bt_field_class_static_array
*array_fc
= (void *) fc
;
1074 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1075 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STATIC_ARRAY
,
1077 return (uint64_t) array_fc
->length
;
1081 void destroy_dynamic_array_field_class(struct bt_object
*obj
)
1083 struct bt_field_class_dynamic_array
*fc
= (void *) obj
;
1086 BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc
);
1087 finalize_array_field_class((void *) fc
);
1088 BT_LOGD_STR("Putting length field path.");
1089 bt_object_put_ref(fc
->length_field_path
);
1093 struct bt_private_field_class
*bt_private_field_class_dynamic_array_create(
1094 struct bt_private_field_class
*priv_element_fc
)
1096 struct bt_field_class
*element_fc
= (void *) priv_element_fc
;
1097 struct bt_field_class_dynamic_array
*array_fc
= NULL
;
1099 BT_ASSERT_PRE_NON_NULL(element_fc
, "Element field class");
1100 BT_LOGD_STR("Creating default dynamic array field class object.");
1101 array_fc
= g_new0(struct bt_field_class_dynamic_array
, 1);
1103 BT_LOGE_STR("Failed to allocate one dynamic array field class.");
1107 init_array_field_class((void *) array_fc
, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
,
1108 destroy_dynamic_array_field_class
, element_fc
);
1109 BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc
);
1113 BT_OBJECT_PUT_REF_AND_RESET(array_fc
);
1116 return (void *) array_fc
;
1119 int bt_private_field_class_dynamic_array_set_length_field_class(
1120 struct bt_private_field_class
*priv_fc
,
1121 struct bt_private_field_class
*priv_length_fc
)
1123 struct bt_field_class
*fc
= (void *) priv_fc
;
1124 struct bt_field_class
*length_fc
= (void *) priv_length_fc
;
1125 struct bt_field_class_dynamic_array
*array_fc
= (void *) fc
;
1127 BT_ASSERT_PRE_NON_NULL(fc
, "Dynamic array field class");
1128 BT_ASSERT_PRE_NON_NULL(length_fc
, "Length field class");
1129 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
,
1131 BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc
, "Length field class");
1132 BT_ASSERT_PRE_FC_HOT(fc
, "Dynamic array field class");
1133 array_fc
->length_fc
= length_fc
;
1134 bt_field_class_freeze(length_fc
);
1138 struct bt_field_path
*bt_field_class_dynamic_array_borrow_length_field_path(
1139 struct bt_field_class
*fc
)
1141 struct bt_field_class_dynamic_array
*seq_fc
= (void *) fc
;
1143 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1144 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
,
1146 return seq_fc
->length_field_path
;
1150 void destroy_string_field_class(struct bt_object
*obj
)
1153 BT_LIB_LOGD("Destroying string field class object: %!+F", obj
);
1157 struct bt_private_field_class
*bt_private_field_class_string_create(void)
1159 struct bt_field_class_string
*string_fc
= NULL
;
1161 BT_LOGD_STR("Creating default string field class object.");
1162 string_fc
= g_new0(struct bt_field_class_string
, 1);
1164 BT_LOGE_STR("Failed to allocate one string field class.");
1168 init_field_class((void *) string_fc
, BT_FIELD_CLASS_TYPE_STRING
,
1169 destroy_string_field_class
);
1170 BT_LIB_LOGD("Created string field class object: %!+F", string_fc
);
1174 BT_OBJECT_PUT_REF_AND_RESET(string_fc
);
1177 return (void *) string_fc
;
1181 void _bt_field_class_freeze(struct bt_field_class
*fc
)
1184 * Element/member/option field classes are frozen when added to
1192 void _bt_field_class_make_part_of_trace(struct bt_field_class
*fc
)
1195 BT_ASSERT_PRE(!fc
->part_of_trace
,
1196 "Field class is already part of a trace: %!+F", fc
);
1197 fc
->part_of_trace
= true;
1200 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1201 case BT_FIELD_CLASS_TYPE_VARIANT
:
1203 struct bt_field_class_named_field_class_container
*container_fc
=
1207 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
1208 struct bt_named_field_class
*named_fc
=
1209 BT_FIELD_CLASS_NAMED_FC_AT_INDEX(
1212 bt_field_class_make_part_of_trace(named_fc
->fc
);
1217 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1218 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1220 struct bt_field_class_array
*array_fc
= (void *) fc
;
1222 bt_field_class_make_part_of_trace(array_fc
->element_fc
);