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