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