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