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
;
242 type_id
= bt_ctf_field_type_get_type_id(type
);
243 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
||
244 bt_ctf_field_type_validate(type
)) {
248 field
= field_create_funcs
[type_id
](type
);
253 /* The type's declaration can't change after this point */
254 bt_ctf_field_type_freeze(type
);
256 bt_object_init(field
, bt_ctf_field_destroy
);
262 void bt_ctf_field_get(struct bt_ctf_field
*field
)
267 void bt_ctf_field_put(struct bt_ctf_field
*field
)
272 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
274 struct bt_ctf_field_type
*ret
= NULL
;
286 enum ctf_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
288 enum ctf_type_id ret
= CTF_TYPE_UNKNOWN
;
294 ret
= bt_ctf_field_type_get_type_id(field
->type
);
299 int bt_ctf_field_is_integer(struct bt_ctf_field
*field
)
301 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_INTEGER
;
304 int bt_ctf_field_is_floating_point(struct bt_ctf_field
*field
)
306 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_FLOAT
;
309 int bt_ctf_field_is_enumeration(struct bt_ctf_field
*field
)
311 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_ENUM
;
314 int bt_ctf_field_is_string(struct bt_ctf_field
*field
)
316 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_STRING
;
319 int bt_ctf_field_is_structure(struct bt_ctf_field
*field
)
321 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_STRUCT
;
324 int bt_ctf_field_is_array(struct bt_ctf_field
*field
)
326 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_ARRAY
;
329 int bt_ctf_field_is_sequence(struct bt_ctf_field
*field
)
331 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_SEQUENCE
;
334 int bt_ctf_field_is_variant(struct bt_ctf_field
*field
)
336 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_VARIANT
;
339 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
340 struct bt_ctf_field
*field
)
342 struct bt_ctf_field
*ret
= NULL
;
343 struct bt_ctf_field_sequence
*sequence
;
349 if (bt_ctf_field_type_get_type_id(field
->type
) !=
354 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
355 ret
= sequence
->length
;
361 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
362 struct bt_ctf_field
*length_field
)
365 struct bt_ctf_field_type_integer
*length_type
;
366 struct bt_ctf_field_integer
*length
;
367 struct bt_ctf_field_sequence
*sequence
;
368 uint64_t sequence_length
;
370 if (!field
|| !length_field
) {
374 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
380 length_type
= container_of(length_field
->type
,
381 struct bt_ctf_field_type_integer
, parent
);
382 /* The length field must be unsigned */
383 if (length_type
->declaration
.signedness
) {
388 length
= container_of(length_field
, struct bt_ctf_field_integer
,
390 sequence_length
= length
->definition
.value
._unsigned
;
391 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
392 if (sequence
->elements
) {
393 g_ptr_array_free(sequence
->elements
, TRUE
);
394 bt_put(sequence
->length
);
397 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
398 if (!sequence
->elements
) {
403 g_ptr_array_set_free_func(sequence
->elements
,
404 (GDestroyNotify
) bt_put
);
405 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
406 bt_get(length_field
);
407 sequence
->length
= length_field
;
412 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
413 struct bt_ctf_field
*field
, const char *name
)
415 struct bt_ctf_field
*new_field
= NULL
;
417 struct bt_ctf_field_structure
*structure
;
418 struct bt_ctf_field_type
*field_type
= NULL
;
421 if (!field
|| !name
||
422 bt_ctf_field_type_get_type_id(field
->type
) !=
427 field_quark
= g_quark_from_string(name
);
428 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
430 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
432 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
433 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
437 if (structure
->fields
->pdata
[index
]) {
438 new_field
= structure
->fields
->pdata
[index
];
442 new_field
= bt_ctf_field_create(field_type
);
447 structure
->fields
->pdata
[index
] = new_field
;
457 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
458 struct bt_ctf_field
*field
, int index
)
461 const char *field_name
;
462 struct bt_ctf_field_structure
*structure
;
463 struct bt_ctf_field_type
*structure_type
;
464 struct bt_ctf_field_type
*field_type
= NULL
;
465 struct bt_ctf_field
*ret_field
= NULL
;
468 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
472 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
473 if (index
>= structure
->fields
->len
) {
477 ret_field
= structure
->fields
->pdata
[index
];
482 /* Field has not been instanciated yet, create it */
483 structure_type
= bt_ctf_field_get_type(field
);
484 if (!structure_type
) {
488 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
489 &field_name
, &field_type
, index
);
490 bt_put(structure_type
);
495 ret_field
= bt_ctf_field_create(field_type
);
500 structure
->fields
->pdata
[index
] = ret_field
;
509 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
510 const char *name
, struct bt_ctf_field
*value
)
514 struct bt_ctf_field_structure
*structure
;
515 struct bt_ctf_field_type
*expected_field_type
= NULL
;
518 if (!field
|| !name
|| !value
||
519 bt_ctf_field_type_get_type_id(field
->type
) !=
525 field_quark
= g_quark_from_string(name
);
526 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
527 expected_field_type
=
528 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
530 if (expected_field_type
!= value
->type
) {
535 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
536 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
540 if (structure
->fields
->pdata
[index
]) {
541 bt_put(structure
->fields
->pdata
[index
]);
544 structure
->fields
->pdata
[index
] = value
;
547 if (expected_field_type
) {
548 bt_put(expected_field_type
);
553 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
556 struct bt_ctf_field
*new_field
= NULL
;
557 struct bt_ctf_field_type
*field_type
= NULL
;
558 struct bt_ctf_field_array
*array
;
560 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
565 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
566 if (index
>= array
->elements
->len
) {
570 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
571 if (array
->elements
->pdata
[(size_t)index
]) {
572 new_field
= array
->elements
->pdata
[(size_t)index
];
576 new_field
= bt_ctf_field_create(field_type
);
577 array
->elements
->pdata
[(size_t)index
] = new_field
;
588 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
591 struct bt_ctf_field
*new_field
= NULL
;
592 struct bt_ctf_field_type
*field_type
= NULL
;
593 struct bt_ctf_field_sequence
*sequence
;
595 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
600 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
601 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
605 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
606 if (sequence
->elements
->pdata
[(size_t) index
]) {
607 new_field
= sequence
->elements
->pdata
[(size_t) index
];
611 new_field
= bt_ctf_field_create(field_type
);
612 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
623 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
624 struct bt_ctf_field
*tag_field
)
626 struct bt_ctf_field
*new_field
= NULL
;
627 struct bt_ctf_field_variant
*variant
;
628 struct bt_ctf_field_type_variant
*variant_type
;
629 struct bt_ctf_field_type
*field_type
;
630 struct bt_ctf_field
*tag_enum
= NULL
;
631 struct bt_ctf_field_integer
*tag_enum_integer
;
632 int64_t tag_enum_value
;
634 if (!field
|| !tag_field
||
635 bt_ctf_field_type_get_type_id(field
->type
) !=
637 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
642 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
643 variant_type
= container_of(field
->type
,
644 struct bt_ctf_field_type_variant
, parent
);
645 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
650 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
653 if (bt_ctf_field_validate(tag_field
) < 0) {
657 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
660 * If the variant currently has a tag and a payload, and if the
661 * requested tag value is the same as the current one, return
662 * the current payload instead of creating a fresh one.
664 if (variant
->tag
&& variant
->payload
) {
665 struct bt_ctf_field
*cur_tag_container
= NULL
;
666 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
667 int64_t cur_tag_value
;
670 bt_ctf_field_enumeration_get_container(variant
->tag
);
671 cur_tag_enum_integer
= container_of(cur_tag_container
,
672 struct bt_ctf_field_integer
, parent
);
673 bt_put(cur_tag_container
);
674 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
676 if (cur_tag_value
== tag_enum_value
) {
677 new_field
= variant
->payload
;
683 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
684 variant_type
, tag_enum_value
);
689 new_field
= bt_ctf_field_create(field_type
);
694 bt_put(variant
->tag
);
695 bt_put(variant
->payload
);
698 variant
->tag
= tag_field
;
699 variant
->payload
= new_field
;
705 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
706 struct bt_ctf_field
*variant_field
)
708 struct bt_ctf_field
*current_field
= NULL
;
709 struct bt_ctf_field_variant
*variant
;
711 if (!variant_field
||
712 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
717 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
720 if (variant
->payload
) {
721 current_field
= variant
->payload
;
722 bt_get(current_field
);
727 return current_field
;
730 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
731 struct bt_ctf_field
*field
)
733 struct bt_ctf_field
*container
= NULL
;
734 struct bt_ctf_field_enumeration
*enumeration
;
736 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
741 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
743 if (!enumeration
->payload
) {
744 struct bt_ctf_field_type_enumeration
*enumeration_type
=
745 container_of(field
->type
,
746 struct bt_ctf_field_type_enumeration
, parent
);
747 enumeration
->payload
=
748 bt_ctf_field_create(enumeration_type
->container
);
751 container
= enumeration
->payload
;
757 const char *bt_ctf_field_enumeration_get_mapping_name(
758 struct bt_ctf_field
*field
)
761 const char *name
= NULL
;
762 struct bt_ctf_field
*container
= NULL
;
763 struct bt_ctf_field_type
*container_type
= NULL
;
764 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
765 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
767 container
= bt_ctf_field_enumeration_get_container(field
);
772 container_type
= bt_ctf_field_get_type(container
);
773 if (!container_type
) {
774 goto error_put_container
;
777 integer_type
= container_of(container_type
,
778 struct bt_ctf_field_type_integer
, parent
);
779 enumeration_type
= container_of(field
->type
,
780 struct bt_ctf_field_type_enumeration
, parent
);
782 if (!integer_type
->declaration
.signedness
) {
784 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
787 goto error_put_container_type
;
790 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
791 enumeration_type
, value
);
794 ret
= bt_ctf_field_signed_integer_get_value(container
,
797 goto error_put_container_type
;
800 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
801 enumeration_type
, value
);
804 error_put_container_type
:
805 bt_put(container_type
);
812 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
816 struct bt_ctf_field_integer
*integer
;
817 struct bt_ctf_field_type_integer
*integer_type
;
819 if (!field
|| !value
|| !field
->payload_set
||
820 bt_ctf_field_type_get_type_id(field
->type
) !=
826 integer_type
= container_of(field
->type
,
827 struct bt_ctf_field_type_integer
, parent
);
828 if (!integer_type
->declaration
.signedness
) {
833 integer
= container_of(field
,
834 struct bt_ctf_field_integer
, parent
);
835 *value
= integer
->definition
.value
._signed
;
840 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
844 struct bt_ctf_field_integer
*integer
;
845 struct bt_ctf_field_type_integer
*integer_type
;
847 int64_t min_value
, max_value
;
850 bt_ctf_field_type_get_type_id(field
->type
) !=
856 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
857 integer_type
= container_of(field
->type
,
858 struct bt_ctf_field_type_integer
, parent
);
859 if (!integer_type
->declaration
.signedness
) {
864 size
= integer_type
->declaration
.len
;
865 min_value
= -((int64_t)1 << (size
- 1));
866 max_value
= ((int64_t)1 << (size
- 1)) - 1;
867 if (value
< min_value
|| value
> max_value
) {
872 integer
->definition
.value
._signed
= value
;
873 integer
->parent
.payload_set
= 1;
878 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
882 struct bt_ctf_field_integer
*integer
;
883 struct bt_ctf_field_type_integer
*integer_type
;
885 if (!field
|| !value
|| !field
->payload_set
||
886 bt_ctf_field_type_get_type_id(field
->type
) !=
892 integer_type
= container_of(field
->type
,
893 struct bt_ctf_field_type_integer
, parent
);
894 if (integer_type
->declaration
.signedness
) {
899 integer
= container_of(field
,
900 struct bt_ctf_field_integer
, parent
);
901 *value
= integer
->definition
.value
._unsigned
;
906 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
910 struct bt_ctf_field_integer
*integer
;
911 struct bt_ctf_field_type_integer
*integer_type
;
916 bt_ctf_field_type_get_type_id(field
->type
) !=
922 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
923 integer_type
= container_of(field
->type
,
924 struct bt_ctf_field_type_integer
, parent
);
925 if (integer_type
->declaration
.signedness
) {
930 size
= integer_type
->declaration
.len
;
931 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
932 if (value
> max_value
) {
937 integer
->definition
.value
._unsigned
= value
;
938 integer
->parent
.payload_set
= 1;
943 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
947 struct bt_ctf_field_floating_point
*floating_point
;
949 if (!field
|| !value
|| !field
->payload_set
||
950 bt_ctf_field_type_get_type_id(field
->type
) !=
956 floating_point
= container_of(field
,
957 struct bt_ctf_field_floating_point
, parent
);
958 *value
= floating_point
->definition
.value
;
963 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
967 struct bt_ctf_field_floating_point
*floating_point
;
970 bt_ctf_field_type_get_type_id(field
->type
) !=
975 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
977 floating_point
->definition
.value
= value
;
978 floating_point
->parent
.payload_set
= 1;
983 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
985 const char *ret
= NULL
;
986 struct bt_ctf_field_string
*string
;
988 if (!field
|| !field
->payload_set
||
989 bt_ctf_field_type_get_type_id(field
->type
) !=
994 string
= container_of(field
,
995 struct bt_ctf_field_string
, parent
);
996 ret
= string
->payload
->str
;
1001 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1005 struct bt_ctf_field_string
*string
;
1007 if (!field
|| !value
||
1008 bt_ctf_field_type_get_type_id(field
->type
) !=
1014 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1015 if (string
->payload
) {
1016 g_string_assign(string
->payload
, value
);
1018 string
->payload
= g_string_new(value
);
1021 string
->parent
.payload_set
= 1;
1026 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1030 struct bt_ctf_field_string
*string_field
;
1032 if (!field
|| !value
||
1033 bt_ctf_field_type_get_type_id(field
->type
) !=
1039 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1041 if (string_field
->payload
) {
1042 g_string_append(string_field
->payload
, value
);
1044 string_field
->payload
= g_string_new(value
);
1047 string_field
->parent
.payload_set
= 1;
1053 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1054 const char *value
, unsigned int length
)
1058 unsigned int effective_length
= length
;
1059 struct bt_ctf_field_string
*string_field
;
1061 if (!field
|| !value
||
1062 bt_ctf_field_type_get_type_id(field
->type
) !=
1068 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1070 /* make sure no null bytes are appended */
1071 for (i
= 0; i
< length
; ++i
) {
1072 if (value
[i
] == '\0') {
1073 effective_length
= i
;
1078 if (string_field
->payload
) {
1079 g_string_append_len(string_field
->payload
, value
,
1082 string_field
->payload
= g_string_new_len(value
,
1086 string_field
->parent
.payload_set
= 1;
1093 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1096 enum ctf_type_id type_id
;
1103 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1104 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1109 ret
= field_validate_funcs
[type_id
](field
);
1115 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1118 enum ctf_type_id type_id
;
1125 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1126 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1131 ret
= field_reset_funcs
[type_id
](field
);
1137 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1138 struct ctf_stream_pos
*pos
)
1141 enum ctf_type_id type_id
;
1143 if (!field
|| !pos
) {
1148 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1149 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1154 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1159 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1162 struct bt_ctf_field
*copy
= NULL
;
1163 enum ctf_type_id type_id
;
1169 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1170 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1174 copy
= bt_ctf_field_create(field
->type
);
1179 copy
->payload_set
= field
->payload_set
;
1180 ret
= field_copy_funcs
[type_id
](field
, copy
);
1190 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1192 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1193 struct bt_ctf_field_type_integer
, parent
);
1194 struct bt_ctf_field_integer
*integer
= g_new0(
1195 struct bt_ctf_field_integer
, 1);
1198 integer
->definition
.declaration
= &integer_type
->declaration
;
1201 return integer
? &integer
->parent
: NULL
;
1205 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1206 struct bt_ctf_field_type
*type
)
1208 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1209 struct bt_ctf_field_enumeration
, 1);
1211 return enumeration
? &enumeration
->parent
: NULL
;
1215 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1216 struct bt_ctf_field_type
*type
)
1218 struct bt_ctf_field_floating_point
*floating_point
;
1219 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1221 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1222 if (!floating_point
) {
1226 floating_point_type
= container_of(type
,
1227 struct bt_ctf_field_type_floating_point
, parent
);
1228 floating_point
->definition
.declaration
= container_of(
1229 type
->declaration
, struct declaration_float
, p
);
1232 floating_point
->definition
.sign
= &floating_point
->sign
;
1233 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1234 floating_point
->definition
.sign
->p
.declaration
=
1235 &floating_point_type
->sign
.p
;
1237 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1238 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1239 floating_point
->definition
.mantissa
->p
.declaration
=
1240 &floating_point_type
->mantissa
.p
;
1242 floating_point
->definition
.exp
= &floating_point
->exp
;
1243 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1244 floating_point
->definition
.exp
->p
.declaration
=
1245 &floating_point_type
->exp
.p
;
1248 return floating_point
? &floating_point
->parent
: NULL
;
1252 struct bt_ctf_field
*bt_ctf_field_structure_create(
1253 struct bt_ctf_field_type
*type
)
1255 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1256 struct bt_ctf_field_type_structure
, parent
);
1257 struct bt_ctf_field_structure
*structure
= g_new0(
1258 struct bt_ctf_field_structure
, 1);
1259 struct bt_ctf_field
*field
= NULL
;
1265 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1266 structure
->fields
= g_ptr_array_new_with_free_func(
1267 (GDestroyNotify
)bt_ctf_field_put
);
1268 g_ptr_array_set_size(structure
->fields
,
1269 g_hash_table_size(structure
->field_name_to_index
));
1270 field
= &structure
->parent
;
1276 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1278 struct bt_ctf_field_variant
*variant
= g_new0(
1279 struct bt_ctf_field_variant
, 1);
1280 return variant
? &variant
->parent
: NULL
;
1284 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1286 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1287 struct bt_ctf_field_type_array
*array_type
;
1288 unsigned int array_length
;
1290 if (!array
|| !type
) {
1294 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1295 array_length
= array_type
->length
;
1296 array
->elements
= g_ptr_array_sized_new(array_length
);
1297 if (!array
->elements
) {
1301 g_ptr_array_set_free_func(array
->elements
,
1302 (GDestroyNotify
)bt_ctf_field_put
);
1303 g_ptr_array_set_size(array
->elements
, array_length
);
1304 return &array
->parent
;
1311 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1312 struct bt_ctf_field_type
*type
)
1314 struct bt_ctf_field_sequence
*sequence
= g_new0(
1315 struct bt_ctf_field_sequence
, 1);
1316 return sequence
? &sequence
->parent
: NULL
;
1320 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1322 struct bt_ctf_field_string
*string
= g_new0(
1323 struct bt_ctf_field_string
, 1);
1324 return string
? &string
->parent
: NULL
;
1328 void bt_ctf_field_destroy(struct bt_object
*obj
)
1330 struct bt_ctf_field
*field
;
1331 struct bt_ctf_field_type
*type
;
1332 enum ctf_type_id type_id
;
1334 field
= container_of(obj
, struct bt_ctf_field
, base
);
1336 type_id
= bt_ctf_field_type_get_type_id(type
);
1337 if (type_id
<= CTF_TYPE_UNKNOWN
||
1338 type_id
>= NR_CTF_TYPES
) {
1342 field_destroy_funcs
[type_id
](field
);
1347 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1349 struct bt_ctf_field_integer
*integer
;
1355 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1360 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1362 struct bt_ctf_field_enumeration
*enumeration
;
1368 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1370 bt_put(enumeration
->payload
);
1371 g_free(enumeration
);
1375 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1377 struct bt_ctf_field_floating_point
*floating_point
;
1383 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1385 g_free(floating_point
);
1389 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1391 struct bt_ctf_field_structure
*structure
;
1397 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1398 g_ptr_array_free(structure
->fields
, TRUE
);
1403 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1405 struct bt_ctf_field_variant
*variant
;
1411 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1412 bt_put(variant
->tag
);
1413 bt_put(variant
->payload
);
1418 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1420 struct bt_ctf_field_array
*array
;
1426 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1427 g_ptr_array_free(array
->elements
, TRUE
);
1432 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1434 struct bt_ctf_field_sequence
*sequence
;
1440 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1441 if (sequence
->elements
) {
1442 g_ptr_array_free(sequence
->elements
, TRUE
);
1444 bt_put(sequence
->length
);
1449 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1451 struct bt_ctf_field_string
*string
;
1456 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1457 if (string
->payload
) {
1458 g_string_free(string
->payload
, TRUE
);
1464 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1466 return (field
&& field
->payload_set
) ? 0 : -1;
1470 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1473 struct bt_ctf_field_enumeration
*enumeration
;
1480 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1482 if (!enumeration
->payload
) {
1487 ret
= bt_ctf_field_validate(enumeration
->payload
);
1493 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1497 struct bt_ctf_field_structure
*structure
;
1504 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1505 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1506 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1516 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1519 struct bt_ctf_field_variant
*variant
;
1526 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1527 ret
= bt_ctf_field_validate(variant
->payload
);
1533 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1537 struct bt_ctf_field_array
*array
;
1544 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1545 for (i
= 0; i
< array
->elements
->len
; i
++) {
1546 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1556 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1560 struct bt_ctf_field_sequence
*sequence
;
1567 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1568 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1569 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1579 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1588 field
->payload_set
= 0;
1594 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1597 struct bt_ctf_field_enumeration
*enumeration
;
1604 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1606 if (!enumeration
->payload
) {
1610 ret
= bt_ctf_field_reset(enumeration
->payload
);
1616 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1620 struct bt_ctf_field_structure
*structure
;
1627 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1628 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1629 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1633 * Structure members are lazily initialized; skip if
1634 * this member has not been allocated yet.
1639 ret
= bt_ctf_field_reset(member
);
1649 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1652 struct bt_ctf_field_variant
*variant
;
1659 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1660 if (variant
->payload
) {
1661 ret
= bt_ctf_field_reset(variant
->payload
);
1668 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1672 struct bt_ctf_field_array
*array
;
1679 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1680 for (i
= 0; i
< array
->elements
->len
; i
++) {
1681 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1685 * Array elements are lazily initialized; skip if
1686 * this member has not been allocated yet.
1691 ret
= bt_ctf_field_reset(member
);
1701 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1705 struct bt_ctf_field_sequence
*sequence
;
1712 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1713 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1714 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1718 * Sequence elements are lazily initialized; skip if
1719 * this member has not been allocated yet.
1724 ret
= bt_ctf_field_reset(member
);
1734 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1737 struct bt_ctf_field_string
*string
;
1744 ret
= bt_ctf_field_generic_reset(field
);
1749 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1750 if (string
->payload
) {
1751 g_string_truncate(string
->payload
, 0);
1758 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1759 struct ctf_stream_pos
*pos
)
1762 struct bt_ctf_field_integer
*integer
= container_of(field
,
1763 struct bt_ctf_field_integer
, parent
);
1766 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1767 if (ret
== -EFAULT
) {
1769 * The field is too large to fit in the current packet's
1770 * remaining space. Bump the packet size and retry.
1772 ret
= increase_packet_size(pos
);
1783 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1784 struct ctf_stream_pos
*pos
)
1786 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1787 field
, struct bt_ctf_field_enumeration
, parent
);
1789 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1793 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1794 struct ctf_stream_pos
*pos
)
1797 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1798 struct bt_ctf_field_floating_point
, parent
);
1801 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1802 if (ret
== -EFAULT
) {
1804 * The field is too large to fit in the current packet's
1805 * remaining space. Bump the packet size and retry.
1807 ret
= increase_packet_size(pos
);
1818 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1819 struct ctf_stream_pos
*pos
)
1823 struct bt_ctf_field_structure
*structure
= container_of(
1824 field
, struct bt_ctf_field_structure
, parent
);
1826 while (!ctf_pos_access_ok(pos
,
1827 offset_align(pos
->offset
,
1828 field
->type
->declaration
->alignment
))) {
1829 ret
= increase_packet_size(pos
);
1835 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1840 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1841 struct bt_ctf_field
*field
= g_ptr_array_index(
1842 structure
->fields
, i
);
1844 ret
= bt_ctf_field_serialize(field
, pos
);
1854 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1855 struct ctf_stream_pos
*pos
)
1857 struct bt_ctf_field_variant
*variant
= container_of(
1858 field
, struct bt_ctf_field_variant
, parent
);
1860 return bt_ctf_field_serialize(variant
->payload
, pos
);
1864 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1865 struct ctf_stream_pos
*pos
)
1869 struct bt_ctf_field_array
*array
= container_of(
1870 field
, struct bt_ctf_field_array
, parent
);
1872 for (i
= 0; i
< array
->elements
->len
; i
++) {
1873 ret
= bt_ctf_field_serialize(
1874 g_ptr_array_index(array
->elements
, i
), pos
);
1884 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1885 struct ctf_stream_pos
*pos
)
1889 struct bt_ctf_field_sequence
*sequence
= container_of(
1890 field
, struct bt_ctf_field_sequence
, parent
);
1892 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1893 ret
= bt_ctf_field_serialize(
1894 g_ptr_array_index(sequence
->elements
, i
), pos
);
1904 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1905 struct ctf_stream_pos
*pos
)
1909 struct bt_ctf_field_string
*string
= container_of(field
,
1910 struct bt_ctf_field_string
, parent
);
1911 struct bt_ctf_field_type
*character_type
=
1912 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1913 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1915 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1916 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1917 (uint64_t) string
->payload
->str
[i
]);
1922 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1929 bt_put(character_type
);
1934 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1935 struct bt_ctf_field
*dst
)
1937 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1939 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1940 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1942 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1943 sizeof(struct definition_integer
));
1948 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1949 struct bt_ctf_field
*dst
)
1952 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1954 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1955 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1957 if (enum_src
->payload
) {
1958 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1959 if (!enum_dst
->payload
) {
1969 int bt_ctf_field_floating_point_copy(
1970 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1972 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1974 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1976 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1979 memcpy(&float_dst
->definition
, &float_src
->definition
,
1980 sizeof(struct definition_float
));
1981 memcpy(&float_dst
->sign
, &float_src
->sign
,
1982 sizeof(struct definition_integer
));
1983 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1984 sizeof(struct definition_integer
));
1985 memcpy(&float_dst
->exp
, &float_src
->exp
,
1986 sizeof(struct definition_integer
));
1991 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
1992 struct bt_ctf_field
*dst
)
1995 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
1997 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
1998 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2000 /* This field_name_to_index HT is owned by the structure field type */
2001 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2002 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2004 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2005 struct bt_ctf_field
*field
=
2006 g_ptr_array_index(struct_src
->fields
, i
);
2007 struct bt_ctf_field
*field_copy
= NULL
;
2010 field_copy
= bt_ctf_field_copy(field
);
2018 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2025 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2026 struct bt_ctf_field
*dst
)
2029 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2031 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2032 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2034 if (variant_src
->tag
) {
2035 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2036 if (!variant_dst
->tag
) {
2041 if (variant_src
->payload
) {
2042 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2043 if (!variant_dst
->payload
) {
2053 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2054 struct bt_ctf_field
*dst
)
2057 struct bt_ctf_field_array
*array_src
, *array_dst
;
2059 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2060 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2062 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2063 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2064 struct bt_ctf_field
*field
=
2065 g_ptr_array_index(array_src
->elements
, i
);
2066 struct bt_ctf_field
*field_copy
= NULL
;
2069 field_copy
= bt_ctf_field_copy(field
);
2077 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2084 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2085 struct bt_ctf_field
*dst
)
2088 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2089 struct bt_ctf_field
*src_length
;
2090 struct bt_ctf_field
*dst_length
;
2092 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2093 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2095 src_length
= bt_ctf_field_sequence_get_length(src
);
2098 /* no length set yet: keep destination sequence empty */
2102 /* copy source length */
2103 dst_length
= bt_ctf_field_copy(src_length
);
2111 /* this will initialize the destination sequence's internal array */
2112 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2119 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2121 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2122 struct bt_ctf_field
*field
=
2123 g_ptr_array_index(sequence_src
->elements
, i
);
2124 struct bt_ctf_field
*field_copy
= NULL
;
2127 field_copy
= bt_ctf_field_copy(field
);
2135 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2142 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2143 struct bt_ctf_field
*dst
)
2146 struct bt_ctf_field_string
*string_src
, *string_dst
;
2148 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2149 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2151 if (string_src
->payload
) {
2152 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2153 if (!string_dst
->payload
) {
2163 int increase_packet_size(struct ctf_stream_pos
*pos
)
2168 ret
= munmap_align(pos
->base_mma
);
2173 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2175 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2176 pos
->packet_size
/ CHAR_BIT
);
2177 } while (ret
== EINTR
);
2184 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2185 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2186 if (pos
->base_mma
== MAP_FAILED
) {