flt.utils.muxer: add IWYU pragma
[babeltrace.git] / src / lib / trace-ir / field.c
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8#define BT_LOG_TAG "LIB/FIELD"
9#include "lib/logging.h"
10
11#include "lib/assert-cond.h"
12#include <babeltrace2/trace-ir/field.h>
13#include "lib/object.h"
14#include "compat/compiler.h"
15#include "compat/fcntl.h"
16#include "common/assert.h"
17#include <inttypes.h>
18#include <stdbool.h>
19
20#include "field.h"
21#include "field-class.h"
22#include "lib/func-status.h"
23#include "utils.h"
24
25#define BT_ASSERT_PRE_DEV_FIELD_HOT(_field) \
26 BT_ASSERT_PRE_DEV_HOT("field", \
27 (const struct bt_field *) (_field), "Field", ": %!+f", (_field))
28
29static
30void reset_single_field(struct bt_field *field);
31
32static
33void reset_array_field(struct bt_field *field);
34
35static
36void reset_structure_field(struct bt_field *field);
37
38static
39void reset_option_field(struct bt_field *field);
40
41static
42void reset_variant_field(struct bt_field *field);
43
44static
45void set_single_field_is_frozen(struct bt_field *field, bool is_frozen);
46
47static
48void set_array_field_is_frozen(struct bt_field *field, bool is_frozen);
49
50static
51void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen);
52
53static
54void set_option_field_is_frozen(struct bt_field *field, bool is_frozen);
55
56static
57void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen);
58
59static
60bool single_field_is_set(const struct bt_field *field);
61
62static
63bool array_field_is_set(const struct bt_field *field);
64
65static
66bool structure_field_is_set(const struct bt_field *field);
67
68static
69bool option_field_is_set(const struct bt_field *field);
70
71static
72bool variant_field_is_set(const struct bt_field *field);
73
74static
75struct bt_field_methods bool_field_methods = {
76 .set_is_frozen = set_single_field_is_frozen,
77 .is_set = single_field_is_set,
78 .reset = reset_single_field,
79};
80
81static
82struct bt_field_methods bit_array_field_methods = {
83 .set_is_frozen = set_single_field_is_frozen,
84 .is_set = single_field_is_set,
85 .reset = reset_single_field,
86};
87
88static
89struct bt_field_methods integer_field_methods = {
90 .set_is_frozen = set_single_field_is_frozen,
91 .is_set = single_field_is_set,
92 .reset = reset_single_field,
93};
94
95static
96struct bt_field_methods real_field_methods = {
97 .set_is_frozen = set_single_field_is_frozen,
98 .is_set = single_field_is_set,
99 .reset = reset_single_field,
100};
101
102static
103struct bt_field_methods string_field_methods = {
104 .set_is_frozen = set_single_field_is_frozen,
105 .is_set = single_field_is_set,
106 .reset = reset_single_field,
107};
108
109static
110struct bt_field_methods structure_field_methods = {
111 .set_is_frozen = set_structure_field_is_frozen,
112 .is_set = structure_field_is_set,
113 .reset = reset_structure_field,
114};
115
116static
117struct bt_field_methods array_field_methods = {
118 .set_is_frozen = set_array_field_is_frozen,
119 .is_set = array_field_is_set,
120 .reset = reset_array_field,
121};
122
123static
124struct bt_field_methods option_field_methods = {
125 .set_is_frozen = set_option_field_is_frozen,
126 .is_set = option_field_is_set,
127 .reset = reset_option_field,
128};
129
130static
131struct bt_field_methods variant_field_methods = {
132 .set_is_frozen = set_variant_field_is_frozen,
133 .is_set = variant_field_is_set,
134 .reset = reset_variant_field,
135};
136
137static
138struct bt_field_methods blob_field_methods = {
139 .set_is_frozen = set_single_field_is_frozen,
140 .is_set = single_field_is_set,
141 .reset = reset_single_field,
142};
143
144static
145struct bt_field *create_bool_field(struct bt_field_class *);
146
147static
148struct bt_field *create_bit_array_field(struct bt_field_class *);
149
150static
151struct bt_field *create_integer_field(struct bt_field_class *);
152
153static
154struct bt_field *create_real_field(struct bt_field_class *);
155
156static
157struct bt_field *create_string_field(struct bt_field_class *);
158
159static
160struct bt_field *create_structure_field(struct bt_field_class *);
161
162static
163struct bt_field *create_static_array_field(struct bt_field_class *);
164
165static
166struct bt_field *create_dynamic_array_field(struct bt_field_class *);
167
168static
169struct bt_field *create_option_field(struct bt_field_class *);
170
171static
172struct bt_field *create_variant_field(struct bt_field_class *);
173
174static
175struct bt_field *create_blob_field(struct bt_field_class *);
176
177static
178void destroy_bool_field(struct bt_field *field);
179
180static
181void destroy_bit_array_field(struct bt_field *field);
182
183static
184void destroy_integer_field(struct bt_field *field);
185
186static
187void destroy_real_field(struct bt_field *field);
188
189static
190void destroy_string_field(struct bt_field *field);
191
192static
193void destroy_structure_field(struct bt_field *field);
194
195static
196void destroy_array_field(struct bt_field *field);
197
198static
199void destroy_option_field(struct bt_field *field);
200
201static
202void destroy_variant_field(struct bt_field *field);
203
204static
205void destroy_blob_field(struct bt_field *field);
206
207BT_EXPORT
208struct bt_field_class *bt_field_borrow_class(struct bt_field *field)
209{
210 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
211 return field->class;
212}
213
214BT_EXPORT
215const struct bt_field_class *bt_field_borrow_class_const(
216 const struct bt_field *field)
217{
218 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
219 return field->class;
220}
221
222BT_EXPORT
223enum bt_field_class_type bt_field_get_class_type(const struct bt_field *field)
224{
225 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
226 return field->class->type;
227}
228
229struct bt_field *bt_field_create(struct bt_field_class *fc)
230{
231 struct bt_field *field = NULL;
232
233 BT_ASSERT(fc);
234
235 switch (fc->type) {
236 case BT_FIELD_CLASS_TYPE_BOOL:
237 field = create_bool_field(fc);
238 break;
239 case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
240 field = create_bit_array_field(fc);
241 break;
242 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
243 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
244 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
245 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
246 field = create_integer_field(fc);
247 break;
248 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL:
249 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL:
250 field = create_real_field(fc);
251 break;
252 case BT_FIELD_CLASS_TYPE_STRING:
253 field = create_string_field(fc);
254 break;
255 case BT_FIELD_CLASS_TYPE_STRUCTURE:
256 field = create_structure_field(fc);
257 break;
258 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
259 field = create_static_array_field(fc);
260 break;
261 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD:
262 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD:
263 field = create_dynamic_array_field(fc);
264 break;
265 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD:
266 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD:
267 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
268 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
269 field = create_option_field(fc);
270 break;
271 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD:
272 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
273 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
274 field = create_variant_field(fc);
275 break;
276 case BT_FIELD_CLASS_TYPE_STATIC_BLOB:
277 case BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITHOUT_LENGTH_FIELD:
278 case BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITH_LENGTH_FIELD:
279 field = create_blob_field(fc);
280 break;
281 default:
282 bt_common_abort();
283 }
284
285 if (!field) {
286 BT_LIB_LOGE_APPEND_CAUSE("Cannot create field object from field class: "
287 "%![fc-]+F", fc);
288 goto end;
289 }
290
291end:
292 return field;
293}
294
295static inline
296void init_field(struct bt_field *field, struct bt_field_class *fc,
297 struct bt_field_methods *methods)
298{
299 BT_ASSERT(field);
300 BT_ASSERT(fc);
301 bt_object_init_unique(&field->base);
302 field->methods = methods;
303 field->class = fc;
304 bt_object_get_ref_no_null_check(fc);
305}
306
307static
308struct bt_field *create_bool_field(struct bt_field_class *fc)
309{
310 struct bt_field_bool *bool_field;
311
312 BT_LIB_LOGD("Creating boolean field object: %![fc-]+F", fc);
313 bool_field = g_new0(struct bt_field_bool, 1);
314 if (!bool_field) {
315 BT_LIB_LOGE_APPEND_CAUSE(
316 "Failed to allocate one boolean field.");
317 goto end;
318 }
319
320 init_field((void *) bool_field, fc, &bool_field_methods);
321 BT_LIB_LOGD("Created boolean field object: %!+f", bool_field);
322
323end:
324 return (void *) bool_field;
325}
326
327static
328struct bt_field *create_bit_array_field(struct bt_field_class *fc)
329{
330 struct bt_field_bit_array *ba_field;
331
332 BT_LIB_LOGD("Creating bit array field object: %![fc-]+F", fc);
333 ba_field = g_new0(struct bt_field_bit_array, 1);
334 if (!ba_field) {
335 BT_LIB_LOGE_APPEND_CAUSE(
336 "Failed to allocate one bit array field.");
337 goto end;
338 }
339
340 init_field((void *) ba_field, fc, &bit_array_field_methods);
341 BT_LIB_LOGD("Created bit array field object: %!+f", ba_field);
342
343end:
344 return (void *) ba_field;
345}
346
347static
348struct bt_field *create_integer_field(struct bt_field_class *fc)
349{
350 struct bt_field_integer *int_field;
351
352 BT_LIB_LOGD("Creating integer field object: %![fc-]+F", fc);
353 int_field = g_new0(struct bt_field_integer, 1);
354 if (!int_field) {
355 BT_LIB_LOGE_APPEND_CAUSE(
356 "Failed to allocate one integer field.");
357 goto end;
358 }
359
360 init_field((void *) int_field, fc, &integer_field_methods);
361 BT_LIB_LOGD("Created integer field object: %!+f", int_field);
362
363end:
364 return (void *) int_field;
365}
366
367static
368struct bt_field *create_real_field(struct bt_field_class *fc)
369{
370 struct bt_field_real *real_field;
371
372 BT_LIB_LOGD("Creating real field object: %![fc-]+F", fc);
373 real_field = g_new0(struct bt_field_real, 1);
374 if (!real_field) {
375 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one real field.");
376 goto end;
377 }
378
379 init_field((void *) real_field, fc, &real_field_methods);
380 BT_LIB_LOGD("Created real field object: %!+f", real_field);
381
382end:
383 return (void *) real_field;
384}
385
386static
387struct bt_field *create_string_field(struct bt_field_class *fc)
388{
389 struct bt_field_string *string_field;
390
391 BT_LIB_LOGD("Creating string field object: %![fc-]+F", fc);
392 string_field = g_new0(struct bt_field_string, 1);
393 if (!string_field) {
394 BT_LIB_LOGE_APPEND_CAUSE(
395 "Failed to allocate one string field.");
396 goto end;
397 }
398
399 init_field((void *) string_field, fc, &string_field_methods);
400 string_field->buf = g_array_sized_new(FALSE, FALSE,
401 sizeof(char), 1);
402 if (!string_field->buf) {
403 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GArray.");
404 bt_field_destroy((void *) string_field);
405 string_field = NULL;
406 goto end;
407 }
408
409 g_array_set_size(string_field->buf, 1);
410 bt_g_array_index(string_field->buf, char, 0) = '\0';
411 BT_LIB_LOGD("Created string field object: %!+f", string_field);
412
413end:
414 return (void *) string_field;
415}
416
417static inline
418int create_fields_from_named_field_classes(
419 struct bt_field_class_named_field_class_container *fc,
420 GPtrArray **fields)
421{
422 int ret = 0;
423 uint64_t i;
424
425 *fields = g_ptr_array_new_with_free_func(
426 (GDestroyNotify) bt_field_destroy);
427 if (!*fields) {
428 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
429 ret = -1;
430 goto end;
431 }
432
433 g_ptr_array_set_size(*fields, fc->named_fcs->len);
434
435 for (i = 0; i < fc->named_fcs->len; i++) {
436 struct bt_field *field;
437 struct bt_named_field_class *named_fc = fc->named_fcs->pdata[i];
438
439 field = bt_field_create(named_fc->fc);
440 if (!field) {
441 BT_LIB_LOGE_APPEND_CAUSE(
442 "Failed to create structure member or variant option field: "
443 "name=\"%s\", %![fc-]+F",
444 named_fc->name->str, named_fc->fc);
445 ret = -1;
446 goto end;
447 }
448
449 g_ptr_array_index(*fields, i) = field;
450 }
451
452end:
453 return ret;
454}
455
456static
457struct bt_field *create_structure_field(struct bt_field_class *fc)
458{
459 struct bt_field_structure *struct_field;
460
461 BT_LIB_LOGD("Creating structure field object: %![fc-]+F", fc);
462 struct_field = g_new0(struct bt_field_structure, 1);
463 if (!struct_field) {
464 BT_LIB_LOGE_APPEND_CAUSE(
465 "Failed to allocate one structure field.");
466 goto end;
467 }
468
469 init_field((void *) struct_field, fc, &structure_field_methods);
470
471 if (create_fields_from_named_field_classes((void *) fc,
472 &struct_field->fields)) {
473 BT_LIB_LOGE_APPEND_CAUSE(
474 "Cannot create structure member fields: %![fc-]+F", fc);
475 bt_field_destroy((void *) struct_field);
476 struct_field = NULL;
477 goto end;
478 }
479
480 BT_LIB_LOGD("Created structure field object: %!+f", struct_field);
481
482end:
483 return (void *) struct_field;
484}
485
486static
487struct bt_field *create_option_field(struct bt_field_class *fc)
488{
489 struct bt_field_option *opt_field;
490 struct bt_field_class_option *opt_fc = (void *) fc;
491
492 BT_LIB_LOGD("Creating option field object: %![fc-]+F", fc);
493 opt_field = g_new0(struct bt_field_option, 1);
494 if (!opt_field) {
495 BT_LIB_LOGE_APPEND_CAUSE(
496 "Failed to allocate one option field.");
497 goto end;
498 }
499
500 init_field((void *) opt_field, fc, &option_field_methods);
501 opt_field->content_field = bt_field_create(opt_fc->content_fc);
502 if (!opt_field->content_field) {
503 BT_LIB_LOGE_APPEND_CAUSE(
504 "Failed to create option field's content field: "
505 "%![opt-fc-]+F, %![content-fc-]+F",
506 opt_fc, opt_fc->content_fc);
507 bt_field_destroy((void *) opt_field);
508 opt_field = NULL;
509 goto end;
510 }
511
512 BT_LIB_LOGD("Created option field object: %!+f", opt_field);
513
514end:
515 return (void *) opt_field;
516}
517
518static
519struct bt_field *create_variant_field(struct bt_field_class *fc)
520{
521 struct bt_field_variant *var_field;
522
523 BT_LIB_LOGD("Creating variant field object: %![fc-]+F", fc);
524 var_field = g_new0(struct bt_field_variant, 1);
525 if (!var_field) {
526 BT_LIB_LOGE_APPEND_CAUSE(
527 "Failed to allocate one variant field.");
528 goto end;
529 }
530
531 init_field((void *) var_field, fc, &variant_field_methods);
532
533 if (create_fields_from_named_field_classes((void *) fc,
534 &var_field->fields)) {
535 BT_LIB_LOGE_APPEND_CAUSE("Cannot create variant member fields: "
536 "%![fc-]+F", fc);
537 bt_field_destroy((void *) var_field);
538 var_field = NULL;
539 goto end;
540 }
541
542 BT_LIB_LOGD("Created variant field object: %!+f", var_field);
543
544end:
545 return (void *) var_field;
546}
547
548static
549struct bt_field *create_blob_field(struct bt_field_class *fc)
550{
551 struct bt_field_blob *blob_field;
552
553 BT_LIB_LOGD("Creating BLOB field object: %![fc-]+F", fc);
554 blob_field = g_new0(struct bt_field_blob, 1);
555 if (!blob_field) {
556 BT_LIB_LOGE_APPEND_CAUSE(
557 "Failed to allocate one BLOB field.");
558 goto end;
559 }
560
561 init_field((void *) blob_field, fc, &blob_field_methods);
562
563 if (bt_field_class_type_is(fc->type, BT_FIELD_CLASS_TYPE_STATIC_BLOB)) {
564 struct bt_field_class_blob_static *blob_static_fc =
565 (void *) fc;
566 blob_field->length = blob_static_fc->length;
567 blob_field->data = g_malloc(blob_field->length);
568 if (!blob_field->data) {
569 BT_LIB_LOGE_APPEND_CAUSE(
570 "Failed to allocate BLOB field data: %![fc-]+F",
571 fc);
572 goto error;
573 }
574 }
575
576 goto end;
577error:
578 bt_field_destroy((void *) blob_field);
579 blob_field = NULL;
580
581end:
582 return (void *) blob_field;
583}
584
585static inline
586int init_array_field_fields(struct bt_field_array *array_field)
587{
588 int ret = 0;
589 uint64_t i;
590 struct bt_field_class_array *array_fc;
591
592 BT_ASSERT(array_field);
593 array_fc = (void *) array_field->common.class;
594 array_field->fields = g_ptr_array_sized_new(array_field->length);
595 if (!array_field->fields) {
596 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
597 ret = -1;
598 goto end;
599 }
600
601 g_ptr_array_set_free_func(array_field->fields,
602 (GDestroyNotify) bt_field_destroy);
603 g_ptr_array_set_size(array_field->fields, array_field->length);
604
605 for (i = 0; i < array_field->length; i++) {
606 array_field->fields->pdata[i] = bt_field_create(
607 array_fc->element_fc);
608 if (!array_field->fields->pdata[i]) {
609 BT_LIB_LOGE_APPEND_CAUSE(
610 "Cannot create array field's element field: "
611 "index=%" PRIu64 ", %![fc-]+F", i, array_fc);
612 ret = -1;
613 goto end;
614 }
615 }
616
617end:
618 return ret;
619}
620
621static
622struct bt_field *create_static_array_field(struct bt_field_class *fc)
623{
624 struct bt_field_class_array_static *array_fc = (void *) fc;
625 struct bt_field_array *array_field;
626
627 BT_LIB_LOGD("Creating static array field object: %![fc-]+F", fc);
628 array_field = g_new0(struct bt_field_array, 1);
629 if (!array_field) {
630 BT_LIB_LOGE_APPEND_CAUSE(
631 "Failed to allocate one static array field.");
632 goto end;
633 }
634
635 init_field((void *) array_field, fc, &array_field_methods);
636 array_field->length = array_fc->length;
637
638 if (init_array_field_fields(array_field)) {
639 BT_LIB_LOGE_APPEND_CAUSE("Cannot create static array fields: "
640 "%![fc-]+F", fc);
641 bt_field_destroy((void *) array_field);
642 array_field = NULL;
643 goto end;
644 }
645
646 BT_LIB_LOGD("Created static array field object: %!+f", array_field);
647
648end:
649 return (void *) array_field;
650}
651
652static
653struct bt_field *create_dynamic_array_field(struct bt_field_class *fc)
654{
655 struct bt_field_array *array_field;
656
657 BT_LIB_LOGD("Creating dynamic array field object: %![fc-]+F", fc);
658 array_field = g_new0(struct bt_field_array, 1);
659 if (!array_field) {
660 BT_LIB_LOGE_APPEND_CAUSE(
661 "Failed to allocate one dynamic array field.");
662 goto end;
663 }
664
665 init_field((void *) array_field, fc, &array_field_methods);
666
667 if (init_array_field_fields(array_field)) {
668 BT_LIB_LOGE_APPEND_CAUSE("Cannot create dynamic array fields: "
669 "%![fc-]+F", fc);
670 bt_field_destroy((void *) array_field);
671 array_field = NULL;
672 goto end;
673 }
674
675 BT_LIB_LOGD("Created dynamic array field object: %!+f", array_field);
676
677end:
678 return (void *) array_field;
679}
680
681BT_EXPORT
682bt_bool bt_field_bool_get_value(const struct bt_field *field)
683{
684 const struct bt_field_bool *bool_field = (const void *) field;
685
686 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
687 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
688 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
689 "boolean-field", BT_FIELD_CLASS_TYPE_BOOL, "Field");
690 return (bt_bool) bool_field->value;
691}
692
693BT_EXPORT
694void bt_field_bool_set_value(struct bt_field *field, bt_bool value)
695{
696 struct bt_field_bool *bool_field = (void *) field;
697
698 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
699 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
700 "boolean-field", BT_FIELD_CLASS_TYPE_BOOL, "Field");
701 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
702 bool_field->value = (bool) value;
703 bt_field_set_single(field, true);
704}
705
706BT_EXPORT
707uint64_t bt_field_bit_array_get_value_as_integer(const struct bt_field *field)
708{
709 const struct bt_field_bit_array *ba_field = (const void *) field;
710
711 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
712 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
713 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
714 "bit-array-field", BT_FIELD_CLASS_TYPE_BIT_ARRAY, "Field");
715 return ba_field->value_as_int;
716}
717
718BT_EXPORT
719void bt_field_bit_array_set_value_as_integer(struct bt_field *field,
720 uint64_t value)
721{
722 struct bt_field_bit_array *ba_field = (void *) field;
723 struct bt_field_class_bit_array *ba_fc;
724
725 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
726 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
727 "bit-array-field", BT_FIELD_CLASS_TYPE_BIT_ARRAY, "Field");
728 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
729 ba_fc = (void *) field->class;
730 ba_field->value_as_int = value;
731
732 if (ba_fc->length < 64) {
733 /* Apply mask */
734 ba_field->value_as_int &= ((UINT64_C(1) << ba_fc->length) - 1);
735 }
736
737 bt_field_set_single(field, true);
738}
739
740BT_EXPORT
741bt_field_bit_array_get_active_flag_labels_status
742bt_field_bit_array_get_active_flag_labels(const struct bt_field *field,
743 bt_field_class_bit_array_flag_label_array *label_array,
744 uint64_t *count)
745{
746 const struct bt_field_bit_array *ba_field = (const void *) field;
747
748 BT_ASSERT_PRE_DEV_NO_ERROR();
749 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
750 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
751 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
752 "bit-array-field", BT_FIELD_CLASS_TYPE_BIT_ARRAY, "Field");
753 BT_ASSERT_PRE_DEV_NON_NULL("label-array-output", label_array,
754 "Label array (output)");
755 BT_ASSERT_PRE_DEV_NON_NULL("count-output", count, "Count (output)");
756 return (int)
757 bt_field_class_bit_array_get_active_flag_labels_for_value_as_integer(
758 field->class, ba_field->value_as_int, label_array, count);
759}
760
761BT_EXPORT
762int64_t bt_field_integer_signed_get_value(const struct bt_field *field)
763{
764 const struct bt_field_integer *int_field = (const void *) field;
765
766 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
767 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
768 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT("field", field, "Field");
769 return int_field->value.i;
770}
771
772BT_EXPORT
773void bt_field_integer_signed_set_value(struct bt_field *field, int64_t value)
774{
775 struct bt_field_integer *int_field = (void *) field;
776
777 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
778 BT_ASSERT_PRE_DEV_FIELD_IS_SIGNED_INT("field", field, "Field");
779 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
780 BT_ASSERT_PRE_DEV("valid-value-for-field-class-field-value-range",
781 bt_util_value_is_in_range_signed(
782 ((struct bt_field_class_integer *) field->class)->range,
783 value),
784 "Value is out of bounds: value=%" PRId64 ", %![field-]+f, "
785 "%![fc-]+F", value, field, field->class);
786 int_field->value.i = value;
787 bt_field_set_single(field, true);
788}
789
790BT_EXPORT
791uint64_t bt_field_integer_unsigned_get_value(const struct bt_field *field)
792{
793 const struct bt_field_integer *int_field = (const void *) field;
794
795 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
796 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
797 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT("field", field, "Field");
798 return int_field->value.u;
799}
800
801BT_EXPORT
802void bt_field_integer_unsigned_set_value(struct bt_field *field, uint64_t value)
803{
804 struct bt_field_integer *int_field = (void *) field;
805
806 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
807 BT_ASSERT_PRE_DEV_FIELD_IS_UNSIGNED_INT("field", field, "Field");
808 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
809 BT_ASSERT_PRE_DEV("valid-value-for-field-class-field-value-range",
810 bt_util_value_is_in_range_unsigned(
811 ((struct bt_field_class_integer *) field->class)->range,
812 value),
813 "Value is out of bounds: value=%" PRIu64 ", %![field-]+f, "
814 "%![fc-]+F", value, field, field->class);
815 int_field->value.u = value;
816 bt_field_set_single(field, true);
817}
818
819BT_EXPORT
820float bt_field_real_single_precision_get_value(const struct bt_field *field)
821{
822 const struct bt_field_real *real_field = (const void *) field;
823
824 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
825 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
826 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
827 "single-precision-real-field",
828 BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL, "Field");
829 return (float) real_field->value;
830}
831
832BT_EXPORT
833double bt_field_real_double_precision_get_value(const struct bt_field *field)
834{
835 const struct bt_field_real *real_field = (const void *) field;
836
837 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
838 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
839 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
840 "double-precision-real-field",
841 BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL, "Field");
842 return real_field->value;
843}
844
845BT_EXPORT
846void bt_field_real_single_precision_set_value(struct bt_field *field,
847 float value)
848{
849 struct bt_field_real *real_field = (void *) field;
850
851 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
852 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
853 "single-precision-real-field",
854 BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL, "Field");
855 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
856
857 real_field->value = (double) value;
858 bt_field_set_single(field, true);
859}
860
861BT_EXPORT
862void bt_field_real_double_precision_set_value(struct bt_field *field,
863 double value)
864{
865 struct bt_field_real *real_field = (void *) field;
866
867 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
868 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
869 "double-precision-real-field",
870 BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL, "Field");
871 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
872
873 real_field->value = value;
874 bt_field_set_single(field, true);
875}
876
877BT_EXPORT
878enum bt_field_enumeration_get_mapping_labels_status
879bt_field_enumeration_unsigned_get_mapping_labels(
880 const struct bt_field *field,
881 bt_field_class_enumeration_mapping_label_array *label_array,
882 uint64_t *count)
883{
884 const struct bt_field_integer *int_field = (const void *) field;
885
886 BT_ASSERT_PRE_DEV_NO_ERROR();
887 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
888 BT_ASSERT_PRE_DEV_NON_NULL("label-array-output", label_array,
889 "Label array (output)");
890 BT_ASSERT_PRE_DEV_NON_NULL("count-output", count, "Count (output)");
891 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
892 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
893 "unsigned-enumeration-field",
894 BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION, "Field");
895 return (int)
896 bt_field_class_enumeration_unsigned_get_mapping_labels_for_value(
897 field->class, int_field->value.u, label_array, count);
898}
899
900BT_EXPORT
901enum bt_field_enumeration_get_mapping_labels_status
902bt_field_enumeration_signed_get_mapping_labels(
903 const struct bt_field *field,
904 bt_field_class_enumeration_mapping_label_array *label_array,
905 uint64_t *count)
906{
907 const struct bt_field_integer *int_field = (const void *) field;
908
909 BT_ASSERT_PRE_DEV_NO_ERROR();
910 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
911 BT_ASSERT_PRE_DEV_NON_NULL("label-array-output", label_array,
912 "Label array (output)");
913 BT_ASSERT_PRE_DEV_NON_NULL("count-output", count, "Count (output)");
914 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
915 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
916 "signed-enumeration-field",
917 BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION, "Field");
918 return (int)
919 bt_field_class_enumeration_signed_get_mapping_labels_for_value(
920 field->class, int_field->value.i, label_array, count);
921}
922
923BT_EXPORT
924const char *bt_field_string_get_value(const struct bt_field *field)
925{
926 const struct bt_field_string *string_field = (const void *) field;
927
928 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
929 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
930 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field, "string-field",
931 BT_FIELD_CLASS_TYPE_STRING, "Field");
932 return (const char *) string_field->buf->data;
933}
934
935BT_EXPORT
936uint64_t bt_field_string_get_length(const struct bt_field *field)
937{
938 const struct bt_field_string *string_field = (const void *) field;
939
940 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
941 BT_ASSERT_PRE_DEV_FIELD_IS_SET("field", field);
942 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field, "string-field",
943 BT_FIELD_CLASS_TYPE_STRING, "Field");
944 return string_field->length;
945}
946
947static inline
948void clear_string_field(struct bt_field *field)
949{
950 struct bt_field_string *string_field = (void *) field;
951
952 BT_ASSERT_DBG(field);
953 string_field->length = 0;
954 bt_g_array_index(string_field->buf, char, 0) = '\0';
955 bt_field_set_single(field, true);
956}
957
958BT_EXPORT
959enum bt_field_string_set_value_status bt_field_string_set_value(
960 struct bt_field *field, const char *value)
961{
962 BT_ASSERT_PRE_DEV_NO_ERROR();
963 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
964 BT_ASSERT_PRE_DEV_NON_NULL("value", value, "Value");
965 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
966 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field, "string-field",
967 BT_FIELD_CLASS_TYPE_STRING, "Field");
968 clear_string_field(field);
969 return (int) bt_field_string_append_with_length(field, value,
970 (uint64_t) strlen(value));
971}
972
973#define BT_ASSERT_PRE_DEV_FOR_APPEND_TO_STRING_FIELD_WITH_LENGTH(_field, _value, _length) \
974 do { \
975 BT_ASSERT_PRE_DEV_NO_ERROR(); \
976 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(_field); \
977 BT_ASSERT_PRE_DEV_NON_NULL("value", (_value), "Value"); \
978 BT_ASSERT_PRE_DEV_FIELD_HOT(_field); \
979 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", \
980 (_field), "string-field", \
981 BT_FIELD_CLASS_TYPE_STRING, "Field"); \
982 BT_ASSERT_PRE_DEV("value-has-no-null-byte", \
983 !memchr((_value), '\0', (_length)), \
984 "String value to append contains a null character: " \
985 "partial-value=\"%.32s\", length=%" PRIu64, \
986 (_value), (_length)); \
987 } while (0)
988
989static
990enum bt_field_string_append_status append_to_string_field_with_length(
991 struct bt_field *field, const char *value, uint64_t length)
992{
993 struct bt_field_string *string_field = (void *) field;
994 char *data;
995 uint64_t new_length;
996
997 BT_ASSERT_DBG(field);
998 BT_ASSERT_DBG(value);
999 new_length = length + string_field->length;
1000
1001 if (G_UNLIKELY(new_length + 1 > string_field->buf->len)) {
1002 g_array_set_size(string_field->buf, new_length + 1);
1003 }
1004
1005 data = string_field->buf->data;
1006 memcpy(data + string_field->length, value, length);
1007 ((char *) string_field->buf->data)[new_length] = '\0';
1008 string_field->length = new_length;
1009 bt_field_set_single(field, true);
1010 return BT_FUNC_STATUS_OK;
1011}
1012
1013BT_EXPORT
1014enum bt_field_string_append_status bt_field_string_append_with_length(
1015 struct bt_field *field, const char *value, uint64_t length)
1016{
1017 BT_ASSERT_PRE_DEV_FOR_APPEND_TO_STRING_FIELD_WITH_LENGTH(field, value,
1018 length);
1019 return append_to_string_field_with_length(field, value, length);
1020}
1021
1022BT_EXPORT
1023enum bt_field_string_append_status bt_field_string_append(
1024 struct bt_field *field, const char *value)
1025{
1026 uint64_t length = (uint64_t) strlen(value);
1027
1028 BT_ASSERT_PRE_DEV_FOR_APPEND_TO_STRING_FIELD_WITH_LENGTH(field, value,
1029 length);
1030 return append_to_string_field_with_length(field, value, length);
1031}
1032
1033BT_EXPORT
1034void bt_field_string_clear(struct bt_field *field)
1035{
1036 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1037 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
1038 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field, "string-field",
1039 BT_FIELD_CLASS_TYPE_STRING, "Field");
1040 clear_string_field(field);
1041}
1042
1043BT_EXPORT
1044uint64_t bt_field_array_get_length(const struct bt_field *field)
1045{
1046 const struct bt_field_array *array_field = (const void *) field;
1047
1048 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1049 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY("field", field, "Field");
1050 return array_field->length;
1051}
1052
1053BT_EXPORT
1054enum bt_field_array_dynamic_set_length_status bt_field_array_dynamic_set_length(
1055 struct bt_field *field, uint64_t length)
1056{
1057 int ret = BT_FUNC_STATUS_OK;
1058 struct bt_field_array *array_field = (void *) field;
1059
1060 BT_ASSERT_PRE_DEV_NO_ERROR();
1061 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1062 BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_ARRAY("field", field, "Field");
1063 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
1064
1065 if (G_UNLIKELY(length > array_field->fields->len)) {
1066 /* Make more room */
1067 struct bt_field_class_array *array_fc;
1068 uint64_t cur_len = array_field->fields->len;
1069 uint64_t i;
1070
1071 g_ptr_array_set_size(array_field->fields, length);
1072 array_fc = (void *) field->class;
1073
1074 for (i = cur_len; i < array_field->fields->len; i++) {
1075 struct bt_field *elem_field = bt_field_create(
1076 array_fc->element_fc);
1077
1078 if (!elem_field) {
1079 BT_LIB_LOGE_APPEND_CAUSE(
1080 "Cannot create element field for "
1081 "dynamic array field: "
1082 "index=%" PRIu64 ", "
1083 "%![array-field-]+f", i, field);
1084 ret = BT_FUNC_STATUS_MEMORY_ERROR;
1085 goto end;
1086 }
1087
1088 BT_ASSERT_DBG(!array_field->fields->pdata[i]);
1089 array_field->fields->pdata[i] = elem_field;
1090 }
1091 }
1092
1093 array_field->length = length;
1094
1095end:
1096 return ret;
1097}
1098
1099static inline
1100struct bt_field *borrow_array_field_element_field_by_index(
1101 struct bt_field *field, uint64_t index, const char *api_func)
1102{
1103 struct bt_field_array *array_field = (void *) field;
1104
1105 BT_ASSERT_PRE_DEV_FIELD_NON_NULL_FROM_FUNC(api_func, field);
1106 BT_ASSERT_PRE_DEV_FIELD_IS_ARRAY_FROM_FUNC(api_func, "field", field,
1107 "Field");
1108 BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func, index,
1109 array_field->length);
1110 return array_field->fields->pdata[index];
1111}
1112
1113BT_EXPORT
1114struct bt_field *bt_field_array_borrow_element_field_by_index(
1115 struct bt_field *field, uint64_t index)
1116{
1117 return borrow_array_field_element_field_by_index(field, index,
1118 __func__);
1119}
1120
1121BT_EXPORT
1122const struct bt_field *
1123bt_field_array_borrow_element_field_by_index_const(
1124 const struct bt_field *field, uint64_t index)
1125{
1126 return borrow_array_field_element_field_by_index((void *) field, index,
1127 __func__);
1128}
1129
1130static inline
1131struct bt_field *borrow_structure_field_member_field_by_index(
1132 struct bt_field *field, uint64_t index, const char *api_func)
1133{
1134 struct bt_field_structure *struct_field = (void *) field;
1135
1136 BT_ASSERT_PRE_DEV_FIELD_NON_NULL_FROM_FUNC(api_func, field);
1137 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE_FROM_FUNC(api_func, "field",
1138 field, "structure-field", BT_FIELD_CLASS_TYPE_STRUCTURE,
1139 "Field");
1140 BT_ASSERT_PRE_DEV_VALID_INDEX_FROM_FUNC(api_func, index,
1141 struct_field->fields->len);
1142 return struct_field->fields->pdata[index];
1143}
1144
1145BT_EXPORT
1146struct bt_field *bt_field_structure_borrow_member_field_by_index(
1147 struct bt_field *field, uint64_t index)
1148{
1149 return borrow_structure_field_member_field_by_index(field,
1150 index, __func__);
1151}
1152
1153BT_EXPORT
1154const struct bt_field *
1155bt_field_structure_borrow_member_field_by_index_const(
1156 const struct bt_field *field, uint64_t index)
1157{
1158 return borrow_structure_field_member_field_by_index(
1159 (void *) field, index, __func__);
1160}
1161
1162static inline
1163struct bt_field *borrow_structure_field_member_field_by_name(
1164 struct bt_field *field, const char *name, const char *api_func)
1165{
1166 struct bt_field *ret_field = NULL;
1167 struct bt_field_class_structure *struct_fc;
1168 struct bt_field_structure *struct_field = (void *) field;
1169 gpointer orig_key;
1170 gpointer index;
1171
1172 BT_ASSERT_PRE_DEV_FIELD_NON_NULL_FROM_FUNC(api_func, field);
1173 BT_ASSERT_PRE_DEV_NON_NULL_FROM_FUNC(api_func, "member-name", name,
1174 "Member name");
1175 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE_FROM_FUNC(api_func, "field",
1176 field, "structure-field", BT_FIELD_CLASS_TYPE_STRUCTURE,
1177 "Field");
1178 struct_fc = (void *) field->class;
1179
1180 if (!g_hash_table_lookup_extended(struct_fc->common.name_to_index, name,
1181 &orig_key, &index)) {
1182 goto end;
1183 }
1184
1185 ret_field = struct_field->fields->pdata[GPOINTER_TO_UINT(index)];
1186 BT_ASSERT_DBG(ret_field);
1187
1188end:
1189 return ret_field;
1190}
1191
1192BT_EXPORT
1193struct bt_field *bt_field_structure_borrow_member_field_by_name(
1194 struct bt_field *field, const char *name)
1195{
1196 return borrow_structure_field_member_field_by_name(field, name,
1197 __func__);
1198}
1199
1200BT_EXPORT
1201const struct bt_field *bt_field_structure_borrow_member_field_by_name_const(
1202 const struct bt_field *field, const char *name)
1203{
1204 return borrow_structure_field_member_field_by_name(
1205 (void *) field, name, __func__);
1206}
1207
1208BT_EXPORT
1209void bt_field_option_set_has_field(struct bt_field *field, bt_bool has_field)
1210{
1211 struct bt_field_option *opt_field = (void *) field;
1212
1213 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1214 BT_ASSERT_PRE_DEV_FIELD_IS_OPTION("field", field, "Field");
1215 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
1216
1217 if (has_field) {
1218 opt_field->selected_field = opt_field->content_field;
1219 } else {
1220 opt_field->selected_field = NULL;
1221 }
1222}
1223
1224BT_EXPORT
1225struct bt_field *bt_field_option_borrow_field(struct bt_field *field)
1226{
1227 struct bt_field_option *opt_field = (void *) field;
1228
1229 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1230 BT_ASSERT_PRE_DEV_FIELD_IS_OPTION("field", field, "Field");
1231 return opt_field->selected_field;
1232}
1233
1234BT_EXPORT
1235const struct bt_field *bt_field_option_borrow_field_const(
1236 const struct bt_field *field)
1237{
1238 return (const void *) bt_field_option_borrow_field((void *) field);
1239}
1240
1241#define BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_OPT_FIELD(_field) \
1242 do { \
1243 struct bt_field_variant *_var_field = (void *) field; \
1244 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(_field); \
1245 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT("field", (_field), \
1246 "Field"); \
1247 BT_ASSERT_PRE_DEV("has-selected-field", \
1248 _var_field->selected_field, \
1249 "Variant field has no selected field: %!+f", \
1250 field); \
1251 } while (0)
1252
1253static inline
1254struct bt_field *borrow_variant_field_selected_option_field(
1255 struct bt_field *field)
1256{
1257 struct bt_field_variant *var_field = (void *) field;
1258
1259 BT_ASSERT_DBG(field);
1260 return var_field->selected_field;
1261}
1262
1263BT_EXPORT
1264struct bt_field *bt_field_variant_borrow_selected_option_field(
1265 struct bt_field *field)
1266{
1267 BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_OPT_FIELD(field);
1268 return borrow_variant_field_selected_option_field(field);
1269}
1270
1271BT_EXPORT
1272const struct bt_field *bt_field_variant_borrow_selected_option_field_const(
1273 const struct bt_field *field)
1274{
1275 BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_OPT_FIELD(field);
1276 return borrow_variant_field_selected_option_field((void *) field);
1277}
1278
1279#define BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_CLASS_OPT(_field) \
1280 do { \
1281 struct bt_field_variant *_var_field = (void *) field; \
1282 BT_ASSERT_PRE_DEV("has-selected-field", \
1283 _var_field->selected_field, \
1284 "Variant field has no selected field: %!+f", \
1285 (_field)); \
1286 } while (0)
1287
1288static
1289const struct bt_field_class_variant_option *
1290borrow_variant_field_selected_class_option(const struct bt_field *field)
1291{
1292 const struct bt_field_class_named_field_class_container *container_fc;
1293 const struct bt_field_variant *var_field = (const void *) field;
1294
1295 BT_ASSERT_DBG(field);
1296 container_fc = (const void *) field->class;
1297 return container_fc->named_fcs->pdata[var_field->selected_index];
1298}
1299
1300BT_EXPORT
1301const struct bt_field_class_variant_option *
1302bt_field_variant_borrow_selected_option_class_const(
1303 const struct bt_field *field)
1304{
1305 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1306 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT("field", field, "Field");
1307 BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_CLASS_OPT(field);
1308 return borrow_variant_field_selected_class_option(field);
1309}
1310
1311BT_EXPORT
1312const struct bt_field_class_variant_with_selector_field_integer_unsigned_option *
1313bt_field_variant_with_selector_field_integer_unsigned_borrow_selected_option_class_const(
1314 const struct bt_field *field)
1315{
1316 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1317 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
1318 "variant-field-with-unsigned-selector-field",
1319 BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD,
1320 "Field");
1321 BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_CLASS_OPT(field);
1322 return (const void *) borrow_variant_field_selected_class_option(field);
1323}
1324
1325BT_EXPORT
1326const struct bt_field_class_variant_with_selector_field_integer_signed_option *
1327bt_field_variant_with_selector_field_integer_signed_borrow_selected_option_class_const(
1328 const struct bt_field *field)
1329{
1330 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1331 BT_ASSERT_PRE_DEV_FIELD_HAS_CLASS_TYPE("field", field,
1332 "variant-field-with-signed-selector-field",
1333 BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD,
1334 "Field");
1335 BT_ASSERT_PRE_DEV_FOR_BORROW_VAR_FIELD_SEL_CLASS_OPT(field);
1336 return (const void *) borrow_variant_field_selected_class_option(field);
1337}
1338
1339BT_EXPORT
1340enum bt_field_variant_select_option_by_index_status
1341bt_field_variant_select_option_by_index(
1342 struct bt_field *field, uint64_t index)
1343{
1344 struct bt_field_variant *var_field = (void *) field;
1345
1346 BT_ASSERT_PRE_DEV_NO_ERROR();
1347 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1348 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT("field", field, "Field");
1349 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
1350 BT_ASSERT_PRE_DEV_VALID_INDEX(index, var_field->fields->len);
1351 var_field->selected_field = var_field->fields->pdata[index];
1352 var_field->selected_index = index;
1353 return BT_FUNC_STATUS_OK;
1354}
1355
1356BT_EXPORT
1357uint64_t bt_field_variant_get_selected_option_index(
1358 const struct bt_field *field)
1359{
1360 const struct bt_field_variant *var_field = (const void *) field;
1361
1362 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1363 BT_ASSERT_PRE_DEV_FIELD_IS_VARIANT("field", field, "Field");
1364 BT_ASSERT_PRE_DEV("has-selected-field", var_field->selected_field,
1365 "Variant field has no selected field: %!+f", field);
1366 return var_field->selected_index;
1367}
1368
1369BT_EXPORT
1370uint8_t *bt_field_blob_get_data(struct bt_field *field)
1371{
1372 const struct bt_field_blob *blob_field = (const void *) field;
1373
1374 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1375 BT_ASSERT_PRE_DEV_FIELD_IS_BLOB("field", field, "Field");
1376 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
1377 BT_ASSERT_PRE_DEV("blob-field-length-is-set",
1378 blob_field->length > 0,
1379 "BLOB field length is not set: %!+f", field);
1380
1381 /* Assume that the user will fill the bytes. */
1382 bt_field_set_single(field, true);
1383
1384 return blob_field->data;
1385}
1386
1387BT_EXPORT
1388const uint8_t *bt_field_blob_get_data_const(const struct bt_field *field)
1389{
1390 const struct bt_field_blob *blob_field = (const void *) field;
1391
1392 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1393 BT_ASSERT_PRE_DEV_FIELD_IS_BLOB("field", field, "Field");
1394
1395 return blob_field->data;
1396}
1397
1398BT_EXPORT
1399enum bt_field_blob_dynamic_set_length_status bt_field_blob_dynamic_set_length(
1400 struct bt_field *field, uint64_t length)
1401{
1402 int ret;
1403 struct bt_field_blob *blob_field = (void *) field;
1404
1405 BT_ASSERT_PRE_DEV_NO_ERROR();
1406 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1407 BT_ASSERT_PRE_DEV_FIELD_IS_DYNAMIC_BLOB("field", field, "Field");
1408 BT_ASSERT_PRE_DEV_FIELD_HOT(field);
1409
1410 if (G_UNLIKELY(length > blob_field->length)) {
1411 /* Make more room */
1412 uint8_t *data = g_realloc(blob_field->data, length);
1413
1414 if (!data) {
1415 BT_LIB_LOGE_APPEND_CAUSE(
1416 "Failed to reallocate BLOB field data: %!+f",
1417 field);
1418 ret = BT_FIELD_DYNAMIC_BLOB_SET_LENGTH_STATUS_MEMORY_ERROR;
1419 goto end;
1420 }
1421
1422 blob_field->data = data;
1423 }
1424
1425 blob_field->length = length;
1426 ret = BT_FIELD_DYNAMIC_BLOB_SET_LENGTH_STATUS_OK;
1427
1428end:
1429 return ret;
1430}
1431
1432BT_EXPORT
1433uint64_t bt_field_blob_get_length(const struct bt_field *field)
1434{
1435 const struct bt_field_blob *blob_field = (const void *) field;
1436
1437 BT_ASSERT_PRE_DEV_FIELD_NON_NULL(field);
1438 BT_ASSERT_PRE_DEV_FIELD_IS_BLOB("field", field, "Field");
1439
1440 return blob_field->length;
1441}
1442
1443
1444static inline
1445void bt_field_finalize(struct bt_field *field)
1446{
1447 BT_ASSERT(field);
1448 BT_LOGD_STR("Putting field's class.");
1449 BT_OBJECT_PUT_REF_AND_RESET(field->class);
1450}
1451
1452static
1453void destroy_bool_field(struct bt_field *field)
1454{
1455 BT_ASSERT(field);
1456 BT_LIB_LOGD("Destroying boolean field object: %!+f", field);
1457 bt_field_finalize(field);
1458 g_free(field);
1459}
1460
1461static
1462void destroy_bit_array_field(struct bt_field *field)
1463{
1464 BT_ASSERT(field);
1465 BT_LIB_LOGD("Destroying bit array field object: %!+f", field);
1466 bt_field_finalize(field);
1467 g_free(field);
1468}
1469
1470static
1471void destroy_integer_field(struct bt_field *field)
1472{
1473 BT_ASSERT(field);
1474 BT_LIB_LOGD("Destroying integer field object: %!+f", field);
1475 bt_field_finalize(field);
1476 g_free(field);
1477}
1478
1479static
1480void destroy_real_field(struct bt_field *field)
1481{
1482 BT_ASSERT(field);
1483 BT_LIB_LOGD("Destroying real field object: %!+f", field);
1484 bt_field_finalize(field);
1485 g_free(field);
1486}
1487
1488static
1489void destroy_structure_field(struct bt_field *field)
1490{
1491 struct bt_field_structure *struct_field = (void *) field;
1492
1493 BT_ASSERT(field);
1494 BT_LIB_LOGD("Destroying structure field object: %!+f", field);
1495 bt_field_finalize(field);
1496
1497 if (struct_field->fields) {
1498 g_ptr_array_free(struct_field->fields, TRUE);
1499 struct_field->fields = NULL;
1500 }
1501
1502 g_free(field);
1503}
1504
1505static
1506void destroy_option_field(struct bt_field *field)
1507{
1508 struct bt_field_option *opt_field = (void *) field;
1509
1510 BT_ASSERT(field);
1511 BT_LIB_LOGD("Destroying option field object: %!+f", field);
1512 bt_field_finalize(field);
1513
1514 if (opt_field->content_field) {
1515 bt_field_destroy(opt_field->content_field);
1516 }
1517
1518 g_free(field);
1519}
1520
1521static
1522void destroy_variant_field(struct bt_field *field)
1523{
1524 struct bt_field_variant *var_field = (void *) field;
1525
1526 BT_ASSERT(field);
1527 BT_LIB_LOGD("Destroying variant field object: %!+f", field);
1528 bt_field_finalize(field);
1529
1530 if (var_field->fields) {
1531 g_ptr_array_free(var_field->fields, TRUE);
1532 var_field->fields = NULL;
1533 }
1534
1535 g_free(field);
1536}
1537
1538static
1539void destroy_blob_field(struct bt_field *field)
1540{
1541 struct bt_field_blob *blob_field = (void *) field;
1542
1543 BT_ASSERT(field);
1544 BT_LIB_LOGD("Destroying BLOB field object: %!+f", field);
1545 bt_field_finalize(field);
1546
1547 g_free(blob_field->data);
1548
1549 g_free(field);
1550}
1551
1552static
1553void destroy_array_field(struct bt_field *field)
1554{
1555 struct bt_field_array *array_field = (void *) field;
1556
1557 BT_ASSERT(field);
1558 BT_LIB_LOGD("Destroying array field object: %!+f", field);
1559 bt_field_finalize(field);
1560
1561 if (array_field->fields) {
1562 g_ptr_array_free(array_field->fields, TRUE);
1563 array_field->fields = NULL;
1564 }
1565
1566 g_free(field);
1567}
1568
1569static
1570void destroy_string_field(struct bt_field *field)
1571{
1572 struct bt_field_string *string_field = (void *) field;
1573
1574 BT_ASSERT(field);
1575 BT_LIB_LOGD("Destroying string field object: %!+f", field);
1576 bt_field_finalize(field);
1577
1578 if (string_field->buf) {
1579 g_array_free(string_field->buf, TRUE);
1580 string_field->buf = NULL;
1581 }
1582
1583 g_free(field);
1584}
1585
1586void bt_field_destroy(struct bt_field *field)
1587{
1588 BT_ASSERT(field);
1589
1590 switch (field->class->type) {
1591 case BT_FIELD_CLASS_TYPE_BOOL:
1592 destroy_bool_field(field);
1593 break;
1594 case BT_FIELD_CLASS_TYPE_BIT_ARRAY:
1595 destroy_bit_array_field(field);
1596 break;
1597 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER:
1598 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER:
1599 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
1600 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
1601 destroy_integer_field(field);
1602 break;
1603 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL:
1604 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL:
1605 destroy_real_field(field);
1606 break;
1607 case BT_FIELD_CLASS_TYPE_STRING:
1608 destroy_string_field(field);
1609 break;
1610 case BT_FIELD_CLASS_TYPE_STRUCTURE:
1611 destroy_structure_field(field);
1612 break;
1613 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY:
1614 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD:
1615 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD:
1616 destroy_array_field(field);
1617 break;
1618 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD:
1619 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD:
1620 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
1621 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
1622 destroy_option_field(field);
1623 break;
1624 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD:
1625 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD:
1626 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD:
1627 destroy_variant_field(field);
1628 break;
1629 case BT_FIELD_CLASS_TYPE_STATIC_BLOB:
1630 case BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITHOUT_LENGTH_FIELD:
1631 case BT_FIELD_CLASS_TYPE_DYNAMIC_BLOB_WITH_LENGTH_FIELD:
1632 destroy_blob_field(field);
1633 break;
1634 default:
1635 bt_common_abort();
1636 }
1637}
1638
1639static
1640void reset_single_field(struct bt_field *field)
1641{
1642 BT_ASSERT_DBG(field);
1643 field->is_set = false;
1644}
1645
1646static
1647void reset_structure_field(struct bt_field *field)
1648{
1649 uint64_t i;
1650 struct bt_field_structure *struct_field = (void *) field;
1651
1652 BT_ASSERT_DBG(field);
1653
1654 for (i = 0; i < struct_field->fields->len; i++) {
1655 bt_field_reset(struct_field->fields->pdata[i]);
1656 }
1657}
1658
1659static
1660void reset_option_field(struct bt_field *field)
1661{
1662 struct bt_field_option *opt_field = (void *) field;
1663
1664 BT_ASSERT_DBG(opt_field);
1665 bt_field_reset(opt_field->content_field);
1666 opt_field->selected_field = NULL;
1667}
1668
1669static
1670void reset_variant_field(struct bt_field *field)
1671{
1672 uint64_t i;
1673 struct bt_field_variant *var_field = (void *) field;
1674
1675 BT_ASSERT_DBG(field);
1676
1677 for (i = 0; i < var_field->fields->len; i++) {
1678 bt_field_reset(var_field->fields->pdata[i]);
1679 }
1680}
1681
1682static
1683void reset_array_field(struct bt_field *field)
1684{
1685 uint64_t i;
1686 struct bt_field_array *array_field = (void *) field;
1687
1688 BT_ASSERT_DBG(field);
1689
1690 for (i = 0; i < array_field->fields->len; i++) {
1691 bt_field_reset(array_field->fields->pdata[i]);
1692 }
1693}
1694
1695static
1696void set_single_field_is_frozen(struct bt_field *field, bool is_frozen)
1697{
1698 field->frozen = is_frozen;
1699}
1700
1701static
1702void set_structure_field_is_frozen(struct bt_field *field, bool is_frozen)
1703{
1704 uint64_t i;
1705 struct bt_field_structure *struct_field = (void *) field;
1706
1707 BT_LIB_LOGD("Setting structure field's frozen state: "
1708 "%![field-]+f, is-frozen=%d", field, is_frozen);
1709
1710 for (i = 0; i < struct_field->fields->len; i++) {
1711 struct bt_field *member_field = struct_field->fields->pdata[i];
1712
1713 BT_LIB_LOGD("Setting structure field's member field's "
1714 "frozen state: %![field-]+f, index=%" PRIu64,
1715 member_field, i);
1716 _bt_field_set_is_frozen(member_field, is_frozen);
1717 }
1718
1719 set_single_field_is_frozen(field, is_frozen);
1720}
1721
1722static
1723void set_option_field_is_frozen(struct bt_field *field, bool is_frozen)
1724{
1725 struct bt_field_option *opt_field = (void *) field;
1726
1727 BT_LIB_LOGD("Setting option field's frozen state: "
1728 "%![field-]+f, is-frozen=%d", field, is_frozen);
1729 _bt_field_set_is_frozen(opt_field->content_field, is_frozen);
1730 set_single_field_is_frozen(field, is_frozen);
1731}
1732
1733static
1734void set_variant_field_is_frozen(struct bt_field *field, bool is_frozen)
1735{
1736 uint64_t i;
1737 struct bt_field_variant *var_field = (void *) field;
1738
1739 BT_LIB_LOGD("Setting variant field's frozen state: "
1740 "%![field-]+f, is-frozen=%d", field, is_frozen);
1741
1742 for (i = 0; i < var_field->fields->len; i++) {
1743 struct bt_field *option_field = var_field->fields->pdata[i];
1744
1745 BT_LIB_LOGD("Setting variant field's option field's "
1746 "frozen state: %![field-]+f, index=%" PRIu64,
1747 option_field, i);
1748 _bt_field_set_is_frozen(option_field, is_frozen);
1749 }
1750
1751 set_single_field_is_frozen(field, is_frozen);
1752}
1753
1754static
1755void set_array_field_is_frozen(struct bt_field *field, bool is_frozen)
1756{
1757 uint64_t i;
1758 struct bt_field_array *array_field = (void *) field;
1759
1760 BT_LIB_LOGD("Setting array field's frozen state: "
1761 "%![field-]+f, is-frozen=%d", field, is_frozen);
1762
1763 for (i = 0; i < array_field->fields->len; i++) {
1764 struct bt_field *elem_field = array_field->fields->pdata[i];
1765
1766 BT_LIB_LOGD("Setting array field's element field's "
1767 "frozen state: %![field-]+f, index=%" PRIu64,
1768 elem_field, i);
1769 _bt_field_set_is_frozen(elem_field, is_frozen);
1770 }
1771
1772 set_single_field_is_frozen(field, is_frozen);
1773}
1774
1775void _bt_field_set_is_frozen(const struct bt_field *field,
1776 bool is_frozen)
1777{
1778 BT_ASSERT_DBG(field);
1779 BT_LIB_LOGD("Setting field object's frozen state: %!+f, is-frozen=%d",
1780 field, is_frozen);
1781 BT_ASSERT_DBG(field->methods->set_is_frozen);
1782 field->methods->set_is_frozen((void *) field, is_frozen);
1783}
1784
1785static
1786bool single_field_is_set(const struct bt_field *field)
1787{
1788 BT_ASSERT_DBG(field);
1789 return field->is_set;
1790}
1791
1792static
1793bool structure_field_is_set(const struct bt_field *field)
1794{
1795 bool is_set = true;
1796 uint64_t i;
1797 const struct bt_field_structure *struct_field = (const void *) field;
1798
1799 BT_ASSERT_DBG(field);
1800
1801 for (i = 0; i < struct_field->fields->len; i++) {
1802 is_set = bt_field_is_set(struct_field->fields->pdata[i]);
1803 if (!is_set) {
1804 goto end;
1805 }
1806 }
1807
1808end:
1809 return is_set;
1810}
1811
1812static
1813bool option_field_is_set(const struct bt_field *field)
1814{
1815 const struct bt_field_option *opt_field = (const void *) field;
1816 bool is_set = false;
1817
1818 BT_ASSERT_DBG(field);
1819
1820 if (opt_field->selected_field) {
1821 is_set = bt_field_is_set(opt_field->selected_field);
1822 }
1823
1824 return is_set;
1825}
1826
1827static
1828bool variant_field_is_set(const struct bt_field *field)
1829{
1830 const struct bt_field_variant *var_field = (const void *) field;
1831 bool is_set = false;
1832
1833 BT_ASSERT_DBG(field);
1834
1835 if (var_field->selected_field) {
1836 is_set = bt_field_is_set(var_field->selected_field);
1837 }
1838
1839 return is_set;
1840}
1841
1842static
1843bool array_field_is_set(const struct bt_field *field)
1844{
1845 bool is_set = true;
1846 uint64_t i;
1847 const struct bt_field_array *array_field = (const void *) field;
1848
1849 BT_ASSERT_DBG(field);
1850
1851 for (i = 0; i < array_field->length; i++) {
1852 is_set = bt_field_is_set(array_field->fields->pdata[i]);
1853 if (!is_set) {
1854 goto end;
1855 }
1856 }
1857
1858end:
1859 return is_set;
1860}
This page took 0.029166 seconds and 5 git commands to generate.