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
*(* const 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 (* const 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 (* const 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 (* const 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 (* const 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 (* const field_copy_funcs
[])(struct bt_ctf_field
*,
219 struct bt_ctf_field
*) = {
220 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_copy
,
221 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_copy
,
222 [CTF_TYPE_FLOAT
] = bt_ctf_field_floating_point_copy
,
223 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_copy
,
224 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_copy
,
225 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_copy
,
226 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_copy
,
227 [CTF_TYPE_STRING
] = bt_ctf_field_string_copy
,
230 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
232 struct bt_ctf_field
*field
= NULL
;
233 enum ctf_type_id type_id
;
239 type_id
= bt_ctf_field_type_get_type_id(type
);
240 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
||
241 bt_ctf_field_type_validate(type
)) {
245 field
= field_create_funcs
[type_id
](type
);
250 /* The type's declaration can't change after this point */
251 bt_ctf_field_type_freeze(type
);
252 bt_ctf_field_type_get(type
);
253 bt_ctf_ref_init(&field
->ref_count
);
259 void bt_ctf_field_get(struct bt_ctf_field
*field
)
262 bt_ctf_ref_get(&field
->ref_count
);
266 void bt_ctf_field_put(struct bt_ctf_field
*field
)
269 bt_ctf_ref_put(&field
->ref_count
, bt_ctf_field_destroy
);
273 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
275 struct bt_ctf_field_type
*ret
= NULL
;
282 bt_ctf_field_type_get(ret
);
287 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
288 struct bt_ctf_field
*field
)
290 struct bt_ctf_field
*ret
= NULL
;
291 struct bt_ctf_field_sequence
*sequence
;
297 if (bt_ctf_field_type_get_type_id(field
->type
) !=
302 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
303 ret
= sequence
->length
;
304 bt_ctf_field_get(ret
);
309 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
310 struct bt_ctf_field
*length_field
)
313 struct bt_ctf_field_type_integer
*length_type
;
314 struct bt_ctf_field_integer
*length
;
315 struct bt_ctf_field_sequence
*sequence
;
316 uint64_t sequence_length
;
318 if (!field
|| !length_field
) {
322 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
328 length_type
= container_of(length_field
->type
,
329 struct bt_ctf_field_type_integer
, parent
);
330 /* The length field must be unsigned */
331 if (length_type
->declaration
.signedness
) {
336 length
= container_of(length_field
, struct bt_ctf_field_integer
,
338 sequence_length
= length
->definition
.value
._unsigned
;
339 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
340 if (sequence
->elements
) {
341 g_ptr_array_free(sequence
->elements
, TRUE
);
342 bt_ctf_field_put(sequence
->length
);
345 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
346 if (!sequence
->elements
) {
351 g_ptr_array_set_free_func(sequence
->elements
,
352 (GDestroyNotify
)bt_ctf_field_put
);
353 g_ptr_array_set_size(sequence
->elements
, (size_t)sequence_length
);
354 bt_ctf_field_get(length_field
);
355 sequence
->length
= length_field
;
360 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
361 struct bt_ctf_field
*field
, const char *name
)
363 struct bt_ctf_field
*new_field
= NULL
;
365 struct bt_ctf_field_structure
*structure
;
366 struct bt_ctf_field_type
*field_type
= NULL
;
369 if (!field
|| !name
||
370 bt_ctf_field_type_get_type_id(field
->type
) !=
375 field_quark
= g_quark_from_string(name
);
376 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
378 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
380 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
381 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
385 if (structure
->fields
->pdata
[index
]) {
386 new_field
= structure
->fields
->pdata
[index
];
390 new_field
= bt_ctf_field_create(field_type
);
395 structure
->fields
->pdata
[index
] = new_field
;
397 bt_ctf_field_get(new_field
);
400 bt_ctf_field_type_put(field_type
);
405 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
406 struct bt_ctf_field
*field
, int index
)
409 const char *field_name
;
410 struct bt_ctf_field_structure
*structure
;
411 struct bt_ctf_field_type
*structure_type
;
412 struct bt_ctf_field_type
*field_type
= NULL
;
413 struct bt_ctf_field
*ret_field
= NULL
;
416 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
420 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
421 if (index
>= structure
->fields
->len
) {
425 ret_field
= structure
->fields
->pdata
[index
];
430 /* Field has not been instanciated yet, create it */
431 structure_type
= bt_ctf_field_get_type(field
);
432 if (!structure_type
) {
436 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
437 &field_name
, &field_type
, index
);
438 bt_ctf_field_type_put(structure_type
);
443 ret_field
= bt_ctf_field_create(field_type
);
448 structure
->fields
->pdata
[index
] = ret_field
;
450 bt_ctf_field_get(ret_field
);
453 bt_ctf_field_type_put(field_type
);
459 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
460 const char *name
, struct bt_ctf_field
*value
)
464 struct bt_ctf_field_structure
*structure
;
465 struct bt_ctf_field_type
*expected_field_type
= NULL
;
468 if (!field
|| !name
|| !value
||
469 bt_ctf_field_type_get_type_id(field
->type
) !=
475 field_quark
= g_quark_from_string(name
);
476 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
477 expected_field_type
=
478 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
480 if (expected_field_type
!= value
->type
) {
485 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
486 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
490 if (structure
->fields
->pdata
[index
]) {
491 bt_ctf_field_put(structure
->fields
->pdata
[index
]);
494 structure
->fields
->pdata
[index
] = value
;
495 bt_ctf_field_get(value
);
497 if (expected_field_type
) {
498 bt_ctf_field_type_put(expected_field_type
);
503 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
506 struct bt_ctf_field
*new_field
= NULL
;
507 struct bt_ctf_field_type
*field_type
= NULL
;
508 struct bt_ctf_field_array
*array
;
510 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
515 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
516 if (index
>= array
->elements
->len
) {
520 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
521 if (array
->elements
->pdata
[(size_t)index
]) {
522 new_field
= array
->elements
->pdata
[(size_t)index
];
526 new_field
= bt_ctf_field_create(field_type
);
527 array
->elements
->pdata
[(size_t)index
] = new_field
;
530 bt_ctf_field_type_put(field_type
);
533 bt_ctf_field_get(new_field
);
538 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
541 struct bt_ctf_field
*new_field
= NULL
;
542 struct bt_ctf_field_type
*field_type
= NULL
;
543 struct bt_ctf_field_sequence
*sequence
;
545 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
550 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
551 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
555 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
556 if (sequence
->elements
->pdata
[(size_t)index
]) {
557 new_field
= sequence
->elements
->pdata
[(size_t)index
];
561 new_field
= bt_ctf_field_create(field_type
);
562 sequence
->elements
->pdata
[(size_t)index
] = new_field
;
565 bt_ctf_field_type_put(field_type
);
568 bt_ctf_field_get(new_field
);
573 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
574 struct bt_ctf_field
*tag_field
)
576 struct bt_ctf_field
*new_field
= NULL
;
577 struct bt_ctf_field_variant
*variant
;
578 struct bt_ctf_field_type_variant
*variant_type
;
579 struct bt_ctf_field_type
*field_type
;
580 struct bt_ctf_field
*tag_enum
= NULL
;
581 struct bt_ctf_field_integer
*tag_enum_integer
;
582 int64_t tag_enum_value
;
584 if (!field
|| !tag_field
||
585 bt_ctf_field_type_get_type_id(field
->type
) !=
587 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
592 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
593 variant_type
= container_of(field
->type
,
594 struct bt_ctf_field_type_variant
, parent
);
595 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
600 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
603 if (bt_ctf_field_validate(tag_field
) < 0) {
607 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
610 * If the variant currently has a tag and a payload, and if the
611 * requested tag value is the same as the current one, return
612 * the current payload instead of creating a fresh one.
614 if (variant
->tag
&& variant
->payload
) {
615 struct bt_ctf_field
*cur_tag_container
= NULL
;
616 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
617 int64_t cur_tag_value
;
620 bt_ctf_field_enumeration_get_container(variant
->tag
);
621 cur_tag_enum_integer
= container_of(cur_tag_container
,
622 struct bt_ctf_field_integer
, parent
);
623 bt_ctf_field_put(cur_tag_container
);
624 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
626 if (cur_tag_value
== tag_enum_value
) {
627 new_field
= variant
->payload
;
628 bt_ctf_field_get(new_field
);
633 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
634 variant_type
, tag_enum_value
);
639 new_field
= bt_ctf_field_create(field_type
);
644 bt_ctf_field_put(variant
->tag
);
645 bt_ctf_field_put(variant
->payload
);
646 bt_ctf_field_get(new_field
);
647 bt_ctf_field_get(tag_field
);
648 variant
->tag
= tag_field
;
649 variant
->payload
= new_field
;
651 bt_ctf_field_put(tag_enum
);
655 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
656 struct bt_ctf_field
*field
)
658 struct bt_ctf_field
*container
= NULL
;
659 struct bt_ctf_field_enumeration
*enumeration
;
661 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
666 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
668 if (!enumeration
->payload
) {
669 struct bt_ctf_field_type_enumeration
*enumeration_type
=
670 container_of(field
->type
,
671 struct bt_ctf_field_type_enumeration
, parent
);
672 enumeration
->payload
=
673 bt_ctf_field_create(enumeration_type
->container
);
676 container
= enumeration
->payload
;
677 bt_ctf_field_get(container
);
682 const char *bt_ctf_field_enumeration_get_mapping_name(
683 struct bt_ctf_field
*field
)
686 const char *name
= NULL
;
687 struct bt_ctf_field
*container
= NULL
;
688 struct bt_ctf_field_type
*container_type
= NULL
;
689 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
690 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
692 container
= bt_ctf_field_enumeration_get_container(field
);
697 container_type
= bt_ctf_field_get_type(container
);
698 if (!container_type
) {
699 goto error_put_container
;
702 integer_type
= container_of(container_type
,
703 struct bt_ctf_field_type_integer
, parent
);
704 enumeration_type
= container_of(field
->type
,
705 struct bt_ctf_field_type_enumeration
, parent
);
707 if (!integer_type
->declaration
.signedness
) {
709 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
712 goto error_put_container_type
;
715 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
716 enumeration_type
, value
);
719 ret
= bt_ctf_field_signed_integer_get_value(container
,
722 goto error_put_container_type
;
725 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
726 enumeration_type
, value
);
729 error_put_container_type
:
730 bt_ctf_field_type_put(container_type
);
732 bt_ctf_field_put(container
);
737 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
741 struct bt_ctf_field_integer
*integer
;
742 struct bt_ctf_field_type_integer
*integer_type
;
744 if (!field
|| !value
|| !field
->payload_set
||
745 bt_ctf_field_type_get_type_id(field
->type
) !=
751 integer_type
= container_of(field
->type
,
752 struct bt_ctf_field_type_integer
, parent
);
753 if (!integer_type
->declaration
.signedness
) {
758 integer
= container_of(field
,
759 struct bt_ctf_field_integer
, parent
);
760 *value
= integer
->definition
.value
._signed
;
765 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
769 struct bt_ctf_field_integer
*integer
;
770 struct bt_ctf_field_type_integer
*integer_type
;
772 int64_t min_value
, max_value
;
775 bt_ctf_field_type_get_type_id(field
->type
) !=
781 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
782 integer_type
= container_of(field
->type
,
783 struct bt_ctf_field_type_integer
, parent
);
784 if (!integer_type
->declaration
.signedness
) {
789 size
= integer_type
->declaration
.len
;
790 min_value
= -((int64_t)1 << (size
- 1));
791 max_value
= ((int64_t)1 << (size
- 1)) - 1;
792 if (value
< min_value
|| value
> max_value
) {
797 integer
->definition
.value
._signed
= value
;
798 integer
->parent
.payload_set
= 1;
803 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
807 struct bt_ctf_field_integer
*integer
;
808 struct bt_ctf_field_type_integer
*integer_type
;
810 if (!field
|| !value
|| !field
->payload_set
||
811 bt_ctf_field_type_get_type_id(field
->type
) !=
817 integer_type
= container_of(field
->type
,
818 struct bt_ctf_field_type_integer
, parent
);
819 if (integer_type
->declaration
.signedness
) {
824 integer
= container_of(field
,
825 struct bt_ctf_field_integer
, parent
);
826 *value
= integer
->definition
.value
._unsigned
;
831 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
835 struct bt_ctf_field_integer
*integer
;
836 struct bt_ctf_field_type_integer
*integer_type
;
841 bt_ctf_field_type_get_type_id(field
->type
) !=
847 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
848 integer_type
= container_of(field
->type
,
849 struct bt_ctf_field_type_integer
, parent
);
850 if (integer_type
->declaration
.signedness
) {
855 size
= integer_type
->declaration
.len
;
856 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
857 if (value
> max_value
) {
862 integer
->definition
.value
._unsigned
= value
;
863 integer
->parent
.payload_set
= 1;
868 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
872 struct bt_ctf_field_floating_point
*floating_point
;
874 if (!field
|| !value
|| !field
->payload_set
||
875 bt_ctf_field_type_get_type_id(field
->type
) !=
881 floating_point
= container_of(field
,
882 struct bt_ctf_field_floating_point
, parent
);
883 *value
= floating_point
->definition
.value
;
888 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
892 struct bt_ctf_field_floating_point
*floating_point
;
895 bt_ctf_field_type_get_type_id(field
->type
) !=
900 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
902 floating_point
->definition
.value
= value
;
903 floating_point
->parent
.payload_set
= 1;
908 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
910 const char *ret
= NULL
;
911 struct bt_ctf_field_string
*string
;
913 if (!field
|| !field
->payload_set
||
914 bt_ctf_field_type_get_type_id(field
->type
) !=
919 string
= container_of(field
,
920 struct bt_ctf_field_string
, parent
);
921 ret
= string
->payload
->str
;
926 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
930 struct bt_ctf_field_string
*string
;
932 if (!field
|| !value
||
933 bt_ctf_field_type_get_type_id(field
->type
) !=
939 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
940 if (string
->payload
) {
941 g_string_assign(string
->payload
, value
);
943 string
->payload
= g_string_new(value
);
946 string
->parent
.payload_set
= 1;
951 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
955 struct bt_ctf_field_string
*string_field
;
957 if (!field
|| !value
||
958 bt_ctf_field_type_get_type_id(field
->type
) !=
964 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
966 if (string_field
->payload
) {
967 g_string_append(string_field
->payload
, value
);
969 string_field
->payload
= g_string_new(value
);
972 string_field
->parent
.payload_set
= 1;
978 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
979 const char *value
, unsigned int length
)
983 unsigned int effective_length
= length
;
984 struct bt_ctf_field_string
*string_field
;
986 if (!field
|| !value
||
987 bt_ctf_field_type_get_type_id(field
->type
) !=
993 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
995 /* make sure no null bytes are appended */
996 for (i
= 0; i
< length
; ++i
) {
997 if (value
[i
] == '\0') {
998 effective_length
= i
;
1003 if (string_field
->payload
) {
1004 g_string_append_len(string_field
->payload
, value
,
1007 string_field
->payload
= g_string_new_len(value
,
1011 string_field
->parent
.payload_set
= 1;
1018 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1021 enum ctf_type_id type_id
;
1028 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1029 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1034 ret
= field_validate_funcs
[type_id
](field
);
1040 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1043 enum ctf_type_id type_id
;
1050 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1051 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1056 ret
= field_reset_funcs
[type_id
](field
);
1062 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1063 struct ctf_stream_pos
*pos
)
1066 enum ctf_type_id type_id
;
1068 if (!field
|| !pos
) {
1073 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1074 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1079 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1085 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1088 struct bt_ctf_field
*copy
= NULL
;
1089 enum ctf_type_id type_id
;
1095 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1096 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1100 copy
= bt_ctf_field_create(field
->type
);
1105 copy
->payload_set
= field
->payload_set
;
1106 ret
= field_copy_funcs
[type_id
](field
, copy
);
1108 bt_ctf_field_put(copy
);
1116 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1118 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1119 struct bt_ctf_field_type_integer
, parent
);
1120 struct bt_ctf_field_integer
*integer
= g_new0(
1121 struct bt_ctf_field_integer
, 1);
1124 integer
->definition
.declaration
= &integer_type
->declaration
;
1127 return integer
? &integer
->parent
: NULL
;
1131 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1132 struct bt_ctf_field_type
*type
)
1134 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1135 struct bt_ctf_field_enumeration
, 1);
1137 return enumeration
? &enumeration
->parent
: NULL
;
1141 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1142 struct bt_ctf_field_type
*type
)
1144 struct bt_ctf_field_floating_point
*floating_point
;
1145 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1147 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1148 if (!floating_point
) {
1152 floating_point_type
= container_of(type
,
1153 struct bt_ctf_field_type_floating_point
, parent
);
1154 floating_point
->definition
.declaration
= container_of(
1155 type
->declaration
, struct declaration_float
, p
);
1158 floating_point
->definition
.sign
= &floating_point
->sign
;
1159 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1160 floating_point
->definition
.sign
->p
.declaration
=
1161 &floating_point_type
->sign
.p
;
1163 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1164 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1165 floating_point
->definition
.mantissa
->p
.declaration
=
1166 &floating_point_type
->mantissa
.p
;
1168 floating_point
->definition
.exp
= &floating_point
->exp
;
1169 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1170 floating_point
->definition
.exp
->p
.declaration
=
1171 &floating_point_type
->exp
.p
;
1174 return floating_point
? &floating_point
->parent
: NULL
;
1178 struct bt_ctf_field
*bt_ctf_field_structure_create(
1179 struct bt_ctf_field_type
*type
)
1181 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1182 struct bt_ctf_field_type_structure
, parent
);
1183 struct bt_ctf_field_structure
*structure
= g_new0(
1184 struct bt_ctf_field_structure
, 1);
1185 struct bt_ctf_field
*field
= NULL
;
1187 if (!structure
|| !structure_type
->fields
->len
) {
1191 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1192 structure
->fields
= g_ptr_array_new_with_free_func(
1193 (GDestroyNotify
)bt_ctf_field_put
);
1194 g_ptr_array_set_size(structure
->fields
,
1195 g_hash_table_size(structure
->field_name_to_index
));
1196 field
= &structure
->parent
;
1202 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1204 struct bt_ctf_field_variant
*variant
= g_new0(
1205 struct bt_ctf_field_variant
, 1);
1206 return variant
? &variant
->parent
: NULL
;
1210 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1212 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1213 struct bt_ctf_field_type_array
*array_type
;
1214 unsigned int array_length
;
1216 if (!array
|| !type
) {
1220 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1221 array_length
= array_type
->length
;
1222 array
->elements
= g_ptr_array_sized_new(array_length
);
1223 if (!array
->elements
) {
1227 g_ptr_array_set_free_func(array
->elements
,
1228 (GDestroyNotify
)bt_ctf_field_put
);
1229 g_ptr_array_set_size(array
->elements
, array_length
);
1230 return &array
->parent
;
1237 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1238 struct bt_ctf_field_type
*type
)
1240 struct bt_ctf_field_sequence
*sequence
= g_new0(
1241 struct bt_ctf_field_sequence
, 1);
1242 return sequence
? &sequence
->parent
: NULL
;
1246 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1248 struct bt_ctf_field_string
*string
= g_new0(
1249 struct bt_ctf_field_string
, 1);
1250 return string
? &string
->parent
: NULL
;
1254 void bt_ctf_field_destroy(struct bt_ctf_ref
*ref
)
1256 struct bt_ctf_field
*field
;
1257 struct bt_ctf_field_type
*type
;
1258 enum ctf_type_id type_id
;
1264 field
= container_of(ref
, struct bt_ctf_field
, ref_count
);
1266 type_id
= bt_ctf_field_type_get_type_id(type
);
1267 if (type_id
<= CTF_TYPE_UNKNOWN
||
1268 type_id
>= NR_CTF_TYPES
) {
1272 field_destroy_funcs
[type_id
](field
);
1274 bt_ctf_field_type_put(type
);
1279 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1281 struct bt_ctf_field_integer
*integer
;
1287 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1292 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1294 struct bt_ctf_field_enumeration
*enumeration
;
1300 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1302 bt_ctf_field_put(enumeration
->payload
);
1303 g_free(enumeration
);
1307 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1309 struct bt_ctf_field_floating_point
*floating_point
;
1315 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1317 g_free(floating_point
);
1321 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1323 struct bt_ctf_field_structure
*structure
;
1329 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1330 g_ptr_array_free(structure
->fields
, TRUE
);
1335 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1337 struct bt_ctf_field_variant
*variant
;
1343 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1344 bt_ctf_field_put(variant
->tag
);
1345 bt_ctf_field_put(variant
->payload
);
1350 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1352 struct bt_ctf_field_array
*array
;
1358 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1359 g_ptr_array_free(array
->elements
, TRUE
);
1364 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1366 struct bt_ctf_field_sequence
*sequence
;
1372 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1373 g_ptr_array_free(sequence
->elements
, TRUE
);
1374 bt_ctf_field_put(sequence
->length
);
1379 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1381 struct bt_ctf_field_string
*string
;
1386 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1387 if (string
->payload
) {
1388 g_string_free(string
->payload
, TRUE
);
1394 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1396 return (field
&& field
->payload_set
) ? 0 : -1;
1400 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1403 struct bt_ctf_field_enumeration
*enumeration
;
1410 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1412 if (!enumeration
->payload
) {
1417 ret
= bt_ctf_field_validate(enumeration
->payload
);
1423 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1427 struct bt_ctf_field_structure
*structure
;
1434 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1435 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1436 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1446 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1449 struct bt_ctf_field_variant
*variant
;
1456 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1457 ret
= bt_ctf_field_validate(variant
->payload
);
1463 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1467 struct bt_ctf_field_array
*array
;
1474 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1475 for (i
= 0; i
< array
->elements
->len
; i
++) {
1476 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1486 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1490 struct bt_ctf_field_sequence
*sequence
;
1497 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1498 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1499 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1509 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1518 field
->payload_set
= 0;
1524 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1527 struct bt_ctf_field_enumeration
*enumeration
;
1534 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1536 if (!enumeration
->payload
) {
1540 ret
= bt_ctf_field_reset(enumeration
->payload
);
1546 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1550 struct bt_ctf_field_structure
*structure
;
1557 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1558 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1559 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1563 * Structure members are lazily initialized; skip if
1564 * this member has not been allocated yet.
1569 ret
= bt_ctf_field_reset(member
);
1579 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1582 struct bt_ctf_field_variant
*variant
;
1589 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1590 if (variant
->payload
) {
1591 ret
= bt_ctf_field_reset(variant
->payload
);
1598 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1602 struct bt_ctf_field_array
*array
;
1609 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1610 for (i
= 0; i
< array
->elements
->len
; i
++) {
1611 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1615 * Array elements are lazily initialized; skip if
1616 * this member has not been allocated yet.
1621 ret
= bt_ctf_field_reset(member
);
1631 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1635 struct bt_ctf_field_sequence
*sequence
;
1642 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1643 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1644 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1648 * Sequence elements are lazily initialized; skip if
1649 * this member has not been allocated yet.
1654 ret
= bt_ctf_field_reset(member
);
1664 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1667 struct bt_ctf_field_string
*string
;
1674 ret
= bt_ctf_field_generic_reset(field
);
1679 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1680 if (string
->payload
) {
1681 g_string_truncate(string
->payload
, 0);
1688 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1689 struct ctf_stream_pos
*pos
)
1692 struct bt_ctf_field_integer
*integer
= container_of(field
,
1693 struct bt_ctf_field_integer
, parent
);
1696 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1697 if (ret
== -EFAULT
) {
1699 * The field is too large to fit in the current packet's
1700 * remaining space. Bump the packet size and retry.
1702 ret
= increase_packet_size(pos
);
1713 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1714 struct ctf_stream_pos
*pos
)
1716 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1717 field
, struct bt_ctf_field_enumeration
, parent
);
1719 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1723 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1724 struct ctf_stream_pos
*pos
)
1727 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1728 struct bt_ctf_field_floating_point
, parent
);
1731 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1732 if (ret
== -EFAULT
) {
1734 * The field is too large to fit in the current packet's
1735 * remaining space. Bump the packet size and retry.
1737 ret
= increase_packet_size(pos
);
1748 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1749 struct ctf_stream_pos
*pos
)
1753 struct bt_ctf_field_structure
*structure
= container_of(
1754 field
, struct bt_ctf_field_structure
, parent
);
1756 while (!ctf_pos_access_ok(pos
,
1757 offset_align(pos
->offset
,
1758 field
->type
->declaration
->alignment
))) {
1759 ret
= increase_packet_size(pos
);
1765 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1770 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1771 struct bt_ctf_field
*field
= g_ptr_array_index(
1772 structure
->fields
, i
);
1774 ret
= bt_ctf_field_serialize(field
, pos
);
1784 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1785 struct ctf_stream_pos
*pos
)
1787 struct bt_ctf_field_variant
*variant
= container_of(
1788 field
, struct bt_ctf_field_variant
, parent
);
1790 return bt_ctf_field_serialize(variant
->payload
, pos
);
1794 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1795 struct ctf_stream_pos
*pos
)
1799 struct bt_ctf_field_array
*array
= container_of(
1800 field
, struct bt_ctf_field_array
, parent
);
1802 for (i
= 0; i
< array
->elements
->len
; i
++) {
1803 ret
= bt_ctf_field_serialize(
1804 g_ptr_array_index(array
->elements
, i
), pos
);
1814 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1815 struct ctf_stream_pos
*pos
)
1819 struct bt_ctf_field_sequence
*sequence
= container_of(
1820 field
, struct bt_ctf_field_sequence
, parent
);
1822 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1823 ret
= bt_ctf_field_serialize(
1824 g_ptr_array_index(sequence
->elements
, i
), pos
);
1834 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1835 struct ctf_stream_pos
*pos
)
1839 struct bt_ctf_field_string
*string
= container_of(field
,
1840 struct bt_ctf_field_string
, parent
);
1841 struct bt_ctf_field_type
*character_type
=
1842 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1843 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1845 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1846 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1847 (uint64_t) string
->payload
->str
[i
]);
1852 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1858 bt_ctf_field_put(character
);
1859 bt_ctf_field_type_put(character_type
);
1864 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1865 struct bt_ctf_field
*dst
)
1867 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1869 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1870 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1872 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1873 sizeof(struct definition_integer
));
1878 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1879 struct bt_ctf_field
*dst
)
1882 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1884 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1885 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1887 if (enum_src
->payload
) {
1888 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1889 if (!enum_dst
->payload
) {
1899 int bt_ctf_field_floating_point_copy(
1900 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1902 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1904 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1906 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1909 memcpy(&float_dst
->definition
, &float_src
->definition
,
1910 sizeof(struct definition_float
));
1911 memcpy(&float_dst
->sign
, &float_src
->sign
,
1912 sizeof(struct definition_integer
));
1913 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1914 sizeof(struct definition_integer
));
1915 memcpy(&float_dst
->exp
, &float_src
->exp
,
1916 sizeof(struct definition_integer
));
1921 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
1922 struct bt_ctf_field
*dst
)
1925 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
1927 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
1928 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
1930 /* This field_name_to_index HT is owned by the structure field type */
1931 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
1932 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
1934 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
1935 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1936 g_ptr_array_index(struct_src
->fields
, i
));
1942 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
1949 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
1950 struct bt_ctf_field
*dst
)
1953 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
1955 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
1956 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
1958 if (variant_src
->tag
) {
1959 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
1960 if (!variant_dst
->tag
) {
1965 if (variant_src
->payload
) {
1966 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
1967 if (!variant_dst
->payload
) {
1977 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
1978 struct bt_ctf_field
*dst
)
1981 struct bt_ctf_field_array
*array_src
, *array_dst
;
1983 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
1984 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
1986 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
1987 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
1988 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1989 g_ptr_array_index(array_src
->elements
, i
));
1995 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2002 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2003 struct bt_ctf_field
*dst
)
2006 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2007 struct bt_ctf_field
*src_length
;
2008 struct bt_ctf_field
*dst_length
;
2010 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2011 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2013 src_length
= bt_ctf_field_sequence_get_length(src
);
2016 /* no length set yet: keep destination sequence empty */
2020 /* copy source length */
2021 dst_length
= bt_ctf_field_copy(src_length
);
2022 bt_ctf_field_put(src_length
);
2029 /* this will initialize the destination sequence's internal array */
2030 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2031 bt_ctf_field_put(dst_length
);
2037 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2039 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2040 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
2041 g_ptr_array_index(sequence_src
->elements
, i
));
2048 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2055 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2056 struct bt_ctf_field
*dst
)
2059 struct bt_ctf_field_string
*string_src
, *string_dst
;
2061 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2062 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2064 if (string_src
->payload
) {
2065 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2066 if (!string_dst
->payload
) {
2076 int increase_packet_size(struct ctf_stream_pos
*pos
)
2081 ret
= munmap_align(pos
->base_mma
);
2086 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2087 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2088 pos
->packet_size
/ CHAR_BIT
);
2093 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2094 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2095 if (pos
->base_mma
== MAP_FAILED
) {