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