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
*);
81 int bt_ctf_field_structure_validate(struct bt_ctf_field
*);
83 int bt_ctf_field_variant_validate(struct bt_ctf_field
*);
85 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*);
87 int bt_ctf_field_array_validate(struct bt_ctf_field
*);
89 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*);
92 int bt_ctf_field_generic_reset(struct bt_ctf_field
*);
94 int bt_ctf_field_structure_reset(struct bt_ctf_field
*);
96 int bt_ctf_field_variant_reset(struct bt_ctf_field
*);
98 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*);
100 int bt_ctf_field_array_reset(struct bt_ctf_field
*);
102 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*);
104 int bt_ctf_field_string_reset(struct bt_ctf_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 ret
= field_copy_funcs
[type_id
](field
, copy
);
1011 bt_ctf_field_put(copy
);
1019 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1021 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1022 struct bt_ctf_field_type_integer
, parent
);
1023 struct bt_ctf_field_integer
*integer
= g_new0(
1024 struct bt_ctf_field_integer
, 1);
1027 integer
->definition
.declaration
= &integer_type
->declaration
;
1030 return integer
? &integer
->parent
: NULL
;
1034 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1035 struct bt_ctf_field_type
*type
)
1037 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1038 struct bt_ctf_field_enumeration
, 1);
1040 return enumeration
? &enumeration
->parent
: NULL
;
1044 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1045 struct bt_ctf_field_type
*type
)
1047 struct bt_ctf_field_floating_point
*floating_point
;
1048 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1050 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1051 if (!floating_point
) {
1055 floating_point_type
= container_of(type
,
1056 struct bt_ctf_field_type_floating_point
, parent
);
1057 floating_point
->definition
.declaration
= container_of(
1058 type
->declaration
, struct declaration_float
, p
);
1061 floating_point
->definition
.sign
= &floating_point
->sign
;
1062 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1063 floating_point
->definition
.sign
->p
.declaration
=
1064 &floating_point_type
->sign
.p
;
1066 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1067 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1068 floating_point
->definition
.mantissa
->p
.declaration
=
1069 &floating_point_type
->mantissa
.p
;
1071 floating_point
->definition
.exp
= &floating_point
->exp
;
1072 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1073 floating_point
->definition
.exp
->p
.declaration
=
1074 &floating_point_type
->exp
.p
;
1077 return floating_point
? &floating_point
->parent
: NULL
;
1081 struct bt_ctf_field
*bt_ctf_field_structure_create(
1082 struct bt_ctf_field_type
*type
)
1084 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1085 struct bt_ctf_field_type_structure
, parent
);
1086 struct bt_ctf_field_structure
*structure
= g_new0(
1087 struct bt_ctf_field_structure
, 1);
1088 struct bt_ctf_field
*field
= NULL
;
1090 if (!structure
|| !structure_type
->fields
->len
) {
1094 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1095 structure
->fields
= g_ptr_array_new_with_free_func(
1096 (GDestroyNotify
)bt_ctf_field_put
);
1097 g_ptr_array_set_size(structure
->fields
,
1098 g_hash_table_size(structure
->field_name_to_index
));
1099 field
= &structure
->parent
;
1105 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1107 struct bt_ctf_field_variant
*variant
= g_new0(
1108 struct bt_ctf_field_variant
, 1);
1109 return variant
? &variant
->parent
: NULL
;
1113 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1115 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1116 struct bt_ctf_field_type_array
*array_type
;
1117 unsigned int array_length
;
1119 if (!array
|| !type
) {
1123 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1124 array_length
= array_type
->length
;
1125 array
->elements
= g_ptr_array_sized_new(array_length
);
1126 if (!array
->elements
) {
1130 g_ptr_array_set_free_func(array
->elements
,
1131 (GDestroyNotify
)bt_ctf_field_put
);
1132 g_ptr_array_set_size(array
->elements
, array_length
);
1133 return &array
->parent
;
1140 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1141 struct bt_ctf_field_type
*type
)
1143 struct bt_ctf_field_sequence
*sequence
= g_new0(
1144 struct bt_ctf_field_sequence
, 1);
1145 return sequence
? &sequence
->parent
: NULL
;
1149 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1151 struct bt_ctf_field_string
*string
= g_new0(
1152 struct bt_ctf_field_string
, 1);
1153 return string
? &string
->parent
: NULL
;
1157 void bt_ctf_field_destroy(struct bt_ctf_ref
*ref
)
1159 struct bt_ctf_field
*field
;
1160 struct bt_ctf_field_type
*type
;
1161 enum ctf_type_id type_id
;
1167 field
= container_of(ref
, struct bt_ctf_field
, ref_count
);
1169 type_id
= bt_ctf_field_type_get_type_id(type
);
1170 if (type_id
<= CTF_TYPE_UNKNOWN
||
1171 type_id
>= NR_CTF_TYPES
) {
1175 field_destroy_funcs
[type_id
](field
);
1177 bt_ctf_field_type_put(type
);
1182 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1184 struct bt_ctf_field_integer
*integer
;
1190 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1195 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1197 struct bt_ctf_field_enumeration
*enumeration
;
1203 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1205 bt_ctf_field_put(enumeration
->payload
);
1206 g_free(enumeration
);
1210 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1212 struct bt_ctf_field_floating_point
*floating_point
;
1218 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1220 g_free(floating_point
);
1224 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1226 struct bt_ctf_field_structure
*structure
;
1232 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1233 g_ptr_array_free(structure
->fields
, TRUE
);
1238 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1240 struct bt_ctf_field_variant
*variant
;
1246 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1247 bt_ctf_field_put(variant
->tag
);
1248 bt_ctf_field_put(variant
->payload
);
1253 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1255 struct bt_ctf_field_array
*array
;
1261 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1262 g_ptr_array_free(array
->elements
, TRUE
);
1267 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1269 struct bt_ctf_field_sequence
*sequence
;
1275 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1276 g_ptr_array_free(sequence
->elements
, TRUE
);
1277 bt_ctf_field_put(sequence
->length
);
1282 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1284 struct bt_ctf_field_string
*string
;
1289 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1290 g_string_free(string
->payload
, TRUE
);
1295 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1297 return (field
&& field
->payload_set
) ? 0 : -1;
1301 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1304 struct bt_ctf_field_enumeration
*enumeration
;
1311 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1313 if (!enumeration
->payload
) {
1318 ret
= bt_ctf_field_validate(enumeration
->payload
);
1324 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1328 struct bt_ctf_field_structure
*structure
;
1335 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1336 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1337 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1347 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1350 struct bt_ctf_field_variant
*variant
;
1357 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1358 ret
= bt_ctf_field_validate(variant
->payload
);
1364 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1368 struct bt_ctf_field_array
*array
;
1375 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1376 for (i
= 0; i
< array
->elements
->len
; i
++) {
1377 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1387 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1391 struct bt_ctf_field_sequence
*sequence
;
1398 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1399 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1400 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1410 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1419 field
->payload_set
= 0;
1425 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1428 struct bt_ctf_field_enumeration
*enumeration
;
1435 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1437 if (!enumeration
->payload
) {
1441 ret
= bt_ctf_field_reset(enumeration
->payload
);
1447 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1451 struct bt_ctf_field_structure
*structure
;
1458 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1459 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1460 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1464 * Structure members are lazily initialized; skip if
1465 * this member has not been allocated yet.
1470 ret
= bt_ctf_field_reset(member
);
1480 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1483 struct bt_ctf_field_variant
*variant
;
1490 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1491 if (variant
->payload
) {
1492 ret
= bt_ctf_field_reset(variant
->payload
);
1499 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1503 struct bt_ctf_field_array
*array
;
1510 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1511 for (i
= 0; i
< array
->elements
->len
; i
++) {
1512 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1516 * Array elements are lazily initialized; skip if
1517 * this member has not been allocated yet.
1522 ret
= bt_ctf_field_reset(member
);
1532 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1536 struct bt_ctf_field_sequence
*sequence
;
1543 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1544 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1545 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1549 * Sequence elements are lazily initialized; skip if
1550 * this member has not been allocated yet.
1555 ret
= bt_ctf_field_reset(member
);
1565 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1568 struct bt_ctf_field_string
*string
;
1575 ret
= bt_ctf_field_generic_reset(field
);
1580 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1581 if (string
->payload
) {
1582 g_string_truncate(string
->payload
, 0);
1589 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1590 struct ctf_stream_pos
*pos
)
1593 struct bt_ctf_field_integer
*integer
= container_of(field
,
1594 struct bt_ctf_field_integer
, parent
);
1597 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1598 if (ret
== -EFAULT
) {
1600 * The field is too large to fit in the current packet's
1601 * remaining space. Bump the packet size and retry.
1603 ret
= increase_packet_size(pos
);
1614 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1615 struct ctf_stream_pos
*pos
)
1617 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1618 field
, struct bt_ctf_field_enumeration
, parent
);
1620 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1624 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1625 struct ctf_stream_pos
*pos
)
1628 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1629 struct bt_ctf_field_floating_point
, parent
);
1632 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1633 if (ret
== -EFAULT
) {
1635 * The field is too large to fit in the current packet's
1636 * remaining space. Bump the packet size and retry.
1638 ret
= increase_packet_size(pos
);
1649 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1650 struct ctf_stream_pos
*pos
)
1654 struct bt_ctf_field_structure
*structure
= container_of(
1655 field
, struct bt_ctf_field_structure
, parent
);
1657 while (!ctf_pos_access_ok(pos
,
1658 offset_align(pos
->offset
,
1659 field
->type
->declaration
->alignment
))) {
1660 ret
= increase_packet_size(pos
);
1666 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1671 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1672 struct bt_ctf_field
*field
= g_ptr_array_index(
1673 structure
->fields
, i
);
1675 ret
= bt_ctf_field_serialize(field
, pos
);
1685 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1686 struct ctf_stream_pos
*pos
)
1688 struct bt_ctf_field_variant
*variant
= container_of(
1689 field
, struct bt_ctf_field_variant
, parent
);
1691 return bt_ctf_field_serialize(variant
->payload
, pos
);
1695 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1696 struct ctf_stream_pos
*pos
)
1700 struct bt_ctf_field_array
*array
= container_of(
1701 field
, struct bt_ctf_field_array
, parent
);
1703 for (i
= 0; i
< array
->elements
->len
; i
++) {
1704 ret
= bt_ctf_field_serialize(
1705 g_ptr_array_index(array
->elements
, i
), pos
);
1715 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1716 struct ctf_stream_pos
*pos
)
1720 struct bt_ctf_field_sequence
*sequence
= container_of(
1721 field
, struct bt_ctf_field_sequence
, parent
);
1723 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1724 ret
= bt_ctf_field_serialize(
1725 g_ptr_array_index(sequence
->elements
, i
), pos
);
1735 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1736 struct ctf_stream_pos
*pos
)
1740 struct bt_ctf_field_string
*string
= container_of(field
,
1741 struct bt_ctf_field_string
, parent
);
1742 struct bt_ctf_field_type
*character_type
=
1743 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1744 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1746 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1747 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1748 (uint64_t) string
->payload
->str
[i
]);
1753 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1759 bt_ctf_field_put(character
);
1760 bt_ctf_field_type_put(character_type
);
1765 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1766 struct bt_ctf_field
*dst
)
1768 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1770 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1771 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1773 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1774 sizeof(struct definition_integer
));
1779 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1780 struct bt_ctf_field
*dst
)
1783 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1785 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1786 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1788 if (enum_src
->payload
) {
1789 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1790 if (!enum_dst
->payload
) {
1800 int bt_ctf_field_floating_point_copy(
1801 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1803 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1805 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1807 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1810 memcpy(&float_dst
->definition
, &float_src
->definition
,
1811 sizeof(struct definition_float
));
1812 memcpy(&float_dst
->sign
, &float_src
->sign
,
1813 sizeof(struct definition_integer
));
1814 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1815 sizeof(struct definition_integer
));
1816 memcpy(&float_dst
->exp
, &float_src
->exp
,
1817 sizeof(struct definition_integer
));
1822 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
1823 struct bt_ctf_field
*dst
)
1826 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
1828 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
1829 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
1831 /* This field_name_to_index HT is owned by the structure field type */
1832 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
1833 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
1835 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
1836 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1837 g_ptr_array_index(struct_src
->fields
, i
));
1843 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
1850 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
1851 struct bt_ctf_field
*dst
)
1854 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
1856 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
1857 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
1859 if (variant_src
->tag
) {
1860 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
1861 if (!variant_dst
->tag
) {
1866 if (variant_src
->payload
) {
1867 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
1868 if (!variant_dst
->payload
) {
1878 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
1879 struct bt_ctf_field
*dst
)
1882 struct bt_ctf_field_array
*array_src
, *array_dst
;
1884 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
1885 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
1887 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
1888 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
1889 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1890 g_ptr_array_index(array_src
->elements
, i
));
1896 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
1903 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
1904 struct bt_ctf_field
*dst
)
1907 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
1909 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
1910 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
1912 g_ptr_array_set_size(sequence_dst
->elements
,
1913 sequence_src
->elements
->len
);
1914 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
1915 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1916 g_ptr_array_index(sequence_src
->elements
, i
));
1922 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
1929 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
1930 struct bt_ctf_field
*dst
)
1933 struct bt_ctf_field_string
*string_src
, *string_dst
;
1935 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
1936 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
1938 if (string_src
->payload
) {
1939 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
1940 if (!string_dst
->payload
) {
1950 int increase_packet_size(struct ctf_stream_pos
*pos
)
1955 ret
= munmap_align(pos
->base_mma
);
1960 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
1961 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
1962 pos
->packet_size
/ CHAR_BIT
);
1967 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
1968 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
1969 if (pos
->base_mma
== MAP_FAILED
) {