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 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
299 struct bt_ctf_field
*field
)
301 struct bt_ctf_field
*ret
= NULL
;
302 struct bt_ctf_field_sequence
*sequence
;
308 if (bt_ctf_field_type_get_type_id(field
->type
) !=
313 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
314 ret
= sequence
->length
;
320 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
321 struct bt_ctf_field
*length_field
)
324 struct bt_ctf_field_type_integer
*length_type
;
325 struct bt_ctf_field_integer
*length
;
326 struct bt_ctf_field_sequence
*sequence
;
327 uint64_t sequence_length
;
329 if (!field
|| !length_field
) {
333 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
339 length_type
= container_of(length_field
->type
,
340 struct bt_ctf_field_type_integer
, parent
);
341 /* The length field must be unsigned */
342 if (length_type
->declaration
.signedness
) {
347 length
= container_of(length_field
, struct bt_ctf_field_integer
,
349 sequence_length
= length
->definition
.value
._unsigned
;
350 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
351 if (sequence
->elements
) {
352 g_ptr_array_free(sequence
->elements
, TRUE
);
353 bt_put(sequence
->length
);
356 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
357 if (!sequence
->elements
) {
362 g_ptr_array_set_free_func(sequence
->elements
,
363 (GDestroyNotify
) bt_put
);
364 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
365 bt_get(length_field
);
366 sequence
->length
= length_field
;
371 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
372 struct bt_ctf_field
*field
, const char *name
)
374 struct bt_ctf_field
*new_field
= NULL
;
376 struct bt_ctf_field_structure
*structure
;
377 struct bt_ctf_field_type
*field_type
= NULL
;
380 if (!field
|| !name
||
381 bt_ctf_field_type_get_type_id(field
->type
) !=
386 field_quark
= g_quark_from_string(name
);
387 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
389 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
391 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
392 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
396 if (structure
->fields
->pdata
[index
]) {
397 new_field
= structure
->fields
->pdata
[index
];
401 new_field
= bt_ctf_field_create(field_type
);
406 structure
->fields
->pdata
[index
] = new_field
;
416 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
417 struct bt_ctf_field
*field
, int index
)
420 const char *field_name
;
421 struct bt_ctf_field_structure
*structure
;
422 struct bt_ctf_field_type
*structure_type
;
423 struct bt_ctf_field_type
*field_type
= NULL
;
424 struct bt_ctf_field
*ret_field
= NULL
;
427 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
431 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
432 if (index
>= structure
->fields
->len
) {
436 ret_field
= structure
->fields
->pdata
[index
];
441 /* Field has not been instanciated yet, create it */
442 structure_type
= bt_ctf_field_get_type(field
);
443 if (!structure_type
) {
447 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
448 &field_name
, &field_type
, index
);
449 bt_put(structure_type
);
454 ret_field
= bt_ctf_field_create(field_type
);
459 structure
->fields
->pdata
[index
] = ret_field
;
468 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
469 const char *name
, struct bt_ctf_field
*value
)
473 struct bt_ctf_field_structure
*structure
;
474 struct bt_ctf_field_type
*expected_field_type
= NULL
;
477 if (!field
|| !name
|| !value
||
478 bt_ctf_field_type_get_type_id(field
->type
) !=
484 field_quark
= g_quark_from_string(name
);
485 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
486 expected_field_type
=
487 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
489 if (expected_field_type
!= value
->type
) {
494 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
495 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
499 if (structure
->fields
->pdata
[index
]) {
500 bt_put(structure
->fields
->pdata
[index
]);
503 structure
->fields
->pdata
[index
] = value
;
506 if (expected_field_type
) {
507 bt_put(expected_field_type
);
512 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
515 struct bt_ctf_field
*new_field
= NULL
;
516 struct bt_ctf_field_type
*field_type
= NULL
;
517 struct bt_ctf_field_array
*array
;
519 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
524 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
525 if (index
>= array
->elements
->len
) {
529 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
530 if (array
->elements
->pdata
[(size_t)index
]) {
531 new_field
= array
->elements
->pdata
[(size_t)index
];
535 new_field
= bt_ctf_field_create(field_type
);
536 array
->elements
->pdata
[(size_t)index
] = new_field
;
547 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
550 struct bt_ctf_field
*new_field
= NULL
;
551 struct bt_ctf_field_type
*field_type
= NULL
;
552 struct bt_ctf_field_sequence
*sequence
;
554 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
559 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
560 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
564 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
565 if (sequence
->elements
->pdata
[(size_t) index
]) {
566 new_field
= sequence
->elements
->pdata
[(size_t) index
];
570 new_field
= bt_ctf_field_create(field_type
);
571 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
582 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
583 struct bt_ctf_field
*tag_field
)
585 struct bt_ctf_field
*new_field
= NULL
;
586 struct bt_ctf_field_variant
*variant
;
587 struct bt_ctf_field_type_variant
*variant_type
;
588 struct bt_ctf_field_type
*field_type
;
589 struct bt_ctf_field
*tag_enum
= NULL
;
590 struct bt_ctf_field_integer
*tag_enum_integer
;
591 int64_t tag_enum_value
;
593 if (!field
|| !tag_field
||
594 bt_ctf_field_type_get_type_id(field
->type
) !=
596 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
601 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
602 variant_type
= container_of(field
->type
,
603 struct bt_ctf_field_type_variant
, parent
);
604 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
609 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
612 if (bt_ctf_field_validate(tag_field
) < 0) {
616 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
619 * If the variant currently has a tag and a payload, and if the
620 * requested tag value is the same as the current one, return
621 * the current payload instead of creating a fresh one.
623 if (variant
->tag
&& variant
->payload
) {
624 struct bt_ctf_field
*cur_tag_container
= NULL
;
625 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
626 int64_t cur_tag_value
;
629 bt_ctf_field_enumeration_get_container(variant
->tag
);
630 cur_tag_enum_integer
= container_of(cur_tag_container
,
631 struct bt_ctf_field_integer
, parent
);
632 bt_put(cur_tag_container
);
633 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
635 if (cur_tag_value
== tag_enum_value
) {
636 new_field
= variant
->payload
;
642 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
643 variant_type
, tag_enum_value
);
648 new_field
= bt_ctf_field_create(field_type
);
653 bt_put(variant
->tag
);
654 bt_put(variant
->payload
);
657 variant
->tag
= tag_field
;
658 variant
->payload
= new_field
;
664 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
665 struct bt_ctf_field
*variant_field
)
667 struct bt_ctf_field
*current_field
= NULL
;
668 struct bt_ctf_field_variant
*variant
;
670 if (!variant_field
||
671 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
676 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
679 if (variant
->payload
) {
680 current_field
= variant
->payload
;
681 bt_get(current_field
);
686 return current_field
;
689 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
690 struct bt_ctf_field
*field
)
692 struct bt_ctf_field
*container
= NULL
;
693 struct bt_ctf_field_enumeration
*enumeration
;
695 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
700 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
702 if (!enumeration
->payload
) {
703 struct bt_ctf_field_type_enumeration
*enumeration_type
=
704 container_of(field
->type
,
705 struct bt_ctf_field_type_enumeration
, parent
);
706 enumeration
->payload
=
707 bt_ctf_field_create(enumeration_type
->container
);
710 container
= enumeration
->payload
;
716 const char *bt_ctf_field_enumeration_get_mapping_name(
717 struct bt_ctf_field
*field
)
720 const char *name
= NULL
;
721 struct bt_ctf_field
*container
= NULL
;
722 struct bt_ctf_field_type
*container_type
= NULL
;
723 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
724 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
726 container
= bt_ctf_field_enumeration_get_container(field
);
731 container_type
= bt_ctf_field_get_type(container
);
732 if (!container_type
) {
733 goto error_put_container
;
736 integer_type
= container_of(container_type
,
737 struct bt_ctf_field_type_integer
, parent
);
738 enumeration_type
= container_of(field
->type
,
739 struct bt_ctf_field_type_enumeration
, parent
);
741 if (!integer_type
->declaration
.signedness
) {
743 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
746 goto error_put_container_type
;
749 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
750 enumeration_type
, value
);
753 ret
= bt_ctf_field_signed_integer_get_value(container
,
756 goto error_put_container_type
;
759 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
760 enumeration_type
, value
);
763 error_put_container_type
:
764 bt_put(container_type
);
771 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
775 struct bt_ctf_field_integer
*integer
;
776 struct bt_ctf_field_type_integer
*integer_type
;
778 if (!field
|| !value
|| !field
->payload_set
||
779 bt_ctf_field_type_get_type_id(field
->type
) !=
785 integer_type
= container_of(field
->type
,
786 struct bt_ctf_field_type_integer
, parent
);
787 if (!integer_type
->declaration
.signedness
) {
792 integer
= container_of(field
,
793 struct bt_ctf_field_integer
, parent
);
794 *value
= integer
->definition
.value
._signed
;
799 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
803 struct bt_ctf_field_integer
*integer
;
804 struct bt_ctf_field_type_integer
*integer_type
;
806 int64_t min_value
, max_value
;
809 bt_ctf_field_type_get_type_id(field
->type
) !=
815 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
816 integer_type
= container_of(field
->type
,
817 struct bt_ctf_field_type_integer
, parent
);
818 if (!integer_type
->declaration
.signedness
) {
823 size
= integer_type
->declaration
.len
;
824 min_value
= -((int64_t)1 << (size
- 1));
825 max_value
= ((int64_t)1 << (size
- 1)) - 1;
826 if (value
< min_value
|| value
> max_value
) {
831 integer
->definition
.value
._signed
= value
;
832 integer
->parent
.payload_set
= 1;
837 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
841 struct bt_ctf_field_integer
*integer
;
842 struct bt_ctf_field_type_integer
*integer_type
;
844 if (!field
|| !value
|| !field
->payload_set
||
845 bt_ctf_field_type_get_type_id(field
->type
) !=
851 integer_type
= container_of(field
->type
,
852 struct bt_ctf_field_type_integer
, parent
);
853 if (integer_type
->declaration
.signedness
) {
858 integer
= container_of(field
,
859 struct bt_ctf_field_integer
, parent
);
860 *value
= integer
->definition
.value
._unsigned
;
865 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
869 struct bt_ctf_field_integer
*integer
;
870 struct bt_ctf_field_type_integer
*integer_type
;
875 bt_ctf_field_type_get_type_id(field
->type
) !=
881 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
882 integer_type
= container_of(field
->type
,
883 struct bt_ctf_field_type_integer
, parent
);
884 if (integer_type
->declaration
.signedness
) {
889 size
= integer_type
->declaration
.len
;
890 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
891 if (value
> max_value
) {
896 integer
->definition
.value
._unsigned
= value
;
897 integer
->parent
.payload_set
= 1;
902 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
906 struct bt_ctf_field_floating_point
*floating_point
;
908 if (!field
|| !value
|| !field
->payload_set
||
909 bt_ctf_field_type_get_type_id(field
->type
) !=
915 floating_point
= container_of(field
,
916 struct bt_ctf_field_floating_point
, parent
);
917 *value
= floating_point
->definition
.value
;
922 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
926 struct bt_ctf_field_floating_point
*floating_point
;
929 bt_ctf_field_type_get_type_id(field
->type
) !=
934 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
936 floating_point
->definition
.value
= value
;
937 floating_point
->parent
.payload_set
= 1;
942 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
944 const char *ret
= NULL
;
945 struct bt_ctf_field_string
*string
;
947 if (!field
|| !field
->payload_set
||
948 bt_ctf_field_type_get_type_id(field
->type
) !=
953 string
= container_of(field
,
954 struct bt_ctf_field_string
, parent
);
955 ret
= string
->payload
->str
;
960 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
964 struct bt_ctf_field_string
*string
;
966 if (!field
|| !value
||
967 bt_ctf_field_type_get_type_id(field
->type
) !=
973 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
974 if (string
->payload
) {
975 g_string_assign(string
->payload
, value
);
977 string
->payload
= g_string_new(value
);
980 string
->parent
.payload_set
= 1;
985 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
989 struct bt_ctf_field_string
*string_field
;
991 if (!field
|| !value
||
992 bt_ctf_field_type_get_type_id(field
->type
) !=
998 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1000 if (string_field
->payload
) {
1001 g_string_append(string_field
->payload
, value
);
1003 string_field
->payload
= g_string_new(value
);
1006 string_field
->parent
.payload_set
= 1;
1012 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1013 const char *value
, unsigned int length
)
1017 unsigned int effective_length
= length
;
1018 struct bt_ctf_field_string
*string_field
;
1020 if (!field
|| !value
||
1021 bt_ctf_field_type_get_type_id(field
->type
) !=
1027 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1029 /* make sure no null bytes are appended */
1030 for (i
= 0; i
< length
; ++i
) {
1031 if (value
[i
] == '\0') {
1032 effective_length
= i
;
1037 if (string_field
->payload
) {
1038 g_string_append_len(string_field
->payload
, value
,
1041 string_field
->payload
= g_string_new_len(value
,
1045 string_field
->parent
.payload_set
= 1;
1052 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1055 enum ctf_type_id type_id
;
1062 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1063 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1068 ret
= field_validate_funcs
[type_id
](field
);
1074 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1077 enum ctf_type_id type_id
;
1084 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1085 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1090 ret
= field_reset_funcs
[type_id
](field
);
1096 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1097 struct ctf_stream_pos
*pos
)
1100 enum ctf_type_id type_id
;
1102 if (!field
|| !pos
) {
1107 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1108 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1113 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1118 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1121 struct bt_ctf_field
*copy
= NULL
;
1122 enum ctf_type_id type_id
;
1128 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1129 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1133 copy
= bt_ctf_field_create(field
->type
);
1138 copy
->payload_set
= field
->payload_set
;
1139 ret
= field_copy_funcs
[type_id
](field
, copy
);
1149 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1151 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1152 struct bt_ctf_field_type_integer
, parent
);
1153 struct bt_ctf_field_integer
*integer
= g_new0(
1154 struct bt_ctf_field_integer
, 1);
1157 integer
->definition
.declaration
= &integer_type
->declaration
;
1160 return integer
? &integer
->parent
: NULL
;
1164 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1165 struct bt_ctf_field_type
*type
)
1167 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1168 struct bt_ctf_field_enumeration
, 1);
1170 return enumeration
? &enumeration
->parent
: NULL
;
1174 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1175 struct bt_ctf_field_type
*type
)
1177 struct bt_ctf_field_floating_point
*floating_point
;
1178 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1180 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1181 if (!floating_point
) {
1185 floating_point_type
= container_of(type
,
1186 struct bt_ctf_field_type_floating_point
, parent
);
1187 floating_point
->definition
.declaration
= container_of(
1188 type
->declaration
, struct declaration_float
, p
);
1191 floating_point
->definition
.sign
= &floating_point
->sign
;
1192 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1193 floating_point
->definition
.sign
->p
.declaration
=
1194 &floating_point_type
->sign
.p
;
1196 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1197 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1198 floating_point
->definition
.mantissa
->p
.declaration
=
1199 &floating_point_type
->mantissa
.p
;
1201 floating_point
->definition
.exp
= &floating_point
->exp
;
1202 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1203 floating_point
->definition
.exp
->p
.declaration
=
1204 &floating_point_type
->exp
.p
;
1207 return floating_point
? &floating_point
->parent
: NULL
;
1211 struct bt_ctf_field
*bt_ctf_field_structure_create(
1212 struct bt_ctf_field_type
*type
)
1214 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1215 struct bt_ctf_field_type_structure
, parent
);
1216 struct bt_ctf_field_structure
*structure
= g_new0(
1217 struct bt_ctf_field_structure
, 1);
1218 struct bt_ctf_field
*field
= NULL
;
1220 if (!structure
|| !structure_type
->fields
->len
) {
1224 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1225 structure
->fields
= g_ptr_array_new_with_free_func(
1226 (GDestroyNotify
)bt_ctf_field_put
);
1227 g_ptr_array_set_size(structure
->fields
,
1228 g_hash_table_size(structure
->field_name_to_index
));
1229 field
= &structure
->parent
;
1235 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1237 struct bt_ctf_field_variant
*variant
= g_new0(
1238 struct bt_ctf_field_variant
, 1);
1239 return variant
? &variant
->parent
: NULL
;
1243 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1245 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1246 struct bt_ctf_field_type_array
*array_type
;
1247 unsigned int array_length
;
1249 if (!array
|| !type
) {
1253 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1254 array_length
= array_type
->length
;
1255 array
->elements
= g_ptr_array_sized_new(array_length
);
1256 if (!array
->elements
) {
1260 g_ptr_array_set_free_func(array
->elements
,
1261 (GDestroyNotify
)bt_ctf_field_put
);
1262 g_ptr_array_set_size(array
->elements
, array_length
);
1263 return &array
->parent
;
1270 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1271 struct bt_ctf_field_type
*type
)
1273 struct bt_ctf_field_sequence
*sequence
= g_new0(
1274 struct bt_ctf_field_sequence
, 1);
1275 return sequence
? &sequence
->parent
: NULL
;
1279 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1281 struct bt_ctf_field_string
*string
= g_new0(
1282 struct bt_ctf_field_string
, 1);
1283 return string
? &string
->parent
: NULL
;
1287 void bt_ctf_field_destroy(struct bt_object
*obj
)
1289 struct bt_ctf_field
*field
;
1290 struct bt_ctf_field_type
*type
;
1291 enum ctf_type_id type_id
;
1293 field
= container_of(obj
, struct bt_ctf_field
, base
);
1295 type_id
= bt_ctf_field_type_get_type_id(type
);
1296 if (type_id
<= CTF_TYPE_UNKNOWN
||
1297 type_id
>= NR_CTF_TYPES
) {
1301 field_destroy_funcs
[type_id
](field
);
1306 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1308 struct bt_ctf_field_integer
*integer
;
1314 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1319 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1321 struct bt_ctf_field_enumeration
*enumeration
;
1327 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1329 bt_put(enumeration
->payload
);
1330 g_free(enumeration
);
1334 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1336 struct bt_ctf_field_floating_point
*floating_point
;
1342 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1344 g_free(floating_point
);
1348 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1350 struct bt_ctf_field_structure
*structure
;
1356 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1357 g_ptr_array_free(structure
->fields
, TRUE
);
1362 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1364 struct bt_ctf_field_variant
*variant
;
1370 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1371 bt_put(variant
->tag
);
1372 bt_put(variant
->payload
);
1377 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1379 struct bt_ctf_field_array
*array
;
1385 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1386 g_ptr_array_free(array
->elements
, TRUE
);
1391 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1393 struct bt_ctf_field_sequence
*sequence
;
1399 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1400 if (sequence
->elements
) {
1401 g_ptr_array_free(sequence
->elements
, TRUE
);
1403 bt_put(sequence
->length
);
1408 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1410 struct bt_ctf_field_string
*string
;
1415 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1416 if (string
->payload
) {
1417 g_string_free(string
->payload
, TRUE
);
1423 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1425 return (field
&& field
->payload_set
) ? 0 : -1;
1429 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1432 struct bt_ctf_field_enumeration
*enumeration
;
1439 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1441 if (!enumeration
->payload
) {
1446 ret
= bt_ctf_field_validate(enumeration
->payload
);
1452 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1456 struct bt_ctf_field_structure
*structure
;
1463 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1464 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1465 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1475 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1478 struct bt_ctf_field_variant
*variant
;
1485 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1486 ret
= bt_ctf_field_validate(variant
->payload
);
1492 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1496 struct bt_ctf_field_array
*array
;
1503 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1504 for (i
= 0; i
< array
->elements
->len
; i
++) {
1505 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1515 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1519 struct bt_ctf_field_sequence
*sequence
;
1526 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1527 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1528 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1538 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1547 field
->payload_set
= 0;
1553 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1556 struct bt_ctf_field_enumeration
*enumeration
;
1563 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1565 if (!enumeration
->payload
) {
1569 ret
= bt_ctf_field_reset(enumeration
->payload
);
1575 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1579 struct bt_ctf_field_structure
*structure
;
1586 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1587 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1588 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1592 * Structure members are lazily initialized; skip if
1593 * this member has not been allocated yet.
1598 ret
= bt_ctf_field_reset(member
);
1608 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1611 struct bt_ctf_field_variant
*variant
;
1618 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1619 if (variant
->payload
) {
1620 ret
= bt_ctf_field_reset(variant
->payload
);
1627 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1631 struct bt_ctf_field_array
*array
;
1638 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1639 for (i
= 0; i
< array
->elements
->len
; i
++) {
1640 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1644 * Array elements are lazily initialized; skip if
1645 * this member has not been allocated yet.
1650 ret
= bt_ctf_field_reset(member
);
1660 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1664 struct bt_ctf_field_sequence
*sequence
;
1671 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1672 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1673 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1677 * Sequence elements are lazily initialized; skip if
1678 * this member has not been allocated yet.
1683 ret
= bt_ctf_field_reset(member
);
1693 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1696 struct bt_ctf_field_string
*string
;
1703 ret
= bt_ctf_field_generic_reset(field
);
1708 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1709 if (string
->payload
) {
1710 g_string_truncate(string
->payload
, 0);
1717 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1718 struct ctf_stream_pos
*pos
)
1721 struct bt_ctf_field_integer
*integer
= container_of(field
,
1722 struct bt_ctf_field_integer
, parent
);
1725 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1726 if (ret
== -EFAULT
) {
1728 * The field is too large to fit in the current packet's
1729 * remaining space. Bump the packet size and retry.
1731 ret
= increase_packet_size(pos
);
1742 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1743 struct ctf_stream_pos
*pos
)
1745 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1746 field
, struct bt_ctf_field_enumeration
, parent
);
1748 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1752 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1753 struct ctf_stream_pos
*pos
)
1756 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1757 struct bt_ctf_field_floating_point
, parent
);
1760 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1761 if (ret
== -EFAULT
) {
1763 * The field is too large to fit in the current packet's
1764 * remaining space. Bump the packet size and retry.
1766 ret
= increase_packet_size(pos
);
1777 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1778 struct ctf_stream_pos
*pos
)
1782 struct bt_ctf_field_structure
*structure
= container_of(
1783 field
, struct bt_ctf_field_structure
, parent
);
1785 while (!ctf_pos_access_ok(pos
,
1786 offset_align(pos
->offset
,
1787 field
->type
->declaration
->alignment
))) {
1788 ret
= increase_packet_size(pos
);
1794 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1799 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1800 struct bt_ctf_field
*field
= g_ptr_array_index(
1801 structure
->fields
, i
);
1803 ret
= bt_ctf_field_serialize(field
, pos
);
1813 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1814 struct ctf_stream_pos
*pos
)
1816 struct bt_ctf_field_variant
*variant
= container_of(
1817 field
, struct bt_ctf_field_variant
, parent
);
1819 return bt_ctf_field_serialize(variant
->payload
, pos
);
1823 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1824 struct ctf_stream_pos
*pos
)
1828 struct bt_ctf_field_array
*array
= container_of(
1829 field
, struct bt_ctf_field_array
, parent
);
1831 for (i
= 0; i
< array
->elements
->len
; i
++) {
1832 ret
= bt_ctf_field_serialize(
1833 g_ptr_array_index(array
->elements
, i
), pos
);
1843 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1844 struct ctf_stream_pos
*pos
)
1848 struct bt_ctf_field_sequence
*sequence
= container_of(
1849 field
, struct bt_ctf_field_sequence
, parent
);
1851 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1852 ret
= bt_ctf_field_serialize(
1853 g_ptr_array_index(sequence
->elements
, i
), pos
);
1863 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1864 struct ctf_stream_pos
*pos
)
1868 struct bt_ctf_field_string
*string
= container_of(field
,
1869 struct bt_ctf_field_string
, parent
);
1870 struct bt_ctf_field_type
*character_type
=
1871 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1872 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1874 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1875 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1876 (uint64_t) string
->payload
->str
[i
]);
1881 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1888 bt_put(character_type
);
1893 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1894 struct bt_ctf_field
*dst
)
1896 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1898 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1899 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1901 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1902 sizeof(struct definition_integer
));
1907 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1908 struct bt_ctf_field
*dst
)
1911 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1913 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1914 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1916 if (enum_src
->payload
) {
1917 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1918 if (!enum_dst
->payload
) {
1928 int bt_ctf_field_floating_point_copy(
1929 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1931 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1933 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1935 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1938 memcpy(&float_dst
->definition
, &float_src
->definition
,
1939 sizeof(struct definition_float
));
1940 memcpy(&float_dst
->sign
, &float_src
->sign
,
1941 sizeof(struct definition_integer
));
1942 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1943 sizeof(struct definition_integer
));
1944 memcpy(&float_dst
->exp
, &float_src
->exp
,
1945 sizeof(struct definition_integer
));
1950 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
1951 struct bt_ctf_field
*dst
)
1954 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
1956 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
1957 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
1959 /* This field_name_to_index HT is owned by the structure field type */
1960 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
1961 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
1963 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
1964 struct bt_ctf_field
*field
=
1965 g_ptr_array_index(struct_src
->fields
, i
);
1966 struct bt_ctf_field
*field_copy
= NULL
;
1969 field_copy
= bt_ctf_field_copy(field
);
1977 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
1984 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
1985 struct bt_ctf_field
*dst
)
1988 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
1990 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
1991 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
1993 if (variant_src
->tag
) {
1994 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
1995 if (!variant_dst
->tag
) {
2000 if (variant_src
->payload
) {
2001 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2002 if (!variant_dst
->payload
) {
2012 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2013 struct bt_ctf_field
*dst
)
2016 struct bt_ctf_field_array
*array_src
, *array_dst
;
2018 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2019 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2021 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2022 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2023 struct bt_ctf_field
*field
=
2024 g_ptr_array_index(array_src
->elements
, i
);
2025 struct bt_ctf_field
*field_copy
= NULL
;
2028 field_copy
= bt_ctf_field_copy(field
);
2036 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2043 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2044 struct bt_ctf_field
*dst
)
2047 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2048 struct bt_ctf_field
*src_length
;
2049 struct bt_ctf_field
*dst_length
;
2051 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2052 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2054 src_length
= bt_ctf_field_sequence_get_length(src
);
2057 /* no length set yet: keep destination sequence empty */
2061 /* copy source length */
2062 dst_length
= bt_ctf_field_copy(src_length
);
2070 /* this will initialize the destination sequence's internal array */
2071 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2078 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2080 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2081 struct bt_ctf_field
*field
=
2082 g_ptr_array_index(sequence_src
->elements
, i
);
2083 struct bt_ctf_field
*field_copy
= NULL
;
2086 field_copy
= bt_ctf_field_copy(field
);
2094 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2101 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2102 struct bt_ctf_field
*dst
)
2105 struct bt_ctf_field_string
*string_src
, *string_dst
;
2107 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2108 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2110 if (string_src
->payload
) {
2111 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2112 if (!string_dst
->payload
) {
2122 int increase_packet_size(struct ctf_stream_pos
*pos
)
2127 ret
= munmap_align(pos
->base_mma
);
2132 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2133 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2134 pos
->packet_size
/ CHAR_BIT
);
2139 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2140 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2141 if (pos
->base_mma
== MAP_FAILED
) {