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>
36 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
39 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
41 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
42 struct bt_ctf_field_type
*);
44 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
45 struct bt_ctf_field_type
*);
47 struct bt_ctf_field
*bt_ctf_field_structure_create(
48 struct bt_ctf_field_type
*);
50 struct bt_ctf_field
*bt_ctf_field_variant_create(
51 struct bt_ctf_field_type
*);
53 struct bt_ctf_field
*bt_ctf_field_array_create(
54 struct bt_ctf_field_type
*);
56 struct bt_ctf_field
*bt_ctf_field_sequence_create(
57 struct bt_ctf_field_type
*);
59 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
62 void bt_ctf_field_destroy(struct bt_object
*);
64 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
66 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
68 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
70 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
72 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
74 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
76 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
78 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
81 int bt_ctf_field_generic_validate(struct bt_ctf_field
*);
83 int bt_ctf_field_structure_validate(struct bt_ctf_field
*);
85 int bt_ctf_field_variant_validate(struct bt_ctf_field
*);
87 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*);
89 int bt_ctf_field_array_validate(struct bt_ctf_field
*);
91 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*);
94 int bt_ctf_field_generic_reset(struct bt_ctf_field
*);
96 int bt_ctf_field_structure_reset(struct bt_ctf_field
*);
98 int bt_ctf_field_variant_reset(struct bt_ctf_field
*);
100 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*);
102 int bt_ctf_field_array_reset(struct bt_ctf_field
*);
104 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*);
106 int bt_ctf_field_string_reset(struct bt_ctf_field
*);
109 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
110 struct ctf_stream_pos
*);
112 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
113 struct ctf_stream_pos
*);
115 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
116 struct ctf_stream_pos
*);
118 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
119 struct ctf_stream_pos
*);
121 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
122 struct ctf_stream_pos
*);
124 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
125 struct ctf_stream_pos
*);
127 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
128 struct ctf_stream_pos
*);
130 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
131 struct ctf_stream_pos
*);
134 int bt_ctf_field_integer_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
136 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
138 int bt_ctf_field_floating_point_copy(struct bt_ctf_field
*,
139 struct bt_ctf_field
*);
141 int bt_ctf_field_structure_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
143 int bt_ctf_field_variant_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
145 int bt_ctf_field_array_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
147 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
149 int bt_ctf_field_string_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
152 int increase_packet_size(struct ctf_stream_pos
*pos
);
155 struct bt_ctf_field
*(* const field_create_funcs
[])(
156 struct bt_ctf_field_type
*) = {
157 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_create
,
158 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_create
,
160 bt_ctf_field_floating_point_create
,
161 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_create
,
162 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_create
,
163 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_create
,
164 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_create
,
165 [CTF_TYPE_STRING
] = bt_ctf_field_string_create
,
169 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
170 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_destroy
,
171 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_destroy
,
173 bt_ctf_field_floating_point_destroy
,
174 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_destroy
,
175 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_destroy
,
176 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_destroy
,
177 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
178 [CTF_TYPE_STRING
] = bt_ctf_field_string_destroy
,
182 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
183 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_validate
,
184 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_validate
,
185 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_validate
,
186 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_validate
,
187 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_validate
,
188 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_validate
,
189 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_validate
,
190 [CTF_TYPE_STRING
] = bt_ctf_field_generic_validate
,
194 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
195 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_reset
,
196 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_reset
,
197 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_reset
,
198 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_reset
,
199 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_reset
,
200 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_reset
,
201 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_reset
,
202 [CTF_TYPE_STRING
] = bt_ctf_field_string_reset
,
206 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
207 struct ctf_stream_pos
*) = {
208 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_serialize
,
209 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_serialize
,
211 bt_ctf_field_floating_point_serialize
,
212 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_serialize
,
213 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_serialize
,
214 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_serialize
,
215 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
216 [CTF_TYPE_STRING
] = bt_ctf_field_string_serialize
,
220 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
221 struct bt_ctf_field
*) = {
222 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_copy
,
223 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_copy
,
224 [CTF_TYPE_FLOAT
] = bt_ctf_field_floating_point_copy
,
225 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_copy
,
226 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_copy
,
227 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_copy
,
228 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_copy
,
229 [CTF_TYPE_STRING
] = bt_ctf_field_string_copy
,
232 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
234 struct bt_ctf_field
*field
= NULL
;
235 enum ctf_type_id type_id
;
241 type_id
= bt_ctf_field_type_get_type_id(type
);
242 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
||
243 bt_ctf_field_type_validate(type
)) {
247 field
= field_create_funcs
[type_id
](type
);
252 /* The type's declaration can't change after this point */
253 bt_ctf_field_type_freeze(type
);
255 bt_object_init(field
, bt_ctf_field_destroy
);
261 void bt_ctf_field_get(struct bt_ctf_field
*field
)
266 void bt_ctf_field_put(struct bt_ctf_field
*field
)
271 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
273 struct bt_ctf_field_type
*ret
= NULL
;
285 enum ctf_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
287 enum ctf_type_id ret
= CTF_TYPE_UNKNOWN
;
293 ret
= bt_ctf_field_type_get_type_id(field
->type
);
298 int bt_ctf_field_is_integer(struct bt_ctf_field
*field
)
300 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_INTEGER
;
303 int bt_ctf_field_is_floating_point(struct bt_ctf_field
*field
)
305 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_FLOAT
;
308 int bt_ctf_field_is_enumeration(struct bt_ctf_field
*field
)
310 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_ENUM
;
313 int bt_ctf_field_is_string(struct bt_ctf_field
*field
)
315 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_STRING
;
318 int bt_ctf_field_is_structure(struct bt_ctf_field
*field
)
320 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_STRUCT
;
323 int bt_ctf_field_is_array(struct bt_ctf_field
*field
)
325 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_ARRAY
;
328 int bt_ctf_field_is_sequence(struct bt_ctf_field
*field
)
330 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_SEQUENCE
;
333 int bt_ctf_field_is_variant(struct bt_ctf_field
*field
)
335 return bt_ctf_field_get_type_id(field
) == CTF_TYPE_VARIANT
;
338 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
339 struct bt_ctf_field
*field
)
341 struct bt_ctf_field
*ret
= NULL
;
342 struct bt_ctf_field_sequence
*sequence
;
348 if (bt_ctf_field_type_get_type_id(field
->type
) !=
353 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
354 ret
= sequence
->length
;
360 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
361 struct bt_ctf_field
*length_field
)
364 struct bt_ctf_field_type_integer
*length_type
;
365 struct bt_ctf_field_integer
*length
;
366 struct bt_ctf_field_sequence
*sequence
;
367 uint64_t sequence_length
;
369 if (!field
|| !length_field
) {
373 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
379 length_type
= container_of(length_field
->type
,
380 struct bt_ctf_field_type_integer
, parent
);
381 /* The length field must be unsigned */
382 if (length_type
->declaration
.signedness
) {
387 length
= container_of(length_field
, struct bt_ctf_field_integer
,
389 sequence_length
= length
->definition
.value
._unsigned
;
390 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
391 if (sequence
->elements
) {
392 g_ptr_array_free(sequence
->elements
, TRUE
);
393 bt_put(sequence
->length
);
396 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
397 if (!sequence
->elements
) {
402 g_ptr_array_set_free_func(sequence
->elements
,
403 (GDestroyNotify
) bt_put
);
404 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
405 bt_get(length_field
);
406 sequence
->length
= length_field
;
411 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
412 struct bt_ctf_field
*field
, const char *name
)
414 struct bt_ctf_field
*new_field
= NULL
;
416 struct bt_ctf_field_structure
*structure
;
417 struct bt_ctf_field_type
*field_type
= NULL
;
420 if (!field
|| !name
||
421 bt_ctf_field_type_get_type_id(field
->type
) !=
426 field_quark
= g_quark_from_string(name
);
427 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
429 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
431 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
432 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
436 if (structure
->fields
->pdata
[index
]) {
437 new_field
= structure
->fields
->pdata
[index
];
441 new_field
= bt_ctf_field_create(field_type
);
446 structure
->fields
->pdata
[index
] = new_field
;
456 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
457 struct bt_ctf_field
*field
, int index
)
460 const char *field_name
;
461 struct bt_ctf_field_structure
*structure
;
462 struct bt_ctf_field_type
*structure_type
;
463 struct bt_ctf_field_type
*field_type
= NULL
;
464 struct bt_ctf_field
*ret_field
= NULL
;
467 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
471 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
472 if (index
>= structure
->fields
->len
) {
476 ret_field
= structure
->fields
->pdata
[index
];
481 /* Field has not been instanciated yet, create it */
482 structure_type
= bt_ctf_field_get_type(field
);
483 if (!structure_type
) {
487 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
488 &field_name
, &field_type
, index
);
489 bt_put(structure_type
);
494 ret_field
= bt_ctf_field_create(field_type
);
499 structure
->fields
->pdata
[index
] = ret_field
;
508 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
509 const char *name
, struct bt_ctf_field
*value
)
513 struct bt_ctf_field_structure
*structure
;
514 struct bt_ctf_field_type
*expected_field_type
= NULL
;
517 if (!field
|| !name
|| !value
||
518 bt_ctf_field_type_get_type_id(field
->type
) !=
524 field_quark
= g_quark_from_string(name
);
525 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
526 expected_field_type
=
527 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
529 if (expected_field_type
!= value
->type
) {
534 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
535 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
539 if (structure
->fields
->pdata
[index
]) {
540 bt_put(structure
->fields
->pdata
[index
]);
543 structure
->fields
->pdata
[index
] = value
;
546 if (expected_field_type
) {
547 bt_put(expected_field_type
);
552 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
555 struct bt_ctf_field
*new_field
= NULL
;
556 struct bt_ctf_field_type
*field_type
= NULL
;
557 struct bt_ctf_field_array
*array
;
559 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
564 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
565 if (index
>= array
->elements
->len
) {
569 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
570 if (array
->elements
->pdata
[(size_t)index
]) {
571 new_field
= array
->elements
->pdata
[(size_t)index
];
575 new_field
= bt_ctf_field_create(field_type
);
576 array
->elements
->pdata
[(size_t)index
] = new_field
;
587 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
590 struct bt_ctf_field
*new_field
= NULL
;
591 struct bt_ctf_field_type
*field_type
= NULL
;
592 struct bt_ctf_field_sequence
*sequence
;
594 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
599 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
600 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
604 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
605 if (sequence
->elements
->pdata
[(size_t) index
]) {
606 new_field
= sequence
->elements
->pdata
[(size_t) index
];
610 new_field
= bt_ctf_field_create(field_type
);
611 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
622 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
623 struct bt_ctf_field
*tag_field
)
625 struct bt_ctf_field
*new_field
= NULL
;
626 struct bt_ctf_field_variant
*variant
;
627 struct bt_ctf_field_type_variant
*variant_type
;
628 struct bt_ctf_field_type
*field_type
;
629 struct bt_ctf_field
*tag_enum
= NULL
;
630 struct bt_ctf_field_integer
*tag_enum_integer
;
631 int64_t tag_enum_value
;
633 if (!field
|| !tag_field
||
634 bt_ctf_field_type_get_type_id(field
->type
) !=
636 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
641 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
642 variant_type
= container_of(field
->type
,
643 struct bt_ctf_field_type_variant
, parent
);
644 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
649 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
652 if (bt_ctf_field_validate(tag_field
) < 0) {
656 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
659 * If the variant currently has a tag and a payload, and if the
660 * requested tag value is the same as the current one, return
661 * the current payload instead of creating a fresh one.
663 if (variant
->tag
&& variant
->payload
) {
664 struct bt_ctf_field
*cur_tag_container
= NULL
;
665 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
666 int64_t cur_tag_value
;
669 bt_ctf_field_enumeration_get_container(variant
->tag
);
670 cur_tag_enum_integer
= container_of(cur_tag_container
,
671 struct bt_ctf_field_integer
, parent
);
672 bt_put(cur_tag_container
);
673 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
675 if (cur_tag_value
== tag_enum_value
) {
676 new_field
= variant
->payload
;
682 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
683 variant_type
, tag_enum_value
);
688 new_field
= bt_ctf_field_create(field_type
);
693 bt_put(variant
->tag
);
694 bt_put(variant
->payload
);
697 variant
->tag
= tag_field
;
698 variant
->payload
= new_field
;
704 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
705 struct bt_ctf_field
*variant_field
)
707 struct bt_ctf_field
*current_field
= NULL
;
708 struct bt_ctf_field_variant
*variant
;
710 if (!variant_field
||
711 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
716 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
719 if (variant
->payload
) {
720 current_field
= variant
->payload
;
721 bt_get(current_field
);
726 return current_field
;
729 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
730 struct bt_ctf_field
*field
)
732 struct bt_ctf_field
*container
= NULL
;
733 struct bt_ctf_field_enumeration
*enumeration
;
735 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
740 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
742 if (!enumeration
->payload
) {
743 struct bt_ctf_field_type_enumeration
*enumeration_type
=
744 container_of(field
->type
,
745 struct bt_ctf_field_type_enumeration
, parent
);
746 enumeration
->payload
=
747 bt_ctf_field_create(enumeration_type
->container
);
750 container
= enumeration
->payload
;
756 const char *bt_ctf_field_enumeration_get_mapping_name(
757 struct bt_ctf_field
*field
)
760 const char *name
= NULL
;
761 struct bt_ctf_field
*container
= NULL
;
762 struct bt_ctf_field_type
*container_type
= NULL
;
763 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
764 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
766 container
= bt_ctf_field_enumeration_get_container(field
);
771 container_type
= bt_ctf_field_get_type(container
);
772 if (!container_type
) {
773 goto error_put_container
;
776 integer_type
= container_of(container_type
,
777 struct bt_ctf_field_type_integer
, parent
);
778 enumeration_type
= container_of(field
->type
,
779 struct bt_ctf_field_type_enumeration
, parent
);
781 if (!integer_type
->declaration
.signedness
) {
783 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
786 goto error_put_container_type
;
789 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
790 enumeration_type
, value
);
793 ret
= bt_ctf_field_signed_integer_get_value(container
,
796 goto error_put_container_type
;
799 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
800 enumeration_type
, value
);
803 error_put_container_type
:
804 bt_put(container_type
);
811 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
815 struct bt_ctf_field_integer
*integer
;
816 struct bt_ctf_field_type_integer
*integer_type
;
818 if (!field
|| !value
|| !field
->payload_set
||
819 bt_ctf_field_type_get_type_id(field
->type
) !=
825 integer_type
= container_of(field
->type
,
826 struct bt_ctf_field_type_integer
, parent
);
827 if (!integer_type
->declaration
.signedness
) {
832 integer
= container_of(field
,
833 struct bt_ctf_field_integer
, parent
);
834 *value
= integer
->definition
.value
._signed
;
839 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
843 struct bt_ctf_field_integer
*integer
;
844 struct bt_ctf_field_type_integer
*integer_type
;
846 int64_t min_value
, max_value
;
849 bt_ctf_field_type_get_type_id(field
->type
) !=
855 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
856 integer_type
= container_of(field
->type
,
857 struct bt_ctf_field_type_integer
, parent
);
858 if (!integer_type
->declaration
.signedness
) {
863 size
= integer_type
->declaration
.len
;
864 min_value
= -((int64_t)1 << (size
- 1));
865 max_value
= ((int64_t)1 << (size
- 1)) - 1;
866 if (value
< min_value
|| value
> max_value
) {
871 integer
->definition
.value
._signed
= value
;
872 integer
->parent
.payload_set
= 1;
877 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
881 struct bt_ctf_field_integer
*integer
;
882 struct bt_ctf_field_type_integer
*integer_type
;
884 if (!field
|| !value
|| !field
->payload_set
||
885 bt_ctf_field_type_get_type_id(field
->type
) !=
891 integer_type
= container_of(field
->type
,
892 struct bt_ctf_field_type_integer
, parent
);
893 if (integer_type
->declaration
.signedness
) {
898 integer
= container_of(field
,
899 struct bt_ctf_field_integer
, parent
);
900 *value
= integer
->definition
.value
._unsigned
;
905 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
909 struct bt_ctf_field_integer
*integer
;
910 struct bt_ctf_field_type_integer
*integer_type
;
915 bt_ctf_field_type_get_type_id(field
->type
) !=
921 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
922 integer_type
= container_of(field
->type
,
923 struct bt_ctf_field_type_integer
, parent
);
924 if (integer_type
->declaration
.signedness
) {
929 size
= integer_type
->declaration
.len
;
930 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
931 if (value
> max_value
) {
936 integer
->definition
.value
._unsigned
= value
;
937 integer
->parent
.payload_set
= 1;
942 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
946 struct bt_ctf_field_floating_point
*floating_point
;
948 if (!field
|| !value
|| !field
->payload_set
||
949 bt_ctf_field_type_get_type_id(field
->type
) !=
955 floating_point
= container_of(field
,
956 struct bt_ctf_field_floating_point
, parent
);
957 *value
= floating_point
->definition
.value
;
962 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
966 struct bt_ctf_field_floating_point
*floating_point
;
969 bt_ctf_field_type_get_type_id(field
->type
) !=
974 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
976 floating_point
->definition
.value
= value
;
977 floating_point
->parent
.payload_set
= 1;
982 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
984 const char *ret
= NULL
;
985 struct bt_ctf_field_string
*string
;
987 if (!field
|| !field
->payload_set
||
988 bt_ctf_field_type_get_type_id(field
->type
) !=
993 string
= container_of(field
,
994 struct bt_ctf_field_string
, parent
);
995 ret
= string
->payload
->str
;
1000 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1004 struct bt_ctf_field_string
*string
;
1006 if (!field
|| !value
||
1007 bt_ctf_field_type_get_type_id(field
->type
) !=
1013 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1014 if (string
->payload
) {
1015 g_string_assign(string
->payload
, value
);
1017 string
->payload
= g_string_new(value
);
1020 string
->parent
.payload_set
= 1;
1025 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1029 struct bt_ctf_field_string
*string_field
;
1031 if (!field
|| !value
||
1032 bt_ctf_field_type_get_type_id(field
->type
) !=
1038 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1040 if (string_field
->payload
) {
1041 g_string_append(string_field
->payload
, value
);
1043 string_field
->payload
= g_string_new(value
);
1046 string_field
->parent
.payload_set
= 1;
1052 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1053 const char *value
, unsigned int length
)
1057 unsigned int effective_length
= length
;
1058 struct bt_ctf_field_string
*string_field
;
1060 if (!field
|| !value
||
1061 bt_ctf_field_type_get_type_id(field
->type
) !=
1067 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1069 /* make sure no null bytes are appended */
1070 for (i
= 0; i
< length
; ++i
) {
1071 if (value
[i
] == '\0') {
1072 effective_length
= i
;
1077 if (string_field
->payload
) {
1078 g_string_append_len(string_field
->payload
, value
,
1081 string_field
->payload
= g_string_new_len(value
,
1085 string_field
->parent
.payload_set
= 1;
1092 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1095 enum ctf_type_id type_id
;
1102 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1103 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1108 ret
= field_validate_funcs
[type_id
](field
);
1114 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1117 enum ctf_type_id type_id
;
1124 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1125 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1130 ret
= field_reset_funcs
[type_id
](field
);
1136 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1137 struct ctf_stream_pos
*pos
)
1140 enum ctf_type_id type_id
;
1142 if (!field
|| !pos
) {
1147 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1148 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1153 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1158 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1161 struct bt_ctf_field
*copy
= NULL
;
1162 enum ctf_type_id type_id
;
1168 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1169 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1173 copy
= bt_ctf_field_create(field
->type
);
1178 copy
->payload_set
= field
->payload_set
;
1179 ret
= field_copy_funcs
[type_id
](field
, copy
);
1189 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1191 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1192 struct bt_ctf_field_type_integer
, parent
);
1193 struct bt_ctf_field_integer
*integer
= g_new0(
1194 struct bt_ctf_field_integer
, 1);
1197 integer
->definition
.declaration
= &integer_type
->declaration
;
1200 return integer
? &integer
->parent
: NULL
;
1204 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1205 struct bt_ctf_field_type
*type
)
1207 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1208 struct bt_ctf_field_enumeration
, 1);
1210 return enumeration
? &enumeration
->parent
: NULL
;
1214 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1215 struct bt_ctf_field_type
*type
)
1217 struct bt_ctf_field_floating_point
*floating_point
;
1218 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1220 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1221 if (!floating_point
) {
1225 floating_point_type
= container_of(type
,
1226 struct bt_ctf_field_type_floating_point
, parent
);
1227 floating_point
->definition
.declaration
= container_of(
1228 type
->declaration
, struct declaration_float
, p
);
1231 floating_point
->definition
.sign
= &floating_point
->sign
;
1232 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1233 floating_point
->definition
.sign
->p
.declaration
=
1234 &floating_point_type
->sign
.p
;
1236 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1237 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1238 floating_point
->definition
.mantissa
->p
.declaration
=
1239 &floating_point_type
->mantissa
.p
;
1241 floating_point
->definition
.exp
= &floating_point
->exp
;
1242 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1243 floating_point
->definition
.exp
->p
.declaration
=
1244 &floating_point_type
->exp
.p
;
1247 return floating_point
? &floating_point
->parent
: NULL
;
1251 struct bt_ctf_field
*bt_ctf_field_structure_create(
1252 struct bt_ctf_field_type
*type
)
1254 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1255 struct bt_ctf_field_type_structure
, parent
);
1256 struct bt_ctf_field_structure
*structure
= g_new0(
1257 struct bt_ctf_field_structure
, 1);
1258 struct bt_ctf_field
*field
= NULL
;
1264 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1265 structure
->fields
= g_ptr_array_new_with_free_func(
1266 (GDestroyNotify
)bt_ctf_field_put
);
1267 g_ptr_array_set_size(structure
->fields
,
1268 g_hash_table_size(structure
->field_name_to_index
));
1269 field
= &structure
->parent
;
1275 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1277 struct bt_ctf_field_variant
*variant
= g_new0(
1278 struct bt_ctf_field_variant
, 1);
1279 return variant
? &variant
->parent
: NULL
;
1283 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1285 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1286 struct bt_ctf_field_type_array
*array_type
;
1287 unsigned int array_length
;
1289 if (!array
|| !type
) {
1293 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1294 array_length
= array_type
->length
;
1295 array
->elements
= g_ptr_array_sized_new(array_length
);
1296 if (!array
->elements
) {
1300 g_ptr_array_set_free_func(array
->elements
,
1301 (GDestroyNotify
)bt_ctf_field_put
);
1302 g_ptr_array_set_size(array
->elements
, array_length
);
1303 return &array
->parent
;
1310 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1311 struct bt_ctf_field_type
*type
)
1313 struct bt_ctf_field_sequence
*sequence
= g_new0(
1314 struct bt_ctf_field_sequence
, 1);
1315 return sequence
? &sequence
->parent
: NULL
;
1319 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1321 struct bt_ctf_field_string
*string
= g_new0(
1322 struct bt_ctf_field_string
, 1);
1323 return string
? &string
->parent
: NULL
;
1327 void bt_ctf_field_destroy(struct bt_object
*obj
)
1329 struct bt_ctf_field
*field
;
1330 struct bt_ctf_field_type
*type
;
1331 enum ctf_type_id type_id
;
1333 field
= container_of(obj
, struct bt_ctf_field
, base
);
1335 type_id
= bt_ctf_field_type_get_type_id(type
);
1336 if (type_id
<= CTF_TYPE_UNKNOWN
||
1337 type_id
>= NR_CTF_TYPES
) {
1341 field_destroy_funcs
[type_id
](field
);
1346 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1348 struct bt_ctf_field_integer
*integer
;
1354 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1359 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1361 struct bt_ctf_field_enumeration
*enumeration
;
1367 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1369 bt_put(enumeration
->payload
);
1370 g_free(enumeration
);
1374 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1376 struct bt_ctf_field_floating_point
*floating_point
;
1382 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1384 g_free(floating_point
);
1388 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1390 struct bt_ctf_field_structure
*structure
;
1396 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1397 g_ptr_array_free(structure
->fields
, TRUE
);
1402 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1404 struct bt_ctf_field_variant
*variant
;
1410 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1411 bt_put(variant
->tag
);
1412 bt_put(variant
->payload
);
1417 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1419 struct bt_ctf_field_array
*array
;
1425 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1426 g_ptr_array_free(array
->elements
, TRUE
);
1431 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1433 struct bt_ctf_field_sequence
*sequence
;
1439 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1440 if (sequence
->elements
) {
1441 g_ptr_array_free(sequence
->elements
, TRUE
);
1443 bt_put(sequence
->length
);
1448 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1450 struct bt_ctf_field_string
*string
;
1455 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1456 if (string
->payload
) {
1457 g_string_free(string
->payload
, TRUE
);
1463 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1465 return (field
&& field
->payload_set
) ? 0 : -1;
1469 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1472 struct bt_ctf_field_enumeration
*enumeration
;
1479 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1481 if (!enumeration
->payload
) {
1486 ret
= bt_ctf_field_validate(enumeration
->payload
);
1492 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1496 struct bt_ctf_field_structure
*structure
;
1503 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1504 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1505 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1515 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1518 struct bt_ctf_field_variant
*variant
;
1525 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1526 ret
= bt_ctf_field_validate(variant
->payload
);
1532 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1536 struct bt_ctf_field_array
*array
;
1543 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1544 for (i
= 0; i
< array
->elements
->len
; i
++) {
1545 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1555 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1559 struct bt_ctf_field_sequence
*sequence
;
1566 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1567 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1568 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1578 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1587 field
->payload_set
= 0;
1593 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1596 struct bt_ctf_field_enumeration
*enumeration
;
1603 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1605 if (!enumeration
->payload
) {
1609 ret
= bt_ctf_field_reset(enumeration
->payload
);
1615 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1619 struct bt_ctf_field_structure
*structure
;
1626 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1627 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1628 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1632 * Structure members are lazily initialized; skip if
1633 * this member has not been allocated yet.
1638 ret
= bt_ctf_field_reset(member
);
1648 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1651 struct bt_ctf_field_variant
*variant
;
1658 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1659 if (variant
->payload
) {
1660 ret
= bt_ctf_field_reset(variant
->payload
);
1667 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1671 struct bt_ctf_field_array
*array
;
1678 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1679 for (i
= 0; i
< array
->elements
->len
; i
++) {
1680 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1684 * Array elements are lazily initialized; skip if
1685 * this member has not been allocated yet.
1690 ret
= bt_ctf_field_reset(member
);
1700 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1704 struct bt_ctf_field_sequence
*sequence
;
1711 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1712 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1713 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1717 * Sequence elements are lazily initialized; skip if
1718 * this member has not been allocated yet.
1723 ret
= bt_ctf_field_reset(member
);
1733 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1736 struct bt_ctf_field_string
*string
;
1743 ret
= bt_ctf_field_generic_reset(field
);
1748 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1749 if (string
->payload
) {
1750 g_string_truncate(string
->payload
, 0);
1757 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1758 struct ctf_stream_pos
*pos
)
1761 struct bt_ctf_field_integer
*integer
= container_of(field
,
1762 struct bt_ctf_field_integer
, parent
);
1765 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1766 if (ret
== -EFAULT
) {
1768 * The field is too large to fit in the current packet's
1769 * remaining space. Bump the packet size and retry.
1771 ret
= increase_packet_size(pos
);
1782 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1783 struct ctf_stream_pos
*pos
)
1785 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1786 field
, struct bt_ctf_field_enumeration
, parent
);
1788 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1792 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1793 struct ctf_stream_pos
*pos
)
1796 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1797 struct bt_ctf_field_floating_point
, parent
);
1800 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1801 if (ret
== -EFAULT
) {
1803 * The field is too large to fit in the current packet's
1804 * remaining space. Bump the packet size and retry.
1806 ret
= increase_packet_size(pos
);
1817 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1818 struct ctf_stream_pos
*pos
)
1822 struct bt_ctf_field_structure
*structure
= container_of(
1823 field
, struct bt_ctf_field_structure
, parent
);
1825 while (!ctf_pos_access_ok(pos
,
1826 offset_align(pos
->offset
,
1827 field
->type
->declaration
->alignment
))) {
1828 ret
= increase_packet_size(pos
);
1834 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1839 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1840 struct bt_ctf_field
*field
= g_ptr_array_index(
1841 structure
->fields
, i
);
1843 ret
= bt_ctf_field_serialize(field
, pos
);
1853 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1854 struct ctf_stream_pos
*pos
)
1856 struct bt_ctf_field_variant
*variant
= container_of(
1857 field
, struct bt_ctf_field_variant
, parent
);
1859 return bt_ctf_field_serialize(variant
->payload
, pos
);
1863 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1864 struct ctf_stream_pos
*pos
)
1868 struct bt_ctf_field_array
*array
= container_of(
1869 field
, struct bt_ctf_field_array
, parent
);
1871 for (i
= 0; i
< array
->elements
->len
; i
++) {
1872 ret
= bt_ctf_field_serialize(
1873 g_ptr_array_index(array
->elements
, i
), pos
);
1883 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1884 struct ctf_stream_pos
*pos
)
1888 struct bt_ctf_field_sequence
*sequence
= container_of(
1889 field
, struct bt_ctf_field_sequence
, parent
);
1891 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1892 ret
= bt_ctf_field_serialize(
1893 g_ptr_array_index(sequence
->elements
, i
), pos
);
1903 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1904 struct ctf_stream_pos
*pos
)
1908 struct bt_ctf_field_string
*string
= container_of(field
,
1909 struct bt_ctf_field_string
, parent
);
1910 struct bt_ctf_field_type
*character_type
=
1911 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1912 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1914 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1915 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1916 (uint64_t) string
->payload
->str
[i
]);
1921 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1928 bt_put(character_type
);
1933 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1934 struct bt_ctf_field
*dst
)
1936 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1938 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1939 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1941 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1942 sizeof(struct definition_integer
));
1947 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1948 struct bt_ctf_field
*dst
)
1951 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1953 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1954 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1956 if (enum_src
->payload
) {
1957 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1958 if (!enum_dst
->payload
) {
1968 int bt_ctf_field_floating_point_copy(
1969 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1971 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1973 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1975 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1978 memcpy(&float_dst
->definition
, &float_src
->definition
,
1979 sizeof(struct definition_float
));
1980 memcpy(&float_dst
->sign
, &float_src
->sign
,
1981 sizeof(struct definition_integer
));
1982 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1983 sizeof(struct definition_integer
));
1984 memcpy(&float_dst
->exp
, &float_src
->exp
,
1985 sizeof(struct definition_integer
));
1990 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
1991 struct bt_ctf_field
*dst
)
1994 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
1996 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
1997 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
1999 /* This field_name_to_index HT is owned by the structure field type */
2000 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2001 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2003 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2004 struct bt_ctf_field
*field
=
2005 g_ptr_array_index(struct_src
->fields
, i
);
2006 struct bt_ctf_field
*field_copy
= NULL
;
2009 field_copy
= bt_ctf_field_copy(field
);
2017 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2024 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2025 struct bt_ctf_field
*dst
)
2028 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2030 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2031 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2033 if (variant_src
->tag
) {
2034 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2035 if (!variant_dst
->tag
) {
2040 if (variant_src
->payload
) {
2041 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2042 if (!variant_dst
->payload
) {
2052 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2053 struct bt_ctf_field
*dst
)
2056 struct bt_ctf_field_array
*array_src
, *array_dst
;
2058 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2059 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2061 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2062 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2063 struct bt_ctf_field
*field
=
2064 g_ptr_array_index(array_src
->elements
, i
);
2065 struct bt_ctf_field
*field_copy
= NULL
;
2068 field_copy
= bt_ctf_field_copy(field
);
2076 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2083 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2084 struct bt_ctf_field
*dst
)
2087 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2088 struct bt_ctf_field
*src_length
;
2089 struct bt_ctf_field
*dst_length
;
2091 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2092 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2094 src_length
= bt_ctf_field_sequence_get_length(src
);
2097 /* no length set yet: keep destination sequence empty */
2101 /* copy source length */
2102 dst_length
= bt_ctf_field_copy(src_length
);
2110 /* this will initialize the destination sequence's internal array */
2111 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2118 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2120 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2121 struct bt_ctf_field
*field
=
2122 g_ptr_array_index(sequence_src
->elements
, i
);
2123 struct bt_ctf_field
*field_copy
= NULL
;
2126 field_copy
= bt_ctf_field_copy(field
);
2134 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2141 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2142 struct bt_ctf_field
*dst
)
2145 struct bt_ctf_field_string
*string_src
, *string_dst
;
2147 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2148 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2150 if (string_src
->payload
) {
2151 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2152 if (!string_dst
->payload
) {
2162 int increase_packet_size(struct ctf_stream_pos
*pos
)
2167 ret
= munmap_align(pos
->base_mma
);
2172 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2173 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2174 pos
->packet_size
/ CHAR_BIT
);
2179 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2180 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2181 if (pos
->base_mma
== MAP_FAILED
) {