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
;
1000 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1001 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1006 copy
= bt_ctf_field_create(field
->type
);
1011 bt_ctf_field_type_get(field
->type
);
1012 copy
->type
= field
->type
;
1013 ret
= field_copy_funcs
[type_id
](field
, copy
);
1015 bt_ctf_field_put(copy
);
1023 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1025 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1026 struct bt_ctf_field_type_integer
, parent
);
1027 struct bt_ctf_field_integer
*integer
= g_new0(
1028 struct bt_ctf_field_integer
, 1);
1031 integer
->definition
.declaration
= &integer_type
->declaration
;
1034 return integer
? &integer
->parent
: NULL
;
1038 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1039 struct bt_ctf_field_type
*type
)
1041 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1042 struct bt_ctf_field_enumeration
, 1);
1044 return enumeration
? &enumeration
->parent
: NULL
;
1048 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1049 struct bt_ctf_field_type
*type
)
1051 struct bt_ctf_field_floating_point
*floating_point
;
1052 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1054 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1055 if (!floating_point
) {
1059 floating_point_type
= container_of(type
,
1060 struct bt_ctf_field_type_floating_point
, parent
);
1061 floating_point
->definition
.declaration
= container_of(
1062 type
->declaration
, struct declaration_float
, p
);
1065 floating_point
->definition
.sign
= &floating_point
->sign
;
1066 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1067 floating_point
->definition
.sign
->p
.declaration
=
1068 &floating_point_type
->sign
.p
;
1070 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1071 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1072 floating_point
->definition
.mantissa
->p
.declaration
=
1073 &floating_point_type
->mantissa
.p
;
1075 floating_point
->definition
.exp
= &floating_point
->exp
;
1076 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1077 floating_point
->definition
.exp
->p
.declaration
=
1078 &floating_point_type
->exp
.p
;
1081 return floating_point
? &floating_point
->parent
: NULL
;
1085 struct bt_ctf_field
*bt_ctf_field_structure_create(
1086 struct bt_ctf_field_type
*type
)
1088 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1089 struct bt_ctf_field_type_structure
, parent
);
1090 struct bt_ctf_field_structure
*structure
= g_new0(
1091 struct bt_ctf_field_structure
, 1);
1092 struct bt_ctf_field
*field
= NULL
;
1094 if (!structure
|| !structure_type
->fields
->len
) {
1098 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1099 structure
->fields
= g_ptr_array_new_with_free_func(
1100 (GDestroyNotify
)bt_ctf_field_put
);
1101 g_ptr_array_set_size(structure
->fields
,
1102 g_hash_table_size(structure
->field_name_to_index
));
1103 field
= &structure
->parent
;
1109 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1111 struct bt_ctf_field_variant
*variant
= g_new0(
1112 struct bt_ctf_field_variant
, 1);
1113 return variant
? &variant
->parent
: NULL
;
1117 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1119 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1120 struct bt_ctf_field_type_array
*array_type
;
1121 unsigned int array_length
;
1123 if (!array
|| !type
) {
1127 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1128 array_length
= array_type
->length
;
1129 array
->elements
= g_ptr_array_sized_new(array_length
);
1130 if (!array
->elements
) {
1134 g_ptr_array_set_free_func(array
->elements
,
1135 (GDestroyNotify
)bt_ctf_field_put
);
1136 g_ptr_array_set_size(array
->elements
, array_length
);
1137 return &array
->parent
;
1144 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1145 struct bt_ctf_field_type
*type
)
1147 struct bt_ctf_field_sequence
*sequence
= g_new0(
1148 struct bt_ctf_field_sequence
, 1);
1149 return sequence
? &sequence
->parent
: NULL
;
1153 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1155 struct bt_ctf_field_string
*string
= g_new0(
1156 struct bt_ctf_field_string
, 1);
1157 return string
? &string
->parent
: NULL
;
1161 void bt_ctf_field_destroy(struct bt_ctf_ref
*ref
)
1163 struct bt_ctf_field
*field
;
1164 struct bt_ctf_field_type
*type
;
1165 enum ctf_type_id type_id
;
1171 field
= container_of(ref
, struct bt_ctf_field
, ref_count
);
1173 type_id
= bt_ctf_field_type_get_type_id(type
);
1174 if (type_id
<= CTF_TYPE_UNKNOWN
||
1175 type_id
>= NR_CTF_TYPES
) {
1179 field_destroy_funcs
[type_id
](field
);
1181 bt_ctf_field_type_put(type
);
1186 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1188 struct bt_ctf_field_integer
*integer
;
1194 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1199 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1201 struct bt_ctf_field_enumeration
*enumeration
;
1207 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1209 bt_ctf_field_put(enumeration
->payload
);
1210 g_free(enumeration
);
1214 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1216 struct bt_ctf_field_floating_point
*floating_point
;
1222 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1224 g_free(floating_point
);
1228 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1230 struct bt_ctf_field_structure
*structure
;
1236 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1237 g_ptr_array_free(structure
->fields
, TRUE
);
1242 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1244 struct bt_ctf_field_variant
*variant
;
1250 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1251 bt_ctf_field_put(variant
->tag
);
1252 bt_ctf_field_put(variant
->payload
);
1257 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1259 struct bt_ctf_field_array
*array
;
1265 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1266 g_ptr_array_free(array
->elements
, TRUE
);
1271 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1273 struct bt_ctf_field_sequence
*sequence
;
1279 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1280 g_ptr_array_free(sequence
->elements
, TRUE
);
1281 bt_ctf_field_put(sequence
->length
);
1286 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1288 struct bt_ctf_field_string
*string
;
1293 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1294 g_string_free(string
->payload
, TRUE
);
1299 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1301 return (field
&& field
->payload_set
) ? 0 : -1;
1305 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1308 struct bt_ctf_field_enumeration
*enumeration
;
1315 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1317 if (!enumeration
->payload
) {
1322 ret
= bt_ctf_field_validate(enumeration
->payload
);
1328 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1332 struct bt_ctf_field_structure
*structure
;
1339 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1340 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1341 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1351 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1354 struct bt_ctf_field_variant
*variant
;
1361 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1362 ret
= bt_ctf_field_validate(variant
->payload
);
1368 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1372 struct bt_ctf_field_array
*array
;
1379 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1380 for (i
= 0; i
< array
->elements
->len
; i
++) {
1381 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1391 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1395 struct bt_ctf_field_sequence
*sequence
;
1402 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1403 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1404 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1414 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1423 field
->payload_set
= 0;
1429 int bt_ctf_field_enumeration_reset(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
) {
1445 ret
= bt_ctf_field_reset(enumeration
->payload
);
1451 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1455 struct bt_ctf_field_structure
*structure
;
1462 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1463 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1464 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1468 * Structure members are lazily initialized; skip if
1469 * this member has not been allocated yet.
1474 ret
= bt_ctf_field_reset(member
);
1484 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1487 struct bt_ctf_field_variant
*variant
;
1494 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1495 if (variant
->payload
) {
1496 ret
= bt_ctf_field_reset(variant
->payload
);
1503 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1507 struct bt_ctf_field_array
*array
;
1514 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1515 for (i
= 0; i
< array
->elements
->len
; i
++) {
1516 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1520 * Array elements are lazily initialized; skip if
1521 * this member has not been allocated yet.
1526 ret
= bt_ctf_field_reset(member
);
1536 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1540 struct bt_ctf_field_sequence
*sequence
;
1547 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1548 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1549 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1553 * Sequence elements are lazily initialized; skip if
1554 * this member has not been allocated yet.
1559 ret
= bt_ctf_field_reset(member
);
1569 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1572 struct bt_ctf_field_string
*string
;
1579 ret
= bt_ctf_field_generic_reset(field
);
1584 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1585 if (string
->payload
) {
1586 g_string_truncate(string
->payload
, 0);
1593 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1594 struct ctf_stream_pos
*pos
)
1597 struct bt_ctf_field_integer
*integer
= container_of(field
,
1598 struct bt_ctf_field_integer
, parent
);
1601 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1602 if (ret
== -EFAULT
) {
1604 * The field is too large to fit in the current packet's
1605 * remaining space. Bump the packet size and retry.
1607 ret
= increase_packet_size(pos
);
1618 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1619 struct ctf_stream_pos
*pos
)
1621 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1622 field
, struct bt_ctf_field_enumeration
, parent
);
1624 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1628 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1629 struct ctf_stream_pos
*pos
)
1632 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1633 struct bt_ctf_field_floating_point
, parent
);
1636 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1637 if (ret
== -EFAULT
) {
1639 * The field is too large to fit in the current packet's
1640 * remaining space. Bump the packet size and retry.
1642 ret
= increase_packet_size(pos
);
1653 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1654 struct ctf_stream_pos
*pos
)
1658 struct bt_ctf_field_structure
*structure
= container_of(
1659 field
, struct bt_ctf_field_structure
, parent
);
1661 while (!ctf_pos_access_ok(pos
,
1662 offset_align(pos
->offset
,
1663 field
->type
->declaration
->alignment
))) {
1664 ret
= increase_packet_size(pos
);
1670 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1675 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1676 struct bt_ctf_field
*field
= g_ptr_array_index(
1677 structure
->fields
, i
);
1679 ret
= bt_ctf_field_serialize(field
, pos
);
1689 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1690 struct ctf_stream_pos
*pos
)
1692 struct bt_ctf_field_variant
*variant
= container_of(
1693 field
, struct bt_ctf_field_variant
, parent
);
1695 return bt_ctf_field_serialize(variant
->payload
, pos
);
1699 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1700 struct ctf_stream_pos
*pos
)
1704 struct bt_ctf_field_array
*array
= container_of(
1705 field
, struct bt_ctf_field_array
, parent
);
1707 for (i
= 0; i
< array
->elements
->len
; i
++) {
1708 ret
= bt_ctf_field_serialize(
1709 g_ptr_array_index(array
->elements
, i
), pos
);
1719 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1720 struct ctf_stream_pos
*pos
)
1724 struct bt_ctf_field_sequence
*sequence
= container_of(
1725 field
, struct bt_ctf_field_sequence
, parent
);
1727 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1728 ret
= bt_ctf_field_serialize(
1729 g_ptr_array_index(sequence
->elements
, i
), pos
);
1739 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1740 struct ctf_stream_pos
*pos
)
1744 struct bt_ctf_field_string
*string
= container_of(field
,
1745 struct bt_ctf_field_string
, parent
);
1746 struct bt_ctf_field_type
*character_type
=
1747 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1748 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1750 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1751 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1752 (uint64_t) string
->payload
->str
[i
]);
1757 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1763 bt_ctf_field_put(character
);
1764 bt_ctf_field_type_put(character_type
);
1769 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1770 struct bt_ctf_field
*dst
)
1772 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1774 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1775 integer_dst
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1777 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1778 sizeof(struct definition_integer
));
1783 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1784 struct bt_ctf_field
*dst
)
1787 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1789 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1790 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1792 if (enum_src
->payload
) {
1793 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1794 if (!enum_dst
->payload
) {
1804 int bt_ctf_field_floating_point_copy(
1805 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1807 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1809 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1811 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1814 memcpy(&float_dst
->definition
, &float_src
->definition
,
1815 sizeof(struct definition_float
));
1816 memcpy(&float_dst
->sign
, &float_src
->sign
,
1817 sizeof(struct definition_integer
));
1818 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1819 sizeof(struct definition_integer
));
1820 memcpy(&float_dst
->exp
, &float_src
->exp
,
1821 sizeof(struct definition_integer
));
1826 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
1827 struct bt_ctf_field
*dst
)
1830 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
1832 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
1833 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
1835 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
1836 struct_dst
->fields
= g_ptr_array_sized_new(struct_src
->fields
->len
);
1837 if (!struct_dst
->fields
) {
1842 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
1843 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1844 g_ptr_array_index(struct_src
->fields
, i
));
1850 g_ptr_array_add(struct_dst
->fields
, field_copy
);
1857 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
1858 struct bt_ctf_field
*dst
)
1861 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
1863 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
1864 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
1866 if (variant_src
->tag
) {
1867 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
1868 if (!variant_dst
->tag
) {
1873 if (variant_src
->payload
) {
1874 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
1875 if (!variant_dst
->payload
) {
1885 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
1886 struct bt_ctf_field
*dst
)
1889 struct bt_ctf_field_array
*array_src
, *array_dst
;
1891 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
1892 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
1894 array_dst
->elements
= g_ptr_array_sized_new(array_src
->elements
->len
);
1895 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
1896 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1897 g_ptr_array_index(array_src
->elements
, i
));
1903 g_ptr_array_add(array_dst
->elements
, field_copy
);
1910 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
1911 struct bt_ctf_field
*dst
)
1914 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
1916 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
1917 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
1919 sequence_dst
->elements
= g_ptr_array_sized_new(
1920 sequence_src
->elements
->len
);
1921 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
1922 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1923 g_ptr_array_index(sequence_src
->elements
, i
));
1929 g_ptr_array_add(sequence_dst
->elements
, field_copy
);
1936 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
1937 struct bt_ctf_field
*dst
)
1940 struct bt_ctf_field_string
*string_src
, *string_dst
;
1942 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
1943 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
1945 if (string_src
->payload
) {
1946 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
1947 if (!string_dst
->payload
) {
1957 int increase_packet_size(struct ctf_stream_pos
*pos
)
1962 ret
= munmap_align(pos
->base_mma
);
1967 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
1968 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1969 pos
->packet_size
/ CHAR_BIT
);
1974 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1975 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1976 if (pos
->base_mma
== MAP_FAILED
) {