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/compiler.h>
34 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
37 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
39 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
40 struct bt_ctf_field_type
*);
42 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
43 struct bt_ctf_field_type
*);
45 struct bt_ctf_field
*bt_ctf_field_structure_create(
46 struct bt_ctf_field_type
*);
48 struct bt_ctf_field
*bt_ctf_field_variant_create(
49 struct bt_ctf_field_type
*);
51 struct bt_ctf_field
*bt_ctf_field_array_create(
52 struct bt_ctf_field_type
*);
54 struct bt_ctf_field
*bt_ctf_field_sequence_create(
55 struct bt_ctf_field_type
*);
57 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
60 void bt_ctf_field_destroy(struct bt_ctf_ref
*);
62 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
64 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
66 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
68 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
70 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
72 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
74 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
76 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
79 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
);
81 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
);
83 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
);
85 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
);
87 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
);
89 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
);
92 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
);
94 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
);
96 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
);
98 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
);
100 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
);
102 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
);
104 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
);
107 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
108 struct ctf_stream_pos
*);
110 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
111 struct ctf_stream_pos
*);
113 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
114 struct ctf_stream_pos
*);
116 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
117 struct ctf_stream_pos
*);
119 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
120 struct ctf_stream_pos
*);
122 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
123 struct ctf_stream_pos
*);
125 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
126 struct ctf_stream_pos
*);
128 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
129 struct ctf_stream_pos
*);
132 int bt_ctf_field_integer_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
134 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
136 int bt_ctf_field_floating_point_copy(struct bt_ctf_field
*,
137 struct bt_ctf_field
*);
139 int bt_ctf_field_structure_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
141 int bt_ctf_field_variant_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
143 int bt_ctf_field_array_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
145 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
147 int bt_ctf_field_string_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
150 int increase_packet_size(struct ctf_stream_pos
*pos
);
153 struct bt_ctf_field
*(*field_create_funcs
[])(
154 struct bt_ctf_field_type
*) = {
155 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_create
,
156 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_create
,
158 bt_ctf_field_floating_point_create
,
159 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_create
,
160 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_create
,
161 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_create
,
162 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_create
,
163 [CTF_TYPE_STRING
] = bt_ctf_field_string_create
,
167 void (*field_destroy_funcs
[])(struct bt_ctf_field
*) = {
168 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_destroy
,
169 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_destroy
,
171 bt_ctf_field_floating_point_destroy
,
172 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_destroy
,
173 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_destroy
,
174 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_destroy
,
175 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
176 [CTF_TYPE_STRING
] = bt_ctf_field_string_destroy
,
180 int (*field_validate_funcs
[])(struct bt_ctf_field
*) = {
181 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_validate
,
182 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_validate
,
183 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_validate
,
184 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_validate
,
185 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_validate
,
186 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_validate
,
187 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_validate
,
188 [CTF_TYPE_STRING
] = bt_ctf_field_generic_validate
,
192 int (*field_reset_funcs
[])(struct bt_ctf_field
*) = {
193 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_reset
,
194 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_reset
,
195 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_reset
,
196 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_reset
,
197 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_reset
,
198 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_reset
,
199 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_reset
,
200 [CTF_TYPE_STRING
] = bt_ctf_field_string_reset
,
204 int (*field_serialize_funcs
[])(struct bt_ctf_field
*,
205 struct ctf_stream_pos
*) = {
206 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_serialize
,
207 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_serialize
,
209 bt_ctf_field_floating_point_serialize
,
210 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_serialize
,
211 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_serialize
,
212 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_serialize
,
213 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
214 [CTF_TYPE_STRING
] = bt_ctf_field_string_serialize
,
218 int (*field_copy_funcs
[])(struct bt_ctf_field
*, struct bt_ctf_field
*) = {
219 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_copy
,
220 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_copy
,
221 [CTF_TYPE_FLOAT
] = bt_ctf_field_floating_point_copy
,
222 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_copy
,
223 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_copy
,
224 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_copy
,
225 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_copy
,
226 [CTF_TYPE_STRING
] = bt_ctf_field_string_copy
,
229 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
231 struct bt_ctf_field
*field
= NULL
;
232 enum ctf_type_id type_id
;
238 type_id
= bt_ctf_field_type_get_type_id(type
);
239 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
||
240 bt_ctf_field_type_validate(type
)) {
244 field
= field_create_funcs
[type_id
](type
);
249 /* The type's declaration can't change after this point */
250 bt_ctf_field_type_freeze(type
);
251 bt_ctf_field_type_get(type
);
252 bt_ctf_ref_init(&field
->ref_count
);
258 void bt_ctf_field_get(struct bt_ctf_field
*field
)
261 bt_ctf_ref_get(&field
->ref_count
);
265 void bt_ctf_field_put(struct bt_ctf_field
*field
)
268 bt_ctf_ref_put(&field
->ref_count
, bt_ctf_field_destroy
);
272 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
274 struct bt_ctf_field_type
*ret
= NULL
;
281 bt_ctf_field_type_get(ret
);
286 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
287 struct bt_ctf_field
*field
)
289 struct bt_ctf_field
*ret
= NULL
;
290 struct bt_ctf_field_sequence
*sequence
;
296 if (bt_ctf_field_type_get_type_id(field
->type
) !=
301 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
302 ret
= sequence
->length
;
303 bt_ctf_field_get(ret
);
308 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
309 struct bt_ctf_field
*length_field
)
312 struct bt_ctf_field_type_integer
*length_type
;
313 struct bt_ctf_field_integer
*length
;
314 struct bt_ctf_field_sequence
*sequence
;
315 uint64_t sequence_length
;
317 if (!field
|| !length_field
) {
321 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
327 length_type
= container_of(length_field
->type
,
328 struct bt_ctf_field_type_integer
, parent
);
329 /* The length field must be unsigned */
330 if (length_type
->declaration
.signedness
) {
335 length
= container_of(length_field
, struct bt_ctf_field_integer
,
337 sequence_length
= length
->definition
.value
._unsigned
;
338 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
339 if (sequence
->elements
) {
340 g_ptr_array_free(sequence
->elements
, TRUE
);
341 bt_ctf_field_put(sequence
->length
);
344 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
345 if (!sequence
->elements
) {
350 g_ptr_array_set_free_func(sequence
->elements
,
351 (GDestroyNotify
)bt_ctf_field_put
);
352 g_ptr_array_set_size(sequence
->elements
, (size_t)sequence_length
);
353 bt_ctf_field_get(length_field
);
354 sequence
->length
= length_field
;
359 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
360 struct bt_ctf_field
*field
, const char *name
)
362 struct bt_ctf_field
*new_field
= NULL
;
364 struct bt_ctf_field_structure
*structure
;
365 struct bt_ctf_field_type
*field_type
= NULL
;
368 if (!field
|| !name
||
369 bt_ctf_field_type_get_type_id(field
->type
) !=
374 field_quark
= g_quark_from_string(name
);
375 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
377 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
379 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
380 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
384 if (structure
->fields
->pdata
[index
]) {
385 new_field
= structure
->fields
->pdata
[index
];
389 new_field
= bt_ctf_field_create(field_type
);
394 structure
->fields
->pdata
[index
] = new_field
;
396 bt_ctf_field_get(new_field
);
399 bt_ctf_field_type_put(field_type
);
404 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
405 struct bt_ctf_field
*field
, int index
)
408 const char *field_name
;
409 struct bt_ctf_field_structure
*structure
;
410 struct bt_ctf_field_type
*structure_type
;
411 struct bt_ctf_field_type
*field_type
= NULL
;
412 struct bt_ctf_field
*ret_field
= NULL
;
415 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
419 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
420 if (index
>= structure
->fields
->len
) {
424 ret_field
= structure
->fields
->pdata
[index
];
429 /* Field has not been instanciated yet, create it */
430 structure_type
= bt_ctf_field_get_type(field
);
431 if (!structure_type
) {
435 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
436 &field_name
, &field_type
, index
);
437 bt_ctf_field_type_put(structure_type
);
442 ret_field
= bt_ctf_field_create(field_type
);
447 structure
->fields
->pdata
[index
] = ret_field
;
449 bt_ctf_field_get(ret_field
);
452 bt_ctf_field_type_put(field_type
);
458 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
459 const char *name
, struct bt_ctf_field
*value
)
463 struct bt_ctf_field_structure
*structure
;
464 struct bt_ctf_field_type
*expected_field_type
= NULL
;
467 if (!field
|| !name
|| !value
||
468 bt_ctf_field_type_get_type_id(field
->type
) !=
474 field_quark
= g_quark_from_string(name
);
475 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
476 expected_field_type
=
477 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
479 if (expected_field_type
!= value
->type
) {
484 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
485 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
489 if (structure
->fields
->pdata
[index
]) {
490 bt_ctf_field_put(structure
->fields
->pdata
[index
]);
493 structure
->fields
->pdata
[index
] = value
;
494 bt_ctf_field_get(value
);
496 if (expected_field_type
) {
497 bt_ctf_field_type_put(expected_field_type
);
502 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
505 struct bt_ctf_field
*new_field
= NULL
;
506 struct bt_ctf_field_type
*field_type
= NULL
;
507 struct bt_ctf_field_array
*array
;
509 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
514 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
515 if (index
>= array
->elements
->len
) {
519 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
520 if (array
->elements
->pdata
[(size_t)index
]) {
521 new_field
= array
->elements
->pdata
[(size_t)index
];
525 new_field
= bt_ctf_field_create(field_type
);
526 bt_ctf_field_get(new_field
);
527 array
->elements
->pdata
[(size_t)index
] = new_field
;
530 bt_ctf_field_type_put(field_type
);
535 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
538 struct bt_ctf_field
*new_field
= NULL
;
539 struct bt_ctf_field_type
*field_type
= NULL
;
540 struct bt_ctf_field_sequence
*sequence
;
542 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
547 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
548 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
552 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
553 if (sequence
->elements
->pdata
[(size_t)index
]) {
554 new_field
= sequence
->elements
->pdata
[(size_t)index
];
558 new_field
= bt_ctf_field_create(field_type
);
559 bt_ctf_field_get(new_field
);
560 sequence
->elements
->pdata
[(size_t)index
] = new_field
;
563 bt_ctf_field_type_put(field_type
);
568 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
569 struct bt_ctf_field
*tag_field
)
571 struct bt_ctf_field
*new_field
= NULL
;
572 struct bt_ctf_field_variant
*variant
;
573 struct bt_ctf_field_type_variant
*variant_type
;
574 struct bt_ctf_field_type
*field_type
;
575 struct bt_ctf_field
*tag_enum
= NULL
;
576 struct bt_ctf_field_integer
*tag_enum_integer
;
577 int64_t tag_enum_value
;
579 if (!field
|| !tag_field
||
580 bt_ctf_field_type_get_type_id(field
->type
) !=
582 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
587 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
588 variant_type
= container_of(field
->type
,
589 struct bt_ctf_field_type_variant
, parent
);
590 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
595 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
598 if (bt_ctf_field_validate(tag_field
) < 0) {
602 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
603 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
604 variant_type
, tag_enum_value
);
609 new_field
= bt_ctf_field_create(field_type
);
614 bt_ctf_field_put(variant
->tag
);
615 bt_ctf_field_put(variant
->payload
);
616 bt_ctf_field_get(new_field
);
617 bt_ctf_field_get(tag_field
);
618 variant
->tag
= tag_field
;
619 variant
->payload
= new_field
;
621 bt_ctf_field_put(tag_enum
);
625 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
626 struct bt_ctf_field
*field
)
628 struct bt_ctf_field
*container
= NULL
;
629 struct bt_ctf_field_enumeration
*enumeration
;
631 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
636 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
638 if (!enumeration
->payload
) {
639 struct bt_ctf_field_type_enumeration
*enumeration_type
=
640 container_of(field
->type
,
641 struct bt_ctf_field_type_enumeration
, parent
);
642 enumeration
->payload
=
643 bt_ctf_field_create(enumeration_type
->container
);
646 container
= enumeration
->payload
;
647 bt_ctf_field_get(container
);
652 const char *bt_ctf_field_enumeration_get_mapping_name(
653 struct bt_ctf_field
*field
)
656 const char *name
= NULL
;
657 struct bt_ctf_field
*container
= NULL
;
658 struct bt_ctf_field_type
*container_type
= NULL
;
659 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
660 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
662 container
= bt_ctf_field_enumeration_get_container(field
);
667 container_type
= bt_ctf_field_get_type(container
);
668 if (!container_type
) {
669 goto error_put_container
;
672 integer_type
= container_of(container_type
,
673 struct bt_ctf_field_type_integer
, parent
);
674 enumeration_type
= container_of(field
->type
,
675 struct bt_ctf_field_type_enumeration
, parent
);
677 if (!integer_type
->declaration
.signedness
) {
679 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
682 goto error_put_container_type
;
685 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
686 enumeration_type
, value
);
689 ret
= bt_ctf_field_signed_integer_get_value(container
,
692 goto error_put_container_type
;
695 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
696 enumeration_type
, value
);
699 error_put_container_type
:
700 bt_ctf_field_type_put(container_type
);
702 bt_ctf_field_put(container
);
707 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
711 struct bt_ctf_field_integer
*integer
;
712 struct bt_ctf_field_type_integer
*integer_type
;
714 if (!field
|| !value
|| !field
->payload_set
||
715 bt_ctf_field_type_get_type_id(field
->type
) !=
721 integer_type
= container_of(field
->type
,
722 struct bt_ctf_field_type_integer
, parent
);
723 if (!integer_type
->declaration
.signedness
) {
728 integer
= container_of(field
,
729 struct bt_ctf_field_integer
, parent
);
730 *value
= integer
->definition
.value
._signed
;
735 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
739 struct bt_ctf_field_integer
*integer
;
740 struct bt_ctf_field_type_integer
*integer_type
;
742 int64_t min_value
, max_value
;
745 bt_ctf_field_type_get_type_id(field
->type
) !=
751 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
752 integer_type
= container_of(field
->type
,
753 struct bt_ctf_field_type_integer
, parent
);
754 if (!integer_type
->declaration
.signedness
) {
759 size
= integer_type
->declaration
.len
;
760 min_value
= -((int64_t)1 << (size
- 1));
761 max_value
= ((int64_t)1 << (size
- 1)) - 1;
762 if (value
< min_value
|| value
> max_value
) {
767 integer
->definition
.value
._signed
= value
;
768 integer
->parent
.payload_set
= 1;
773 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
777 struct bt_ctf_field_integer
*integer
;
778 struct bt_ctf_field_type_integer
*integer_type
;
780 if (!field
|| !value
|| !field
->payload_set
||
781 bt_ctf_field_type_get_type_id(field
->type
) !=
787 integer_type
= container_of(field
->type
,
788 struct bt_ctf_field_type_integer
, parent
);
789 if (integer_type
->declaration
.signedness
) {
794 integer
= container_of(field
,
795 struct bt_ctf_field_integer
, parent
);
796 *value
= integer
->definition
.value
._unsigned
;
801 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
805 struct bt_ctf_field_integer
*integer
;
806 struct bt_ctf_field_type_integer
*integer_type
;
811 bt_ctf_field_type_get_type_id(field
->type
) !=
817 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
818 integer_type
= container_of(field
->type
,
819 struct bt_ctf_field_type_integer
, parent
);
820 if (integer_type
->declaration
.signedness
) {
825 size
= integer_type
->declaration
.len
;
826 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
827 if (value
> max_value
) {
832 integer
->definition
.value
._unsigned
= value
;
833 integer
->parent
.payload_set
= 1;
838 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
842 struct bt_ctf_field_floating_point
*floating_point
;
844 if (!field
|| !value
|| !field
->payload_set
||
845 bt_ctf_field_type_get_type_id(field
->type
) !=
851 floating_point
= container_of(field
,
852 struct bt_ctf_field_floating_point
, parent
);
853 *value
= floating_point
->definition
.value
;
858 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
862 struct bt_ctf_field_floating_point
*floating_point
;
865 bt_ctf_field_type_get_type_id(field
->type
) !=
870 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
872 floating_point
->definition
.value
= value
;
873 floating_point
->parent
.payload_set
= 1;
878 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
880 const char *ret
= NULL
;
881 struct bt_ctf_field_string
*string
;
883 if (!field
|| !field
->payload_set
||
884 bt_ctf_field_type_get_type_id(field
->type
) !=
889 string
= container_of(field
,
890 struct bt_ctf_field_string
, parent
);
891 ret
= string
->payload
->str
;
896 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
900 struct bt_ctf_field_string
*string
;
902 if (!field
|| !value
||
903 bt_ctf_field_type_get_type_id(field
->type
) !=
909 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
910 if (string
->payload
) {
911 g_string_assign(string
->payload
, value
);
913 string
->payload
= g_string_new(value
);
916 string
->parent
.payload_set
= 1;
922 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
925 enum ctf_type_id type_id
;
932 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
933 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
938 ret
= field_validate_funcs
[type_id
](field
);
944 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
947 enum ctf_type_id type_id
;
954 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
955 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
960 ret
= field_reset_funcs
[type_id
](field
);
966 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
967 struct ctf_stream_pos
*pos
)
970 enum ctf_type_id type_id
;
972 if (!field
|| !pos
) {
977 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
978 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
983 ret
= field_serialize_funcs
[type_id
](field
, pos
);
989 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
992 struct bt_ctf_field
*copy
= NULL
;
993 enum ctf_type_id type_id
;
999 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1000 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1004 copy
= bt_ctf_field_create(field
->type
);
1009 bt_ctf_field_type_get(field
->type
);
1010 copy
->type
= field
->type
;
1011 ret
= field_copy_funcs
[type_id
](field
, copy
);
1013 bt_ctf_field_put(copy
);
1021 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1023 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1024 struct bt_ctf_field_type_integer
, parent
);
1025 struct bt_ctf_field_integer
*integer
= g_new0(
1026 struct bt_ctf_field_integer
, 1);
1029 integer
->definition
.declaration
= &integer_type
->declaration
;
1032 return integer
? &integer
->parent
: NULL
;
1036 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1037 struct bt_ctf_field_type
*type
)
1039 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1040 struct bt_ctf_field_enumeration
, 1);
1042 return enumeration
? &enumeration
->parent
: NULL
;
1046 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1047 struct bt_ctf_field_type
*type
)
1049 struct bt_ctf_field_floating_point
*floating_point
;
1050 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1052 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1053 if (!floating_point
) {
1057 floating_point_type
= container_of(type
,
1058 struct bt_ctf_field_type_floating_point
, parent
);
1059 floating_point
->definition
.declaration
= container_of(
1060 type
->declaration
, struct declaration_float
, p
);
1063 floating_point
->definition
.sign
= &floating_point
->sign
;
1064 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1065 floating_point
->definition
.sign
->p
.declaration
=
1066 &floating_point_type
->sign
.p
;
1068 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1069 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1070 floating_point
->definition
.mantissa
->p
.declaration
=
1071 &floating_point_type
->mantissa
.p
;
1073 floating_point
->definition
.exp
= &floating_point
->exp
;
1074 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1075 floating_point
->definition
.exp
->p
.declaration
=
1076 &floating_point_type
->exp
.p
;
1079 return floating_point
? &floating_point
->parent
: NULL
;
1083 struct bt_ctf_field
*bt_ctf_field_structure_create(
1084 struct bt_ctf_field_type
*type
)
1086 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1087 struct bt_ctf_field_type_structure
, parent
);
1088 struct bt_ctf_field_structure
*structure
= g_new0(
1089 struct bt_ctf_field_structure
, 1);
1090 struct bt_ctf_field
*field
= NULL
;
1092 if (!structure
|| !structure_type
->fields
->len
) {
1096 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1097 structure
->fields
= g_ptr_array_new_with_free_func(
1098 (GDestroyNotify
)bt_ctf_field_put
);
1099 g_ptr_array_set_size(structure
->fields
,
1100 g_hash_table_size(structure
->field_name_to_index
));
1101 field
= &structure
->parent
;
1107 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1109 struct bt_ctf_field_variant
*variant
= g_new0(
1110 struct bt_ctf_field_variant
, 1);
1111 return variant
? &variant
->parent
: NULL
;
1115 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1117 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1118 struct bt_ctf_field_type_array
*array_type
;
1119 unsigned int array_length
;
1121 if (!array
|| !type
) {
1125 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1126 array_length
= array_type
->length
;
1127 array
->elements
= g_ptr_array_sized_new(array_length
);
1128 if (!array
->elements
) {
1132 g_ptr_array_set_free_func(array
->elements
,
1133 (GDestroyNotify
)bt_ctf_field_put
);
1134 g_ptr_array_set_size(array
->elements
, array_length
);
1135 return &array
->parent
;
1142 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1143 struct bt_ctf_field_type
*type
)
1145 struct bt_ctf_field_sequence
*sequence
= g_new0(
1146 struct bt_ctf_field_sequence
, 1);
1147 return sequence
? &sequence
->parent
: NULL
;
1151 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1153 struct bt_ctf_field_string
*string
= g_new0(
1154 struct bt_ctf_field_string
, 1);
1155 return string
? &string
->parent
: NULL
;
1159 void bt_ctf_field_destroy(struct bt_ctf_ref
*ref
)
1161 struct bt_ctf_field
*field
;
1162 struct bt_ctf_field_type
*type
;
1163 enum ctf_type_id type_id
;
1169 field
= container_of(ref
, struct bt_ctf_field
, ref_count
);
1171 type_id
= bt_ctf_field_type_get_type_id(type
);
1172 if (type_id
<= CTF_TYPE_UNKNOWN
||
1173 type_id
>= NR_CTF_TYPES
) {
1177 field_destroy_funcs
[type_id
](field
);
1179 bt_ctf_field_type_put(type
);
1184 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1186 struct bt_ctf_field_integer
*integer
;
1192 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1197 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1199 struct bt_ctf_field_enumeration
*enumeration
;
1205 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1207 bt_ctf_field_put(enumeration
->payload
);
1208 g_free(enumeration
);
1212 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1214 struct bt_ctf_field_floating_point
*floating_point
;
1220 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1222 g_free(floating_point
);
1226 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1228 struct bt_ctf_field_structure
*structure
;
1234 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1235 g_ptr_array_free(structure
->fields
, TRUE
);
1240 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1242 struct bt_ctf_field_variant
*variant
;
1248 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1249 bt_ctf_field_put(variant
->tag
);
1250 bt_ctf_field_put(variant
->payload
);
1255 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1257 struct bt_ctf_field_array
*array
;
1263 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1264 g_ptr_array_free(array
->elements
, TRUE
);
1269 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1271 struct bt_ctf_field_sequence
*sequence
;
1277 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1278 g_ptr_array_free(sequence
->elements
, TRUE
);
1279 bt_ctf_field_put(sequence
->length
);
1284 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1286 struct bt_ctf_field_string
*string
;
1291 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1292 g_string_free(string
->payload
, TRUE
);
1297 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1299 return (field
&& field
->payload_set
) ? 0 : -1;
1303 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1306 struct bt_ctf_field_enumeration
*enumeration
;
1313 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1315 if (!enumeration
->payload
) {
1320 ret
= bt_ctf_field_validate(enumeration
->payload
);
1326 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1330 struct bt_ctf_field_structure
*structure
;
1337 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1338 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1339 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1349 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1352 struct bt_ctf_field_variant
*variant
;
1359 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1360 ret
= bt_ctf_field_validate(variant
->payload
);
1366 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1370 struct bt_ctf_field_array
*array
;
1377 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1378 for (i
= 0; i
< array
->elements
->len
; i
++) {
1379 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1389 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1393 struct bt_ctf_field_sequence
*sequence
;
1400 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1401 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1402 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1412 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1421 field
->payload_set
= 0;
1427 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1430 struct bt_ctf_field_enumeration
*enumeration
;
1437 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1439 if (!enumeration
->payload
) {
1443 ret
= bt_ctf_field_reset(enumeration
->payload
);
1449 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1453 struct bt_ctf_field_structure
*structure
;
1460 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1461 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1462 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1466 * Structure members are lazily initialized; skip if
1467 * this member has not been allocated yet.
1472 ret
= bt_ctf_field_reset(member
);
1482 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1485 struct bt_ctf_field_variant
*variant
;
1492 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1493 if (variant
->payload
) {
1494 ret
= bt_ctf_field_reset(variant
->payload
);
1501 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1505 struct bt_ctf_field_array
*array
;
1512 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1513 for (i
= 0; i
< array
->elements
->len
; i
++) {
1514 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1518 * Array elements are lazily initialized; skip if
1519 * this member has not been allocated yet.
1524 ret
= bt_ctf_field_reset(member
);
1534 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1538 struct bt_ctf_field_sequence
*sequence
;
1545 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1546 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1547 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1551 * Sequence elements are lazily initialized; skip if
1552 * this member has not been allocated yet.
1557 ret
= bt_ctf_field_reset(member
);
1567 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1570 struct bt_ctf_field_string
*string
;
1577 ret
= bt_ctf_field_generic_reset(field
);
1582 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1583 if (string
->payload
) {
1584 g_string_truncate(string
->payload
, 0);
1591 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1592 struct ctf_stream_pos
*pos
)
1595 struct bt_ctf_field_integer
*integer
= container_of(field
,
1596 struct bt_ctf_field_integer
, parent
);
1599 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1600 if (ret
== -EFAULT
) {
1602 * The field is too large to fit in the current packet's
1603 * remaining space. Bump the packet size and retry.
1605 ret
= increase_packet_size(pos
);
1616 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1617 struct ctf_stream_pos
*pos
)
1619 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1620 field
, struct bt_ctf_field_enumeration
, parent
);
1622 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1626 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1627 struct ctf_stream_pos
*pos
)
1630 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1631 struct bt_ctf_field_floating_point
, parent
);
1634 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1635 if (ret
== -EFAULT
) {
1637 * The field is too large to fit in the current packet's
1638 * remaining space. Bump the packet size and retry.
1640 ret
= increase_packet_size(pos
);
1651 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1652 struct ctf_stream_pos
*pos
)
1656 struct bt_ctf_field_structure
*structure
= container_of(
1657 field
, struct bt_ctf_field_structure
, parent
);
1659 while (!ctf_pos_access_ok(pos
,
1660 offset_align(pos
->offset
,
1661 field
->type
->declaration
->alignment
))) {
1662 ret
= increase_packet_size(pos
);
1668 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1673 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1674 struct bt_ctf_field
*field
= g_ptr_array_index(
1675 structure
->fields
, i
);
1677 ret
= bt_ctf_field_serialize(field
, pos
);
1687 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1688 struct ctf_stream_pos
*pos
)
1690 struct bt_ctf_field_variant
*variant
= container_of(
1691 field
, struct bt_ctf_field_variant
, parent
);
1693 return bt_ctf_field_serialize(variant
->payload
, pos
);
1697 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1698 struct ctf_stream_pos
*pos
)
1702 struct bt_ctf_field_array
*array
= container_of(
1703 field
, struct bt_ctf_field_array
, parent
);
1705 for (i
= 0; i
< array
->elements
->len
; i
++) {
1706 ret
= bt_ctf_field_serialize(
1707 g_ptr_array_index(array
->elements
, i
), pos
);
1717 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1718 struct ctf_stream_pos
*pos
)
1722 struct bt_ctf_field_sequence
*sequence
= container_of(
1723 field
, struct bt_ctf_field_sequence
, parent
);
1725 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1726 ret
= bt_ctf_field_serialize(
1727 g_ptr_array_index(sequence
->elements
, i
), pos
);
1737 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1738 struct ctf_stream_pos
*pos
)
1742 struct bt_ctf_field_string
*string
= container_of(field
,
1743 struct bt_ctf_field_string
, parent
);
1744 struct bt_ctf_field_type
*character_type
=
1745 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1746 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1748 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1749 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1750 (uint64_t) string
->payload
->str
[i
]);
1755 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1761 bt_ctf_field_put(character
);
1762 bt_ctf_field_type_put(character_type
);
1767 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1768 struct bt_ctf_field
*dst
)
1770 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1772 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1773 integer_dst
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1775 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1776 sizeof(struct definition_integer
));
1781 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1782 struct bt_ctf_field
*dst
)
1785 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1787 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1788 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1790 if (enum_src
->payload
) {
1791 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1792 if (!enum_dst
->payload
) {
1802 int bt_ctf_field_floating_point_copy(
1803 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1805 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1807 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1809 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1812 memcpy(&float_dst
->definition
, &float_src
->definition
,
1813 sizeof(struct definition_float
));
1814 memcpy(&float_dst
->sign
, &float_src
->sign
,
1815 sizeof(struct definition_integer
));
1816 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1817 sizeof(struct definition_integer
));
1818 memcpy(&float_dst
->exp
, &float_src
->exp
,
1819 sizeof(struct definition_integer
));
1824 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
1825 struct bt_ctf_field
*dst
)
1828 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
1830 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
1831 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
1833 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
1834 struct_dst
->fields
= g_ptr_array_sized_new(struct_src
->fields
->len
);
1835 if (!struct_dst
->fields
) {
1840 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
1841 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1842 g_ptr_array_index(struct_src
->fields
, i
));
1848 g_ptr_array_add(struct_dst
->fields
, field_copy
);
1855 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
1856 struct bt_ctf_field
*dst
)
1859 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
1861 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
1862 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
1864 if (variant_src
->tag
) {
1865 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
1866 if (!variant_dst
->tag
) {
1871 if (variant_src
->payload
) {
1872 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
1873 if (!variant_dst
->payload
) {
1883 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
1884 struct bt_ctf_field
*dst
)
1887 struct bt_ctf_field_array
*array_src
, *array_dst
;
1889 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
1890 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
1892 array_dst
->elements
= g_ptr_array_sized_new(array_src
->elements
->len
);
1893 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
1894 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1895 g_ptr_array_index(array_src
->elements
, i
));
1901 g_ptr_array_add(array_dst
->elements
, field_copy
);
1908 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
1909 struct bt_ctf_field
*dst
)
1912 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
1914 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
1915 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
1917 sequence_dst
->elements
= g_ptr_array_sized_new(
1918 sequence_src
->elements
->len
);
1919 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
1920 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1921 g_ptr_array_index(sequence_src
->elements
, i
));
1927 g_ptr_array_add(sequence_dst
->elements
, field_copy
);
1934 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
1935 struct bt_ctf_field
*dst
)
1938 struct bt_ctf_field_string
*string_src
, *string_dst
;
1940 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
1941 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
1943 if (string_src
->payload
) {
1944 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
1945 if (!string_dst
->payload
) {
1955 int increase_packet_size(struct ctf_stream_pos
*pos
)
1960 ret
= munmap_align(pos
->base_mma
);
1965 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
1966 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1967 pos
->packet_size
/ CHAR_BIT
);
1972 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1973 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1974 if (pos
->base_mma
== MAP_FAILED
) {