1 #ifndef BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
2 #define BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
5 * Babeltrace - CTF writer: Event Fields
7 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The Common Trace Format (CTF) Specification is available at
30 * http://www.efficios.com/ctf
36 #include <babeltrace/assert-pre-internal.h>
37 #include <babeltrace/babeltrace-internal.h>
38 #include <babeltrace/babeltrace.h>
39 #include <babeltrace/common-internal.h>
40 #include <babeltrace/ctf-writer/field-types-internal.h>
41 #include <babeltrace/ctf-writer/fields.h>
42 #include <babeltrace/ctf-writer/serialize-internal.h>
43 #include <babeltrace/ctf-writer/utils-internal.h>
44 #include <babeltrace/object-internal.h>
45 #include <babeltrace/types.h>
52 #define BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(_field, _type_id, _name) \
53 BT_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
54 _name " has the wrong type ID: expected-type-id=%s, " \
56 bt_ctf_field_type_id_string((int) (_type_id)), (_field))
58 #define BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(_field, _name) \
59 BT_ASSERT_PRE(bt_ctf_field_common_is_set_recursive(_field), \
60 _name " is not set: field-addr=%p", (_field))
62 #define BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(_field, _name) \
63 BT_ASSERT_PRE_HOT((_field), (_name), ": field-addr=%p", (_field))
65 struct bt_ctf_field_common
;
67 typedef void (*bt_ctf_field_common_method_set_is_frozen
)(struct bt_ctf_field_common
*,
69 typedef int (*bt_ctf_field_common_method_validate
)(struct bt_ctf_field_common
*);
70 typedef struct bt_ctf_field_common
*(*bt_ctf_field_common_method_copy
)(
71 struct bt_ctf_field_common
*);
72 typedef bt_bool (*bt_ctf_field_common_method_is_set
)(struct bt_ctf_field_common
*);
73 typedef void (*bt_ctf_field_common_method_reset
)(struct bt_ctf_field_common
*);
75 struct bt_ctf_field_common_methods
{
76 bt_ctf_field_common_method_set_is_frozen set_is_frozen
;
77 bt_ctf_field_common_method_validate validate
;
78 bt_ctf_field_common_method_copy copy
;
79 bt_ctf_field_common_method_is_set is_set
;
80 bt_ctf_field_common_method_reset reset
;
83 struct bt_ctf_field_common
{
84 struct bt_object base
;
85 struct bt_ctf_field_type_common
*type
;
86 struct bt_ctf_field_common_methods
*methods
;
91 * Specialized data for either CTF IR or CTF writer APIs.
92 * See comment in `field-types-internal.h` for more details.
103 struct bt_ctf_field_common_integer
{
104 struct bt_ctf_field_common common
;
111 struct bt_ctf_field_common_floating_point
{
112 struct bt_ctf_field_common common
;
116 struct bt_ctf_field_common_structure
{
117 struct bt_ctf_field_common common
;
119 /* Array of `struct bt_ctf_field_common *`, owned by this */
123 struct bt_ctf_field_common_variant
{
124 struct bt_ctf_field_common common
;
131 /* Weak: belongs to `choices` below */
132 struct bt_ctf_field_common
*current_field
;
134 /* Array of `struct bt_ctf_field_common *`, owned by this */
138 struct bt_ctf_field_common_array
{
139 struct bt_ctf_field_common common
;
141 /* Array of `struct bt_ctf_field_common *`, owned by this */
145 struct bt_ctf_field_common_sequence
{
146 struct bt_ctf_field_common common
;
149 * This is the true sequence field's length: its value can be
150 * less than `elements->len` below because we never shrink the
151 * array of elements to avoid reallocation.
155 /* Array of `struct bt_ctf_field_common *`, owned by this */
159 struct bt_ctf_field_common_string
{
160 struct bt_ctf_field_common common
;
166 struct bt_ctf_field_common
*bt_ctf_field_common_copy(struct bt_ctf_field_common
*field
);
169 int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common
*field
,
170 struct bt_ctf_field_type_common
*type
,
171 bool is_shared
, bt_object_release_func release_func
,
172 struct bt_ctf_field_common_methods
*methods
,
173 bt_ctf_field_common_create_func field_create_func
,
174 GDestroyNotify field_release_func
);
177 int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common
*field
,
178 struct bt_ctf_field_type_common
*type
,
179 bool is_shared
, bt_object_release_func release_func
,
180 struct bt_ctf_field_common_methods
*methods
,
181 bt_ctf_field_common_create_func field_create_func
,
182 GDestroyNotify field_destroy_func
);
185 int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common
*field
,
186 struct bt_ctf_field_type_common
*type
,
187 bool is_shared
, bt_object_release_func release_func
,
188 struct bt_ctf_field_common_methods
*methods
,
189 GDestroyNotify field_destroy_func
);
192 int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common
*field
,
193 struct bt_ctf_field_type_common
*type
,
194 bool is_shared
, bt_object_release_func release_func
,
195 struct bt_ctf_field_common_methods
*methods
,
196 bt_ctf_field_common_create_func field_create_func
,
197 GDestroyNotify field_release_func
);
200 int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common
*field
,
201 struct bt_ctf_field_type_common
*type
,
202 bool is_shared
, bt_object_release_func release_func
,
203 struct bt_ctf_field_common_methods
*methods
);
206 int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common
*field
);
209 int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common
*field
);
212 int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common
*field
);
215 int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common
*field
);
218 int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common
*field
);
221 void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common
*field
);
224 void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common
*field
);
227 void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common
*field
);
230 void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common
*field
);
233 void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common
*field
);
236 void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common
*field
,
240 void bt_ctf_field_common_structure_set_is_frozen_recursive(
241 struct bt_ctf_field_common
*field
, bool is_frozen
);
244 void bt_ctf_field_common_variant_set_is_frozen_recursive(
245 struct bt_ctf_field_common
*field
, bool is_frozen
);
248 void bt_ctf_field_common_array_set_is_frozen_recursive(
249 struct bt_ctf_field_common
*field
, bool is_frozen
);
252 void bt_ctf_field_common_sequence_set_is_frozen_recursive(
253 struct bt_ctf_field_common
*field
, bool is_frozen
);
256 void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common
*field
,
260 bt_bool
bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common
*field
);
263 bt_bool
bt_ctf_field_common_structure_is_set_recursive(
264 struct bt_ctf_field_common
*field
);
267 bt_bool
bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common
*field
);
270 bt_bool
bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common
*field
);
273 bt_bool
bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common
*field
);
276 # define bt_ctf_field_common_validate_recursive _bt_ctf_field_common_validate_recursive
277 # define bt_ctf_field_common_set_is_frozen_recursive _bt_ctf_field_common_set_is_frozen_recursive
278 # define bt_ctf_field_common_is_set_recursive _bt_ctf_field_common_is_set_recursive
279 # define bt_ctf_field_common_reset_recursive _bt_ctf_field_common_reset_recursive
280 # define bt_ctf_field_common_set _bt_ctf_field_common_set
282 # define bt_ctf_field_common_validate_recursive(_field) (-1)
283 # define bt_ctf_field_common_set_is_frozen_recursive(_field, _is_frozen)
284 # define bt_ctf_field_common_is_set_recursive(_field) (BT_FALSE)
285 # define bt_ctf_field_common_reset_recursive(_field)
286 # define bt_ctf_field_common_set(_field, _val)
290 static inline bool field_type_common_has_known_id(
291 struct bt_ctf_field_type_common
*ft
)
293 return (int) ft
->id
> BT_CTF_FIELD_TYPE_ID_UNKNOWN
||
294 (int) ft
->id
< BT_CTF_FIELD_TYPE_ID_NR
;
298 int _bt_ctf_field_common_validate_recursive(struct bt_ctf_field_common
*field
)
303 BT_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
308 BT_ASSERT(field_type_common_has_known_id(field
->type
));
310 if (field
->methods
->validate
) {
311 ret
= field
->methods
->validate(field
);
319 void _bt_ctf_field_common_reset_recursive(struct bt_ctf_field_common
*field
)
322 BT_ASSERT(field
->methods
->reset
);
323 field
->methods
->reset(field
);
327 void _bt_ctf_field_common_set(struct bt_ctf_field_common
*field
, bool value
)
330 field
->payload_set
= value
;
334 bt_bool
_bt_ctf_field_common_is_set_recursive(struct bt_ctf_field_common
*field
)
336 bt_bool is_set
= BT_FALSE
;
342 BT_ASSERT(field_type_common_has_known_id(field
->type
));
343 BT_ASSERT(field
->methods
->is_set
);
344 is_set
= field
->methods
->is_set(field
);
351 void bt_ctf_field_common_initialize(struct bt_ctf_field_common
*field
,
352 struct bt_ctf_field_type_common
*ft
, bool is_shared
,
353 bt_object_release_func release_func
,
354 struct bt_ctf_field_common_methods
*methods
)
358 bt_object_init(&field
->base
, is_shared
, release_func
);
359 field
->methods
= methods
;
360 field
->type
= bt_object_get_ref(ft
);
364 struct bt_ctf_field_type_common
*bt_ctf_field_common_borrow_type(
365 struct bt_ctf_field_common
*field
)
367 struct bt_ctf_field_type_common
*ret
= NULL
;
369 BT_ASSERT_PRE_NON_NULL(field
, "Field");
375 int64_t bt_ctf_field_common_sequence_get_length(struct bt_ctf_field_common
*field
)
377 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
379 BT_ASSERT_PRE_NON_NULL(field
, "Sequence field");
380 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
382 return (int64_t) sequence
->length
;
386 int bt_ctf_field_common_sequence_set_length(struct bt_ctf_field_common
*field
,
387 uint64_t length
, bt_ctf_field_common_create_func field_create_func
)
390 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
392 BT_ASSERT_PRE_NON_NULL(field
, "Sequence field");
393 BT_ASSERT_PRE(((int64_t) length
) >= 0,
394 "Invalid sequence length (too large): length=%" PRId64
,
396 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "Sequence field");
398 if (unlikely(length
> sequence
->elements
->len
)) {
400 struct bt_ctf_field_type_common_sequence
*sequence_ft
;
401 uint64_t cur_len
= sequence
->elements
->len
;
404 g_ptr_array_set_size(sequence
->elements
, length
);
405 sequence_ft
= BT_CTF_FROM_COMMON(sequence
->common
.type
);
407 for (i
= cur_len
; i
< sequence
->elements
->len
; i
++) {
408 struct bt_ctf_field_common
*elem_field
=
409 field_create_func(sequence_ft
->element_ft
);
416 BT_ASSERT(!sequence
->elements
->pdata
[i
]);
417 sequence
->elements
->pdata
[i
] = elem_field
;
421 sequence
->length
= length
;
428 struct bt_ctf_field_common
*bt_ctf_field_common_structure_borrow_field_by_name(
429 struct bt_ctf_field_common
*field
, const char *name
)
431 struct bt_ctf_field_common
*ret
= NULL
;
433 struct bt_ctf_field_type_common_structure
*structure_ft
;
434 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
436 GHashTable
*field_name_to_index
;
438 BT_ASSERT_PRE_NON_NULL(field
, "Structure field");
439 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
440 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
441 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Field");
442 structure_ft
= BT_CTF_FROM_COMMON(field
->type
);
443 field_name_to_index
= structure_ft
->field_name_to_index
;
444 field_quark
= g_quark_from_string(name
);
445 if (!g_hash_table_lookup_extended(field_name_to_index
,
446 GUINT_TO_POINTER(field_quark
),
447 NULL
, (gpointer
*) &index
)) {
448 BT_LOGV("Invalid parameter: no such field in structure field's type: "
449 "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"",
450 field
, field
->type
, name
);
454 ret
= structure
->fields
->pdata
[index
];
462 struct bt_ctf_field_common
*bt_ctf_field_common_structure_borrow_field_by_index(
463 struct bt_ctf_field_common
*field
, uint64_t index
)
465 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
467 BT_ASSERT_PRE_NON_NULL(field
, "Structure field");
468 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
469 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Field");
470 BT_ASSERT_PRE(index
< structure
->fields
->len
,
471 "Index is out of bound: struct-field-addr=%p, "
472 "index=%" PRIu64
", count=%u", field
, index
,
473 structure
->fields
->len
);
474 return structure
->fields
->pdata
[index
];
478 struct bt_ctf_field_common
*bt_ctf_field_common_array_borrow_field(
479 struct bt_ctf_field_common
*field
, uint64_t index
)
481 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
483 BT_ASSERT_PRE_NON_NULL(field
, "Array field");
484 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_ARRAY
,
486 BT_ASSERT_PRE(index
< array
->elements
->len
,
487 "Index is out of bound: array-field-addr=%p, "
488 "index=%" PRIu64
", count=%u", field
,
489 index
, array
->elements
->len
);
490 return array
->elements
->pdata
[(size_t) index
];
494 struct bt_ctf_field_common
*bt_ctf_field_common_sequence_borrow_field(
495 struct bt_ctf_field_common
*field
, uint64_t index
)
497 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
499 BT_ASSERT_PRE_NON_NULL(field
, "Sequence field");
500 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
502 BT_ASSERT_PRE(index
< sequence
->length
,
503 "Index is out of bound: seq-field-addr=%p, "
504 "index=%" PRIu64
", count=%u", field
, index
,
505 sequence
->elements
->len
);
506 return sequence
->elements
->pdata
[(size_t) index
];
510 int bt_ctf_field_common_variant_set_tag(struct bt_ctf_field_common
*variant_field
,
511 uint64_t tag_uval
, bool is_signed
)
514 int64_t choice_index
;
515 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
517 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
518 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
519 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
521 /* Find matching index in variant field's type */
522 choice_index
= bt_ctf_field_type_common_variant_find_choice_index(
523 variant_field
->type
, tag_uval
, is_signed
);
524 if (choice_index
< 0) {
529 /* Select corresponding field */
530 BT_ASSERT(choice_index
< variant
->fields
->len
);
531 variant
->current_field
= variant
->fields
->pdata
[choice_index
];
532 variant
->tag_value
.u
= tag_uval
;
539 struct bt_ctf_field_common
*bt_ctf_field_common_variant_borrow_current_field(
540 struct bt_ctf_field_common
*variant_field
)
542 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
544 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
545 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
546 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
547 BT_ASSERT_PRE(variant
->current_field
,
548 "Variant field has no current field: field-addr=%p", variant_field
);
549 return variant
->current_field
;
553 int bt_ctf_field_common_variant_get_tag_signed(struct bt_ctf_field_common
*variant_field
,
556 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
558 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
559 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
560 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
561 BT_ASSERT_PRE(variant
->current_field
,
562 "Variant field has no current field: field-addr=%p", variant_field
);
563 *tag
= variant
->tag_value
.i
;
568 int bt_ctf_field_common_variant_get_tag_unsigned(struct bt_ctf_field_common
*variant_field
,
571 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
573 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
574 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
575 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
576 BT_ASSERT_PRE(variant
->current_field
,
577 "Variant field has no current field: field-addr=%p", variant_field
);
578 *tag
= variant
->tag_value
.u
;
583 int bt_ctf_field_common_floating_point_get_value(struct bt_ctf_field_common
*field
,
586 struct bt_ctf_field_common_floating_point
*floating_point
=
587 BT_CTF_FROM_COMMON(field
);
589 BT_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
590 BT_ASSERT_PRE_NON_NULL(value
, "Value");
591 BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "Floating point number field");
592 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
593 BT_CTF_FIELD_TYPE_ID_FLOAT
, "Field");
594 *value
= floating_point
->payload
;
599 int bt_ctf_field_common_floating_point_set_value(struct bt_ctf_field_common
*field
,
602 struct bt_ctf_field_common_floating_point
*floating_point
=
603 BT_CTF_FROM_COMMON(field
);
605 BT_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
606 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "Floating point number field");
607 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
608 BT_CTF_FIELD_TYPE_ID_FLOAT
, "Field");
609 floating_point
->payload
= value
;
610 bt_ctf_field_common_set(field
, true);
615 const char *bt_ctf_field_common_string_get_value(struct bt_ctf_field_common
*field
)
617 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
619 BT_ASSERT_PRE_NON_NULL(field
, "String field");
620 BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "String field");
621 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
622 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
623 return (const char *) string
->buf
->data
;
627 int bt_ctf_field_common_string_clear(struct bt_ctf_field_common
*field
)
629 struct bt_ctf_field_common_string
*string_field
= BT_CTF_FROM_COMMON(field
);
631 BT_ASSERT_PRE_NON_NULL(field
, "String field");
632 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
633 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
634 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
635 string_field
->size
= 0;
636 bt_ctf_field_common_set(field
, true);
641 int bt_ctf_field_common_string_append_len(struct bt_ctf_field_common
*field
,
642 const char *value
, unsigned int length
)
644 struct bt_ctf_field_common_string
*string_field
= BT_CTF_FROM_COMMON(field
);
648 BT_ASSERT_PRE_NON_NULL(field
, "String field");
649 BT_ASSERT_PRE_NON_NULL(value
, "Value");
650 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
651 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
652 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
654 /* Make sure no null bytes are appended */
655 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
656 "String value to append contains a null character: "
657 "partial-value=\"%.32s\", length=%u", value
, length
);
659 new_size
= string_field
->size
+ length
;
661 if (unlikely(new_size
+ 1 > string_field
->buf
->len
)) {
662 g_array_set_size(string_field
->buf
, new_size
+ 1);
665 data
= string_field
->buf
->data
;
666 memcpy(data
+ string_field
->size
, value
, length
);
667 ((char *) string_field
->buf
->data
)[new_size
] = '\0';
668 string_field
->size
= new_size
;
669 bt_ctf_field_common_set(field
, true);
674 int bt_ctf_field_common_string_append(struct bt_ctf_field_common
*field
,
677 BT_ASSERT_PRE_NON_NULL(value
, "Value");
678 return bt_ctf_field_common_string_append_len(field
, value
,
683 int bt_ctf_field_common_string_set_value(struct bt_ctf_field_common
*field
,
686 BT_ASSERT_PRE_NON_NULL(field
, "String field");
687 BT_ASSERT_PRE_NON_NULL(value
, "Value");
688 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
689 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
690 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
691 bt_ctf_field_common_string_clear(field
);
692 return bt_ctf_field_common_string_append_len(field
,
693 value
, strlen(value
));
697 void bt_ctf_field_common_finalize(struct bt_ctf_field_common
*field
)
700 BT_LOGD_STR("Putting field's type.");
701 bt_object_put_ref(field
->type
);
705 void bt_ctf_field_common_integer_finalize(struct bt_ctf_field_common
*field
)
708 BT_LOGD("Finalizing common integer field object: addr=%p", field
);
709 bt_ctf_field_common_finalize(field
);
713 void bt_ctf_field_common_floating_point_finalize(struct bt_ctf_field_common
*field
)
716 BT_LOGD("Finalizing common floating point number field object: addr=%p", field
);
717 bt_ctf_field_common_finalize(field
);
721 void bt_ctf_field_common_structure_finalize_recursive(struct bt_ctf_field_common
*field
)
723 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
726 BT_LOGD("Finalizing common structure field object: addr=%p", field
);
727 bt_ctf_field_common_finalize(field
);
729 if (structure
->fields
) {
730 g_ptr_array_free(structure
->fields
, TRUE
);
735 void bt_ctf_field_common_variant_finalize_recursive(struct bt_ctf_field_common
*field
)
737 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
740 BT_LOGD("Finalizing common variant field object: addr=%p", field
);
741 bt_ctf_field_common_finalize(field
);
743 if (variant
->fields
) {
744 g_ptr_array_free(variant
->fields
, TRUE
);
749 void bt_ctf_field_common_array_finalize_recursive(struct bt_ctf_field_common
*field
)
751 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
754 BT_LOGD("Finalizing common array field object: addr=%p", field
);
755 bt_ctf_field_common_finalize(field
);
757 if (array
->elements
) {
758 g_ptr_array_free(array
->elements
, TRUE
);
763 void bt_ctf_field_common_sequence_finalize_recursive(struct bt_ctf_field_common
*field
)
765 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
768 BT_LOGD("Finalizing common sequence field object: addr=%p", field
);
769 bt_ctf_field_common_finalize(field
);
771 if (sequence
->elements
) {
772 g_ptr_array_free(sequence
->elements
, TRUE
);
777 void bt_ctf_field_common_string_finalize(struct bt_ctf_field_common
*field
)
779 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
782 BT_LOGD("Finalizing common string field object: addr=%p", field
);
783 bt_ctf_field_common_finalize(field
);
786 g_array_free(string
->buf
, TRUE
);
791 static inline bool value_is_in_range_signed(unsigned int size
, int64_t value
)
794 int64_t min_value
, max_value
;
796 min_value
= -(1ULL << (size
- 1));
797 max_value
= (1ULL << (size
- 1)) - 1;
798 if (value
< min_value
|| value
> max_value
) {
799 BT_LOGF("Value is out of bounds: value=%" PRId64
", "
800 "min-value=%" PRId64
", max-value=%" PRId64
,
801 value
, min_value
, max_value
);
809 static inline bool value_is_in_range_unsigned(unsigned int size
, uint64_t value
)
814 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
815 if (value
> max_value
) {
816 BT_LOGF("Value is out of bounds: value=%" PRIu64
", "
817 "max-value=%" PRIu64
,
825 struct bt_ctf_field_enumeration
{
826 struct bt_ctf_field_common common
;
827 struct bt_ctf_field_common_integer
*container
;
830 struct bt_ctf_field_variant
{
831 struct bt_ctf_field_common_variant common
;
832 struct bt_ctf_field_enumeration
*tag
;
836 int bt_ctf_field_serialize_recursive(struct bt_ctf_field
*field
,
837 struct bt_ctf_stream_pos
*pos
,
838 enum bt_ctf_byte_order native_byte_order
);
841 int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field
*field
,
842 const char *name
, struct bt_ctf_field
*value
);
845 struct bt_ctf_field
*bt_ctf_field_enumeration_borrow_container(
846 struct bt_ctf_field
*field
);
849 bt_bool
bt_ctf_field_is_set_recursive(struct bt_ctf_field
*field
)
851 return bt_ctf_field_common_is_set_recursive((void *) field
);
854 #endif /* BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H */