1 #ifndef BABELTRACE_CTF_IR_FIELDS_INTERNAL_H
2 #define BABELTRACE_CTF_IR_FIELDS_INTERNAL_H
5 * Babeltrace - CTF IR: Event Fields internal
7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #include <babeltrace/assert-pre-internal.h>
31 #include <babeltrace/common-internal.h>
32 #include <babeltrace/ctf-ir/field-types-internal.h>
33 #include <babeltrace/ctf-ir/utils-internal.h>
34 #include <babeltrace/object-internal.h>
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/types.h>
43 #define BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(_field, _type_id, _name) \
44 BT_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
45 _name " has the wrong type ID: expected-type-id=%s, " \
47 bt_common_field_type_id_string((int) (_type_id)), (_field))
49 #define BT_ASSERT_PRE_FIELD_COMMON_IS_SET(_field, _name) \
50 BT_ASSERT_PRE(bt_field_common_is_set_recursive(_field), \
51 _name " is not set: %!+_f", (_field))
53 #define BT_ASSERT_PRE_FIELD_COMMON_HOT(_field, _name) \
54 BT_ASSERT_PRE_HOT((_field), (_name), ": %!+_f", (_field))
57 struct bt_field_common
;
59 typedef void (*bt_field_common_method_set_is_frozen
)(struct bt_field_common
*,
61 typedef int (*bt_field_common_method_validate
)(struct bt_field_common
*);
62 typedef struct bt_field_common
*(*bt_field_common_method_copy
)(
63 struct bt_field_common
*);
64 typedef bt_bool (*bt_field_common_method_is_set
)(struct bt_field_common
*);
65 typedef void (*bt_field_common_method_reset
)(struct bt_field_common
*);
67 struct bt_field_common_methods
{
68 bt_field_common_method_set_is_frozen set_is_frozen
;
69 bt_field_common_method_validate validate
;
70 bt_field_common_method_copy copy
;
71 bt_field_common_method_is_set is_set
;
72 bt_field_common_method_reset reset
;
75 struct bt_field_common
{
76 struct bt_object base
;
77 struct bt_field_type_common
*type
;
78 struct bt_field_common_methods
*methods
;
83 * Specialized data for either CTF IR or CTF writer APIs.
84 * See comment in `field-types-internal.h` for more details.
95 struct bt_field_common_integer
{
96 struct bt_field_common common
;
103 struct bt_field_common_floating_point
{
104 struct bt_field_common common
;
108 struct bt_field_common_structure
{
109 struct bt_field_common common
;
111 /* Array of `struct bt_field_common *`, owned by this */
115 struct bt_field_common_variant
{
116 struct bt_field_common common
;
123 /* Weak: belongs to `choices` below */
124 struct bt_field_common
*current_field
;
126 /* Array of `struct bt_field_common *`, owned by this */
130 struct bt_field_common_array
{
131 struct bt_field_common common
;
133 /* Array of `struct bt_field_common *`, owned by this */
137 struct bt_field_common_sequence
{
138 struct bt_field_common common
;
141 * This is the true sequence field's length: its value can be
142 * less than `elements->len` below because we never shrink the
143 * array of elements to avoid reallocation.
147 /* Array of `struct bt_field_common *`, owned by this */
151 struct bt_field_common_string
{
152 struct bt_field_common common
;
157 struct bt_field_enumeration
{
158 struct bt_field_common_integer common
;
162 struct bt_field_common
*bt_field_common_copy(struct bt_field_common
*field
);
165 int bt_field_common_structure_initialize(struct bt_field_common
*field
,
166 struct bt_field_type_common
*type
,
167 bool is_shared
, bt_object_release_func release_func
,
168 struct bt_field_common_methods
*methods
,
169 bt_field_common_create_func field_create_func
,
170 GDestroyNotify field_release_func
);
173 int bt_field_common_array_initialize(struct bt_field_common
*field
,
174 struct bt_field_type_common
*type
,
175 bool is_shared
, bt_object_release_func release_func
,
176 struct bt_field_common_methods
*methods
,
177 bt_field_common_create_func field_create_func
,
178 GDestroyNotify field_destroy_func
);
181 int bt_field_common_sequence_initialize(struct bt_field_common
*field
,
182 struct bt_field_type_common
*type
,
183 bool is_shared
, bt_object_release_func release_func
,
184 struct bt_field_common_methods
*methods
,
185 GDestroyNotify field_destroy_func
);
188 int bt_field_common_variant_initialize(struct bt_field_common
*field
,
189 struct bt_field_type_common
*type
,
190 bool is_shared
, bt_object_release_func release_func
,
191 struct bt_field_common_methods
*methods
,
192 bt_field_common_create_func field_create_func
,
193 GDestroyNotify field_release_func
);
196 int bt_field_common_string_initialize(struct bt_field_common
*field
,
197 struct bt_field_type_common
*type
,
198 bool is_shared
, bt_object_release_func release_func
,
199 struct bt_field_common_methods
*methods
);
202 int bt_field_common_generic_validate(struct bt_field_common
*field
);
205 int bt_field_common_structure_validate_recursive(struct bt_field_common
*field
);
208 int bt_field_common_variant_validate_recursive(struct bt_field_common
*field
);
211 int bt_field_common_array_validate_recursive(struct bt_field_common
*field
);
214 int bt_field_common_sequence_validate_recursive(struct bt_field_common
*field
);
217 void bt_field_common_generic_reset(struct bt_field_common
*field
);
220 void bt_field_common_structure_reset_recursive(struct bt_field_common
*field
);
223 void bt_field_common_variant_reset_recursive(struct bt_field_common
*field
);
226 void bt_field_common_array_reset_recursive(struct bt_field_common
*field
);
229 void bt_field_common_sequence_reset_recursive(struct bt_field_common
*field
);
232 void bt_field_common_generic_set_is_frozen(struct bt_field_common
*field
,
236 void bt_field_common_structure_set_is_frozen_recursive(
237 struct bt_field_common
*field
, bool is_frozen
);
240 void bt_field_common_variant_set_is_frozen_recursive(
241 struct bt_field_common
*field
, bool is_frozen
);
244 void bt_field_common_array_set_is_frozen_recursive(
245 struct bt_field_common
*field
, bool is_frozen
);
248 void bt_field_common_sequence_set_is_frozen_recursive(
249 struct bt_field_common
*field
, bool is_frozen
);
252 void _bt_field_common_set_is_frozen_recursive(struct bt_field_common
*field
,
256 bt_bool
bt_field_common_generic_is_set(struct bt_field_common
*field
);
259 bt_bool
bt_field_common_structure_is_set_recursive(
260 struct bt_field_common
*field
);
263 bt_bool
bt_field_common_variant_is_set_recursive(struct bt_field_common
*field
);
266 bt_bool
bt_field_common_array_is_set_recursive(struct bt_field_common
*field
);
269 bt_bool
bt_field_common_sequence_is_set_recursive(struct bt_field_common
*field
);
272 # define bt_field_common_validate_recursive _bt_field_common_validate_recursive
273 # define bt_field_common_set_is_frozen_recursive _bt_field_common_set_is_frozen_recursive
274 # define bt_field_common_is_set_recursive _bt_field_common_is_set_recursive
275 # define bt_field_common_reset_recursive _bt_field_common_reset_recursive
276 # define bt_field_common_set _bt_field_common_set
277 # define bt_field_validate_recursive _bt_field_validate_recursive
278 # define bt_field_set_is_frozen_recursive _bt_field_set_is_frozen_recursive
279 # define bt_field_is_set_recursive _bt_field_is_set_recursive
280 # define bt_field_reset_recursive _bt_field_reset_recursive
281 # define bt_field_set _bt_field_set
283 # define bt_field_common_validate_recursive(_field) (-1)
284 # define bt_field_common_set_is_frozen_recursive(_field, _is_frozen)
285 # define bt_field_common_is_set_recursive(_field) (BT_FALSE)
286 # define bt_field_common_reset_recursive(_field)
287 # define bt_field_common_set(_field, _val)
288 # define bt_field_validate_recursive(_field) (-1)
289 # define bt_field_set_is_frozen_recursive(_field, _is_frozen)
290 # define bt_field_is_set_recursive(_field) (BT_FALSE)
291 # define bt_field_reset_recursive(_field)
292 # define bt_field_set(_field, _val)
296 static inline bool field_type_common_has_known_id(
297 struct bt_field_type_common
*ft
)
299 return (int) ft
->id
> BT_FIELD_TYPE_ID_UNKNOWN
||
300 (int) ft
->id
< BT_FIELD_TYPE_ID_NR
;
304 int _bt_field_common_validate_recursive(struct bt_field_common
*field
)
309 BT_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
314 BT_ASSERT(field_type_common_has_known_id(field
->type
));
316 if (field
->methods
->validate
) {
317 ret
= field
->methods
->validate(field
);
325 void _bt_field_common_reset_recursive(struct bt_field_common
*field
)
328 BT_ASSERT(field
->methods
->reset
);
329 field
->methods
->reset(field
);
333 void _bt_field_common_set(struct bt_field_common
*field
, bool value
)
336 field
->payload_set
= value
;
340 bt_bool
_bt_field_common_is_set_recursive(struct bt_field_common
*field
)
342 bt_bool is_set
= BT_FALSE
;
348 BT_ASSERT(field_type_common_has_known_id(field
->type
));
349 BT_ASSERT(field
->methods
->is_set
);
350 is_set
= field
->methods
->is_set(field
);
357 void bt_field_common_initialize(struct bt_field_common
*field
,
358 struct bt_field_type_common
*ft
, bool is_shared
,
359 bt_object_release_func release_func
,
360 struct bt_field_common_methods
*methods
)
364 bt_object_init(&field
->base
, is_shared
, release_func
);
365 field
->methods
= methods
;
366 field
->type
= bt_get(ft
);
370 struct bt_field_type_common
*bt_field_common_borrow_type(
371 struct bt_field_common
*field
)
373 struct bt_field_type_common
*ret
= NULL
;
375 BT_ASSERT_PRE_NON_NULL(field
, "Field");
381 int64_t bt_field_common_sequence_get_length(struct bt_field_common
*field
)
383 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
385 BT_ASSERT_PRE_NON_NULL(field
, "Sequence field");
386 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
, BT_FIELD_TYPE_ID_SEQUENCE
,
388 return (int64_t) sequence
->length
;
392 int bt_field_common_sequence_set_length(struct bt_field_common
*field
,
393 uint64_t length
, bt_field_common_create_func field_create_func
)
396 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
398 BT_ASSERT_PRE_NON_NULL(field
, "Sequence field");
399 BT_ASSERT_PRE(((int64_t) length
) >= 0,
400 "Invalid sequence length (too large): length=%" PRId64
,
402 BT_ASSERT_PRE_FIELD_COMMON_HOT(field
, "Sequence field");
404 if (unlikely(length
> sequence
->elements
->len
)) {
406 struct bt_field_type_common_sequence
*sequence_ft
;
407 uint64_t cur_len
= sequence
->elements
->len
;
410 g_ptr_array_set_size(sequence
->elements
, length
);
411 sequence_ft
= BT_FROM_COMMON(sequence
->common
.type
);
413 for (i
= cur_len
; i
< sequence
->elements
->len
; i
++) {
414 struct bt_field_common
*elem_field
=
415 field_create_func(sequence_ft
->element_ft
);
422 BT_ASSERT(!sequence
->elements
->pdata
[i
]);
423 sequence
->elements
->pdata
[i
] = elem_field
;
427 sequence
->length
= length
;
434 struct bt_field_common
*bt_field_common_structure_borrow_field_by_name(
435 struct bt_field_common
*field
, const char *name
)
437 struct bt_field_common
*ret
= NULL
;
439 struct bt_field_type_common_structure
*structure_ft
;
440 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
442 GHashTable
*field_name_to_index
;
444 BT_ASSERT_PRE_NON_NULL(field
, "Structure field");
445 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
446 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
,
447 BT_FIELD_TYPE_ID_STRUCT
, "Field");
448 structure_ft
= BT_FROM_COMMON(field
->type
);
449 field_name_to_index
= structure_ft
->field_name_to_index
;
450 field_quark
= g_quark_from_string(name
);
451 if (!g_hash_table_lookup_extended(field_name_to_index
,
452 GUINT_TO_POINTER(field_quark
),
453 NULL
, (gpointer
*) &index
)) {
454 BT_LOGV("Invalid parameter: no such field in structure field's type: "
455 "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"",
456 field
, field
->type
, name
);
460 ret
= structure
->fields
->pdata
[index
];
468 struct bt_field_common
*bt_field_common_structure_borrow_field_by_index(
469 struct bt_field_common
*field
, uint64_t index
)
471 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
473 BT_ASSERT_PRE_NON_NULL(field
, "Structure field");
474 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
,
475 BT_FIELD_TYPE_ID_STRUCT
, "Field");
476 BT_ASSERT_PRE(index
< structure
->fields
->len
,
477 "Index is out of bound: %![struct-field-]+_f, "
478 "index=%" PRIu64
", count=%u", field
, index
,
479 structure
->fields
->len
);
480 return structure
->fields
->pdata
[index
];
484 struct bt_field_common
*bt_field_common_array_borrow_field(
485 struct bt_field_common
*field
, uint64_t index
)
487 struct bt_field_common_array
*array
= BT_FROM_COMMON(field
);
489 BT_ASSERT_PRE_NON_NULL(field
, "Array field");
490 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
, BT_FIELD_TYPE_ID_ARRAY
,
492 BT_ASSERT_PRE(index
< array
->elements
->len
,
493 "Index is out of bound: %![array-field-]+_f, "
494 "index=%" PRIu64
", count=%u", field
,
495 index
, array
->elements
->len
);
496 return array
->elements
->pdata
[(size_t) index
];
500 struct bt_field_common
*bt_field_common_sequence_borrow_field(
501 struct bt_field_common
*field
, uint64_t index
)
503 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
505 BT_ASSERT_PRE_NON_NULL(field
, "Sequence field");
506 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
, BT_FIELD_TYPE_ID_SEQUENCE
,
508 BT_ASSERT_PRE(index
< sequence
->length
,
509 "Index is out of bound: %![seq-field-]+_f, "
510 "index=%" PRIu64
", count=%u", field
, index
,
511 sequence
->elements
->len
);
512 return sequence
->elements
->pdata
[(size_t) index
];
516 int bt_field_variant_common_set_tag(struct bt_field_common
*variant_field
,
517 uint64_t tag_uval
, bool is_signed
)
520 int64_t choice_index
;
521 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(variant_field
);
523 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
524 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
525 BT_FIELD_TYPE_ID_VARIANT
, "Field");
527 /* Find matching index in variant field's type */
528 choice_index
= bt_field_type_common_variant_find_choice_index(
529 variant_field
->type
, tag_uval
, is_signed
);
530 if (choice_index
< 0) {
535 /* Select corresponding field */
536 BT_ASSERT(choice_index
< variant
->fields
->len
);
537 variant
->current_field
= variant
->fields
->pdata
[choice_index
];
538 variant
->tag_value
.u
= tag_uval
;
545 struct bt_field_common
*bt_field_common_variant_borrow_current_field(
546 struct bt_field_common
*variant_field
)
548 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(variant_field
);
550 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
551 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
552 BT_FIELD_TYPE_ID_VARIANT
, "Field");
553 BT_ASSERT_PRE(variant
->current_field
,
554 "Variant field has no current field: %!+_f", variant_field
);
555 return variant
->current_field
;
559 int bt_field_common_variant_get_tag_signed(struct bt_field_common
*variant_field
,
562 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(variant_field
);
564 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
565 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
566 BT_FIELD_TYPE_ID_VARIANT
, "Field");
567 BT_ASSERT_PRE(variant
->current_field
,
568 "Variant field has no current field: %!+_f", variant_field
);
569 *tag
= variant
->tag_value
.i
;
574 int bt_field_common_variant_get_tag_unsigned(struct bt_field_common
*variant_field
,
577 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(variant_field
);
579 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
580 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
581 BT_FIELD_TYPE_ID_VARIANT
, "Field");
582 BT_ASSERT_PRE(variant
->current_field
,
583 "Variant field has no current field: %!+_f", variant_field
);
584 *tag
= variant
->tag_value
.u
;
589 struct bt_field_type_enumeration_mapping_iterator
*
590 bt_field_common_enumeration_get_mappings(struct bt_field_common
*field
,
591 bt_field_common_create_func field_create_func
,
594 struct bt_field_type_common_enumeration
*enum_type
= NULL
;
595 struct bt_field_type_common_integer
*integer_type
= NULL
;
596 struct bt_field_type_enumeration_mapping_iterator
*iter
= NULL
;
599 BT_ASSERT(field
->type
->id
== BT_FIELD_TYPE_ID_ENUM
);
600 BT_ASSERT(field
->payload_set
);
601 enum_type
= BT_FROM_COMMON(field
->type
);
602 integer_type
= enum_type
->container_ft
;
604 if (!integer_type
->is_signed
) {
605 iter
= bt_field_type_common_enumeration_unsigned_find_mappings_by_value(
608 iter
= bt_field_type_common_enumeration_signed_find_mappings_by_value(
609 field
->type
, (int64_t) uval
);
616 int bt_field_common_floating_point_get_value(struct bt_field_common
*field
,
619 struct bt_field_common_floating_point
*floating_point
=
620 BT_FROM_COMMON(field
);
622 BT_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
623 BT_ASSERT_PRE_NON_NULL(value
, "Value");
624 BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field
, "Floating point number field");
625 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
,
626 BT_FIELD_TYPE_ID_FLOAT
, "Field");
627 *value
= floating_point
->payload
;
632 int bt_field_common_floating_point_set_value(struct bt_field_common
*field
,
635 struct bt_field_common_floating_point
*floating_point
=
636 BT_FROM_COMMON(field
);
638 BT_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
639 BT_ASSERT_PRE_FIELD_COMMON_HOT(field
, "Floating point number field");
640 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
,
641 BT_FIELD_TYPE_ID_FLOAT
, "Field");
642 floating_point
->payload
= value
;
643 bt_field_common_set(field
, true);
648 const char *bt_field_common_string_get_value(struct bt_field_common
*field
)
650 struct bt_field_common_string
*string
= BT_FROM_COMMON(field
);
652 BT_ASSERT_PRE_NON_NULL(field
, "String field");
653 BT_ASSERT_PRE_FIELD_COMMON_IS_SET(field
, "String field");
654 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
,
655 BT_FIELD_TYPE_ID_STRING
, "Field");
656 return (const char *) string
->buf
->data
;
660 int bt_field_common_string_clear(struct bt_field_common
*field
)
662 struct bt_field_common_string
*string_field
= BT_FROM_COMMON(field
);
664 BT_ASSERT_PRE_NON_NULL(field
, "String field");
665 BT_ASSERT_PRE_FIELD_COMMON_HOT(field
, "String field");
666 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
,
667 BT_FIELD_TYPE_ID_STRING
, "Field");
668 string_field
->size
= 0;
669 bt_field_common_set(field
, true);
674 int bt_field_common_string_append_len(struct bt_field_common
*field
,
675 const char *value
, unsigned int length
)
677 struct bt_field_common_string
*string_field
= BT_FROM_COMMON(field
);
681 BT_ASSERT_PRE_NON_NULL(field
, "String field");
682 BT_ASSERT_PRE_NON_NULL(value
, "Value");
683 BT_ASSERT_PRE_FIELD_COMMON_HOT(field
, "String field");
684 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
,
685 BT_FIELD_TYPE_ID_STRING
, "Field");
687 /* Make sure no null bytes are appended */
688 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
689 "String value to append contains a null character: "
690 "partial-value=\"%.32s\", length=%u", value
, length
);
692 new_size
= string_field
->size
+ length
;
694 if (unlikely(new_size
+ 1 > string_field
->buf
->len
)) {
695 g_array_set_size(string_field
->buf
, new_size
+ 1);
698 data
= string_field
->buf
->data
;
699 memcpy(data
+ string_field
->size
, value
, length
);
700 ((char *) string_field
->buf
->data
)[new_size
] = '\0';
701 string_field
->size
= new_size
;
702 bt_field_common_set(field
, true);
707 int bt_field_common_string_append(struct bt_field_common
*field
,
710 BT_ASSERT_PRE_NON_NULL(value
, "Value");
711 return bt_field_common_string_append_len(field
, value
,
716 int bt_field_common_string_set_value(struct bt_field_common
*field
,
719 BT_ASSERT_PRE_NON_NULL(field
, "String field");
720 BT_ASSERT_PRE_NON_NULL(value
, "Value");
721 BT_ASSERT_PRE_FIELD_COMMON_HOT(field
, "String field");
722 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID(field
,
723 BT_FIELD_TYPE_ID_STRING
, "Field");
724 bt_field_common_string_clear(field
);
725 return bt_field_common_string_append_len(field
,
726 value
, strlen(value
));
730 int _bt_field_validate_recursive(struct bt_field
*field
)
732 return _bt_field_common_validate_recursive((void *) field
);
736 void _bt_field_set_is_frozen_recursive(struct bt_field
*field
, bool is_frozen
)
738 return _bt_field_common_set_is_frozen_recursive((void *) field
,
743 bt_bool
_bt_field_is_set_recursive(struct bt_field
*field
)
745 return _bt_field_common_is_set_recursive((void *) field
);
749 void _bt_field_reset_recursive(struct bt_field
*field
)
751 _bt_field_common_reset_recursive((void *) field
);
755 void _bt_field_set(struct bt_field
*field
, bool value
)
757 _bt_field_common_set((void *) field
, value
);
761 void bt_field_common_finalize(struct bt_field_common
*field
)
764 BT_LOGD_STR("Putting field's type.");
769 void bt_field_common_integer_finalize(struct bt_field_common
*field
)
772 BT_LOGD("Finalizing common integer field object: addr=%p", field
);
773 bt_field_common_finalize(field
);
777 void bt_field_common_floating_point_finalize(struct bt_field_common
*field
)
780 BT_LOGD("Finalizing common floating point number field object: addr=%p", field
);
781 bt_field_common_finalize(field
);
785 void bt_field_common_structure_finalize_recursive(struct bt_field_common
*field
)
787 struct bt_field_common_structure
*structure
= BT_FROM_COMMON(field
);
790 BT_LOGD("Finalizing common structure field object: addr=%p", field
);
791 bt_field_common_finalize(field
);
793 if (structure
->fields
) {
794 g_ptr_array_free(structure
->fields
, TRUE
);
799 void bt_field_common_variant_finalize_recursive(struct bt_field_common
*field
)
801 struct bt_field_common_variant
*variant
= BT_FROM_COMMON(field
);
804 BT_LOGD("Finalizing common variant field object: addr=%p", field
);
805 bt_field_common_finalize(field
);
807 if (variant
->fields
) {
808 g_ptr_array_free(variant
->fields
, TRUE
);
813 void bt_field_common_array_finalize_recursive(struct bt_field_common
*field
)
815 struct bt_field_common_array
*array
= BT_FROM_COMMON(field
);
818 BT_LOGD("Finalizing common array field object: addr=%p", field
);
819 bt_field_common_finalize(field
);
821 if (array
->elements
) {
822 g_ptr_array_free(array
->elements
, TRUE
);
827 void bt_field_common_sequence_finalize_recursive(struct bt_field_common
*field
)
829 struct bt_field_common_sequence
*sequence
= BT_FROM_COMMON(field
);
832 BT_LOGD("Finalizing common sequence field object: addr=%p", field
);
833 bt_field_common_finalize(field
);
835 if (sequence
->elements
) {
836 g_ptr_array_free(sequence
->elements
, TRUE
);
841 void bt_field_common_string_finalize(struct bt_field_common
*field
)
843 struct bt_field_common_string
*string
= BT_FROM_COMMON(field
);
846 BT_LOGD("Finalizing common string field object: addr=%p", field
);
847 bt_field_common_finalize(field
);
850 g_array_free(string
->buf
, TRUE
);
855 static inline bool value_is_in_range_signed(unsigned int size
, int64_t value
)
858 int64_t min_value
, max_value
;
860 min_value
= -(1ULL << (size
- 1));
861 max_value
= (1ULL << (size
- 1)) - 1;
862 if (value
< min_value
|| value
> max_value
) {
863 BT_LOGF("Value is out of bounds: value=%" PRId64
", "
864 "min-value=%" PRId64
", max-value=%" PRId64
,
865 value
, min_value
, max_value
);
873 static inline bool value_is_in_range_unsigned(unsigned int size
, uint64_t value
)
878 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
879 if (value
> max_value
) {
880 BT_LOGF("Value is out of bounds: value=%" PRIu64
", "
881 "max-value=%" PRIu64
,
890 struct bt_field
*bt_field_create_recursive(struct bt_field_type
*type
);
893 void bt_field_destroy_recursive(struct bt_field
*field
);
895 #endif /* BABELTRACE_CTF_IR_FIELDS_INTERNAL_H */