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