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