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 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_create
,
171 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_create
,
172 [BT_CTF_TYPE_ID_FLOAT
] =
173 bt_ctf_field_floating_point_create
,
174 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_create
,
175 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_create
,
176 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_create
,
177 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_create
,
178 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_create
,
182 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
183 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_destroy
,
184 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_destroy
,
185 [BT_CTF_TYPE_ID_FLOAT
] =
186 bt_ctf_field_floating_point_destroy
,
187 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_destroy
,
188 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_destroy
,
189 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_destroy
,
190 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
191 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_destroy
,
195 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
196 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_validate
,
197 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_validate
,
198 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_validate
,
199 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_validate
,
200 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_validate
,
201 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_validate
,
202 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_validate
,
203 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_generic_validate
,
207 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
208 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_reset
,
209 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_reset
,
210 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_reset
,
211 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_reset
,
212 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_reset
,
213 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_reset
,
214 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_reset
,
215 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_reset
,
219 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
220 struct ctf_stream_pos
*) = {
221 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_serialize
,
222 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_serialize
,
223 [BT_CTF_TYPE_ID_FLOAT
] =
224 bt_ctf_field_floating_point_serialize
,
225 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_serialize
,
226 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_serialize
,
227 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_serialize
,
228 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
229 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_serialize
,
233 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
234 struct bt_ctf_field
*) = {
235 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_copy
,
236 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_copy
,
237 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_floating_point_copy
,
238 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_copy
,
239 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_copy
,
240 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_copy
,
241 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_copy
,
242 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_copy
,
246 void (* const field_freeze_funcs
[])(struct bt_ctf_field
*) = {
247 [BT_CTF_TYPE_ID_INTEGER
] = generic_field_freeze
,
248 [BT_CTF_TYPE_ID_FLOAT
] = generic_field_freeze
,
249 [BT_CTF_TYPE_ID_STRING
] = generic_field_freeze
,
250 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_freeze
,
251 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_freeze
,
252 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_freeze
,
253 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_freeze
,
254 [BT_CTF_TYPE_ID_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 bt_ctf_type_id type_id
;
267 type_id
= bt_ctf_field_type_get_type_id(type
);
268 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
269 type_id
>= BT_CTF_NR_TYPE_IDS
) {
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
)
305 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
307 struct bt_ctf_field_type
*ret
= NULL
;
319 enum bt_ctf_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
321 enum bt_ctf_type_id ret
= BT_CTF_TYPE_ID_UNKNOWN
;
327 ret
= bt_ctf_field_type_get_type_id(field
->type
);
332 int bt_ctf_field_is_integer(struct bt_ctf_field
*field
)
334 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_INTEGER
;
337 int bt_ctf_field_is_floating_point(struct bt_ctf_field
*field
)
339 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_FLOAT
;
342 int bt_ctf_field_is_enumeration(struct bt_ctf_field
*field
)
344 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_ENUM
;
347 int bt_ctf_field_is_string(struct bt_ctf_field
*field
)
349 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_STRING
;
352 int bt_ctf_field_is_structure(struct bt_ctf_field
*field
)
354 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_STRUCT
;
357 int bt_ctf_field_is_array(struct bt_ctf_field
*field
)
359 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_ARRAY
;
362 int bt_ctf_field_is_sequence(struct bt_ctf_field
*field
)
364 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_SEQUENCE
;
367 int bt_ctf_field_is_variant(struct bt_ctf_field
*field
)
369 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_VARIANT
;
372 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
373 struct bt_ctf_field
*field
)
375 struct bt_ctf_field
*ret
= NULL
;
376 struct bt_ctf_field_sequence
*sequence
;
382 if (bt_ctf_field_type_get_type_id(field
->type
) !=
383 BT_CTF_TYPE_ID_SEQUENCE
) {
387 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
388 ret
= sequence
->length
;
394 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
395 struct bt_ctf_field
*length_field
)
398 struct bt_ctf_field_type_integer
*length_type
;
399 struct bt_ctf_field_integer
*length
;
400 struct bt_ctf_field_sequence
*sequence
;
401 uint64_t sequence_length
;
403 if (!field
|| !length_field
|| field
->frozen
) {
407 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
408 BT_CTF_TYPE_ID_INTEGER
) {
413 length_type
= container_of(length_field
->type
,
414 struct bt_ctf_field_type_integer
, parent
);
415 /* The length field must be unsigned */
416 if (length_type
->declaration
.signedness
) {
421 length
= container_of(length_field
, struct bt_ctf_field_integer
,
423 sequence_length
= length
->definition
.value
._unsigned
;
424 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
425 if (sequence
->elements
) {
426 g_ptr_array_free(sequence
->elements
, TRUE
);
427 bt_put(sequence
->length
);
430 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
431 if (!sequence
->elements
) {
436 g_ptr_array_set_free_func(sequence
->elements
,
437 (GDestroyNotify
) bt_put
);
438 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
439 bt_get(length_field
);
440 sequence
->length
= length_field
;
445 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
446 struct bt_ctf_field
*field
, const char *name
)
448 struct bt_ctf_field
*new_field
= NULL
;
450 struct bt_ctf_field_structure
*structure
;
451 struct bt_ctf_field_type
*field_type
= NULL
;
454 if (!field
|| !name
||
455 bt_ctf_field_type_get_type_id(field
->type
) !=
456 BT_CTF_TYPE_ID_STRUCT
) {
460 field_quark
= g_quark_from_string(name
);
461 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
463 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
465 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
466 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
470 if (structure
->fields
->pdata
[index
]) {
471 new_field
= structure
->fields
->pdata
[index
];
475 /* We don't want to modify this field if it's frozen */
480 new_field
= bt_ctf_field_create(field_type
);
485 structure
->fields
->pdata
[index
] = new_field
;
495 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
496 struct bt_ctf_field
*field
, int index
)
499 const char *field_name
;
500 struct bt_ctf_field_structure
*structure
;
501 struct bt_ctf_field_type
*structure_type
;
502 struct bt_ctf_field_type
*field_type
= NULL
;
503 struct bt_ctf_field
*ret_field
= NULL
;
506 bt_ctf_field_type_get_type_id(field
->type
) !=
507 BT_CTF_TYPE_ID_STRUCT
) {
511 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
512 if (index
>= structure
->fields
->len
) {
516 ret_field
= structure
->fields
->pdata
[index
];
521 /* We don't want to modify this field if it's frozen */
526 /* Field has not been instanciated yet, create it */
527 structure_type
= bt_ctf_field_get_type(field
);
528 if (!structure_type
) {
532 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
533 &field_name
, &field_type
, index
);
534 bt_put(structure_type
);
539 ret_field
= bt_ctf_field_create(field_type
);
544 structure
->fields
->pdata
[index
] = ret_field
;
553 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
554 const char *name
, struct bt_ctf_field
*value
)
558 struct bt_ctf_field_structure
*structure
;
559 struct bt_ctf_field_type
*expected_field_type
= NULL
;
562 if (!field
|| !name
|| !value
|| field
->frozen
||
563 bt_ctf_field_type_get_type_id(field
->type
) !=
564 BT_CTF_TYPE_ID_STRUCT
) {
569 field_quark
= g_quark_from_string(name
);
570 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
571 expected_field_type
=
572 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
575 if (bt_ctf_field_type_compare(expected_field_type
, value
->type
)) {
580 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
581 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
585 if (structure
->fields
->pdata
[index
]) {
586 bt_put(structure
->fields
->pdata
[index
]);
589 structure
->fields
->pdata
[index
] = value
;
592 if (expected_field_type
) {
593 bt_put(expected_field_type
);
598 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
601 struct bt_ctf_field
*new_field
= NULL
;
602 struct bt_ctf_field_type
*field_type
= NULL
;
603 struct bt_ctf_field_array
*array
;
605 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
606 BT_CTF_TYPE_ID_ARRAY
) {
610 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
611 if (index
>= array
->elements
->len
) {
615 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
616 if (array
->elements
->pdata
[(size_t)index
]) {
617 new_field
= array
->elements
->pdata
[(size_t)index
];
621 /* We don't want to modify this field if it's frozen */
626 new_field
= bt_ctf_field_create(field_type
);
627 array
->elements
->pdata
[(size_t)index
] = new_field
;
638 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
641 struct bt_ctf_field
*new_field
= NULL
;
642 struct bt_ctf_field_type
*field_type
= NULL
;
643 struct bt_ctf_field_sequence
*sequence
;
645 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
646 BT_CTF_TYPE_ID_SEQUENCE
) {
650 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
651 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
655 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
656 if (sequence
->elements
->pdata
[(size_t) index
]) {
657 new_field
= sequence
->elements
->pdata
[(size_t) index
];
661 /* We don't want to modify this field if it's frozen */
666 new_field
= bt_ctf_field_create(field_type
);
667 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
678 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
679 struct bt_ctf_field
*tag_field
)
681 struct bt_ctf_field
*new_field
= NULL
;
682 struct bt_ctf_field_variant
*variant
;
683 struct bt_ctf_field_type_variant
*variant_type
;
684 struct bt_ctf_field_type
*field_type
;
685 struct bt_ctf_field
*tag_enum
= NULL
;
686 struct bt_ctf_field_integer
*tag_enum_integer
;
687 int64_t tag_enum_value
;
689 if (!field
|| !tag_field
||
690 bt_ctf_field_type_get_type_id(field
->type
) !=
691 BT_CTF_TYPE_ID_VARIANT
||
692 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
693 BT_CTF_TYPE_ID_ENUM
) {
697 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
698 variant_type
= container_of(field
->type
,
699 struct bt_ctf_field_type_variant
, parent
);
700 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
705 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
708 if (bt_ctf_field_validate(tag_field
) < 0) {
712 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
715 * If the variant currently has a tag and a payload, and if the
716 * requested tag value is the same as the current one, return
717 * the current payload instead of creating a fresh one.
719 if (variant
->tag
&& variant
->payload
) {
720 struct bt_ctf_field
*cur_tag_container
= NULL
;
721 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
722 int64_t cur_tag_value
;
725 bt_ctf_field_enumeration_get_container(variant
->tag
);
726 assert(cur_tag_container
);
727 cur_tag_enum_integer
= container_of(cur_tag_container
,
728 struct bt_ctf_field_integer
, parent
);
729 bt_put(cur_tag_container
);
730 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
732 if (cur_tag_value
== tag_enum_value
) {
733 new_field
= variant
->payload
;
739 /* We don't want to modify this field if it's frozen */
744 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
745 variant_type
, tag_enum_value
);
750 new_field
= bt_ctf_field_create(field_type
);
755 bt_put(variant
->tag
);
756 bt_put(variant
->payload
);
759 variant
->tag
= tag_field
;
760 variant
->payload
= new_field
;
766 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
767 struct bt_ctf_field
*variant_field
)
769 struct bt_ctf_field
*current_field
= NULL
;
770 struct bt_ctf_field_variant
*variant
;
772 if (!variant_field
||
773 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
774 BT_CTF_TYPE_ID_VARIANT
) {
778 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
781 if (variant
->payload
) {
782 current_field
= variant
->payload
;
783 bt_get(current_field
);
788 return current_field
;
791 struct bt_ctf_field
*bt_ctf_field_variant_get_tag(
792 struct bt_ctf_field
*variant_field
)
794 struct bt_ctf_field
*tag
= NULL
;
795 struct bt_ctf_field_variant
*variant
;
797 if (!variant_field
||
798 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
799 BT_CTF_TYPE_ID_VARIANT
) {
803 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
806 tag
= bt_get(variant
->tag
);
812 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
813 struct bt_ctf_field
*field
)
815 struct bt_ctf_field
*container
= NULL
;
816 struct bt_ctf_field_enumeration
*enumeration
;
818 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
819 BT_CTF_TYPE_ID_ENUM
) {
823 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
825 if (!enumeration
->payload
) {
826 /* We don't want to modify this field if it's frozen */
831 struct bt_ctf_field_type_enumeration
*enumeration_type
=
832 container_of(field
->type
,
833 struct bt_ctf_field_type_enumeration
, parent
);
834 enumeration
->payload
=
835 bt_ctf_field_create(enumeration_type
->container
);
838 container
= enumeration
->payload
;
844 const char *bt_ctf_field_enumeration_get_mapping_name(
845 struct bt_ctf_field
*field
)
848 const char *name
= NULL
;
849 struct bt_ctf_field
*container
= NULL
;
850 struct bt_ctf_field_type
*container_type
= NULL
;
851 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
852 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
854 container
= bt_ctf_field_enumeration_get_container(field
);
859 container_type
= bt_ctf_field_get_type(container
);
860 if (!container_type
) {
861 goto error_put_container
;
864 integer_type
= container_of(container_type
,
865 struct bt_ctf_field_type_integer
, parent
);
866 enumeration_type
= container_of(field
->type
,
867 struct bt_ctf_field_type_enumeration
, parent
);
869 if (!integer_type
->declaration
.signedness
) {
871 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
874 goto error_put_container_type
;
877 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
878 enumeration_type
, value
);
881 ret
= bt_ctf_field_signed_integer_get_value(container
,
884 goto error_put_container_type
;
887 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
888 enumeration_type
, value
);
891 error_put_container_type
:
892 bt_put(container_type
);
899 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
903 struct bt_ctf_field_integer
*integer
;
904 struct bt_ctf_field_type_integer
*integer_type
;
906 if (!field
|| !value
|| !field
->payload_set
||
907 bt_ctf_field_type_get_type_id(field
->type
) !=
908 BT_CTF_TYPE_ID_INTEGER
) {
913 integer_type
= container_of(field
->type
,
914 struct bt_ctf_field_type_integer
, parent
);
915 if (!integer_type
->declaration
.signedness
) {
920 integer
= container_of(field
,
921 struct bt_ctf_field_integer
, parent
);
922 *value
= integer
->definition
.value
._signed
;
927 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
931 struct bt_ctf_field_integer
*integer
;
932 struct bt_ctf_field_type_integer
*integer_type
;
934 int64_t min_value
, max_value
;
936 if (!field
|| field
->frozen
||
937 bt_ctf_field_type_get_type_id(field
->type
) !=
938 BT_CTF_TYPE_ID_INTEGER
) {
943 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
944 integer_type
= container_of(field
->type
,
945 struct bt_ctf_field_type_integer
, parent
);
946 if (!integer_type
->declaration
.signedness
) {
951 size
= integer_type
->declaration
.len
;
952 min_value
= -(1ULL << (size
- 1));
953 max_value
= (1ULL << (size
- 1)) - 1;
954 if (value
< min_value
|| value
> max_value
) {
959 integer
->definition
.value
._signed
= value
;
960 integer
->parent
.payload_set
= 1;
965 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
969 struct bt_ctf_field_integer
*integer
;
970 struct bt_ctf_field_type_integer
*integer_type
;
972 if (!field
|| !value
|| !field
->payload_set
||
973 bt_ctf_field_type_get_type_id(field
->type
) !=
974 BT_CTF_TYPE_ID_INTEGER
) {
979 integer_type
= container_of(field
->type
,
980 struct bt_ctf_field_type_integer
, parent
);
981 if (integer_type
->declaration
.signedness
) {
986 integer
= container_of(field
,
987 struct bt_ctf_field_integer
, parent
);
988 *value
= integer
->definition
.value
._unsigned
;
993 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
997 struct bt_ctf_field_integer
*integer
;
998 struct bt_ctf_field_type_integer
*integer_type
;
1002 if (!field
|| field
->frozen
||
1003 bt_ctf_field_type_get_type_id(field
->type
) !=
1004 BT_CTF_TYPE_ID_INTEGER
) {
1009 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1010 integer_type
= container_of(field
->type
,
1011 struct bt_ctf_field_type_integer
, parent
);
1012 if (integer_type
->declaration
.signedness
) {
1017 size
= integer_type
->declaration
.len
;
1018 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
1019 if (value
> max_value
) {
1024 integer
->definition
.value
._unsigned
= value
;
1025 integer
->parent
.payload_set
= 1;
1030 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
1034 struct bt_ctf_field_floating_point
*floating_point
;
1036 if (!field
|| !value
|| !field
->payload_set
||
1037 bt_ctf_field_type_get_type_id(field
->type
) !=
1038 BT_CTF_TYPE_ID_FLOAT
) {
1043 floating_point
= container_of(field
,
1044 struct bt_ctf_field_floating_point
, parent
);
1045 *value
= floating_point
->definition
.value
;
1050 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
1054 struct bt_ctf_field_floating_point
*floating_point
;
1056 if (!field
|| field
->frozen
||
1057 bt_ctf_field_type_get_type_id(field
->type
) !=
1058 BT_CTF_TYPE_ID_FLOAT
) {
1062 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1064 floating_point
->definition
.value
= value
;
1065 floating_point
->parent
.payload_set
= 1;
1070 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
1072 const char *ret
= NULL
;
1073 struct bt_ctf_field_string
*string
;
1075 if (!field
|| !field
->payload_set
||
1076 bt_ctf_field_type_get_type_id(field
->type
) !=
1077 BT_CTF_TYPE_ID_STRING
) {
1081 string
= container_of(field
,
1082 struct bt_ctf_field_string
, parent
);
1083 ret
= string
->payload
->str
;
1088 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1092 struct bt_ctf_field_string
*string
;
1094 if (!field
|| !value
|| field
->frozen
||
1095 bt_ctf_field_type_get_type_id(field
->type
) !=
1096 BT_CTF_TYPE_ID_STRING
) {
1101 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1102 if (string
->payload
) {
1103 g_string_assign(string
->payload
, value
);
1105 string
->payload
= g_string_new(value
);
1108 string
->parent
.payload_set
= 1;
1113 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1117 struct bt_ctf_field_string
*string_field
;
1119 if (!field
|| !value
|| field
->frozen
||
1120 bt_ctf_field_type_get_type_id(field
->type
) !=
1121 BT_CTF_TYPE_ID_STRING
) {
1126 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1128 if (string_field
->payload
) {
1129 g_string_append(string_field
->payload
, value
);
1131 string_field
->payload
= g_string_new(value
);
1134 string_field
->parent
.payload_set
= 1;
1140 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1141 const char *value
, unsigned int length
)
1145 unsigned int effective_length
= length
;
1146 struct bt_ctf_field_string
*string_field
;
1148 if (!field
|| !value
|| field
->frozen
||
1149 bt_ctf_field_type_get_type_id(field
->type
) !=
1150 BT_CTF_TYPE_ID_STRING
) {
1155 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1157 /* make sure no null bytes are appended */
1158 for (i
= 0; i
< length
; ++i
) {
1159 if (value
[i
] == '\0') {
1160 effective_length
= i
;
1165 if (string_field
->payload
) {
1166 g_string_append_len(string_field
->payload
, value
,
1169 string_field
->payload
= g_string_new_len(value
,
1173 string_field
->parent
.payload_set
= 1;
1180 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1183 enum bt_ctf_type_id type_id
;
1190 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1191 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1196 ret
= field_validate_funcs
[type_id
](field
);
1202 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1205 enum bt_ctf_type_id type_id
;
1212 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1213 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1218 ret
= field_reset_funcs
[type_id
](field
);
1224 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1225 struct ctf_stream_pos
*pos
)
1228 enum bt_ctf_type_id type_id
;
1230 if (!field
|| !pos
) {
1235 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1236 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1241 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 bt_ctf_type_id type_id
;
1256 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1257 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
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 bt_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
<= BT_CTF_TYPE_ID_UNKNOWN
||
1425 type_id
>= BT_CTF_NR_TYPE_IDS
) {
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
]);
1596 struct bt_ctf_field_type
*field_type
=
1597 bt_ctf_field_get_type(field
);
1599 (void) bt_ctf_field_type_structure_get_field(field_type
,
1601 fprintf(stderr
, "Field %s failed validation\n",
1602 name
? name
: "NULL");
1612 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1615 struct bt_ctf_field_variant
*variant
;
1622 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1623 ret
= bt_ctf_field_validate(variant
->payload
);
1629 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1633 struct bt_ctf_field_array
*array
;
1640 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1641 for (i
= 0; i
< array
->elements
->len
; i
++) {
1642 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1652 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1656 struct bt_ctf_field_sequence
*sequence
;
1663 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1664 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1665 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1667 fprintf(stderr
, "Failed to validate sequence field %zu\n", i
);
1676 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1685 field
->payload_set
= 0;
1691 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1694 struct bt_ctf_field_enumeration
*enumeration
;
1701 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1703 if (!enumeration
->payload
) {
1707 ret
= bt_ctf_field_reset(enumeration
->payload
);
1713 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1717 struct bt_ctf_field_structure
*structure
;
1724 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1725 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1726 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1730 * Structure members are lazily initialized; skip if
1731 * this member has not been allocated yet.
1736 ret
= bt_ctf_field_reset(member
);
1746 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1749 struct bt_ctf_field_variant
*variant
;
1756 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1757 if (variant
->payload
) {
1758 ret
= bt_ctf_field_reset(variant
->payload
);
1765 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1769 struct bt_ctf_field_array
*array
;
1776 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1777 for (i
= 0; i
< array
->elements
->len
; i
++) {
1778 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1782 * Array elements are lazily initialized; skip if
1783 * this member has not been allocated yet.
1788 ret
= bt_ctf_field_reset(member
);
1798 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1802 struct bt_ctf_field_sequence
*sequence
;
1809 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1810 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1811 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1815 * Sequence elements are lazily initialized; skip if
1816 * this member has not been allocated yet.
1821 ret
= bt_ctf_field_reset(member
);
1831 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1834 struct bt_ctf_field_string
*string
;
1841 ret
= bt_ctf_field_generic_reset(field
);
1846 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1847 if (string
->payload
) {
1848 g_string_truncate(string
->payload
, 0);
1855 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1856 struct ctf_stream_pos
*pos
)
1859 struct bt_ctf_field_integer
*integer
= container_of(field
,
1860 struct bt_ctf_field_integer
, parent
);
1863 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1864 if (ret
== -EFAULT
) {
1866 * The field is too large to fit in the current packet's
1867 * remaining space. Bump the packet size and retry.
1869 ret
= increase_packet_size(pos
);
1880 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1881 struct ctf_stream_pos
*pos
)
1883 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1884 field
, struct bt_ctf_field_enumeration
, parent
);
1886 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1890 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1891 struct ctf_stream_pos
*pos
)
1894 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1895 struct bt_ctf_field_floating_point
, parent
);
1898 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1899 if (ret
== -EFAULT
) {
1901 * The field is too large to fit in the current packet's
1902 * remaining space. Bump the packet size and retry.
1904 ret
= increase_packet_size(pos
);
1915 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1916 struct ctf_stream_pos
*pos
)
1920 struct bt_ctf_field_structure
*structure
= container_of(
1921 field
, struct bt_ctf_field_structure
, parent
);
1923 while (!ctf_pos_access_ok(pos
,
1924 offset_align(pos
->offset
,
1925 field
->type
->declaration
->alignment
))) {
1926 ret
= increase_packet_size(pos
);
1932 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1937 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1938 struct bt_ctf_field
*field
= g_ptr_array_index(
1939 structure
->fields
, i
);
1941 ret
= bt_ctf_field_serialize(field
, pos
);
1951 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1952 struct ctf_stream_pos
*pos
)
1954 struct bt_ctf_field_variant
*variant
= container_of(
1955 field
, struct bt_ctf_field_variant
, parent
);
1957 return bt_ctf_field_serialize(variant
->payload
, pos
);
1961 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1962 struct ctf_stream_pos
*pos
)
1966 struct bt_ctf_field_array
*array
= container_of(
1967 field
, struct bt_ctf_field_array
, parent
);
1969 for (i
= 0; i
< array
->elements
->len
; i
++) {
1970 ret
= bt_ctf_field_serialize(
1971 g_ptr_array_index(array
->elements
, i
), pos
);
1981 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1982 struct ctf_stream_pos
*pos
)
1986 struct bt_ctf_field_sequence
*sequence
= container_of(
1987 field
, struct bt_ctf_field_sequence
, parent
);
1989 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1990 ret
= bt_ctf_field_serialize(
1991 g_ptr_array_index(sequence
->elements
, i
), pos
);
2001 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
2002 struct ctf_stream_pos
*pos
)
2006 struct bt_ctf_field_string
*string
= container_of(field
,
2007 struct bt_ctf_field_string
, parent
);
2008 struct bt_ctf_field_type
*character_type
=
2009 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
2010 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
2012 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
2013 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
2014 (uint64_t) string
->payload
->str
[i
]);
2019 ret
= bt_ctf_field_integer_serialize(character
, pos
);
2026 bt_put(character_type
);
2031 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
2032 struct bt_ctf_field
*dst
)
2034 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
2036 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
2037 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
2039 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
2040 sizeof(struct definition_integer
));
2045 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
2046 struct bt_ctf_field
*dst
)
2049 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
2051 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
2052 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
2054 if (enum_src
->payload
) {
2055 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
2056 if (!enum_dst
->payload
) {
2066 int bt_ctf_field_floating_point_copy(
2067 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
2069 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
2071 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
2073 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
2076 memcpy(&float_dst
->definition
, &float_src
->definition
,
2077 sizeof(struct definition_float
));
2078 memcpy(&float_dst
->sign
, &float_src
->sign
,
2079 sizeof(struct definition_integer
));
2080 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
2081 sizeof(struct definition_integer
));
2082 memcpy(&float_dst
->exp
, &float_src
->exp
,
2083 sizeof(struct definition_integer
));
2088 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2089 struct bt_ctf_field
*dst
)
2092 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2094 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2095 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2097 /* This field_name_to_index HT is owned by the structure field type */
2098 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2099 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2101 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2102 struct bt_ctf_field
*field
=
2103 g_ptr_array_index(struct_src
->fields
, i
);
2104 struct bt_ctf_field
*field_copy
= NULL
;
2107 field_copy
= bt_ctf_field_copy(field
);
2115 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2122 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2123 struct bt_ctf_field
*dst
)
2126 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2128 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2129 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2131 if (variant_src
->tag
) {
2132 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2133 if (!variant_dst
->tag
) {
2138 if (variant_src
->payload
) {
2139 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2140 if (!variant_dst
->payload
) {
2150 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2151 struct bt_ctf_field
*dst
)
2154 struct bt_ctf_field_array
*array_src
, *array_dst
;
2156 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2157 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2159 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2160 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2161 struct bt_ctf_field
*field
=
2162 g_ptr_array_index(array_src
->elements
, i
);
2163 struct bt_ctf_field
*field_copy
= NULL
;
2166 field_copy
= bt_ctf_field_copy(field
);
2174 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2181 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2182 struct bt_ctf_field
*dst
)
2185 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2186 struct bt_ctf_field
*src_length
;
2187 struct bt_ctf_field
*dst_length
;
2189 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2190 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2192 src_length
= bt_ctf_field_sequence_get_length(src
);
2195 /* no length set yet: keep destination sequence empty */
2199 /* copy source length */
2200 dst_length
= bt_ctf_field_copy(src_length
);
2208 /* this will initialize the destination sequence's internal array */
2209 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2216 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2218 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2219 struct bt_ctf_field
*field
=
2220 g_ptr_array_index(sequence_src
->elements
, i
);
2221 struct bt_ctf_field
*field_copy
= NULL
;
2224 field_copy
= bt_ctf_field_copy(field
);
2232 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2239 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2240 struct bt_ctf_field
*dst
)
2243 struct bt_ctf_field_string
*string_src
, *string_dst
;
2245 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2246 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2248 if (string_src
->payload
) {
2249 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2250 if (!string_dst
->payload
) {
2260 int increase_packet_size(struct ctf_stream_pos
*pos
)
2265 ret
= munmap_align(pos
->base_mma
);
2270 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2272 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2273 pos
->packet_size
/ CHAR_BIT
);
2274 } while (ret
== EINTR
);
2281 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2282 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2283 if (pos
->base_mma
== MAP_FAILED
) {
2291 void generic_field_freeze(struct bt_ctf_field
*field
)
2297 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*field
)
2299 struct bt_ctf_field_enumeration
*enum_field
=
2300 container_of(field
, struct bt_ctf_field_enumeration
, parent
);
2302 bt_ctf_field_freeze(enum_field
->payload
);
2303 generic_field_freeze(field
);
2307 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*field
)
2310 struct bt_ctf_field_structure
*structure_field
=
2311 container_of(field
, struct bt_ctf_field_structure
, parent
);
2313 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
2314 struct bt_ctf_field
*field
=
2315 g_ptr_array_index(structure_field
->fields
, i
);
2317 bt_ctf_field_freeze(field
);
2320 generic_field_freeze(field
);
2324 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*field
)
2326 struct bt_ctf_field_variant
*variant_field
=
2327 container_of(field
, struct bt_ctf_field_variant
, parent
);
2329 bt_ctf_field_freeze(variant_field
->tag
);
2330 bt_ctf_field_freeze(variant_field
->payload
);
2331 generic_field_freeze(field
);
2335 void bt_ctf_field_array_freeze(struct bt_ctf_field
*field
)
2338 struct bt_ctf_field_array
*array_field
=
2339 container_of(field
, struct bt_ctf_field_array
, parent
);
2341 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
2342 struct bt_ctf_field
*field
=
2343 g_ptr_array_index(array_field
->elements
, i
);
2345 bt_ctf_field_freeze(field
);
2348 generic_field_freeze(field
);
2352 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*field
)
2355 struct bt_ctf_field_sequence
*sequence_field
=
2356 container_of(field
, struct bt_ctf_field_sequence
, parent
);
2358 bt_ctf_field_freeze(sequence_field
->length
);
2360 for (i
= 0; i
< sequence_field
->elements
->len
; i
++) {
2361 struct bt_ctf_field
*field
=
2362 g_ptr_array_index(sequence_field
->elements
, i
);
2364 bt_ctf_field_freeze(field
);
2367 generic_field_freeze(field
);
2371 void bt_ctf_field_freeze(struct bt_ctf_field
*field
)
2373 enum bt_ctf_type_id type_id
;
2379 type_id
= bt_ctf_field_get_type_id(field
);
2380 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
2381 type_id
>= BT_CTF_NR_TYPE_IDS
) {
2385 field_freeze_funcs
[type_id
](field
);