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