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