2 * SPDX-License-Identifier: MIT
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * The Common Trace Format (CTF) Specification is available at
7 * http://www.efficios.com/ctf
10 #ifndef BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
11 #define BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
20 #include <babeltrace2-ctf-writer/fields.h>
21 #include <babeltrace2/types.h>
23 #include "common/macros.h"
24 #include "common/common.h"
25 #include "ctfser/ctfser.h"
27 #include "assert-pre.h"
28 #include "field-types.h"
32 #define BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(_field, _type_id, _name) \
33 BT_CTF_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
34 _name " has the wrong type ID: expected-type-id=%s, " \
36 bt_ctf_field_type_id_string((int) (_type_id)), (_field))
38 #define BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(_field, _name) \
39 BT_CTF_ASSERT_PRE(bt_ctf_field_common_is_set_recursive(_field), \
40 _name " is not set: field-addr=%p", (_field))
42 #define BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(_field, _name) \
43 BT_CTF_ASSERT_PRE_HOT((_field), (_name), ": field-addr=%p", (_field))
45 struct bt_ctf_field_common
;
47 typedef void (*bt_ctf_field_common_method_set_is_frozen
)(struct bt_ctf_field_common
*,
49 typedef int (*bt_ctf_field_common_method_validate
)(struct bt_ctf_field_common
*);
50 typedef struct bt_ctf_field_common
*(*bt_ctf_field_common_method_copy
)(
51 struct bt_ctf_field_common
*);
52 typedef bt_ctf_bool (*bt_ctf_field_common_method_is_set
)(struct bt_ctf_field_common
*);
53 typedef void (*bt_ctf_field_common_method_reset
)(struct bt_ctf_field_common
*);
55 struct bt_ctf_field_common_methods
{
56 bt_ctf_field_common_method_set_is_frozen set_is_frozen
;
57 bt_ctf_field_common_method_validate validate
;
58 bt_ctf_field_common_method_copy copy
;
59 bt_ctf_field_common_method_is_set is_set
;
60 bt_ctf_field_common_method_reset reset
;
63 struct bt_ctf_field_common
{
64 struct bt_ctf_object base
;
65 struct bt_ctf_field_type_common
*type
;
66 struct bt_ctf_field_common_methods
*methods
;
71 * Specialized data for either CTF IR or CTF writer APIs.
72 * See comment in `field-types.h` for more details.
83 struct bt_ctf_field_common_integer
{
84 struct bt_ctf_field_common common
;
91 struct bt_ctf_field_common_floating_point
{
92 struct bt_ctf_field_common common
;
96 struct bt_ctf_field_common_structure
{
97 struct bt_ctf_field_common common
;
99 /* Array of `struct bt_ctf_field_common *`, owned by this */
103 struct bt_ctf_field_common_variant
{
104 struct bt_ctf_field_common common
;
111 /* Weak: belongs to `choices` below */
112 struct bt_ctf_field_common
*current_field
;
114 /* Array of `struct bt_ctf_field_common *`, owned by this */
118 struct bt_ctf_field_common_array
{
119 struct bt_ctf_field_common common
;
121 /* Array of `struct bt_ctf_field_common *`, owned by this */
125 struct bt_ctf_field_common_sequence
{
126 struct bt_ctf_field_common common
;
129 * This is the true sequence field's length: its value can be
130 * less than `elements->len` below because we never shrink the
131 * array of elements to avoid reallocation.
135 /* Array of `struct bt_ctf_field_common *`, owned by this */
139 struct bt_ctf_field_common_string
{
140 struct bt_ctf_field_common common
;
146 struct bt_ctf_field_common
*bt_ctf_field_common_copy(struct bt_ctf_field_common
*field
);
149 int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common
*field
,
150 struct bt_ctf_field_type_common
*type
,
151 bool is_shared
, bt_ctf_object_release_func release_func
,
152 struct bt_ctf_field_common_methods
*methods
,
153 bt_ctf_field_common_create_func field_create_func
,
154 GDestroyNotify field_release_func
);
157 int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common
*field
,
158 struct bt_ctf_field_type_common
*type
,
159 bool is_shared
, bt_ctf_object_release_func release_func
,
160 struct bt_ctf_field_common_methods
*methods
,
161 bt_ctf_field_common_create_func field_create_func
,
162 GDestroyNotify field_destroy_func
);
165 int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common
*field
,
166 struct bt_ctf_field_type_common
*type
,
167 bool is_shared
, bt_ctf_object_release_func release_func
,
168 struct bt_ctf_field_common_methods
*methods
,
169 GDestroyNotify field_destroy_func
);
172 int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common
*field
,
173 struct bt_ctf_field_type_common
*type
,
174 bool is_shared
, bt_ctf_object_release_func release_func
,
175 struct bt_ctf_field_common_methods
*methods
,
176 bt_ctf_field_common_create_func field_create_func
,
177 GDestroyNotify field_release_func
);
180 int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common
*field
,
181 struct bt_ctf_field_type_common
*type
,
182 bool is_shared
, bt_ctf_object_release_func release_func
,
183 struct bt_ctf_field_common_methods
*methods
);
186 int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common
*field
);
189 int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common
*field
);
192 int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common
*field
);
195 int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common
*field
);
198 int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common
*field
);
201 void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common
*field
);
204 void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common
*field
);
207 void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common
*field
);
210 void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common
*field
);
213 void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common
*field
);
216 void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common
*field
,
220 void bt_ctf_field_common_structure_set_is_frozen_recursive(
221 struct bt_ctf_field_common
*field
, bool is_frozen
);
224 void bt_ctf_field_common_variant_set_is_frozen_recursive(
225 struct bt_ctf_field_common
*field
, bool is_frozen
);
228 void bt_ctf_field_common_array_set_is_frozen_recursive(
229 struct bt_ctf_field_common
*field
, bool is_frozen
);
232 void bt_ctf_field_common_sequence_set_is_frozen_recursive(
233 struct bt_ctf_field_common
*field
, bool is_frozen
);
236 void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common
*field
,
240 bt_ctf_bool
bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common
*field
);
243 bt_ctf_bool
bt_ctf_field_common_structure_is_set_recursive(
244 struct bt_ctf_field_common
*field
);
247 bt_ctf_bool
bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common
*field
);
250 bt_ctf_bool
bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common
*field
);
253 bt_ctf_bool
bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common
*field
);
256 # define bt_ctf_field_common_validate_recursive _bt_ctf_field_common_validate_recursive
257 # define bt_ctf_field_common_set_is_frozen_recursive _bt_ctf_field_common_set_is_frozen_recursive
258 # define bt_ctf_field_common_is_set_recursive _bt_ctf_field_common_is_set_recursive
259 # define bt_ctf_field_common_reset_recursive _bt_ctf_field_common_reset_recursive
260 # define bt_ctf_field_common_set _bt_ctf_field_common_set
262 # define bt_ctf_field_common_validate_recursive(_field) (-1)
263 # define bt_ctf_field_common_set_is_frozen_recursive(_field, _is_frozen)
264 # define bt_ctf_field_common_is_set_recursive(_field) (BT_CTF_FALSE)
265 # define bt_ctf_field_common_reset_recursive(_field)
266 # define bt_ctf_field_common_set(_field, _val)
270 static inline bool field_type_common_has_known_id(
271 struct bt_ctf_field_type_common
*ft
)
273 return (int) ft
->id
> BT_CTF_FIELD_TYPE_ID_UNKNOWN
&&
274 (int) ft
->id
< BT_CTF_FIELD_TYPE_ID_NR
;
278 int _bt_ctf_field_common_validate_recursive(struct bt_ctf_field_common
*field
)
283 BT_CTF_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
288 BT_ASSERT_DBG(field_type_common_has_known_id(field
->type
));
290 if (field
->methods
->validate
) {
291 ret
= field
->methods
->validate(field
);
299 void _bt_ctf_field_common_reset_recursive(struct bt_ctf_field_common
*field
)
301 BT_ASSERT_DBG(field
);
302 BT_ASSERT_DBG(field
->methods
->reset
);
303 field
->methods
->reset(field
);
307 void _bt_ctf_field_common_set(struct bt_ctf_field_common
*field
, bool value
)
309 BT_ASSERT_DBG(field
);
310 field
->payload_set
= value
;
314 bt_ctf_bool
_bt_ctf_field_common_is_set_recursive(struct bt_ctf_field_common
*field
)
316 bt_ctf_bool is_set
= BT_CTF_FALSE
;
322 BT_ASSERT_DBG(field_type_common_has_known_id(field
->type
));
323 BT_ASSERT_DBG(field
->methods
->is_set
);
324 is_set
= field
->methods
->is_set(field
);
331 void bt_ctf_field_common_initialize(struct bt_ctf_field_common
*field
,
332 struct bt_ctf_field_type_common
*ft
, bool is_shared
,
333 bt_ctf_object_release_func release_func
,
334 struct bt_ctf_field_common_methods
*methods
)
336 BT_ASSERT_DBG(field
);
338 bt_ctf_object_init(&field
->base
, is_shared
, release_func
);
339 field
->methods
= methods
;
340 field
->type
= (void *) bt_ctf_object_get_ref(ft
);
344 struct bt_ctf_field_type_common
*bt_ctf_field_common_borrow_type(
345 struct bt_ctf_field_common
*field
)
347 struct bt_ctf_field_type_common
*ret
= NULL
;
349 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Field");
355 int64_t bt_ctf_field_common_sequence_get_length(struct bt_ctf_field_common
*field
)
357 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
359 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Sequence field");
360 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
362 return (int64_t) sequence
->length
;
366 int bt_ctf_field_common_sequence_set_length(struct bt_ctf_field_common
*field
,
367 uint64_t length
, bt_ctf_field_common_create_func field_create_func
)
370 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
372 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Sequence field");
373 BT_CTF_ASSERT_PRE(((int64_t) length
) >= 0,
374 "Invalid sequence length (too large): length=%" PRId64
,
376 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "Sequence field");
378 if (G_UNLIKELY(length
> sequence
->elements
->len
)) {
380 struct bt_ctf_field_type_common_sequence
*sequence_ft
;
381 uint64_t cur_len
= sequence
->elements
->len
;
384 g_ptr_array_set_size(sequence
->elements
, length
);
385 sequence_ft
= BT_CTF_FROM_COMMON(sequence
->common
.type
);
387 for (i
= cur_len
; i
< sequence
->elements
->len
; i
++) {
388 struct bt_ctf_field_common
*elem_field
=
389 field_create_func(sequence_ft
->element_ft
);
396 BT_ASSERT_DBG(!sequence
->elements
->pdata
[i
]);
397 sequence
->elements
->pdata
[i
] = elem_field
;
401 sequence
->length
= length
;
408 struct bt_ctf_field_common
*bt_ctf_field_common_structure_borrow_field_by_name(
409 struct bt_ctf_field_common
*field
, const char *name
)
411 struct bt_ctf_field_common
*ret
= NULL
;
413 struct bt_ctf_field_type_common_structure
*structure_ft
;
414 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
416 GHashTable
*field_name_to_index
;
418 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Structure field");
419 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Field name");
420 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
421 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Field");
422 structure_ft
= BT_CTF_FROM_COMMON(field
->type
);
423 field_name_to_index
= structure_ft
->field_name_to_index
;
424 field_quark
= g_quark_from_string(name
);
425 if (!g_hash_table_lookup_extended(field_name_to_index
,
426 GUINT_TO_POINTER(field_quark
),
427 NULL
, (gpointer
*) &index
)) {
428 BT_LOGT("Invalid parameter: no such field in structure field's type: "
429 "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"",
430 field
, field
->type
, name
);
434 ret
= structure
->fields
->pdata
[index
];
442 struct bt_ctf_field_common
*bt_ctf_field_common_structure_borrow_field_by_index(
443 struct bt_ctf_field_common
*field
, uint64_t index
)
445 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
447 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Structure field");
448 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
449 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Field");
450 BT_CTF_ASSERT_PRE(index
< structure
->fields
->len
,
451 "Index is out of bound: struct-field-addr=%p, "
452 "index=%" PRIu64
", count=%u", field
, index
,
453 structure
->fields
->len
);
454 return structure
->fields
->pdata
[index
];
458 struct bt_ctf_field_common
*bt_ctf_field_common_array_borrow_field(
459 struct bt_ctf_field_common
*field
, uint64_t index
)
461 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
463 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Array field");
464 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_ARRAY
,
466 BT_CTF_ASSERT_PRE(index
< array
->elements
->len
,
467 "Index is out of bound: array-field-addr=%p, "
468 "index=%" PRIu64
", count=%u", field
,
469 index
, array
->elements
->len
);
470 return array
->elements
->pdata
[(size_t) index
];
474 struct bt_ctf_field_common
*bt_ctf_field_common_sequence_borrow_field(
475 struct bt_ctf_field_common
*field
, uint64_t index
)
477 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
479 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Sequence field");
480 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
482 BT_CTF_ASSERT_PRE(index
< sequence
->length
,
483 "Index is out of bound: seq-field-addr=%p, "
484 "index=%" PRIu64
", count=%u", field
, index
,
485 sequence
->elements
->len
);
486 return sequence
->elements
->pdata
[(size_t) index
];
490 int bt_ctf_field_common_variant_set_tag(struct bt_ctf_field_common
*variant_field
,
491 uint64_t tag_uval
, bool is_signed
)
494 int64_t choice_index
;
495 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
497 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
498 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
499 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
501 /* Find matching index in variant field's type */
502 choice_index
= bt_ctf_field_type_common_variant_find_choice_index(
503 variant_field
->type
, tag_uval
, is_signed
);
504 if (choice_index
< 0) {
509 /* Select corresponding field */
510 BT_ASSERT_DBG(choice_index
< variant
->fields
->len
);
511 variant
->current_field
= variant
->fields
->pdata
[choice_index
];
512 variant
->tag_value
.u
= tag_uval
;
519 struct bt_ctf_field_common
*bt_ctf_field_common_variant_borrow_current_field(
520 struct bt_ctf_field_common
*variant_field
)
522 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
524 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
525 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
526 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
527 BT_CTF_ASSERT_PRE(variant
->current_field
,
528 "Variant field has no current field: field-addr=%p", variant_field
);
529 return variant
->current_field
;
533 int bt_ctf_field_common_variant_get_tag_signed(struct bt_ctf_field_common
*variant_field
,
536 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
538 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
539 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
540 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
541 BT_CTF_ASSERT_PRE(variant
->current_field
,
542 "Variant field has no current field: field-addr=%p", variant_field
);
543 *tag
= variant
->tag_value
.i
;
548 int bt_ctf_field_common_variant_get_tag_unsigned(struct bt_ctf_field_common
*variant_field
,
551 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
553 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
554 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
555 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
556 BT_CTF_ASSERT_PRE(variant
->current_field
,
557 "Variant field has no current field: field-addr=%p", variant_field
);
558 *tag
= variant
->tag_value
.u
;
563 int bt_ctf_field_common_floating_point_get_value(struct bt_ctf_field_common
*field
,
566 struct bt_ctf_field_common_floating_point
*floating_point
=
567 BT_CTF_FROM_COMMON(field
);
569 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
570 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
571 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "Floating point number field");
572 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
573 BT_CTF_FIELD_TYPE_ID_FLOAT
, "Field");
574 *value
= floating_point
->payload
;
579 int bt_ctf_field_common_floating_point_set_value(struct bt_ctf_field_common
*field
,
582 struct bt_ctf_field_common_floating_point
*floating_point
=
583 BT_CTF_FROM_COMMON(field
);
585 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
586 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "Floating point number field");
587 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
588 BT_CTF_FIELD_TYPE_ID_FLOAT
, "Field");
589 floating_point
->payload
= value
;
590 bt_ctf_field_common_set(field
, true);
595 const char *bt_ctf_field_common_string_get_value(struct bt_ctf_field_common
*field
)
597 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
599 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
600 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "String field");
601 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
602 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
603 return (const char *) string
->buf
->data
;
607 int bt_ctf_field_common_string_clear(struct bt_ctf_field_common
*field
)
609 struct bt_ctf_field_common_string
*string_field
= BT_CTF_FROM_COMMON(field
);
611 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
612 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
613 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
614 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
615 string_field
->size
= 0;
616 bt_ctf_field_common_set(field
, true);
621 int bt_ctf_field_common_string_append_len(struct bt_ctf_field_common
*field
,
622 const char *value
, unsigned int length
)
624 struct bt_ctf_field_common_string
*string_field
= BT_CTF_FROM_COMMON(field
);
628 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
629 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
630 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
631 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
632 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
634 /* Make sure no null bytes are appended */
635 BT_CTF_ASSERT_PRE(!memchr(value
, '\0', length
),
636 "String value to append contains a null character: "
637 "partial-value=\"%.32s\", length=%u", value
, length
);
639 new_size
= string_field
->size
+ length
;
641 if (G_UNLIKELY(new_size
+ 1 > string_field
->buf
->len
)) {
642 g_array_set_size(string_field
->buf
, new_size
+ 1);
645 data
= string_field
->buf
->data
;
646 memcpy(data
+ string_field
->size
, value
, length
);
647 ((char *) string_field
->buf
->data
)[new_size
] = '\0';
648 string_field
->size
= new_size
;
649 bt_ctf_field_common_set(field
, true);
654 int bt_ctf_field_common_string_append(struct bt_ctf_field_common
*field
,
657 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
658 return bt_ctf_field_common_string_append_len(field
, value
,
663 int bt_ctf_field_common_string_set_value(struct bt_ctf_field_common
*field
,
666 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
667 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
668 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
669 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
670 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
671 bt_ctf_field_common_string_clear(field
);
672 return bt_ctf_field_common_string_append_len(field
,
673 value
, strlen(value
));
677 void bt_ctf_field_common_finalize(struct bt_ctf_field_common
*field
)
679 BT_ASSERT_DBG(field
);
680 BT_LOGD_STR("Putting field's type.");
681 bt_ctf_object_put_ref(field
->type
);
685 void bt_ctf_field_common_integer_finalize(struct bt_ctf_field_common
*field
)
687 BT_ASSERT_DBG(field
);
688 BT_LOGD("Finalizing common integer field object: addr=%p", field
);
689 bt_ctf_field_common_finalize(field
);
693 void bt_ctf_field_common_floating_point_finalize(struct bt_ctf_field_common
*field
)
695 BT_ASSERT_DBG(field
);
696 BT_LOGD("Finalizing common floating point number field object: addr=%p", field
);
697 bt_ctf_field_common_finalize(field
);
701 void bt_ctf_field_common_structure_finalize_recursive(struct bt_ctf_field_common
*field
)
703 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
705 BT_ASSERT_DBG(field
);
706 BT_LOGD("Finalizing common structure field object: addr=%p", field
);
707 bt_ctf_field_common_finalize(field
);
709 if (structure
->fields
) {
710 g_ptr_array_free(structure
->fields
, TRUE
);
715 void bt_ctf_field_common_variant_finalize_recursive(struct bt_ctf_field_common
*field
)
717 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
719 BT_ASSERT_DBG(field
);
720 BT_LOGD("Finalizing common variant field object: addr=%p", field
);
721 bt_ctf_field_common_finalize(field
);
723 if (variant
->fields
) {
724 g_ptr_array_free(variant
->fields
, TRUE
);
729 void bt_ctf_field_common_array_finalize_recursive(struct bt_ctf_field_common
*field
)
731 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
733 BT_ASSERT_DBG(field
);
734 BT_LOGD("Finalizing common array field object: addr=%p", field
);
735 bt_ctf_field_common_finalize(field
);
737 if (array
->elements
) {
738 g_ptr_array_free(array
->elements
, TRUE
);
743 void bt_ctf_field_common_sequence_finalize_recursive(struct bt_ctf_field_common
*field
)
745 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
747 BT_ASSERT_DBG(field
);
748 BT_LOGD("Finalizing common sequence field object: addr=%p", field
);
749 bt_ctf_field_common_finalize(field
);
751 if (sequence
->elements
) {
752 g_ptr_array_free(sequence
->elements
, TRUE
);
757 void bt_ctf_field_common_string_finalize(struct bt_ctf_field_common
*field
)
759 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
761 BT_ASSERT_DBG(field
);
762 BT_LOGD("Finalizing common string field object: addr=%p", field
);
763 bt_ctf_field_common_finalize(field
);
766 g_array_free(string
->buf
, TRUE
);
770 BT_CTF_ASSERT_PRE_FUNC
771 static inline bool value_is_in_range_signed(unsigned int size
, int64_t value
)
774 int64_t min_value
, max_value
;
776 min_value
= -(1ULL << (size
- 1));
777 max_value
= (1ULL << (size
- 1)) - 1;
778 if (value
< min_value
|| value
> max_value
) {
779 BT_LOGF("Value is out of bounds: value=%" PRId64
", "
780 "min-value=%" PRId64
", max-value=%" PRId64
,
781 value
, min_value
, max_value
);
788 BT_CTF_ASSERT_PRE_FUNC
789 static inline bool value_is_in_range_unsigned(unsigned int size
, uint64_t value
)
794 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
795 if (value
> max_value
) {
796 BT_LOGF("Value is out of bounds: value=%" PRIu64
", "
797 "max-value=%" PRIu64
,
805 struct bt_ctf_field_enumeration
{
806 struct bt_ctf_field_common common
;
807 struct bt_ctf_field_common_integer
*container
;
810 struct bt_ctf_field_variant
{
811 struct bt_ctf_field_common_variant common
;
812 struct bt_ctf_field_enumeration
*tag
;
816 int bt_ctf_field_serialize_recursive(struct bt_ctf_field
*field
,
817 struct bt_ctfser
*ctfser
,
818 enum bt_ctf_byte_order native_byte_order
);
821 int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field
*field
,
822 const char *name
, struct bt_ctf_field
*value
);
825 struct bt_ctf_field
*bt_ctf_field_enumeration_borrow_container(
826 struct bt_ctf_field
*field
);
829 bt_ctf_bool
bt_ctf_field_is_set_recursive(struct bt_ctf_field
*field
)
831 return bt_ctf_field_common_is_set_recursive((void *) field
);
834 #endif /* BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H */