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 assert(cur_tag_container
);
681 cur_tag_enum_integer
= container_of(cur_tag_container
,
682 struct bt_ctf_field_integer
, parent
);
683 bt_put(cur_tag_container
);
684 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
686 if (cur_tag_value
== tag_enum_value
) {
687 new_field
= variant
->payload
;
693 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
694 variant_type
, tag_enum_value
);
699 new_field
= bt_ctf_field_create(field_type
);
704 bt_put(variant
->tag
);
705 bt_put(variant
->payload
);
708 variant
->tag
= tag_field
;
709 variant
->payload
= new_field
;
715 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
716 struct bt_ctf_field
*variant_field
)
718 struct bt_ctf_field
*current_field
= NULL
;
719 struct bt_ctf_field_variant
*variant
;
721 if (!variant_field
||
722 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
727 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
730 if (variant
->payload
) {
731 current_field
= variant
->payload
;
732 bt_get(current_field
);
737 return current_field
;
740 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
741 struct bt_ctf_field
*field
)
743 struct bt_ctf_field
*container
= NULL
;
744 struct bt_ctf_field_enumeration
*enumeration
;
746 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
751 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
753 if (!enumeration
->payload
) {
754 struct bt_ctf_field_type_enumeration
*enumeration_type
=
755 container_of(field
->type
,
756 struct bt_ctf_field_type_enumeration
, parent
);
757 enumeration
->payload
=
758 bt_ctf_field_create(enumeration_type
->container
);
761 container
= enumeration
->payload
;
767 const char *bt_ctf_field_enumeration_get_mapping_name(
768 struct bt_ctf_field
*field
)
771 const char *name
= NULL
;
772 struct bt_ctf_field
*container
= NULL
;
773 struct bt_ctf_field_type
*container_type
= NULL
;
774 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
775 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
777 container
= bt_ctf_field_enumeration_get_container(field
);
782 container_type
= bt_ctf_field_get_type(container
);
783 if (!container_type
) {
784 goto error_put_container
;
787 integer_type
= container_of(container_type
,
788 struct bt_ctf_field_type_integer
, parent
);
789 enumeration_type
= container_of(field
->type
,
790 struct bt_ctf_field_type_enumeration
, parent
);
792 if (!integer_type
->declaration
.signedness
) {
794 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
797 goto error_put_container_type
;
800 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
801 enumeration_type
, value
);
804 ret
= bt_ctf_field_signed_integer_get_value(container
,
807 goto error_put_container_type
;
810 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
811 enumeration_type
, value
);
814 error_put_container_type
:
815 bt_put(container_type
);
822 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
826 struct bt_ctf_field_integer
*integer
;
827 struct bt_ctf_field_type_integer
*integer_type
;
829 if (!field
|| !value
|| !field
->payload_set
||
830 bt_ctf_field_type_get_type_id(field
->type
) !=
836 integer_type
= container_of(field
->type
,
837 struct bt_ctf_field_type_integer
, parent
);
838 if (!integer_type
->declaration
.signedness
) {
843 integer
= container_of(field
,
844 struct bt_ctf_field_integer
, parent
);
845 *value
= integer
->definition
.value
._signed
;
850 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
854 struct bt_ctf_field_integer
*integer
;
855 struct bt_ctf_field_type_integer
*integer_type
;
857 int64_t min_value
, max_value
;
860 bt_ctf_field_type_get_type_id(field
->type
) !=
866 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
867 integer_type
= container_of(field
->type
,
868 struct bt_ctf_field_type_integer
, parent
);
869 if (!integer_type
->declaration
.signedness
) {
874 size
= integer_type
->declaration
.len
;
875 min_value
= -((int64_t)1 << (size
- 1));
876 max_value
= ((int64_t)1 << (size
- 1)) - 1;
877 if (value
< min_value
|| value
> max_value
) {
882 integer
->definition
.value
._signed
= value
;
883 integer
->parent
.payload_set
= 1;
888 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
892 struct bt_ctf_field_integer
*integer
;
893 struct bt_ctf_field_type_integer
*integer_type
;
895 if (!field
|| !value
|| !field
->payload_set
||
896 bt_ctf_field_type_get_type_id(field
->type
) !=
902 integer_type
= container_of(field
->type
,
903 struct bt_ctf_field_type_integer
, parent
);
904 if (integer_type
->declaration
.signedness
) {
909 integer
= container_of(field
,
910 struct bt_ctf_field_integer
, parent
);
911 *value
= integer
->definition
.value
._unsigned
;
916 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
920 struct bt_ctf_field_integer
*integer
;
921 struct bt_ctf_field_type_integer
*integer_type
;
926 bt_ctf_field_type_get_type_id(field
->type
) !=
932 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
933 integer_type
= container_of(field
->type
,
934 struct bt_ctf_field_type_integer
, parent
);
935 if (integer_type
->declaration
.signedness
) {
940 size
= integer_type
->declaration
.len
;
941 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
942 if (value
> max_value
) {
947 integer
->definition
.value
._unsigned
= value
;
948 integer
->parent
.payload_set
= 1;
953 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
957 struct bt_ctf_field_floating_point
*floating_point
;
959 if (!field
|| !value
|| !field
->payload_set
||
960 bt_ctf_field_type_get_type_id(field
->type
) !=
966 floating_point
= container_of(field
,
967 struct bt_ctf_field_floating_point
, parent
);
968 *value
= floating_point
->definition
.value
;
973 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
977 struct bt_ctf_field_floating_point
*floating_point
;
980 bt_ctf_field_type_get_type_id(field
->type
) !=
985 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
987 floating_point
->definition
.value
= value
;
988 floating_point
->parent
.payload_set
= 1;
993 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
995 const char *ret
= NULL
;
996 struct bt_ctf_field_string
*string
;
998 if (!field
|| !field
->payload_set
||
999 bt_ctf_field_type_get_type_id(field
->type
) !=
1004 string
= container_of(field
,
1005 struct bt_ctf_field_string
, parent
);
1006 ret
= string
->payload
->str
;
1011 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1015 struct bt_ctf_field_string
*string
;
1017 if (!field
|| !value
||
1018 bt_ctf_field_type_get_type_id(field
->type
) !=
1024 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1025 if (string
->payload
) {
1026 g_string_assign(string
->payload
, value
);
1028 string
->payload
= g_string_new(value
);
1031 string
->parent
.payload_set
= 1;
1036 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1040 struct bt_ctf_field_string
*string_field
;
1042 if (!field
|| !value
||
1043 bt_ctf_field_type_get_type_id(field
->type
) !=
1049 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1051 if (string_field
->payload
) {
1052 g_string_append(string_field
->payload
, value
);
1054 string_field
->payload
= g_string_new(value
);
1057 string_field
->parent
.payload_set
= 1;
1063 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1064 const char *value
, unsigned int length
)
1068 unsigned int effective_length
= length
;
1069 struct bt_ctf_field_string
*string_field
;
1071 if (!field
|| !value
||
1072 bt_ctf_field_type_get_type_id(field
->type
) !=
1078 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1080 /* make sure no null bytes are appended */
1081 for (i
= 0; i
< length
; ++i
) {
1082 if (value
[i
] == '\0') {
1083 effective_length
= i
;
1088 if (string_field
->payload
) {
1089 g_string_append_len(string_field
->payload
, value
,
1092 string_field
->payload
= g_string_new_len(value
,
1096 string_field
->parent
.payload_set
= 1;
1103 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1106 enum ctf_type_id type_id
;
1113 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1114 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1119 ret
= field_validate_funcs
[type_id
](field
);
1125 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1128 enum ctf_type_id type_id
;
1135 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1136 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1141 ret
= field_reset_funcs
[type_id
](field
);
1147 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1148 struct ctf_stream_pos
*pos
)
1151 enum ctf_type_id type_id
;
1153 if (!field
|| !pos
) {
1158 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1159 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1164 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1169 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1172 struct bt_ctf_field
*copy
= NULL
;
1173 enum ctf_type_id type_id
;
1179 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1180 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1184 copy
= bt_ctf_field_create(field
->type
);
1189 copy
->payload_set
= field
->payload_set
;
1190 ret
= field_copy_funcs
[type_id
](field
, copy
);
1200 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1202 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1203 struct bt_ctf_field_type_integer
, parent
);
1204 struct bt_ctf_field_integer
*integer
= g_new0(
1205 struct bt_ctf_field_integer
, 1);
1208 integer
->definition
.declaration
= &integer_type
->declaration
;
1211 return integer
? &integer
->parent
: NULL
;
1215 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1216 struct bt_ctf_field_type
*type
)
1218 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1219 struct bt_ctf_field_enumeration
, 1);
1221 return enumeration
? &enumeration
->parent
: NULL
;
1225 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1226 struct bt_ctf_field_type
*type
)
1228 struct bt_ctf_field_floating_point
*floating_point
;
1229 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1231 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1232 if (!floating_point
) {
1236 floating_point_type
= container_of(type
,
1237 struct bt_ctf_field_type_floating_point
, parent
);
1238 floating_point
->definition
.declaration
= container_of(
1239 type
->declaration
, struct declaration_float
, p
);
1242 floating_point
->definition
.sign
= &floating_point
->sign
;
1243 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1244 floating_point
->definition
.sign
->p
.declaration
=
1245 &floating_point_type
->sign
.p
;
1247 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1248 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1249 floating_point
->definition
.mantissa
->p
.declaration
=
1250 &floating_point_type
->mantissa
.p
;
1252 floating_point
->definition
.exp
= &floating_point
->exp
;
1253 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1254 floating_point
->definition
.exp
->p
.declaration
=
1255 &floating_point_type
->exp
.p
;
1258 return floating_point
? &floating_point
->parent
: NULL
;
1262 struct bt_ctf_field
*bt_ctf_field_structure_create(
1263 struct bt_ctf_field_type
*type
)
1265 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1266 struct bt_ctf_field_type_structure
, parent
);
1267 struct bt_ctf_field_structure
*structure
= g_new0(
1268 struct bt_ctf_field_structure
, 1);
1269 struct bt_ctf_field
*field
= NULL
;
1275 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1276 structure
->fields
= g_ptr_array_new_with_free_func(
1277 (GDestroyNotify
)bt_ctf_field_put
);
1278 g_ptr_array_set_size(structure
->fields
,
1279 g_hash_table_size(structure
->field_name_to_index
));
1280 field
= &structure
->parent
;
1286 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1288 struct bt_ctf_field_variant
*variant
= g_new0(
1289 struct bt_ctf_field_variant
, 1);
1290 return variant
? &variant
->parent
: NULL
;
1294 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1296 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1297 struct bt_ctf_field_type_array
*array_type
;
1298 unsigned int array_length
;
1300 if (!array
|| !type
) {
1304 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1305 array_length
= array_type
->length
;
1306 array
->elements
= g_ptr_array_sized_new(array_length
);
1307 if (!array
->elements
) {
1311 g_ptr_array_set_free_func(array
->elements
,
1312 (GDestroyNotify
)bt_ctf_field_put
);
1313 g_ptr_array_set_size(array
->elements
, array_length
);
1314 return &array
->parent
;
1321 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1322 struct bt_ctf_field_type
*type
)
1324 struct bt_ctf_field_sequence
*sequence
= g_new0(
1325 struct bt_ctf_field_sequence
, 1);
1326 return sequence
? &sequence
->parent
: NULL
;
1330 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1332 struct bt_ctf_field_string
*string
= g_new0(
1333 struct bt_ctf_field_string
, 1);
1334 return string
? &string
->parent
: NULL
;
1338 void bt_ctf_field_destroy(struct bt_object
*obj
)
1340 struct bt_ctf_field
*field
;
1341 struct bt_ctf_field_type
*type
;
1342 enum ctf_type_id type_id
;
1344 field
= container_of(obj
, struct bt_ctf_field
, base
);
1346 type_id
= bt_ctf_field_type_get_type_id(type
);
1347 if (type_id
<= CTF_TYPE_UNKNOWN
||
1348 type_id
>= NR_CTF_TYPES
) {
1352 field_destroy_funcs
[type_id
](field
);
1357 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1359 struct bt_ctf_field_integer
*integer
;
1365 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1370 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1372 struct bt_ctf_field_enumeration
*enumeration
;
1378 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1380 bt_put(enumeration
->payload
);
1381 g_free(enumeration
);
1385 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1387 struct bt_ctf_field_floating_point
*floating_point
;
1393 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1395 g_free(floating_point
);
1399 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1401 struct bt_ctf_field_structure
*structure
;
1407 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1408 g_ptr_array_free(structure
->fields
, TRUE
);
1413 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1415 struct bt_ctf_field_variant
*variant
;
1421 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1422 bt_put(variant
->tag
);
1423 bt_put(variant
->payload
);
1428 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1430 struct bt_ctf_field_array
*array
;
1436 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1437 g_ptr_array_free(array
->elements
, TRUE
);
1442 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1444 struct bt_ctf_field_sequence
*sequence
;
1450 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1451 if (sequence
->elements
) {
1452 g_ptr_array_free(sequence
->elements
, TRUE
);
1454 bt_put(sequence
->length
);
1459 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1461 struct bt_ctf_field_string
*string
;
1466 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1467 if (string
->payload
) {
1468 g_string_free(string
->payload
, TRUE
);
1474 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1476 return (field
&& field
->payload_set
) ? 0 : -1;
1480 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1483 struct bt_ctf_field_enumeration
*enumeration
;
1490 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1492 if (!enumeration
->payload
) {
1497 ret
= bt_ctf_field_validate(enumeration
->payload
);
1503 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1507 struct bt_ctf_field_structure
*structure
;
1514 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1515 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1516 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1526 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1529 struct bt_ctf_field_variant
*variant
;
1536 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1537 ret
= bt_ctf_field_validate(variant
->payload
);
1543 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1547 struct bt_ctf_field_array
*array
;
1554 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1555 for (i
= 0; i
< array
->elements
->len
; i
++) {
1556 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1566 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1570 struct bt_ctf_field_sequence
*sequence
;
1577 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1578 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1579 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1589 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1598 field
->payload_set
= 0;
1604 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1607 struct bt_ctf_field_enumeration
*enumeration
;
1614 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1616 if (!enumeration
->payload
) {
1620 ret
= bt_ctf_field_reset(enumeration
->payload
);
1626 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1630 struct bt_ctf_field_structure
*structure
;
1637 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1638 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1639 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1643 * Structure members are lazily initialized; skip if
1644 * this member has not been allocated yet.
1649 ret
= bt_ctf_field_reset(member
);
1659 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1662 struct bt_ctf_field_variant
*variant
;
1669 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1670 if (variant
->payload
) {
1671 ret
= bt_ctf_field_reset(variant
->payload
);
1678 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1682 struct bt_ctf_field_array
*array
;
1689 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1690 for (i
= 0; i
< array
->elements
->len
; i
++) {
1691 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1695 * Array elements are lazily initialized; skip if
1696 * this member has not been allocated yet.
1701 ret
= bt_ctf_field_reset(member
);
1711 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1715 struct bt_ctf_field_sequence
*sequence
;
1722 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1723 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1724 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1728 * Sequence elements are lazily initialized; skip if
1729 * this member has not been allocated yet.
1734 ret
= bt_ctf_field_reset(member
);
1744 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1747 struct bt_ctf_field_string
*string
;
1754 ret
= bt_ctf_field_generic_reset(field
);
1759 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1760 if (string
->payload
) {
1761 g_string_truncate(string
->payload
, 0);
1768 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1769 struct ctf_stream_pos
*pos
)
1772 struct bt_ctf_field_integer
*integer
= container_of(field
,
1773 struct bt_ctf_field_integer
, parent
);
1776 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1777 if (ret
== -EFAULT
) {
1779 * The field is too large to fit in the current packet's
1780 * remaining space. Bump the packet size and retry.
1782 ret
= increase_packet_size(pos
);
1793 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1794 struct ctf_stream_pos
*pos
)
1796 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1797 field
, struct bt_ctf_field_enumeration
, parent
);
1799 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1803 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1804 struct ctf_stream_pos
*pos
)
1807 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1808 struct bt_ctf_field_floating_point
, parent
);
1811 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1812 if (ret
== -EFAULT
) {
1814 * The field is too large to fit in the current packet's
1815 * remaining space. Bump the packet size and retry.
1817 ret
= increase_packet_size(pos
);
1828 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1829 struct ctf_stream_pos
*pos
)
1833 struct bt_ctf_field_structure
*structure
= container_of(
1834 field
, struct bt_ctf_field_structure
, parent
);
1836 while (!ctf_pos_access_ok(pos
,
1837 offset_align(pos
->offset
,
1838 field
->type
->declaration
->alignment
))) {
1839 ret
= increase_packet_size(pos
);
1845 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1850 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1851 struct bt_ctf_field
*field
= g_ptr_array_index(
1852 structure
->fields
, i
);
1854 ret
= bt_ctf_field_serialize(field
, pos
);
1864 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1865 struct ctf_stream_pos
*pos
)
1867 struct bt_ctf_field_variant
*variant
= container_of(
1868 field
, struct bt_ctf_field_variant
, parent
);
1870 return bt_ctf_field_serialize(variant
->payload
, pos
);
1874 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1875 struct ctf_stream_pos
*pos
)
1879 struct bt_ctf_field_array
*array
= container_of(
1880 field
, struct bt_ctf_field_array
, parent
);
1882 for (i
= 0; i
< array
->elements
->len
; i
++) {
1883 ret
= bt_ctf_field_serialize(
1884 g_ptr_array_index(array
->elements
, i
), pos
);
1894 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1895 struct ctf_stream_pos
*pos
)
1899 struct bt_ctf_field_sequence
*sequence
= container_of(
1900 field
, struct bt_ctf_field_sequence
, parent
);
1902 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1903 ret
= bt_ctf_field_serialize(
1904 g_ptr_array_index(sequence
->elements
, i
), pos
);
1914 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1915 struct ctf_stream_pos
*pos
)
1919 struct bt_ctf_field_string
*string
= container_of(field
,
1920 struct bt_ctf_field_string
, parent
);
1921 struct bt_ctf_field_type
*character_type
=
1922 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1923 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1925 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1926 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1927 (uint64_t) string
->payload
->str
[i
]);
1932 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1939 bt_put(character_type
);
1944 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1945 struct bt_ctf_field
*dst
)
1947 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1949 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1950 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1952 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1953 sizeof(struct definition_integer
));
1958 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1959 struct bt_ctf_field
*dst
)
1962 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1964 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1965 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1967 if (enum_src
->payload
) {
1968 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1969 if (!enum_dst
->payload
) {
1979 int bt_ctf_field_floating_point_copy(
1980 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1982 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1984 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1986 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1989 memcpy(&float_dst
->definition
, &float_src
->definition
,
1990 sizeof(struct definition_float
));
1991 memcpy(&float_dst
->sign
, &float_src
->sign
,
1992 sizeof(struct definition_integer
));
1993 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1994 sizeof(struct definition_integer
));
1995 memcpy(&float_dst
->exp
, &float_src
->exp
,
1996 sizeof(struct definition_integer
));
2001 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2002 struct bt_ctf_field
*dst
)
2005 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2007 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2008 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2010 /* This field_name_to_index HT is owned by the structure field type */
2011 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2012 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2014 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2015 struct bt_ctf_field
*field
=
2016 g_ptr_array_index(struct_src
->fields
, i
);
2017 struct bt_ctf_field
*field_copy
= NULL
;
2020 field_copy
= bt_ctf_field_copy(field
);
2028 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2035 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2036 struct bt_ctf_field
*dst
)
2039 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2041 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2042 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2044 if (variant_src
->tag
) {
2045 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2046 if (!variant_dst
->tag
) {
2051 if (variant_src
->payload
) {
2052 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2053 if (!variant_dst
->payload
) {
2063 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2064 struct bt_ctf_field
*dst
)
2067 struct bt_ctf_field_array
*array_src
, *array_dst
;
2069 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2070 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2072 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2073 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2074 struct bt_ctf_field
*field
=
2075 g_ptr_array_index(array_src
->elements
, i
);
2076 struct bt_ctf_field
*field_copy
= NULL
;
2079 field_copy
= bt_ctf_field_copy(field
);
2087 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2094 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2095 struct bt_ctf_field
*dst
)
2098 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2099 struct bt_ctf_field
*src_length
;
2100 struct bt_ctf_field
*dst_length
;
2102 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2103 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2105 src_length
= bt_ctf_field_sequence_get_length(src
);
2108 /* no length set yet: keep destination sequence empty */
2112 /* copy source length */
2113 dst_length
= bt_ctf_field_copy(src_length
);
2121 /* this will initialize the destination sequence's internal array */
2122 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2129 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2131 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2132 struct bt_ctf_field
*field
=
2133 g_ptr_array_index(sequence_src
->elements
, i
);
2134 struct bt_ctf_field
*field_copy
= NULL
;
2137 field_copy
= bt_ctf_field_copy(field
);
2145 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2152 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2153 struct bt_ctf_field
*dst
)
2156 struct bt_ctf_field_string
*string_src
, *string_dst
;
2158 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2159 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2161 if (string_src
->payload
) {
2162 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2163 if (!string_dst
->payload
) {
2173 int increase_packet_size(struct ctf_stream_pos
*pos
)
2178 ret
= munmap_align(pos
->base_mma
);
2183 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2185 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2186 pos
->packet_size
/ CHAR_BIT
);
2187 } while (ret
== EINTR
);
2194 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2195 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2196 if (pos
->base_mma
== MAP_FAILED
) {