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_enumeration_get_container(
792 struct bt_ctf_field
*field
)
794 struct bt_ctf_field
*container
= NULL
;
795 struct bt_ctf_field_enumeration
*enumeration
;
797 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
798 BT_CTF_TYPE_ID_ENUM
) {
802 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
804 if (!enumeration
->payload
) {
805 /* We don't want to modify this field if it's frozen */
810 struct bt_ctf_field_type_enumeration
*enumeration_type
=
811 container_of(field
->type
,
812 struct bt_ctf_field_type_enumeration
, parent
);
813 enumeration
->payload
=
814 bt_ctf_field_create(enumeration_type
->container
);
817 container
= enumeration
->payload
;
823 const char *bt_ctf_field_enumeration_get_mapping_name(
824 struct bt_ctf_field
*field
)
827 const char *name
= NULL
;
828 struct bt_ctf_field
*container
= NULL
;
829 struct bt_ctf_field_type
*container_type
= NULL
;
830 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
831 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
833 container
= bt_ctf_field_enumeration_get_container(field
);
838 container_type
= bt_ctf_field_get_type(container
);
839 if (!container_type
) {
840 goto error_put_container
;
843 integer_type
= container_of(container_type
,
844 struct bt_ctf_field_type_integer
, parent
);
845 enumeration_type
= container_of(field
->type
,
846 struct bt_ctf_field_type_enumeration
, parent
);
848 if (!integer_type
->declaration
.signedness
) {
850 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
853 goto error_put_container_type
;
856 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
857 enumeration_type
, value
);
860 ret
= bt_ctf_field_signed_integer_get_value(container
,
863 goto error_put_container_type
;
866 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
867 enumeration_type
, value
);
870 error_put_container_type
:
871 bt_put(container_type
);
878 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
882 struct bt_ctf_field_integer
*integer
;
883 struct bt_ctf_field_type_integer
*integer_type
;
885 if (!field
|| !value
|| !field
->payload_set
||
886 bt_ctf_field_type_get_type_id(field
->type
) !=
887 BT_CTF_TYPE_ID_INTEGER
) {
892 integer_type
= container_of(field
->type
,
893 struct bt_ctf_field_type_integer
, parent
);
894 if (!integer_type
->declaration
.signedness
) {
899 integer
= container_of(field
,
900 struct bt_ctf_field_integer
, parent
);
901 *value
= integer
->definition
.value
._signed
;
906 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
910 struct bt_ctf_field_integer
*integer
;
911 struct bt_ctf_field_type_integer
*integer_type
;
913 int64_t min_value
, max_value
;
915 if (!field
|| field
->frozen
||
916 bt_ctf_field_type_get_type_id(field
->type
) !=
917 BT_CTF_TYPE_ID_INTEGER
) {
922 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
923 integer_type
= container_of(field
->type
,
924 struct bt_ctf_field_type_integer
, parent
);
925 if (!integer_type
->declaration
.signedness
) {
930 size
= integer_type
->declaration
.len
;
931 min_value
= -(1ULL << (size
- 1));
932 max_value
= (1ULL << (size
- 1)) - 1;
933 if (value
< min_value
|| value
> max_value
) {
938 integer
->definition
.value
._signed
= value
;
939 integer
->parent
.payload_set
= 1;
944 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
948 struct bt_ctf_field_integer
*integer
;
949 struct bt_ctf_field_type_integer
*integer_type
;
951 if (!field
|| !value
|| !field
->payload_set
||
952 bt_ctf_field_type_get_type_id(field
->type
) !=
953 BT_CTF_TYPE_ID_INTEGER
) {
958 integer_type
= container_of(field
->type
,
959 struct bt_ctf_field_type_integer
, parent
);
960 if (integer_type
->declaration
.signedness
) {
965 integer
= container_of(field
,
966 struct bt_ctf_field_integer
, parent
);
967 *value
= integer
->definition
.value
._unsigned
;
972 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
976 struct bt_ctf_field_integer
*integer
;
977 struct bt_ctf_field_type_integer
*integer_type
;
981 if (!field
|| field
->frozen
||
982 bt_ctf_field_type_get_type_id(field
->type
) !=
983 BT_CTF_TYPE_ID_INTEGER
) {
988 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
989 integer_type
= container_of(field
->type
,
990 struct bt_ctf_field_type_integer
, parent
);
991 if (integer_type
->declaration
.signedness
) {
996 size
= integer_type
->declaration
.len
;
997 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
998 if (value
> max_value
) {
1003 integer
->definition
.value
._unsigned
= value
;
1004 integer
->parent
.payload_set
= 1;
1009 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
1013 struct bt_ctf_field_floating_point
*floating_point
;
1015 if (!field
|| !value
|| !field
->payload_set
||
1016 bt_ctf_field_type_get_type_id(field
->type
) !=
1017 BT_CTF_TYPE_ID_FLOAT
) {
1022 floating_point
= container_of(field
,
1023 struct bt_ctf_field_floating_point
, parent
);
1024 *value
= floating_point
->definition
.value
;
1029 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
1033 struct bt_ctf_field_floating_point
*floating_point
;
1035 if (!field
|| field
->frozen
||
1036 bt_ctf_field_type_get_type_id(field
->type
) !=
1037 BT_CTF_TYPE_ID_FLOAT
) {
1041 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1043 floating_point
->definition
.value
= value
;
1044 floating_point
->parent
.payload_set
= 1;
1049 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
1051 const char *ret
= NULL
;
1052 struct bt_ctf_field_string
*string
;
1054 if (!field
|| !field
->payload_set
||
1055 bt_ctf_field_type_get_type_id(field
->type
) !=
1056 BT_CTF_TYPE_ID_STRING
) {
1060 string
= container_of(field
,
1061 struct bt_ctf_field_string
, parent
);
1062 ret
= string
->payload
->str
;
1067 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1071 struct bt_ctf_field_string
*string
;
1073 if (!field
|| !value
|| field
->frozen
||
1074 bt_ctf_field_type_get_type_id(field
->type
) !=
1075 BT_CTF_TYPE_ID_STRING
) {
1080 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1081 if (string
->payload
) {
1082 g_string_assign(string
->payload
, value
);
1084 string
->payload
= g_string_new(value
);
1087 string
->parent
.payload_set
= 1;
1092 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1096 struct bt_ctf_field_string
*string_field
;
1098 if (!field
|| !value
|| field
->frozen
||
1099 bt_ctf_field_type_get_type_id(field
->type
) !=
1100 BT_CTF_TYPE_ID_STRING
) {
1105 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1107 if (string_field
->payload
) {
1108 g_string_append(string_field
->payload
, value
);
1110 string_field
->payload
= g_string_new(value
);
1113 string_field
->parent
.payload_set
= 1;
1119 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1120 const char *value
, unsigned int length
)
1124 unsigned int effective_length
= length
;
1125 struct bt_ctf_field_string
*string_field
;
1127 if (!field
|| !value
|| field
->frozen
||
1128 bt_ctf_field_type_get_type_id(field
->type
) !=
1129 BT_CTF_TYPE_ID_STRING
) {
1134 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1136 /* make sure no null bytes are appended */
1137 for (i
= 0; i
< length
; ++i
) {
1138 if (value
[i
] == '\0') {
1139 effective_length
= i
;
1144 if (string_field
->payload
) {
1145 g_string_append_len(string_field
->payload
, value
,
1148 string_field
->payload
= g_string_new_len(value
,
1152 string_field
->parent
.payload_set
= 1;
1159 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1162 enum bt_ctf_type_id type_id
;
1169 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1170 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1175 ret
= field_validate_funcs
[type_id
](field
);
1181 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1184 enum bt_ctf_type_id type_id
;
1191 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1192 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1197 ret
= field_reset_funcs
[type_id
](field
);
1203 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1204 struct ctf_stream_pos
*pos
)
1207 enum bt_ctf_type_id type_id
;
1209 if (!field
|| !pos
) {
1214 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1215 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1220 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1225 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1228 struct bt_ctf_field
*copy
= NULL
;
1229 enum bt_ctf_type_id type_id
;
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
) {
1240 copy
= bt_ctf_field_create(field
->type
);
1245 copy
->payload_set
= field
->payload_set
;
1246 ret
= field_copy_funcs
[type_id
](field
, copy
);
1256 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1258 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1259 struct bt_ctf_field_type_integer
, parent
);
1260 struct bt_ctf_field_integer
*integer
= g_new0(
1261 struct bt_ctf_field_integer
, 1);
1264 integer
->definition
.declaration
= &integer_type
->declaration
;
1267 return integer
? &integer
->parent
: NULL
;
1271 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1272 struct bt_ctf_field_type
*type
)
1274 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1275 struct bt_ctf_field_enumeration
, 1);
1277 return enumeration
? &enumeration
->parent
: NULL
;
1281 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1282 struct bt_ctf_field_type
*type
)
1284 struct bt_ctf_field_floating_point
*floating_point
;
1285 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1287 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1288 if (!floating_point
) {
1292 floating_point_type
= container_of(type
,
1293 struct bt_ctf_field_type_floating_point
, parent
);
1294 floating_point
->definition
.declaration
= container_of(
1295 type
->declaration
, struct declaration_float
, p
);
1298 floating_point
->definition
.sign
= &floating_point
->sign
;
1299 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1300 floating_point
->definition
.sign
->p
.declaration
=
1301 &floating_point_type
->sign
.p
;
1303 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1304 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1305 floating_point
->definition
.mantissa
->p
.declaration
=
1306 &floating_point_type
->mantissa
.p
;
1308 floating_point
->definition
.exp
= &floating_point
->exp
;
1309 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1310 floating_point
->definition
.exp
->p
.declaration
=
1311 &floating_point_type
->exp
.p
;
1314 return floating_point
? &floating_point
->parent
: NULL
;
1318 struct bt_ctf_field
*bt_ctf_field_structure_create(
1319 struct bt_ctf_field_type
*type
)
1321 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1322 struct bt_ctf_field_type_structure
, parent
);
1323 struct bt_ctf_field_structure
*structure
= g_new0(
1324 struct bt_ctf_field_structure
, 1);
1325 struct bt_ctf_field
*field
= NULL
;
1331 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1332 structure
->fields
= g_ptr_array_new_with_free_func(
1333 (GDestroyNotify
)bt_ctf_field_put
);
1334 g_ptr_array_set_size(structure
->fields
,
1335 g_hash_table_size(structure
->field_name_to_index
));
1336 field
= &structure
->parent
;
1342 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1344 struct bt_ctf_field_variant
*variant
= g_new0(
1345 struct bt_ctf_field_variant
, 1);
1346 return variant
? &variant
->parent
: NULL
;
1350 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1352 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1353 struct bt_ctf_field_type_array
*array_type
;
1354 unsigned int array_length
;
1356 if (!array
|| !type
) {
1360 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1361 array_length
= array_type
->length
;
1362 array
->elements
= g_ptr_array_sized_new(array_length
);
1363 if (!array
->elements
) {
1367 g_ptr_array_set_free_func(array
->elements
,
1368 (GDestroyNotify
)bt_ctf_field_put
);
1369 g_ptr_array_set_size(array
->elements
, array_length
);
1370 return &array
->parent
;
1377 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1378 struct bt_ctf_field_type
*type
)
1380 struct bt_ctf_field_sequence
*sequence
= g_new0(
1381 struct bt_ctf_field_sequence
, 1);
1382 return sequence
? &sequence
->parent
: NULL
;
1386 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1388 struct bt_ctf_field_string
*string
= g_new0(
1389 struct bt_ctf_field_string
, 1);
1390 return string
? &string
->parent
: NULL
;
1394 void bt_ctf_field_destroy(struct bt_object
*obj
)
1396 struct bt_ctf_field
*field
;
1397 struct bt_ctf_field_type
*type
;
1398 enum bt_ctf_type_id type_id
;
1400 field
= container_of(obj
, struct bt_ctf_field
, base
);
1402 type_id
= bt_ctf_field_type_get_type_id(type
);
1403 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
1404 type_id
>= BT_CTF_NR_TYPE_IDS
) {
1408 field_destroy_funcs
[type_id
](field
);
1413 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1415 struct bt_ctf_field_integer
*integer
;
1421 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1426 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1428 struct bt_ctf_field_enumeration
*enumeration
;
1434 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1436 bt_put(enumeration
->payload
);
1437 g_free(enumeration
);
1441 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1443 struct bt_ctf_field_floating_point
*floating_point
;
1449 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1451 g_free(floating_point
);
1455 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1457 struct bt_ctf_field_structure
*structure
;
1463 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1464 g_ptr_array_free(structure
->fields
, TRUE
);
1469 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1471 struct bt_ctf_field_variant
*variant
;
1477 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1478 bt_put(variant
->tag
);
1479 bt_put(variant
->payload
);
1484 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1486 struct bt_ctf_field_array
*array
;
1492 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1493 g_ptr_array_free(array
->elements
, TRUE
);
1498 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1500 struct bt_ctf_field_sequence
*sequence
;
1506 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1507 if (sequence
->elements
) {
1508 g_ptr_array_free(sequence
->elements
, TRUE
);
1510 bt_put(sequence
->length
);
1515 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1517 struct bt_ctf_field_string
*string
;
1522 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1523 if (string
->payload
) {
1524 g_string_free(string
->payload
, TRUE
);
1530 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1532 return (field
&& field
->payload_set
) ? 0 : -1;
1536 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1539 struct bt_ctf_field_enumeration
*enumeration
;
1546 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1548 if (!enumeration
->payload
) {
1553 ret
= bt_ctf_field_validate(enumeration
->payload
);
1559 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1563 struct bt_ctf_field_structure
*structure
;
1570 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1571 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1572 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1582 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1585 struct bt_ctf_field_variant
*variant
;
1592 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1593 ret
= bt_ctf_field_validate(variant
->payload
);
1599 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1603 struct bt_ctf_field_array
*array
;
1610 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1611 for (i
= 0; i
< array
->elements
->len
; i
++) {
1612 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1622 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1626 struct bt_ctf_field_sequence
*sequence
;
1633 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1634 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1635 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1645 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1654 field
->payload_set
= 0;
1660 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1663 struct bt_ctf_field_enumeration
*enumeration
;
1670 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1672 if (!enumeration
->payload
) {
1676 ret
= bt_ctf_field_reset(enumeration
->payload
);
1682 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1686 struct bt_ctf_field_structure
*structure
;
1693 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1694 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1695 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1699 * Structure members are lazily initialized; skip if
1700 * this member has not been allocated yet.
1705 ret
= bt_ctf_field_reset(member
);
1715 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1718 struct bt_ctf_field_variant
*variant
;
1725 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1726 if (variant
->payload
) {
1727 ret
= bt_ctf_field_reset(variant
->payload
);
1734 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1738 struct bt_ctf_field_array
*array
;
1745 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1746 for (i
= 0; i
< array
->elements
->len
; i
++) {
1747 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1751 * Array elements are lazily initialized; skip if
1752 * this member has not been allocated yet.
1757 ret
= bt_ctf_field_reset(member
);
1767 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1771 struct bt_ctf_field_sequence
*sequence
;
1778 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1779 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1780 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1784 * Sequence elements are lazily initialized; skip if
1785 * this member has not been allocated yet.
1790 ret
= bt_ctf_field_reset(member
);
1800 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1803 struct bt_ctf_field_string
*string
;
1810 ret
= bt_ctf_field_generic_reset(field
);
1815 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1816 if (string
->payload
) {
1817 g_string_truncate(string
->payload
, 0);
1824 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1825 struct ctf_stream_pos
*pos
)
1828 struct bt_ctf_field_integer
*integer
= container_of(field
,
1829 struct bt_ctf_field_integer
, parent
);
1832 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1833 if (ret
== -EFAULT
) {
1835 * The field is too large to fit in the current packet's
1836 * remaining space. Bump the packet size and retry.
1838 ret
= increase_packet_size(pos
);
1849 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1850 struct ctf_stream_pos
*pos
)
1852 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1853 field
, struct bt_ctf_field_enumeration
, parent
);
1855 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1859 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1860 struct ctf_stream_pos
*pos
)
1863 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1864 struct bt_ctf_field_floating_point
, parent
);
1867 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1868 if (ret
== -EFAULT
) {
1870 * The field is too large to fit in the current packet's
1871 * remaining space. Bump the packet size and retry.
1873 ret
= increase_packet_size(pos
);
1884 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1885 struct ctf_stream_pos
*pos
)
1889 struct bt_ctf_field_structure
*structure
= container_of(
1890 field
, struct bt_ctf_field_structure
, parent
);
1892 while (!ctf_pos_access_ok(pos
,
1893 offset_align(pos
->offset
,
1894 field
->type
->declaration
->alignment
))) {
1895 ret
= increase_packet_size(pos
);
1901 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1906 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1907 struct bt_ctf_field
*field
= g_ptr_array_index(
1908 structure
->fields
, i
);
1910 ret
= bt_ctf_field_serialize(field
, pos
);
1920 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1921 struct ctf_stream_pos
*pos
)
1923 struct bt_ctf_field_variant
*variant
= container_of(
1924 field
, struct bt_ctf_field_variant
, parent
);
1926 return bt_ctf_field_serialize(variant
->payload
, pos
);
1930 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1931 struct ctf_stream_pos
*pos
)
1935 struct bt_ctf_field_array
*array
= container_of(
1936 field
, struct bt_ctf_field_array
, parent
);
1938 for (i
= 0; i
< array
->elements
->len
; i
++) {
1939 ret
= bt_ctf_field_serialize(
1940 g_ptr_array_index(array
->elements
, i
), pos
);
1950 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1951 struct ctf_stream_pos
*pos
)
1955 struct bt_ctf_field_sequence
*sequence
= container_of(
1956 field
, struct bt_ctf_field_sequence
, parent
);
1958 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1959 ret
= bt_ctf_field_serialize(
1960 g_ptr_array_index(sequence
->elements
, i
), pos
);
1970 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1971 struct ctf_stream_pos
*pos
)
1975 struct bt_ctf_field_string
*string
= container_of(field
,
1976 struct bt_ctf_field_string
, parent
);
1977 struct bt_ctf_field_type
*character_type
=
1978 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1979 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1981 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1982 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1983 (uint64_t) string
->payload
->str
[i
]);
1988 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1995 bt_put(character_type
);
2000 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
2001 struct bt_ctf_field
*dst
)
2003 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
2005 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
2006 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
2008 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
2009 sizeof(struct definition_integer
));
2014 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
2015 struct bt_ctf_field
*dst
)
2018 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
2020 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
2021 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
2023 if (enum_src
->payload
) {
2024 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
2025 if (!enum_dst
->payload
) {
2035 int bt_ctf_field_floating_point_copy(
2036 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
2038 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
2040 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
2042 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
2045 memcpy(&float_dst
->definition
, &float_src
->definition
,
2046 sizeof(struct definition_float
));
2047 memcpy(&float_dst
->sign
, &float_src
->sign
,
2048 sizeof(struct definition_integer
));
2049 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
2050 sizeof(struct definition_integer
));
2051 memcpy(&float_dst
->exp
, &float_src
->exp
,
2052 sizeof(struct definition_integer
));
2057 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2058 struct bt_ctf_field
*dst
)
2061 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2063 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2064 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2066 /* This field_name_to_index HT is owned by the structure field type */
2067 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2068 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2070 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2071 struct bt_ctf_field
*field
=
2072 g_ptr_array_index(struct_src
->fields
, i
);
2073 struct bt_ctf_field
*field_copy
= NULL
;
2076 field_copy
= bt_ctf_field_copy(field
);
2084 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2091 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2092 struct bt_ctf_field
*dst
)
2095 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2097 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2098 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2100 if (variant_src
->tag
) {
2101 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2102 if (!variant_dst
->tag
) {
2107 if (variant_src
->payload
) {
2108 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2109 if (!variant_dst
->payload
) {
2119 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2120 struct bt_ctf_field
*dst
)
2123 struct bt_ctf_field_array
*array_src
, *array_dst
;
2125 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2126 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2128 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2129 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2130 struct bt_ctf_field
*field
=
2131 g_ptr_array_index(array_src
->elements
, i
);
2132 struct bt_ctf_field
*field_copy
= NULL
;
2135 field_copy
= bt_ctf_field_copy(field
);
2143 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2150 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2151 struct bt_ctf_field
*dst
)
2154 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2155 struct bt_ctf_field
*src_length
;
2156 struct bt_ctf_field
*dst_length
;
2158 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2159 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2161 src_length
= bt_ctf_field_sequence_get_length(src
);
2164 /* no length set yet: keep destination sequence empty */
2168 /* copy source length */
2169 dst_length
= bt_ctf_field_copy(src_length
);
2177 /* this will initialize the destination sequence's internal array */
2178 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2185 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2187 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2188 struct bt_ctf_field
*field
=
2189 g_ptr_array_index(sequence_src
->elements
, i
);
2190 struct bt_ctf_field
*field_copy
= NULL
;
2193 field_copy
= bt_ctf_field_copy(field
);
2201 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2208 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2209 struct bt_ctf_field
*dst
)
2212 struct bt_ctf_field_string
*string_src
, *string_dst
;
2214 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2215 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2217 if (string_src
->payload
) {
2218 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2219 if (!string_dst
->payload
) {
2229 int increase_packet_size(struct ctf_stream_pos
*pos
)
2234 ret
= munmap_align(pos
->base_mma
);
2239 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2241 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2242 pos
->packet_size
/ CHAR_BIT
);
2243 } while (ret
== EINTR
);
2250 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2251 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2252 if (pos
->base_mma
== MAP_FAILED
) {
2260 void generic_field_freeze(struct bt_ctf_field
*field
)
2266 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*field
)
2268 struct bt_ctf_field_enumeration
*enum_field
=
2269 container_of(field
, struct bt_ctf_field_enumeration
, parent
);
2271 bt_ctf_field_freeze(enum_field
->payload
);
2272 generic_field_freeze(field
);
2276 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*field
)
2279 struct bt_ctf_field_structure
*structure_field
=
2280 container_of(field
, struct bt_ctf_field_structure
, parent
);
2282 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
2283 struct bt_ctf_field
*field
=
2284 g_ptr_array_index(structure_field
->fields
, i
);
2286 bt_ctf_field_freeze(field
);
2289 generic_field_freeze(field
);
2293 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*field
)
2295 struct bt_ctf_field_variant
*variant_field
=
2296 container_of(field
, struct bt_ctf_field_variant
, parent
);
2298 bt_ctf_field_freeze(variant_field
->tag
);
2299 bt_ctf_field_freeze(variant_field
->payload
);
2300 generic_field_freeze(field
);
2304 void bt_ctf_field_array_freeze(struct bt_ctf_field
*field
)
2307 struct bt_ctf_field_array
*array_field
=
2308 container_of(field
, struct bt_ctf_field_array
, parent
);
2310 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
2311 struct bt_ctf_field
*field
=
2312 g_ptr_array_index(array_field
->elements
, i
);
2314 bt_ctf_field_freeze(field
);
2317 generic_field_freeze(field
);
2321 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*field
)
2324 struct bt_ctf_field_sequence
*sequence_field
=
2325 container_of(field
, struct bt_ctf_field_sequence
, parent
);
2327 bt_ctf_field_freeze(sequence_field
->length
);
2329 for (i
= 0; i
< sequence_field
->elements
->len
; i
++) {
2330 struct bt_ctf_field
*field
=
2331 g_ptr_array_index(sequence_field
->elements
, i
);
2333 bt_ctf_field_freeze(field
);
2336 generic_field_freeze(field
);
2340 void bt_ctf_field_freeze(struct bt_ctf_field
*field
)
2342 enum bt_ctf_type_id type_id
;
2348 type_id
= bt_ctf_field_get_type_id(field
);
2349 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
2350 type_id
>= BT_CTF_NR_TYPE_IDS
) {
2354 field_freeze_funcs
[type_id
](field
);