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/ctf-writer/serialize-internal.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/ref.h>
34 #include <babeltrace/compiler.h>
35 #include <babeltrace/compat/fcntl.h>
36 #include <babeltrace/align.h>
38 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
41 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
43 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
44 struct bt_ctf_field_type
*);
46 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
47 struct bt_ctf_field_type
*);
49 struct bt_ctf_field
*bt_ctf_field_structure_create(
50 struct bt_ctf_field_type
*);
52 struct bt_ctf_field
*bt_ctf_field_variant_create(
53 struct bt_ctf_field_type
*);
55 struct bt_ctf_field
*bt_ctf_field_array_create(
56 struct bt_ctf_field_type
*);
58 struct bt_ctf_field
*bt_ctf_field_sequence_create(
59 struct bt_ctf_field_type
*);
61 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
64 void bt_ctf_field_destroy(struct bt_object
*);
66 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
68 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
70 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
72 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
74 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
76 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
78 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
80 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
83 int bt_ctf_field_generic_validate(struct bt_ctf_field
*);
85 int bt_ctf_field_structure_validate(struct bt_ctf_field
*);
87 int bt_ctf_field_variant_validate(struct bt_ctf_field
*);
89 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*);
91 int bt_ctf_field_array_validate(struct bt_ctf_field
*);
93 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*);
96 int bt_ctf_field_generic_reset(struct bt_ctf_field
*);
98 int bt_ctf_field_structure_reset(struct bt_ctf_field
*);
100 int bt_ctf_field_variant_reset(struct bt_ctf_field
*);
102 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*);
104 int bt_ctf_field_array_reset(struct bt_ctf_field
*);
106 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*);
108 int bt_ctf_field_string_reset(struct bt_ctf_field
*);
111 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
112 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
114 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
115 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
117 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
118 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
120 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
121 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
123 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
124 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
126 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
127 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
129 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
130 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
132 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
133 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
);
136 int bt_ctf_field_integer_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
138 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
140 int bt_ctf_field_floating_point_copy(struct bt_ctf_field
*,
141 struct bt_ctf_field
*);
143 int bt_ctf_field_structure_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
145 int bt_ctf_field_variant_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
147 int bt_ctf_field_array_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
149 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
151 int bt_ctf_field_string_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
154 void generic_field_freeze(struct bt_ctf_field
*);
156 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*);
158 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*);
160 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*);
162 void bt_ctf_field_array_freeze(struct bt_ctf_field
*);
164 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*);
167 bool bt_ctf_field_generic_is_set(struct bt_ctf_field
*);
169 bool bt_ctf_field_structure_is_set(struct bt_ctf_field
*);
171 bool bt_ctf_field_variant_is_set(struct bt_ctf_field
*);
173 bool bt_ctf_field_enumeration_is_set(struct bt_ctf_field
*);
175 bool bt_ctf_field_array_is_set(struct bt_ctf_field
*);
177 bool bt_ctf_field_sequence_is_set(struct bt_ctf_field
*);
180 int increase_packet_size(struct bt_ctf_stream_pos
*pos
);
183 struct bt_ctf_field
*(* const field_create_funcs
[])(
184 struct bt_ctf_field_type
*) = {
185 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_create
,
186 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_create
,
187 [BT_CTF_TYPE_ID_FLOAT
] =
188 bt_ctf_field_floating_point_create
,
189 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_create
,
190 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_create
,
191 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_create
,
192 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_create
,
193 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_create
,
197 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
198 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_destroy
,
199 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_destroy
,
200 [BT_CTF_TYPE_ID_FLOAT
] =
201 bt_ctf_field_floating_point_destroy
,
202 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_destroy
,
203 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_destroy
,
204 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_destroy
,
205 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
206 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_destroy
,
210 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
211 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_validate
,
212 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_validate
,
213 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_validate
,
214 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_validate
,
215 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_validate
,
216 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_validate
,
217 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_validate
,
218 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_generic_validate
,
222 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
223 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_reset
,
224 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_reset
,
225 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_reset
,
226 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_reset
,
227 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_reset
,
228 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_reset
,
229 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_reset
,
230 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_reset
,
234 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
235 struct bt_ctf_stream_pos
*, enum bt_ctf_byte_order
) = {
236 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_serialize
,
237 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_serialize
,
238 [BT_CTF_TYPE_ID_FLOAT
] =
239 bt_ctf_field_floating_point_serialize
,
240 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_serialize
,
241 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_serialize
,
242 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_serialize
,
243 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
244 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_serialize
,
248 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
249 struct bt_ctf_field
*) = {
250 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_integer_copy
,
251 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_copy
,
252 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_floating_point_copy
,
253 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_copy
,
254 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_copy
,
255 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_copy
,
256 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_copy
,
257 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_string_copy
,
261 void (* const field_freeze_funcs
[])(struct bt_ctf_field
*) = {
262 [BT_CTF_TYPE_ID_INTEGER
] = generic_field_freeze
,
263 [BT_CTF_TYPE_ID_FLOAT
] = generic_field_freeze
,
264 [BT_CTF_TYPE_ID_STRING
] = generic_field_freeze
,
265 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_freeze
,
266 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_freeze
,
267 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_freeze
,
268 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_freeze
,
269 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_freeze
,
273 bool (* const field_is_set_funcs
[])(struct bt_ctf_field
*) = {
274 [BT_CTF_TYPE_ID_INTEGER
] = bt_ctf_field_generic_is_set
,
275 [BT_CTF_TYPE_ID_ENUM
] = bt_ctf_field_enumeration_is_set
,
276 [BT_CTF_TYPE_ID_FLOAT
] = bt_ctf_field_generic_is_set
,
277 [BT_CTF_TYPE_ID_STRUCT
] = bt_ctf_field_structure_is_set
,
278 [BT_CTF_TYPE_ID_VARIANT
] = bt_ctf_field_variant_is_set
,
279 [BT_CTF_TYPE_ID_ARRAY
] = bt_ctf_field_array_is_set
,
280 [BT_CTF_TYPE_ID_SEQUENCE
] = bt_ctf_field_sequence_is_set
,
281 [BT_CTF_TYPE_ID_STRING
] = bt_ctf_field_generic_is_set
,
284 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
286 struct bt_ctf_field
*field
= NULL
;
287 enum bt_ctf_type_id type_id
;
294 type_id
= bt_ctf_field_type_get_type_id(type
);
295 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
296 type_id
>= BT_CTF_NR_TYPE_IDS
) {
300 /* Field class MUST be valid */
301 ret
= bt_ctf_field_type_validate(type
);
308 field
= field_create_funcs
[type_id
](type
);
313 /* The type's declaration can't change after this point */
314 bt_ctf_field_type_freeze(type
);
316 bt_object_init(field
, bt_ctf_field_destroy
);
322 void bt_ctf_field_get(struct bt_ctf_field
*field
)
327 void bt_ctf_field_put(struct bt_ctf_field
*field
)
332 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
334 struct bt_ctf_field_type
*ret
= NULL
;
346 enum bt_ctf_type_id
bt_ctf_field_get_type_id(struct bt_ctf_field
*field
)
348 enum bt_ctf_type_id ret
= BT_CTF_TYPE_ID_UNKNOWN
;
354 ret
= bt_ctf_field_type_get_type_id(field
->type
);
359 int bt_ctf_field_is_integer(struct bt_ctf_field
*field
)
361 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_INTEGER
;
364 int bt_ctf_field_is_floating_point(struct bt_ctf_field
*field
)
366 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_FLOAT
;
369 int bt_ctf_field_is_enumeration(struct bt_ctf_field
*field
)
371 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_ENUM
;
374 int bt_ctf_field_is_string(struct bt_ctf_field
*field
)
376 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_STRING
;
379 int bt_ctf_field_is_structure(struct bt_ctf_field
*field
)
381 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_STRUCT
;
384 int bt_ctf_field_is_array(struct bt_ctf_field
*field
)
386 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_ARRAY
;
389 int bt_ctf_field_is_sequence(struct bt_ctf_field
*field
)
391 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_SEQUENCE
;
394 int bt_ctf_field_is_variant(struct bt_ctf_field
*field
)
396 return bt_ctf_field_get_type_id(field
) == BT_CTF_TYPE_ID_VARIANT
;
399 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
400 struct bt_ctf_field
*field
)
402 struct bt_ctf_field
*ret
= NULL
;
403 struct bt_ctf_field_sequence
*sequence
;
409 if (bt_ctf_field_type_get_type_id(field
->type
) !=
410 BT_CTF_TYPE_ID_SEQUENCE
) {
414 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
415 ret
= sequence
->length
;
421 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
422 struct bt_ctf_field
*length_field
)
425 struct bt_ctf_field_type_integer
*length_type
;
426 struct bt_ctf_field_integer
*length
;
427 struct bt_ctf_field_sequence
*sequence
;
428 uint64_t sequence_length
;
430 if (!field
|| !length_field
|| field
->frozen
) {
434 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
435 BT_CTF_TYPE_ID_INTEGER
) {
440 length_type
= container_of(length_field
->type
,
441 struct bt_ctf_field_type_integer
, parent
);
442 /* The length field must be unsigned */
443 if (length_type
->is_signed
) {
448 length
= container_of(length_field
, struct bt_ctf_field_integer
,
450 sequence_length
= length
->payload
.unsignd
;
451 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
452 if (sequence
->elements
) {
453 g_ptr_array_free(sequence
->elements
, TRUE
);
454 bt_put(sequence
->length
);
457 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
458 if (!sequence
->elements
) {
463 g_ptr_array_set_free_func(sequence
->elements
,
464 (GDestroyNotify
) bt_put
);
465 g_ptr_array_set_size(sequence
->elements
, (size_t) sequence_length
);
466 bt_get(length_field
);
467 sequence
->length
= length_field
;
472 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
473 struct bt_ctf_field
*field
, const char *name
)
475 struct bt_ctf_field
*new_field
= NULL
;
477 struct bt_ctf_field_structure
*structure
;
478 struct bt_ctf_field_type
*field_type
= NULL
;
481 if (!field
|| !name
||
482 bt_ctf_field_type_get_type_id(field
->type
) !=
483 BT_CTF_TYPE_ID_STRUCT
) {
487 field_quark
= g_quark_from_string(name
);
488 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
490 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
492 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
493 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
497 if (structure
->fields
->pdata
[index
]) {
498 new_field
= structure
->fields
->pdata
[index
];
502 /* We don't want to modify this field if it's frozen */
507 new_field
= bt_ctf_field_create(field_type
);
512 structure
->fields
->pdata
[index
] = new_field
;
522 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
523 struct bt_ctf_field
*field
, int index
)
526 const char *field_name
;
527 struct bt_ctf_field_structure
*structure
;
528 struct bt_ctf_field_type
*structure_type
;
529 struct bt_ctf_field_type
*field_type
= NULL
;
530 struct bt_ctf_field
*ret_field
= NULL
;
533 bt_ctf_field_type_get_type_id(field
->type
) !=
534 BT_CTF_TYPE_ID_STRUCT
) {
538 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
539 if (index
>= structure
->fields
->len
) {
543 ret_field
= structure
->fields
->pdata
[index
];
548 /* We don't want to modify this field if it's frozen */
553 /* Field has not been instanciated yet, create it */
554 structure_type
= bt_ctf_field_get_type(field
);
555 if (!structure_type
) {
559 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
560 &field_name
, &field_type
, index
);
561 bt_put(structure_type
);
566 ret_field
= bt_ctf_field_create(field_type
);
571 structure
->fields
->pdata
[index
] = ret_field
;
580 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
581 const char *name
, struct bt_ctf_field
*value
)
585 struct bt_ctf_field_structure
*structure
;
586 struct bt_ctf_field_type
*expected_field_type
= NULL
;
589 if (!field
|| !name
|| !value
|| field
->frozen
||
590 bt_ctf_field_type_get_type_id(field
->type
) !=
591 BT_CTF_TYPE_ID_STRUCT
) {
596 field_quark
= g_quark_from_string(name
);
597 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
598 expected_field_type
=
599 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
602 if (bt_ctf_field_type_compare(expected_field_type
, value
->type
)) {
607 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
608 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
612 if (structure
->fields
->pdata
[index
]) {
613 bt_put(structure
->fields
->pdata
[index
]);
616 structure
->fields
->pdata
[index
] = value
;
619 if (expected_field_type
) {
620 bt_put(expected_field_type
);
625 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
628 struct bt_ctf_field
*new_field
= NULL
;
629 struct bt_ctf_field_type
*field_type
= NULL
;
630 struct bt_ctf_field_array
*array
;
632 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
633 BT_CTF_TYPE_ID_ARRAY
) {
637 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
638 if (index
>= array
->elements
->len
) {
642 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
643 if (array
->elements
->pdata
[(size_t)index
]) {
644 new_field
= array
->elements
->pdata
[(size_t)index
];
648 /* We don't want to modify this field if it's frozen */
653 new_field
= bt_ctf_field_create(field_type
);
654 array
->elements
->pdata
[(size_t)index
] = new_field
;
665 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
668 struct bt_ctf_field
*new_field
= NULL
;
669 struct bt_ctf_field_type
*field_type
= NULL
;
670 struct bt_ctf_field_sequence
*sequence
;
672 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
673 BT_CTF_TYPE_ID_SEQUENCE
) {
677 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
678 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
682 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
683 if (sequence
->elements
->pdata
[(size_t) index
]) {
684 new_field
= sequence
->elements
->pdata
[(size_t) index
];
688 /* We don't want to modify this field if it's frozen */
693 new_field
= bt_ctf_field_create(field_type
);
694 sequence
->elements
->pdata
[(size_t) index
] = new_field
;
705 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
706 struct bt_ctf_field
*tag_field
)
708 struct bt_ctf_field
*new_field
= NULL
;
709 struct bt_ctf_field_variant
*variant
;
710 struct bt_ctf_field_type_variant
*variant_type
;
711 struct bt_ctf_field_type
*field_type
;
712 struct bt_ctf_field
*tag_enum
= NULL
;
713 struct bt_ctf_field_integer
*tag_enum_integer
;
714 int64_t tag_enum_value
;
716 if (!field
|| !tag_field
||
717 bt_ctf_field_type_get_type_id(field
->type
) !=
718 BT_CTF_TYPE_ID_VARIANT
||
719 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
720 BT_CTF_TYPE_ID_ENUM
) {
724 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
725 variant_type
= container_of(field
->type
,
726 struct bt_ctf_field_type_variant
, parent
);
727 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
732 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
735 if (bt_ctf_field_validate(tag_field
) < 0) {
739 tag_enum_value
= tag_enum_integer
->payload
.signd
;
742 * If the variant currently has a tag and a payload, and if the
743 * requested tag value is the same as the current one, return
744 * the current payload instead of creating a fresh one.
746 if (variant
->tag
&& variant
->payload
) {
747 struct bt_ctf_field
*cur_tag_container
= NULL
;
748 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
749 int64_t cur_tag_value
;
752 bt_ctf_field_enumeration_get_container(variant
->tag
);
753 assert(cur_tag_container
);
754 cur_tag_enum_integer
= container_of(cur_tag_container
,
755 struct bt_ctf_field_integer
, parent
);
756 bt_put(cur_tag_container
);
757 cur_tag_value
= cur_tag_enum_integer
->payload
.signd
;
759 if (cur_tag_value
== tag_enum_value
) {
760 new_field
= variant
->payload
;
766 /* We don't want to modify this field if it's frozen */
771 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
772 variant_type
, tag_enum_value
);
777 new_field
= bt_ctf_field_create(field_type
);
782 bt_put(variant
->tag
);
783 bt_put(variant
->payload
);
786 variant
->tag
= tag_field
;
787 variant
->payload
= new_field
;
793 struct bt_ctf_field
*bt_ctf_field_variant_get_current_field(
794 struct bt_ctf_field
*variant_field
)
796 struct bt_ctf_field
*current_field
= NULL
;
797 struct bt_ctf_field_variant
*variant
;
799 if (!variant_field
||
800 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
801 BT_CTF_TYPE_ID_VARIANT
) {
805 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
808 if (variant
->payload
) {
809 current_field
= variant
->payload
;
810 bt_get(current_field
);
815 return current_field
;
818 struct bt_ctf_field
*bt_ctf_field_variant_get_tag(
819 struct bt_ctf_field
*variant_field
)
821 struct bt_ctf_field
*tag
= NULL
;
822 struct bt_ctf_field_variant
*variant
;
824 if (!variant_field
||
825 bt_ctf_field_type_get_type_id(variant_field
->type
) !=
826 BT_CTF_TYPE_ID_VARIANT
) {
830 variant
= container_of(variant_field
, struct bt_ctf_field_variant
,
833 tag
= bt_get(variant
->tag
);
839 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
840 struct bt_ctf_field
*field
)
842 struct bt_ctf_field
*container
= NULL
;
843 struct bt_ctf_field_enumeration
*enumeration
;
845 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
846 BT_CTF_TYPE_ID_ENUM
) {
850 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
852 if (!enumeration
->payload
) {
853 /* We don't want to modify this field if it's frozen */
858 struct bt_ctf_field_type_enumeration
*enumeration_type
=
859 container_of(field
->type
,
860 struct bt_ctf_field_type_enumeration
, parent
);
861 enumeration
->payload
=
862 bt_ctf_field_create(enumeration_type
->container
);
865 container
= enumeration
->payload
;
871 struct bt_ctf_field_type_enumeration_mapping_iterator
*
872 bt_ctf_field_enumeration_get_mappings(struct bt_ctf_field
*field
)
875 struct bt_ctf_field
*container
= NULL
;
876 struct bt_ctf_field_type
*container_type
= NULL
;
877 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
878 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
880 container
= bt_ctf_field_enumeration_get_container(field
);
885 container_type
= bt_ctf_field_get_type(container
);
886 if (!container_type
) {
887 goto error_put_container
;
890 integer_type
= container_of(container_type
,
891 struct bt_ctf_field_type_integer
, parent
);
893 if (!integer_type
->is_signed
) {
896 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
899 goto error_put_container_type
;
901 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
906 ret
= bt_ctf_field_signed_integer_get_value(container
,
909 goto error_put_container_type
;
911 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
915 error_put_container_type
:
916 bt_put(container_type
);
923 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
927 struct bt_ctf_field_integer
*integer
;
928 struct bt_ctf_field_type_integer
*integer_type
;
930 if (!field
|| !value
|| !field
->payload_set
||
931 bt_ctf_field_type_get_type_id(field
->type
) !=
932 BT_CTF_TYPE_ID_INTEGER
) {
937 integer_type
= container_of(field
->type
,
938 struct bt_ctf_field_type_integer
, parent
);
939 if (!integer_type
->is_signed
) {
944 integer
= container_of(field
,
945 struct bt_ctf_field_integer
, parent
);
946 *value
= integer
->payload
.signd
;
951 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
955 struct bt_ctf_field_integer
*integer
;
956 struct bt_ctf_field_type_integer
*integer_type
;
958 int64_t min_value
, max_value
;
960 if (!field
|| field
->frozen
||
961 bt_ctf_field_type_get_type_id(field
->type
) !=
962 BT_CTF_TYPE_ID_INTEGER
) {
967 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
968 integer_type
= container_of(field
->type
,
969 struct bt_ctf_field_type_integer
, parent
);
970 if (!integer_type
->is_signed
) {
975 size
= integer_type
->size
;
976 min_value
= -(1ULL << (size
- 1));
977 max_value
= (1ULL << (size
- 1)) - 1;
978 if (value
< min_value
|| value
> max_value
) {
983 integer
->payload
.signd
= value
;
984 integer
->parent
.payload_set
= 1;
989 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
993 struct bt_ctf_field_integer
*integer
;
994 struct bt_ctf_field_type_integer
*integer_type
;
996 if (!field
|| !value
|| !field
->payload_set
||
997 bt_ctf_field_type_get_type_id(field
->type
) !=
998 BT_CTF_TYPE_ID_INTEGER
) {
1003 integer_type
= container_of(field
->type
,
1004 struct bt_ctf_field_type_integer
, parent
);
1005 if (integer_type
->is_signed
) {
1010 integer
= container_of(field
,
1011 struct bt_ctf_field_integer
, parent
);
1012 *value
= integer
->payload
.unsignd
;
1017 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
1021 struct bt_ctf_field_integer
*integer
;
1022 struct bt_ctf_field_type_integer
*integer_type
;
1026 if (!field
|| field
->frozen
||
1027 bt_ctf_field_type_get_type_id(field
->type
) !=
1028 BT_CTF_TYPE_ID_INTEGER
) {
1033 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1034 integer_type
= container_of(field
->type
,
1035 struct bt_ctf_field_type_integer
, parent
);
1036 if (integer_type
->is_signed
) {
1041 size
= integer_type
->size
;
1042 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
1043 if (value
> max_value
) {
1048 integer
->payload
.unsignd
= value
;
1049 integer
->parent
.payload_set
= 1;
1054 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
1058 struct bt_ctf_field_floating_point
*floating_point
;
1060 if (!field
|| !value
|| !field
->payload_set
||
1061 bt_ctf_field_type_get_type_id(field
->type
) !=
1062 BT_CTF_TYPE_ID_FLOAT
) {
1067 floating_point
= container_of(field
,
1068 struct bt_ctf_field_floating_point
, parent
);
1069 *value
= floating_point
->payload
;
1074 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
1078 struct bt_ctf_field_floating_point
*floating_point
;
1080 if (!field
|| field
->frozen
||
1081 bt_ctf_field_type_get_type_id(field
->type
) !=
1082 BT_CTF_TYPE_ID_FLOAT
) {
1086 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1088 floating_point
->payload
= value
;
1089 floating_point
->parent
.payload_set
= 1;
1094 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
1096 const char *ret
= NULL
;
1097 struct bt_ctf_field_string
*string
;
1099 if (!field
|| !field
->payload_set
||
1100 bt_ctf_field_type_get_type_id(field
->type
) !=
1101 BT_CTF_TYPE_ID_STRING
) {
1105 string
= container_of(field
,
1106 struct bt_ctf_field_string
, parent
);
1107 ret
= string
->payload
->str
;
1112 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
1116 struct bt_ctf_field_string
*string
;
1118 if (!field
|| !value
|| field
->frozen
||
1119 bt_ctf_field_type_get_type_id(field
->type
) !=
1120 BT_CTF_TYPE_ID_STRING
) {
1125 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1126 if (string
->payload
) {
1127 g_string_assign(string
->payload
, value
);
1129 string
->payload
= g_string_new(value
);
1132 string
->parent
.payload_set
= 1;
1137 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
1141 struct bt_ctf_field_string
*string_field
;
1143 if (!field
|| !value
|| field
->frozen
||
1144 bt_ctf_field_type_get_type_id(field
->type
) !=
1145 BT_CTF_TYPE_ID_STRING
) {
1150 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1152 if (string_field
->payload
) {
1153 g_string_append(string_field
->payload
, value
);
1155 string_field
->payload
= g_string_new(value
);
1158 string_field
->parent
.payload_set
= 1;
1164 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
1165 const char *value
, unsigned int length
)
1169 unsigned int effective_length
= length
;
1170 struct bt_ctf_field_string
*string_field
;
1172 if (!field
|| !value
|| field
->frozen
||
1173 bt_ctf_field_type_get_type_id(field
->type
) !=
1174 BT_CTF_TYPE_ID_STRING
) {
1179 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
1181 /* make sure no null bytes are appended */
1182 for (i
= 0; i
< length
; ++i
) {
1183 if (value
[i
] == '\0') {
1184 effective_length
= i
;
1189 if (string_field
->payload
) {
1190 g_string_append_len(string_field
->payload
, value
,
1193 string_field
->payload
= g_string_new_len(value
,
1197 string_field
->parent
.payload_set
= 1;
1204 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1207 enum bt_ctf_type_id type_id
;
1214 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1215 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1220 ret
= field_validate_funcs
[type_id
](field
);
1226 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1229 enum bt_ctf_type_id type_id
;
1236 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1237 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1242 ret
= field_reset_funcs
[type_id
](field
);
1248 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1249 struct bt_ctf_stream_pos
*pos
,
1250 enum bt_ctf_byte_order native_byte_order
)
1253 enum bt_ctf_type_id type_id
;
1255 if (!field
|| !pos
) {
1260 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1261 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1266 ret
= field_serialize_funcs
[type_id
](field
, pos
, native_byte_order
);
1273 bool bt_ctf_field_is_set(struct bt_ctf_field
*field
)
1275 bool is_set
= false;
1276 enum bt_ctf_type_id type_id
;
1282 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1283 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1287 is_set
= field_is_set_funcs
[type_id
](field
);
1292 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1295 struct bt_ctf_field
*copy
= NULL
;
1296 enum bt_ctf_type_id type_id
;
1302 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1303 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
|| type_id
>= BT_CTF_NR_TYPE_IDS
) {
1307 copy
= bt_ctf_field_create(field
->type
);
1312 copy
->payload_set
= field
->payload_set
;
1313 ret
= field_copy_funcs
[type_id
](field
, copy
);
1323 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1325 struct bt_ctf_field_integer
*integer
= g_new0(
1326 struct bt_ctf_field_integer
, 1);
1328 return integer
? &integer
->parent
: NULL
;
1332 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1333 struct bt_ctf_field_type
*type
)
1335 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1336 struct bt_ctf_field_enumeration
, 1);
1338 return enumeration
? &enumeration
->parent
: NULL
;
1342 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1343 struct bt_ctf_field_type
*type
)
1345 struct bt_ctf_field_floating_point
*floating_point
;
1347 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1348 return floating_point
? &floating_point
->parent
: NULL
;
1352 struct bt_ctf_field
*bt_ctf_field_structure_create(
1353 struct bt_ctf_field_type
*type
)
1355 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1356 struct bt_ctf_field_type_structure
, parent
);
1357 struct bt_ctf_field_structure
*structure
= g_new0(
1358 struct bt_ctf_field_structure
, 1);
1359 struct bt_ctf_field
*field
= NULL
;
1365 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1366 structure
->fields
= g_ptr_array_new_with_free_func(
1367 (GDestroyNotify
)bt_ctf_field_put
);
1368 g_ptr_array_set_size(structure
->fields
,
1369 g_hash_table_size(structure
->field_name_to_index
));
1370 field
= &structure
->parent
;
1376 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1378 struct bt_ctf_field_variant
*variant
= g_new0(
1379 struct bt_ctf_field_variant
, 1);
1380 return variant
? &variant
->parent
: NULL
;
1384 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1386 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1387 struct bt_ctf_field_type_array
*array_type
;
1388 unsigned int array_length
;
1390 if (!array
|| !type
) {
1394 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1395 array_length
= array_type
->length
;
1396 array
->elements
= g_ptr_array_sized_new(array_length
);
1397 if (!array
->elements
) {
1401 g_ptr_array_set_free_func(array
->elements
,
1402 (GDestroyNotify
)bt_ctf_field_put
);
1403 g_ptr_array_set_size(array
->elements
, array_length
);
1404 return &array
->parent
;
1411 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1412 struct bt_ctf_field_type
*type
)
1414 struct bt_ctf_field_sequence
*sequence
= g_new0(
1415 struct bt_ctf_field_sequence
, 1);
1416 return sequence
? &sequence
->parent
: NULL
;
1420 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1422 struct bt_ctf_field_string
*string
= g_new0(
1423 struct bt_ctf_field_string
, 1);
1424 return string
? &string
->parent
: NULL
;
1428 void bt_ctf_field_destroy(struct bt_object
*obj
)
1430 struct bt_ctf_field
*field
;
1431 struct bt_ctf_field_type
*type
;
1432 enum bt_ctf_type_id type_id
;
1434 field
= container_of(obj
, struct bt_ctf_field
, base
);
1436 type_id
= bt_ctf_field_type_get_type_id(type
);
1437 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
1438 type_id
>= BT_CTF_NR_TYPE_IDS
) {
1442 field_destroy_funcs
[type_id
](field
);
1447 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1449 struct bt_ctf_field_integer
*integer
;
1455 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1460 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1462 struct bt_ctf_field_enumeration
*enumeration
;
1468 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1470 bt_put(enumeration
->payload
);
1471 g_free(enumeration
);
1475 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1477 struct bt_ctf_field_floating_point
*floating_point
;
1483 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1485 g_free(floating_point
);
1489 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1491 struct bt_ctf_field_structure
*structure
;
1497 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1498 g_ptr_array_free(structure
->fields
, TRUE
);
1503 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1505 struct bt_ctf_field_variant
*variant
;
1511 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1512 bt_put(variant
->tag
);
1513 bt_put(variant
->payload
);
1518 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1520 struct bt_ctf_field_array
*array
;
1526 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1527 g_ptr_array_free(array
->elements
, TRUE
);
1532 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1534 struct bt_ctf_field_sequence
*sequence
;
1540 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1541 if (sequence
->elements
) {
1542 g_ptr_array_free(sequence
->elements
, TRUE
);
1544 bt_put(sequence
->length
);
1549 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1551 struct bt_ctf_field_string
*string
;
1556 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1557 if (string
->payload
) {
1558 g_string_free(string
->payload
, TRUE
);
1564 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1566 return (field
&& field
->payload_set
) ? 0 : -1;
1570 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1573 struct bt_ctf_field_enumeration
*enumeration
;
1580 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1582 if (!enumeration
->payload
) {
1587 ret
= bt_ctf_field_validate(enumeration
->payload
);
1593 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1597 struct bt_ctf_field_structure
*structure
;
1604 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1605 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1606 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1609 struct bt_ctf_field_type
*field_type
=
1610 bt_ctf_field_get_type(field
);
1612 (void) bt_ctf_field_type_structure_get_field(field_type
,
1614 fprintf(stderr
, "Field %s failed validation\n",
1615 name
? name
: "NULL");
1625 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1628 struct bt_ctf_field_variant
*variant
;
1635 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1636 ret
= bt_ctf_field_validate(variant
->payload
);
1642 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1646 struct bt_ctf_field_array
*array
;
1653 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1654 for (i
= 0; i
< array
->elements
->len
; i
++) {
1655 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1657 fprintf(stderr
, "Failed to validate array field #%zu\n", i
);
1666 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1670 struct bt_ctf_field_sequence
*sequence
;
1677 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1678 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1679 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1681 fprintf(stderr
, "Failed to validate sequence field #%zu\n", i
);
1690 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1699 field
->payload_set
= 0;
1705 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1708 struct bt_ctf_field_enumeration
*enumeration
;
1715 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1717 if (!enumeration
->payload
) {
1721 ret
= bt_ctf_field_reset(enumeration
->payload
);
1727 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1731 struct bt_ctf_field_structure
*structure
;
1738 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1739 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1740 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1744 * Structure members are lazily initialized; skip if
1745 * this member has not been allocated yet.
1750 ret
= bt_ctf_field_reset(member
);
1760 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1763 struct bt_ctf_field_variant
*variant
;
1770 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1771 if (variant
->payload
) {
1772 ret
= bt_ctf_field_reset(variant
->payload
);
1779 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1783 struct bt_ctf_field_array
*array
;
1790 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1791 for (i
= 0; i
< array
->elements
->len
; i
++) {
1792 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1796 * Array elements are lazily initialized; skip if
1797 * this member has not been allocated yet.
1802 ret
= bt_ctf_field_reset(member
);
1812 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1816 struct bt_ctf_field_sequence
*sequence
;
1823 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1824 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1825 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1829 * Sequence elements are lazily initialized; skip if
1830 * this member has not been allocated yet.
1835 ret
= bt_ctf_field_reset(member
);
1845 int bt_ctf_field_string_reset(struct bt_ctf_field
*field
)
1848 struct bt_ctf_field_string
*string
;
1855 ret
= bt_ctf_field_generic_reset(field
);
1860 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1861 if (string
->payload
) {
1862 g_string_truncate(string
->payload
, 0);
1869 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1870 struct bt_ctf_stream_pos
*pos
,
1871 enum bt_ctf_byte_order native_byte_order
)
1874 struct bt_ctf_field_integer
*integer
= container_of(field
,
1875 struct bt_ctf_field_integer
, parent
);
1877 if (!bt_ctf_field_generic_is_set(field
)) {
1882 ret
= bt_ctf_field_integer_write(integer
, pos
, native_byte_order
);
1883 if (ret
== -EFAULT
) {
1885 * The field is too large to fit in the current packet's
1886 * remaining space. Bump the packet size and retry.
1888 ret
= increase_packet_size(pos
);
1899 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1900 struct bt_ctf_stream_pos
*pos
,
1901 enum bt_ctf_byte_order native_byte_order
)
1903 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1904 field
, struct bt_ctf_field_enumeration
, parent
);
1906 return bt_ctf_field_serialize(enumeration
->payload
, pos
,
1911 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1912 struct bt_ctf_stream_pos
*pos
,
1913 enum bt_ctf_byte_order native_byte_order
)
1916 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1917 struct bt_ctf_field_floating_point
, parent
);
1919 if (!bt_ctf_field_generic_is_set(field
)) {
1924 ret
= bt_ctf_field_floating_point_write(floating_point
, pos
,
1926 if (ret
== -EFAULT
) {
1928 * The field is too large to fit in the current packet's
1929 * remaining space. Bump the packet size and retry.
1931 ret
= increase_packet_size(pos
);
1942 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1943 struct bt_ctf_stream_pos
*pos
,
1944 enum bt_ctf_byte_order native_byte_order
)
1948 struct bt_ctf_field_structure
*structure
= container_of(
1949 field
, struct bt_ctf_field_structure
, parent
);
1951 while (!bt_ctf_stream_pos_access_ok(pos
,
1952 offset_align(pos
->offset
, field
->type
->alignment
))) {
1953 ret
= increase_packet_size(pos
);
1959 if (!bt_ctf_stream_pos_align(pos
, field
->type
->alignment
)) {
1964 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1965 struct bt_ctf_field
*member
= g_ptr_array_index(
1966 structure
->fields
, i
);
1968 ret
= bt_ctf_field_serialize(member
, pos
, native_byte_order
);
1971 struct bt_ctf_field_type
*structure_type
=
1972 bt_ctf_field_get_type(field
);
1974 (void) bt_ctf_field_type_structure_get_field(
1975 structure_type
, &name
, NULL
, i
);
1976 fprintf(stderr
, "Field %s failed to serialize\n",
1977 name
? name
: "NULL");
1978 bt_put(structure_type
);
1987 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1988 struct bt_ctf_stream_pos
*pos
,
1989 enum bt_ctf_byte_order native_byte_order
)
1991 struct bt_ctf_field_variant
*variant
= container_of(
1992 field
, struct bt_ctf_field_variant
, parent
);
1994 return bt_ctf_field_serialize(variant
->payload
, pos
,
1999 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
2000 struct bt_ctf_stream_pos
*pos
,
2001 enum bt_ctf_byte_order native_byte_order
)
2005 struct bt_ctf_field_array
*array
= container_of(
2006 field
, struct bt_ctf_field_array
, parent
);
2008 for (i
= 0; i
< array
->elements
->len
; i
++) {
2009 ret
= bt_ctf_field_serialize(
2010 g_ptr_array_index(array
->elements
, i
), pos
,
2013 fprintf(stderr
, "Failed to serialize array element #%zu\n", i
);
2022 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
2023 struct bt_ctf_stream_pos
*pos
,
2024 enum bt_ctf_byte_order native_byte_order
)
2028 struct bt_ctf_field_sequence
*sequence
= container_of(
2029 field
, struct bt_ctf_field_sequence
, parent
);
2031 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
2032 ret
= bt_ctf_field_serialize(
2033 g_ptr_array_index(sequence
->elements
, i
), pos
,
2036 fprintf(stderr
, "Failed to serialize sequence element #%zu\n", i
);
2045 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
2046 struct bt_ctf_stream_pos
*pos
,
2047 enum bt_ctf_byte_order native_byte_order
)
2051 struct bt_ctf_field_string
*string
= container_of(field
,
2052 struct bt_ctf_field_string
, parent
);
2053 struct bt_ctf_field_type
*character_type
=
2054 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
2055 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
2057 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
2058 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
2059 (uint64_t) string
->payload
->str
[i
]);
2064 ret
= bt_ctf_field_integer_serialize(character
, pos
,
2072 bt_put(character_type
);
2077 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
2078 struct bt_ctf_field
*dst
)
2080 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
2082 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
2083 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
2084 integer_dst
->payload
= integer_src
->payload
;
2089 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
2090 struct bt_ctf_field
*dst
)
2093 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
2095 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
2096 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
2098 if (enum_src
->payload
) {
2099 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
2100 if (!enum_dst
->payload
) {
2110 int bt_ctf_field_floating_point_copy(
2111 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
2113 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
2115 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
2117 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
2119 float_dst
->payload
= float_src
->payload
;
2124 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
2125 struct bt_ctf_field
*dst
)
2128 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
2130 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
2131 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
2133 /* This field_name_to_index HT is owned by the structure field type */
2134 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
2135 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
2137 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
2138 struct bt_ctf_field
*field
=
2139 g_ptr_array_index(struct_src
->fields
, i
);
2140 struct bt_ctf_field
*field_copy
= NULL
;
2143 field_copy
= bt_ctf_field_copy(field
);
2151 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
2158 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
2159 struct bt_ctf_field
*dst
)
2162 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
2164 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
2165 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
2167 if (variant_src
->tag
) {
2168 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
2169 if (!variant_dst
->tag
) {
2174 if (variant_src
->payload
) {
2175 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
2176 if (!variant_dst
->payload
) {
2186 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
2187 struct bt_ctf_field
*dst
)
2190 struct bt_ctf_field_array
*array_src
, *array_dst
;
2192 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
2193 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
2195 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
2196 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
2197 struct bt_ctf_field
*field
=
2198 g_ptr_array_index(array_src
->elements
, i
);
2199 struct bt_ctf_field
*field_copy
= NULL
;
2202 field_copy
= bt_ctf_field_copy(field
);
2210 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
2217 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
2218 struct bt_ctf_field
*dst
)
2221 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2222 struct bt_ctf_field
*src_length
;
2223 struct bt_ctf_field
*dst_length
;
2225 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2226 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2228 src_length
= bt_ctf_field_sequence_get_length(src
);
2231 /* no length set yet: keep destination sequence empty */
2235 /* copy source length */
2236 dst_length
= bt_ctf_field_copy(src_length
);
2244 /* this will initialize the destination sequence's internal array */
2245 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2252 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2254 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2255 struct bt_ctf_field
*field
=
2256 g_ptr_array_index(sequence_src
->elements
, i
);
2257 struct bt_ctf_field
*field_copy
= NULL
;
2260 field_copy
= bt_ctf_field_copy(field
);
2268 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2275 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2276 struct bt_ctf_field
*dst
)
2279 struct bt_ctf_field_string
*string_src
, *string_dst
;
2281 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2282 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2284 if (string_src
->payload
) {
2285 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2286 if (!string_dst
->payload
) {
2296 int increase_packet_size(struct bt_ctf_stream_pos
*pos
)
2301 ret
= munmap_align(pos
->base_mma
);
2306 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2308 ret
= bt_posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2309 pos
->packet_size
/ CHAR_BIT
);
2310 } while (ret
== EINTR
);
2317 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2318 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2319 if (pos
->base_mma
== MAP_FAILED
) {
2327 void generic_field_freeze(struct bt_ctf_field
*field
)
2333 void bt_ctf_field_enumeration_freeze(struct bt_ctf_field
*field
)
2335 struct bt_ctf_field_enumeration
*enum_field
=
2336 container_of(field
, struct bt_ctf_field_enumeration
, parent
);
2338 bt_ctf_field_freeze(enum_field
->payload
);
2339 generic_field_freeze(field
);
2343 void bt_ctf_field_structure_freeze(struct bt_ctf_field
*field
)
2346 struct bt_ctf_field_structure
*structure_field
=
2347 container_of(field
, struct bt_ctf_field_structure
, parent
);
2349 for (i
= 0; i
< structure_field
->fields
->len
; i
++) {
2350 struct bt_ctf_field
*field
=
2351 g_ptr_array_index(structure_field
->fields
, i
);
2353 bt_ctf_field_freeze(field
);
2356 generic_field_freeze(field
);
2360 void bt_ctf_field_variant_freeze(struct bt_ctf_field
*field
)
2362 struct bt_ctf_field_variant
*variant_field
=
2363 container_of(field
, struct bt_ctf_field_variant
, parent
);
2365 bt_ctf_field_freeze(variant_field
->tag
);
2366 bt_ctf_field_freeze(variant_field
->payload
);
2367 generic_field_freeze(field
);
2371 void bt_ctf_field_array_freeze(struct bt_ctf_field
*field
)
2374 struct bt_ctf_field_array
*array_field
=
2375 container_of(field
, struct bt_ctf_field_array
, parent
);
2377 for (i
= 0; i
< array_field
->elements
->len
; i
++) {
2378 struct bt_ctf_field
*field
=
2379 g_ptr_array_index(array_field
->elements
, i
);
2381 bt_ctf_field_freeze(field
);
2384 generic_field_freeze(field
);
2388 void bt_ctf_field_sequence_freeze(struct bt_ctf_field
*field
)
2391 struct bt_ctf_field_sequence
*sequence_field
=
2392 container_of(field
, struct bt_ctf_field_sequence
, parent
);
2394 bt_ctf_field_freeze(sequence_field
->length
);
2396 for (i
= 0; i
< sequence_field
->elements
->len
; i
++) {
2397 struct bt_ctf_field
*field
=
2398 g_ptr_array_index(sequence_field
->elements
, i
);
2400 bt_ctf_field_freeze(field
);
2403 generic_field_freeze(field
);
2407 void bt_ctf_field_freeze(struct bt_ctf_field
*field
)
2409 enum bt_ctf_type_id type_id
;
2415 type_id
= bt_ctf_field_get_type_id(field
);
2416 if (type_id
<= BT_CTF_TYPE_ID_UNKNOWN
||
2417 type_id
>= BT_CTF_NR_TYPE_IDS
) {
2421 field_freeze_funcs
[type_id
](field
);
2427 bool bt_ctf_field_generic_is_set(struct bt_ctf_field
*field
)
2429 return field
&& field
->payload_set
;
2433 bool bt_ctf_field_enumeration_is_set(struct bt_ctf_field
*field
)
2435 bool is_set
= false;
2436 struct bt_ctf_field_enumeration
*enumeration
;
2442 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
2444 if (!enumeration
->payload
) {
2448 is_set
= bt_ctf_field_is_set(enumeration
->payload
);
2454 bool bt_ctf_field_structure_is_set(struct bt_ctf_field
*field
)
2456 bool is_set
= false;
2458 struct bt_ctf_field_structure
*structure
;
2464 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
2465 for (i
= 0; i
< structure
->fields
->len
; i
++) {
2466 is_set
= bt_ctf_field_is_set(structure
->fields
->pdata
[i
]);
2476 bool bt_ctf_field_variant_is_set(struct bt_ctf_field
*field
)
2478 bool is_set
= false;
2479 struct bt_ctf_field_variant
*variant
;
2485 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
2486 is_set
= bt_ctf_field_is_set(variant
->payload
);
2492 bool bt_ctf_field_array_is_set(struct bt_ctf_field
*field
)
2495 bool is_set
= false;
2496 struct bt_ctf_field_array
*array
;
2502 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
2503 for (i
= 0; i
< array
->elements
->len
; i
++) {
2504 is_set
= bt_ctf_field_is_set(array
->elements
->pdata
[i
]);
2514 bool bt_ctf_field_sequence_is_set(struct bt_ctf_field
*field
)
2517 bool is_set
= false;
2518 struct bt_ctf_field_sequence
*sequence
;
2524 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
2525 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
2526 is_set
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);