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-ir/fields-internal.h>
30 #include <babeltrace/ctf-ir/field-types-internal.h>
31 #include <babeltrace/object-internal.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/compiler.h>
34 #include <babeltrace/compat/fcntl.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_object
*);
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 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_create
,
158 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_create
,
159 [BT_CTF_TYPE_ID_FLOAT
] =
160 bt_ctf_field_floating_point_create
,
161 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_create
,
162 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_create
,
163 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_create
,
164 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_create
,
165 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_create
,
169 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
170 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_destroy
,
171 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_destroy
,
172 [BT_CTF_TYPE_ID_FLOAT
] =
173 bt_ctf_field_floating_point_destroy
,
174 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_destroy
,
175 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_destroy
,
176 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_destroy
,
177 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
178 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_destroy
,
182 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
183 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_validate
,
184 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_validate
,
185 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_validate
,
186 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_validate
,
187 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_validate
,
188 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_validate
,
189 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_validate
,
190 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_generic_validate
,
194 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
195 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_reset
,
196 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_reset
,
197 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_reset
,
198 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_reset
,
199 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_reset
,
200 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_reset
,
201 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_reset
,
202 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_reset
,
206 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
207 struct ctf_stream_pos
*) = {
208 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_serialize
,
209 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_serialize
,
210 [BT_CTF_TYPE_ID_FLOAT
] =
211 bt_ctf_field_floating_point_serialize
,
212 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_serialize
,
213 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_serialize
,
214 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_serialize
,
215 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
216 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_serialize
,
220 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
221 struct bt_ctf_field
*) = {
222 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_copy
,
223 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_copy
,
224 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_floating_point_copy
,
225 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_copy
,
226 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_copy
,
227 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_copy
,
228 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_copy
,
229 [BT_CTF_TYPE_ID_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 bt_ctf_type_id type_id
;
242 type_id
= bt_ctf_field_type_get_type_id(type
);
243 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
244 type_id
>= BT_CTF_NR_TYPE_IDS
) {
248 /* Field class MUST be valid */
249 ret
= bt_ctf_field_type_validate(type
);
256 field
= field_create_funcs
[type_id
](type
);
261 /* The type's declaration can't change after this point */
262 bt_ctf_field_type_freeze(type
);
264 bt_object_init(field
, bt_ctf_field_destroy
);
270 void bt_ctf_field_get(struct bt_ctf_field
*field
)
275 void bt_ctf_field_put(struct bt_ctf_field
*field
)
280 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
282 struct bt_ctf_field_type
*ret
= NULL
;
294 enum bt_ctf_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
296 enum bt_ctf_type_id ret
= BT_CTF_TYPE_ID_UNKNOWN
;
302 ret
= bt_ctf_field_type_get_type_id(field
->type
);
307 int bt_ctf_field_is_integer(struct bt_ctf_field
*field
)
309 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_INTEGER
;
312 int bt_ctf_field_is_floating_point(struct bt_ctf_field
*field
)
314 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_FLOAT
;
317 int bt_ctf_field_is_enumeration(struct bt_ctf_field
*field
)
319 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_ENUM
;
322 int bt_ctf_field_is_string(struct bt_ctf_field
*field
)
324 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_STRING
;
327 int bt_ctf_field_is_structure(struct bt_ctf_field
*field
)
329 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_STRUCT
;
332 int bt_ctf_field_is_array(struct bt_ctf_field
*field
)
334 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_ARRAY
;
337 int bt_ctf_field_is_sequence(struct bt_ctf_field
*field
)
339 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_SEQUENCE
;
342 int bt_ctf_field_is_variant(struct bt_ctf_field
*field
)
344 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_VARIANT
;
347 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
348 struct bt_ctf_field
*field
)
350 struct bt_ctf_field
*ret
= NULL
;
351 struct bt_ctf_field_sequence
*sequence
;
357 if (bt_ctf_field_type_get_type_id(field
->type
) !=
358 BT_CTF_TYPE_ID_SEQUENCE
) {
362 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
363 ret
= sequence
->length
;
369 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
370 struct bt_ctf_field
*length_field
)
373 struct bt_ctf_field_type_integer
*length_type
;
374 struct bt_ctf_field_integer
*length
;
375 struct bt_ctf_field_sequence
*sequence
;
376 uint64_t sequence_length
;
378 if (!field
|| !length_field
) {
382 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
383 BT_CTF_TYPE_ID_INTEGER
) {
388 length_type
= container_of(length_field
->type
,
389 struct bt_ctf_field_type_integer
, parent
);
390 /* The length field must be unsigned */
391 if (length_type
->declaration
.signedness
) {
396 length
= container_of(length_field
, struct bt_ctf_field_integer
,
398 sequence_length
= length
->definition
.value
._unsigned
;
399 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
400 if (sequence
->elements
) {
401 g_ptr_array_free(sequence
->elements
, TRUE
);
402 bt_put(sequence
->length
);
405 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
406 if (!sequence
->elements
) {
411 g_ptr_array_set_free_func(sequence
->elements
,
412 (GDestroyNotify
) bt_put
);
413 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
414 bt_get(length_field
);
415 sequence
->length
= length_field
;
420 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
421 struct bt_ctf_field
*field
, const char *name
)
423 struct bt_ctf_field
*new_field
= NULL
;
425 struct bt_ctf_field_structure
*structure
;
426 struct bt_ctf_field_type
*field_type
= NULL
;
429 if (!field
|| !name
||
430 bt_ctf_field_type_get_type_id(field
->type
) !=
431 BT_CTF_TYPE_ID_STRUCT
) {
435 field_quark
= g_quark_from_string(name
);
436 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
438 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
440 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
441 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
445 if (structure
->fields
->pdata
[index
]) {
446 new_field
= structure
->fields
->pdata
[index
];
450 new_field
= bt_ctf_field_create(field_type
);
455 structure
->fields
->pdata
[index
] = new_field
;
465 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
466 struct bt_ctf_field
*field
, int index
)
469 const char *field_name
;
470 struct bt_ctf_field_structure
*structure
;
471 struct bt_ctf_field_type
*structure_type
;
472 struct bt_ctf_field_type
*field_type
= NULL
;
473 struct bt_ctf_field
*ret_field
= NULL
;
476 bt_ctf_field_type_get_type_id(field
->type
) !=
477 BT_CTF_TYPE_ID_STRUCT
) {
481 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
482 if (index
>= structure
->fields
->len
) {
486 ret_field
= structure
->fields
->pdata
[index
];
491 /* Field has not been instanciated yet, create it */
492 structure_type
= bt_ctf_field_get_type(field
);
493 if (!structure_type
) {
497 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
498 &field_name
, &field_type
, index
);
499 bt_put(structure_type
);
504 ret_field
= bt_ctf_field_create(field_type
);
509 structure
->fields
->pdata
[index
] = ret_field
;
518 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
519 const char *name
, struct bt_ctf_field
*value
)
523 struct bt_ctf_field_structure
*structure
;
524 struct bt_ctf_field_type
*expected_field_type
= NULL
;
527 if (!field
|| !name
|| !value
||
528 bt_ctf_field_type_get_type_id(field
->type
) !=
529 BT_CTF_TYPE_ID_STRUCT
) {
534 field_quark
= g_quark_from_string(name
);
535 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
536 expected_field_type
=
537 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
540 if (bt_ctf_field_type_compare(expected_field_type
, value
->type
)) {
545 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
546 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
550 if (structure
->fields
->pdata
[index
]) {
551 bt_put(structure
->fields
->pdata
[index
]);
554 structure
->fields
->pdata
[index
] = value
;
557 if (expected_field_type
) {
558 bt_put(expected_field_type
);
563 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
566 struct bt_ctf_field
*new_field
= NULL
;
567 struct bt_ctf_field_type
*field_type
= NULL
;
568 struct bt_ctf_field_array
*array
;
570 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
571 BT_CTF_TYPE_ID_ARRAY
) {
575 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
576 if (index
>= array
->elements
->len
) {
580 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
581 if (array
->elements
->pdata
[(size_t)index
]) {
582 new_field
= array
->elements
->pdata
[(size_t)index
];
586 new_field
= bt_ctf_field_create(field_type
);
587 array
->elements
->pdata
[(size_t)index
] = new_field
;
598 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
601 struct bt_ctf_field
*new_field
= NULL
;
602 struct bt_ctf_field_type
*field_type
= NULL
;
603 struct bt_ctf_field_sequence
*sequence
;
605 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
606 BT_CTF_TYPE_ID_SEQUENCE
) {
610 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
611 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
615 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
616 if (sequence
->elements
->pdata
[(size_t) index
]) {
617 new_field
= sequence
->elements
->pdata
[(size_t) index
];
621 new_field
= bt_ctf_field_create(field_type
);
622 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
633 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
634 struct bt_ctf_field
*tag_field
)
636 struct bt_ctf_field
*new_field
= NULL
;
637 struct bt_ctf_field_variant
*variant
;
638 struct bt_ctf_field_type_variant
*variant_type
;
639 struct bt_ctf_field_type
*field_type
;
640 struct bt_ctf_field
*tag_enum
= NULL
;
641 struct bt_ctf_field_integer
*tag_enum_integer
;
642 int64_t tag_enum_value
;
644 if (!field
|| !tag_field
||
645 bt_ctf_field_type_get_type_id(field
->type
) !=
646 BT_CTF_TYPE_ID_VARIANT
||
647 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
648 BT_CTF_TYPE_ID_ENUM
) {
652 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
653 variant_type
= container_of(field
->type
,
654 struct bt_ctf_field_type_variant
, parent
);
655 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
660 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
663 if (bt_ctf_field_validate(tag_field
) < 0) {
667 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
670 * If the variant currently has a tag and a payload, and if the
671 * requested tag value is the same as the current one, return
672 * the current payload instead of creating a fresh one.
674 if (variant
->tag
&& variant
->payload
) {
675 struct bt_ctf_field
*cur_tag_container
= NULL
;
676 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
677 int64_t cur_tag_value
;
680 bt_ctf_field_enumeration_get_container(variant
->tag
);
681 assert(cur_tag_container
);
682 cur_tag_enum_integer
= container_of(cur_tag_container
,
683 struct bt_ctf_field_integer
, parent
);
684 bt_put(cur_tag_container
);
685 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
687 if (cur_tag_value
== tag_enum_value
) {
688 new_field
= variant
->payload
;
694 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
695 variant_type
, tag_enum_value
);
700 new_field
= bt_ctf_field_create(field_type
);
705 bt_put(variant
->tag
);
706 bt_put(variant
->payload
);
709 variant
->tag
= tag_field
;
710 variant
->payload
= new_field
;
716 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
717 struct bt_ctf_field
*variant_field
)
719 struct bt_ctf_field
*current_field
= NULL
;
720 struct bt_ctf_field_variant
*variant
;
722 if (!variant_field
||
723 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
724 BT_CTF_TYPE_ID_VARIANT
) {
728 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
731 if (variant
->payload
) {
732 current_field
= variant
->payload
;
733 bt_get(current_field
);
738 return current_field
;
741 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
742 struct bt_ctf_field
*field
)
744 struct bt_ctf_field
*container
= NULL
;
745 struct bt_ctf_field_enumeration
*enumeration
;
747 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
748 BT_CTF_TYPE_ID_ENUM
) {
752 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
754 if (!enumeration
->payload
) {
755 struct bt_ctf_field_type_enumeration
*enumeration_type
=
756 container_of(field
->type
,
757 struct bt_ctf_field_type_enumeration
, parent
);
758 enumeration
->payload
=
759 bt_ctf_field_create(enumeration_type
->container
);
762 container
= enumeration
->payload
;
768 const char *bt_ctf_field_enumeration_get_mapping_name(
769 struct bt_ctf_field
*field
)
772 const char *name
= NULL
;
773 struct bt_ctf_field
*container
= NULL
;
774 struct bt_ctf_field_type
*container_type
= NULL
;
775 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
776 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
778 container
= bt_ctf_field_enumeration_get_container(field
);
783 container_type
= bt_ctf_field_get_type(container
);
784 if (!container_type
) {
785 goto error_put_container
;
788 integer_type
= container_of(container_type
,
789 struct bt_ctf_field_type_integer
, parent
);
790 enumeration_type
= container_of(field
->type
,
791 struct bt_ctf_field_type_enumeration
, parent
);
793 if (!integer_type
->declaration
.signedness
) {
795 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
798 goto error_put_container_type
;
801 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
802 enumeration_type
, value
);
805 ret
= bt_ctf_field_signed_integer_get_value(container
,
808 goto error_put_container_type
;
811 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
812 enumeration_type
, value
);
815 error_put_container_type
:
816 bt_put(container_type
);
823 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
827 struct bt_ctf_field_integer
*integer
;
828 struct bt_ctf_field_type_integer
*integer_type
;
830 if (!field
|| !value
|| !field
->payload_set
||
831 bt_ctf_field_type_get_type_id(field
->type
) !=
832 BT_CTF_TYPE_ID_INTEGER
) {
837 integer_type
= container_of(field
->type
,
838 struct bt_ctf_field_type_integer
, parent
);
839 if (!integer_type
->declaration
.signedness
) {
844 integer
= container_of(field
,
845 struct bt_ctf_field_integer
, parent
);
846 *value
= integer
->definition
.value
._signed
;
851 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
855 struct bt_ctf_field_integer
*integer
;
856 struct bt_ctf_field_type_integer
*integer_type
;
858 int64_t min_value
, max_value
;
861 bt_ctf_field_type_get_type_id(field
->type
) !=
862 BT_CTF_TYPE_ID_INTEGER
) {
867 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
868 integer_type
= container_of(field
->type
,
869 struct bt_ctf_field_type_integer
, parent
);
870 if (!integer_type
->declaration
.signedness
) {
875 size
= integer_type
->declaration
.len
;
876 min_value
= -((int64_t)1 << (size
- 1));
877 max_value
= ((int64_t)1 << (size
- 1)) - 1;
878 if (value
< min_value
|| value
> max_value
) {
883 integer
->definition
.value
._signed
= value
;
884 integer
->parent
.payload_set
= 1;
889 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
893 struct bt_ctf_field_integer
*integer
;
894 struct bt_ctf_field_type_integer
*integer_type
;
896 if (!field
|| !value
|| !field
->payload_set
||
897 bt_ctf_field_type_get_type_id(field
->type
) !=
898 BT_CTF_TYPE_ID_INTEGER
) {
903 integer_type
= container_of(field
->type
,
904 struct bt_ctf_field_type_integer
, parent
);
905 if (integer_type
->declaration
.signedness
) {
910 integer
= container_of(field
,
911 struct bt_ctf_field_integer
, parent
);
912 *value
= integer
->definition
.value
._unsigned
;
917 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
921 struct bt_ctf_field_integer
*integer
;
922 struct bt_ctf_field_type_integer
*integer_type
;
927 bt_ctf_field_type_get_type_id(field
->type
) !=
928 BT_CTF_TYPE_ID_INTEGER
) {
933 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
934 integer_type
= container_of(field
->type
,
935 struct bt_ctf_field_type_integer
, parent
);
936 if (integer_type
->declaration
.signedness
) {
941 size
= integer_type
->declaration
.len
;
942 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
943 if (value
> max_value
) {
948 integer
->definition
.value
._unsigned
= value
;
949 integer
->parent
.payload_set
= 1;
954 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
958 struct bt_ctf_field_floating_point
*floating_point
;
960 if (!field
|| !value
|| !field
->payload_set
||
961 bt_ctf_field_type_get_type_id(field
->type
) !=
962 BT_CTF_TYPE_ID_FLOAT
) {
967 floating_point
= container_of(field
,
968 struct bt_ctf_field_floating_point
, parent
);
969 *value
= floating_point
->definition
.value
;
974 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
978 struct bt_ctf_field_floating_point
*floating_point
;
981 bt_ctf_field_type_get_type_id(field
->type
) !=
982 BT_CTF_TYPE_ID_FLOAT
) {
986 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
988 floating_point
->definition
.value
= value
;
989 floating_point
->parent
.payload_set
= 1;
994 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
996 const char *ret
= NULL
;
997 struct bt_ctf_field_string
*string
;
999 if (!field
|| !field
->payload_set
||
1000 bt_ctf_field_type_get_type_id(field
->type
) !=
1001 BT_CTF_TYPE_ID_STRING
) {
1005 string
= container_of(field
,
1006 struct bt_ctf_field_string
, parent
);
1007 ret
= string
->payload
->str
;
1012 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1016 struct bt_ctf_field_string
*string
;
1018 if (!field
|| !value
||
1019 bt_ctf_field_type_get_type_id(field
->type
) !=
1020 BT_CTF_TYPE_ID_STRING
) {
1025 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1026 if (string
->payload
) {
1027 g_string_assign(string
->payload
, value
);
1029 string
->payload
= g_string_new(value
);
1032 string
->parent
.payload_set
= 1;
1037 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1041 struct bt_ctf_field_string
*string_field
;
1043 if (!field
|| !value
||
1044 bt_ctf_field_type_get_type_id(field
->type
) !=
1045 BT_CTF_TYPE_ID_STRING
) {
1050 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1052 if (string_field
->payload
) {
1053 g_string_append(string_field
->payload
, value
);
1055 string_field
->payload
= g_string_new(value
);
1058 string_field
->parent
.payload_set
= 1;
1064 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1065 const char *value
, unsigned int length
)
1069 unsigned int effective_length
= length
;
1070 struct bt_ctf_field_string
*string_field
;
1072 if (!field
|| !value
||
1073 bt_ctf_field_type_get_type_id(field
->type
) !=
1074 BT_CTF_TYPE_ID_STRING
) {
1079 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1081 /* make sure no null bytes are appended */
1082 for (i
= 0; i
< length
; ++i
) {
1083 if (value
[i
] == '\0') {
1084 effective_length
= i
;
1089 if (string_field
->payload
) {
1090 g_string_append_len(string_field
->payload
, value
,
1093 string_field
->payload
= g_string_new_len(value
,
1097 string_field
->parent
.payload_set
= 1;
1104 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1107 enum bt_ctf_type_id type_id
;
1114 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1115 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1120 ret
= field_validate_funcs
[type_id
](field
);
1126 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1129 enum bt_ctf_type_id type_id
;
1136 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1137 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1142 ret
= field_reset_funcs
[type_id
](field
);
1148 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1149 struct ctf_stream_pos
*pos
)
1152 enum bt_ctf_type_id type_id
;
1154 if (!field
|| !pos
) {
1159 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1160 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1165 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1170 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1173 struct bt_ctf_field
*copy
= NULL
;
1174 enum bt_ctf_type_id type_id
;
1180 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1181 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1185 copy
= bt_ctf_field_create(field
->type
);
1190 copy
->payload_set
= field
->payload_set
;
1191 ret
= field_copy_funcs
[type_id
](field
, copy
);
1201 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1203 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1204 struct bt_ctf_field_type_integer
, parent
);
1205 struct bt_ctf_field_integer
*integer
= g_new0(
1206 struct bt_ctf_field_integer
, 1);
1209 integer
->definition
.declaration
= &integer_type
->declaration
;
1212 return integer
? &integer
->parent
: NULL
;
1216 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1217 struct bt_ctf_field_type
*type
)
1219 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1220 struct bt_ctf_field_enumeration
, 1);
1222 return enumeration
? &enumeration
->parent
: NULL
;
1226 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1227 struct bt_ctf_field_type
*type
)
1229 struct bt_ctf_field_floating_point
*floating_point
;
1230 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1232 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1233 if (!floating_point
) {
1237 floating_point_type
= container_of(type
,
1238 struct bt_ctf_field_type_floating_point
, parent
);
1239 floating_point
->definition
.declaration
= container_of(
1240 type
->declaration
, struct declaration_float
, p
);
1243 floating_point
->definition
.sign
= &floating_point
->sign
;
1244 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1245 floating_point
->definition
.sign
->p
.declaration
=
1246 &floating_point_type
->sign
.p
;
1248 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1249 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1250 floating_point
->definition
.mantissa
->p
.declaration
=
1251 &floating_point_type
->mantissa
.p
;
1253 floating_point
->definition
.exp
= &floating_point
->exp
;
1254 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1255 floating_point
->definition
.exp
->p
.declaration
=
1256 &floating_point_type
->exp
.p
;
1259 return floating_point
? &floating_point
->parent
: NULL
;
1263 struct bt_ctf_field
*bt_ctf_field_structure_create(
1264 struct bt_ctf_field_type
*type
)
1266 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1267 struct bt_ctf_field_type_structure
, parent
);
1268 struct bt_ctf_field_structure
*structure
= g_new0(
1269 struct bt_ctf_field_structure
, 1);
1270 struct bt_ctf_field
*field
= NULL
;
1276 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1277 structure
->fields
= g_ptr_array_new_with_free_func(
1278 (GDestroyNotify
)bt_ctf_field_put
);
1279 g_ptr_array_set_size(structure
->fields
,
1280 g_hash_table_size(structure
->field_name_to_index
));
1281 field
= &structure
->parent
;
1287 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1289 struct bt_ctf_field_variant
*variant
= g_new0(
1290 struct bt_ctf_field_variant
, 1);
1291 return variant
? &variant
->parent
: NULL
;
1295 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1297 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1298 struct bt_ctf_field_type_array
*array_type
;
1299 unsigned int array_length
;
1301 if (!array
|| !type
) {
1305 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1306 array_length
= array_type
->length
;
1307 array
->elements
= g_ptr_array_sized_new(array_length
);
1308 if (!array
->elements
) {
1312 g_ptr_array_set_free_func(array
->elements
,
1313 (GDestroyNotify
)bt_ctf_field_put
);
1314 g_ptr_array_set_size(array
->elements
, array_length
);
1315 return &array
->parent
;
1322 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1323 struct bt_ctf_field_type
*type
)
1325 struct bt_ctf_field_sequence
*sequence
= g_new0(
1326 struct bt_ctf_field_sequence
, 1);
1327 return sequence
? &sequence
->parent
: NULL
;
1331 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1333 struct bt_ctf_field_string
*string
= g_new0(
1334 struct bt_ctf_field_string
, 1);
1335 return string
? &string
->parent
: NULL
;
1339 void bt_ctf_field_destroy(struct bt_object
*obj
)
1341 struct bt_ctf_field
*field
;
1342 struct bt_ctf_field_type
*type
;
1343 enum bt_ctf_type_id type_id
;
1345 field
= container_of(obj
, struct bt_ctf_field
, base
);
1347 type_id
= bt_ctf_field_type_get_type_id(type
);
1348 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
1349 type_id
>= BT_CTF_NR_TYPE_IDS
) {
1353 field_destroy_funcs
[type_id
](field
);
1358 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1360 struct bt_ctf_field_integer
*integer
;
1366 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1371 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1373 struct bt_ctf_field_enumeration
*enumeration
;
1379 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1381 bt_put(enumeration
->payload
);
1382 g_free(enumeration
);
1386 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1388 struct bt_ctf_field_floating_point
*floating_point
;
1394 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1396 g_free(floating_point
);
1400 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1402 struct bt_ctf_field_structure
*structure
;
1408 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1409 g_ptr_array_free(structure
->fields
, TRUE
);
1414 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1416 struct bt_ctf_field_variant
*variant
;
1422 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1423 bt_put(variant
->tag
);
1424 bt_put(variant
->payload
);
1429 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1431 struct bt_ctf_field_array
*array
;
1437 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1438 g_ptr_array_free(array
->elements
, TRUE
);
1443 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1445 struct bt_ctf_field_sequence
*sequence
;
1451 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1452 if (sequence
->elements
) {
1453 g_ptr_array_free(sequence
->elements
, TRUE
);
1455 bt_put(sequence
->length
);
1460 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1462 struct bt_ctf_field_string
*string
;
1467 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1468 if (string
->payload
) {
1469 g_string_free(string
->payload
, TRUE
);
1475 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1477 return (field
&& field
->payload_set
) ? 0 : -1;
1481 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1484 struct bt_ctf_field_enumeration
*enumeration
;
1491 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1493 if (!enumeration
->payload
) {
1498 ret
= bt_ctf_field_validate(enumeration
->payload
);
1504 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1508 struct bt_ctf_field_structure
*structure
;
1515 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1516 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1517 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1527 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1530 struct bt_ctf_field_variant
*variant
;
1537 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1538 ret
= bt_ctf_field_validate(variant
->payload
);
1544 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1548 struct bt_ctf_field_array
*array
;
1555 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1556 for (i
= 0; i
< array
->elements
->len
; i
++) {
1557 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1567 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1571 struct bt_ctf_field_sequence
*sequence
;
1578 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1579 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1580 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1590 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1599 field
->payload_set
= 0;
1605 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1608 struct bt_ctf_field_enumeration
*enumeration
;
1615 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1617 if (!enumeration
->payload
) {
1621 ret
= bt_ctf_field_reset(enumeration
->payload
);
1627 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1631 struct bt_ctf_field_structure
*structure
;
1638 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1639 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1640 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1644 * Structure members are lazily initialized; skip if
1645 * this member has not been allocated yet.
1650 ret
= bt_ctf_field_reset(member
);
1660 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1663 struct bt_ctf_field_variant
*variant
;
1670 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1671 if (variant
->payload
) {
1672 ret
= bt_ctf_field_reset(variant
->payload
);
1679 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1683 struct bt_ctf_field_array
*array
;
1690 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1691 for (i
= 0; i
< array
->elements
->len
; i
++) {
1692 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1696 * Array elements are lazily initialized; skip if
1697 * this member has not been allocated yet.
1702 ret
= bt_ctf_field_reset(member
);
1712 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1716 struct bt_ctf_field_sequence
*sequence
;
1723 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1724 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1725 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1729 * Sequence elements are lazily initialized; skip if
1730 * this member has not been allocated yet.
1735 ret
= bt_ctf_field_reset(member
);
1745 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1748 struct bt_ctf_field_string
*string
;
1755 ret
= bt_ctf_field_generic_reset(field
);
1760 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1761 if (string
->payload
) {
1762 g_string_truncate(string
->payload
, 0);
1769 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1770 struct ctf_stream_pos
*pos
)
1773 struct bt_ctf_field_integer
*integer
= container_of(field
,
1774 struct bt_ctf_field_integer
, parent
);
1777 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1778 if (ret
== -EFAULT
) {
1780 * The field is too large to fit in the current packet's
1781 * remaining space. Bump the packet size and retry.
1783 ret
= increase_packet_size(pos
);
1794 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1795 struct ctf_stream_pos
*pos
)
1797 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1798 field
, struct bt_ctf_field_enumeration
, parent
);
1800 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1804 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1805 struct ctf_stream_pos
*pos
)
1808 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1809 struct bt_ctf_field_floating_point
, parent
);
1812 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1813 if (ret
== -EFAULT
) {
1815 * The field is too large to fit in the current packet's
1816 * remaining space. Bump the packet size and retry.
1818 ret
= increase_packet_size(pos
);
1829 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1830 struct ctf_stream_pos
*pos
)
1834 struct bt_ctf_field_structure
*structure
= container_of(
1835 field
, struct bt_ctf_field_structure
, parent
);
1837 while (!ctf_pos_access_ok(pos
,
1838 offset_align(pos
->offset
,
1839 field
->type
->declaration
->alignment
))) {
1840 ret
= increase_packet_size(pos
);
1846 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1851 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1852 struct bt_ctf_field
*field
= g_ptr_array_index(
1853 structure
->fields
, i
);
1855 ret
= bt_ctf_field_serialize(field
, pos
);
1865 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1866 struct ctf_stream_pos
*pos
)
1868 struct bt_ctf_field_variant
*variant
= container_of(
1869 field
, struct bt_ctf_field_variant
, parent
);
1871 return bt_ctf_field_serialize(variant
->payload
, pos
);
1875 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1876 struct ctf_stream_pos
*pos
)
1880 struct bt_ctf_field_array
*array
= container_of(
1881 field
, struct bt_ctf_field_array
, parent
);
1883 for (i
= 0; i
< array
->elements
->len
; i
++) {
1884 ret
= bt_ctf_field_serialize(
1885 g_ptr_array_index(array
->elements
, i
), pos
);
1895 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1896 struct ctf_stream_pos
*pos
)
1900 struct bt_ctf_field_sequence
*sequence
= container_of(
1901 field
, struct bt_ctf_field_sequence
, parent
);
1903 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1904 ret
= bt_ctf_field_serialize(
1905 g_ptr_array_index(sequence
->elements
, i
), pos
);
1915 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1916 struct ctf_stream_pos
*pos
)
1920 struct bt_ctf_field_string
*string
= container_of(field
,
1921 struct bt_ctf_field_string
, parent
);
1922 struct bt_ctf_field_type
*character_type
=
1923 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1924 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1926 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1927 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1928 (uint64_t) string
->payload
->str
[i
]);
1933 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1940 bt_put(character_type
);
1945 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1946 struct bt_ctf_field
*dst
)
1948 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1950 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1951 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1953 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1954 sizeof(struct definition_integer
));
1959 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1960 struct bt_ctf_field
*dst
)
1963 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1965 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1966 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1968 if (enum_src
->payload
) {
1969 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1970 if (!enum_dst
->payload
) {
1980 int bt_ctf_field_floating_point_copy(
1981 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1983 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1985 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1987 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1990 memcpy(&float_dst
->definition
, &float_src
->definition
,
1991 sizeof(struct definition_float
));
1992 memcpy(&float_dst
->sign
, &float_src
->sign
,
1993 sizeof(struct definition_integer
));
1994 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1995 sizeof(struct definition_integer
));
1996 memcpy(&float_dst
->exp
, &float_src
->exp
,
1997 sizeof(struct definition_integer
));
2002 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2003 struct bt_ctf_field
*dst
)
2006 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2008 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2009 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2011 /* This field_name_to_index HT is owned by the structure field type */
2012 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2013 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2015 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2016 struct bt_ctf_field
*field
=
2017 g_ptr_array_index(struct_src
->fields
, i
);
2018 struct bt_ctf_field
*field_copy
= NULL
;
2021 field_copy
= bt_ctf_field_copy(field
);
2029 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2036 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2037 struct bt_ctf_field
*dst
)
2040 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2042 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2043 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2045 if (variant_src
->tag
) {
2046 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2047 if (!variant_dst
->tag
) {
2052 if (variant_src
->payload
) {
2053 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2054 if (!variant_dst
->payload
) {
2064 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2065 struct bt_ctf_field
*dst
)
2068 struct bt_ctf_field_array
*array_src
, *array_dst
;
2070 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2071 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2073 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2074 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2075 struct bt_ctf_field
*field
=
2076 g_ptr_array_index(array_src
->elements
, i
);
2077 struct bt_ctf_field
*field_copy
= NULL
;
2080 field_copy
= bt_ctf_field_copy(field
);
2088 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2095 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2096 struct bt_ctf_field
*dst
)
2099 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2100 struct bt_ctf_field
*src_length
;
2101 struct bt_ctf_field
*dst_length
;
2103 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2104 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2106 src_length
= bt_ctf_field_sequence_get_length(src
);
2109 /* no length set yet: keep destination sequence empty */
2113 /* copy source length */
2114 dst_length
= bt_ctf_field_copy(src_length
);
2122 /* this will initialize the destination sequence's internal array */
2123 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2130 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2132 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2133 struct bt_ctf_field
*field
=
2134 g_ptr_array_index(sequence_src
->elements
, i
);
2135 struct bt_ctf_field
*field_copy
= NULL
;
2138 field_copy
= bt_ctf_field_copy(field
);
2146 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2153 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2154 struct bt_ctf_field
*dst
)
2157 struct bt_ctf_field_string
*string_src
, *string_dst
;
2159 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2160 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2162 if (string_src
->payload
) {
2163 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2164 if (!string_dst
->payload
) {
2174 int increase_packet_size(struct ctf_stream_pos
*pos
)
2179 ret
= munmap_align(pos
->base_mma
);
2184 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2186 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2187 pos
->packet_size
/ CHAR_BIT
);
2188 } while (ret
== EINTR
);
2195 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2196 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2197 if (pos
->base_mma
== MAP_FAILED
) {