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