2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
24 #define BT_LOG_TAG "LIB/FIELD-CLASS"
25 #include "lib/logging.h"
27 #include "lib/assert-pre.h"
28 #include <babeltrace2/trace-ir/field-class.h>
29 #include <babeltrace2/trace-ir/field-class-const.h>
30 #include <babeltrace2/trace-ir/field-const.h>
31 #include <babeltrace2/trace-ir/field.h>
32 #include <babeltrace2/trace-ir/clock-class.h>
33 #include "lib/object.h"
34 #include "compat/compiler.h"
35 #include "compat/endian.h"
36 #include "common/assert.h"
37 #include "compat/glib.h"
42 #include "clock-class.h"
43 #include "field-class.h"
45 #include "field-path.h"
47 #include "lib/func-status.h"
48 #include "lib/integer-range-set.h"
49 #include "lib/value.h"
51 enum bt_field_class_type
bt_field_class_get_type(
52 const struct bt_field_class
*fc
)
54 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
59 int init_field_class(struct bt_field_class
*fc
, enum bt_field_class_type type
,
60 bt_object_release_func release_func
)
65 BT_ASSERT(release_func
);
66 bt_object_init_shared(&fc
->base
, release_func
);
68 fc
->user_attributes
= bt_value_map_create();
69 if (!fc
->user_attributes
) {
70 BT_LIB_LOGE_APPEND_CAUSE(
71 "Failed to create a map value object.");
81 void finalize_field_class(struct bt_field_class
*fc
)
83 BT_OBJECT_PUT_REF_AND_RESET(fc
->user_attributes
);
87 void destroy_bit_array_field_class(struct bt_object
*obj
)
90 BT_LIB_LOGD("Destroying bit array field class object: %!+F", obj
);
91 finalize_field_class((void *) obj
);
95 struct bt_field_class
*bt_field_class_bit_array_create(
96 struct bt_trace_class
*trace_class
, uint64_t length
)
98 struct bt_field_class_bit_array
*ba_fc
= NULL
;
100 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
101 BT_ASSERT_PRE(length
> 0 && length
<= 64,
102 "Unsupported length for bit array field class "
103 "(minimum is 1, maximum is 64): length=%" PRIu64
, length
);
104 BT_LOGD("Creating default bit array field class object.");
105 ba_fc
= g_new0(struct bt_field_class_bit_array
, 1);
107 BT_LIB_LOGE_APPEND_CAUSE(
108 "Failed to allocate one bit array field class.");
112 if (init_field_class((void *) ba_fc
, BT_FIELD_CLASS_TYPE_BIT_ARRAY
,
113 destroy_bit_array_field_class
)) {
117 ba_fc
->length
= length
;
118 BT_LIB_LOGD("Created bit array field class object: %!+F", ba_fc
);
122 BT_OBJECT_PUT_REF_AND_RESET(ba_fc
);
125 return (void *) ba_fc
;
128 uint64_t bt_field_class_bit_array_get_length(const struct bt_field_class
*fc
)
130 const struct bt_field_class_bit_array
*ba_fc
= (const void *) fc
;
132 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
133 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_BIT_ARRAY
,
135 return ba_fc
->length
;
139 void destroy_bool_field_class(struct bt_object
*obj
)
142 BT_LIB_LOGD("Destroying boolean field class object: %!+F", obj
);
143 finalize_field_class((void *) obj
);
147 struct bt_field_class
*bt_field_class_bool_create(
148 bt_trace_class
*trace_class
)
150 struct bt_field_class_bool
*bool_fc
= NULL
;
152 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
153 BT_LOGD("Creating default boolean field class object.");
154 bool_fc
= g_new0(struct bt_field_class_bool
, 1);
156 BT_LIB_LOGE_APPEND_CAUSE(
157 "Failed to allocate one boolean field class.");
161 if (init_field_class((void *) bool_fc
, BT_FIELD_CLASS_TYPE_BOOL
,
162 destroy_bool_field_class
)) {
166 BT_LIB_LOGD("Created boolean field class object: %!+F", bool_fc
);
170 BT_OBJECT_PUT_REF_AND_RESET(bool_fc
);
173 return (void *) bool_fc
;
177 int init_integer_field_class(struct bt_field_class_integer
*fc
,
178 enum bt_field_class_type type
,
179 bt_object_release_func release_func
)
183 ret
= init_field_class((void *) fc
, type
, release_func
);
189 fc
->base
= BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
;
196 void destroy_integer_field_class(struct bt_object
*obj
)
199 BT_LIB_LOGD("Destroying integer field class object: %!+F", obj
);
200 finalize_field_class((void *) obj
);
205 struct bt_field_class
*create_integer_field_class(bt_trace_class
*trace_class
,
206 enum bt_field_class_type type
)
208 struct bt_field_class_integer
*int_fc
= NULL
;
210 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
211 BT_LOGD("Creating default integer field class object: type=%s",
212 bt_common_field_class_type_string(type
));
213 int_fc
= g_new0(struct bt_field_class_integer
, 1);
215 BT_LIB_LOGE_APPEND_CAUSE(
216 "Failed to allocate one integer field class.");
220 if (init_integer_field_class(int_fc
, type
,
221 destroy_integer_field_class
)) {
225 BT_LIB_LOGD("Created integer field class object: %!+F", int_fc
);
229 BT_OBJECT_PUT_REF_AND_RESET(int_fc
);
232 return (void *) int_fc
;
235 struct bt_field_class
*bt_field_class_integer_unsigned_create(
236 bt_trace_class
*trace_class
)
238 return create_integer_field_class(trace_class
,
239 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
);
242 struct bt_field_class
*bt_field_class_integer_signed_create(
243 bt_trace_class
*trace_class
)
245 return create_integer_field_class(trace_class
,
246 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
);
249 uint64_t bt_field_class_integer_get_field_value_range(
250 const struct bt_field_class
*fc
)
252 const struct bt_field_class_integer
*int_fc
= (const void *) fc
;
254 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
255 BT_ASSERT_PRE_DEV_FC_IS_INT(fc
, "Field class");
256 return int_fc
->range
;
260 bool size_is_valid_for_enumeration_field_class(struct bt_field_class
*fc
,
267 void bt_field_class_integer_set_field_value_range(
268 struct bt_field_class
*fc
, uint64_t size
)
270 struct bt_field_class_integer
*int_fc
= (void *) fc
;
272 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
273 BT_ASSERT_PRE_FC_IS_INT(fc
, "Field class");
274 BT_ASSERT_PRE_DEV_FC_HOT(fc
, "Field class");
275 BT_ASSERT_PRE(size
<= 64,
276 "Unsupported size for integer field class's field value range "
277 "(maximum is 64): size=%" PRIu64
, size
);
279 int_fc
->common
.type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
280 int_fc
->common
.type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
281 size_is_valid_for_enumeration_field_class(fc
, size
),
282 "Invalid field value range for enumeration field class: "
283 "at least one of the current mapping ranges contains values "
284 "which are outside this range: %!+F, size=%" PRIu64
, fc
, size
);
285 int_fc
->range
= size
;
286 BT_LIB_LOGD("Set integer field class's field value range: %!+F", fc
);
289 enum bt_field_class_integer_preferred_display_base
290 bt_field_class_integer_get_preferred_display_base(const struct bt_field_class
*fc
)
292 const struct bt_field_class_integer
*int_fc
= (const void *) fc
;
294 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
295 BT_ASSERT_PRE_DEV_FC_IS_INT(fc
, "Field class");
299 void bt_field_class_integer_set_preferred_display_base(
300 struct bt_field_class
*fc
,
301 enum bt_field_class_integer_preferred_display_base base
)
303 struct bt_field_class_integer
*int_fc
= (void *) fc
;
305 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
306 BT_ASSERT_PRE_FC_IS_INT(fc
, "Field class");
307 BT_ASSERT_PRE_DEV_FC_HOT(fc
, "Field class");
309 BT_LIB_LOGD("Set integer field class's preferred display base: %!+F", fc
);
313 void finalize_enumeration_field_class_mapping(
314 struct bt_field_class_enumeration_mapping
*mapping
)
318 if (mapping
->label
) {
319 g_string_free(mapping
->label
, TRUE
);
320 mapping
->label
= NULL
;
323 BT_OBJECT_PUT_REF_AND_RESET(mapping
->range_set
);
327 void destroy_enumeration_field_class(struct bt_object
*obj
)
329 struct bt_field_class_enumeration
*fc
= (void *) obj
;
332 BT_LIB_LOGD("Destroying enumeration field class object: %!+F", fc
);
333 finalize_field_class((void *) obj
);
338 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
339 finalize_enumeration_field_class_mapping(
340 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, i
));
343 g_array_free(fc
->mappings
, TRUE
);
348 g_ptr_array_free(fc
->label_buf
, TRUE
);
349 fc
->label_buf
= NULL
;
356 struct bt_field_class
*create_enumeration_field_class(
357 bt_trace_class
*trace_class
, enum bt_field_class_type type
)
359 struct bt_field_class_enumeration
*enum_fc
= NULL
;
361 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
362 BT_LOGD("Creating default enumeration field class object: type=%s",
363 bt_common_field_class_type_string(type
));
364 enum_fc
= g_new0(struct bt_field_class_enumeration
, 1);
366 BT_LIB_LOGE_APPEND_CAUSE(
367 "Failed to allocate one enumeration field class.");
371 if (init_integer_field_class((void *) enum_fc
, type
,
372 destroy_enumeration_field_class
)) {
376 enum_fc
->mappings
= g_array_new(FALSE
, TRUE
,
377 sizeof(struct bt_field_class_enumeration_mapping
));
378 if (!enum_fc
->mappings
) {
379 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
383 enum_fc
->label_buf
= g_ptr_array_new();
384 if (!enum_fc
->label_buf
) {
385 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
389 BT_LIB_LOGD("Created enumeration field class object: %!+F", enum_fc
);
393 BT_OBJECT_PUT_REF_AND_RESET(enum_fc
);
396 return (void *) enum_fc
;
399 struct bt_field_class
*bt_field_class_enumeration_unsigned_create(
400 bt_trace_class
*trace_class
)
402 return create_enumeration_field_class(trace_class
,
403 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
);
406 struct bt_field_class
*bt_field_class_enumeration_signed_create(
407 bt_trace_class
*trace_class
)
409 return create_enumeration_field_class(trace_class
,
410 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
);
413 uint64_t bt_field_class_enumeration_get_mapping_count(
414 const struct bt_field_class
*fc
)
416 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
418 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
419 BT_ASSERT_PRE_DEV_FC_IS_ENUM(fc
, "Field class");
420 return (uint64_t) enum_fc
->mappings
->len
;
423 const struct bt_field_class_enumeration_unsigned_mapping
*
424 bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(
425 const struct bt_field_class
*fc
, uint64_t index
)
427 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
429 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
430 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, enum_fc
->mappings
->len
);
431 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
433 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, index
);
436 const struct bt_field_class_enumeration_signed_mapping
*
437 bt_field_class_enumeration_signed_borrow_mapping_by_index_const(
438 const struct bt_field_class
*fc
, uint64_t index
)
440 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
442 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
443 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, enum_fc
->mappings
->len
);
444 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
446 return (const void *) BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, index
);
450 const struct bt_field_class_enumeration_mapping
*
451 borrow_enumeration_field_class_mapping_by_label(
452 const struct bt_field_class_enumeration
*fc
, const char *label
)
454 struct bt_field_class_enumeration_mapping
*mapping
= NULL
;
458 BT_ASSERT_PRE_DEV_NON_NULL(label
, "Label");
460 for (i
= 0; i
< fc
->mappings
->len
; i
++) {
461 struct bt_field_class_enumeration_mapping
*this_mapping
=
462 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(fc
, i
);
464 if (strcmp(this_mapping
->label
->str
, label
) == 0) {
465 mapping
= this_mapping
;
474 const struct bt_field_class_enumeration_signed_mapping
*
475 bt_field_class_enumeration_signed_borrow_mapping_by_label_const(
476 const struct bt_field_class
*fc
, const char *label
)
478 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
479 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
481 return (const void *) borrow_enumeration_field_class_mapping_by_label(
482 (const void *) fc
, label
);
485 const struct bt_field_class_enumeration_unsigned_mapping
*
486 bt_field_class_enumeration_unsigned_borrow_mapping_by_label_const(
487 const struct bt_field_class
*fc
, const char *label
)
489 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
490 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
,
491 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
, "Field class");
492 return (const void *) borrow_enumeration_field_class_mapping_by_label(
493 (const void *) fc
, label
);
496 const char *bt_field_class_enumeration_mapping_get_label(
497 const struct bt_field_class_enumeration_mapping
*mapping
)
499 BT_ASSERT_PRE_DEV_NON_NULL(mapping
, "Enumeration field class mapping");
500 return mapping
->label
->str
;
503 const struct bt_integer_range_set_unsigned
*
504 bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(
505 const struct bt_field_class_enumeration_unsigned_mapping
*u_mapping
)
507 const struct bt_field_class_enumeration_mapping
*mapping
=
508 (const void *) u_mapping
;
510 BT_ASSERT_PRE_DEV_NON_NULL(mapping
, "Enumeration field class mapping");
511 return (const void *) mapping
->range_set
;
514 const struct bt_integer_range_set_signed
*
515 bt_field_class_enumeration_signed_mapping_borrow_ranges_const(
516 const struct bt_field_class_enumeration_signed_mapping
*s_mapping
)
518 const struct bt_field_class_enumeration_mapping
*mapping
=
519 (const void *) s_mapping
;
521 BT_ASSERT_PRE_DEV_NON_NULL(mapping
, "Enumeration field class mapping");
522 return (const void *) mapping
->range_set
;
525 enum bt_field_class_enumeration_get_mapping_labels_for_value_status
526 bt_field_class_enumeration_unsigned_get_mapping_labels_for_value(
527 const struct bt_field_class
*fc
, uint64_t value
,
528 bt_field_class_enumeration_mapping_label_array
*label_array
,
531 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
534 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
535 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Label array (output)");
536 BT_ASSERT_PRE_DEV_NON_NULL(count
, "Count (output)");
537 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
539 g_ptr_array_set_size(enum_fc
->label_buf
, 0);
541 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
543 const struct bt_field_class_enumeration_mapping
*mapping
=
544 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
546 for (j
= 0; j
< mapping
->range_set
->ranges
->len
; j
++) {
547 const struct bt_integer_range
*range
= (const void *)
548 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
549 mapping
->range_set
, j
);
551 if (value
>= range
->lower
.u
&&
552 value
<= range
->upper
.u
) {
553 g_ptr_array_add(enum_fc
->label_buf
,
554 mapping
->label
->str
);
560 *label_array
= (void *) enum_fc
->label_buf
->pdata
;
561 *count
= (uint64_t) enum_fc
->label_buf
->len
;
562 return BT_FUNC_STATUS_OK
;
565 enum bt_field_class_enumeration_get_mapping_labels_for_value_status
566 bt_field_class_enumeration_signed_get_mapping_labels_for_value(
567 const struct bt_field_class
*fc
, int64_t value
,
568 bt_field_class_enumeration_mapping_label_array
*label_array
,
571 const struct bt_field_class_enumeration
*enum_fc
= (const void *) fc
;
574 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
575 BT_ASSERT_PRE_DEV_NON_NULL(label_array
, "Label array (output)");
576 BT_ASSERT_PRE_DEV_NON_NULL(count
, "Count (output)");
577 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
579 g_ptr_array_set_size(enum_fc
->label_buf
, 0);
581 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
583 const struct bt_field_class_enumeration_mapping
*mapping
=
584 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
586 for (j
= 0; j
< mapping
->range_set
->ranges
->len
; j
++) {
587 const struct bt_integer_range
*range
= (const void *)
588 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
589 mapping
->range_set
, j
);
591 if (value
>= range
->lower
.i
&&
592 value
<= range
->upper
.i
) {
593 g_ptr_array_add(enum_fc
->label_buf
,
594 mapping
->label
->str
);
600 *label_array
= (void *) enum_fc
->label_buf
->pdata
;
601 *count
= (uint64_t) enum_fc
->label_buf
->len
;
602 return BT_FUNC_STATUS_OK
;
606 bool enumeration_field_class_has_mapping_with_label(
607 const struct bt_field_class_enumeration
*enum_fc
,
616 for (i
= 0; i
< enum_fc
->mappings
->len
; i
++) {
617 struct bt_field_class_enumeration_mapping
*mapping_candidate
=
618 BT_FIELD_CLASS_ENUM_MAPPING_AT_INDEX(enum_fc
, i
);
620 if (strcmp(mapping_candidate
->label
->str
, label
) == 0) {
631 enum bt_field_class_enumeration_add_mapping_status
632 add_mapping_to_enumeration_field_class(struct bt_field_class
*fc
,
633 const char *label
, const struct bt_integer_range_set
*range_set
)
635 enum bt_field_class_enumeration_add_mapping_status status
=
637 struct bt_field_class_enumeration
*enum_fc
= (void *) fc
;
638 struct bt_field_class_enumeration_mapping mapping
= { 0 };
641 BT_ASSERT_PRE_NON_NULL(label
, "Label");
642 BT_ASSERT_PRE_NON_NULL(range_set
, "Integer range set");
643 BT_ASSERT_PRE(!enumeration_field_class_has_mapping_with_label(
645 "Duplicate mapping name in enumeration field class: "
646 "%![enum-fc-]+F, label=\"%s\"", fc
, label
);
647 mapping
.range_set
= range_set
;
648 bt_object_get_ref(mapping
.range_set
);
649 mapping
.label
= g_string_new(label
);
650 if (!mapping
.label
) {
651 finalize_enumeration_field_class_mapping(&mapping
);
652 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
656 g_array_append_val(enum_fc
->mappings
, mapping
);
657 BT_LIB_LOGD("Added mapping to enumeration field class: "
658 "%![fc-]+F, label=\"%s\"", fc
, label
);
664 enum bt_field_class_enumeration_add_mapping_status
665 bt_field_class_enumeration_unsigned_add_mapping(
666 struct bt_field_class
*fc
, const char *label
,
667 const struct bt_integer_range_set_unsigned
*range_set
)
669 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
670 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
,
672 return add_mapping_to_enumeration_field_class(fc
, label
,
673 (const void *) range_set
);
676 enum bt_field_class_enumeration_add_mapping_status
677 bt_field_class_enumeration_signed_add_mapping(
678 struct bt_field_class
*fc
, const char *label
,
679 const struct bt_integer_range_set_signed
*range_set
)
681 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
682 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
,
684 return add_mapping_to_enumeration_field_class(fc
, label
,
685 (const void *) range_set
);
689 void destroy_real_field_class(struct bt_object
*obj
)
692 BT_LIB_LOGD("Destroying real field class object: %!+F", obj
);
693 finalize_field_class((void *) obj
);
697 struct bt_field_class
*bt_field_class_real_create(bt_trace_class
*trace_class
)
699 struct bt_field_class_real
*real_fc
= NULL
;
701 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
702 BT_LOGD_STR("Creating default real field class object.");
703 real_fc
= g_new0(struct bt_field_class_real
, 1);
705 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field class.");
709 if (init_field_class((void *) real_fc
, BT_FIELD_CLASS_TYPE_REAL
,
710 destroy_real_field_class
)) {
714 BT_LIB_LOGD("Created real field class object: %!+F", real_fc
);
718 BT_OBJECT_PUT_REF_AND_RESET(real_fc
);
721 return (void *) real_fc
;
724 bt_bool
bt_field_class_real_is_single_precision(const struct bt_field_class
*fc
)
726 const struct bt_field_class_real
*real_fc
= (const void *) fc
;
728 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
729 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_REAL
, "Field class");
730 return real_fc
->is_single_precision
;
733 void bt_field_class_real_set_is_single_precision(struct bt_field_class
*fc
,
734 bt_bool is_single_precision
)
736 struct bt_field_class_real
*real_fc
= (void *) fc
;
738 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
739 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_REAL
, "Field class");
740 BT_ASSERT_PRE_DEV_FC_HOT(fc
, "Field class");
741 real_fc
->is_single_precision
= (bool) is_single_precision
;
742 BT_LIB_LOGD("Set real field class's \"is single precision\" property: "
747 int init_named_field_classes_container(
748 struct bt_field_class_named_field_class_container
*fc
,
749 enum bt_field_class_type type
,
750 bt_object_release_func fc_release_func
,
751 GDestroyNotify named_fc_destroy_func
)
755 ret
= init_field_class((void *) fc
, type
, fc_release_func
);
760 fc
->named_fcs
= g_ptr_array_new_with_free_func(named_fc_destroy_func
);
761 if (!fc
->named_fcs
) {
762 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
767 fc
->name_to_index
= g_hash_table_new(g_str_hash
, g_str_equal
);
768 if (!fc
->name_to_index
) {
769 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GHashTable.");
779 void finalize_named_field_class(struct bt_named_field_class
*named_fc
)
782 BT_LIB_LOGD("Finalizing named field class: "
783 "addr=%p, name=\"%s\", %![fc-]+F",
784 named_fc
, named_fc
->name
? named_fc
->name
->str
: NULL
,
786 BT_OBJECT_PUT_REF_AND_RESET(named_fc
->user_attributes
);
788 if (named_fc
->name
) {
789 g_string_free(named_fc
->name
, TRUE
);
790 named_fc
->name
= NULL
;
793 BT_LOGD_STR("Putting named field class's field class.");
794 BT_OBJECT_PUT_REF_AND_RESET(named_fc
->fc
);
798 void destroy_named_field_class(gpointer ptr
)
800 struct bt_named_field_class
*named_fc
= ptr
;
802 BT_OBJECT_PUT_REF_AND_RESET(named_fc
->user_attributes
);
805 finalize_named_field_class(ptr
);
811 void destroy_variant_with_selector_option(gpointer ptr
)
813 struct bt_field_class_variant_with_selector_option
*opt
= ptr
;
816 finalize_named_field_class(&opt
->common
);
817 BT_OBJECT_PUT_REF_AND_RESET(opt
->range_set
);
823 void finalize_named_field_classes_container(
824 struct bt_field_class_named_field_class_container
*fc
)
829 g_ptr_array_free(fc
->named_fcs
, TRUE
);
830 fc
->named_fcs
= NULL
;
834 if (fc
->name_to_index
) {
835 g_hash_table_destroy(fc
->name_to_index
);
836 fc
->name_to_index
= NULL
;
841 void destroy_structure_field_class(struct bt_object
*obj
)
844 BT_LIB_LOGD("Destroying structure field class object: %!+F", obj
);
845 finalize_field_class((void *) obj
);
846 finalize_named_field_classes_container((void *) obj
);
850 struct bt_field_class
*bt_field_class_structure_create(
851 bt_trace_class
*trace_class
)
854 struct bt_field_class_structure
*struct_fc
= NULL
;
856 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
857 BT_LOGD_STR("Creating default structure field class object.");
858 struct_fc
= g_new0(struct bt_field_class_structure
, 1);
860 BT_LIB_LOGE_APPEND_CAUSE(
861 "Failed to allocate one structure field class.");
865 ret
= init_named_field_classes_container((void *) struct_fc
,
866 BT_FIELD_CLASS_TYPE_STRUCTURE
, destroy_structure_field_class
,
867 destroy_named_field_class
);
869 /* init_named_field_classes_container() logs errors */
873 BT_LIB_LOGD("Created structure field class object: %!+F", struct_fc
);
877 BT_OBJECT_PUT_REF_AND_RESET(struct_fc
);
880 return (void *) struct_fc
;
884 int init_named_field_class(struct bt_named_field_class
*named_fc
,
885 const char *name
, struct bt_field_class
*fc
)
887 int status
= BT_FUNC_STATUS_OK
;
892 named_fc
->name
= g_string_new(name
);
893 if (!named_fc
->name
) {
894 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
895 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
899 named_fc
->user_attributes
= bt_value_map_create();
900 if (!named_fc
->user_attributes
) {
901 BT_LIB_LOGE_APPEND_CAUSE(
902 "Failed to create a map value object.");
903 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
908 bt_object_get_no_null_check(named_fc
->fc
);
915 struct bt_named_field_class
*create_named_field_class(const char *name
,
916 struct bt_field_class
*fc
)
918 struct bt_named_field_class
*named_fc
= g_new0(
919 struct bt_named_field_class
, 1);
922 BT_LIB_LOGE_APPEND_CAUSE(
923 "Failed to allocate a named field class.");
927 if (init_named_field_class(named_fc
, name
, fc
)) {
928 /* init_named_field_class() logs errors */
935 destroy_named_field_class(named_fc
);
943 struct bt_field_class_variant_with_selector_option
*
944 create_variant_with_selector_option(
945 const char *name
, struct bt_field_class
*fc
,
946 const struct bt_integer_range_set
*range_set
)
948 struct bt_field_class_variant_with_selector_option
*opt
= g_new0(
949 struct bt_field_class_variant_with_selector_option
, 1);
951 BT_ASSERT(range_set
);
954 BT_LIB_LOGE_APPEND_CAUSE(
955 "Failed to allocate a named field class.");
959 if (init_named_field_class(&opt
->common
, name
, fc
)) {
963 opt
->range_set
= range_set
;
964 bt_object_get_no_null_check(opt
->range_set
);
965 bt_integer_range_set_freeze(range_set
);
969 destroy_variant_with_selector_option(opt
);
977 int append_named_field_class_to_container_field_class(
978 struct bt_field_class_named_field_class_container
*container_fc
,
979 struct bt_named_field_class
*named_fc
)
981 BT_ASSERT(container_fc
);
983 BT_ASSERT_PRE_DEV_FC_HOT(container_fc
, "Field class");
984 BT_ASSERT_PRE(!bt_g_hash_table_contains(container_fc
->name_to_index
,
985 named_fc
->name
->str
),
986 "Duplicate member/option name in structure/variant field class: "
987 "%![container-fc-]+F, name=\"%s\"", container_fc
,
988 named_fc
->name
->str
);
991 * Freeze the contained field class, but not the named field
992 * class itself, as it's still possible afterwards to modify
993 * properties of the member/option object.
995 bt_field_class_freeze(named_fc
->fc
);
996 g_ptr_array_add(container_fc
->named_fcs
, named_fc
);
997 g_hash_table_insert(container_fc
->name_to_index
, named_fc
->name
->str
,
998 GUINT_TO_POINTER(container_fc
->named_fcs
->len
- 1));
999 return BT_FUNC_STATUS_OK
;
1002 enum bt_field_class_structure_append_member_status
1003 bt_field_class_structure_append_member(
1004 struct bt_field_class
*fc
, const char *name
,
1005 struct bt_field_class
*member_fc
)
1007 enum bt_field_class_structure_append_member_status status
;
1008 struct bt_named_field_class
*named_fc
= NULL
;
1010 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1011 BT_ASSERT_PRE_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1013 named_fc
= create_named_field_class(name
, member_fc
);
1015 /* create_named_field_class() logs errors */
1016 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1020 status
= append_named_field_class_to_container_field_class((void *) fc
,
1022 if (status
== BT_FUNC_STATUS_OK
) {
1023 /* Moved to the container */
1031 uint64_t bt_field_class_structure_get_member_count(
1032 const struct bt_field_class
*fc
)
1034 struct bt_field_class_structure
*struct_fc
= (void *) fc
;
1036 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1037 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1039 return (uint64_t) struct_fc
->common
.named_fcs
->len
;
1043 struct bt_named_field_class
*
1044 borrow_named_field_class_from_container_field_class_at_index(
1045 struct bt_field_class_named_field_class_container
*fc
,
1049 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, fc
->named_fcs
->len
);
1050 return fc
->named_fcs
->pdata
[index
];
1053 const struct bt_field_class_structure_member
*
1054 bt_field_class_structure_borrow_member_by_index_const(
1055 const struct bt_field_class
*fc
, uint64_t index
)
1057 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1058 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1060 return (const void *)
1061 borrow_named_field_class_from_container_field_class_at_index(
1062 (void *) fc
, index
);
1065 struct bt_field_class_structure_member
*
1066 bt_field_class_structure_borrow_member_by_index(
1067 struct bt_field_class
*fc
, uint64_t index
)
1069 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1070 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1073 borrow_named_field_class_from_container_field_class_at_index(
1074 (void *) fc
, index
);
1078 struct bt_named_field_class
*
1079 borrow_named_field_class_from_container_field_class_by_name(
1080 struct bt_field_class_named_field_class_container
*fc
,
1083 struct bt_named_field_class
*named_fc
= NULL
;
1088 BT_ASSERT_PRE_DEV_NON_NULL(name
, "Name");
1089 if (!g_hash_table_lookup_extended(fc
->name_to_index
, name
, &orig_key
,
1094 named_fc
= fc
->named_fcs
->pdata
[GPOINTER_TO_UINT(value
)];
1100 const struct bt_field_class_structure_member
*
1101 bt_field_class_structure_borrow_member_by_name_const(
1102 const struct bt_field_class
*fc
, const char *name
)
1104 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1105 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1107 return (const void *)
1108 borrow_named_field_class_from_container_field_class_by_name(
1112 struct bt_field_class_structure_member
*
1113 bt_field_class_structure_borrow_member_by_name(
1114 struct bt_field_class
*fc
, const char *name
)
1116 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1117 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STRUCTURE
,
1120 borrow_named_field_class_from_container_field_class_by_name(
1124 const char *bt_field_class_structure_member_get_name(
1125 const struct bt_field_class_structure_member
*member
)
1127 const struct bt_named_field_class
*named_fc
= (const void *) member
;
1129 BT_ASSERT_PRE_DEV_NON_NULL(member
, "Structure field class member");
1130 return named_fc
->name
->str
;
1133 const struct bt_field_class
*
1134 bt_field_class_structure_member_borrow_field_class_const(
1135 const struct bt_field_class_structure_member
*member
)
1137 const struct bt_named_field_class
*named_fc
= (const void *) member
;
1139 BT_ASSERT_PRE_DEV_NON_NULL(member
, "Structure field class member");
1140 return named_fc
->fc
;
1144 void destroy_option_field_class(struct bt_object
*obj
)
1146 struct bt_field_class_option
*fc
= (void *) obj
;
1149 BT_LIB_LOGD("Destroying option field class object: %!+F", fc
);
1150 finalize_field_class((void *) obj
);
1151 BT_LOGD_STR("Putting content field class.");
1152 BT_OBJECT_PUT_REF_AND_RESET(fc
->content_fc
);
1153 BT_LOGD_STR("Putting selector field path.");
1154 BT_OBJECT_PUT_REF_AND_RESET(fc
->selector_field_path
);
1155 BT_LOGD_STR("Putting selector field class.");
1156 BT_OBJECT_PUT_REF_AND_RESET(fc
->selector_fc
);
1160 struct bt_field_class
*bt_field_class_option_create(bt_trace_class
*trace_class
,
1161 bt_field_class
*content_fc
, bt_field_class
*selector_fc
)
1163 struct bt_field_class_option
*opt_fc
= NULL
;
1165 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
1166 BT_ASSERT_PRE_NON_NULL(content_fc
, "Content field class");
1167 BT_LIB_LOGD("Creating option field class: "
1168 "%![content-fc-]+F, %![sel-fc-]+F", content_fc
, selector_fc
);
1169 opt_fc
= g_new0(struct bt_field_class_option
, 1);
1171 BT_LIB_LOGE_APPEND_CAUSE(
1172 "Failed to allocate one option field class.");
1176 if (init_field_class((void *) opt_fc
, BT_FIELD_CLASS_TYPE_OPTION
,
1177 destroy_option_field_class
)) {
1181 opt_fc
->content_fc
= content_fc
;
1182 bt_object_get_no_null_check(opt_fc
->content_fc
);
1183 bt_field_class_freeze(opt_fc
->content_fc
);
1186 BT_ASSERT_PRE_FC_HAS_ID(selector_fc
, BT_FIELD_CLASS_TYPE_BOOL
,
1187 "Selector field class");
1188 opt_fc
->selector_fc
= selector_fc
;
1189 bt_object_get_no_null_check(opt_fc
->selector_fc
);
1190 bt_field_class_freeze(selector_fc
);
1193 BT_LIB_LOGD("Created option field class object: "
1194 "%![opt-fc-]+F, %![sel-fc-]+F", opt_fc
, selector_fc
);
1198 BT_OBJECT_PUT_REF_AND_RESET(opt_fc
);
1201 return (void *) opt_fc
;
1204 const struct bt_field_class
*bt_field_class_option_borrow_field_class_const(
1205 const struct bt_field_class
*fc
)
1207 struct bt_field_class_option
*opt_fc
= (void *) fc
;
1209 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1210 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_OPTION
,
1212 return opt_fc
->content_fc
;
1215 const struct bt_field_path
*
1216 bt_field_class_option_borrow_selector_field_path_const(
1217 const struct bt_field_class
*fc
)
1219 struct bt_field_class_option
*opt_fc
= (void *) fc
;
1221 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1222 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_OPTION
,
1224 return opt_fc
->selector_field_path
;
1228 void finalize_variant_field_class(struct bt_field_class_variant
*var_fc
)
1231 BT_LIB_LOGD("Finalizing variant field class object: %!+F", var_fc
);
1232 finalize_field_class((void *) var_fc
);
1233 finalize_named_field_classes_container((void *) var_fc
);
1237 void destroy_variant_field_class(struct bt_object
*obj
)
1239 struct bt_field_class_variant
*fc
= (void *) obj
;
1242 finalize_variant_field_class(fc
);
1247 void destroy_variant_with_selector_field_class(struct bt_object
*obj
)
1249 struct bt_field_class_variant_with_selector
*fc
= (void *) obj
;
1252 finalize_variant_field_class(&fc
->common
);
1253 BT_LOGD_STR("Putting selector field path.");
1254 BT_OBJECT_PUT_REF_AND_RESET(fc
->selector_field_path
);
1255 BT_LOGD_STR("Putting selector field class.");
1256 BT_OBJECT_PUT_REF_AND_RESET(fc
->selector_fc
);
1260 struct bt_field_class
*bt_field_class_variant_create(
1261 bt_trace_class
*trace_class
, bt_field_class
*selector_fc
)
1264 struct bt_field_class_variant
*var_fc
= NULL
;
1265 struct bt_field_class_variant_with_selector
*var_with_sel_fc
= NULL
;
1266 enum bt_field_class_type fc_type
;
1268 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
1271 BT_ASSERT_PRE_FC_IS_INT(selector_fc
, "Selector field class");
1274 BT_LIB_LOGD("Creating default variant field class: %![sel-fc-]+F",
1278 var_with_sel_fc
= g_new0(
1279 struct bt_field_class_variant_with_selector
, 1);
1280 if (!var_with_sel_fc
) {
1281 BT_LIB_LOGE_APPEND_CAUSE(
1282 "Failed to allocate one variant field class with selector.");
1286 if (selector_fc
->type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
1287 selector_fc
->type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
1288 fc_type
= BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
;
1290 fc_type
= BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
;
1293 ret
= init_named_field_classes_container(
1294 (void *) var_with_sel_fc
, fc_type
,
1295 destroy_variant_with_selector_field_class
,
1296 destroy_variant_with_selector_option
);
1298 /* init_named_field_classes_container() logs errors */
1302 var_with_sel_fc
->selector_fc
= selector_fc
;
1303 bt_object_get_no_null_check(var_with_sel_fc
->selector_fc
);
1304 bt_field_class_freeze(selector_fc
);
1305 var_fc
= (void *) var_with_sel_fc
;
1307 var_fc
= g_new0(struct bt_field_class_variant
, 1);
1309 BT_LIB_LOGE_APPEND_CAUSE(
1310 "Failed to allocate one variant field class without selector.");
1314 ret
= init_named_field_classes_container((void *) var_fc
,
1315 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
,
1316 destroy_variant_field_class
, destroy_named_field_class
);
1318 /* init_named_field_classes_container() logs errors */
1324 BT_LIB_LOGD("Created default variant field class with selector object: "
1325 "%![var-fc-]+F, %![sel-fc-]+F", var_fc
, selector_fc
);
1329 BT_OBJECT_PUT_REF_AND_RESET(var_fc
);
1332 return (void *) var_fc
;
1335 enum bt_field_class_variant_without_selector_append_option_status
1336 bt_field_class_variant_without_selector_append_option(struct bt_field_class
*fc
,
1337 const char *name
, struct bt_field_class
*option_fc
)
1339 enum bt_field_class_variant_without_selector_append_option_status status
;
1340 struct bt_named_field_class
*named_fc
= NULL
;
1342 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1343 BT_ASSERT_PRE_NON_NULL(name
, "Name");
1344 BT_ASSERT_PRE_NON_NULL(option_fc
, "Option field class");
1345 BT_ASSERT_PRE_FC_HAS_ID(fc
,
1346 BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
, "Field class");
1347 named_fc
= create_named_field_class(name
, option_fc
);
1349 /* create_named_field_class() logs errors */
1350 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1354 status
= append_named_field_class_to_container_field_class((void *) fc
,
1356 if (status
== BT_FUNC_STATUS_OK
) {
1357 /* Moved to the container */
1363 destroy_named_field_class(named_fc
);
1370 int ranges_overlap(GPtrArray
*var_fc_opts
, const struct bt_integer_range_set
*range_set
,
1371 bool is_signed
, bool *has_overlap
)
1373 int status
= BT_FUNC_STATUS_OK
;
1374 struct bt_integer_range_set
*full_range_set
;
1377 *has_overlap
= false;
1380 * Build a single range set with all the ranges and test for
1384 full_range_set
= (void *) bt_integer_range_set_signed_create();
1386 full_range_set
= (void *) bt_integer_range_set_unsigned_create();
1389 if (!full_range_set
) {
1390 BT_LOGE_STR("Failed to create a range set.");
1391 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1395 /* Add existing option ranges */
1396 for (i
= 0; i
< var_fc_opts
->len
; i
++) {
1397 struct bt_field_class_variant_with_selector_option
*opt
=
1398 var_fc_opts
->pdata
[i
];
1401 for (j
= 0; j
< opt
->range_set
->ranges
->len
; j
++) {
1402 struct bt_integer_range
*range
= BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1406 status
= bt_integer_range_set_signed_add_range(
1407 (void *) full_range_set
, range
->lower
.i
,
1410 status
= bt_integer_range_set_unsigned_add_range(
1411 (void *) full_range_set
, range
->lower
.u
,
1421 /* Add new ranges */
1422 for (i
= 0; i
< range_set
->ranges
->len
; i
++) {
1423 struct bt_integer_range
*range
= BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(
1427 status
= bt_integer_range_set_signed_add_range(
1428 (void *) full_range_set
, range
->lower
.i
,
1431 status
= bt_integer_range_set_unsigned_add_range(
1432 (void *) full_range_set
, range
->lower
.u
,
1441 /* Check overlaps */
1443 *has_overlap
= bt_integer_range_set_signed_has_overlaps(full_range_set
);
1445 *has_overlap
= bt_integer_range_set_unsigned_has_overlaps(
1450 bt_object_put_ref(full_range_set
);
1455 int append_option_to_variant_with_selector_field_class(
1456 struct bt_field_class
*fc
, const char *name
,
1457 struct bt_field_class
*option_fc
,
1458 const struct bt_integer_range_set
*range_set
,
1459 enum bt_field_class_type expected_type
)
1462 struct bt_field_class_variant_with_selector
*var_fc
= (void *) fc
;
1463 struct bt_field_class_variant_with_selector_option
*opt
= NULL
;
1466 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
1467 BT_ASSERT_PRE_NON_NULL(name
, "Name");
1468 BT_ASSERT_PRE_NON_NULL(option_fc
, "Option field class");
1469 BT_ASSERT_PRE_NON_NULL(range_set
, "Integer range set");
1470 BT_ASSERT_PRE_FC_HAS_ID(fc
, expected_type
, "Field class");
1471 BT_ASSERT_PRE(range_set
->ranges
->len
> 0,
1472 "Integer range set is empty: %!+R", range_set
);
1473 status
= ranges_overlap(var_fc
->common
.common
.named_fcs
, range_set
,
1474 expected_type
== BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
,
1477 /* ranges_overlap() logs errors */
1481 BT_ASSERT_PRE(!has_overlap
,
1482 "Integer range set's ranges and existing ranges have an overlap: "
1484 opt
= create_variant_with_selector_option(name
, option_fc
, range_set
);
1486 /* create_variant_with_selector_option() logs errors */
1487 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
1491 status
= append_named_field_class_to_container_field_class((void *) fc
,
1493 if (status
== BT_FUNC_STATUS_OK
) {
1494 /* Moved to the container */
1500 destroy_variant_with_selector_option(opt
);
1506 enum bt_field_class_variant_with_selector_append_option_status
1507 bt_field_class_variant_with_selector_unsigned_append_option(
1508 struct bt_field_class
*fc
, const char *name
,
1509 struct bt_field_class
*option_fc
,
1510 const struct bt_integer_range_set_unsigned
*range_set
)
1512 return append_option_to_variant_with_selector_field_class(fc
,
1513 name
, option_fc
, (const void *) range_set
,
1514 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
);
1517 enum bt_field_class_variant_with_selector_append_option_status
1518 bt_field_class_variant_with_selector_signed_append_option(
1519 struct bt_field_class
*fc
, const char *name
,
1520 struct bt_field_class
*option_fc
,
1521 const struct bt_integer_range_set_signed
*range_set
)
1523 return append_option_to_variant_with_selector_field_class(fc
,
1524 name
, option_fc
, (const void *) range_set
,
1525 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
);
1528 uint64_t bt_field_class_variant_get_option_count(const struct bt_field_class
*fc
)
1530 const struct bt_field_class_variant
*var_fc
= (const void *) fc
;
1532 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1533 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc
, "Field class");
1534 return (uint64_t) var_fc
->common
.named_fcs
->len
;
1537 const struct bt_field_class_variant_option
*
1538 bt_field_class_variant_borrow_option_by_name_const(
1539 const struct bt_field_class
*fc
, const char *name
)
1541 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1542 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc
, "Field class");
1543 return (const void *)
1544 borrow_named_field_class_from_container_field_class_by_name(
1548 const struct bt_field_class_variant_option
*
1549 bt_field_class_variant_borrow_option_by_index_const(
1550 const struct bt_field_class
*fc
, uint64_t index
)
1552 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1553 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc
, "Field class");
1554 return (const void *)
1555 borrow_named_field_class_from_container_field_class_at_index(
1556 (void *) fc
, index
);
1559 struct bt_field_class_variant_option
*
1560 bt_field_class_variant_borrow_option_by_name(
1561 struct bt_field_class
*fc
, const char *name
)
1563 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1564 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc
, "Field class");
1566 borrow_named_field_class_from_container_field_class_by_name(
1570 struct bt_field_class_variant_option
*
1571 bt_field_class_variant_borrow_option_by_index(
1572 struct bt_field_class
*fc
, uint64_t index
)
1574 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1575 BT_ASSERT_PRE_DEV_FC_IS_VARIANT(fc
, "Field class");
1577 borrow_named_field_class_from_container_field_class_at_index(
1578 (void *) fc
, index
);
1581 const struct bt_field_class_variant_with_selector_unsigned_option
*
1582 bt_field_class_variant_with_selector_unsigned_borrow_option_by_name_const(
1583 const struct bt_field_class
*fc
, const char *name
)
1585 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1586 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
,
1587 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
,
1589 return (const void *)
1590 borrow_named_field_class_from_container_field_class_by_name(
1594 const struct bt_field_class_variant_with_selector_unsigned_option
*
1595 bt_field_class_variant_with_selector_unsigned_borrow_option_by_index_const(
1596 const struct bt_field_class
*fc
, uint64_t index
)
1598 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1599 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
,
1600 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
,
1602 return (const void *)
1603 borrow_named_field_class_from_container_field_class_at_index(
1604 (void *) fc
, index
);
1607 const struct bt_field_class_variant_with_selector_signed_option
*
1608 bt_field_class_variant_with_selector_signed_borrow_option_by_name_const(
1609 const struct bt_field_class
*fc
, const char *name
)
1611 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1612 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
,
1613 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
,
1615 return (const void *)
1616 borrow_named_field_class_from_container_field_class_by_name(
1620 const struct bt_field_class_variant_with_selector_signed_option
*
1621 bt_field_class_variant_with_selector_signed_borrow_option_by_index_const(
1622 const struct bt_field_class
*fc
, uint64_t index
)
1624 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1625 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
,
1626 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
,
1628 return (const void *)
1629 borrow_named_field_class_from_container_field_class_at_index(
1630 (void *) fc
, index
);
1633 const char *bt_field_class_variant_option_get_name(
1634 const struct bt_field_class_variant_option
*option
)
1636 const struct bt_named_field_class
*named_fc
= (const void *) option
;
1638 BT_ASSERT_PRE_DEV_NON_NULL(option
, "Variant field class option");
1639 return named_fc
->name
->str
;
1642 const struct bt_field_class
*
1643 bt_field_class_variant_option_borrow_field_class_const(
1644 const struct bt_field_class_variant_option
*option
)
1646 const struct bt_named_field_class
*named_fc
= (const void *) option
;
1648 BT_ASSERT_PRE_DEV_NON_NULL(option
, "Variant field class option");
1649 return named_fc
->fc
;
1652 const struct bt_integer_range_set_unsigned
*
1653 bt_field_class_variant_with_selector_unsigned_option_borrow_ranges_const(
1654 const struct bt_field_class_variant_with_selector_unsigned_option
*option
)
1656 const struct bt_field_class_variant_with_selector_option
*opt
=
1657 (const void *) option
;
1659 BT_ASSERT_PRE_DEV_NON_NULL(option
, "Variant field class option");
1660 return (const void *) opt
->range_set
;
1663 const struct bt_integer_range_set_signed
*
1664 bt_field_class_variant_with_selector_signed_option_borrow_ranges_const(
1665 const struct bt_field_class_variant_with_selector_signed_option
*option
)
1667 const struct bt_field_class_variant_with_selector_option
*opt
=
1668 (const void *) option
;
1670 BT_ASSERT_PRE_DEV_NON_NULL(option
, "Variant field class option");
1671 return (const void *) opt
->range_set
;
1674 const struct bt_field_path
*
1675 bt_field_class_variant_with_selector_borrow_selector_field_path_const(
1676 const struct bt_field_class
*fc
)
1678 const struct bt_field_class_variant_with_selector
*var_fc
=
1681 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1682 BT_ASSERT_PRE_DEV_FC_IS_VARIANT_WITH_SEL(fc
, "Field class");
1683 return var_fc
->selector_field_path
;
1687 int init_array_field_class(struct bt_field_class_array
*fc
,
1688 enum bt_field_class_type type
, bt_object_release_func release_func
,
1689 struct bt_field_class
*element_fc
)
1693 BT_ASSERT(element_fc
);
1694 ret
= init_field_class((void *) fc
, type
, release_func
);
1699 fc
->element_fc
= element_fc
;
1700 bt_object_get_no_null_check(fc
->element_fc
);
1701 bt_field_class_freeze(element_fc
);
1708 void finalize_array_field_class(struct bt_field_class_array
*array_fc
)
1710 BT_ASSERT(array_fc
);
1711 BT_LOGD_STR("Putting element field class.");
1712 finalize_field_class((void *) array_fc
);
1713 BT_OBJECT_PUT_REF_AND_RESET(array_fc
->element_fc
);
1717 void destroy_static_array_field_class(struct bt_object
*obj
)
1720 BT_LIB_LOGD("Destroying static array field class object: %!+F", obj
);
1721 finalize_array_field_class((void *) obj
);
1725 struct bt_field_class
*
1726 bt_field_class_array_static_create(bt_trace_class
*trace_class
,
1727 struct bt_field_class
*element_fc
, uint64_t length
)
1729 struct bt_field_class_array_static
*array_fc
= NULL
;
1731 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
1732 BT_ASSERT_PRE_NON_NULL(element_fc
, "Element field class");
1733 BT_LOGD_STR("Creating default static array field class object.");
1734 array_fc
= g_new0(struct bt_field_class_array_static
, 1);
1736 BT_LIB_LOGE_APPEND_CAUSE(
1737 "Failed to allocate one static array field class.");
1741 if (init_array_field_class((void *) array_fc
,
1742 BT_FIELD_CLASS_TYPE_STATIC_ARRAY
,
1743 destroy_static_array_field_class
, element_fc
)) {
1747 array_fc
->length
= length
;
1748 BT_LIB_LOGD("Created static array field class object: %!+F", array_fc
);
1752 BT_OBJECT_PUT_REF_AND_RESET(array_fc
);
1755 return (void *) array_fc
;
1758 const struct bt_field_class
*
1759 bt_field_class_array_borrow_element_field_class_const(
1760 const struct bt_field_class
*fc
)
1762 const struct bt_field_class_array
*array_fc
= (const void *) fc
;
1764 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1765 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc
, "Field class");
1766 return array_fc
->element_fc
;
1769 struct bt_field_class
*
1770 bt_field_class_array_borrow_element_field_class(struct bt_field_class
*fc
)
1772 struct bt_field_class_array
*array_fc
= (void *) fc
;
1774 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1775 BT_ASSERT_PRE_DEV_FC_IS_ARRAY(fc
, "Field class");
1776 return array_fc
->element_fc
;
1779 uint64_t bt_field_class_array_static_get_length(const struct bt_field_class
*fc
)
1781 const struct bt_field_class_array_static
*array_fc
= (const void *) fc
;
1783 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1784 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_STATIC_ARRAY
,
1786 return (uint64_t) array_fc
->length
;
1790 void destroy_dynamic_array_field_class(struct bt_object
*obj
)
1792 struct bt_field_class_array_dynamic
*fc
= (void *) obj
;
1795 BT_LIB_LOGD("Destroying dynamic array field class object: %!+F", fc
);
1796 finalize_array_field_class((void *) fc
);
1797 BT_LOGD_STR("Putting length field path.");
1798 BT_OBJECT_PUT_REF_AND_RESET(fc
->length_field_path
);
1799 BT_LOGD_STR("Putting length field class.");
1800 BT_OBJECT_PUT_REF_AND_RESET(fc
->length_fc
);
1804 struct bt_field_class
*bt_field_class_array_dynamic_create(
1805 struct bt_trace_class
*trace_class
,
1806 struct bt_field_class
*element_fc
,
1807 struct bt_field_class
*length_fc
)
1809 struct bt_field_class_array_dynamic
*array_fc
= NULL
;
1811 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
1812 BT_ASSERT_PRE_NON_NULL(element_fc
, "Element field class");
1813 BT_LOGD_STR("Creating default dynamic array field class object.");
1814 array_fc
= g_new0(struct bt_field_class_array_dynamic
, 1);
1816 BT_LIB_LOGE_APPEND_CAUSE(
1817 "Failed to allocate one dynamic array field class.");
1821 if (init_array_field_class((void *) array_fc
,
1822 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
,
1823 destroy_dynamic_array_field_class
, element_fc
)) {
1828 BT_ASSERT_PRE_FC_IS_UNSIGNED_INT(length_fc
,
1829 "Length field class");
1830 array_fc
->length_fc
= length_fc
;
1831 bt_object_get_no_null_check(array_fc
->length_fc
);
1832 bt_field_class_freeze(length_fc
);
1835 BT_LIB_LOGD("Created dynamic array field class object: %!+F", array_fc
);
1839 BT_OBJECT_PUT_REF_AND_RESET(array_fc
);
1842 return (void *) array_fc
;
1845 const struct bt_field_path
*
1846 bt_field_class_array_dynamic_borrow_length_field_path_const(
1847 const struct bt_field_class
*fc
)
1849 const struct bt_field_class_array_dynamic
*seq_fc
= (const void *) fc
;
1851 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1852 BT_ASSERT_PRE_DEV_FC_HAS_ID(fc
, BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
,
1854 return seq_fc
->length_field_path
;
1858 void destroy_string_field_class(struct bt_object
*obj
)
1861 BT_LIB_LOGD("Destroying string field class object: %!+F", obj
);
1862 finalize_field_class((void *) obj
);
1866 struct bt_field_class
*bt_field_class_string_create(bt_trace_class
*trace_class
)
1868 struct bt_field_class_string
*string_fc
= NULL
;
1870 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
1871 BT_LOGD_STR("Creating default string field class object.");
1872 string_fc
= g_new0(struct bt_field_class_string
, 1);
1874 BT_LIB_LOGE_APPEND_CAUSE(
1875 "Failed to allocate one string field class.");
1879 if (init_field_class((void *) string_fc
, BT_FIELD_CLASS_TYPE_STRING
,
1880 destroy_string_field_class
)) {
1884 BT_LIB_LOGD("Created string field class object: %!+F", string_fc
);
1888 BT_OBJECT_PUT_REF_AND_RESET(string_fc
);
1891 return (void *) string_fc
;
1895 void _bt_field_class_freeze(const struct bt_field_class
*c_fc
)
1897 struct bt_field_class
*fc
= (void *) c_fc
;
1900 * Element/member/option field classes are frozen when added to
1904 bt_value_freeze(fc
->user_attributes
);
1908 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1909 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
1910 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
:
1911 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
:
1913 struct bt_field_class_named_field_class_container
*container_fc
=
1917 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
1918 bt_named_field_class_freeze(
1919 container_fc
->named_fcs
->pdata
[i
]);
1930 void _bt_named_field_class_freeze(const struct bt_named_field_class
*named_fc
)
1932 BT_ASSERT(named_fc
);
1933 BT_ASSERT(named_fc
->fc
->frozen
);
1934 BT_LIB_LOGD("Freezing named field class's user attributes: %!+v",
1935 named_fc
->user_attributes
);
1936 bt_value_freeze(named_fc
->user_attributes
);
1937 ((struct bt_named_field_class
*) named_fc
)->frozen
= true;
1941 void bt_field_class_make_part_of_trace_class(const struct bt_field_class
*c_fc
)
1943 struct bt_field_class
*fc
= (void *) c_fc
;
1946 BT_ASSERT_PRE(!fc
->part_of_trace_class
,
1947 "Field class is already part of a trace: %!+F", fc
);
1948 fc
->part_of_trace_class
= true;
1951 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1952 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR
:
1953 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR
:
1954 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR
:
1956 struct bt_field_class_named_field_class_container
*container_fc
=
1960 for (i
= 0; i
< container_fc
->named_fcs
->len
; i
++) {
1961 struct bt_named_field_class
*named_fc
=
1962 container_fc
->named_fcs
->pdata
[i
];
1964 bt_field_class_make_part_of_trace_class(named_fc
->fc
);
1969 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1970 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1972 struct bt_field_class_array
*array_fc
= (void *) fc
;
1974 bt_field_class_make_part_of_trace_class(array_fc
->element_fc
);
1982 const struct bt_value
*bt_field_class_borrow_user_attributes_const(
1983 const struct bt_field_class
*fc
)
1985 BT_ASSERT_PRE_DEV_NON_NULL(fc
, "Field class");
1986 return fc
->user_attributes
;
1989 struct bt_value
*bt_field_class_borrow_user_attributes(
1990 struct bt_field_class
*field_class
)
1992 return (void *) bt_field_class_borrow_user_attributes_const(
1993 (void *) field_class
);
1997 void bt_field_class_set_user_attributes(
1998 struct bt_field_class
*fc
,
1999 const struct bt_value
*user_attributes
)
2001 BT_ASSERT_PRE_NON_NULL(fc
, "Field class");
2002 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
2003 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
2004 "User attributes object is not a map value object.");
2005 BT_ASSERT_PRE_DEV_FC_HOT(fc
, "Field class");
2006 bt_object_put_no_null_check(fc
->user_attributes
);
2007 fc
->user_attributes
= (void *) user_attributes
;
2008 bt_object_get_no_null_check(fc
->user_attributes
);
2012 const struct bt_value
*bt_named_field_class_borrow_user_attributes_const(
2013 const struct bt_named_field_class
*named_fc
)
2015 return named_fc
->user_attributes
;
2019 void bt_named_field_class_set_user_attributes(
2020 struct bt_named_field_class
*named_fc
,
2021 const struct bt_value
*user_attributes
)
2023 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
2024 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
2025 "User attributes object is not a map value object.");
2026 BT_ASSERT_PRE_DEV_HOT(named_fc
,
2027 "Structure field class member or variant field class option",
2029 bt_object_put_no_null_check(named_fc
->user_attributes
);
2030 named_fc
->user_attributes
= (void *) user_attributes
;
2031 bt_object_get_no_null_check(named_fc
->user_attributes
);
2034 const struct bt_value
*
2035 bt_field_class_structure_member_borrow_user_attributes_const(
2036 const struct bt_field_class_structure_member
*member
)
2038 BT_ASSERT_PRE_NON_NULL(member
, "Structure field class member");
2039 return bt_named_field_class_borrow_user_attributes_const(
2040 (const void *) member
);
2044 bt_field_class_structure_member_borrow_user_attributes(
2045 struct bt_field_class_structure_member
*member
)
2047 BT_ASSERT_PRE_NON_NULL(member
, "Structure field class member");
2048 return (void *) bt_named_field_class_borrow_user_attributes_const(
2052 void bt_field_class_structure_member_set_user_attributes(
2053 struct bt_field_class_structure_member
*member
,
2054 const struct bt_value
*user_attributes
)
2056 BT_ASSERT_PRE_NON_NULL(member
, "Structure field class member");
2057 bt_named_field_class_set_user_attributes((void *) member
,
2061 const struct bt_value
*bt_field_class_variant_option_borrow_user_attributes_const(
2062 const struct bt_field_class_variant_option
*option
)
2064 BT_ASSERT_PRE_NON_NULL(option
, "Variant field class option");
2065 return bt_named_field_class_borrow_user_attributes_const(
2066 (const void *) option
);
2069 struct bt_value
*bt_field_class_variant_option_borrow_user_attributes(
2070 struct bt_field_class_variant_option
*option
)
2072 BT_ASSERT_PRE_NON_NULL(option
, "Variant field class option");
2073 return (void *) bt_named_field_class_borrow_user_attributes_const(
2077 void bt_field_class_variant_option_set_user_attributes(
2078 struct bt_field_class_variant_option
*option
,
2079 const struct bt_value
*user_attributes
)
2081 BT_ASSERT_PRE_NON_NULL(option
, "Variant field class option");
2082 bt_named_field_class_set_user_attributes((void *) option
,
2086 void bt_field_class_get_ref(const struct bt_field_class
*field_class
)
2088 bt_object_get_ref(field_class
);
2091 void bt_field_class_put_ref(const struct bt_field_class
*field_class
)
2093 bt_object_put_ref(field_class
);