1 #ifndef BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
2 #define BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * The Common Trace Format (CTF) Specification is available at
28 * http://www.efficios.com/ctf
34 #include <babeltrace/assert-pre-internal.h>
35 #include <babeltrace/babeltrace-internal.h>
36 #include <babeltrace/common-internal.h>
37 #include <babeltrace/ctf-writer/field-types-internal.h>
38 #include <babeltrace/ctf-writer/fields.h>
39 #include <babeltrace/ctf-writer/utils-internal.h>
40 #include <babeltrace/ctf-writer/object-internal.h>
41 #include <babeltrace/ctfser-internal.h>
42 #include <babeltrace/types.h>
49 #define BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(_field, _type_id, _name) \
50 BT_ASSERT_PRE((_field)->type->id == ((int) (_type_id)), \
51 _name " has the wrong type ID: expected-type-id=%s, " \
53 bt_ctf_field_type_id_string((int) (_type_id)), (_field))
55 #define BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(_field, _name) \
56 BT_ASSERT_PRE(bt_ctf_field_common_is_set_recursive(_field), \
57 _name " is not set: field-addr=%p", (_field))
59 #define BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(_field, _name) \
60 BT_ASSERT_PRE_HOT((_field), (_name), ": field-addr=%p", (_field))
62 struct bt_ctf_field_common
;
64 typedef void (*bt_ctf_field_common_method_set_is_frozen
)(struct bt_ctf_field_common
*,
66 typedef int (*bt_ctf_field_common_method_validate
)(struct bt_ctf_field_common
*);
67 typedef struct bt_ctf_field_common
*(*bt_ctf_field_common_method_copy
)(
68 struct bt_ctf_field_common
*);
69 typedef bt_bool (*bt_ctf_field_common_method_is_set
)(struct bt_ctf_field_common
*);
70 typedef void (*bt_ctf_field_common_method_reset
)(struct bt_ctf_field_common
*);
72 struct bt_ctf_field_common_methods
{
73 bt_ctf_field_common_method_set_is_frozen set_is_frozen
;
74 bt_ctf_field_common_method_validate validate
;
75 bt_ctf_field_common_method_copy copy
;
76 bt_ctf_field_common_method_is_set is_set
;
77 bt_ctf_field_common_method_reset reset
;
80 struct bt_ctf_field_common
{
81 struct bt_ctf_object base
;
82 struct bt_ctf_field_type_common
*type
;
83 struct bt_ctf_field_common_methods
*methods
;
88 * Specialized data for either CTF IR or CTF writer APIs.
89 * See comment in `field-types-internal.h` for more details.
100 struct bt_ctf_field_common_integer
{
101 struct bt_ctf_field_common common
;
108 struct bt_ctf_field_common_floating_point
{
109 struct bt_ctf_field_common common
;
113 struct bt_ctf_field_common_structure
{
114 struct bt_ctf_field_common common
;
116 /* Array of `struct bt_ctf_field_common *`, owned by this */
120 struct bt_ctf_field_common_variant
{
121 struct bt_ctf_field_common common
;
128 /* Weak: belongs to `choices` below */
129 struct bt_ctf_field_common
*current_field
;
131 /* Array of `struct bt_ctf_field_common *`, owned by this */
135 struct bt_ctf_field_common_array
{
136 struct bt_ctf_field_common common
;
138 /* Array of `struct bt_ctf_field_common *`, owned by this */
142 struct bt_ctf_field_common_sequence
{
143 struct bt_ctf_field_common common
;
146 * This is the true sequence field's length: its value can be
147 * less than `elements->len` below because we never shrink the
148 * array of elements to avoid reallocation.
152 /* Array of `struct bt_ctf_field_common *`, owned by this */
156 struct bt_ctf_field_common_string
{
157 struct bt_ctf_field_common common
;
163 struct bt_ctf_field_common
*bt_ctf_field_common_copy(struct bt_ctf_field_common
*field
);
166 int bt_ctf_field_common_structure_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
);
174 int bt_ctf_field_common_array_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
,
178 bt_ctf_field_common_create_func field_create_func
,
179 GDestroyNotify field_destroy_func
);
182 int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common
*field
,
183 struct bt_ctf_field_type_common
*type
,
184 bool is_shared
, bt_ctf_object_release_func release_func
,
185 struct bt_ctf_field_common_methods
*methods
,
186 GDestroyNotify field_destroy_func
);
189 int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common
*field
,
190 struct bt_ctf_field_type_common
*type
,
191 bool is_shared
, bt_ctf_object_release_func release_func
,
192 struct bt_ctf_field_common_methods
*methods
,
193 bt_ctf_field_common_create_func field_create_func
,
194 GDestroyNotify field_release_func
);
197 int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common
*field
,
198 struct bt_ctf_field_type_common
*type
,
199 bool is_shared
, bt_ctf_object_release_func release_func
,
200 struct bt_ctf_field_common_methods
*methods
);
203 int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common
*field
);
206 int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common
*field
);
209 int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common
*field
);
212 int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common
*field
);
215 int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common
*field
);
218 void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common
*field
);
221 void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common
*field
);
224 void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common
*field
);
227 void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common
*field
);
230 void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common
*field
);
233 void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common
*field
,
237 void bt_ctf_field_common_structure_set_is_frozen_recursive(
238 struct bt_ctf_field_common
*field
, bool is_frozen
);
241 void bt_ctf_field_common_variant_set_is_frozen_recursive(
242 struct bt_ctf_field_common
*field
, bool is_frozen
);
245 void bt_ctf_field_common_array_set_is_frozen_recursive(
246 struct bt_ctf_field_common
*field
, bool is_frozen
);
249 void bt_ctf_field_common_sequence_set_is_frozen_recursive(
250 struct bt_ctf_field_common
*field
, bool is_frozen
);
253 void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common
*field
,
257 bt_bool
bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common
*field
);
260 bt_bool
bt_ctf_field_common_structure_is_set_recursive(
261 struct bt_ctf_field_common
*field
);
264 bt_bool
bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common
*field
);
267 bt_bool
bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common
*field
);
270 bt_bool
bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common
*field
);
273 # define bt_ctf_field_common_validate_recursive _bt_ctf_field_common_validate_recursive
274 # define bt_ctf_field_common_set_is_frozen_recursive _bt_ctf_field_common_set_is_frozen_recursive
275 # define bt_ctf_field_common_is_set_recursive _bt_ctf_field_common_is_set_recursive
276 # define bt_ctf_field_common_reset_recursive _bt_ctf_field_common_reset_recursive
277 # define bt_ctf_field_common_set _bt_ctf_field_common_set
279 # define bt_ctf_field_common_validate_recursive(_field) (-1)
280 # define bt_ctf_field_common_set_is_frozen_recursive(_field, _is_frozen)
281 # define bt_ctf_field_common_is_set_recursive(_field) (BT_FALSE)
282 # define bt_ctf_field_common_reset_recursive(_field)
283 # define bt_ctf_field_common_set(_field, _val)
287 static inline bool field_type_common_has_known_id(
288 struct bt_ctf_field_type_common
*ft
)
290 return (int) ft
->id
> BT_CTF_FIELD_TYPE_ID_UNKNOWN
||
291 (int) ft
->id
< BT_CTF_FIELD_TYPE_ID_NR
;
295 int _bt_ctf_field_common_validate_recursive(struct bt_ctf_field_common
*field
)
300 BT_ASSERT_PRE_MSG("%s", "Invalid field: field is NULL.");
305 BT_ASSERT(field_type_common_has_known_id(field
->type
));
307 if (field
->methods
->validate
) {
308 ret
= field
->methods
->validate(field
);
316 void _bt_ctf_field_common_reset_recursive(struct bt_ctf_field_common
*field
)
319 BT_ASSERT(field
->methods
->reset
);
320 field
->methods
->reset(field
);
324 void _bt_ctf_field_common_set(struct bt_ctf_field_common
*field
, bool value
)
327 field
->payload_set
= value
;
331 bt_bool
_bt_ctf_field_common_is_set_recursive(struct bt_ctf_field_common
*field
)
333 bt_bool is_set
= BT_FALSE
;
339 BT_ASSERT(field_type_common_has_known_id(field
->type
));
340 BT_ASSERT(field
->methods
->is_set
);
341 is_set
= field
->methods
->is_set(field
);
348 void bt_ctf_field_common_initialize(struct bt_ctf_field_common
*field
,
349 struct bt_ctf_field_type_common
*ft
, bool is_shared
,
350 bt_ctf_object_release_func release_func
,
351 struct bt_ctf_field_common_methods
*methods
)
355 bt_ctf_object_init(&field
->base
, is_shared
, release_func
);
356 field
->methods
= methods
;
357 field
->type
= (void *) bt_ctf_object_get_ref(ft
);
361 struct bt_ctf_field_type_common
*bt_ctf_field_common_borrow_type(
362 struct bt_ctf_field_common
*field
)
364 struct bt_ctf_field_type_common
*ret
= NULL
;
366 BT_ASSERT_PRE_NON_NULL(field
, "Field");
372 int64_t bt_ctf_field_common_sequence_get_length(struct bt_ctf_field_common
*field
)
374 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
376 BT_ASSERT_PRE_NON_NULL(field
, "Sequence field");
377 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
379 return (int64_t) sequence
->length
;
383 int bt_ctf_field_common_sequence_set_length(struct bt_ctf_field_common
*field
,
384 uint64_t length
, bt_ctf_field_common_create_func field_create_func
)
387 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
389 BT_ASSERT_PRE_NON_NULL(field
, "Sequence field");
390 BT_ASSERT_PRE(((int64_t) length
) >= 0,
391 "Invalid sequence length (too large): length=%" PRId64
,
393 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "Sequence field");
395 if (unlikely(length
> sequence
->elements
->len
)) {
397 struct bt_ctf_field_type_common_sequence
*sequence_ft
;
398 uint64_t cur_len
= sequence
->elements
->len
;
401 g_ptr_array_set_size(sequence
->elements
, length
);
402 sequence_ft
= BT_CTF_FROM_COMMON(sequence
->common
.type
);
404 for (i
= cur_len
; i
< sequence
->elements
->len
; i
++) {
405 struct bt_ctf_field_common
*elem_field
=
406 field_create_func(sequence_ft
->element_ft
);
413 BT_ASSERT(!sequence
->elements
->pdata
[i
]);
414 sequence
->elements
->pdata
[i
] = elem_field
;
418 sequence
->length
= length
;
425 struct bt_ctf_field_common
*bt_ctf_field_common_structure_borrow_field_by_name(
426 struct bt_ctf_field_common
*field
, const char *name
)
428 struct bt_ctf_field_common
*ret
= NULL
;
430 struct bt_ctf_field_type_common_structure
*structure_ft
;
431 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
433 GHashTable
*field_name_to_index
;
435 BT_ASSERT_PRE_NON_NULL(field
, "Structure field");
436 BT_ASSERT_PRE_NON_NULL(name
, "Field name");
437 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
438 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Field");
439 structure_ft
= BT_CTF_FROM_COMMON(field
->type
);
440 field_name_to_index
= structure_ft
->field_name_to_index
;
441 field_quark
= g_quark_from_string(name
);
442 if (!g_hash_table_lookup_extended(field_name_to_index
,
443 GUINT_TO_POINTER(field_quark
),
444 NULL
, (gpointer
*) &index
)) {
445 BT_LOGV("Invalid parameter: no such field in structure field's type: "
446 "struct-field-addr=%p, struct-ft-addr=%p, name=\"%s\"",
447 field
, field
->type
, name
);
451 ret
= structure
->fields
->pdata
[index
];
459 struct bt_ctf_field_common
*bt_ctf_field_common_structure_borrow_field_by_index(
460 struct bt_ctf_field_common
*field
, uint64_t index
)
462 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
464 BT_ASSERT_PRE_NON_NULL(field
, "Structure field");
465 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
466 BT_CTF_FIELD_TYPE_ID_STRUCT
, "Field");
467 BT_ASSERT_PRE(index
< structure
->fields
->len
,
468 "Index is out of bound: struct-field-addr=%p, "
469 "index=%" PRIu64
", count=%u", field
, index
,
470 structure
->fields
->len
);
471 return structure
->fields
->pdata
[index
];
475 struct bt_ctf_field_common
*bt_ctf_field_common_array_borrow_field(
476 struct bt_ctf_field_common
*field
, uint64_t index
)
478 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
480 BT_ASSERT_PRE_NON_NULL(field
, "Array field");
481 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_ARRAY
,
483 BT_ASSERT_PRE(index
< array
->elements
->len
,
484 "Index is out of bound: array-field-addr=%p, "
485 "index=%" PRIu64
", count=%u", field
,
486 index
, array
->elements
->len
);
487 return array
->elements
->pdata
[(size_t) index
];
491 struct bt_ctf_field_common
*bt_ctf_field_common_sequence_borrow_field(
492 struct bt_ctf_field_common
*field
, uint64_t index
)
494 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
496 BT_ASSERT_PRE_NON_NULL(field
, "Sequence field");
497 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
, BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
499 BT_ASSERT_PRE(index
< sequence
->length
,
500 "Index is out of bound: seq-field-addr=%p, "
501 "index=%" PRIu64
", count=%u", field
, index
,
502 sequence
->elements
->len
);
503 return sequence
->elements
->pdata
[(size_t) index
];
507 int bt_ctf_field_common_variant_set_tag(struct bt_ctf_field_common
*variant_field
,
508 uint64_t tag_uval
, bool is_signed
)
511 int64_t choice_index
;
512 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
514 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
515 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
516 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
518 /* Find matching index in variant field's type */
519 choice_index
= bt_ctf_field_type_common_variant_find_choice_index(
520 variant_field
->type
, tag_uval
, is_signed
);
521 if (choice_index
< 0) {
526 /* Select corresponding field */
527 BT_ASSERT(choice_index
< variant
->fields
->len
);
528 variant
->current_field
= variant
->fields
->pdata
[choice_index
];
529 variant
->tag_value
.u
= tag_uval
;
536 struct bt_ctf_field_common
*bt_ctf_field_common_variant_borrow_current_field(
537 struct bt_ctf_field_common
*variant_field
)
539 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
541 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
542 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
543 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
544 BT_ASSERT_PRE(variant
->current_field
,
545 "Variant field has no current field: field-addr=%p", variant_field
);
546 return variant
->current_field
;
550 int bt_ctf_field_common_variant_get_tag_signed(struct bt_ctf_field_common
*variant_field
,
553 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
555 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
556 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
557 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
558 BT_ASSERT_PRE(variant
->current_field
,
559 "Variant field has no current field: field-addr=%p", variant_field
);
560 *tag
= variant
->tag_value
.i
;
565 int bt_ctf_field_common_variant_get_tag_unsigned(struct bt_ctf_field_common
*variant_field
,
568 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(variant_field
);
570 BT_ASSERT_PRE_NON_NULL(variant_field
, "Variant field");
571 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(variant_field
,
572 BT_CTF_FIELD_TYPE_ID_VARIANT
, "Field");
573 BT_ASSERT_PRE(variant
->current_field
,
574 "Variant field has no current field: field-addr=%p", variant_field
);
575 *tag
= variant
->tag_value
.u
;
580 int bt_ctf_field_common_floating_point_get_value(struct bt_ctf_field_common
*field
,
583 struct bt_ctf_field_common_floating_point
*floating_point
=
584 BT_CTF_FROM_COMMON(field
);
586 BT_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
587 BT_ASSERT_PRE_NON_NULL(value
, "Value");
588 BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "Floating point number field");
589 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
590 BT_CTF_FIELD_TYPE_ID_FLOAT
, "Field");
591 *value
= floating_point
->payload
;
596 int bt_ctf_field_common_floating_point_set_value(struct bt_ctf_field_common
*field
,
599 struct bt_ctf_field_common_floating_point
*floating_point
=
600 BT_CTF_FROM_COMMON(field
);
602 BT_ASSERT_PRE_NON_NULL(field
, "Floating point number field");
603 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "Floating point number field");
604 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
605 BT_CTF_FIELD_TYPE_ID_FLOAT
, "Field");
606 floating_point
->payload
= value
;
607 bt_ctf_field_common_set(field
, true);
612 const char *bt_ctf_field_common_string_get_value(struct bt_ctf_field_common
*field
)
614 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
616 BT_ASSERT_PRE_NON_NULL(field
, "String field");
617 BT_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field
, "String field");
618 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
619 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
620 return (const char *) string
->buf
->data
;
624 int bt_ctf_field_common_string_clear(struct bt_ctf_field_common
*field
)
626 struct bt_ctf_field_common_string
*string_field
= BT_CTF_FROM_COMMON(field
);
628 BT_ASSERT_PRE_NON_NULL(field
, "String field");
629 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
630 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
631 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
632 string_field
->size
= 0;
633 bt_ctf_field_common_set(field
, true);
638 int bt_ctf_field_common_string_append_len(struct bt_ctf_field_common
*field
,
639 const char *value
, unsigned int length
)
641 struct bt_ctf_field_common_string
*string_field
= BT_CTF_FROM_COMMON(field
);
645 BT_ASSERT_PRE_NON_NULL(field
, "String field");
646 BT_ASSERT_PRE_NON_NULL(value
, "Value");
647 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
648 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
649 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
651 /* Make sure no null bytes are appended */
652 BT_ASSERT_PRE(memchr(value
, '\0', length
) == NULL
,
653 "String value to append contains a null character: "
654 "partial-value=\"%.32s\", length=%u", value
, length
);
656 new_size
= string_field
->size
+ length
;
658 if (unlikely(new_size
+ 1 > string_field
->buf
->len
)) {
659 g_array_set_size(string_field
->buf
, new_size
+ 1);
662 data
= string_field
->buf
->data
;
663 memcpy(data
+ string_field
->size
, value
, length
);
664 ((char *) string_field
->buf
->data
)[new_size
] = '\0';
665 string_field
->size
= new_size
;
666 bt_ctf_field_common_set(field
, true);
671 int bt_ctf_field_common_string_append(struct bt_ctf_field_common
*field
,
674 BT_ASSERT_PRE_NON_NULL(value
, "Value");
675 return bt_ctf_field_common_string_append_len(field
, value
,
680 int bt_ctf_field_common_string_set_value(struct bt_ctf_field_common
*field
,
683 BT_ASSERT_PRE_NON_NULL(field
, "String field");
684 BT_ASSERT_PRE_NON_NULL(value
, "Value");
685 BT_ASSERT_PRE_CTF_FIELD_COMMON_HOT(field
, "String field");
686 BT_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(field
,
687 BT_CTF_FIELD_TYPE_ID_STRING
, "Field");
688 bt_ctf_field_common_string_clear(field
);
689 return bt_ctf_field_common_string_append_len(field
,
690 value
, strlen(value
));
694 void bt_ctf_field_common_finalize(struct bt_ctf_field_common
*field
)
697 BT_LOGD_STR("Putting field's type.");
698 bt_ctf_object_put_ref(field
->type
);
702 void bt_ctf_field_common_integer_finalize(struct bt_ctf_field_common
*field
)
705 BT_LOGD("Finalizing common integer field object: addr=%p", field
);
706 bt_ctf_field_common_finalize(field
);
710 void bt_ctf_field_common_floating_point_finalize(struct bt_ctf_field_common
*field
)
713 BT_LOGD("Finalizing common floating point number field object: addr=%p", field
);
714 bt_ctf_field_common_finalize(field
);
718 void bt_ctf_field_common_structure_finalize_recursive(struct bt_ctf_field_common
*field
)
720 struct bt_ctf_field_common_structure
*structure
= BT_CTF_FROM_COMMON(field
);
723 BT_LOGD("Finalizing common structure field object: addr=%p", field
);
724 bt_ctf_field_common_finalize(field
);
726 if (structure
->fields
) {
727 g_ptr_array_free(structure
->fields
, TRUE
);
732 void bt_ctf_field_common_variant_finalize_recursive(struct bt_ctf_field_common
*field
)
734 struct bt_ctf_field_common_variant
*variant
= BT_CTF_FROM_COMMON(field
);
737 BT_LOGD("Finalizing common variant field object: addr=%p", field
);
738 bt_ctf_field_common_finalize(field
);
740 if (variant
->fields
) {
741 g_ptr_array_free(variant
->fields
, TRUE
);
746 void bt_ctf_field_common_array_finalize_recursive(struct bt_ctf_field_common
*field
)
748 struct bt_ctf_field_common_array
*array
= BT_CTF_FROM_COMMON(field
);
751 BT_LOGD("Finalizing common array field object: addr=%p", field
);
752 bt_ctf_field_common_finalize(field
);
754 if (array
->elements
) {
755 g_ptr_array_free(array
->elements
, TRUE
);
760 void bt_ctf_field_common_sequence_finalize_recursive(struct bt_ctf_field_common
*field
)
762 struct bt_ctf_field_common_sequence
*sequence
= BT_CTF_FROM_COMMON(field
);
765 BT_LOGD("Finalizing common sequence field object: addr=%p", field
);
766 bt_ctf_field_common_finalize(field
);
768 if (sequence
->elements
) {
769 g_ptr_array_free(sequence
->elements
, TRUE
);
774 void bt_ctf_field_common_string_finalize(struct bt_ctf_field_common
*field
)
776 struct bt_ctf_field_common_string
*string
= BT_CTF_FROM_COMMON(field
);
779 BT_LOGD("Finalizing common string field object: addr=%p", field
);
780 bt_ctf_field_common_finalize(field
);
783 g_array_free(string
->buf
, TRUE
);
788 static inline bool value_is_in_range_signed(unsigned int size
, int64_t value
)
791 int64_t min_value
, max_value
;
793 min_value
= -(1ULL << (size
- 1));
794 max_value
= (1ULL << (size
- 1)) - 1;
795 if (value
< min_value
|| value
> max_value
) {
796 BT_LOGF("Value is out of bounds: value=%" PRId64
", "
797 "min-value=%" PRId64
", max-value=%" PRId64
,
798 value
, min_value
, max_value
);
806 static inline bool value_is_in_range_unsigned(unsigned int size
, uint64_t value
)
811 max_value
= (size
== 64) ? UINT64_MAX
: ((uint64_t) 1 << size
) - 1;
812 if (value
> max_value
) {
813 BT_LOGF("Value is out of bounds: value=%" PRIu64
", "
814 "max-value=%" PRIu64
,
822 struct bt_ctf_field_enumeration
{
823 struct bt_ctf_field_common common
;
824 struct bt_ctf_field_common_integer
*container
;
827 struct bt_ctf_field_variant
{
828 struct bt_ctf_field_common_variant common
;
829 struct bt_ctf_field_enumeration
*tag
;
833 int bt_ctf_field_serialize_recursive(struct bt_ctf_field
*field
,
834 struct bt_ctfser
*ctfser
,
835 enum bt_ctf_byte_order native_byte_order
);
838 int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field
*field
,
839 const char *name
, struct bt_ctf_field
*value
);
842 struct bt_ctf_field
*bt_ctf_field_enumeration_borrow_container(
843 struct bt_ctf_field
*field
);
846 bt_bool
bt_ctf_field_is_set_recursive(struct bt_ctf_field
*field
)
848 return bt_ctf_field_common_is_set_recursive((void *) field
);
851 #endif /* BABELTRACE_CTF_WRITER_FIELDS_INTERNAL_H */