4 * Babeltrace CTF IR - Event Fields
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/ctf-writer/event-fields.h>
30 #include <babeltrace/ctf-ir/event-fields-internal.h>
31 #include <babeltrace/ctf-ir/event-types-internal.h>
32 #include <babeltrace/compiler.h>
34 #define PACKET_LEN_INCREMENT (getpagesize() * 8 * CHAR_BIT)
37 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*);
39 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
40 struct bt_ctf_field_type
*);
42 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
43 struct bt_ctf_field_type
*);
45 struct bt_ctf_field
*bt_ctf_field_structure_create(
46 struct bt_ctf_field_type
*);
48 struct bt_ctf_field
*bt_ctf_field_variant_create(
49 struct bt_ctf_field_type
*);
51 struct bt_ctf_field
*bt_ctf_field_array_create(
52 struct bt_ctf_field_type
*);
54 struct bt_ctf_field
*bt_ctf_field_sequence_create(
55 struct bt_ctf_field_type
*);
57 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*);
60 void bt_ctf_field_destroy(struct bt_ctf_ref
*);
62 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*);
64 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*);
66 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*);
68 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*);
70 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*);
72 void bt_ctf_field_array_destroy(struct bt_ctf_field
*);
74 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*);
76 void bt_ctf_field_string_destroy(struct bt_ctf_field
*);
79 int bt_ctf_field_generic_validate(struct bt_ctf_field
*);
81 int bt_ctf_field_structure_validate(struct bt_ctf_field
*);
83 int bt_ctf_field_variant_validate(struct bt_ctf_field
*);
85 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*);
87 int bt_ctf_field_array_validate(struct bt_ctf_field
*);
89 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*);
92 int bt_ctf_field_generic_reset(struct bt_ctf_field
*);
94 int bt_ctf_field_structure_reset(struct bt_ctf_field
*);
96 int bt_ctf_field_variant_reset(struct bt_ctf_field
*);
98 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*);
100 int bt_ctf_field_array_reset(struct bt_ctf_field
*);
102 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*);
104 int bt_ctf_field_string_reset(struct bt_ctf_field
*);
107 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*,
108 struct ctf_stream_pos
*);
110 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*,
111 struct ctf_stream_pos
*);
113 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*,
114 struct ctf_stream_pos
*);
116 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*,
117 struct ctf_stream_pos
*);
119 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*,
120 struct ctf_stream_pos
*);
122 int bt_ctf_field_array_serialize(struct bt_ctf_field
*,
123 struct ctf_stream_pos
*);
125 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*,
126 struct ctf_stream_pos
*);
128 int bt_ctf_field_string_serialize(struct bt_ctf_field
*,
129 struct ctf_stream_pos
*);
132 int bt_ctf_field_integer_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
134 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
136 int bt_ctf_field_floating_point_copy(struct bt_ctf_field
*,
137 struct bt_ctf_field
*);
139 int bt_ctf_field_structure_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
141 int bt_ctf_field_variant_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
143 int bt_ctf_field_array_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
145 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
147 int bt_ctf_field_string_copy(struct bt_ctf_field
*, struct bt_ctf_field
*);
150 int increase_packet_size(struct ctf_stream_pos
*pos
);
153 struct bt_ctf_field
*(* const field_create_funcs
[])(
154 struct bt_ctf_field_type
*) = {
155 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_create
,
156 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_create
,
158 bt_ctf_field_floating_point_create
,
159 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_create
,
160 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_create
,
161 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_create
,
162 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_create
,
163 [CTF_TYPE_STRING
] = bt_ctf_field_string_create
,
167 void (* const field_destroy_funcs
[])(struct bt_ctf_field
*) = {
168 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_destroy
,
169 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_destroy
,
171 bt_ctf_field_floating_point_destroy
,
172 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_destroy
,
173 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_destroy
,
174 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_destroy
,
175 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_destroy
,
176 [CTF_TYPE_STRING
] = bt_ctf_field_string_destroy
,
180 int (* const field_validate_funcs
[])(struct bt_ctf_field
*) = {
181 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_validate
,
182 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_validate
,
183 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_validate
,
184 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_validate
,
185 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_validate
,
186 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_validate
,
187 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_validate
,
188 [CTF_TYPE_STRING
] = bt_ctf_field_generic_validate
,
192 int (* const field_reset_funcs
[])(struct bt_ctf_field
*) = {
193 [CTF_TYPE_INTEGER
] = bt_ctf_field_generic_reset
,
194 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_reset
,
195 [CTF_TYPE_FLOAT
] = bt_ctf_field_generic_reset
,
196 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_reset
,
197 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_reset
,
198 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_reset
,
199 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_reset
,
200 [CTF_TYPE_STRING
] = bt_ctf_field_string_reset
,
204 int (* const field_serialize_funcs
[])(struct bt_ctf_field
*,
205 struct ctf_stream_pos
*) = {
206 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_serialize
,
207 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_serialize
,
209 bt_ctf_field_floating_point_serialize
,
210 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_serialize
,
211 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_serialize
,
212 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_serialize
,
213 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_serialize
,
214 [CTF_TYPE_STRING
] = bt_ctf_field_string_serialize
,
218 int (* const field_copy_funcs
[])(struct bt_ctf_field
*,
219 struct bt_ctf_field
*) = {
220 [CTF_TYPE_INTEGER
] = bt_ctf_field_integer_copy
,
221 [CTF_TYPE_ENUM
] = bt_ctf_field_enumeration_copy
,
222 [CTF_TYPE_FLOAT
] = bt_ctf_field_floating_point_copy
,
223 [CTF_TYPE_STRUCT
] = bt_ctf_field_structure_copy
,
224 [CTF_TYPE_VARIANT
] = bt_ctf_field_variant_copy
,
225 [CTF_TYPE_ARRAY
] = bt_ctf_field_array_copy
,
226 [CTF_TYPE_SEQUENCE
] = bt_ctf_field_sequence_copy
,
227 [CTF_TYPE_STRING
] = bt_ctf_field_string_copy
,
230 struct bt_ctf_field
*bt_ctf_field_create(struct bt_ctf_field_type
*type
)
232 struct bt_ctf_field
*field
= NULL
;
233 enum ctf_type_id type_id
;
239 type_id
= bt_ctf_field_type_get_type_id(type
);
240 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
||
241 bt_ctf_field_type_validate(type
)) {
245 field
= field_create_funcs
[type_id
](type
);
250 /* The type's declaration can't change after this point */
251 bt_ctf_field_type_freeze(type
);
252 bt_ctf_field_type_get(type
);
253 bt_ctf_ref_init(&field
->ref_count
);
259 void bt_ctf_field_get(struct bt_ctf_field
*field
)
262 bt_ctf_ref_get(&field
->ref_count
);
266 void bt_ctf_field_put(struct bt_ctf_field
*field
)
269 bt_ctf_ref_put(&field
->ref_count
, bt_ctf_field_destroy
);
273 struct bt_ctf_field_type
*bt_ctf_field_get_type(struct bt_ctf_field
*field
)
275 struct bt_ctf_field_type
*ret
= NULL
;
282 bt_ctf_field_type_get(ret
);
287 struct bt_ctf_field
*bt_ctf_field_sequence_get_length(
288 struct bt_ctf_field
*field
)
290 struct bt_ctf_field
*ret
= NULL
;
291 struct bt_ctf_field_sequence
*sequence
;
297 if (bt_ctf_field_type_get_type_id(field
->type
) !=
302 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
303 ret
= sequence
->length
;
304 bt_ctf_field_get(ret
);
309 int bt_ctf_field_sequence_set_length(struct bt_ctf_field
*field
,
310 struct bt_ctf_field
*length_field
)
313 struct bt_ctf_field_type_integer
*length_type
;
314 struct bt_ctf_field_integer
*length
;
315 struct bt_ctf_field_sequence
*sequence
;
316 uint64_t sequence_length
;
318 if (!field
|| !length_field
) {
322 if (bt_ctf_field_type_get_type_id(length_field
->type
) !=
328 length_type
= container_of(length_field
->type
,
329 struct bt_ctf_field_type_integer
, parent
);
330 /* The length field must be unsigned */
331 if (length_type
->declaration
.signedness
) {
336 length
= container_of(length_field
, struct bt_ctf_field_integer
,
338 sequence_length
= length
->definition
.value
._unsigned
;
339 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
340 if (sequence
->elements
) {
341 g_ptr_array_free(sequence
->elements
, TRUE
);
342 bt_ctf_field_put(sequence
->length
);
345 sequence
->elements
= g_ptr_array_sized_new((size_t)sequence_length
);
346 if (!sequence
->elements
) {
351 g_ptr_array_set_free_func(sequence
->elements
,
352 (GDestroyNotify
)bt_ctf_field_put
);
353 g_ptr_array_set_size(sequence
->elements
, (size_t)sequence_length
);
354 bt_ctf_field_get(length_field
);
355 sequence
->length
= length_field
;
360 struct bt_ctf_field
*bt_ctf_field_structure_get_field(
361 struct bt_ctf_field
*field
, const char *name
)
363 struct bt_ctf_field
*new_field
= NULL
;
365 struct bt_ctf_field_structure
*structure
;
366 struct bt_ctf_field_type
*field_type
= NULL
;
369 if (!field
|| !name
||
370 bt_ctf_field_type_get_type_id(field
->type
) !=
375 field_quark
= g_quark_from_string(name
);
376 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
378 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
380 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
381 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*)&index
)) {
385 if (structure
->fields
->pdata
[index
]) {
386 new_field
= structure
->fields
->pdata
[index
];
390 new_field
= bt_ctf_field_create(field_type
);
395 structure
->fields
->pdata
[index
] = new_field
;
397 bt_ctf_field_get(new_field
);
400 bt_ctf_field_type_put(field_type
);
405 struct bt_ctf_field
*bt_ctf_field_structure_get_field_by_index(
406 struct bt_ctf_field
*field
, int index
)
409 const char *field_name
;
410 struct bt_ctf_field_structure
*structure
;
411 struct bt_ctf_field_type
*structure_type
;
412 struct bt_ctf_field_type
*field_type
= NULL
;
413 struct bt_ctf_field
*ret_field
= NULL
;
416 bt_ctf_field_type_get_type_id(field
->type
) != CTF_TYPE_STRUCT
) {
420 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
421 if (index
>= structure
->fields
->len
) {
425 ret_field
= structure
->fields
->pdata
[index
];
430 /* Field has not been instanciated yet, create it */
431 structure_type
= bt_ctf_field_get_type(field
);
432 if (!structure_type
) {
436 ret
= bt_ctf_field_type_structure_get_field(structure_type
,
437 &field_name
, &field_type
, index
);
438 bt_ctf_field_type_put(structure_type
);
443 ret_field
= bt_ctf_field_create(field_type
);
448 structure
->fields
->pdata
[index
] = ret_field
;
450 bt_ctf_field_get(ret_field
);
453 bt_ctf_field_type_put(field_type
);
459 int bt_ctf_field_structure_set_field(struct bt_ctf_field
*field
,
460 const char *name
, struct bt_ctf_field
*value
)
464 struct bt_ctf_field_structure
*structure
;
465 struct bt_ctf_field_type
*expected_field_type
= NULL
;
468 if (!field
|| !name
|| !value
||
469 bt_ctf_field_type_get_type_id(field
->type
) !=
475 field_quark
= g_quark_from_string(name
);
476 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
477 expected_field_type
=
478 bt_ctf_field_type_structure_get_field_type_by_name(field
->type
,
480 if (expected_field_type
!= value
->type
) {
485 if (!g_hash_table_lookup_extended(structure
->field_name_to_index
,
486 GUINT_TO_POINTER(field_quark
), NULL
, (gpointer
*) &index
)) {
490 if (structure
->fields
->pdata
[index
]) {
491 bt_ctf_field_put(structure
->fields
->pdata
[index
]);
494 structure
->fields
->pdata
[index
] = value
;
495 bt_ctf_field_get(value
);
497 if (expected_field_type
) {
498 bt_ctf_field_type_put(expected_field_type
);
503 struct bt_ctf_field
*bt_ctf_field_array_get_field(struct bt_ctf_field
*field
,
506 struct bt_ctf_field
*new_field
= NULL
;
507 struct bt_ctf_field_type
*field_type
= NULL
;
508 struct bt_ctf_field_array
*array
;
510 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
515 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
516 if (index
>= array
->elements
->len
) {
520 field_type
= bt_ctf_field_type_array_get_element_type(field
->type
);
521 if (array
->elements
->pdata
[(size_t)index
]) {
522 new_field
= array
->elements
->pdata
[(size_t)index
];
526 new_field
= bt_ctf_field_create(field_type
);
527 bt_ctf_field_get(new_field
);
528 array
->elements
->pdata
[(size_t)index
] = new_field
;
531 bt_ctf_field_type_put(field_type
);
536 struct bt_ctf_field
*bt_ctf_field_sequence_get_field(struct bt_ctf_field
*field
,
539 struct bt_ctf_field
*new_field
= NULL
;
540 struct bt_ctf_field_type
*field_type
= NULL
;
541 struct bt_ctf_field_sequence
*sequence
;
543 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
548 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
549 if (!sequence
->elements
|| sequence
->elements
->len
<= index
) {
553 field_type
= bt_ctf_field_type_sequence_get_element_type(field
->type
);
554 if (sequence
->elements
->pdata
[(size_t)index
]) {
555 new_field
= sequence
->elements
->pdata
[(size_t)index
];
559 new_field
= bt_ctf_field_create(field_type
);
560 bt_ctf_field_get(new_field
);
561 sequence
->elements
->pdata
[(size_t)index
] = new_field
;
564 bt_ctf_field_type_put(field_type
);
569 struct bt_ctf_field
*bt_ctf_field_variant_get_field(struct bt_ctf_field
*field
,
570 struct bt_ctf_field
*tag_field
)
572 struct bt_ctf_field
*new_field
= NULL
;
573 struct bt_ctf_field_variant
*variant
;
574 struct bt_ctf_field_type_variant
*variant_type
;
575 struct bt_ctf_field_type
*field_type
;
576 struct bt_ctf_field
*tag_enum
= NULL
;
577 struct bt_ctf_field_integer
*tag_enum_integer
;
578 int64_t tag_enum_value
;
580 if (!field
|| !tag_field
||
581 bt_ctf_field_type_get_type_id(field
->type
) !=
583 bt_ctf_field_type_get_type_id(tag_field
->type
) !=
588 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
589 variant_type
= container_of(field
->type
,
590 struct bt_ctf_field_type_variant
, parent
);
591 tag_enum
= bt_ctf_field_enumeration_get_container(tag_field
);
596 tag_enum_integer
= container_of(tag_enum
, struct bt_ctf_field_integer
,
599 if (bt_ctf_field_validate(tag_field
) < 0) {
603 tag_enum_value
= tag_enum_integer
->definition
.value
._signed
;
606 * If the variant currently has a tag and a payload, and if the
607 * requested tag value is the same as the current one, return
608 * the current payload instead of creating a fresh one.
610 if (variant
->tag
&& variant
->payload
) {
611 struct bt_ctf_field
*cur_tag_container
= NULL
;
612 struct bt_ctf_field_integer
*cur_tag_enum_integer
;
613 int64_t cur_tag_value
;
616 bt_ctf_field_enumeration_get_container(variant
->tag
);
617 cur_tag_enum_integer
= container_of(cur_tag_container
,
618 struct bt_ctf_field_integer
, parent
);
619 bt_ctf_field_put(cur_tag_container
);
620 cur_tag_value
= cur_tag_enum_integer
->definition
.value
._signed
;
622 if (cur_tag_value
== tag_enum_value
) {
623 new_field
= variant
->payload
;
624 bt_ctf_field_get(new_field
);
629 field_type
= bt_ctf_field_type_variant_get_field_type_signed(
630 variant_type
, tag_enum_value
);
635 new_field
= bt_ctf_field_create(field_type
);
640 bt_ctf_field_put(variant
->tag
);
641 bt_ctf_field_put(variant
->payload
);
642 bt_ctf_field_get(new_field
);
643 bt_ctf_field_get(tag_field
);
644 variant
->tag
= tag_field
;
645 variant
->payload
= new_field
;
647 bt_ctf_field_put(tag_enum
);
651 struct bt_ctf_field
*bt_ctf_field_enumeration_get_container(
652 struct bt_ctf_field
*field
)
654 struct bt_ctf_field
*container
= NULL
;
655 struct bt_ctf_field_enumeration
*enumeration
;
657 if (!field
|| bt_ctf_field_type_get_type_id(field
->type
) !=
662 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
664 if (!enumeration
->payload
) {
665 struct bt_ctf_field_type_enumeration
*enumeration_type
=
666 container_of(field
->type
,
667 struct bt_ctf_field_type_enumeration
, parent
);
668 enumeration
->payload
=
669 bt_ctf_field_create(enumeration_type
->container
);
672 container
= enumeration
->payload
;
673 bt_ctf_field_get(container
);
678 const char *bt_ctf_field_enumeration_get_mapping_name(
679 struct bt_ctf_field
*field
)
682 const char *name
= NULL
;
683 struct bt_ctf_field
*container
= NULL
;
684 struct bt_ctf_field_type
*container_type
= NULL
;
685 struct bt_ctf_field_type_integer
*integer_type
= NULL
;
686 struct bt_ctf_field_type_enumeration
*enumeration_type
= NULL
;
688 container
= bt_ctf_field_enumeration_get_container(field
);
693 container_type
= bt_ctf_field_get_type(container
);
694 if (!container_type
) {
695 goto error_put_container
;
698 integer_type
= container_of(container_type
,
699 struct bt_ctf_field_type_integer
, parent
);
700 enumeration_type
= container_of(field
->type
,
701 struct bt_ctf_field_type_enumeration
, parent
);
703 if (!integer_type
->declaration
.signedness
) {
705 ret
= bt_ctf_field_unsigned_integer_get_value(container
,
708 goto error_put_container_type
;
711 name
= bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
712 enumeration_type
, value
);
715 ret
= bt_ctf_field_signed_integer_get_value(container
,
718 goto error_put_container_type
;
721 name
= bt_ctf_field_type_enumeration_get_mapping_name_signed(
722 enumeration_type
, value
);
725 error_put_container_type
:
726 bt_ctf_field_type_put(container_type
);
728 bt_ctf_field_put(container
);
733 int bt_ctf_field_signed_integer_get_value(struct bt_ctf_field
*field
,
737 struct bt_ctf_field_integer
*integer
;
738 struct bt_ctf_field_type_integer
*integer_type
;
740 if (!field
|| !value
|| !field
->payload_set
||
741 bt_ctf_field_type_get_type_id(field
->type
) !=
747 integer_type
= container_of(field
->type
,
748 struct bt_ctf_field_type_integer
, parent
);
749 if (!integer_type
->declaration
.signedness
) {
754 integer
= container_of(field
,
755 struct bt_ctf_field_integer
, parent
);
756 *value
= integer
->definition
.value
._signed
;
761 int bt_ctf_field_signed_integer_set_value(struct bt_ctf_field
*field
,
765 struct bt_ctf_field_integer
*integer
;
766 struct bt_ctf_field_type_integer
*integer_type
;
768 int64_t min_value
, max_value
;
771 bt_ctf_field_type_get_type_id(field
->type
) !=
777 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
778 integer_type
= container_of(field
->type
,
779 struct bt_ctf_field_type_integer
, parent
);
780 if (!integer_type
->declaration
.signedness
) {
785 size
= integer_type
->declaration
.len
;
786 min_value
= -((int64_t)1 << (size
- 1));
787 max_value
= ((int64_t)1 << (size
- 1)) - 1;
788 if (value
< min_value
|| value
> max_value
) {
793 integer
->definition
.value
._signed
= value
;
794 integer
->parent
.payload_set
= 1;
799 int bt_ctf_field_unsigned_integer_get_value(struct bt_ctf_field
*field
,
803 struct bt_ctf_field_integer
*integer
;
804 struct bt_ctf_field_type_integer
*integer_type
;
806 if (!field
|| !value
|| !field
->payload_set
||
807 bt_ctf_field_type_get_type_id(field
->type
) !=
813 integer_type
= container_of(field
->type
,
814 struct bt_ctf_field_type_integer
, parent
);
815 if (integer_type
->declaration
.signedness
) {
820 integer
= container_of(field
,
821 struct bt_ctf_field_integer
, parent
);
822 *value
= integer
->definition
.value
._unsigned
;
827 int bt_ctf_field_unsigned_integer_set_value(struct bt_ctf_field
*field
,
831 struct bt_ctf_field_integer
*integer
;
832 struct bt_ctf_field_type_integer
*integer_type
;
837 bt_ctf_field_type_get_type_id(field
->type
) !=
843 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
844 integer_type
= container_of(field
->type
,
845 struct bt_ctf_field_type_integer
, parent
);
846 if (integer_type
->declaration
.signedness
) {
851 size
= integer_type
->declaration
.len
;
852 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t)1 << size
) - 1;
853 if (value
> max_value
) {
858 integer
->definition
.value
._unsigned
= value
;
859 integer
->parent
.payload_set
= 1;
864 int bt_ctf_field_floating_point_get_value(struct bt_ctf_field
*field
,
868 struct bt_ctf_field_floating_point
*floating_point
;
870 if (!field
|| !value
|| !field
->payload_set
||
871 bt_ctf_field_type_get_type_id(field
->type
) !=
877 floating_point
= container_of(field
,
878 struct bt_ctf_field_floating_point
, parent
);
879 *value
= floating_point
->definition
.value
;
884 int bt_ctf_field_floating_point_set_value(struct bt_ctf_field
*field
,
888 struct bt_ctf_field_floating_point
*floating_point
;
891 bt_ctf_field_type_get_type_id(field
->type
) !=
896 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
898 floating_point
->definition
.value
= value
;
899 floating_point
->parent
.payload_set
= 1;
904 const char *bt_ctf_field_string_get_value(struct bt_ctf_field
*field
)
906 const char *ret
= NULL
;
907 struct bt_ctf_field_string
*string
;
909 if (!field
|| !field
->payload_set
||
910 bt_ctf_field_type_get_type_id(field
->type
) !=
915 string
= container_of(field
,
916 struct bt_ctf_field_string
, parent
);
917 ret
= string
->payload
->str
;
922 int bt_ctf_field_string_set_value(struct bt_ctf_field
*field
,
926 struct bt_ctf_field_string
*string
;
928 if (!field
|| !value
||
929 bt_ctf_field_type_get_type_id(field
->type
) !=
935 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
936 if (string
->payload
) {
937 g_string_assign(string
->payload
, value
);
939 string
->payload
= g_string_new(value
);
942 string
->parent
.payload_set
= 1;
947 int bt_ctf_field_string_append(struct bt_ctf_field
*field
,
951 struct bt_ctf_field_string
*string_field
;
953 if (!field
|| !value
||
954 bt_ctf_field_type_get_type_id(field
->type
) !=
960 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
962 if (string_field
->payload
) {
963 g_string_append(string_field
->payload
, value
);
965 string_field
->payload
= g_string_new(value
);
968 string_field
->parent
.payload_set
= 1;
974 int bt_ctf_field_string_append_len(struct bt_ctf_field
*field
,
975 const char *value
, unsigned int length
)
979 unsigned int effective_length
= length
;
980 struct bt_ctf_field_string
*string_field
;
982 if (!field
|| !value
||
983 bt_ctf_field_type_get_type_id(field
->type
) !=
989 string_field
= container_of(field
, struct bt_ctf_field_string
, parent
);
991 /* make sure no null bytes are appended */
992 for (i
= 0; i
< length
; ++i
) {
993 if (value
[i
] == '\0') {
994 effective_length
= i
;
999 if (string_field
->payload
) {
1000 g_string_append_len(string_field
->payload
, value
,
1003 string_field
->payload
= g_string_new_len(value
,
1007 string_field
->parent
.payload_set
= 1;
1014 int bt_ctf_field_validate(struct bt_ctf_field
*field
)
1017 enum ctf_type_id type_id
;
1024 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1025 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1030 ret
= field_validate_funcs
[type_id
](field
);
1036 int bt_ctf_field_reset(struct bt_ctf_field
*field
)
1039 enum ctf_type_id type_id
;
1046 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1047 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1052 ret
= field_reset_funcs
[type_id
](field
);
1058 int bt_ctf_field_serialize(struct bt_ctf_field
*field
,
1059 struct ctf_stream_pos
*pos
)
1062 enum ctf_type_id type_id
;
1064 if (!field
|| !pos
) {
1069 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1070 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1075 ret
= field_serialize_funcs
[type_id
](field
, pos
);
1081 struct bt_ctf_field
*bt_ctf_field_copy(struct bt_ctf_field
*field
)
1084 struct bt_ctf_field
*copy
= NULL
;
1085 enum ctf_type_id type_id
;
1091 type_id
= bt_ctf_field_type_get_type_id(field
->type
);
1092 if (type_id
<= CTF_TYPE_UNKNOWN
|| type_id
>= NR_CTF_TYPES
) {
1096 copy
= bt_ctf_field_create(field
->type
);
1101 copy
->payload_set
= field
->payload_set
;
1102 ret
= field_copy_funcs
[type_id
](field
, copy
);
1104 bt_ctf_field_put(copy
);
1112 struct bt_ctf_field
*bt_ctf_field_integer_create(struct bt_ctf_field_type
*type
)
1114 struct bt_ctf_field_type_integer
*integer_type
= container_of(type
,
1115 struct bt_ctf_field_type_integer
, parent
);
1116 struct bt_ctf_field_integer
*integer
= g_new0(
1117 struct bt_ctf_field_integer
, 1);
1120 integer
->definition
.declaration
= &integer_type
->declaration
;
1123 return integer
? &integer
->parent
: NULL
;
1127 struct bt_ctf_field
*bt_ctf_field_enumeration_create(
1128 struct bt_ctf_field_type
*type
)
1130 struct bt_ctf_field_enumeration
*enumeration
= g_new0(
1131 struct bt_ctf_field_enumeration
, 1);
1133 return enumeration
? &enumeration
->parent
: NULL
;
1137 struct bt_ctf_field
*bt_ctf_field_floating_point_create(
1138 struct bt_ctf_field_type
*type
)
1140 struct bt_ctf_field_floating_point
*floating_point
;
1141 struct bt_ctf_field_type_floating_point
*floating_point_type
;
1143 floating_point
= g_new0(struct bt_ctf_field_floating_point
, 1);
1144 if (!floating_point
) {
1148 floating_point_type
= container_of(type
,
1149 struct bt_ctf_field_type_floating_point
, parent
);
1150 floating_point
->definition
.declaration
= container_of(
1151 type
->declaration
, struct declaration_float
, p
);
1154 floating_point
->definition
.sign
= &floating_point
->sign
;
1155 floating_point
->sign
.declaration
= &floating_point_type
->sign
;
1156 floating_point
->definition
.sign
->p
.declaration
=
1157 &floating_point_type
->sign
.p
;
1159 floating_point
->definition
.mantissa
= &floating_point
->mantissa
;
1160 floating_point
->mantissa
.declaration
= &floating_point_type
->mantissa
;
1161 floating_point
->definition
.mantissa
->p
.declaration
=
1162 &floating_point_type
->mantissa
.p
;
1164 floating_point
->definition
.exp
= &floating_point
->exp
;
1165 floating_point
->exp
.declaration
= &floating_point_type
->exp
;
1166 floating_point
->definition
.exp
->p
.declaration
=
1167 &floating_point_type
->exp
.p
;
1170 return floating_point
? &floating_point
->parent
: NULL
;
1174 struct bt_ctf_field
*bt_ctf_field_structure_create(
1175 struct bt_ctf_field_type
*type
)
1177 struct bt_ctf_field_type_structure
*structure_type
= container_of(type
,
1178 struct bt_ctf_field_type_structure
, parent
);
1179 struct bt_ctf_field_structure
*structure
= g_new0(
1180 struct bt_ctf_field_structure
, 1);
1181 struct bt_ctf_field
*field
= NULL
;
1183 if (!structure
|| !structure_type
->fields
->len
) {
1187 structure
->field_name_to_index
= structure_type
->field_name_to_index
;
1188 structure
->fields
= g_ptr_array_new_with_free_func(
1189 (GDestroyNotify
)bt_ctf_field_put
);
1190 g_ptr_array_set_size(structure
->fields
,
1191 g_hash_table_size(structure
->field_name_to_index
));
1192 field
= &structure
->parent
;
1198 struct bt_ctf_field
*bt_ctf_field_variant_create(struct bt_ctf_field_type
*type
)
1200 struct bt_ctf_field_variant
*variant
= g_new0(
1201 struct bt_ctf_field_variant
, 1);
1202 return variant
? &variant
->parent
: NULL
;
1206 struct bt_ctf_field
*bt_ctf_field_array_create(struct bt_ctf_field_type
*type
)
1208 struct bt_ctf_field_array
*array
= g_new0(struct bt_ctf_field_array
, 1);
1209 struct bt_ctf_field_type_array
*array_type
;
1210 unsigned int array_length
;
1212 if (!array
|| !type
) {
1216 array_type
= container_of(type
, struct bt_ctf_field_type_array
, parent
);
1217 array_length
= array_type
->length
;
1218 array
->elements
= g_ptr_array_sized_new(array_length
);
1219 if (!array
->elements
) {
1223 g_ptr_array_set_free_func(array
->elements
,
1224 (GDestroyNotify
)bt_ctf_field_put
);
1225 g_ptr_array_set_size(array
->elements
, array_length
);
1226 return &array
->parent
;
1233 struct bt_ctf_field
*bt_ctf_field_sequence_create(
1234 struct bt_ctf_field_type
*type
)
1236 struct bt_ctf_field_sequence
*sequence
= g_new0(
1237 struct bt_ctf_field_sequence
, 1);
1238 return sequence
? &sequence
->parent
: NULL
;
1242 struct bt_ctf_field
*bt_ctf_field_string_create(struct bt_ctf_field_type
*type
)
1244 struct bt_ctf_field_string
*string
= g_new0(
1245 struct bt_ctf_field_string
, 1);
1246 return string
? &string
->parent
: NULL
;
1250 void bt_ctf_field_destroy(struct bt_ctf_ref
*ref
)
1252 struct bt_ctf_field
*field
;
1253 struct bt_ctf_field_type
*type
;
1254 enum ctf_type_id type_id
;
1260 field
= container_of(ref
, struct bt_ctf_field
, ref_count
);
1262 type_id
= bt_ctf_field_type_get_type_id(type
);
1263 if (type_id
<= CTF_TYPE_UNKNOWN
||
1264 type_id
>= NR_CTF_TYPES
) {
1268 field_destroy_funcs
[type_id
](field
);
1270 bt_ctf_field_type_put(type
);
1275 void bt_ctf_field_integer_destroy(struct bt_ctf_field
*field
)
1277 struct bt_ctf_field_integer
*integer
;
1283 integer
= container_of(field
, struct bt_ctf_field_integer
, parent
);
1288 void bt_ctf_field_enumeration_destroy(struct bt_ctf_field
*field
)
1290 struct bt_ctf_field_enumeration
*enumeration
;
1296 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1298 bt_ctf_field_put(enumeration
->payload
);
1299 g_free(enumeration
);
1303 void bt_ctf_field_floating_point_destroy(struct bt_ctf_field
*field
)
1305 struct bt_ctf_field_floating_point
*floating_point
;
1311 floating_point
= container_of(field
, struct bt_ctf_field_floating_point
,
1313 g_free(floating_point
);
1317 void bt_ctf_field_structure_destroy(struct bt_ctf_field
*field
)
1319 struct bt_ctf_field_structure
*structure
;
1325 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1326 g_ptr_array_free(structure
->fields
, TRUE
);
1331 void bt_ctf_field_variant_destroy(struct bt_ctf_field
*field
)
1333 struct bt_ctf_field_variant
*variant
;
1339 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1340 bt_ctf_field_put(variant
->tag
);
1341 bt_ctf_field_put(variant
->payload
);
1346 void bt_ctf_field_array_destroy(struct bt_ctf_field
*field
)
1348 struct bt_ctf_field_array
*array
;
1354 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1355 g_ptr_array_free(array
->elements
, TRUE
);
1360 void bt_ctf_field_sequence_destroy(struct bt_ctf_field
*field
)
1362 struct bt_ctf_field_sequence
*sequence
;
1368 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1369 g_ptr_array_free(sequence
->elements
, TRUE
);
1370 bt_ctf_field_put(sequence
->length
);
1375 void bt_ctf_field_string_destroy(struct bt_ctf_field
*field
)
1377 struct bt_ctf_field_string
*string
;
1382 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1383 if (string
->payload
) {
1384 g_string_free(string
->payload
, TRUE
);
1390 int bt_ctf_field_generic_validate(struct bt_ctf_field
*field
)
1392 return (field
&& field
->payload_set
) ? 0 : -1;
1396 int bt_ctf_field_enumeration_validate(struct bt_ctf_field
*field
)
1399 struct bt_ctf_field_enumeration
*enumeration
;
1406 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1408 if (!enumeration
->payload
) {
1413 ret
= bt_ctf_field_validate(enumeration
->payload
);
1419 int bt_ctf_field_structure_validate(struct bt_ctf_field
*field
)
1423 struct bt_ctf_field_structure
*structure
;
1430 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1431 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1432 ret
= bt_ctf_field_validate(structure
->fields
->pdata
[i
]);
1442 int bt_ctf_field_variant_validate(struct bt_ctf_field
*field
)
1445 struct bt_ctf_field_variant
*variant
;
1452 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1453 ret
= bt_ctf_field_validate(variant
->payload
);
1459 int bt_ctf_field_array_validate(struct bt_ctf_field
*field
)
1463 struct bt_ctf_field_array
*array
;
1470 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1471 for (i
= 0; i
< array
->elements
->len
; i
++) {
1472 ret
= bt_ctf_field_validate(array
->elements
->pdata
[i
]);
1482 int bt_ctf_field_sequence_validate(struct bt_ctf_field
*field
)
1486 struct bt_ctf_field_sequence
*sequence
;
1493 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1494 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1495 ret
= bt_ctf_field_validate(sequence
->elements
->pdata
[i
]);
1505 int bt_ctf_field_generic_reset(struct bt_ctf_field
*field
)
1514 field
->payload_set
= 0;
1520 int bt_ctf_field_enumeration_reset(struct bt_ctf_field
*field
)
1523 struct bt_ctf_field_enumeration
*enumeration
;
1530 enumeration
= container_of(field
, struct bt_ctf_field_enumeration
,
1532 if (!enumeration
->payload
) {
1536 ret
= bt_ctf_field_reset(enumeration
->payload
);
1542 int bt_ctf_field_structure_reset(struct bt_ctf_field
*field
)
1546 struct bt_ctf_field_structure
*structure
;
1553 structure
= container_of(field
, struct bt_ctf_field_structure
, parent
);
1554 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1555 struct bt_ctf_field
*member
= structure
->fields
->pdata
[i
];
1559 * Structure members are lazily initialized; skip if
1560 * this member has not been allocated yet.
1565 ret
= bt_ctf_field_reset(member
);
1575 int bt_ctf_field_variant_reset(struct bt_ctf_field
*field
)
1578 struct bt_ctf_field_variant
*variant
;
1585 variant
= container_of(field
, struct bt_ctf_field_variant
, parent
);
1586 if (variant
->payload
) {
1587 ret
= bt_ctf_field_reset(variant
->payload
);
1594 int bt_ctf_field_array_reset(struct bt_ctf_field
*field
)
1598 struct bt_ctf_field_array
*array
;
1605 array
= container_of(field
, struct bt_ctf_field_array
, parent
);
1606 for (i
= 0; i
< array
->elements
->len
; i
++) {
1607 struct bt_ctf_field
*member
= array
->elements
->pdata
[i
];
1611 * Array elements are lazily initialized; skip if
1612 * this member has not been allocated yet.
1617 ret
= bt_ctf_field_reset(member
);
1627 int bt_ctf_field_sequence_reset(struct bt_ctf_field
*field
)
1631 struct bt_ctf_field_sequence
*sequence
;
1638 sequence
= container_of(field
, struct bt_ctf_field_sequence
, parent
);
1639 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1640 struct bt_ctf_field
*member
= sequence
->elements
->pdata
[i
];
1644 * Sequence elements 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_string_reset(struct bt_ctf_field
*field
)
1663 struct bt_ctf_field_string
*string
;
1670 ret
= bt_ctf_field_generic_reset(field
);
1675 string
= container_of(field
, struct bt_ctf_field_string
, parent
);
1676 if (string
->payload
) {
1677 g_string_truncate(string
->payload
, 0);
1684 int bt_ctf_field_integer_serialize(struct bt_ctf_field
*field
,
1685 struct ctf_stream_pos
*pos
)
1688 struct bt_ctf_field_integer
*integer
= container_of(field
,
1689 struct bt_ctf_field_integer
, parent
);
1692 ret
= ctf_integer_write(&pos
->parent
, &integer
->definition
.p
);
1693 if (ret
== -EFAULT
) {
1695 * The field is too large to fit in the current packet's
1696 * remaining space. Bump the packet size and retry.
1698 ret
= increase_packet_size(pos
);
1709 int bt_ctf_field_enumeration_serialize(struct bt_ctf_field
*field
,
1710 struct ctf_stream_pos
*pos
)
1712 struct bt_ctf_field_enumeration
*enumeration
= container_of(
1713 field
, struct bt_ctf_field_enumeration
, parent
);
1715 return bt_ctf_field_serialize(enumeration
->payload
, pos
);
1719 int bt_ctf_field_floating_point_serialize(struct bt_ctf_field
*field
,
1720 struct ctf_stream_pos
*pos
)
1723 struct bt_ctf_field_floating_point
*floating_point
= container_of(field
,
1724 struct bt_ctf_field_floating_point
, parent
);
1727 ret
= ctf_float_write(&pos
->parent
, &floating_point
->definition
.p
);
1728 if (ret
== -EFAULT
) {
1730 * The field is too large to fit in the current packet's
1731 * remaining space. Bump the packet size and retry.
1733 ret
= increase_packet_size(pos
);
1744 int bt_ctf_field_structure_serialize(struct bt_ctf_field
*field
,
1745 struct ctf_stream_pos
*pos
)
1749 struct bt_ctf_field_structure
*structure
= container_of(
1750 field
, struct bt_ctf_field_structure
, parent
);
1752 while (!ctf_pos_access_ok(pos
,
1753 offset_align(pos
->offset
,
1754 field
->type
->declaration
->alignment
))) {
1755 ret
= increase_packet_size(pos
);
1761 if (!ctf_align_pos(pos
, field
->type
->declaration
->alignment
)) {
1766 for (i
= 0; i
< structure
->fields
->len
; i
++) {
1767 struct bt_ctf_field
*field
= g_ptr_array_index(
1768 structure
->fields
, i
);
1770 ret
= bt_ctf_field_serialize(field
, pos
);
1780 int bt_ctf_field_variant_serialize(struct bt_ctf_field
*field
,
1781 struct ctf_stream_pos
*pos
)
1783 struct bt_ctf_field_variant
*variant
= container_of(
1784 field
, struct bt_ctf_field_variant
, parent
);
1786 return bt_ctf_field_serialize(variant
->payload
, pos
);
1790 int bt_ctf_field_array_serialize(struct bt_ctf_field
*field
,
1791 struct ctf_stream_pos
*pos
)
1795 struct bt_ctf_field_array
*array
= container_of(
1796 field
, struct bt_ctf_field_array
, parent
);
1798 for (i
= 0; i
< array
->elements
->len
; i
++) {
1799 ret
= bt_ctf_field_serialize(
1800 g_ptr_array_index(array
->elements
, i
), pos
);
1810 int bt_ctf_field_sequence_serialize(struct bt_ctf_field
*field
,
1811 struct ctf_stream_pos
*pos
)
1815 struct bt_ctf_field_sequence
*sequence
= container_of(
1816 field
, struct bt_ctf_field_sequence
, parent
);
1818 for (i
= 0; i
< sequence
->elements
->len
; i
++) {
1819 ret
= bt_ctf_field_serialize(
1820 g_ptr_array_index(sequence
->elements
, i
), pos
);
1830 int bt_ctf_field_string_serialize(struct bt_ctf_field
*field
,
1831 struct ctf_stream_pos
*pos
)
1835 struct bt_ctf_field_string
*string
= container_of(field
,
1836 struct bt_ctf_field_string
, parent
);
1837 struct bt_ctf_field_type
*character_type
=
1838 get_field_type(FIELD_TYPE_ALIAS_UINT8_T
);
1839 struct bt_ctf_field
*character
= bt_ctf_field_create(character_type
);
1841 for (i
= 0; i
< string
->payload
->len
+ 1; i
++) {
1842 ret
= bt_ctf_field_unsigned_integer_set_value(character
,
1843 (uint64_t) string
->payload
->str
[i
]);
1848 ret
= bt_ctf_field_integer_serialize(character
, pos
);
1854 bt_ctf_field_put(character
);
1855 bt_ctf_field_type_put(character_type
);
1860 int bt_ctf_field_integer_copy(struct bt_ctf_field
*src
,
1861 struct bt_ctf_field
*dst
)
1863 struct bt_ctf_field_integer
*integer_src
, *integer_dst
;
1865 integer_src
= container_of(src
, struct bt_ctf_field_integer
, parent
);
1866 integer_dst
= container_of(dst
, struct bt_ctf_field_integer
, parent
);
1868 memcpy(&integer_dst
->definition
, &integer_src
->definition
,
1869 sizeof(struct definition_integer
));
1874 int bt_ctf_field_enumeration_copy(struct bt_ctf_field
*src
,
1875 struct bt_ctf_field
*dst
)
1878 struct bt_ctf_field_enumeration
*enum_src
, *enum_dst
;
1880 enum_src
= container_of(src
, struct bt_ctf_field_enumeration
, parent
);
1881 enum_dst
= container_of(dst
, struct bt_ctf_field_enumeration
, parent
);
1883 if (enum_src
->payload
) {
1884 enum_dst
->payload
= bt_ctf_field_copy(enum_src
->payload
);
1885 if (!enum_dst
->payload
) {
1895 int bt_ctf_field_floating_point_copy(
1896 struct bt_ctf_field
*src
, struct bt_ctf_field
*dst
)
1898 struct bt_ctf_field_floating_point
*float_src
, *float_dst
;
1900 float_src
= container_of(src
, struct bt_ctf_field_floating_point
,
1902 float_dst
= container_of(dst
, struct bt_ctf_field_floating_point
,
1905 memcpy(&float_dst
->definition
, &float_src
->definition
,
1906 sizeof(struct definition_float
));
1907 memcpy(&float_dst
->sign
, &float_src
->sign
,
1908 sizeof(struct definition_integer
));
1909 memcpy(&float_dst
->mantissa
, &float_src
->mantissa
,
1910 sizeof(struct definition_integer
));
1911 memcpy(&float_dst
->exp
, &float_src
->exp
,
1912 sizeof(struct definition_integer
));
1917 int bt_ctf_field_structure_copy(struct bt_ctf_field
*src
,
1918 struct bt_ctf_field
*dst
)
1921 struct bt_ctf_field_structure
*struct_src
, *struct_dst
;
1923 struct_src
= container_of(src
, struct bt_ctf_field_structure
, parent
);
1924 struct_dst
= container_of(dst
, struct bt_ctf_field_structure
, parent
);
1926 /* This field_name_to_index HT is owned by the structure field type */
1927 struct_dst
->field_name_to_index
= struct_src
->field_name_to_index
;
1928 g_ptr_array_set_size(struct_dst
->fields
, struct_src
->fields
->len
);
1930 for (i
= 0; i
< struct_src
->fields
->len
; i
++) {
1931 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1932 g_ptr_array_index(struct_src
->fields
, i
));
1938 g_ptr_array_index(struct_dst
->fields
, i
) = field_copy
;
1945 int bt_ctf_field_variant_copy(struct bt_ctf_field
*src
,
1946 struct bt_ctf_field
*dst
)
1949 struct bt_ctf_field_variant
*variant_src
, *variant_dst
;
1951 variant_src
= container_of(src
, struct bt_ctf_field_variant
, parent
);
1952 variant_dst
= container_of(dst
, struct bt_ctf_field_variant
, parent
);
1954 if (variant_src
->tag
) {
1955 variant_dst
->tag
= bt_ctf_field_copy(variant_src
->tag
);
1956 if (!variant_dst
->tag
) {
1961 if (variant_src
->payload
) {
1962 variant_dst
->payload
= bt_ctf_field_copy(variant_src
->payload
);
1963 if (!variant_dst
->payload
) {
1973 int bt_ctf_field_array_copy(struct bt_ctf_field
*src
,
1974 struct bt_ctf_field
*dst
)
1977 struct bt_ctf_field_array
*array_src
, *array_dst
;
1979 array_src
= container_of(src
, struct bt_ctf_field_array
, parent
);
1980 array_dst
= container_of(dst
, struct bt_ctf_field_array
, parent
);
1982 g_ptr_array_set_size(array_dst
->elements
, array_src
->elements
->len
);
1983 for (i
= 0; i
< array_src
->elements
->len
; i
++) {
1984 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
1985 g_ptr_array_index(array_src
->elements
, i
));
1991 g_ptr_array_index(array_dst
->elements
, i
) = field_copy
;
1998 int bt_ctf_field_sequence_copy(struct bt_ctf_field
*src
,
1999 struct bt_ctf_field
*dst
)
2002 struct bt_ctf_field_sequence
*sequence_src
, *sequence_dst
;
2003 struct bt_ctf_field
*src_length
;
2004 struct bt_ctf_field
*dst_length
;
2006 sequence_src
= container_of(src
, struct bt_ctf_field_sequence
, parent
);
2007 sequence_dst
= container_of(dst
, struct bt_ctf_field_sequence
, parent
);
2009 src_length
= bt_ctf_field_sequence_get_length(src
);
2012 /* no length set yet: keep destination sequence empty */
2016 /* copy source length */
2017 dst_length
= bt_ctf_field_copy(src_length
);
2018 bt_ctf_field_put(src_length
);
2025 /* this will initialize the destination sequence's internal array */
2026 ret
= bt_ctf_field_sequence_set_length(dst
, dst_length
);
2027 bt_ctf_field_put(dst_length
);
2033 assert(sequence_dst
->elements
->len
== sequence_src
->elements
->len
);
2035 for (i
= 0; i
< sequence_src
->elements
->len
; i
++) {
2036 struct bt_ctf_field
*field_copy
= bt_ctf_field_copy(
2037 g_ptr_array_index(sequence_src
->elements
, i
));
2044 g_ptr_array_index(sequence_dst
->elements
, i
) = field_copy
;
2051 int bt_ctf_field_string_copy(struct bt_ctf_field
*src
,
2052 struct bt_ctf_field
*dst
)
2055 struct bt_ctf_field_string
*string_src
, *string_dst
;
2057 string_src
= container_of(src
, struct bt_ctf_field_string
, parent
);
2058 string_dst
= container_of(dst
, struct bt_ctf_field_string
, parent
);
2060 if (string_src
->payload
) {
2061 string_dst
->payload
= g_string_new(string_src
->payload
->str
);
2062 if (!string_dst
->payload
) {
2072 int increase_packet_size(struct ctf_stream_pos
*pos
)
2077 ret
= munmap_align(pos
->base_mma
);
2082 pos
->packet_size
+= PACKET_LEN_INCREMENT
;
2083 ret
= posix_fallocate(pos
->fd
, pos
->mmap_offset
,
2084 pos
->packet_size
/ CHAR_BIT
);
2089 pos
->base_mma
= mmap_align(pos
->packet_size
/ CHAR_BIT
, pos
->prot
,
2090 pos
->flags
, pos
->fd
, pos
->mmap_offset
);
2091 if (pos
->base_mma
== MAP_FAILED
) {