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