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
;
145 struct bt_ctf_field_common
*bt_ctf_field_common_copy(struct bt_ctf_field_common
*field
);
147 int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common
*field
,
148 struct bt_ctf_field_type_common
*type
,
149 bool is_shared
, bt_ctf_object_release_func release_func
,
150 struct bt_ctf_field_common_methods
*methods
,
151 bt_ctf_field_common_create_func field_create_func
,
152 GDestroyNotify field_release_func
);
154 int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common
*field
,
155 struct bt_ctf_field_type_common
*type
,
156 bool is_shared
, bt_ctf_object_release_func release_func
,
157 struct bt_ctf_field_common_methods
*methods
,
158 bt_ctf_field_common_create_func field_create_func
,
159 GDestroyNotify field_destroy_func
);
161 int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common
*field
,
162 struct bt_ctf_field_type_common
*type
,
163 bool is_shared
, bt_ctf_object_release_func release_func
,
164 struct bt_ctf_field_common_methods
*methods
,
165 GDestroyNotify field_destroy_func
);
167 int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common
*field
,
168 struct bt_ctf_field_type_common
*type
,
169 bool is_shared
, bt_ctf_object_release_func release_func
,
170 struct bt_ctf_field_common_methods
*methods
,
171 bt_ctf_field_common_create_func field_create_func
,
172 GDestroyNotify field_release_func
);
174 int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common
*field
,
175 struct bt_ctf_field_type_common
*type
,
176 bool is_shared
, bt_ctf_object_release_func release_func
,
177 struct bt_ctf_field_common_methods
*methods
);
179 int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common
*field
);
181 int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common
*field
);
183 int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common
*field
);
185 int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common
*field
);
187 int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common
*field
);
189 void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common
*field
);
191 void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common
*field
);
193 void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common
*field
);
195 void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common
*field
);
197 void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common
*field
);
199 void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common
*field
,
202 void bt_ctf_field_common_structure_set_is_frozen_recursive(
203 struct bt_ctf_field_common
*field
, bool is_frozen
);
205 void bt_ctf_field_common_variant_set_is_frozen_recursive(
206 struct bt_ctf_field_common
*field
, bool is_frozen
);
208 void bt_ctf_field_common_array_set_is_frozen_recursive(
209 struct bt_ctf_field_common
*field
, bool is_frozen
);
211 void bt_ctf_field_common_sequence_set_is_frozen_recursive(
212 struct bt_ctf_field_common
*field
, bool is_frozen
);
214 void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common
*field
,
217 bt_ctf_bool
bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common
*field
);
219 bt_ctf_bool
bt_ctf_field_common_structure_is_set_recursive(
220 struct bt_ctf_field_common
*field
);
222 bt_ctf_bool
bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common
*field
);
224 bt_ctf_bool
bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common
*field
);
226 bt_ctf_bool
bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common
*field
);
229 # define bt_ctf_field_common_validate_recursive _bt_ctf_field_common_validate_recursive
230 # define bt_ctf_field_common_set_is_frozen_recursive _bt_ctf_field_common_set_is_frozen_recursive
231 # define bt_ctf_field_common_is_set_recursive _bt_ctf_field_common_is_set_recursive
232 # define bt_ctf_field_common_reset_recursive _bt_ctf_field_common_reset_recursive
233 # define bt_ctf_field_common_set _bt_ctf_field_common_set
235 # define bt_ctf_field_common_validate_recursive(_field) (-1)
236 # define bt_ctf_field_common_set_is_frozen_recursive(_field, _is_frozen)
237 # define bt_ctf_field_common_is_set_recursive(_field) (BT_CTF_FALSE)
238 # define bt_ctf_field_common_reset_recursive(_field)
239 # define bt_ctf_field_common_set(_field, _val)
243 static inline bool field_type_common_has_known_id(
244 struct bt_ctf_field_type_common
*ft
)
246 return (int) ft
->id
> BT_CTF_FIELD_TYPE_ID_UNKNOWN
&&
247 (int) ft
->id
< BT_CTF_FIELD_TYPE_ID_NR
;
251 int _bt_ctf_field_common_validate_recursive(struct bt_ctf_field_common
*field
)
256 BT_CTF_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
261 BT_ASSERT_DBG(field_type_common_has_known_id(field
->type
));
263 if (field
->methods
->validate
) {
264 ret
= field
->methods
->validate(field
);
272 void _bt_ctf_field_common_reset_recursive(struct bt_ctf_field_common
*field
)
274 BT_ASSERT_DBG(field
);
275 BT_ASSERT_DBG(field
->methods
->reset
);
276 field
->methods
->reset(field
);
280 void _bt_ctf_field_common_set(struct bt_ctf_field_common
*field
, bool value
)
282 BT_ASSERT_DBG(field
);
283 field
->payload_set
= value
;
287 bt_ctf_bool
_bt_ctf_field_common_is_set_recursive(struct bt_ctf_field_common
*field
)
289 bt_ctf_bool is_set
= BT_CTF_FALSE
;
295 BT_ASSERT_DBG(field_type_common_has_known_id(field
->type
));
296 BT_ASSERT_DBG(field
->methods
->is_set
);
297 is_set
= field
->methods
->is_set(field
);
304 void bt_ctf_field_common_initialize(struct bt_ctf_field_common
*field
,
305 struct bt_ctf_field_type_common
*ft
, bool is_shared
,
306 bt_ctf_object_release_func release_func
,
307 struct bt_ctf_field_common_methods
*methods
)
309 BT_ASSERT_DBG(field
);
311 bt_ctf_object_init(&field
->base
, is_shared
, release_func
);
312 field
->methods
= methods
;
313 field
->type
= (void *) bt_ctf_object_get_ref(ft
);
317 struct bt_ctf_field_type_common
*bt_ctf_field_common_borrow_type(
318 struct bt_ctf_field_common
*field
)
320 struct bt_ctf_field_type_common
*ret
= NULL
;
322 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Field");
328 int64_t bt_ctf_field_common_sequence_get_length(struct bt_ctf_field_common
*field
)
330 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
332 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Sequence field");
333 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
335 return (int64_t) sequence
->length
;
339 int bt_ctf_field_common_sequence_set_length(struct bt_ctf_field_common
*field
,
340 uint64_t length
, bt_ctf_field_common_create_func field_create_func
)
343 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
345 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Sequence field");
346 BT_CTF_ASSERT_PRE(((int64_t) length
) >= 0,
347 "Invalid sequence length (too large): length=%" PRId64
,
349 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "Sequence field");
351 if (G_UNLIKELY(length
> sequence
->elements
->len
)) {
353 struct bt_ctf_field_type_common_sequence
*sequence_ft
;
354 uint64_t cur_len
= sequence
->elements
->len
;
357 g_ptr_array_set_size(sequence
->elements
, length
);
358 sequence_ft
= BT_CTF_FROM_COMMON(sequence
->common
.type
);
360 for (i
= cur_len
; i
< sequence
->elements
->len
; i
++) {
361 struct bt_ctf_field_common
*elem_field
=
362 field_create_func(sequence_ft
->element_ft
);
369 BT_ASSERT_DBG(!sequence
->elements
->pdata
[i
]);
370 sequence
->elements
->pdata
[i
] = elem_field
;
374 sequence
->length
= length
;
381 struct bt_ctf_field_common
*bt_ctf_field_common_structure_borrow_field_by_name(
382 struct bt_ctf_field_common
*field
, const char *name
)
384 struct bt_ctf_field_common
*ret
= NULL
;
386 struct bt_ctf_field_type_common_structure
*structure_ft
;
387 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
389 GHashTable
*field_name_to_index
;
391 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Structure field");
392 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Field name");
393 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
394 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Field");
395 structure_ft
= BT_CTF_FROM_COMMON(field
->type
);
396 field_name_to_index
= structure_ft
->field_name_to_index
;
397 field_quark
= g_quark_from_string(name
);
398 if (!g_hash_table_lookup_extended(field_name_to_index
,
399 GUINT_TO_POINTER(field_quark
),
400 NULL
, (gpointer
*) &index
)) {
401 BT_LOGT("Invalid parameter: no such field in structure field's type: "
402 "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"",
403 field
, field
->type
, name
);
407 ret
= structure
->fields
->pdata
[index
];
415 struct bt_ctf_field_common
*bt_ctf_field_common_structure_borrow_field_by_index(
416 struct bt_ctf_field_common
*field
, uint64_t index
)
418 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
420 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Structure field");
421 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
422 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Field");
423 BT_CTF_ASSERT_PRE(index
< structure
->fields
->len
,
424 "Index is out of bound: struct-field-addr=%p, "
425 "index=%" PRIu64
", count=%u", field
, index
,
426 structure
->fields
->len
);
427 return structure
->fields
->pdata
[index
];
431 struct bt_ctf_field_common
*bt_ctf_field_common_array_borrow_field(
432 struct bt_ctf_field_common
*field
, uint64_t index
)
434 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
436 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Array field");
437 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_ARRAY
,
439 BT_CTF_ASSERT_PRE(index
< array
->elements
->len
,
440 "Index is out of bound: array-field-addr=%p, "
441 "index=%" PRIu64
", count=%u", field
,
442 index
, array
->elements
->len
);
443 return array
->elements
->pdata
[(size_t) index
];
447 struct bt_ctf_field_common
*bt_ctf_field_common_sequence_borrow_field(
448 struct bt_ctf_field_common
*field
, uint64_t index
)
450 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
452 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Sequence field");
453 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
455 BT_CTF_ASSERT_PRE(index
< sequence
->length
,
456 "Index is out of bound: seq-field-addr=%p, "
457 "index=%" PRIu64
", count=%u", field
, index
,
458 sequence
->elements
->len
);
459 return sequence
->elements
->pdata
[(size_t) index
];
463 int bt_ctf_field_common_variant_set_tag(struct bt_ctf_field_common
*variant_field
,
464 uint64_t tag_uval
, bool is_signed
)
467 int64_t choice_index
;
468 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
470 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
471 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
472 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
474 /* Find matching index in variant field's type */
475 choice_index
= bt_ctf_field_type_common_variant_find_choice_index(
476 variant_field
->type
, tag_uval
, is_signed
);
477 if (choice_index
< 0) {
482 /* Select corresponding field */
483 BT_ASSERT_DBG(choice_index
< variant
->fields
->len
);
484 variant
->current_field
= variant
->fields
->pdata
[choice_index
];
485 variant
->tag_value
.u
= tag_uval
;
492 struct bt_ctf_field_common
*bt_ctf_field_common_variant_borrow_current_field(
493 struct bt_ctf_field_common
*variant_field
)
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");
500 BT_CTF_ASSERT_PRE(variant
->current_field
,
501 "Variant field has no current field: field-addr=%p", variant_field
);
502 return variant
->current_field
;
506 int bt_ctf_field_common_variant_get_tag_signed(struct bt_ctf_field_common
*variant_field
,
509 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
511 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
512 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
513 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
514 BT_CTF_ASSERT_PRE(variant
->current_field
,
515 "Variant field has no current field: field-addr=%p", variant_field
);
516 *tag
= variant
->tag_value
.i
;
521 int bt_ctf_field_common_variant_get_tag_unsigned(struct bt_ctf_field_common
*variant_field
,
524 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
526 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
527 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
528 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
529 BT_CTF_ASSERT_PRE(variant
->current_field
,
530 "Variant field has no current field: field-addr=%p", variant_field
);
531 *tag
= variant
->tag_value
.u
;
536 int bt_ctf_field_common_floating_point_get_value(struct bt_ctf_field_common
*field
,
539 struct bt_ctf_field_common_floating_point
*floating_point
=
540 BT_CTF_FROM_COMMON(field
);
542 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
543 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
544 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "Floating point number field");
545 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
546 BT_CTF_FIELD_TYPE_ID_FLOAT
, "Field");
547 *value
= floating_point
->payload
;
552 int bt_ctf_field_common_floating_point_set_value(struct bt_ctf_field_common
*field
,
555 struct bt_ctf_field_common_floating_point
*floating_point
=
556 BT_CTF_FROM_COMMON(field
);
558 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
559 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "Floating point number field");
560 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
561 BT_CTF_FIELD_TYPE_ID_FLOAT
, "Field");
562 floating_point
->payload
= value
;
563 bt_ctf_field_common_set(field
, true);
568 const char *bt_ctf_field_common_string_get_value(struct bt_ctf_field_common
*field
)
570 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
572 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
573 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "String field");
574 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
575 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
576 return (const char *) string
->buf
->data
;
580 int bt_ctf_field_common_string_clear(struct bt_ctf_field_common
*field
)
582 struct bt_ctf_field_common_string
*string_field
= BT_CTF_FROM_COMMON(field
);
584 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
585 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
586 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
587 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
588 string_field
->size
= 0;
589 bt_ctf_field_common_set(field
, true);
594 int bt_ctf_field_common_string_append_len(struct bt_ctf_field_common
*field
,
595 const char *value
, unsigned int length
)
597 struct bt_ctf_field_common_string
*string_field
= BT_CTF_FROM_COMMON(field
);
601 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
602 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
603 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
604 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
605 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
607 /* Make sure no null bytes are appended */
608 BT_CTF_ASSERT_PRE(!memchr(value
, '\0', length
),
609 "String value to append contains a null character: "
610 "partial-value=\"%.32s\", length=%u", value
, length
);
612 new_size
= string_field
->size
+ length
;
614 if (G_UNLIKELY(new_size
+ 1 > string_field
->buf
->len
)) {
615 g_array_set_size(string_field
->buf
, new_size
+ 1);
618 data
= string_field
->buf
->data
;
619 memcpy(data
+ string_field
->size
, value
, length
);
620 ((char *) string_field
->buf
->data
)[new_size
] = '\0';
621 string_field
->size
= new_size
;
622 bt_ctf_field_common_set(field
, true);
627 int bt_ctf_field_common_string_append(struct bt_ctf_field_common
*field
,
630 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
631 return bt_ctf_field_common_string_append_len(field
, value
,
636 int bt_ctf_field_common_string_set_value(struct bt_ctf_field_common
*field
,
639 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
640 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
641 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
642 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
643 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
644 bt_ctf_field_common_string_clear(field
);
645 return bt_ctf_field_common_string_append_len(field
,
646 value
, strlen(value
));
650 void bt_ctf_field_common_finalize(struct bt_ctf_field_common
*field
)
652 BT_ASSERT_DBG(field
);
653 BT_LOGD_STR("Putting field's type.");
654 bt_ctf_object_put_ref(field
->type
);
658 void bt_ctf_field_common_integer_finalize(struct bt_ctf_field_common
*field
)
660 BT_ASSERT_DBG(field
);
661 BT_LOGD("Finalizing common integer field object: addr=%p", field
);
662 bt_ctf_field_common_finalize(field
);
666 void bt_ctf_field_common_floating_point_finalize(struct bt_ctf_field_common
*field
)
668 BT_ASSERT_DBG(field
);
669 BT_LOGD("Finalizing common floating point number field object: addr=%p", field
);
670 bt_ctf_field_common_finalize(field
);
674 void bt_ctf_field_common_structure_finalize_recursive(struct bt_ctf_field_common
*field
)
676 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
678 BT_ASSERT_DBG(field
);
679 BT_LOGD("Finalizing common structure field object: addr=%p", field
);
680 bt_ctf_field_common_finalize(field
);
682 if (structure
->fields
) {
683 g_ptr_array_free(structure
->fields
, TRUE
);
688 void bt_ctf_field_common_variant_finalize_recursive(struct bt_ctf_field_common
*field
)
690 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
692 BT_ASSERT_DBG(field
);
693 BT_LOGD("Finalizing common variant field object: addr=%p", field
);
694 bt_ctf_field_common_finalize(field
);
696 if (variant
->fields
) {
697 g_ptr_array_free(variant
->fields
, TRUE
);
702 void bt_ctf_field_common_array_finalize_recursive(struct bt_ctf_field_common
*field
)
704 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
706 BT_ASSERT_DBG(field
);
707 BT_LOGD("Finalizing common array field object: addr=%p", field
);
708 bt_ctf_field_common_finalize(field
);
710 if (array
->elements
) {
711 g_ptr_array_free(array
->elements
, TRUE
);
716 void bt_ctf_field_common_sequence_finalize_recursive(struct bt_ctf_field_common
*field
)
718 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
720 BT_ASSERT_DBG(field
);
721 BT_LOGD("Finalizing common sequence field object: addr=%p", field
);
722 bt_ctf_field_common_finalize(field
);
724 if (sequence
->elements
) {
725 g_ptr_array_free(sequence
->elements
, TRUE
);
730 void bt_ctf_field_common_string_finalize(struct bt_ctf_field_common
*field
)
732 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
734 BT_ASSERT_DBG(field
);
735 BT_LOGD("Finalizing common string field object: addr=%p", field
);
736 bt_ctf_field_common_finalize(field
);
739 g_array_free(string
->buf
, TRUE
);
743 BT_CTF_ASSERT_PRE_FUNC
744 static inline bool value_is_in_range_signed(unsigned int size
, int64_t value
)
747 int64_t min_value
, max_value
;
749 min_value
= -(1ULL << (size
- 1));
750 max_value
= (1ULL << (size
- 1)) - 1;
751 if (value
< min_value
|| value
> max_value
) {
752 BT_LOGF("Value is out of bounds: value=%" PRId64
", "
753 "min-value=%" PRId64
", max-value=%" PRId64
,
754 value
, min_value
, max_value
);
761 BT_CTF_ASSERT_PRE_FUNC
762 static inline bool value_is_in_range_unsigned(unsigned int size
, uint64_t value
)
767 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
768 if (value
> max_value
) {
769 BT_LOGF("Value is out of bounds: value=%" PRIu64
", "
770 "max-value=%" PRIu64
,
778 struct bt_ctf_field_enumeration
{
779 struct bt_ctf_field_common common
;
780 struct bt_ctf_field_common_integer
*container
;
783 struct bt_ctf_field_variant
{
784 struct bt_ctf_field_common_variant common
;
785 struct bt_ctf_field_enumeration
*tag
;
788 int bt_ctf_field_serialize_recursive(struct bt_ctf_field
*field
,
789 struct bt_ctfser
*ctfser
,
790 enum bt_ctf_byte_order native_byte_order
);
792 int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field
*field
,
793 const char *name
, struct bt_ctf_field
*value
);
795 struct bt_ctf_field
*bt_ctf_field_enumeration_borrow_container(
796 struct bt_ctf_field
*field
);
799 #define BT_FIELD_UNUSED_ATTR __attribute__((unused))
801 #define BT_FIELD_UNUSED_ATTR
805 bt_ctf_bool
bt_ctf_field_is_set_recursive(
806 struct bt_ctf_field
*field BT_FIELD_UNUSED_ATTR
)
808 return bt_ctf_field_common_is_set_recursive((void *) field
);
811 #endif /* BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H */