4 * Babeltrace CTF IR - Event Fields
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-ir/fields-internal.h>
30 #include <babeltrace/ctf-ir/field-types-internal.h>
31 #include <babeltrace/object-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/compiler.h>
34 #include <babeltrace/compat/fcntl.h>
36 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
39 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
41 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
42 struct bt_ctf_field_type
*);
44 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
45 struct bt_ctf_field_type
*);
47 struct bt_ctf_field
*bt_ctf_field_structure_create(
48 struct bt_ctf_field_type
*);
50 struct bt_ctf_field
*bt_ctf_field_variant_create(
51 struct bt_ctf_field_type
*);
53 struct bt_ctf_field
*bt_ctf_field_array_create(
54 struct bt_ctf_field_type
*);
56 struct bt_ctf_field
*bt_ctf_field_sequence_create(
57 struct bt_ctf_field_type
*);
59 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
62 void bt_ctf_field_destroy(struct bt_object
*);
64 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
66 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
68 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
70 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
72 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
74 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
76 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
78 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
81 int bt_ctf_field_generic_validate(struct bt_ctf_field
*);
83 int bt_ctf_field_structure_validate(struct bt_ctf_field
*);
85 int bt_ctf_field_variant_validate(struct bt_ctf_field
*);
87 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*);
89 int bt_ctf_field_array_validate(struct bt_ctf_field
*);
91 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*);
94 int bt_ctf_field_generic_reset(struct bt_ctf_field
*);
96 int bt_ctf_field_structure_reset(struct bt_ctf_field
*);
98 int bt_ctf_field_variant_reset(struct bt_ctf_field
*);
100 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*);
102 int bt_ctf_field_array_reset(struct bt_ctf_field
*);
104 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*);
106 int bt_ctf_field_string_reset(struct bt_ctf_field
*);
109 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
110 struct ctf_stream_pos
*);
112 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
113 struct ctf_stream_pos
*);
115 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
116 struct ctf_stream_pos
*);
118 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
119 struct ctf_stream_pos
*);
121 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
122 struct ctf_stream_pos
*);
124 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
125 struct ctf_stream_pos
*);
127 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
128 struct ctf_stream_pos
*);
130 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
131 struct ctf_stream_pos
*);
134 int bt_ctf_field_integer_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
136 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
138 int bt_ctf_field_floating_point_copy(struct bt_ctf_field
*,
139 struct bt_ctf_field
*);
141 int bt_ctf_field_structure_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
143 int bt_ctf_field_variant_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
145 int bt_ctf_field_array_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
147 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
149 int bt_ctf_field_string_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
152 void generic_field_freeze(struct bt_ctf_field
*);
154 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*);
156 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*);
158 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*);
160 void bt_ctf_field_array_freeze(struct bt_ctf_field
*);
162 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*);
165 int increase_packet_size(struct ctf_stream_pos
*pos
);
168 struct bt_ctf_field
*(* const field_create_funcs
[])(
169 struct bt_ctf_field_type
*) = {
170 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_create
,
171 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_create
,
173 bt_ctf_field_floating_point_create
,
174 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_create
,
175 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_create
,
176 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_create
,
177 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_create
,
178 [CTF_TYPE_STRING
] = bt_ctf_field_string_create
,
182 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
183 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_destroy
,
184 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_destroy
,
186 bt_ctf_field_floating_point_destroy
,
187 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_destroy
,
188 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_destroy
,
189 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_destroy
,
190 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
191 [CTF_TYPE_STRING
] = bt_ctf_field_string_destroy
,
195 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
196 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_validate
,
197 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_validate
,
198 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_validate
,
199 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_validate
,
200 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_validate
,
201 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_validate
,
202 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_validate
,
203 [CTF_TYPE_STRING
] = bt_ctf_field_generic_validate
,
207 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
208 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_reset
,
209 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_reset
,
210 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_reset
,
211 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_reset
,
212 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_reset
,
213 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_reset
,
214 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_reset
,
215 [CTF_TYPE_STRING
] = bt_ctf_field_string_reset
,
219 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
220 struct ctf_stream_pos
*) = {
221 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_serialize
,
222 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_serialize
,
224 bt_ctf_field_floating_point_serialize
,
225 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_serialize
,
226 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_serialize
,
227 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_serialize
,
228 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
229 [CTF_TYPE_STRING
] = bt_ctf_field_string_serialize
,
233 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
234 struct bt_ctf_field
*) = {
235 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_copy
,
236 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_copy
,
237 [CTF_TYPE_FLOAT
] = bt_ctf_field_floating_point_copy
,
238 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_copy
,
239 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_copy
,
240 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_copy
,
241 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_copy
,
242 [CTF_TYPE_STRING
] = bt_ctf_field_string_copy
,
246 void (* const field_freeze_funcs
[])(struct bt_ctf_field
*) = {
247 [CTF_TYPE_INTEGER
] = generic_field_freeze
,
248 [CTF_TYPE_FLOAT
] = generic_field_freeze
,
249 [CTF_TYPE_STRING
] = generic_field_freeze
,
250 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_freeze
,
251 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_freeze
,
252 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_freeze
,
253 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_freeze
,
254 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_freeze
,
257 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
259 struct bt_ctf_field
*field
= NULL
;
260 enum ctf_type_id type_id
;
267 type_id
= bt_ctf_field_type_get_type_id(type
);
268 if (type_id
<= CTF_TYPE_UNKNOWN
||
269 type_id
>= NR_CTF_TYPES
) {
273 /* Field class MUST be valid */
274 ret
= bt_ctf_field_type_validate(type
);
281 field
= field_create_funcs
[type_id
](type
);
286 /* The type's declaration can't change after this point */
287 bt_ctf_field_type_freeze(type
);
289 bt_object_init(field
, bt_ctf_field_destroy
);
295 void bt_ctf_field_get(struct bt_ctf_field
*field
)
300 void bt_ctf_field_put(struct bt_ctf_field
*field
)
306 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
308 struct bt_ctf_field_type
*ret
= NULL
;
321 enum ctf_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
323 enum ctf_type_id ret
= CTF_TYPE_UNKNOWN
;
329 ret
= bt_ctf_field_type_get_type_id(field
->type
);
335 int bt_ctf_field_is_integer(struct bt_ctf_field
*field
)
337 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_INTEGER
;
341 int bt_ctf_field_is_floating_point(struct bt_ctf_field
*field
)
343 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_FLOAT
;
347 int bt_ctf_field_is_enumeration(struct bt_ctf_field
*field
)
349 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_ENUM
;
353 int bt_ctf_field_is_string(struct bt_ctf_field
*field
)
355 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_STRING
;
359 int bt_ctf_field_is_structure(struct bt_ctf_field
*field
)
361 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_STRUCT
;
365 int bt_ctf_field_is_array(struct bt_ctf_field
*field
)
367 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_ARRAY
;
371 int bt_ctf_field_is_sequence(struct bt_ctf_field
*field
)
373 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_SEQUENCE
;
377 int bt_ctf_field_is_variant(struct bt_ctf_field
*field
)
379 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_VARIANT
;
383 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
384 struct bt_ctf_field
*field
)
386 struct bt_ctf_field
*ret
= NULL
;
387 struct bt_ctf_field_sequence
*sequence
;
393 if (bt_ctf_field_type_get_type_id(field
->type
) !=
398 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
399 ret
= sequence
->length
;
405 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
406 struct bt_ctf_field
*length_field
)
409 struct bt_ctf_field_type_integer
*length_type
;
410 struct bt_ctf_field_integer
*length
;
411 struct bt_ctf_field_sequence
*sequence
;
412 uint64_t sequence_length
;
414 if (!field
|| !length_field
|| field
->frozen
) {
418 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
424 length_type
= container_of(length_field
->type
,
425 struct bt_ctf_field_type_integer
, parent
);
426 /* The length field must be unsigned */
427 if (length_type
->declaration
.signedness
) {
432 length
= container_of(length_field
, struct bt_ctf_field_integer
,
434 sequence_length
= length
->definition
.value
._unsigned
;
435 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
436 if (sequence
->elements
) {
437 g_ptr_array_free(sequence
->elements
, TRUE
);
438 bt_put(sequence
->length
);
441 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
442 if (!sequence
->elements
) {
447 g_ptr_array_set_free_func(sequence
->elements
,
448 (GDestroyNotify
) bt_put
);
449 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
450 bt_get(length_field
);
451 sequence
->length
= length_field
;
456 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
457 struct bt_ctf_field
*field
, const char *name
)
459 struct bt_ctf_field
*new_field
= NULL
;
461 struct bt_ctf_field_structure
*structure
;
462 struct bt_ctf_field_type
*field_type
= NULL
;
465 if (!field
|| !name
||
466 bt_ctf_field_type_get_type_id(field
->type
) !=
471 field_quark
= g_quark_from_string(name
);
472 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
474 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
476 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
477 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
481 if (structure
->fields
->pdata
[index
]) {
482 new_field
= structure
->fields
->pdata
[index
];
486 /* We don't want to modify this field if it's frozen */
491 new_field
= bt_ctf_field_create(field_type
);
496 structure
->fields
->pdata
[index
] = new_field
;
507 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
508 struct bt_ctf_field
*field
, int index
)
511 const char *field_name
;
512 struct bt_ctf_field_structure
*structure
;
513 struct bt_ctf_field_type
*structure_type
;
514 struct bt_ctf_field_type
*field_type
= NULL
;
515 struct bt_ctf_field
*ret_field
= NULL
;
518 bt_ctf_field_type_get_type_id(field
->type
) !=
523 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
524 if (index
>= structure
->fields
->len
) {
528 ret_field
= structure
->fields
->pdata
[index
];
533 /* We don't want to modify this field if it's frozen */
538 /* Field has not been instanciated yet, create it */
539 structure_type
= bt_ctf_field_get_type(field
);
540 if (!structure_type
) {
544 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
545 &field_name
, &field_type
, index
);
546 bt_put(structure_type
);
551 ret_field
= bt_ctf_field_create(field_type
);
556 structure
->fields
->pdata
[index
] = ret_field
;
565 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
566 const char *name
, struct bt_ctf_field
*value
)
570 struct bt_ctf_field_structure
*structure
;
571 struct bt_ctf_field_type
*expected_field_type
= NULL
;
574 if (!field
|| !name
|| !value
|| field
->frozen
||
575 bt_ctf_field_type_get_type_id(field
->type
) !=
581 field_quark
= g_quark_from_string(name
);
582 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
583 expected_field_type
=
584 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
587 if (bt_ctf_field_type_compare(expected_field_type
, value
->type
)) {
592 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
593 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
597 if (structure
->fields
->pdata
[index
]) {
598 bt_put(structure
->fields
->pdata
[index
]);
601 structure
->fields
->pdata
[index
] = value
;
604 if (expected_field_type
) {
605 bt_put(expected_field_type
);
610 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
613 struct bt_ctf_field
*new_field
= NULL
;
614 struct bt_ctf_field_type
*field_type
= NULL
;
615 struct bt_ctf_field_array
*array
;
617 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
622 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
623 if (index
>= array
->elements
->len
) {
627 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
628 if (array
->elements
->pdata
[(size_t)index
]) {
629 new_field
= array
->elements
->pdata
[(size_t)index
];
633 /* We don't want to modify this field if it's frozen */
638 new_field
= bt_ctf_field_create(field_type
);
639 array
->elements
->pdata
[(size_t)index
] = new_field
;
650 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
653 struct bt_ctf_field
*new_field
= NULL
;
654 struct bt_ctf_field_type
*field_type
= NULL
;
655 struct bt_ctf_field_sequence
*sequence
;
657 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
662 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
663 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
667 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
668 if (sequence
->elements
->pdata
[(size_t) index
]) {
669 new_field
= sequence
->elements
->pdata
[(size_t) index
];
673 /* We don't want to modify this field if it's frozen */
678 new_field
= bt_ctf_field_create(field_type
);
679 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
690 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
691 struct bt_ctf_field
*tag_field
)
693 struct bt_ctf_field
*new_field
= NULL
;
694 struct bt_ctf_field_variant
*variant
;
695 struct bt_ctf_field_type_variant
*variant_type
;
696 struct bt_ctf_field_type
*field_type
;
697 struct bt_ctf_field
*tag_enum
= NULL
;
698 struct bt_ctf_field_integer
*tag_enum_integer
;
699 int64_t tag_enum_value
;
701 if (!field
|| !tag_field
||
702 bt_ctf_field_type_get_type_id(field
->type
) !=
704 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
709 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
710 variant_type
= container_of(field
->type
,
711 struct bt_ctf_field_type_variant
, parent
);
712 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
717 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
720 if (bt_ctf_field_validate(tag_field
) < 0) {
724 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
727 * If the variant currently has a tag and a payload, and if the
728 * requested tag value is the same as the current one, return
729 * the current payload instead of creating a fresh one.
731 if (variant
->tag
&& variant
->payload
) {
732 struct bt_ctf_field
*cur_tag_container
= NULL
;
733 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
734 int64_t cur_tag_value
;
737 bt_ctf_field_enumeration_get_container(variant
->tag
);
738 assert(cur_tag_container
);
739 cur_tag_enum_integer
= container_of(cur_tag_container
,
740 struct bt_ctf_field_integer
, parent
);
741 bt_put(cur_tag_container
);
742 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
744 if (cur_tag_value
== tag_enum_value
) {
745 new_field
= variant
->payload
;
751 /* We don't want to modify this field if it's frozen */
756 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
757 variant_type
, tag_enum_value
);
762 new_field
= bt_ctf_field_create(field_type
);
767 bt_put(variant
->tag
);
768 bt_put(variant
->payload
);
771 variant
->tag
= tag_field
;
772 variant
->payload
= new_field
;
779 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
780 struct bt_ctf_field
*variant_field
)
782 struct bt_ctf_field
*current_field
= NULL
;
783 struct bt_ctf_field_variant
*variant
;
785 if (!variant_field
||
786 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
791 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
794 if (variant
->payload
) {
795 current_field
= variant
->payload
;
796 bt_get(current_field
);
801 return current_field
;
804 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
805 struct bt_ctf_field
*field
)
807 struct bt_ctf_field
*container
= NULL
;
808 struct bt_ctf_field_enumeration
*enumeration
;
810 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
815 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
817 if (!enumeration
->payload
) {
818 /* We don't want to modify this field if it's frozen */
823 struct bt_ctf_field_type_enumeration
*enumeration_type
=
824 container_of(field
->type
,
825 struct bt_ctf_field_type_enumeration
, parent
);
826 enumeration
->payload
=
827 bt_ctf_field_create(enumeration_type
->container
);
830 container
= enumeration
->payload
;
837 const char *bt_ctf_field_enumeration_get_mapping_name(
838 struct bt_ctf_field
*field
)
841 const char *name
= NULL
;
842 struct bt_ctf_field
*container
= NULL
;
843 struct bt_ctf_field_type
*container_type
= NULL
;
844 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
845 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
847 container
= bt_ctf_field_enumeration_get_container(field
);
852 container_type
= bt_ctf_field_get_type(container
);
853 if (!container_type
) {
854 goto error_put_container
;
857 integer_type
= container_of(container_type
,
858 struct bt_ctf_field_type_integer
, parent
);
859 enumeration_type
= container_of(field
->type
,
860 struct bt_ctf_field_type_enumeration
, parent
);
862 if (!integer_type
->declaration
.signedness
) {
864 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
867 goto error_put_container_type
;
870 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
871 enumeration_type
, value
);
874 ret
= bt_ctf_field_signed_integer_get_value(container
,
877 goto error_put_container_type
;
880 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
881 enumeration_type
, value
);
884 error_put_container_type
:
885 bt_put(container_type
);
893 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
897 struct bt_ctf_field_integer
*integer
;
898 struct bt_ctf_field_type_integer
*integer_type
;
900 if (!field
|| !value
|| !field
->payload_set
||
901 bt_ctf_field_type_get_type_id(field
->type
) !=
907 integer_type
= container_of(field
->type
,
908 struct bt_ctf_field_type_integer
, parent
);
909 if (!integer_type
->declaration
.signedness
) {
914 integer
= container_of(field
,
915 struct bt_ctf_field_integer
, parent
);
916 *value
= integer
->definition
.value
._signed
;
921 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
925 struct bt_ctf_field_integer
*integer
;
926 struct bt_ctf_field_type_integer
*integer_type
;
928 int64_t min_value
, max_value
;
930 if (!field
|| field
->frozen
||
931 bt_ctf_field_type_get_type_id(field
->type
) !=
937 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
938 integer_type
= container_of(field
->type
,
939 struct bt_ctf_field_type_integer
, parent
);
940 if (!integer_type
->declaration
.signedness
) {
945 size
= integer_type
->declaration
.len
;
946 min_value
= -(1ULL << (size
- 1));
947 max_value
= (1ULL << (size
- 1)) - 1;
948 if (value
< min_value
|| value
> max_value
) {
953 integer
->definition
.value
._signed
= value
;
954 integer
->parent
.payload_set
= 1;
960 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
964 struct bt_ctf_field_integer
*integer
;
965 struct bt_ctf_field_type_integer
*integer_type
;
967 if (!field
|| !value
|| !field
->payload_set
||
968 bt_ctf_field_type_get_type_id(field
->type
) !=
974 integer_type
= container_of(field
->type
,
975 struct bt_ctf_field_type_integer
, parent
);
976 if (integer_type
->declaration
.signedness
) {
981 integer
= container_of(field
,
982 struct bt_ctf_field_integer
, parent
);
983 *value
= integer
->definition
.value
._unsigned
;
988 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
992 struct bt_ctf_field_integer
*integer
;
993 struct bt_ctf_field_type_integer
*integer_type
;
997 if (!field
|| field
->frozen
||
998 bt_ctf_field_type_get_type_id(field
->type
) !=
1004 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1005 integer_type
= container_of(field
->type
,
1006 struct bt_ctf_field_type_integer
, parent
);
1007 if (integer_type
->declaration
.signedness
) {
1012 size
= integer_type
->declaration
.len
;
1013 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
1014 if (value
> max_value
) {
1019 integer
->definition
.value
._unsigned
= value
;
1020 integer
->parent
.payload_set
= 1;
1026 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
1030 struct bt_ctf_field_floating_point
*floating_point
;
1032 if (!field
|| !value
|| !field
->payload_set
||
1033 bt_ctf_field_type_get_type_id(field
->type
) !=
1039 floating_point
= container_of(field
,
1040 struct bt_ctf_field_floating_point
, parent
);
1041 *value
= floating_point
->definition
.value
;
1046 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
1050 struct bt_ctf_field_floating_point
*floating_point
;
1052 if (!field
|| field
->frozen
||
1053 bt_ctf_field_type_get_type_id(field
->type
) !=
1058 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1060 floating_point
->definition
.value
= value
;
1061 floating_point
->parent
.payload_set
= 1;
1067 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
1069 const char *ret
= NULL
;
1070 struct bt_ctf_field_string
*string
;
1072 if (!field
|| !field
->payload_set
||
1073 bt_ctf_field_type_get_type_id(field
->type
) !=
1078 string
= container_of(field
,
1079 struct bt_ctf_field_string
, parent
);
1080 ret
= string
->payload
->str
;
1085 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1089 struct bt_ctf_field_string
*string
;
1091 if (!field
|| !value
|| field
->frozen
||
1092 bt_ctf_field_type_get_type_id(field
->type
) !=
1098 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1099 if (string
->payload
) {
1100 g_string_assign(string
->payload
, value
);
1102 string
->payload
= g_string_new(value
);
1105 string
->parent
.payload_set
= 1;
1111 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1115 struct bt_ctf_field_string
*string_field
;
1117 if (!field
|| !value
|| field
->frozen
||
1118 bt_ctf_field_type_get_type_id(field
->type
) !=
1124 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1126 if (string_field
->payload
) {
1127 g_string_append(string_field
->payload
, value
);
1129 string_field
->payload
= g_string_new(value
);
1132 string_field
->parent
.payload_set
= 1;
1139 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1140 const char *value
, unsigned int length
)
1144 unsigned int effective_length
= length
;
1145 struct bt_ctf_field_string
*string_field
;
1147 if (!field
|| !value
|| field
->frozen
||
1148 bt_ctf_field_type_get_type_id(field
->type
) !=
1154 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1156 /* make sure no null bytes are appended */
1157 for (i
= 0; i
< length
; ++i
) {
1158 if (value
[i
] == '\0') {
1159 effective_length
= i
;
1164 if (string_field
->payload
) {
1165 g_string_append_len(string_field
->payload
, value
,
1168 string_field
->payload
= g_string_new_len(value
,
1172 string_field
->parent
.payload_set
= 1;
1179 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1182 enum ctf_type_id type_id
;
1189 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1190 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1195 ret
= field_validate_funcs
[type_id
](field
);
1201 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1204 enum ctf_type_id type_id
;
1211 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1212 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1217 ret
= field_reset_funcs
[type_id
](field
);
1223 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1224 struct ctf_stream_pos
*pos
)
1227 enum ctf_type_id type_id
;
1229 if (!field
|| !pos
) {
1234 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1235 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1240 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1246 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1249 struct bt_ctf_field
*copy
= NULL
;
1250 enum ctf_type_id type_id
;
1256 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1257 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1261 copy
= bt_ctf_field_create(field
->type
);
1266 copy
->payload_set
= field
->payload_set
;
1267 ret
= field_copy_funcs
[type_id
](field
, copy
);
1277 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1279 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1280 struct bt_ctf_field_type_integer
, parent
);
1281 struct bt_ctf_field_integer
*integer
= g_new0(
1282 struct bt_ctf_field_integer
, 1);
1285 integer
->definition
.declaration
= &integer_type
->declaration
;
1288 return integer
? &integer
->parent
: NULL
;
1292 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1293 struct bt_ctf_field_type
*type
)
1295 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1296 struct bt_ctf_field_enumeration
, 1);
1298 return enumeration
? &enumeration
->parent
: NULL
;
1302 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1303 struct bt_ctf_field_type
*type
)
1305 struct bt_ctf_field_floating_point
*floating_point
;
1306 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1308 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1309 if (!floating_point
) {
1313 floating_point_type
= container_of(type
,
1314 struct bt_ctf_field_type_floating_point
, parent
);
1315 floating_point
->definition
.declaration
= container_of(
1316 type
->declaration
, struct declaration_float
, p
);
1319 floating_point
->definition
.sign
= &floating_point
->sign
;
1320 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1321 floating_point
->definition
.sign
->p
.declaration
=
1322 &floating_point_type
->sign
.p
;
1324 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1325 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1326 floating_point
->definition
.mantissa
->p
.declaration
=
1327 &floating_point_type
->mantissa
.p
;
1329 floating_point
->definition
.exp
= &floating_point
->exp
;
1330 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1331 floating_point
->definition
.exp
->p
.declaration
=
1332 &floating_point_type
->exp
.p
;
1335 return floating_point
? &floating_point
->parent
: NULL
;
1339 struct bt_ctf_field
*bt_ctf_field_structure_create(
1340 struct bt_ctf_field_type
*type
)
1342 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1343 struct bt_ctf_field_type_structure
, parent
);
1344 struct bt_ctf_field_structure
*structure
= g_new0(
1345 struct bt_ctf_field_structure
, 1);
1346 struct bt_ctf_field
*field
= NULL
;
1352 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1353 structure
->fields
= g_ptr_array_new_with_free_func(
1354 (GDestroyNotify
)bt_ctf_field_put
);
1355 g_ptr_array_set_size(structure
->fields
,
1356 g_hash_table_size(structure
->field_name_to_index
));
1357 field
= &structure
->parent
;
1363 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1365 struct bt_ctf_field_variant
*variant
= g_new0(
1366 struct bt_ctf_field_variant
, 1);
1367 return variant
? &variant
->parent
: NULL
;
1371 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1373 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1374 struct bt_ctf_field_type_array
*array_type
;
1375 unsigned int array_length
;
1377 if (!array
|| !type
) {
1381 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1382 array_length
= array_type
->length
;
1383 array
->elements
= g_ptr_array_sized_new(array_length
);
1384 if (!array
->elements
) {
1388 g_ptr_array_set_free_func(array
->elements
,
1389 (GDestroyNotify
)bt_ctf_field_put
);
1390 g_ptr_array_set_size(array
->elements
, array_length
);
1391 return &array
->parent
;
1398 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1399 struct bt_ctf_field_type
*type
)
1401 struct bt_ctf_field_sequence
*sequence
= g_new0(
1402 struct bt_ctf_field_sequence
, 1);
1403 return sequence
? &sequence
->parent
: NULL
;
1407 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1409 struct bt_ctf_field_string
*string
= g_new0(
1410 struct bt_ctf_field_string
, 1);
1411 return string
? &string
->parent
: NULL
;
1415 void bt_ctf_field_destroy(struct bt_object
*obj
)
1417 struct bt_ctf_field
*field
;
1418 struct bt_ctf_field_type
*type
;
1419 enum ctf_type_id type_id
;
1421 field
= container_of(obj
, struct bt_ctf_field
, base
);
1423 type_id
= bt_ctf_field_type_get_type_id(type
);
1424 if (type_id
<= CTF_TYPE_UNKNOWN
||
1425 type_id
>= NR_CTF_TYPES
) {
1429 field_destroy_funcs
[type_id
](field
);
1434 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1436 struct bt_ctf_field_integer
*integer
;
1442 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1447 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1449 struct bt_ctf_field_enumeration
*enumeration
;
1455 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1457 bt_put(enumeration
->payload
);
1458 g_free(enumeration
);
1462 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1464 struct bt_ctf_field_floating_point
*floating_point
;
1470 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1472 g_free(floating_point
);
1476 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1478 struct bt_ctf_field_structure
*structure
;
1484 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1485 g_ptr_array_free(structure
->fields
, TRUE
);
1490 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1492 struct bt_ctf_field_variant
*variant
;
1498 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1499 bt_put(variant
->tag
);
1500 bt_put(variant
->payload
);
1505 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1507 struct bt_ctf_field_array
*array
;
1513 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1514 g_ptr_array_free(array
->elements
, TRUE
);
1519 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1521 struct bt_ctf_field_sequence
*sequence
;
1527 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1528 if (sequence
->elements
) {
1529 g_ptr_array_free(sequence
->elements
, TRUE
);
1531 bt_put(sequence
->length
);
1536 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1538 struct bt_ctf_field_string
*string
;
1543 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1544 if (string
->payload
) {
1545 g_string_free(string
->payload
, TRUE
);
1551 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1553 return (field
&& field
->payload_set
) ? 0 : -1;
1557 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1560 struct bt_ctf_field_enumeration
*enumeration
;
1567 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1569 if (!enumeration
->payload
) {
1574 ret
= bt_ctf_field_validate(enumeration
->payload
);
1580 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1584 struct bt_ctf_field_structure
*structure
;
1591 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1592 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1593 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1603 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1606 struct bt_ctf_field_variant
*variant
;
1613 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1614 ret
= bt_ctf_field_validate(variant
->payload
);
1620 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1624 struct bt_ctf_field_array
*array
;
1631 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1632 for (i
= 0; i
< array
->elements
->len
; i
++) {
1633 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1643 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1647 struct bt_ctf_field_sequence
*sequence
;
1654 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1655 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1656 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1666 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1675 field
->payload_set
= 0;
1681 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1684 struct bt_ctf_field_enumeration
*enumeration
;
1691 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1693 if (!enumeration
->payload
) {
1697 ret
= bt_ctf_field_reset(enumeration
->payload
);
1703 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1707 struct bt_ctf_field_structure
*structure
;
1714 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1715 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1716 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1720 * Structure members are lazily initialized; skip if
1721 * this member has not been allocated yet.
1726 ret
= bt_ctf_field_reset(member
);
1736 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1739 struct bt_ctf_field_variant
*variant
;
1746 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1747 if (variant
->payload
) {
1748 ret
= bt_ctf_field_reset(variant
->payload
);
1755 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1759 struct bt_ctf_field_array
*array
;
1766 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1767 for (i
= 0; i
< array
->elements
->len
; i
++) {
1768 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1772 * Array elements are lazily initialized; skip if
1773 * this member has not been allocated yet.
1778 ret
= bt_ctf_field_reset(member
);
1788 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1792 struct bt_ctf_field_sequence
*sequence
;
1799 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1800 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1801 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1805 * Sequence elements are lazily initialized; skip if
1806 * this member has not been allocated yet.
1811 ret
= bt_ctf_field_reset(member
);
1821 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1824 struct bt_ctf_field_string
*string
;
1831 ret
= bt_ctf_field_generic_reset(field
);
1836 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1837 if (string
->payload
) {
1838 g_string_truncate(string
->payload
, 0);
1845 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1846 struct ctf_stream_pos
*pos
)
1849 struct bt_ctf_field_integer
*integer
= container_of(field
,
1850 struct bt_ctf_field_integer
, parent
);
1853 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1854 if (ret
== -EFAULT
) {
1856 * The field is too large to fit in the current packet's
1857 * remaining space. Bump the packet size and retry.
1859 ret
= increase_packet_size(pos
);
1870 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1871 struct ctf_stream_pos
*pos
)
1873 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1874 field
, struct bt_ctf_field_enumeration
, parent
);
1876 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1880 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1881 struct ctf_stream_pos
*pos
)
1884 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1885 struct bt_ctf_field_floating_point
, parent
);
1888 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1889 if (ret
== -EFAULT
) {
1891 * The field is too large to fit in the current packet's
1892 * remaining space. Bump the packet size and retry.
1894 ret
= increase_packet_size(pos
);
1905 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1906 struct ctf_stream_pos
*pos
)
1910 struct bt_ctf_field_structure
*structure
= container_of(
1911 field
, struct bt_ctf_field_structure
, parent
);
1913 while (!ctf_pos_access_ok(pos
,
1914 offset_align(pos
->offset
,
1915 field
->type
->declaration
->alignment
))) {
1916 ret
= increase_packet_size(pos
);
1922 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1927 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1928 struct bt_ctf_field
*field
= g_ptr_array_index(
1929 structure
->fields
, i
);
1931 ret
= bt_ctf_field_serialize(field
, pos
);
1941 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1942 struct ctf_stream_pos
*pos
)
1944 struct bt_ctf_field_variant
*variant
= container_of(
1945 field
, struct bt_ctf_field_variant
, parent
);
1947 return bt_ctf_field_serialize(variant
->payload
, pos
);
1951 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1952 struct ctf_stream_pos
*pos
)
1956 struct bt_ctf_field_array
*array
= container_of(
1957 field
, struct bt_ctf_field_array
, parent
);
1959 for (i
= 0; i
< array
->elements
->len
; i
++) {
1960 ret
= bt_ctf_field_serialize(
1961 g_ptr_array_index(array
->elements
, i
), pos
);
1971 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1972 struct ctf_stream_pos
*pos
)
1976 struct bt_ctf_field_sequence
*sequence
= container_of(
1977 field
, struct bt_ctf_field_sequence
, parent
);
1979 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1980 ret
= bt_ctf_field_serialize(
1981 g_ptr_array_index(sequence
->elements
, i
), pos
);
1991 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1992 struct ctf_stream_pos
*pos
)
1996 struct bt_ctf_field_string
*string
= container_of(field
,
1997 struct bt_ctf_field_string
, parent
);
1998 struct bt_ctf_field_type
*character_type
=
1999 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
2000 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
2002 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
2003 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
2004 (uint64_t) string
->payload
->str
[i
]);
2009 ret
= bt_ctf_field_integer_serialize(character
, pos
);
2016 bt_put(character_type
);
2021 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
2022 struct bt_ctf_field
*dst
)
2024 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
2026 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
2027 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
2029 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
2030 sizeof(struct definition_integer
));
2035 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
2036 struct bt_ctf_field
*dst
)
2039 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
2041 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
2042 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
2044 if (enum_src
->payload
) {
2045 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
2046 if (!enum_dst
->payload
) {
2056 int bt_ctf_field_floating_point_copy(
2057 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
2059 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
2061 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
2063 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
2066 memcpy(&float_dst
->definition
, &float_src
->definition
,
2067 sizeof(struct definition_float
));
2068 memcpy(&float_dst
->sign
, &float_src
->sign
,
2069 sizeof(struct definition_integer
));
2070 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
2071 sizeof(struct definition_integer
));
2072 memcpy(&float_dst
->exp
, &float_src
->exp
,
2073 sizeof(struct definition_integer
));
2078 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2079 struct bt_ctf_field
*dst
)
2082 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2084 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2085 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2087 /* This field_name_to_index HT is owned by the structure field type */
2088 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2089 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2091 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2092 struct bt_ctf_field
*field
=
2093 g_ptr_array_index(struct_src
->fields
, i
);
2094 struct bt_ctf_field
*field_copy
= NULL
;
2097 field_copy
= bt_ctf_field_copy(field
);
2105 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2112 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2113 struct bt_ctf_field
*dst
)
2116 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2118 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2119 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2121 if (variant_src
->tag
) {
2122 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2123 if (!variant_dst
->tag
) {
2128 if (variant_src
->payload
) {
2129 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2130 if (!variant_dst
->payload
) {
2140 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2141 struct bt_ctf_field
*dst
)
2144 struct bt_ctf_field_array
*array_src
, *array_dst
;
2146 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2147 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2149 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2150 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2151 struct bt_ctf_field
*field
=
2152 g_ptr_array_index(array_src
->elements
, i
);
2153 struct bt_ctf_field
*field_copy
= NULL
;
2156 field_copy
= bt_ctf_field_copy(field
);
2164 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2171 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2172 struct bt_ctf_field
*dst
)
2175 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2176 struct bt_ctf_field
*src_length
;
2177 struct bt_ctf_field
*dst_length
;
2179 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2180 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2182 src_length
= bt_ctf_field_sequence_get_length(src
);
2185 /* no length set yet: keep destination sequence empty */
2189 /* copy source length */
2190 dst_length
= bt_ctf_field_copy(src_length
);
2198 /* this will initialize the destination sequence's internal array */
2199 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2206 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2208 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2209 struct bt_ctf_field
*field
=
2210 g_ptr_array_index(sequence_src
->elements
, i
);
2211 struct bt_ctf_field
*field_copy
= NULL
;
2214 field_copy
= bt_ctf_field_copy(field
);
2222 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2229 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2230 struct bt_ctf_field
*dst
)
2233 struct bt_ctf_field_string
*string_src
, *string_dst
;
2235 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2236 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2238 if (string_src
->payload
) {
2239 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2240 if (!string_dst
->payload
) {
2250 int increase_packet_size(struct ctf_stream_pos
*pos
)
2255 ret
= munmap_align(pos
->base_mma
);
2260 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2262 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2263 pos
->packet_size
/ CHAR_BIT
);
2264 } while (ret
== EINTR
);
2271 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2272 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2273 if (pos
->base_mma
== MAP_FAILED
) {
2281 void generic_field_freeze(struct bt_ctf_field
*field
)
2287 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*field
)
2289 struct bt_ctf_field_enumeration
*enum_field
=
2290 container_of(field
, struct bt_ctf_field_enumeration
, parent
);
2292 bt_ctf_field_freeze(enum_field
->payload
);
2293 generic_field_freeze(field
);
2297 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*field
)
2300 struct bt_ctf_field_structure
*structure_field
=
2301 container_of(field
, struct bt_ctf_field_structure
, parent
);
2303 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
2304 struct bt_ctf_field
*field
=
2305 g_ptr_array_index(structure_field
->fields
, i
);
2307 bt_ctf_field_freeze(field
);
2310 generic_field_freeze(field
);
2314 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*field
)
2316 struct bt_ctf_field_variant
*variant_field
=
2317 container_of(field
, struct bt_ctf_field_variant
, parent
);
2319 bt_ctf_field_freeze(variant_field
->tag
);
2320 bt_ctf_field_freeze(variant_field
->payload
);
2321 generic_field_freeze(field
);
2325 void bt_ctf_field_array_freeze(struct bt_ctf_field
*field
)
2328 struct bt_ctf_field_array
*array_field
=
2329 container_of(field
, struct bt_ctf_field_array
, parent
);
2331 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
2332 struct bt_ctf_field
*field
=
2333 g_ptr_array_index(array_field
->elements
, i
);
2335 bt_ctf_field_freeze(field
);
2338 generic_field_freeze(field
);
2342 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*field
)
2345 struct bt_ctf_field_sequence
*sequence_field
=
2346 container_of(field
, struct bt_ctf_field_sequence
, parent
);
2348 bt_ctf_field_freeze(sequence_field
->length
);
2350 for (i
= 0; i
< sequence_field
->elements
->len
; i
++) {
2351 struct bt_ctf_field
*field
=
2352 g_ptr_array_index(sequence_field
->elements
, i
);
2354 bt_ctf_field_freeze(field
);
2357 generic_field_freeze(field
);
2361 void bt_ctf_field_freeze(struct bt_ctf_field
*field
)
2363 enum ctf_type_id type_id
;
2369 type_id
= bt_ctf_field_get_type_id(field
);
2370 if (type_id
<= CTF_TYPE_UNKNOWN
||
2371 type_id
>= NR_CTF_TYPES
) {
2375 field_freeze_funcs
[type_id
](field
);