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