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>
22 #include "common/macros.h"
23 #include "common/common.h"
24 #include "ctfser/ctfser.h"
26 #include "assert-pre.h"
27 #include "field-types.h"
31 #define BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(_field, _type_id, _name) \
32 BT_CTF_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
33 _name " has the wrong type ID: expected-type-id=%s, " \
35 bt_ctf_field_type_id_string((int) (_type_id)), (_field))
37 #define BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(_field, _name) \
38 BT_CTF_ASSERT_PRE(bt_ctf_field_common_is_set_recursive(_field), \
39 _name " is not set: field-addr=%p", (_field))
41 #define BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(_field, _name) \
42 BT_CTF_ASSERT_PRE_HOT((_field), (_name), ": field-addr=%p", (_field))
44 struct bt_ctf_field_common
;
46 typedef void (*bt_ctf_field_common_method_set_is_frozen
)(struct bt_ctf_field_common
*,
48 typedef int (*bt_ctf_field_common_method_validate
)(struct bt_ctf_field_common
*);
49 typedef struct bt_ctf_field_common
*(*bt_ctf_field_common_method_copy
)(
50 struct bt_ctf_field_common
*);
51 typedef bt_ctf_bool (*bt_ctf_field_common_method_is_set
)(struct bt_ctf_field_common
*);
52 typedef void (*bt_ctf_field_common_method_reset
)(struct bt_ctf_field_common
*);
54 struct bt_ctf_field_common_methods
{
55 bt_ctf_field_common_method_set_is_frozen set_is_frozen
;
56 bt_ctf_field_common_method_validate validate
;
57 bt_ctf_field_common_method_copy copy
;
58 bt_ctf_field_common_method_is_set is_set
;
59 bt_ctf_field_common_method_reset reset
;
62 struct bt_ctf_field_common
{
63 struct bt_ctf_object base
;
64 struct bt_ctf_field_type_common
*type
;
65 struct bt_ctf_field_common_methods
*methods
;
70 * Specialized data for either CTF IR or CTF writer APIs.
71 * See comment in `field-types.h` for more details.
82 struct bt_ctf_field_common_integer
{
83 struct bt_ctf_field_common common
;
90 struct bt_ctf_field_common_floating_point
{
91 struct bt_ctf_field_common common
;
95 struct bt_ctf_field_common_structure
{
96 struct bt_ctf_field_common common
;
98 /* Array of `struct bt_ctf_field_common *`, owned by this */
102 struct bt_ctf_field_common_variant
{
103 struct bt_ctf_field_common common
;
110 /* Weak: belongs to `choices` below */
111 struct bt_ctf_field_common
*current_field
;
113 /* Array of `struct bt_ctf_field_common *`, owned by this */
117 struct bt_ctf_field_common_array
{
118 struct bt_ctf_field_common common
;
120 /* Array of `struct bt_ctf_field_common *`, owned by this */
124 struct bt_ctf_field_common_sequence
{
125 struct bt_ctf_field_common common
;
128 * This is the true sequence field's length: its value can be
129 * less than `elements->len` below because we never shrink the
130 * array of elements to avoid reallocation.
134 /* Array of `struct bt_ctf_field_common *`, owned by this */
138 struct bt_ctf_field_common_string
{
139 struct bt_ctf_field_common common
;
144 struct bt_ctf_field_common
*bt_ctf_field_common_copy(struct bt_ctf_field_common
*field
);
146 int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common
*field
,
147 struct bt_ctf_field_type_common
*type
,
148 bool is_shared
, bt_ctf_object_release_func release_func
,
149 struct bt_ctf_field_common_methods
*methods
,
150 bt_ctf_field_common_create_func field_create_func
,
151 GDestroyNotify field_release_func
);
153 int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common
*field
,
154 struct bt_ctf_field_type_common
*type
,
155 bool is_shared
, bt_ctf_object_release_func release_func
,
156 struct bt_ctf_field_common_methods
*methods
,
157 bt_ctf_field_common_create_func field_create_func
,
158 GDestroyNotify field_destroy_func
);
160 int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common
*field
,
161 struct bt_ctf_field_type_common
*type
,
162 bool is_shared
, bt_ctf_object_release_func release_func
,
163 struct bt_ctf_field_common_methods
*methods
,
164 GDestroyNotify field_destroy_func
);
166 int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common
*field
,
167 struct bt_ctf_field_type_common
*type
,
168 bool is_shared
, bt_ctf_object_release_func release_func
,
169 struct bt_ctf_field_common_methods
*methods
,
170 bt_ctf_field_common_create_func field_create_func
,
171 GDestroyNotify field_release_func
);
173 int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common
*field
,
174 struct bt_ctf_field_type_common
*type
,
175 bool is_shared
, bt_ctf_object_release_func release_func
,
176 struct bt_ctf_field_common_methods
*methods
);
178 int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common
*field
);
180 int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common
*field
);
182 int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common
*field
);
184 int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common
*field
);
186 int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common
*field
);
188 void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common
*field
);
190 void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common
*field
);
192 void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common
*field
);
194 void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common
*field
);
196 void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common
*field
);
198 void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common
*field
,
201 void bt_ctf_field_common_structure_set_is_frozen_recursive(
202 struct bt_ctf_field_common
*field
, bool is_frozen
);
204 void bt_ctf_field_common_variant_set_is_frozen_recursive(
205 struct bt_ctf_field_common
*field
, bool is_frozen
);
207 void bt_ctf_field_common_array_set_is_frozen_recursive(
208 struct bt_ctf_field_common
*field
, bool is_frozen
);
210 void bt_ctf_field_common_sequence_set_is_frozen_recursive(
211 struct bt_ctf_field_common
*field
, bool is_frozen
);
213 void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common
*field
,
216 bt_ctf_bool
bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common
*field
);
218 bt_ctf_bool
bt_ctf_field_common_structure_is_set_recursive(
219 struct bt_ctf_field_common
*field
);
221 bt_ctf_bool
bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common
*field
);
223 bt_ctf_bool
bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common
*field
);
225 bt_ctf_bool
bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common
*field
);
228 # define bt_ctf_field_common_validate_recursive _bt_ctf_field_common_validate_recursive
229 # define bt_ctf_field_common_set_is_frozen_recursive _bt_ctf_field_common_set_is_frozen_recursive
230 # define bt_ctf_field_common_is_set_recursive _bt_ctf_field_common_is_set_recursive
231 # define bt_ctf_field_common_reset_recursive _bt_ctf_field_common_reset_recursive
232 # define bt_ctf_field_common_set _bt_ctf_field_common_set
234 # define bt_ctf_field_common_validate_recursive(_field) (-1)
235 # define bt_ctf_field_common_set_is_frozen_recursive(_field, _is_frozen)
236 # define bt_ctf_field_common_is_set_recursive(_field) (BT_CTF_FALSE)
237 # define bt_ctf_field_common_reset_recursive(_field)
238 # define bt_ctf_field_common_set(_field, _val)
242 static inline bool field_type_common_has_known_id(
243 struct bt_ctf_field_type_common
*ft
)
245 return (int) ft
->id
> BT_CTF_FIELD_TYPE_ID_UNKNOWN
&&
246 (int) ft
->id
< BT_CTF_FIELD_TYPE_ID_NR
;
250 int _bt_ctf_field_common_validate_recursive(struct bt_ctf_field_common
*field
)
255 BT_CTF_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
260 BT_ASSERT_DBG(field_type_common_has_known_id(field
->type
));
262 if (field
->methods
->validate
) {
263 ret
= field
->methods
->validate(field
);
271 void _bt_ctf_field_common_reset_recursive(struct bt_ctf_field_common
*field
)
273 BT_ASSERT_DBG(field
);
274 BT_ASSERT_DBG(field
->methods
->reset
);
275 field
->methods
->reset(field
);
279 void _bt_ctf_field_common_set(struct bt_ctf_field_common
*field
, bool value
)
281 BT_ASSERT_DBG(field
);
282 field
->payload_set
= value
;
286 bt_ctf_bool
_bt_ctf_field_common_is_set_recursive(struct bt_ctf_field_common
*field
)
288 bt_ctf_bool is_set
= BT_CTF_FALSE
;
294 BT_ASSERT_DBG(field_type_common_has_known_id(field
->type
));
295 BT_ASSERT_DBG(field
->methods
->is_set
);
296 is_set
= field
->methods
->is_set(field
);
303 void bt_ctf_field_common_initialize(struct bt_ctf_field_common
*field
,
304 struct bt_ctf_field_type_common
*ft
, bool is_shared
,
305 bt_ctf_object_release_func release_func
,
306 struct bt_ctf_field_common_methods
*methods
)
308 BT_ASSERT_DBG(field
);
310 bt_ctf_object_init(&field
->base
, is_shared
, release_func
);
311 field
->methods
= methods
;
312 field
->type
= (void *) bt_ctf_object_get_ref(ft
);
316 struct bt_ctf_field_type_common
*bt_ctf_field_common_borrow_type(
317 struct bt_ctf_field_common
*field
)
319 struct bt_ctf_field_type_common
*ret
= NULL
;
321 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Field");
327 int64_t bt_ctf_field_common_sequence_get_length(struct bt_ctf_field_common
*field
)
329 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
331 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Sequence field");
332 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
334 return (int64_t) sequence
->length
;
338 int bt_ctf_field_common_sequence_set_length(struct bt_ctf_field_common
*field
,
339 uint64_t length
, bt_ctf_field_common_create_func field_create_func
)
342 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
344 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Sequence field");
345 BT_CTF_ASSERT_PRE(((int64_t) length
) >= 0,
346 "Invalid sequence length (too large): length=%" PRId64
,
348 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "Sequence field");
350 if (G_UNLIKELY(length
> sequence
->elements
->len
)) {
352 struct bt_ctf_field_type_common_sequence
*sequence_ft
;
353 uint64_t cur_len
= sequence
->elements
->len
;
356 g_ptr_array_set_size(sequence
->elements
, length
);
357 sequence_ft
= BT_CTF_FROM_COMMON(sequence
->common
.type
);
359 for (i
= cur_len
; i
< sequence
->elements
->len
; i
++) {
360 struct bt_ctf_field_common
*elem_field
=
361 field_create_func(sequence_ft
->element_ft
);
368 BT_ASSERT_DBG(!sequence
->elements
->pdata
[i
]);
369 sequence
->elements
->pdata
[i
] = elem_field
;
373 sequence
->length
= length
;
380 struct bt_ctf_field_common
*bt_ctf_field_common_structure_borrow_field_by_name(
381 struct bt_ctf_field_common
*field
, const char *name
)
383 struct bt_ctf_field_common
*ret
= NULL
;
385 struct bt_ctf_field_type_common_structure
*structure_ft
;
386 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
388 GHashTable
*field_name_to_index
;
390 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Structure field");
391 BT_CTF_ASSERT_PRE_NON_NULL(name
, "Field name");
392 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
393 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Field");
394 structure_ft
= BT_CTF_FROM_COMMON(field
->type
);
395 field_name_to_index
= structure_ft
->field_name_to_index
;
396 field_quark
= g_quark_from_string(name
);
397 if (!g_hash_table_lookup_extended(field_name_to_index
,
398 GUINT_TO_POINTER(field_quark
),
399 NULL
, (gpointer
*) &index
)) {
400 BT_LOGT("Invalid parameter: no such field in structure field's type: "
401 "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"",
402 field
, field
->type
, name
);
406 ret
= structure
->fields
->pdata
[index
];
414 struct bt_ctf_field_common
*bt_ctf_field_common_structure_borrow_field_by_index(
415 struct bt_ctf_field_common
*field
, uint64_t index
)
417 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
419 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Structure field");
420 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
421 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Field");
422 BT_CTF_ASSERT_PRE(index
< structure
->fields
->len
,
423 "Index is out of bound: struct-field-addr=%p, "
424 "index=%" PRIu64
", count=%u", field
, index
,
425 structure
->fields
->len
);
426 return structure
->fields
->pdata
[index
];
430 struct bt_ctf_field_common
*bt_ctf_field_common_array_borrow_field(
431 struct bt_ctf_field_common
*field
, uint64_t index
)
433 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
435 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Array field");
436 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_ARRAY
,
438 BT_CTF_ASSERT_PRE(index
< array
->elements
->len
,
439 "Index is out of bound: array-field-addr=%p, "
440 "index=%" PRIu64
", count=%u", field
,
441 index
, array
->elements
->len
);
442 return array
->elements
->pdata
[(size_t) index
];
446 struct bt_ctf_field_common
*bt_ctf_field_common_sequence_borrow_field(
447 struct bt_ctf_field_common
*field
, uint64_t index
)
449 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
451 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Sequence field");
452 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
454 BT_CTF_ASSERT_PRE(index
< sequence
->length
,
455 "Index is out of bound: seq-field-addr=%p, "
456 "index=%" PRIu64
", count=%u", field
, index
,
457 sequence
->elements
->len
);
458 return sequence
->elements
->pdata
[(size_t) index
];
462 int bt_ctf_field_common_variant_set_tag(struct bt_ctf_field_common
*variant_field
,
463 uint64_t tag_uval
, bool is_signed
)
466 int64_t choice_index
;
467 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
469 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
470 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
471 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
473 /* Find matching index in variant field's type */
474 choice_index
= bt_ctf_field_type_common_variant_find_choice_index(
475 variant_field
->type
, tag_uval
, is_signed
);
476 if (choice_index
< 0) {
481 /* Select corresponding field */
482 BT_ASSERT_DBG(choice_index
< variant
->fields
->len
);
483 variant
->current_field
= variant
->fields
->pdata
[choice_index
];
484 variant
->tag_value
.u
= tag_uval
;
491 struct bt_ctf_field_common
*bt_ctf_field_common_variant_borrow_current_field(
492 struct bt_ctf_field_common
*variant_field
)
494 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
496 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
497 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
498 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
499 BT_CTF_ASSERT_PRE(variant
->current_field
,
500 "Variant field has no current field: field-addr=%p", variant_field
);
501 return variant
->current_field
;
505 int bt_ctf_field_common_variant_get_tag_signed(struct bt_ctf_field_common
*variant_field
,
508 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
510 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
511 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
512 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
513 BT_CTF_ASSERT_PRE(variant
->current_field
,
514 "Variant field has no current field: field-addr=%p", variant_field
);
515 *tag
= variant
->tag_value
.i
;
520 int bt_ctf_field_common_variant_get_tag_unsigned(struct bt_ctf_field_common
*variant_field
,
523 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
525 BT_CTF_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
526 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
527 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
528 BT_CTF_ASSERT_PRE(variant
->current_field
,
529 "Variant field has no current field: field-addr=%p", variant_field
);
530 *tag
= variant
->tag_value
.u
;
535 int bt_ctf_field_common_floating_point_get_value(struct bt_ctf_field_common
*field
,
538 struct bt_ctf_field_common_floating_point
*floating_point
=
539 BT_CTF_FROM_COMMON(field
);
541 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
542 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
543 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "Floating point number field");
544 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
545 BT_CTF_FIELD_TYPE_ID_FLOAT
, "Field");
546 *value
= floating_point
->payload
;
551 int bt_ctf_field_common_floating_point_set_value(struct bt_ctf_field_common
*field
,
554 struct bt_ctf_field_common_floating_point
*floating_point
=
555 BT_CTF_FROM_COMMON(field
);
557 BT_CTF_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
558 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "Floating point number field");
559 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
560 BT_CTF_FIELD_TYPE_ID_FLOAT
, "Field");
561 floating_point
->payload
= value
;
562 bt_ctf_field_common_set(field
, true);
567 const char *bt_ctf_field_common_string_get_value(struct bt_ctf_field_common
*field
)
569 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
571 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
572 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "String field");
573 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
574 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
575 return (const char *) string
->buf
->data
;
579 int bt_ctf_field_common_string_clear(struct bt_ctf_field_common
*field
)
581 struct bt_ctf_field_common_string
*string_field
= BT_CTF_FROM_COMMON(field
);
583 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
584 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
585 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
586 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
587 string_field
->size
= 0;
588 bt_ctf_field_common_set(field
, true);
593 int bt_ctf_field_common_string_append_len(struct bt_ctf_field_common
*field
,
594 const char *value
, unsigned int length
)
596 struct bt_ctf_field_common_string
*string_field
= BT_CTF_FROM_COMMON(field
);
600 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
601 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
602 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
603 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
604 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
606 /* Make sure no null bytes are appended */
607 BT_CTF_ASSERT_PRE(!memchr(value
, '\0', length
),
608 "String value to append contains a null character: "
609 "partial-value=\"%.32s\", length=%u", value
, length
);
611 new_size
= string_field
->size
+ length
;
613 if (G_UNLIKELY(new_size
+ 1 > string_field
->buf
->len
)) {
614 g_array_set_size(string_field
->buf
, new_size
+ 1);
617 data
= string_field
->buf
->data
;
618 memcpy(data
+ string_field
->size
, value
, length
);
619 ((char *) string_field
->buf
->data
)[new_size
] = '\0';
620 string_field
->size
= new_size
;
621 bt_ctf_field_common_set(field
, true);
626 int bt_ctf_field_common_string_append(struct bt_ctf_field_common
*field
,
629 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
630 return bt_ctf_field_common_string_append_len(field
, value
,
635 int bt_ctf_field_common_string_set_value(struct bt_ctf_field_common
*field
,
638 BT_CTF_ASSERT_PRE_NON_NULL(field
, "String field");
639 BT_CTF_ASSERT_PRE_NON_NULL(value
, "Value");
640 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
641 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
642 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
643 bt_ctf_field_common_string_clear(field
);
644 return bt_ctf_field_common_string_append_len(field
,
645 value
, strlen(value
));
649 void bt_ctf_field_common_finalize(struct bt_ctf_field_common
*field
)
651 BT_ASSERT_DBG(field
);
652 BT_LOGD_STR("Putting field's type.");
653 bt_ctf_object_put_ref(field
->type
);
657 void bt_ctf_field_common_integer_finalize(struct bt_ctf_field_common
*field
)
659 BT_ASSERT_DBG(field
);
660 BT_LOGD("Finalizing common integer field object: addr=%p", field
);
661 bt_ctf_field_common_finalize(field
);
665 void bt_ctf_field_common_floating_point_finalize(struct bt_ctf_field_common
*field
)
667 BT_ASSERT_DBG(field
);
668 BT_LOGD("Finalizing common floating point number field object: addr=%p", field
);
669 bt_ctf_field_common_finalize(field
);
673 void bt_ctf_field_common_structure_finalize_recursive(struct bt_ctf_field_common
*field
)
675 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
677 BT_ASSERT_DBG(field
);
678 BT_LOGD("Finalizing common structure field object: addr=%p", field
);
679 bt_ctf_field_common_finalize(field
);
681 if (structure
->fields
) {
682 g_ptr_array_free(structure
->fields
, TRUE
);
687 void bt_ctf_field_common_variant_finalize_recursive(struct bt_ctf_field_common
*field
)
689 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
691 BT_ASSERT_DBG(field
);
692 BT_LOGD("Finalizing common variant field object: addr=%p", field
);
693 bt_ctf_field_common_finalize(field
);
695 if (variant
->fields
) {
696 g_ptr_array_free(variant
->fields
, TRUE
);
701 void bt_ctf_field_common_array_finalize_recursive(struct bt_ctf_field_common
*field
)
703 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
705 BT_ASSERT_DBG(field
);
706 BT_LOGD("Finalizing common array field object: addr=%p", field
);
707 bt_ctf_field_common_finalize(field
);
709 if (array
->elements
) {
710 g_ptr_array_free(array
->elements
, TRUE
);
715 void bt_ctf_field_common_sequence_finalize_recursive(struct bt_ctf_field_common
*field
)
717 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
719 BT_ASSERT_DBG(field
);
720 BT_LOGD("Finalizing common sequence field object: addr=%p", field
);
721 bt_ctf_field_common_finalize(field
);
723 if (sequence
->elements
) {
724 g_ptr_array_free(sequence
->elements
, TRUE
);
729 void bt_ctf_field_common_string_finalize(struct bt_ctf_field_common
*field
)
731 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
733 BT_ASSERT_DBG(field
);
734 BT_LOGD("Finalizing common string field object: addr=%p", field
);
735 bt_ctf_field_common_finalize(field
);
738 g_array_free(string
->buf
, TRUE
);
742 BT_CTF_ASSERT_PRE_FUNC
743 static inline bool value_is_in_range_signed(unsigned int size
, int64_t value
)
746 int64_t min_value
, max_value
;
748 min_value
= -(1ULL << (size
- 1));
749 max_value
= (1ULL << (size
- 1)) - 1;
750 if (value
< min_value
|| value
> max_value
) {
751 BT_LOGF("Value is out of bounds: value=%" PRId64
", "
752 "min-value=%" PRId64
", max-value=%" PRId64
,
753 value
, min_value
, max_value
);
760 BT_CTF_ASSERT_PRE_FUNC
761 static inline bool value_is_in_range_unsigned(unsigned int size
, uint64_t value
)
766 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
767 if (value
> max_value
) {
768 BT_LOGF("Value is out of bounds: value=%" PRIu64
", "
769 "max-value=%" PRIu64
,
777 struct bt_ctf_field_enumeration
{
778 struct bt_ctf_field_common common
;
779 struct bt_ctf_field_common_integer
*container
;
782 struct bt_ctf_field_variant
{
783 struct bt_ctf_field_common_variant common
;
784 struct bt_ctf_field_enumeration
*tag
;
787 int bt_ctf_field_serialize_recursive(struct bt_ctf_field
*field
,
788 struct bt_ctfser
*ctfser
,
789 enum bt_ctf_byte_order native_byte_order
);
791 int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field
*field
,
792 const char *name
, struct bt_ctf_field
*value
);
794 struct bt_ctf_field
*bt_ctf_field_enumeration_borrow_container(
795 struct bt_ctf_field
*field
);
798 #define BT_FIELD_UNUSED_ATTR __attribute__((unused))
800 #define BT_FIELD_UNUSED_ATTR
804 bt_ctf_bool
bt_ctf_field_is_set_recursive(
805 struct bt_ctf_field
*field BT_FIELD_UNUSED_ATTR
)
807 return bt_ctf_field_common_is_set_recursive((void *) field
);
810 #endif /* BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H */