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-writer/event-fields.h>
30 #include <babeltrace/ctf-ir/event-fields-internal.h>
31 #include <babeltrace/ctf-ir/event-types-internal.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/ref.h>
34 #include <babeltrace/compiler.h>
35 #include <babeltrace/compat/fcntl.h>
37 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
40 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
42 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
43 struct bt_ctf_field_type
*);
45 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
46 struct bt_ctf_field_type
*);
48 struct bt_ctf_field
*bt_ctf_field_structure_create(
49 struct bt_ctf_field_type
*);
51 struct bt_ctf_field
*bt_ctf_field_variant_create(
52 struct bt_ctf_field_type
*);
54 struct bt_ctf_field
*bt_ctf_field_array_create(
55 struct bt_ctf_field_type
*);
57 struct bt_ctf_field
*bt_ctf_field_sequence_create(
58 struct bt_ctf_field_type
*);
60 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
63 void bt_ctf_field_destroy(struct bt_object
*);
65 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
67 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
69 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
71 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
73 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
75 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
77 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
79 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
82 int bt_ctf_field_generic_validate(struct bt_ctf_field
*);
84 int bt_ctf_field_structure_validate(struct bt_ctf_field
*);
86 int bt_ctf_field_variant_validate(struct bt_ctf_field
*);
88 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*);
90 int bt_ctf_field_array_validate(struct bt_ctf_field
*);
92 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*);
95 int bt_ctf_field_generic_reset(struct bt_ctf_field
*);
97 int bt_ctf_field_structure_reset(struct bt_ctf_field
*);
99 int bt_ctf_field_variant_reset(struct bt_ctf_field
*);
101 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*);
103 int bt_ctf_field_array_reset(struct bt_ctf_field
*);
105 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*);
107 int bt_ctf_field_string_reset(struct bt_ctf_field
*);
110 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
111 struct ctf_stream_pos
*);
113 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
114 struct ctf_stream_pos
*);
116 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
117 struct ctf_stream_pos
*);
119 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
120 struct ctf_stream_pos
*);
122 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
123 struct ctf_stream_pos
*);
125 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
126 struct ctf_stream_pos
*);
128 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
129 struct ctf_stream_pos
*);
131 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
132 struct ctf_stream_pos
*);
135 int bt_ctf_field_integer_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
137 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
139 int bt_ctf_field_floating_point_copy(struct bt_ctf_field
*,
140 struct bt_ctf_field
*);
142 int bt_ctf_field_structure_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
144 int bt_ctf_field_variant_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
146 int bt_ctf_field_array_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
148 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
150 int bt_ctf_field_string_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
153 int increase_packet_size(struct ctf_stream_pos
*pos
);
156 struct bt_ctf_field
*(* const field_create_funcs
[])(
157 struct bt_ctf_field_type
*) = {
158 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_create
,
159 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_create
,
161 bt_ctf_field_floating_point_create
,
162 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_create
,
163 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_create
,
164 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_create
,
165 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_create
,
166 [CTF_TYPE_STRING
] = bt_ctf_field_string_create
,
170 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
171 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_destroy
,
172 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_destroy
,
174 bt_ctf_field_floating_point_destroy
,
175 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_destroy
,
176 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_destroy
,
177 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_destroy
,
178 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
179 [CTF_TYPE_STRING
] = bt_ctf_field_string_destroy
,
183 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
184 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_validate
,
185 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_validate
,
186 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_validate
,
187 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_validate
,
188 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_validate
,
189 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_validate
,
190 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_validate
,
191 [CTF_TYPE_STRING
] = bt_ctf_field_generic_validate
,
195 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
196 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_reset
,
197 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_reset
,
198 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_reset
,
199 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_reset
,
200 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_reset
,
201 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_reset
,
202 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_reset
,
203 [CTF_TYPE_STRING
] = bt_ctf_field_string_reset
,
207 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
208 struct ctf_stream_pos
*) = {
209 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_serialize
,
210 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_serialize
,
212 bt_ctf_field_floating_point_serialize
,
213 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_serialize
,
214 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_serialize
,
215 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_serialize
,
216 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
217 [CTF_TYPE_STRING
] = bt_ctf_field_string_serialize
,
221 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
222 struct bt_ctf_field
*) = {
223 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_copy
,
224 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_copy
,
225 [CTF_TYPE_FLOAT
] = bt_ctf_field_floating_point_copy
,
226 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_copy
,
227 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_copy
,
228 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_copy
,
229 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_copy
,
230 [CTF_TYPE_STRING
] = bt_ctf_field_string_copy
,
233 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
235 struct bt_ctf_field
*field
= NULL
;
236 enum ctf_type_id type_id
;
243 type_id
= bt_ctf_field_type_get_type_id(type
);
244 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
248 /* Field class MUST be valid */
249 ret
= bt_ctf_field_type_validate(type
);
256 field
= field_create_funcs
[type_id
](type
);
261 /* The type's declaration can't change after this point */
262 bt_ctf_field_type_freeze(type
);
264 bt_object_init(field
, bt_ctf_field_destroy
);
270 void bt_ctf_field_get(struct bt_ctf_field
*field
)
275 void bt_ctf_field_put(struct bt_ctf_field
*field
)
280 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
282 struct bt_ctf_field_type
*ret
= NULL
;
294 enum ctf_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
296 enum ctf_type_id ret
= CTF_TYPE_UNKNOWN
;
302 ret
= bt_ctf_field_type_get_type_id(field
->type
);
307 int bt_ctf_field_is_integer(struct bt_ctf_field
*field
)
309 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_INTEGER
;
312 int bt_ctf_field_is_floating_point(struct bt_ctf_field
*field
)
314 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_FLOAT
;
317 int bt_ctf_field_is_enumeration(struct bt_ctf_field
*field
)
319 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_ENUM
;
322 int bt_ctf_field_is_string(struct bt_ctf_field
*field
)
324 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_STRING
;
327 int bt_ctf_field_is_structure(struct bt_ctf_field
*field
)
329 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_STRUCT
;
332 int bt_ctf_field_is_array(struct bt_ctf_field
*field
)
334 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_ARRAY
;
337 int bt_ctf_field_is_sequence(struct bt_ctf_field
*field
)
339 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_SEQUENCE
;
342 int bt_ctf_field_is_variant(struct bt_ctf_field
*field
)
344 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_VARIANT
;
347 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
348 struct bt_ctf_field
*field
)
350 struct bt_ctf_field
*ret
= NULL
;
351 struct bt_ctf_field_sequence
*sequence
;
357 if (bt_ctf_field_type_get_type_id(field
->type
) !=
362 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
363 ret
= sequence
->length
;
369 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
370 struct bt_ctf_field
*length_field
)
373 struct bt_ctf_field_type_integer
*length_type
;
374 struct bt_ctf_field_integer
*length
;
375 struct bt_ctf_field_sequence
*sequence
;
376 uint64_t sequence_length
;
378 if (!field
|| !length_field
) {
382 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
388 length_type
= container_of(length_field
->type
,
389 struct bt_ctf_field_type_integer
, parent
);
390 /* The length field must be unsigned */
391 if (length_type
->declaration
.signedness
) {
396 length
= container_of(length_field
, struct bt_ctf_field_integer
,
398 sequence_length
= length
->definition
.value
._unsigned
;
399 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
400 if (sequence
->elements
) {
401 g_ptr_array_free(sequence
->elements
, TRUE
);
402 bt_put(sequence
->length
);
405 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
406 if (!sequence
->elements
) {
411 g_ptr_array_set_free_func(sequence
->elements
,
412 (GDestroyNotify
) bt_put
);
413 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
414 bt_get(length_field
);
415 sequence
->length
= length_field
;
420 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
421 struct bt_ctf_field
*field
, const char *name
)
423 struct bt_ctf_field
*new_field
= NULL
;
425 struct bt_ctf_field_structure
*structure
;
426 struct bt_ctf_field_type
*field_type
= NULL
;
429 if (!field
|| !name
||
430 bt_ctf_field_type_get_type_id(field
->type
) !=
435 field_quark
= g_quark_from_string(name
);
436 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
438 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
440 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
441 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
445 if (structure
->fields
->pdata
[index
]) {
446 new_field
= structure
->fields
->pdata
[index
];
450 new_field
= bt_ctf_field_create(field_type
);
455 structure
->fields
->pdata
[index
] = new_field
;
465 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
466 struct bt_ctf_field
*field
, int index
)
469 const char *field_name
;
470 struct bt_ctf_field_structure
*structure
;
471 struct bt_ctf_field_type
*structure_type
;
472 struct bt_ctf_field_type
*field_type
= NULL
;
473 struct bt_ctf_field
*ret_field
= NULL
;
476 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
480 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
481 if (index
>= structure
->fields
->len
) {
485 ret_field
= structure
->fields
->pdata
[index
];
490 /* Field has not been instanciated yet, create it */
491 structure_type
= bt_ctf_field_get_type(field
);
492 if (!structure_type
) {
496 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
497 &field_name
, &field_type
, index
);
498 bt_put(structure_type
);
503 ret_field
= bt_ctf_field_create(field_type
);
508 structure
->fields
->pdata
[index
] = ret_field
;
517 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
518 const char *name
, struct bt_ctf_field
*value
)
522 struct bt_ctf_field_structure
*structure
;
523 struct bt_ctf_field_type
*expected_field_type
= NULL
;
526 if (!field
|| !name
|| !value
||
527 bt_ctf_field_type_get_type_id(field
->type
) !=
533 field_quark
= g_quark_from_string(name
);
534 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
535 expected_field_type
=
536 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
539 if (bt_ctf_field_type_compare(expected_field_type
, value
->type
)) {
544 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
545 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
549 if (structure
->fields
->pdata
[index
]) {
550 bt_put(structure
->fields
->pdata
[index
]);
553 structure
->fields
->pdata
[index
] = value
;
556 if (expected_field_type
) {
557 bt_put(expected_field_type
);
562 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
565 struct bt_ctf_field
*new_field
= NULL
;
566 struct bt_ctf_field_type
*field_type
= NULL
;
567 struct bt_ctf_field_array
*array
;
569 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
574 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
575 if (index
>= array
->elements
->len
) {
579 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
580 if (array
->elements
->pdata
[(size_t)index
]) {
581 new_field
= array
->elements
->pdata
[(size_t)index
];
585 new_field
= bt_ctf_field_create(field_type
);
586 array
->elements
->pdata
[(size_t)index
] = new_field
;
597 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
600 struct bt_ctf_field
*new_field
= NULL
;
601 struct bt_ctf_field_type
*field_type
= NULL
;
602 struct bt_ctf_field_sequence
*sequence
;
604 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
609 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
610 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
614 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
615 if (sequence
->elements
->pdata
[(size_t) index
]) {
616 new_field
= sequence
->elements
->pdata
[(size_t) index
];
620 new_field
= bt_ctf_field_create(field_type
);
621 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
632 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
633 struct bt_ctf_field
*tag_field
)
635 struct bt_ctf_field
*new_field
= NULL
;
636 struct bt_ctf_field_variant
*variant
;
637 struct bt_ctf_field_type_variant
*variant_type
;
638 struct bt_ctf_field_type
*field_type
;
639 struct bt_ctf_field
*tag_enum
= NULL
;
640 struct bt_ctf_field_integer
*tag_enum_integer
;
641 int64_t tag_enum_value
;
643 if (!field
|| !tag_field
||
644 bt_ctf_field_type_get_type_id(field
->type
) !=
646 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
651 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
652 variant_type
= container_of(field
->type
,
653 struct bt_ctf_field_type_variant
, parent
);
654 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
659 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
662 if (bt_ctf_field_validate(tag_field
) < 0) {
666 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
669 * If the variant currently has a tag and a payload, and if the
670 * requested tag value is the same as the current one, return
671 * the current payload instead of creating a fresh one.
673 if (variant
->tag
&& variant
->payload
) {
674 struct bt_ctf_field
*cur_tag_container
= NULL
;
675 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
676 int64_t cur_tag_value
;
679 bt_ctf_field_enumeration_get_container(variant
->tag
);
680 cur_tag_enum_integer
= container_of(cur_tag_container
,
681 struct bt_ctf_field_integer
, parent
);
682 bt_put(cur_tag_container
);
683 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
685 if (cur_tag_value
== tag_enum_value
) {
686 new_field
= variant
->payload
;
692 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
693 variant_type
, tag_enum_value
);
698 new_field
= bt_ctf_field_create(field_type
);
703 bt_put(variant
->tag
);
704 bt_put(variant
->payload
);
707 variant
->tag
= tag_field
;
708 variant
->payload
= new_field
;
714 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
715 struct bt_ctf_field
*variant_field
)
717 struct bt_ctf_field
*current_field
= NULL
;
718 struct bt_ctf_field_variant
*variant
;
720 if (!variant_field
||
721 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
726 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
729 if (variant
->payload
) {
730 current_field
= variant
->payload
;
731 bt_get(current_field
);
736 return current_field
;
739 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
740 struct bt_ctf_field
*field
)
742 struct bt_ctf_field
*container
= NULL
;
743 struct bt_ctf_field_enumeration
*enumeration
;
745 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
750 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
752 if (!enumeration
->payload
) {
753 struct bt_ctf_field_type_enumeration
*enumeration_type
=
754 container_of(field
->type
,
755 struct bt_ctf_field_type_enumeration
, parent
);
756 enumeration
->payload
=
757 bt_ctf_field_create(enumeration_type
->container
);
760 container
= enumeration
->payload
;
766 const char *bt_ctf_field_enumeration_get_mapping_name(
767 struct bt_ctf_field
*field
)
770 const char *name
= NULL
;
771 struct bt_ctf_field
*container
= NULL
;
772 struct bt_ctf_field_type
*container_type
= NULL
;
773 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
774 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
776 container
= bt_ctf_field_enumeration_get_container(field
);
781 container_type
= bt_ctf_field_get_type(container
);
782 if (!container_type
) {
783 goto error_put_container
;
786 integer_type
= container_of(container_type
,
787 struct bt_ctf_field_type_integer
, parent
);
788 enumeration_type
= container_of(field
->type
,
789 struct bt_ctf_field_type_enumeration
, parent
);
791 if (!integer_type
->declaration
.signedness
) {
793 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
796 goto error_put_container_type
;
799 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
800 enumeration_type
, value
);
803 ret
= bt_ctf_field_signed_integer_get_value(container
,
806 goto error_put_container_type
;
809 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
810 enumeration_type
, value
);
813 error_put_container_type
:
814 bt_put(container_type
);
821 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
825 struct bt_ctf_field_integer
*integer
;
826 struct bt_ctf_field_type_integer
*integer_type
;
828 if (!field
|| !value
|| !field
->payload_set
||
829 bt_ctf_field_type_get_type_id(field
->type
) !=
835 integer_type
= container_of(field
->type
,
836 struct bt_ctf_field_type_integer
, parent
);
837 if (!integer_type
->declaration
.signedness
) {
842 integer
= container_of(field
,
843 struct bt_ctf_field_integer
, parent
);
844 *value
= integer
->definition
.value
._signed
;
849 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
853 struct bt_ctf_field_integer
*integer
;
854 struct bt_ctf_field_type_integer
*integer_type
;
856 int64_t min_value
, max_value
;
859 bt_ctf_field_type_get_type_id(field
->type
) !=
865 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
866 integer_type
= container_of(field
->type
,
867 struct bt_ctf_field_type_integer
, parent
);
868 if (!integer_type
->declaration
.signedness
) {
873 size
= integer_type
->declaration
.len
;
874 min_value
= -((int64_t)1 << (size
- 1));
875 max_value
= ((int64_t)1 << (size
- 1)) - 1;
876 if (value
< min_value
|| value
> max_value
) {
881 integer
->definition
.value
._signed
= value
;
882 integer
->parent
.payload_set
= 1;
887 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
891 struct bt_ctf_field_integer
*integer
;
892 struct bt_ctf_field_type_integer
*integer_type
;
894 if (!field
|| !value
|| !field
->payload_set
||
895 bt_ctf_field_type_get_type_id(field
->type
) !=
901 integer_type
= container_of(field
->type
,
902 struct bt_ctf_field_type_integer
, parent
);
903 if (integer_type
->declaration
.signedness
) {
908 integer
= container_of(field
,
909 struct bt_ctf_field_integer
, parent
);
910 *value
= integer
->definition
.value
._unsigned
;
915 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
919 struct bt_ctf_field_integer
*integer
;
920 struct bt_ctf_field_type_integer
*integer_type
;
925 bt_ctf_field_type_get_type_id(field
->type
) !=
931 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
932 integer_type
= container_of(field
->type
,
933 struct bt_ctf_field_type_integer
, parent
);
934 if (integer_type
->declaration
.signedness
) {
939 size
= integer_type
->declaration
.len
;
940 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
941 if (value
> max_value
) {
946 integer
->definition
.value
._unsigned
= value
;
947 integer
->parent
.payload_set
= 1;
952 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
956 struct bt_ctf_field_floating_point
*floating_point
;
958 if (!field
|| !value
|| !field
->payload_set
||
959 bt_ctf_field_type_get_type_id(field
->type
) !=
965 floating_point
= container_of(field
,
966 struct bt_ctf_field_floating_point
, parent
);
967 *value
= floating_point
->definition
.value
;
972 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
976 struct bt_ctf_field_floating_point
*floating_point
;
979 bt_ctf_field_type_get_type_id(field
->type
) !=
984 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
986 floating_point
->definition
.value
= value
;
987 floating_point
->parent
.payload_set
= 1;
992 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
994 const char *ret
= NULL
;
995 struct bt_ctf_field_string
*string
;
997 if (!field
|| !field
->payload_set
||
998 bt_ctf_field_type_get_type_id(field
->type
) !=
1003 string
= container_of(field
,
1004 struct bt_ctf_field_string
, parent
);
1005 ret
= string
->payload
->str
;
1010 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1014 struct bt_ctf_field_string
*string
;
1016 if (!field
|| !value
||
1017 bt_ctf_field_type_get_type_id(field
->type
) !=
1023 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1024 if (string
->payload
) {
1025 g_string_assign(string
->payload
, value
);
1027 string
->payload
= g_string_new(value
);
1030 string
->parent
.payload_set
= 1;
1035 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1039 struct bt_ctf_field_string
*string_field
;
1041 if (!field
|| !value
||
1042 bt_ctf_field_type_get_type_id(field
->type
) !=
1048 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1050 if (string_field
->payload
) {
1051 g_string_append(string_field
->payload
, value
);
1053 string_field
->payload
= g_string_new(value
);
1056 string_field
->parent
.payload_set
= 1;
1062 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1063 const char *value
, unsigned int length
)
1067 unsigned int effective_length
= length
;
1068 struct bt_ctf_field_string
*string_field
;
1070 if (!field
|| !value
||
1071 bt_ctf_field_type_get_type_id(field
->type
) !=
1077 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1079 /* make sure no null bytes are appended */
1080 for (i
= 0; i
< length
; ++i
) {
1081 if (value
[i
] == '\0') {
1082 effective_length
= i
;
1087 if (string_field
->payload
) {
1088 g_string_append_len(string_field
->payload
, value
,
1091 string_field
->payload
= g_string_new_len(value
,
1095 string_field
->parent
.payload_set
= 1;
1102 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1105 enum ctf_type_id type_id
;
1112 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1113 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1118 ret
= field_validate_funcs
[type_id
](field
);
1124 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1127 enum ctf_type_id type_id
;
1134 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1135 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1140 ret
= field_reset_funcs
[type_id
](field
);
1146 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1147 struct ctf_stream_pos
*pos
)
1150 enum ctf_type_id type_id
;
1152 if (!field
|| !pos
) {
1157 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1158 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1163 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1168 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1171 struct bt_ctf_field
*copy
= NULL
;
1172 enum ctf_type_id type_id
;
1178 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1179 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1183 copy
= bt_ctf_field_create(field
->type
);
1188 copy
->payload_set
= field
->payload_set
;
1189 ret
= field_copy_funcs
[type_id
](field
, copy
);
1199 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1201 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1202 struct bt_ctf_field_type_integer
, parent
);
1203 struct bt_ctf_field_integer
*integer
= g_new0(
1204 struct bt_ctf_field_integer
, 1);
1207 integer
->definition
.declaration
= &integer_type
->declaration
;
1210 return integer
? &integer
->parent
: NULL
;
1214 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1215 struct bt_ctf_field_type
*type
)
1217 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1218 struct bt_ctf_field_enumeration
, 1);
1220 return enumeration
? &enumeration
->parent
: NULL
;
1224 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1225 struct bt_ctf_field_type
*type
)
1227 struct bt_ctf_field_floating_point
*floating_point
;
1228 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1230 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1231 if (!floating_point
) {
1235 floating_point_type
= container_of(type
,
1236 struct bt_ctf_field_type_floating_point
, parent
);
1237 floating_point
->definition
.declaration
= container_of(
1238 type
->declaration
, struct declaration_float
, p
);
1241 floating_point
->definition
.sign
= &floating_point
->sign
;
1242 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1243 floating_point
->definition
.sign
->p
.declaration
=
1244 &floating_point_type
->sign
.p
;
1246 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1247 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1248 floating_point
->definition
.mantissa
->p
.declaration
=
1249 &floating_point_type
->mantissa
.p
;
1251 floating_point
->definition
.exp
= &floating_point
->exp
;
1252 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1253 floating_point
->definition
.exp
->p
.declaration
=
1254 &floating_point_type
->exp
.p
;
1257 return floating_point
? &floating_point
->parent
: NULL
;
1261 struct bt_ctf_field
*bt_ctf_field_structure_create(
1262 struct bt_ctf_field_type
*type
)
1264 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1265 struct bt_ctf_field_type_structure
, parent
);
1266 struct bt_ctf_field_structure
*structure
= g_new0(
1267 struct bt_ctf_field_structure
, 1);
1268 struct bt_ctf_field
*field
= NULL
;
1274 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1275 structure
->fields
= g_ptr_array_new_with_free_func(
1276 (GDestroyNotify
)bt_ctf_field_put
);
1277 g_ptr_array_set_size(structure
->fields
,
1278 g_hash_table_size(structure
->field_name_to_index
));
1279 field
= &structure
->parent
;
1285 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1287 struct bt_ctf_field_variant
*variant
= g_new0(
1288 struct bt_ctf_field_variant
, 1);
1289 return variant
? &variant
->parent
: NULL
;
1293 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1295 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1296 struct bt_ctf_field_type_array
*array_type
;
1297 unsigned int array_length
;
1299 if (!array
|| !type
) {
1303 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1304 array_length
= array_type
->length
;
1305 array
->elements
= g_ptr_array_sized_new(array_length
);
1306 if (!array
->elements
) {
1310 g_ptr_array_set_free_func(array
->elements
,
1311 (GDestroyNotify
)bt_ctf_field_put
);
1312 g_ptr_array_set_size(array
->elements
, array_length
);
1313 return &array
->parent
;
1320 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1321 struct bt_ctf_field_type
*type
)
1323 struct bt_ctf_field_sequence
*sequence
= g_new0(
1324 struct bt_ctf_field_sequence
, 1);
1325 return sequence
? &sequence
->parent
: NULL
;
1329 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1331 struct bt_ctf_field_string
*string
= g_new0(
1332 struct bt_ctf_field_string
, 1);
1333 return string
? &string
->parent
: NULL
;
1337 void bt_ctf_field_destroy(struct bt_object
*obj
)
1339 struct bt_ctf_field
*field
;
1340 struct bt_ctf_field_type
*type
;
1341 enum ctf_type_id type_id
;
1343 field
= container_of(obj
, struct bt_ctf_field
, base
);
1345 type_id
= bt_ctf_field_type_get_type_id(type
);
1346 if (type_id
<= CTF_TYPE_UNKNOWN
||
1347 type_id
>= NR_CTF_TYPES
) {
1351 field_destroy_funcs
[type_id
](field
);
1356 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1358 struct bt_ctf_field_integer
*integer
;
1364 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1369 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1371 struct bt_ctf_field_enumeration
*enumeration
;
1377 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1379 bt_put(enumeration
->payload
);
1380 g_free(enumeration
);
1384 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1386 struct bt_ctf_field_floating_point
*floating_point
;
1392 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1394 g_free(floating_point
);
1398 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1400 struct bt_ctf_field_structure
*structure
;
1406 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1407 g_ptr_array_free(structure
->fields
, TRUE
);
1412 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1414 struct bt_ctf_field_variant
*variant
;
1420 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1421 bt_put(variant
->tag
);
1422 bt_put(variant
->payload
);
1427 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1429 struct bt_ctf_field_array
*array
;
1435 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1436 g_ptr_array_free(array
->elements
, TRUE
);
1441 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1443 struct bt_ctf_field_sequence
*sequence
;
1449 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1450 if (sequence
->elements
) {
1451 g_ptr_array_free(sequence
->elements
, TRUE
);
1453 bt_put(sequence
->length
);
1458 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1460 struct bt_ctf_field_string
*string
;
1465 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1466 if (string
->payload
) {
1467 g_string_free(string
->payload
, TRUE
);
1473 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1475 return (field
&& field
->payload_set
) ? 0 : -1;
1479 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1482 struct bt_ctf_field_enumeration
*enumeration
;
1489 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1491 if (!enumeration
->payload
) {
1496 ret
= bt_ctf_field_validate(enumeration
->payload
);
1502 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1506 struct bt_ctf_field_structure
*structure
;
1513 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1514 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1515 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1525 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1528 struct bt_ctf_field_variant
*variant
;
1535 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1536 ret
= bt_ctf_field_validate(variant
->payload
);
1542 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1546 struct bt_ctf_field_array
*array
;
1553 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1554 for (i
= 0; i
< array
->elements
->len
; i
++) {
1555 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1565 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1569 struct bt_ctf_field_sequence
*sequence
;
1576 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1577 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1578 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1588 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1597 field
->payload_set
= 0;
1603 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1606 struct bt_ctf_field_enumeration
*enumeration
;
1613 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1615 if (!enumeration
->payload
) {
1619 ret
= bt_ctf_field_reset(enumeration
->payload
);
1625 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1629 struct bt_ctf_field_structure
*structure
;
1636 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1637 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1638 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1642 * Structure members are lazily initialized; skip if
1643 * this member has not been allocated yet.
1648 ret
= bt_ctf_field_reset(member
);
1658 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1661 struct bt_ctf_field_variant
*variant
;
1668 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1669 if (variant
->payload
) {
1670 ret
= bt_ctf_field_reset(variant
->payload
);
1677 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1681 struct bt_ctf_field_array
*array
;
1688 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1689 for (i
= 0; i
< array
->elements
->len
; i
++) {
1690 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1694 * Array elements are lazily initialized; skip if
1695 * this member has not been allocated yet.
1700 ret
= bt_ctf_field_reset(member
);
1710 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1714 struct bt_ctf_field_sequence
*sequence
;
1721 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1722 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1723 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1727 * Sequence elements are lazily initialized; skip if
1728 * this member has not been allocated yet.
1733 ret
= bt_ctf_field_reset(member
);
1743 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1746 struct bt_ctf_field_string
*string
;
1753 ret
= bt_ctf_field_generic_reset(field
);
1758 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1759 if (string
->payload
) {
1760 g_string_truncate(string
->payload
, 0);
1767 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1768 struct ctf_stream_pos
*pos
)
1771 struct bt_ctf_field_integer
*integer
= container_of(field
,
1772 struct bt_ctf_field_integer
, parent
);
1775 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1776 if (ret
== -EFAULT
) {
1778 * The field is too large to fit in the current packet's
1779 * remaining space. Bump the packet size and retry.
1781 ret
= increase_packet_size(pos
);
1792 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1793 struct ctf_stream_pos
*pos
)
1795 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1796 field
, struct bt_ctf_field_enumeration
, parent
);
1798 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1802 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1803 struct ctf_stream_pos
*pos
)
1806 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1807 struct bt_ctf_field_floating_point
, parent
);
1810 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1811 if (ret
== -EFAULT
) {
1813 * The field is too large to fit in the current packet's
1814 * remaining space. Bump the packet size and retry.
1816 ret
= increase_packet_size(pos
);
1827 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1828 struct ctf_stream_pos
*pos
)
1832 struct bt_ctf_field_structure
*structure
= container_of(
1833 field
, struct bt_ctf_field_structure
, parent
);
1835 while (!ctf_pos_access_ok(pos
,
1836 offset_align(pos
->offset
,
1837 field
->type
->declaration
->alignment
))) {
1838 ret
= increase_packet_size(pos
);
1844 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1849 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1850 struct bt_ctf_field
*field
= g_ptr_array_index(
1851 structure
->fields
, i
);
1853 ret
= bt_ctf_field_serialize(field
, pos
);
1863 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1864 struct ctf_stream_pos
*pos
)
1866 struct bt_ctf_field_variant
*variant
= container_of(
1867 field
, struct bt_ctf_field_variant
, parent
);
1869 return bt_ctf_field_serialize(variant
->payload
, pos
);
1873 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1874 struct ctf_stream_pos
*pos
)
1878 struct bt_ctf_field_array
*array
= container_of(
1879 field
, struct bt_ctf_field_array
, parent
);
1881 for (i
= 0; i
< array
->elements
->len
; i
++) {
1882 ret
= bt_ctf_field_serialize(
1883 g_ptr_array_index(array
->elements
, i
), pos
);
1893 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1894 struct ctf_stream_pos
*pos
)
1898 struct bt_ctf_field_sequence
*sequence
= container_of(
1899 field
, struct bt_ctf_field_sequence
, parent
);
1901 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1902 ret
= bt_ctf_field_serialize(
1903 g_ptr_array_index(sequence
->elements
, i
), pos
);
1913 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1914 struct ctf_stream_pos
*pos
)
1918 struct bt_ctf_field_string
*string
= container_of(field
,
1919 struct bt_ctf_field_string
, parent
);
1920 struct bt_ctf_field_type
*character_type
=
1921 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1922 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1924 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1925 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1926 (uint64_t) string
->payload
->str
[i
]);
1931 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1938 bt_put(character_type
);
1943 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1944 struct bt_ctf_field
*dst
)
1946 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1948 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1949 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1951 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1952 sizeof(struct definition_integer
));
1957 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1958 struct bt_ctf_field
*dst
)
1961 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1963 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1964 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1966 if (enum_src
->payload
) {
1967 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1968 if (!enum_dst
->payload
) {
1978 int bt_ctf_field_floating_point_copy(
1979 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1981 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1983 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1985 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1988 memcpy(&float_dst
->definition
, &float_src
->definition
,
1989 sizeof(struct definition_float
));
1990 memcpy(&float_dst
->sign
, &float_src
->sign
,
1991 sizeof(struct definition_integer
));
1992 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1993 sizeof(struct definition_integer
));
1994 memcpy(&float_dst
->exp
, &float_src
->exp
,
1995 sizeof(struct definition_integer
));
2000 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2001 struct bt_ctf_field
*dst
)
2004 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2006 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2007 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2009 /* This field_name_to_index HT is owned by the structure field type */
2010 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2011 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2013 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2014 struct bt_ctf_field
*field
=
2015 g_ptr_array_index(struct_src
->fields
, i
);
2016 struct bt_ctf_field
*field_copy
= NULL
;
2019 field_copy
= bt_ctf_field_copy(field
);
2027 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2034 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2035 struct bt_ctf_field
*dst
)
2038 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2040 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2041 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2043 if (variant_src
->tag
) {
2044 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2045 if (!variant_dst
->tag
) {
2050 if (variant_src
->payload
) {
2051 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2052 if (!variant_dst
->payload
) {
2062 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2063 struct bt_ctf_field
*dst
)
2066 struct bt_ctf_field_array
*array_src
, *array_dst
;
2068 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2069 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2071 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2072 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2073 struct bt_ctf_field
*field
=
2074 g_ptr_array_index(array_src
->elements
, i
);
2075 struct bt_ctf_field
*field_copy
= NULL
;
2078 field_copy
= bt_ctf_field_copy(field
);
2086 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2093 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2094 struct bt_ctf_field
*dst
)
2097 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2098 struct bt_ctf_field
*src_length
;
2099 struct bt_ctf_field
*dst_length
;
2101 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2102 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2104 src_length
= bt_ctf_field_sequence_get_length(src
);
2107 /* no length set yet: keep destination sequence empty */
2111 /* copy source length */
2112 dst_length
= bt_ctf_field_copy(src_length
);
2120 /* this will initialize the destination sequence's internal array */
2121 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2128 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2130 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2131 struct bt_ctf_field
*field
=
2132 g_ptr_array_index(sequence_src
->elements
, i
);
2133 struct bt_ctf_field
*field_copy
= NULL
;
2136 field_copy
= bt_ctf_field_copy(field
);
2144 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2151 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2152 struct bt_ctf_field
*dst
)
2155 struct bt_ctf_field_string
*string_src
, *string_dst
;
2157 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2158 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2160 if (string_src
->payload
) {
2161 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2162 if (!string_dst
->payload
) {
2172 int increase_packet_size(struct ctf_stream_pos
*pos
)
2177 ret
= munmap_align(pos
->base_mma
);
2182 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2184 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2185 pos
->packet_size
/ CHAR_BIT
);
2186 } while (ret
== EINTR
);
2193 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2194 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2195 if (pos
->base_mma
== MAP_FAILED
) {