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