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/ctf-ir/common-internal.h>
33 #include <babeltrace/ctf-ir/ref.h>
34 #include <babeltrace/compiler.h>
36 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
39 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
41 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
42 struct bt_ctf_field_type
*);
44 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
45 struct bt_ctf_field_type
*);
47 struct bt_ctf_field
*bt_ctf_field_structure_create(
48 struct bt_ctf_field_type
*);
50 struct bt_ctf_field
*bt_ctf_field_variant_create(
51 struct bt_ctf_field_type
*);
53 struct bt_ctf_field
*bt_ctf_field_array_create(
54 struct bt_ctf_field_type
*);
56 struct bt_ctf_field
*bt_ctf_field_sequence_create(
57 struct bt_ctf_field_type
*);
59 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
62 void bt_ctf_field_destroy(struct bt_ref
*);
64 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
66 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
68 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
70 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
72 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
74 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
76 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
78 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
81 int bt_ctf_field_generic_validate(struct bt_ctf_field
*);
83 int bt_ctf_field_structure_validate(struct bt_ctf_field
*);
85 int bt_ctf_field_variant_validate(struct bt_ctf_field
*);
87 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*);
89 int bt_ctf_field_array_validate(struct bt_ctf_field
*);
91 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*);
94 int bt_ctf_field_generic_reset(struct bt_ctf_field
*);
96 int bt_ctf_field_structure_reset(struct bt_ctf_field
*);
98 int bt_ctf_field_variant_reset(struct bt_ctf_field
*);
100 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*);
102 int bt_ctf_field_array_reset(struct bt_ctf_field
*);
104 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*);
106 int bt_ctf_field_string_reset(struct bt_ctf_field
*);
109 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
110 struct ctf_stream_pos
*);
112 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
113 struct ctf_stream_pos
*);
115 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
116 struct ctf_stream_pos
*);
118 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
119 struct ctf_stream_pos
*);
121 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
122 struct ctf_stream_pos
*);
124 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
125 struct ctf_stream_pos
*);
127 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
128 struct ctf_stream_pos
*);
130 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
131 struct ctf_stream_pos
*);
134 int bt_ctf_field_integer_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
136 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
138 int bt_ctf_field_floating_point_copy(struct bt_ctf_field
*,
139 struct bt_ctf_field
*);
141 int bt_ctf_field_structure_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
143 int bt_ctf_field_variant_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
145 int bt_ctf_field_array_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
147 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
149 int bt_ctf_field_string_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
152 int increase_packet_size(struct ctf_stream_pos
*pos
);
155 struct bt_ctf_field
*(* const field_create_funcs
[])(
156 struct bt_ctf_field_type
*) = {
157 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_create
,
158 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_create
,
160 bt_ctf_field_floating_point_create
,
161 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_create
,
162 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_create
,
163 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_create
,
164 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_create
,
165 [CTF_TYPE_STRING
] = bt_ctf_field_string_create
,
169 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
170 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_destroy
,
171 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_destroy
,
173 bt_ctf_field_floating_point_destroy
,
174 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_destroy
,
175 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_destroy
,
176 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_destroy
,
177 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
178 [CTF_TYPE_STRING
] = bt_ctf_field_string_destroy
,
182 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
183 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_validate
,
184 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_validate
,
185 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_validate
,
186 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_validate
,
187 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_validate
,
188 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_validate
,
189 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_validate
,
190 [CTF_TYPE_STRING
] = bt_ctf_field_generic_validate
,
194 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
195 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_reset
,
196 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_reset
,
197 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_reset
,
198 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_reset
,
199 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_reset
,
200 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_reset
,
201 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_reset
,
202 [CTF_TYPE_STRING
] = bt_ctf_field_string_reset
,
206 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
207 struct ctf_stream_pos
*) = {
208 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_serialize
,
209 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_serialize
,
211 bt_ctf_field_floating_point_serialize
,
212 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_serialize
,
213 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_serialize
,
214 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_serialize
,
215 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
216 [CTF_TYPE_STRING
] = bt_ctf_field_string_serialize
,
220 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
221 struct bt_ctf_field
*) = {
222 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_copy
,
223 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_copy
,
224 [CTF_TYPE_FLOAT
] = bt_ctf_field_floating_point_copy
,
225 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_copy
,
226 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_copy
,
227 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_copy
,
228 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_copy
,
229 [CTF_TYPE_STRING
] = bt_ctf_field_string_copy
,
232 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
234 struct bt_ctf_field
*field
= NULL
;
235 enum ctf_type_id type_id
;
241 type_id
= bt_ctf_field_type_get_type_id(type
);
242 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
||
243 bt_ctf_field_type_validate(type
)) {
247 field
= field_create_funcs
[type_id
](type
);
252 /* The type's declaration can't change after this point */
253 bt_ctf_field_type_freeze(type
);
254 bt_ctf_field_type_get(type
);
255 bt_ctf_base_init(field
, bt_ctf_field_destroy
);
261 void bt_ctf_field_get(struct bt_ctf_field
*field
)
266 void bt_ctf_field_put(struct bt_ctf_field
*field
)
271 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
273 struct bt_ctf_field_type
*ret
= NULL
;
280 bt_ctf_field_type_get(ret
);
285 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
286 struct bt_ctf_field
*field
)
288 struct bt_ctf_field
*ret
= NULL
;
289 struct bt_ctf_field_sequence
*sequence
;
295 if (bt_ctf_field_type_get_type_id(field
->type
) !=
300 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
301 ret
= sequence
->length
;
302 bt_ctf_field_get(ret
);
307 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
308 struct bt_ctf_field
*length_field
)
311 struct bt_ctf_field_type_integer
*length_type
;
312 struct bt_ctf_field_integer
*length
;
313 struct bt_ctf_field_sequence
*sequence
;
314 uint64_t sequence_length
;
316 if (!field
|| !length_field
) {
320 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
326 length_type
= container_of(length_field
->type
,
327 struct bt_ctf_field_type_integer
, parent
);
328 /* The length field must be unsigned */
329 if (length_type
->declaration
.signedness
) {
334 length
= container_of(length_field
, struct bt_ctf_field_integer
,
336 sequence_length
= length
->definition
.value
._unsigned
;
337 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
338 if (sequence
->elements
) {
339 g_ptr_array_free(sequence
->elements
, TRUE
);
340 bt_ctf_field_put(sequence
->length
);
343 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
344 if (!sequence
->elements
) {
349 g_ptr_array_set_free_func(sequence
->elements
,
350 (GDestroyNotify
)bt_ctf_field_put
);
351 g_ptr_array_set_size(sequence
->elements
, (size_t)sequence_length
);
352 bt_ctf_field_get(length_field
);
353 sequence
->length
= length_field
;
358 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
359 struct bt_ctf_field
*field
, const char *name
)
361 struct bt_ctf_field
*new_field
= NULL
;
363 struct bt_ctf_field_structure
*structure
;
364 struct bt_ctf_field_type
*field_type
= NULL
;
367 if (!field
|| !name
||
368 bt_ctf_field_type_get_type_id(field
->type
) !=
373 field_quark
= g_quark_from_string(name
);
374 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
376 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
378 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
379 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
383 if (structure
->fields
->pdata
[index
]) {
384 new_field
= structure
->fields
->pdata
[index
];
388 new_field
= bt_ctf_field_create(field_type
);
393 structure
->fields
->pdata
[index
] = new_field
;
395 bt_ctf_field_get(new_field
);
398 bt_ctf_field_type_put(field_type
);
403 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
404 struct bt_ctf_field
*field
, int index
)
407 const char *field_name
;
408 struct bt_ctf_field_structure
*structure
;
409 struct bt_ctf_field_type
*structure_type
;
410 struct bt_ctf_field_type
*field_type
= NULL
;
411 struct bt_ctf_field
*ret_field
= NULL
;
414 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
418 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
419 if (index
>= structure
->fields
->len
) {
423 ret_field
= structure
->fields
->pdata
[index
];
428 /* Field has not been instanciated yet, create it */
429 structure_type
= bt_ctf_field_get_type(field
);
430 if (!structure_type
) {
434 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
435 &field_name
, &field_type
, index
);
436 bt_ctf_field_type_put(structure_type
);
441 ret_field
= bt_ctf_field_create(field_type
);
446 structure
->fields
->pdata
[index
] = ret_field
;
448 bt_ctf_field_get(ret_field
);
451 bt_ctf_field_type_put(field_type
);
457 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
458 const char *name
, struct bt_ctf_field
*value
)
462 struct bt_ctf_field_structure
*structure
;
463 struct bt_ctf_field_type
*expected_field_type
= NULL
;
466 if (!field
|| !name
|| !value
||
467 bt_ctf_field_type_get_type_id(field
->type
) !=
473 field_quark
= g_quark_from_string(name
);
474 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
475 expected_field_type
=
476 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
478 if (expected_field_type
!= value
->type
) {
483 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
484 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
488 if (structure
->fields
->pdata
[index
]) {
489 bt_ctf_field_put(structure
->fields
->pdata
[index
]);
492 structure
->fields
->pdata
[index
] = value
;
493 bt_ctf_field_get(value
);
495 if (expected_field_type
) {
496 bt_ctf_field_type_put(expected_field_type
);
501 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
504 struct bt_ctf_field
*new_field
= NULL
;
505 struct bt_ctf_field_type
*field_type
= NULL
;
506 struct bt_ctf_field_array
*array
;
508 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
513 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
514 if (index
>= array
->elements
->len
) {
518 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
519 if (array
->elements
->pdata
[(size_t)index
]) {
520 new_field
= array
->elements
->pdata
[(size_t)index
];
524 new_field
= bt_ctf_field_create(field_type
);
525 array
->elements
->pdata
[(size_t)index
] = new_field
;
528 bt_ctf_field_type_put(field_type
);
531 bt_ctf_field_get(new_field
);
536 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
539 struct bt_ctf_field
*new_field
= NULL
;
540 struct bt_ctf_field_type
*field_type
= NULL
;
541 struct bt_ctf_field_sequence
*sequence
;
543 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
548 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
549 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
553 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
554 if (sequence
->elements
->pdata
[(size_t)index
]) {
555 new_field
= sequence
->elements
->pdata
[(size_t)index
];
559 new_field
= bt_ctf_field_create(field_type
);
560 sequence
->elements
->pdata
[(size_t)index
] = new_field
;
563 bt_ctf_field_type_put(field_type
);
566 bt_ctf_field_get(new_field
);
571 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
572 struct bt_ctf_field
*tag_field
)
574 struct bt_ctf_field
*new_field
= NULL
;
575 struct bt_ctf_field_variant
*variant
;
576 struct bt_ctf_field_type_variant
*variant_type
;
577 struct bt_ctf_field_type
*field_type
;
578 struct bt_ctf_field
*tag_enum
= NULL
;
579 struct bt_ctf_field_integer
*tag_enum_integer
;
580 int64_t tag_enum_value
;
582 if (!field
|| !tag_field
||
583 bt_ctf_field_type_get_type_id(field
->type
) !=
585 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
590 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
591 variant_type
= container_of(field
->type
,
592 struct bt_ctf_field_type_variant
, parent
);
593 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
598 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
601 if (bt_ctf_field_validate(tag_field
) < 0) {
605 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
608 * If the variant currently has a tag and a payload, and if the
609 * requested tag value is the same as the current one, return
610 * the current payload instead of creating a fresh one.
612 if (variant
->tag
&& variant
->payload
) {
613 struct bt_ctf_field
*cur_tag_container
= NULL
;
614 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
615 int64_t cur_tag_value
;
618 bt_ctf_field_enumeration_get_container(variant
->tag
);
619 cur_tag_enum_integer
= container_of(cur_tag_container
,
620 struct bt_ctf_field_integer
, parent
);
621 bt_ctf_field_put(cur_tag_container
);
622 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
624 if (cur_tag_value
== tag_enum_value
) {
625 new_field
= variant
->payload
;
626 bt_ctf_field_get(new_field
);
631 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
632 variant_type
, tag_enum_value
);
637 new_field
= bt_ctf_field_create(field_type
);
642 bt_ctf_field_put(variant
->tag
);
643 bt_ctf_field_put(variant
->payload
);
644 bt_ctf_field_get(new_field
);
645 bt_ctf_field_get(tag_field
);
646 variant
->tag
= tag_field
;
647 variant
->payload
= new_field
;
649 bt_ctf_field_put(tag_enum
);
653 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
654 struct bt_ctf_field
*variant_field
)
656 struct bt_ctf_field
*current_field
= NULL
;
657 struct bt_ctf_field_variant
*variant
;
659 if (!variant_field
||
660 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
665 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
668 if (variant
->payload
) {
669 current_field
= variant
->payload
;
670 bt_ctf_field_get(current_field
);
675 return current_field
;
678 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
679 struct bt_ctf_field
*field
)
681 struct bt_ctf_field
*container
= NULL
;
682 struct bt_ctf_field_enumeration
*enumeration
;
684 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
689 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
691 if (!enumeration
->payload
) {
692 struct bt_ctf_field_type_enumeration
*enumeration_type
=
693 container_of(field
->type
,
694 struct bt_ctf_field_type_enumeration
, parent
);
695 enumeration
->payload
=
696 bt_ctf_field_create(enumeration_type
->container
);
699 container
= enumeration
->payload
;
700 bt_ctf_field_get(container
);
705 const char *bt_ctf_field_enumeration_get_mapping_name(
706 struct bt_ctf_field
*field
)
709 const char *name
= NULL
;
710 struct bt_ctf_field
*container
= NULL
;
711 struct bt_ctf_field_type
*container_type
= NULL
;
712 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
713 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
715 container
= bt_ctf_field_enumeration_get_container(field
);
720 container_type
= bt_ctf_field_get_type(container
);
721 if (!container_type
) {
722 goto error_put_container
;
725 integer_type
= container_of(container_type
,
726 struct bt_ctf_field_type_integer
, parent
);
727 enumeration_type
= container_of(field
->type
,
728 struct bt_ctf_field_type_enumeration
, parent
);
730 if (!integer_type
->declaration
.signedness
) {
732 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
735 goto error_put_container_type
;
738 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
739 enumeration_type
, value
);
742 ret
= bt_ctf_field_signed_integer_get_value(container
,
745 goto error_put_container_type
;
748 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
749 enumeration_type
, value
);
752 error_put_container_type
:
753 bt_ctf_field_type_put(container_type
);
755 bt_ctf_field_put(container
);
760 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
764 struct bt_ctf_field_integer
*integer
;
765 struct bt_ctf_field_type_integer
*integer_type
;
767 if (!field
|| !value
|| !field
->payload_set
||
768 bt_ctf_field_type_get_type_id(field
->type
) !=
774 integer_type
= container_of(field
->type
,
775 struct bt_ctf_field_type_integer
, parent
);
776 if (!integer_type
->declaration
.signedness
) {
781 integer
= container_of(field
,
782 struct bt_ctf_field_integer
, parent
);
783 *value
= integer
->definition
.value
._signed
;
788 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
792 struct bt_ctf_field_integer
*integer
;
793 struct bt_ctf_field_type_integer
*integer_type
;
795 int64_t min_value
, max_value
;
798 bt_ctf_field_type_get_type_id(field
->type
) !=
804 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
805 integer_type
= container_of(field
->type
,
806 struct bt_ctf_field_type_integer
, parent
);
807 if (!integer_type
->declaration
.signedness
) {
812 size
= integer_type
->declaration
.len
;
813 min_value
= -((int64_t)1 << (size
- 1));
814 max_value
= ((int64_t)1 << (size
- 1)) - 1;
815 if (value
< min_value
|| value
> max_value
) {
820 integer
->definition
.value
._signed
= value
;
821 integer
->parent
.payload_set
= 1;
826 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
830 struct bt_ctf_field_integer
*integer
;
831 struct bt_ctf_field_type_integer
*integer_type
;
833 if (!field
|| !value
|| !field
->payload_set
||
834 bt_ctf_field_type_get_type_id(field
->type
) !=
840 integer_type
= container_of(field
->type
,
841 struct bt_ctf_field_type_integer
, parent
);
842 if (integer_type
->declaration
.signedness
) {
847 integer
= container_of(field
,
848 struct bt_ctf_field_integer
, parent
);
849 *value
= integer
->definition
.value
._unsigned
;
854 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
858 struct bt_ctf_field_integer
*integer
;
859 struct bt_ctf_field_type_integer
*integer_type
;
864 bt_ctf_field_type_get_type_id(field
->type
) !=
870 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
871 integer_type
= container_of(field
->type
,
872 struct bt_ctf_field_type_integer
, parent
);
873 if (integer_type
->declaration
.signedness
) {
878 size
= integer_type
->declaration
.len
;
879 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
880 if (value
> max_value
) {
885 integer
->definition
.value
._unsigned
= value
;
886 integer
->parent
.payload_set
= 1;
891 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
895 struct bt_ctf_field_floating_point
*floating_point
;
897 if (!field
|| !value
|| !field
->payload_set
||
898 bt_ctf_field_type_get_type_id(field
->type
) !=
904 floating_point
= container_of(field
,
905 struct bt_ctf_field_floating_point
, parent
);
906 *value
= floating_point
->definition
.value
;
911 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
915 struct bt_ctf_field_floating_point
*floating_point
;
918 bt_ctf_field_type_get_type_id(field
->type
) !=
923 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
925 floating_point
->definition
.value
= value
;
926 floating_point
->parent
.payload_set
= 1;
931 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
933 const char *ret
= NULL
;
934 struct bt_ctf_field_string
*string
;
936 if (!field
|| !field
->payload_set
||
937 bt_ctf_field_type_get_type_id(field
->type
) !=
942 string
= container_of(field
,
943 struct bt_ctf_field_string
, parent
);
944 ret
= string
->payload
->str
;
949 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
953 struct bt_ctf_field_string
*string
;
955 if (!field
|| !value
||
956 bt_ctf_field_type_get_type_id(field
->type
) !=
962 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
963 if (string
->payload
) {
964 g_string_assign(string
->payload
, value
);
966 string
->payload
= g_string_new(value
);
969 string
->parent
.payload_set
= 1;
974 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
978 struct bt_ctf_field_string
*string_field
;
980 if (!field
|| !value
||
981 bt_ctf_field_type_get_type_id(field
->type
) !=
987 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
989 if (string_field
->payload
) {
990 g_string_append(string_field
->payload
, value
);
992 string_field
->payload
= g_string_new(value
);
995 string_field
->parent
.payload_set
= 1;
1001 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1002 const char *value
, unsigned int length
)
1006 unsigned int effective_length
= length
;
1007 struct bt_ctf_field_string
*string_field
;
1009 if (!field
|| !value
||
1010 bt_ctf_field_type_get_type_id(field
->type
) !=
1016 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1018 /* make sure no null bytes are appended */
1019 for (i
= 0; i
< length
; ++i
) {
1020 if (value
[i
] == '\0') {
1021 effective_length
= i
;
1026 if (string_field
->payload
) {
1027 g_string_append_len(string_field
->payload
, value
,
1030 string_field
->payload
= g_string_new_len(value
,
1034 string_field
->parent
.payload_set
= 1;
1041 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1044 enum ctf_type_id type_id
;
1051 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1052 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1057 ret
= field_validate_funcs
[type_id
](field
);
1063 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1066 enum ctf_type_id type_id
;
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_reset_funcs
[type_id
](field
);
1085 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1086 struct ctf_stream_pos
*pos
)
1089 enum ctf_type_id type_id
;
1091 if (!field
|| !pos
) {
1096 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1097 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1102 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1107 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1110 struct bt_ctf_field
*copy
= NULL
;
1111 enum ctf_type_id type_id
;
1117 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1118 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1122 copy
= bt_ctf_field_create(field
->type
);
1127 copy
->payload_set
= field
->payload_set
;
1128 ret
= field_copy_funcs
[type_id
](field
, copy
);
1130 bt_ctf_field_put(copy
);
1138 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1140 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1141 struct bt_ctf_field_type_integer
, parent
);
1142 struct bt_ctf_field_integer
*integer
= g_new0(
1143 struct bt_ctf_field_integer
, 1);
1146 integer
->definition
.declaration
= &integer_type
->declaration
;
1149 return integer
? &integer
->parent
: NULL
;
1153 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1154 struct bt_ctf_field_type
*type
)
1156 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1157 struct bt_ctf_field_enumeration
, 1);
1159 return enumeration
? &enumeration
->parent
: NULL
;
1163 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1164 struct bt_ctf_field_type
*type
)
1166 struct bt_ctf_field_floating_point
*floating_point
;
1167 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1169 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1170 if (!floating_point
) {
1174 floating_point_type
= container_of(type
,
1175 struct bt_ctf_field_type_floating_point
, parent
);
1176 floating_point
->definition
.declaration
= container_of(
1177 type
->declaration
, struct declaration_float
, p
);
1180 floating_point
->definition
.sign
= &floating_point
->sign
;
1181 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1182 floating_point
->definition
.sign
->p
.declaration
=
1183 &floating_point_type
->sign
.p
;
1185 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1186 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1187 floating_point
->definition
.mantissa
->p
.declaration
=
1188 &floating_point_type
->mantissa
.p
;
1190 floating_point
->definition
.exp
= &floating_point
->exp
;
1191 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1192 floating_point
->definition
.exp
->p
.declaration
=
1193 &floating_point_type
->exp
.p
;
1196 return floating_point
? &floating_point
->parent
: NULL
;
1200 struct bt_ctf_field
*bt_ctf_field_structure_create(
1201 struct bt_ctf_field_type
*type
)
1203 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1204 struct bt_ctf_field_type_structure
, parent
);
1205 struct bt_ctf_field_structure
*structure
= g_new0(
1206 struct bt_ctf_field_structure
, 1);
1207 struct bt_ctf_field
*field
= NULL
;
1209 if (!structure
|| !structure_type
->fields
->len
) {
1213 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1214 structure
->fields
= g_ptr_array_new_with_free_func(
1215 (GDestroyNotify
)bt_ctf_field_put
);
1216 g_ptr_array_set_size(structure
->fields
,
1217 g_hash_table_size(structure
->field_name_to_index
));
1218 field
= &structure
->parent
;
1224 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1226 struct bt_ctf_field_variant
*variant
= g_new0(
1227 struct bt_ctf_field_variant
, 1);
1228 return variant
? &variant
->parent
: NULL
;
1232 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1234 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1235 struct bt_ctf_field_type_array
*array_type
;
1236 unsigned int array_length
;
1238 if (!array
|| !type
) {
1242 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1243 array_length
= array_type
->length
;
1244 array
->elements
= g_ptr_array_sized_new(array_length
);
1245 if (!array
->elements
) {
1249 g_ptr_array_set_free_func(array
->elements
,
1250 (GDestroyNotify
)bt_ctf_field_put
);
1251 g_ptr_array_set_size(array
->elements
, array_length
);
1252 return &array
->parent
;
1259 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1260 struct bt_ctf_field_type
*type
)
1262 struct bt_ctf_field_sequence
*sequence
= g_new0(
1263 struct bt_ctf_field_sequence
, 1);
1264 return sequence
? &sequence
->parent
: NULL
;
1268 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1270 struct bt_ctf_field_string
*string
= g_new0(
1271 struct bt_ctf_field_string
, 1);
1272 return string
? &string
->parent
: NULL
;
1276 void bt_ctf_field_destroy(struct bt_ref
*ref
)
1278 struct bt_ctf_field
*field
;
1279 struct bt_ctf_base
*base
;
1280 struct bt_ctf_field_type
*type
;
1281 enum ctf_type_id type_id
;
1287 base
= container_of(ref
, struct bt_ctf_base
, ref_count
);
1288 field
= container_of(base
, struct bt_ctf_field
, base
);
1290 type_id
= bt_ctf_field_type_get_type_id(type
);
1291 if (type_id
<= CTF_TYPE_UNKNOWN
||
1292 type_id
>= NR_CTF_TYPES
) {
1296 field_destroy_funcs
[type_id
](field
);
1298 bt_ctf_field_type_put(type
);
1303 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1305 struct bt_ctf_field_integer
*integer
;
1311 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1316 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1318 struct bt_ctf_field_enumeration
*enumeration
;
1324 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1326 bt_ctf_field_put(enumeration
->payload
);
1327 g_free(enumeration
);
1331 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1333 struct bt_ctf_field_floating_point
*floating_point
;
1339 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1341 g_free(floating_point
);
1345 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1347 struct bt_ctf_field_structure
*structure
;
1353 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1354 g_ptr_array_free(structure
->fields
, TRUE
);
1359 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1361 struct bt_ctf_field_variant
*variant
;
1367 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1368 bt_ctf_field_put(variant
->tag
);
1369 bt_ctf_field_put(variant
->payload
);
1374 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1376 struct bt_ctf_field_array
*array
;
1382 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1383 g_ptr_array_free(array
->elements
, TRUE
);
1388 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1390 struct bt_ctf_field_sequence
*sequence
;
1396 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1397 if (sequence
->elements
) {
1398 g_ptr_array_free(sequence
->elements
, TRUE
);
1400 bt_ctf_field_put(sequence
->length
);
1405 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1407 struct bt_ctf_field_string
*string
;
1412 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1413 if (string
->payload
) {
1414 g_string_free(string
->payload
, TRUE
);
1420 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1422 return (field
&& field
->payload_set
) ? 0 : -1;
1426 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1429 struct bt_ctf_field_enumeration
*enumeration
;
1436 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1438 if (!enumeration
->payload
) {
1443 ret
= bt_ctf_field_validate(enumeration
->payload
);
1449 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1453 struct bt_ctf_field_structure
*structure
;
1460 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1461 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1462 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1472 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1475 struct bt_ctf_field_variant
*variant
;
1482 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1483 ret
= bt_ctf_field_validate(variant
->payload
);
1489 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1493 struct bt_ctf_field_array
*array
;
1500 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1501 for (i
= 0; i
< array
->elements
->len
; i
++) {
1502 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1512 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1516 struct bt_ctf_field_sequence
*sequence
;
1523 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1524 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1525 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1535 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1544 field
->payload_set
= 0;
1550 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1553 struct bt_ctf_field_enumeration
*enumeration
;
1560 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1562 if (!enumeration
->payload
) {
1566 ret
= bt_ctf_field_reset(enumeration
->payload
);
1572 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1576 struct bt_ctf_field_structure
*structure
;
1583 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1584 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1585 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1589 * Structure members are lazily initialized; skip if
1590 * this member has not been allocated yet.
1595 ret
= bt_ctf_field_reset(member
);
1605 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1608 struct bt_ctf_field_variant
*variant
;
1615 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1616 if (variant
->payload
) {
1617 ret
= bt_ctf_field_reset(variant
->payload
);
1624 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1628 struct bt_ctf_field_array
*array
;
1635 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1636 for (i
= 0; i
< array
->elements
->len
; i
++) {
1637 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1641 * Array elements are lazily initialized; skip if
1642 * this member has not been allocated yet.
1647 ret
= bt_ctf_field_reset(member
);
1657 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1661 struct bt_ctf_field_sequence
*sequence
;
1668 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1669 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1670 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1674 * Sequence elements are lazily initialized; skip if
1675 * this member has not been allocated yet.
1680 ret
= bt_ctf_field_reset(member
);
1690 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1693 struct bt_ctf_field_string
*string
;
1700 ret
= bt_ctf_field_generic_reset(field
);
1705 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1706 if (string
->payload
) {
1707 g_string_truncate(string
->payload
, 0);
1714 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1715 struct ctf_stream_pos
*pos
)
1718 struct bt_ctf_field_integer
*integer
= container_of(field
,
1719 struct bt_ctf_field_integer
, parent
);
1722 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1723 if (ret
== -EFAULT
) {
1725 * The field is too large to fit in the current packet's
1726 * remaining space. Bump the packet size and retry.
1728 ret
= increase_packet_size(pos
);
1739 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1740 struct ctf_stream_pos
*pos
)
1742 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1743 field
, struct bt_ctf_field_enumeration
, parent
);
1745 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1749 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1750 struct ctf_stream_pos
*pos
)
1753 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1754 struct bt_ctf_field_floating_point
, parent
);
1757 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1758 if (ret
== -EFAULT
) {
1760 * The field is too large to fit in the current packet's
1761 * remaining space. Bump the packet size and retry.
1763 ret
= increase_packet_size(pos
);
1774 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1775 struct ctf_stream_pos
*pos
)
1779 struct bt_ctf_field_structure
*structure
= container_of(
1780 field
, struct bt_ctf_field_structure
, parent
);
1782 while (!ctf_pos_access_ok(pos
,
1783 offset_align(pos
->offset
,
1784 field
->type
->declaration
->alignment
))) {
1785 ret
= increase_packet_size(pos
);
1791 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1796 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1797 struct bt_ctf_field
*field
= g_ptr_array_index(
1798 structure
->fields
, i
);
1800 ret
= bt_ctf_field_serialize(field
, pos
);
1810 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1811 struct ctf_stream_pos
*pos
)
1813 struct bt_ctf_field_variant
*variant
= container_of(
1814 field
, struct bt_ctf_field_variant
, parent
);
1816 return bt_ctf_field_serialize(variant
->payload
, pos
);
1820 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1821 struct ctf_stream_pos
*pos
)
1825 struct bt_ctf_field_array
*array
= container_of(
1826 field
, struct bt_ctf_field_array
, parent
);
1828 for (i
= 0; i
< array
->elements
->len
; i
++) {
1829 ret
= bt_ctf_field_serialize(
1830 g_ptr_array_index(array
->elements
, i
), pos
);
1840 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1841 struct ctf_stream_pos
*pos
)
1845 struct bt_ctf_field_sequence
*sequence
= container_of(
1846 field
, struct bt_ctf_field_sequence
, parent
);
1848 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1849 ret
= bt_ctf_field_serialize(
1850 g_ptr_array_index(sequence
->elements
, i
), pos
);
1860 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1861 struct ctf_stream_pos
*pos
)
1865 struct bt_ctf_field_string
*string
= container_of(field
,
1866 struct bt_ctf_field_string
, parent
);
1867 struct bt_ctf_field_type
*character_type
=
1868 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1869 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1871 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1872 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1873 (uint64_t) string
->payload
->str
[i
]);
1878 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1884 bt_ctf_field_put(character
);
1885 bt_ctf_field_type_put(character_type
);
1890 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1891 struct bt_ctf_field
*dst
)
1893 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1895 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1896 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1898 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1899 sizeof(struct definition_integer
));
1904 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1905 struct bt_ctf_field
*dst
)
1908 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1910 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1911 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1913 if (enum_src
->payload
) {
1914 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1915 if (!enum_dst
->payload
) {
1925 int bt_ctf_field_floating_point_copy(
1926 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1928 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1930 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1932 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1935 memcpy(&float_dst
->definition
, &float_src
->definition
,
1936 sizeof(struct definition_float
));
1937 memcpy(&float_dst
->sign
, &float_src
->sign
,
1938 sizeof(struct definition_integer
));
1939 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1940 sizeof(struct definition_integer
));
1941 memcpy(&float_dst
->exp
, &float_src
->exp
,
1942 sizeof(struct definition_integer
));
1947 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
1948 struct bt_ctf_field
*dst
)
1951 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
1953 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
1954 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
1956 /* This field_name_to_index HT is owned by the structure field type */
1957 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
1958 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
1960 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
1961 struct bt_ctf_field
*field
=
1962 g_ptr_array_index(struct_src
->fields
, i
);
1963 struct bt_ctf_field
*field_copy
= NULL
;
1966 field_copy
= bt_ctf_field_copy(field
);
1974 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
1981 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
1982 struct bt_ctf_field
*dst
)
1985 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
1987 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
1988 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
1990 if (variant_src
->tag
) {
1991 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
1992 if (!variant_dst
->tag
) {
1997 if (variant_src
->payload
) {
1998 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
1999 if (!variant_dst
->payload
) {
2009 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2010 struct bt_ctf_field
*dst
)
2013 struct bt_ctf_field_array
*array_src
, *array_dst
;
2015 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2016 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2018 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2019 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2020 struct bt_ctf_field
*field
=
2021 g_ptr_array_index(array_src
->elements
, i
);
2022 struct bt_ctf_field
*field_copy
= NULL
;
2025 field_copy
= bt_ctf_field_copy(field
);
2033 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2040 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2041 struct bt_ctf_field
*dst
)
2044 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2045 struct bt_ctf_field
*src_length
;
2046 struct bt_ctf_field
*dst_length
;
2048 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2049 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2051 src_length
= bt_ctf_field_sequence_get_length(src
);
2054 /* no length set yet: keep destination sequence empty */
2058 /* copy source length */
2059 dst_length
= bt_ctf_field_copy(src_length
);
2060 bt_ctf_field_put(src_length
);
2067 /* this will initialize the destination sequence's internal array */
2068 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2069 bt_ctf_field_put(dst_length
);
2075 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2077 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2078 struct bt_ctf_field
*field
=
2079 g_ptr_array_index(sequence_src
->elements
, i
);
2080 struct bt_ctf_field
*field_copy
= NULL
;
2083 field_copy
= bt_ctf_field_copy(field
);
2091 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2098 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2099 struct bt_ctf_field
*dst
)
2102 struct bt_ctf_field_string
*string_src
, *string_dst
;
2104 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2105 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2107 if (string_src
->payload
) {
2108 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2109 if (!string_dst
->payload
) {
2119 int increase_packet_size(struct ctf_stream_pos
*pos
)
2124 ret
= munmap_align(pos
->base_mma
);
2129 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2130 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2131 pos
->packet_size
/ CHAR_BIT
);
2136 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2137 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2138 if (pos
->base_mma
== MAP_FAILED
) {