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 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
286 struct bt_ctf_field
*field
)
288 struct bt_ctf_field
*ret
= NULL
;
289 struct bt_ctf_field_sequence
*sequence
;
295 if (bt_ctf_field_type_get_type_id(field
->type
) !=
300 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
301 ret
= sequence
->length
;
307 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
308 struct bt_ctf_field
*length_field
)
311 struct bt_ctf_field_type_integer
*length_type
;
312 struct bt_ctf_field_integer
*length
;
313 struct bt_ctf_field_sequence
*sequence
;
314 uint64_t sequence_length
;
316 if (!field
|| !length_field
) {
320 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
326 length_type
= container_of(length_field
->type
,
327 struct bt_ctf_field_type_integer
, parent
);
328 /* The length field must be unsigned */
329 if (length_type
->declaration
.signedness
) {
334 length
= container_of(length_field
, struct bt_ctf_field_integer
,
336 sequence_length
= length
->definition
.value
._unsigned
;
337 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
338 if (sequence
->elements
) {
339 g_ptr_array_free(sequence
->elements
, TRUE
);
340 bt_put(sequence
->length
);
343 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
344 if (!sequence
->elements
) {
349 g_ptr_array_set_free_func(sequence
->elements
,
350 (GDestroyNotify
) bt_put
);
351 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
352 bt_get(length_field
);
353 sequence
->length
= length_field
;
358 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
359 struct bt_ctf_field
*field
, const char *name
)
361 struct bt_ctf_field
*new_field
= NULL
;
363 struct bt_ctf_field_structure
*structure
;
364 struct bt_ctf_field_type
*field_type
= NULL
;
367 if (!field
|| !name
||
368 bt_ctf_field_type_get_type_id(field
->type
) !=
373 field_quark
= g_quark_from_string(name
);
374 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
376 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
378 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
379 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
383 if (structure
->fields
->pdata
[index
]) {
384 new_field
= structure
->fields
->pdata
[index
];
388 new_field
= bt_ctf_field_create(field_type
);
393 structure
->fields
->pdata
[index
] = new_field
;
403 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
404 struct bt_ctf_field
*field
, int index
)
407 const char *field_name
;
408 struct bt_ctf_field_structure
*structure
;
409 struct bt_ctf_field_type
*structure_type
;
410 struct bt_ctf_field_type
*field_type
= NULL
;
411 struct bt_ctf_field
*ret_field
= NULL
;
414 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
418 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
419 if (index
>= structure
->fields
->len
) {
423 ret_field
= structure
->fields
->pdata
[index
];
428 /* Field has not been instanciated yet, create it */
429 structure_type
= bt_ctf_field_get_type(field
);
430 if (!structure_type
) {
434 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
435 &field_name
, &field_type
, index
);
436 bt_put(structure_type
);
441 ret_field
= bt_ctf_field_create(field_type
);
446 structure
->fields
->pdata
[index
] = ret_field
;
455 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
456 const char *name
, struct bt_ctf_field
*value
)
460 struct bt_ctf_field_structure
*structure
;
461 struct bt_ctf_field_type
*expected_field_type
= NULL
;
464 if (!field
|| !name
|| !value
||
465 bt_ctf_field_type_get_type_id(field
->type
) !=
471 field_quark
= g_quark_from_string(name
);
472 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
473 expected_field_type
=
474 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
476 if (expected_field_type
!= value
->type
) {
481 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
482 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
486 if (structure
->fields
->pdata
[index
]) {
487 bt_put(structure
->fields
->pdata
[index
]);
490 structure
->fields
->pdata
[index
] = value
;
493 if (expected_field_type
) {
494 bt_put(expected_field_type
);
499 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
502 struct bt_ctf_field
*new_field
= NULL
;
503 struct bt_ctf_field_type
*field_type
= NULL
;
504 struct bt_ctf_field_array
*array
;
506 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
511 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
512 if (index
>= array
->elements
->len
) {
516 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
517 if (array
->elements
->pdata
[(size_t)index
]) {
518 new_field
= array
->elements
->pdata
[(size_t)index
];
522 new_field
= bt_ctf_field_create(field_type
);
523 array
->elements
->pdata
[(size_t)index
] = new_field
;
534 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
537 struct bt_ctf_field
*new_field
= NULL
;
538 struct bt_ctf_field_type
*field_type
= NULL
;
539 struct bt_ctf_field_sequence
*sequence
;
541 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
546 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
547 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
551 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
552 if (sequence
->elements
->pdata
[(size_t) index
]) {
553 new_field
= sequence
->elements
->pdata
[(size_t) index
];
557 new_field
= bt_ctf_field_create(field_type
);
558 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
569 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
570 struct bt_ctf_field
*tag_field
)
572 struct bt_ctf_field
*new_field
= NULL
;
573 struct bt_ctf_field_variant
*variant
;
574 struct bt_ctf_field_type_variant
*variant_type
;
575 struct bt_ctf_field_type
*field_type
;
576 struct bt_ctf_field
*tag_enum
= NULL
;
577 struct bt_ctf_field_integer
*tag_enum_integer
;
578 int64_t tag_enum_value
;
580 if (!field
|| !tag_field
||
581 bt_ctf_field_type_get_type_id(field
->type
) !=
583 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
588 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
589 variant_type
= container_of(field
->type
,
590 struct bt_ctf_field_type_variant
, parent
);
591 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
596 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
599 if (bt_ctf_field_validate(tag_field
) < 0) {
603 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
606 * If the variant currently has a tag and a payload, and if the
607 * requested tag value is the same as the current one, return
608 * the current payload instead of creating a fresh one.
610 if (variant
->tag
&& variant
->payload
) {
611 struct bt_ctf_field
*cur_tag_container
= NULL
;
612 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
613 int64_t cur_tag_value
;
616 bt_ctf_field_enumeration_get_container(variant
->tag
);
617 cur_tag_enum_integer
= container_of(cur_tag_container
,
618 struct bt_ctf_field_integer
, parent
);
619 bt_put(cur_tag_container
);
620 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
622 if (cur_tag_value
== tag_enum_value
) {
623 new_field
= variant
->payload
;
629 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
630 variant_type
, tag_enum_value
);
635 new_field
= bt_ctf_field_create(field_type
);
640 bt_put(variant
->tag
);
641 bt_put(variant
->payload
);
644 variant
->tag
= tag_field
;
645 variant
->payload
= new_field
;
651 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
652 struct bt_ctf_field
*variant_field
)
654 struct bt_ctf_field
*current_field
= NULL
;
655 struct bt_ctf_field_variant
*variant
;
657 if (!variant_field
||
658 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
663 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
666 if (variant
->payload
) {
667 current_field
= variant
->payload
;
668 bt_get(current_field
);
673 return current_field
;
676 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
677 struct bt_ctf_field
*field
)
679 struct bt_ctf_field
*container
= NULL
;
680 struct bt_ctf_field_enumeration
*enumeration
;
682 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
687 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
689 if (!enumeration
->payload
) {
690 struct bt_ctf_field_type_enumeration
*enumeration_type
=
691 container_of(field
->type
,
692 struct bt_ctf_field_type_enumeration
, parent
);
693 enumeration
->payload
=
694 bt_ctf_field_create(enumeration_type
->container
);
697 container
= enumeration
->payload
;
703 const char *bt_ctf_field_enumeration_get_mapping_name(
704 struct bt_ctf_field
*field
)
707 const char *name
= NULL
;
708 struct bt_ctf_field
*container
= NULL
;
709 struct bt_ctf_field_type
*container_type
= NULL
;
710 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
711 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
713 container
= bt_ctf_field_enumeration_get_container(field
);
718 container_type
= bt_ctf_field_get_type(container
);
719 if (!container_type
) {
720 goto error_put_container
;
723 integer_type
= container_of(container_type
,
724 struct bt_ctf_field_type_integer
, parent
);
725 enumeration_type
= container_of(field
->type
,
726 struct bt_ctf_field_type_enumeration
, parent
);
728 if (!integer_type
->declaration
.signedness
) {
730 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
733 goto error_put_container_type
;
736 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
737 enumeration_type
, value
);
740 ret
= bt_ctf_field_signed_integer_get_value(container
,
743 goto error_put_container_type
;
746 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
747 enumeration_type
, value
);
750 error_put_container_type
:
751 bt_put(container_type
);
758 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
762 struct bt_ctf_field_integer
*integer
;
763 struct bt_ctf_field_type_integer
*integer_type
;
765 if (!field
|| !value
|| !field
->payload_set
||
766 bt_ctf_field_type_get_type_id(field
->type
) !=
772 integer_type
= container_of(field
->type
,
773 struct bt_ctf_field_type_integer
, parent
);
774 if (!integer_type
->declaration
.signedness
) {
779 integer
= container_of(field
,
780 struct bt_ctf_field_integer
, parent
);
781 *value
= integer
->definition
.value
._signed
;
786 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
790 struct bt_ctf_field_integer
*integer
;
791 struct bt_ctf_field_type_integer
*integer_type
;
793 int64_t min_value
, max_value
;
796 bt_ctf_field_type_get_type_id(field
->type
) !=
802 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
803 integer_type
= container_of(field
->type
,
804 struct bt_ctf_field_type_integer
, parent
);
805 if (!integer_type
->declaration
.signedness
) {
810 size
= integer_type
->declaration
.len
;
811 min_value
= -((int64_t)1 << (size
- 1));
812 max_value
= ((int64_t)1 << (size
- 1)) - 1;
813 if (value
< min_value
|| value
> max_value
) {
818 integer
->definition
.value
._signed
= value
;
819 integer
->parent
.payload_set
= 1;
824 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
828 struct bt_ctf_field_integer
*integer
;
829 struct bt_ctf_field_type_integer
*integer_type
;
831 if (!field
|| !value
|| !field
->payload_set
||
832 bt_ctf_field_type_get_type_id(field
->type
) !=
838 integer_type
= container_of(field
->type
,
839 struct bt_ctf_field_type_integer
, parent
);
840 if (integer_type
->declaration
.signedness
) {
845 integer
= container_of(field
,
846 struct bt_ctf_field_integer
, parent
);
847 *value
= integer
->definition
.value
._unsigned
;
852 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
856 struct bt_ctf_field_integer
*integer
;
857 struct bt_ctf_field_type_integer
*integer_type
;
862 bt_ctf_field_type_get_type_id(field
->type
) !=
868 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
869 integer_type
= container_of(field
->type
,
870 struct bt_ctf_field_type_integer
, parent
);
871 if (integer_type
->declaration
.signedness
) {
876 size
= integer_type
->declaration
.len
;
877 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
878 if (value
> max_value
) {
883 integer
->definition
.value
._unsigned
= value
;
884 integer
->parent
.payload_set
= 1;
889 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
893 struct bt_ctf_field_floating_point
*floating_point
;
895 if (!field
|| !value
|| !field
->payload_set
||
896 bt_ctf_field_type_get_type_id(field
->type
) !=
902 floating_point
= container_of(field
,
903 struct bt_ctf_field_floating_point
, parent
);
904 *value
= floating_point
->definition
.value
;
909 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
913 struct bt_ctf_field_floating_point
*floating_point
;
916 bt_ctf_field_type_get_type_id(field
->type
) !=
921 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
923 floating_point
->definition
.value
= value
;
924 floating_point
->parent
.payload_set
= 1;
929 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
931 const char *ret
= NULL
;
932 struct bt_ctf_field_string
*string
;
934 if (!field
|| !field
->payload_set
||
935 bt_ctf_field_type_get_type_id(field
->type
) !=
940 string
= container_of(field
,
941 struct bt_ctf_field_string
, parent
);
942 ret
= string
->payload
->str
;
947 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
951 struct bt_ctf_field_string
*string
;
953 if (!field
|| !value
||
954 bt_ctf_field_type_get_type_id(field
->type
) !=
960 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
961 if (string
->payload
) {
962 g_string_assign(string
->payload
, value
);
964 string
->payload
= g_string_new(value
);
967 string
->parent
.payload_set
= 1;
972 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
976 struct bt_ctf_field_string
*string_field
;
978 if (!field
|| !value
||
979 bt_ctf_field_type_get_type_id(field
->type
) !=
985 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
987 if (string_field
->payload
) {
988 g_string_append(string_field
->payload
, value
);
990 string_field
->payload
= g_string_new(value
);
993 string_field
->parent
.payload_set
= 1;
999 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1000 const char *value
, unsigned int length
)
1004 unsigned int effective_length
= length
;
1005 struct bt_ctf_field_string
*string_field
;
1007 if (!field
|| !value
||
1008 bt_ctf_field_type_get_type_id(field
->type
) !=
1014 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1016 /* make sure no null bytes are appended */
1017 for (i
= 0; i
< length
; ++i
) {
1018 if (value
[i
] == '\0') {
1019 effective_length
= i
;
1024 if (string_field
->payload
) {
1025 g_string_append_len(string_field
->payload
, value
,
1028 string_field
->payload
= g_string_new_len(value
,
1032 string_field
->parent
.payload_set
= 1;
1039 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1042 enum ctf_type_id type_id
;
1049 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1050 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1055 ret
= field_validate_funcs
[type_id
](field
);
1061 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1064 enum ctf_type_id type_id
;
1071 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1072 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1077 ret
= field_reset_funcs
[type_id
](field
);
1083 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1084 struct ctf_stream_pos
*pos
)
1087 enum ctf_type_id type_id
;
1089 if (!field
|| !pos
) {
1094 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1095 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1100 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1105 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1108 struct bt_ctf_field
*copy
= NULL
;
1109 enum ctf_type_id type_id
;
1115 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1116 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1120 copy
= bt_ctf_field_create(field
->type
);
1125 copy
->payload_set
= field
->payload_set
;
1126 ret
= field_copy_funcs
[type_id
](field
, copy
);
1136 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1138 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1139 struct bt_ctf_field_type_integer
, parent
);
1140 struct bt_ctf_field_integer
*integer
= g_new0(
1141 struct bt_ctf_field_integer
, 1);
1144 integer
->definition
.declaration
= &integer_type
->declaration
;
1147 return integer
? &integer
->parent
: NULL
;
1151 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1152 struct bt_ctf_field_type
*type
)
1154 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1155 struct bt_ctf_field_enumeration
, 1);
1157 return enumeration
? &enumeration
->parent
: NULL
;
1161 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1162 struct bt_ctf_field_type
*type
)
1164 struct bt_ctf_field_floating_point
*floating_point
;
1165 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1167 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1168 if (!floating_point
) {
1172 floating_point_type
= container_of(type
,
1173 struct bt_ctf_field_type_floating_point
, parent
);
1174 floating_point
->definition
.declaration
= container_of(
1175 type
->declaration
, struct declaration_float
, p
);
1178 floating_point
->definition
.sign
= &floating_point
->sign
;
1179 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1180 floating_point
->definition
.sign
->p
.declaration
=
1181 &floating_point_type
->sign
.p
;
1183 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1184 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1185 floating_point
->definition
.mantissa
->p
.declaration
=
1186 &floating_point_type
->mantissa
.p
;
1188 floating_point
->definition
.exp
= &floating_point
->exp
;
1189 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1190 floating_point
->definition
.exp
->p
.declaration
=
1191 &floating_point_type
->exp
.p
;
1194 return floating_point
? &floating_point
->parent
: NULL
;
1198 struct bt_ctf_field
*bt_ctf_field_structure_create(
1199 struct bt_ctf_field_type
*type
)
1201 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1202 struct bt_ctf_field_type_structure
, parent
);
1203 struct bt_ctf_field_structure
*structure
= g_new0(
1204 struct bt_ctf_field_structure
, 1);
1205 struct bt_ctf_field
*field
= NULL
;
1207 if (!structure
|| !structure_type
->fields
->len
) {
1211 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1212 structure
->fields
= g_ptr_array_new_with_free_func(
1213 (GDestroyNotify
)bt_ctf_field_put
);
1214 g_ptr_array_set_size(structure
->fields
,
1215 g_hash_table_size(structure
->field_name_to_index
));
1216 field
= &structure
->parent
;
1222 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1224 struct bt_ctf_field_variant
*variant
= g_new0(
1225 struct bt_ctf_field_variant
, 1);
1226 return variant
? &variant
->parent
: NULL
;
1230 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1232 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1233 struct bt_ctf_field_type_array
*array_type
;
1234 unsigned int array_length
;
1236 if (!array
|| !type
) {
1240 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1241 array_length
= array_type
->length
;
1242 array
->elements
= g_ptr_array_sized_new(array_length
);
1243 if (!array
->elements
) {
1247 g_ptr_array_set_free_func(array
->elements
,
1248 (GDestroyNotify
)bt_ctf_field_put
);
1249 g_ptr_array_set_size(array
->elements
, array_length
);
1250 return &array
->parent
;
1257 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1258 struct bt_ctf_field_type
*type
)
1260 struct bt_ctf_field_sequence
*sequence
= g_new0(
1261 struct bt_ctf_field_sequence
, 1);
1262 return sequence
? &sequence
->parent
: NULL
;
1266 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1268 struct bt_ctf_field_string
*string
= g_new0(
1269 struct bt_ctf_field_string
, 1);
1270 return string
? &string
->parent
: NULL
;
1274 void bt_ctf_field_destroy(struct bt_object
*obj
)
1276 struct bt_ctf_field
*field
;
1277 struct bt_ctf_field_type
*type
;
1278 enum ctf_type_id type_id
;
1280 field
= container_of(obj
, struct bt_ctf_field
, base
);
1282 type_id
= bt_ctf_field_type_get_type_id(type
);
1283 if (type_id
<= CTF_TYPE_UNKNOWN
||
1284 type_id
>= NR_CTF_TYPES
) {
1288 field_destroy_funcs
[type_id
](field
);
1293 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1295 struct bt_ctf_field_integer
*integer
;
1301 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1306 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1308 struct bt_ctf_field_enumeration
*enumeration
;
1314 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1316 bt_put(enumeration
->payload
);
1317 g_free(enumeration
);
1321 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1323 struct bt_ctf_field_floating_point
*floating_point
;
1329 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1331 g_free(floating_point
);
1335 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1337 struct bt_ctf_field_structure
*structure
;
1343 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1344 g_ptr_array_free(structure
->fields
, TRUE
);
1349 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1351 struct bt_ctf_field_variant
*variant
;
1357 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1358 bt_put(variant
->tag
);
1359 bt_put(variant
->payload
);
1364 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1366 struct bt_ctf_field_array
*array
;
1372 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1373 g_ptr_array_free(array
->elements
, TRUE
);
1378 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1380 struct bt_ctf_field_sequence
*sequence
;
1386 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1387 if (sequence
->elements
) {
1388 g_ptr_array_free(sequence
->elements
, TRUE
);
1390 bt_put(sequence
->length
);
1395 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1397 struct bt_ctf_field_string
*string
;
1402 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1403 if (string
->payload
) {
1404 g_string_free(string
->payload
, TRUE
);
1410 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1412 return (field
&& field
->payload_set
) ? 0 : -1;
1416 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1419 struct bt_ctf_field_enumeration
*enumeration
;
1426 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1428 if (!enumeration
->payload
) {
1433 ret
= bt_ctf_field_validate(enumeration
->payload
);
1439 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1443 struct bt_ctf_field_structure
*structure
;
1450 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1451 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1452 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1462 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1465 struct bt_ctf_field_variant
*variant
;
1472 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1473 ret
= bt_ctf_field_validate(variant
->payload
);
1479 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1483 struct bt_ctf_field_array
*array
;
1490 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1491 for (i
= 0; i
< array
->elements
->len
; i
++) {
1492 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1502 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1506 struct bt_ctf_field_sequence
*sequence
;
1513 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1514 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1515 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1525 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1534 field
->payload_set
= 0;
1540 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1543 struct bt_ctf_field_enumeration
*enumeration
;
1550 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1552 if (!enumeration
->payload
) {
1556 ret
= bt_ctf_field_reset(enumeration
->payload
);
1562 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1566 struct bt_ctf_field_structure
*structure
;
1573 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1574 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1575 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1579 * Structure members are lazily initialized; skip if
1580 * this member has not been allocated yet.
1585 ret
= bt_ctf_field_reset(member
);
1595 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1598 struct bt_ctf_field_variant
*variant
;
1605 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1606 if (variant
->payload
) {
1607 ret
= bt_ctf_field_reset(variant
->payload
);
1614 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1618 struct bt_ctf_field_array
*array
;
1625 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1626 for (i
= 0; i
< array
->elements
->len
; i
++) {
1627 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1631 * Array elements are lazily initialized; skip if
1632 * this member has not been allocated yet.
1637 ret
= bt_ctf_field_reset(member
);
1647 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1651 struct bt_ctf_field_sequence
*sequence
;
1658 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1659 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1660 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1664 * Sequence elements are lazily initialized; skip if
1665 * this member has not been allocated yet.
1670 ret
= bt_ctf_field_reset(member
);
1680 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1683 struct bt_ctf_field_string
*string
;
1690 ret
= bt_ctf_field_generic_reset(field
);
1695 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1696 if (string
->payload
) {
1697 g_string_truncate(string
->payload
, 0);
1704 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1705 struct ctf_stream_pos
*pos
)
1708 struct bt_ctf_field_integer
*integer
= container_of(field
,
1709 struct bt_ctf_field_integer
, parent
);
1712 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1713 if (ret
== -EFAULT
) {
1715 * The field is too large to fit in the current packet's
1716 * remaining space. Bump the packet size and retry.
1718 ret
= increase_packet_size(pos
);
1729 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1730 struct ctf_stream_pos
*pos
)
1732 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1733 field
, struct bt_ctf_field_enumeration
, parent
);
1735 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1739 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1740 struct ctf_stream_pos
*pos
)
1743 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1744 struct bt_ctf_field_floating_point
, parent
);
1747 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1748 if (ret
== -EFAULT
) {
1750 * The field is too large to fit in the current packet's
1751 * remaining space. Bump the packet size and retry.
1753 ret
= increase_packet_size(pos
);
1764 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1765 struct ctf_stream_pos
*pos
)
1769 struct bt_ctf_field_structure
*structure
= container_of(
1770 field
, struct bt_ctf_field_structure
, parent
);
1772 while (!ctf_pos_access_ok(pos
,
1773 offset_align(pos
->offset
,
1774 field
->type
->declaration
->alignment
))) {
1775 ret
= increase_packet_size(pos
);
1781 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1786 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1787 struct bt_ctf_field
*field
= g_ptr_array_index(
1788 structure
->fields
, i
);
1790 ret
= bt_ctf_field_serialize(field
, pos
);
1800 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1801 struct ctf_stream_pos
*pos
)
1803 struct bt_ctf_field_variant
*variant
= container_of(
1804 field
, struct bt_ctf_field_variant
, parent
);
1806 return bt_ctf_field_serialize(variant
->payload
, pos
);
1810 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1811 struct ctf_stream_pos
*pos
)
1815 struct bt_ctf_field_array
*array
= container_of(
1816 field
, struct bt_ctf_field_array
, parent
);
1818 for (i
= 0; i
< array
->elements
->len
; i
++) {
1819 ret
= bt_ctf_field_serialize(
1820 g_ptr_array_index(array
->elements
, i
), pos
);
1830 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1831 struct ctf_stream_pos
*pos
)
1835 struct bt_ctf_field_sequence
*sequence
= container_of(
1836 field
, struct bt_ctf_field_sequence
, parent
);
1838 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1839 ret
= bt_ctf_field_serialize(
1840 g_ptr_array_index(sequence
->elements
, i
), pos
);
1850 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1851 struct ctf_stream_pos
*pos
)
1855 struct bt_ctf_field_string
*string
= container_of(field
,
1856 struct bt_ctf_field_string
, parent
);
1857 struct bt_ctf_field_type
*character_type
=
1858 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1859 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1861 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1862 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1863 (uint64_t) string
->payload
->str
[i
]);
1868 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1875 bt_put(character_type
);
1880 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1881 struct bt_ctf_field
*dst
)
1883 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1885 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1886 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1888 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1889 sizeof(struct definition_integer
));
1894 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1895 struct bt_ctf_field
*dst
)
1898 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1900 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1901 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1903 if (enum_src
->payload
) {
1904 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1905 if (!enum_dst
->payload
) {
1915 int bt_ctf_field_floating_point_copy(
1916 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1918 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1920 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1922 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1925 memcpy(&float_dst
->definition
, &float_src
->definition
,
1926 sizeof(struct definition_float
));
1927 memcpy(&float_dst
->sign
, &float_src
->sign
,
1928 sizeof(struct definition_integer
));
1929 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1930 sizeof(struct definition_integer
));
1931 memcpy(&float_dst
->exp
, &float_src
->exp
,
1932 sizeof(struct definition_integer
));
1937 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
1938 struct bt_ctf_field
*dst
)
1941 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
1943 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
1944 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
1946 /* This field_name_to_index HT is owned by the structure field type */
1947 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
1948 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
1950 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
1951 struct bt_ctf_field
*field
=
1952 g_ptr_array_index(struct_src
->fields
, i
);
1953 struct bt_ctf_field
*field_copy
= NULL
;
1956 field_copy
= bt_ctf_field_copy(field
);
1964 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
1971 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
1972 struct bt_ctf_field
*dst
)
1975 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
1977 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
1978 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
1980 if (variant_src
->tag
) {
1981 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
1982 if (!variant_dst
->tag
) {
1987 if (variant_src
->payload
) {
1988 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
1989 if (!variant_dst
->payload
) {
1999 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2000 struct bt_ctf_field
*dst
)
2003 struct bt_ctf_field_array
*array_src
, *array_dst
;
2005 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2006 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2008 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2009 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2010 struct bt_ctf_field
*field
=
2011 g_ptr_array_index(array_src
->elements
, i
);
2012 struct bt_ctf_field
*field_copy
= NULL
;
2015 field_copy
= bt_ctf_field_copy(field
);
2023 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2030 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2031 struct bt_ctf_field
*dst
)
2034 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2035 struct bt_ctf_field
*src_length
;
2036 struct bt_ctf_field
*dst_length
;
2038 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2039 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2041 src_length
= bt_ctf_field_sequence_get_length(src
);
2044 /* no length set yet: keep destination sequence empty */
2048 /* copy source length */
2049 dst_length
= bt_ctf_field_copy(src_length
);
2057 /* this will initialize the destination sequence's internal array */
2058 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2065 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2067 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2068 struct bt_ctf_field
*field
=
2069 g_ptr_array_index(sequence_src
->elements
, i
);
2070 struct bt_ctf_field
*field_copy
= NULL
;
2073 field_copy
= bt_ctf_field_copy(field
);
2081 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2088 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2089 struct bt_ctf_field
*dst
)
2092 struct bt_ctf_field_string
*string_src
, *string_dst
;
2094 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2095 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2097 if (string_src
->payload
) {
2098 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2099 if (!string_dst
->payload
) {
2109 int increase_packet_size(struct ctf_stream_pos
*pos
)
2114 ret
= munmap_align(pos
->base_mma
);
2119 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2120 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2121 pos
->packet_size
/ CHAR_BIT
);
2126 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2127 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2128 if (pos
->base_mma
== MAP_FAILED
) {