lib: add internal object pool API and use it; adapt plugins/tests
[babeltrace.git] / lib / ctf-ir / fields.c
CommitLineData
273b65be 1/*
2e33ac5a 2 * fields.c
273b65be 3 *
d2dc44b6 4 * Babeltrace CTF IR - Event Fields
273b65be 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
fc25abce
PP
29#define BT_LOG_TAG "FIELDS"
30#include <babeltrace/lib-logging-internal.h>
31
8deee039 32#include <babeltrace/assert-pre-internal.h>
2e33ac5a
PP
33#include <babeltrace/ctf-ir/fields-internal.h>
34#include <babeltrace/ctf-ir/field-types-internal.h>
83509119
JG
35#include <babeltrace/object-internal.h>
36#include <babeltrace/ref.h>
3d9990ac
PP
37#include <babeltrace/compiler-internal.h>
38#include <babeltrace/compat/fcntl-internal.h>
39#include <babeltrace/align-internal.h>
8b45963b 40#include <babeltrace/assert-internal.h>
fc25abce 41#include <inttypes.h>
273b65be 42
a6918753
PP
43#define BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(_field, _name) \
44 BT_ASSERT_PRE((_field)->type->id == BT_FIELD_TYPE_ID_INTEGER || \
45 (_field)->type->id == BT_FIELD_TYPE_ID_ENUM, \
46 _name " is not an integer or an enumeration field: " \
47 "%!+f", (_field))
76f869ab 48
8deee039 49static struct bt_field_common_methods bt_field_integer_methods = {
a6918753 50 .set_is_frozen = bt_field_common_generic_set_is_frozen,
8deee039 51 .validate = bt_field_common_generic_validate,
a6918753 52 .copy = NULL,
8deee039
PP
53 .is_set = bt_field_common_generic_is_set,
54 .reset = bt_field_common_generic_reset,
55};
273b65be 56
8deee039 57static struct bt_field_common_methods bt_field_floating_point_methods = {
a6918753 58 .set_is_frozen = bt_field_common_generic_set_is_frozen,
8deee039 59 .validate = bt_field_common_generic_validate,
a6918753 60 .copy = NULL,
8deee039
PP
61 .is_set = bt_field_common_generic_is_set,
62 .reset = bt_field_common_generic_reset,
273b65be
JG
63};
64
8deee039 65static struct bt_field_common_methods bt_field_enumeration_methods = {
a6918753
PP
66 .set_is_frozen = bt_field_common_generic_set_is_frozen,
67 .validate = bt_field_common_generic_validate,
68 .copy = NULL,
69 .is_set = bt_field_common_generic_is_set,
70 .reset = bt_field_common_generic_reset,
273b65be
JG
71};
72
8deee039 73static struct bt_field_common_methods bt_field_string_methods = {
a6918753 74 .set_is_frozen = bt_field_common_generic_set_is_frozen,
8deee039 75 .validate = bt_field_common_generic_validate,
a6918753 76 .copy = NULL,
8deee039
PP
77 .is_set = bt_field_common_generic_is_set,
78 .reset = bt_field_common_string_reset,
273b65be
JG
79};
80
8deee039 81static struct bt_field_common_methods bt_field_structure_methods = {
a6918753 82 .set_is_frozen = bt_field_common_structure_set_is_frozen_recursive,
8deee039 83 .validate = bt_field_common_structure_validate_recursive,
a6918753 84 .copy = NULL,
8deee039
PP
85 .is_set = bt_field_common_structure_is_set_recursive,
86 .reset = bt_field_common_structure_reset_recursive,
12c8a1a3
JG
87};
88
8deee039 89static struct bt_field_common_methods bt_field_sequence_methods = {
a6918753 90 .set_is_frozen = bt_field_common_sequence_set_is_frozen_recursive,
8deee039 91 .validate = bt_field_common_sequence_validate_recursive,
a6918753 92 .copy = NULL,
8deee039
PP
93 .is_set = bt_field_common_sequence_is_set_recursive,
94 .reset = bt_field_common_sequence_reset_recursive,
273b65be
JG
95};
96
8deee039 97static struct bt_field_common_methods bt_field_array_methods = {
a6918753 98 .set_is_frozen = bt_field_common_array_set_is_frozen_recursive,
8deee039 99 .validate = bt_field_common_array_validate_recursive,
a6918753 100 .copy = NULL,
8deee039
PP
101 .is_set = bt_field_common_array_is_set_recursive,
102 .reset = bt_field_common_array_reset_recursive,
87d43dc1
JG
103};
104
8deee039 105static struct bt_field_common_methods bt_field_variant_methods = {
a6918753 106 .set_is_frozen = bt_field_common_variant_set_is_frozen_recursive,
8deee039 107 .validate = bt_field_common_variant_validate_recursive,
a6918753 108 .copy = NULL,
8deee039
PP
109 .is_set = bt_field_common_variant_is_set_recursive,
110 .reset = bt_field_common_variant_reset_recursive,
918be005
PP
111};
112
76f869ab 113static
8deee039 114struct bt_field *bt_field_integer_create(struct bt_field_type *);
76f869ab 115
8deee039
PP
116static
117struct bt_field *bt_field_enumeration_create(struct bt_field_type *);
118
119static
120struct bt_field *bt_field_floating_point_create(struct bt_field_type *);
121
122static
123struct bt_field *bt_field_structure_create(struct bt_field_type *);
124
125static
126struct bt_field *bt_field_variant_create(struct bt_field_type *);
127
128static
129struct bt_field *bt_field_array_create(struct bt_field_type *);
8b45963b 130
8deee039
PP
131static
132struct bt_field *bt_field_sequence_create(struct bt_field_type *);
8b45963b 133
8deee039
PP
134static
135struct bt_field *bt_field_string_create(struct bt_field_type *);
8b45963b 136
8deee039
PP
137static
138struct bt_field *(* const field_create_funcs[])(struct bt_field_type *) = {
139 [BT_FIELD_TYPE_ID_INTEGER] = bt_field_integer_create,
140 [BT_FIELD_TYPE_ID_ENUM] = bt_field_enumeration_create,
141 [BT_FIELD_TYPE_ID_FLOAT] = bt_field_floating_point_create,
142 [BT_FIELD_TYPE_ID_STRUCT] = bt_field_structure_create,
143 [BT_FIELD_TYPE_ID_VARIANT] = bt_field_variant_create,
144 [BT_FIELD_TYPE_ID_ARRAY] = bt_field_array_create,
145 [BT_FIELD_TYPE_ID_SEQUENCE] = bt_field_sequence_create,
146 [BT_FIELD_TYPE_ID_STRING] = bt_field_string_create,
147};
8b45963b 148
a6918753
PP
149static
150void bt_field_integer_destroy(struct bt_field *field);
151
152static
153void bt_field_enumeration_destroy(struct bt_field *field);
154
155static
156void bt_field_floating_point_destroy(struct bt_field *field);
157
158static
159void bt_field_structure_destroy_recursive(struct bt_field *field);
160
161static
162void bt_field_variant_destroy_recursive(struct bt_field *field);
163
164static
165void bt_field_array_destroy_recursive(struct bt_field *field);
166
167static
168void bt_field_sequence_destroy_recursive(struct bt_field *field);
169
170static
171void bt_field_string_destroy(struct bt_field *field);
172
173static
174void (* const field_destroy_funcs[])(struct bt_field *) = {
175 [BT_FIELD_TYPE_ID_INTEGER] = bt_field_integer_destroy,
176 [BT_FIELD_TYPE_ID_ENUM] = bt_field_enumeration_destroy,
177 [BT_FIELD_TYPE_ID_FLOAT] = bt_field_floating_point_destroy,
178 [BT_FIELD_TYPE_ID_STRUCT] = bt_field_structure_destroy_recursive,
179 [BT_FIELD_TYPE_ID_VARIANT] = bt_field_variant_destroy_recursive,
180 [BT_FIELD_TYPE_ID_ARRAY] = bt_field_array_destroy_recursive,
181 [BT_FIELD_TYPE_ID_SEQUENCE] = bt_field_sequence_destroy_recursive,
182 [BT_FIELD_TYPE_ID_STRING] = bt_field_string_destroy,
183};
184
185BT_HIDDEN
186struct bt_field *bt_field_create_recursive(struct bt_field_type *type)
273b65be 187{
839d52a5
PP
188 struct bt_field *field = NULL;
189 enum bt_field_type_id type_id;
273b65be 190
8b45963b 191 BT_ASSERT_PRE_NON_NULL(type, "Field type");
8deee039
PP
192 BT_ASSERT(field_type_common_has_known_id((void *) type));
193 BT_ASSERT_PRE(bt_field_type_common_validate((void *) type) == 0,
8b45963b 194 "Field type is invalid: %!+F", type);
839d52a5 195 type_id = bt_field_type_get_type_id(type);
273b65be
JG
196 field = field_create_funcs[type_id](type);
197 if (!field) {
8deee039 198 goto end;
273b65be
JG
199 }
200
a6918753 201 bt_object_set_is_shared((void *) field, false);
839d52a5 202 bt_field_type_freeze(type);
273b65be 203
8deee039
PP
204end:
205 return field;
273b65be
JG
206}
207
5fe68922 208struct bt_field_type *bt_field_borrow_type(struct bt_field *field)
cd95e351 209{
5fe68922 210 return (void *) bt_field_common_borrow_type((void *) field);
cd95e351
JG
211}
212
839d52a5 213enum bt_field_type_id bt_field_get_type_id(struct bt_field *field)
4ebcc695 214{
8deee039
PP
215 struct bt_field_common *field_common = (void *) field;
216
8b45963b 217 BT_ASSERT_PRE_NON_NULL(field, "Field");
8deee039 218 return field_common->type->id;
4ebcc695
PP
219}
220
839d52a5 221bt_bool bt_field_is_integer(struct bt_field *field)
8f3553be 222{
839d52a5 223 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_INTEGER;
8f3553be
PP
224}
225
839d52a5 226bt_bool bt_field_is_floating_point(struct bt_field *field)
8f3553be 227{
839d52a5 228 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_FLOAT;
8f3553be
PP
229}
230
839d52a5 231bt_bool bt_field_is_enumeration(struct bt_field *field)
8f3553be 232{
839d52a5 233 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_ENUM;
8f3553be
PP
234}
235
839d52a5 236bt_bool bt_field_is_string(struct bt_field *field)
8f3553be 237{
839d52a5 238 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_STRING;
8f3553be
PP
239}
240
839d52a5 241bt_bool bt_field_is_structure(struct bt_field *field)
8f3553be 242{
839d52a5 243 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_STRUCT;
8f3553be
PP
244}
245
839d52a5 246bt_bool bt_field_is_array(struct bt_field *field)
8f3553be 247{
839d52a5 248 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_ARRAY;
8f3553be
PP
249}
250
839d52a5 251bt_bool bt_field_is_sequence(struct bt_field *field)
8f3553be 252{
839d52a5 253 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_SEQUENCE;
8f3553be
PP
254}
255
839d52a5 256bt_bool bt_field_is_variant(struct bt_field *field)
8f3553be 257{
839d52a5 258 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_VARIANT;
8f3553be
PP
259}
260
a6918753 261int64_t bt_field_sequence_get_length(struct bt_field *field)
fea75608 262{
a6918753 263 return bt_field_common_sequence_get_length((void *) field);
fea75608
PP
264}
265
a6918753 266int bt_field_sequence_set_length(struct bt_field *field, uint64_t length)
273b65be 267{
8deee039 268 return bt_field_common_sequence_set_length((void *) field,
a6918753 269 length, (bt_field_common_create_func) bt_field_create_recursive);
273b65be
JG
270}
271
5fe68922 272struct bt_field *bt_field_structure_borrow_field_by_index(
839d52a5 273 struct bt_field *field, uint64_t index)
cd95e351 274{
5fe68922 275 return (void *) bt_field_common_structure_borrow_field_by_index(
8deee039 276 (void *) field, index);
8b45963b 277}
fc25abce 278
5fe68922 279struct bt_field *bt_field_structure_borrow_field_by_name(
8deee039 280 struct bt_field *field, const char *name)
8b45963b 281{
5fe68922 282 return (void *) bt_field_common_structure_borrow_field_by_name(
8deee039 283 (void *) field, name);
cd95e351
JG
284}
285
5fe68922 286struct bt_field *bt_field_array_borrow_field(
8deee039 287 struct bt_field *field, uint64_t index)
273b65be 288{
5fe68922 289 return (void *) bt_field_common_array_borrow_field((void *) field,
a6918753 290 index);
8deee039 291}
8b45963b 292
5fe68922 293struct bt_field *bt_field_sequence_borrow_field(
8deee039
PP
294 struct bt_field *field, uint64_t index)
295{
5fe68922 296 return (void *) bt_field_common_sequence_borrow_field((void *) field,
a6918753 297 index);
273b65be
JG
298}
299
5fe68922 300struct bt_field *bt_field_variant_borrow_current_field(
839d52a5 301 struct bt_field *variant_field)
3f4a108d 302{
5fe68922 303 return (void *) bt_field_common_variant_borrow_current_field(
8deee039 304 (void *) variant_field);
3f4a108d
PP
305}
306
a6918753
PP
307int bt_field_variant_set_tag_signed(struct bt_field *variant_field,
308 int64_t tag)
f78d67fb 309{
a6918753
PP
310 return bt_field_variant_common_set_tag((void *) variant_field,
311 (uint64_t) tag, true);
312}
313
314int bt_field_variant_set_tag_unsigned(struct bt_field *variant_field,
315 uint64_t tag)
316{
317 return bt_field_variant_common_set_tag((void *) variant_field,
318 (uint64_t) tag, false);
f78d67fb
JG
319}
320
a6918753
PP
321int bt_field_variant_get_tag_signed(struct bt_field *variant_field,
322 int64_t *tag)
273b65be 323{
a6918753
PP
324 return bt_field_common_variant_get_tag_signed((void *) variant_field, tag);
325}
326
327int bt_field_variant_get_tag_unsigned(struct bt_field *variant_field,
328 uint64_t *tag)
329{
330 return bt_field_common_variant_get_tag_unsigned((void *) variant_field, tag);
273b65be
JG
331}
332
839d52a5
PP
333struct bt_field_type_enumeration_mapping_iterator *
334bt_field_enumeration_get_mappings(struct bt_field *field)
cd95e351 335{
a6918753
PP
336 struct bt_field_enumeration *enum_field = (void *) field;
337
338 BT_ASSERT_PRE_NON_NULL(field, "Enumeration field");
339 BT_ASSERT_PRE_FIELD_COMMON_HAS_TYPE_ID((struct bt_field_common *) field,
340 BT_FIELD_TYPE_ID_ENUM, "Field");
341 BT_ASSERT_PRE_FIELD_COMMON_IS_SET((struct bt_field_common *) field,
342 "Enumeration field");
8deee039 343 return bt_field_common_enumeration_get_mappings((void *) field,
a6918753
PP
344 (bt_field_common_create_func) bt_field_create_recursive,
345 enum_field->common.payload.unsignd);
346}
347
348BT_ASSERT_PRE_FUNC
349static inline
350struct bt_field_type_common_integer *get_int_enum_int_ft(
351 struct bt_field *field)
352{
353 struct bt_field_common_integer *int_field = (void *) field;
354 struct bt_field_type_common_integer *int_ft = NULL;
355
356 if (int_field->common.type->id == BT_FIELD_TYPE_ID_INTEGER) {
357 int_ft = BT_FROM_COMMON(int_field->common.type);
358 } else if (int_field->common.type->id == BT_FIELD_TYPE_ID_ENUM) {
359 struct bt_field_type_common_enumeration *enum_ft =
360 BT_FROM_COMMON(int_field->common.type);
361 int_ft = enum_ft->container_ft;
362 }
363
364 BT_ASSERT(int_ft);
365 return int_ft;
cd95e351
JG
366}
367
8deee039 368int bt_field_integer_signed_get_value(struct bt_field *field, int64_t *value)
cd95e351 369{
a6918753
PP
370 struct bt_field_common_integer *integer = (void *) field;
371
372 BT_ASSERT_PRE_NON_NULL(field, "Integer/enumeration field");
373 BT_ASSERT_PRE_NON_NULL(value, "Value");
374 BT_ASSERT_PRE_FIELD_COMMON_IS_SET(BT_TO_COMMON(integer),
375 "Integer/enumeration field");
376 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer), "Field");
377 BT_ASSERT_PRE(bt_field_type_common_integer_is_signed(
378 BT_TO_COMMON(get_int_enum_int_ft(field))),
379 "Field's type is unsigned: %!+f", field);
380 *value = integer->payload.signd;
381 return 0;
cd95e351
JG
382}
383
a6918753 384int bt_field_integer_signed_set_value(struct bt_field *field, int64_t value)
273b65be 385{
a6918753
PP
386 int ret = 0;
387 struct bt_field_common_integer *integer = (void *) field;
388
389 BT_ASSERT_PRE_NON_NULL(field, "Integer field");
390 BT_ASSERT_PRE_FIELD_COMMON_HOT(BT_TO_COMMON(integer), "Integer field");
391 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer), "Field");
392 BT_ASSERT_PRE(bt_field_type_common_integer_is_signed(
393 BT_FROM_COMMON(get_int_enum_int_ft(field))),
394 "Field's type is unsigned: %!+f", field);
395 BT_ASSERT_PRE(value_is_in_range_signed(
396 get_int_enum_int_ft(field)->size, value),
397 "Value is out of bounds: value=%" PRId64 ", %![field-]+f",
398 value, field);
399 integer->payload.signd = value;
400 bt_field_set(field, true);
401 return ret;
273b65be
JG
402}
403
a6918753 404int bt_field_integer_unsigned_get_value(struct bt_field *field, uint64_t *value)
cd95e351 405{
a6918753
PP
406 struct bt_field_common_integer *integer = (void *) field;
407
408 BT_ASSERT_PRE_NON_NULL(field, "Integer field");
409 BT_ASSERT_PRE_NON_NULL(value, "Value");
410 BT_ASSERT_PRE_FIELD_COMMON_IS_SET(BT_TO_COMMON(integer), "Integer field");
411 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer), "Field");
412 BT_ASSERT_PRE(!bt_field_type_common_integer_is_signed(
413 BT_FROM_COMMON(get_int_enum_int_ft(field))),
414 "Field's type is signed: %!+f", field);
415 *value = integer->payload.unsignd;
416 return 0;
cd95e351
JG
417}
418
a6918753
PP
419int bt_field_integer_unsigned_set_value(struct bt_field *field,
420 uint64_t value)
273b65be 421{
a6918753
PP
422 struct bt_field_common_integer *integer = (void *) field;
423
424 BT_ASSERT_PRE_NON_NULL(field, "Integer field");
425 BT_ASSERT_PRE_FIELD_COMMON_HOT(BT_TO_COMMON(integer), "Integer field");
426 BT_ASSERT_PRE_FIELD_IS_INT_OR_ENUM(BT_TO_COMMON(integer), "Field");
427 BT_ASSERT_PRE(!bt_field_type_common_integer_is_signed(
428 BT_FROM_COMMON(get_int_enum_int_ft(field))),
429 "Field's type is signed: %!+f", field);
430 BT_ASSERT_PRE(value_is_in_range_unsigned(
431 get_int_enum_int_ft(field)->size, value),
432 "Value is out of bounds: value=%" PRIu64 ", %![field-]+f",
433 value, field);
434 integer->payload.unsignd = value;
435 bt_field_set(field, true);
436 return 0;
8b45963b
PP
437}
438
8deee039
PP
439int bt_field_floating_point_get_value(struct bt_field *field,
440 double *value)
8b45963b 441{
8deee039 442 return bt_field_common_floating_point_get_value((void *) field, value);
273b65be
JG
443}
444
8deee039
PP
445int bt_field_floating_point_set_value(struct bt_field *field,
446 double value)
cd95e351 447{
8deee039 448 return bt_field_common_floating_point_set_value((void *) field, value);
8b45963b 449}
fc25abce 450
8deee039 451const char *bt_field_string_get_value(struct bt_field *field)
8b45963b 452{
8deee039 453 return bt_field_common_string_get_value((void *) field);
cd95e351
JG
454}
455
8deee039 456int bt_field_string_set_value(struct bt_field *field, const char *value)
273b65be 457{
8deee039 458 return bt_field_common_string_set_value((void *) field, value);
273b65be
JG
459}
460
8deee039 461int bt_field_string_append(struct bt_field *field, const char *value)
cd95e351 462{
8deee039 463 return bt_field_common_string_append((void *) field, value);
cd95e351
JG
464}
465
8deee039
PP
466int bt_field_string_append_len(struct bt_field *field,
467 const char *value, unsigned int length)
273b65be 468{
8deee039 469 return bt_field_common_string_append_len((void *) field, value, length);
273b65be
JG
470}
471
a6918753
PP
472int bt_field_string_clear(struct bt_field *string_field)
473{
474 return bt_field_common_string_clear((void *) string_field);
475}
476
8deee039
PP
477BT_HIDDEN
478struct bt_field_common *bt_field_common_copy(struct bt_field_common *field)
c6f9c5a3 479{
8deee039 480 struct bt_field_common *copy = NULL;
c6f9c5a3 481
8deee039
PP
482 BT_ASSERT_PRE_NON_NULL(field, "Field");
483 BT_ASSERT(field_type_common_has_known_id(field->type));
484 BT_ASSERT(field->methods->copy);
485 copy = field->methods->copy(field);
486 if (!copy) {
487 BT_LOGW("Cannot create field: ft-addr=%p", field->type);
488 goto end;
c6f9c5a3
PP
489 }
490
8deee039
PP
491 bt_field_common_set(copy, field->payload_set);
492
493end:
494 return copy;
c6f9c5a3
PP
495}
496
a6918753
PP
497static
498void bt_field_integer_destroy(struct bt_field *field)
8deee039 499{
a6918753
PP
500 BT_LOGD("Destroying integer field object: addr=%p", field);
501 bt_field_common_integer_finalize((void *) field);
502 g_free(field);
8deee039 503}
f98c6554 504
a6918753
PP
505static
506void bt_field_floating_point_destroy(struct bt_field *field)
8deee039 507{
a6918753
PP
508 BT_LOGD("Destroying floating point field object: addr=%p", field);
509 bt_field_common_floating_point_finalize((void *) field);
510 g_free(field);
f98c6554
PP
511}
512
a6918753
PP
513static
514void bt_field_enumeration_destroy(struct bt_field *field)
273b65be 515{
a6918753
PP
516 BT_LOGD("Destroying enumeration field object: addr=%p", field);
517 bt_field_common_finalize((void *) field);
518 g_free(field);
8deee039 519}
273b65be 520
a6918753
PP
521static
522void bt_field_structure_destroy_recursive(struct bt_field *field)
8deee039 523{
a6918753
PP
524 BT_LOGD("Destroying structure field object: addr=%p", field);
525 bt_field_common_structure_finalize_recursive((void *) field);
526 g_free(field);
273b65be
JG
527}
528
a6918753
PP
529static
530void bt_field_variant_destroy_recursive(struct bt_field *field)
8deee039 531{
a6918753
PP
532 BT_LOGD("Destroying variant field object: addr=%p", field);
533 bt_field_common_variant_finalize_recursive((void *) field);
534 g_free(field);
8b45963b 535}
12c8a1a3 536
a6918753
PP
537static
538void bt_field_array_destroy_recursive(struct bt_field *field)
8b45963b 539{
a6918753
PP
540 BT_LOGD("Destroying array field object: addr=%p", field);
541 bt_field_common_array_finalize_recursive((void *) field);
542 g_free(field);
12c8a1a3
JG
543}
544
a6918753
PP
545static
546void bt_field_sequence_destroy_recursive(struct bt_field *field)
273b65be 547{
a6918753
PP
548 BT_LOGD("Destroying sequence field object: addr=%p", field);
549 bt_field_common_sequence_finalize_recursive((void *) field);
550 g_free(field);
273b65be
JG
551}
552
a6918753
PP
553static
554void bt_field_string_destroy(struct bt_field *field)
76f869ab 555{
a6918753
PP
556 BT_LOGD("Destroying string field object: addr=%p", field);
557 bt_field_common_string_finalize((void *) field);
558 g_free(field);
76f869ab
JG
559}
560
8deee039 561BT_HIDDEN
a6918753 562void bt_field_destroy_recursive(struct bt_field *field)
87d43dc1 563{
a6918753 564 struct bt_field_common *field_common = (void *) field;
87d43dc1 565
a6918753
PP
566 if (!field) {
567 return;
87d43dc1 568 }
8b45963b 569
a6918753
PP
570 BT_ASSERT(field_type_common_has_known_id((void *) field_common->type));
571 field_destroy_funcs[field_common->type->id](field);
87d43dc1
JG
572}
573
273b65be 574static
839d52a5 575struct bt_field *bt_field_integer_create(struct bt_field_type *type)
273b65be 576{
8deee039
PP
577 struct bt_field_common_integer *integer =
578 g_new0(struct bt_field_common_integer, 1);
273b65be 579
fc25abce
PP
580 BT_LOGD("Creating integer field object: ft-addr=%p", type);
581
582 if (integer) {
8deee039 583 bt_field_common_initialize(BT_TO_COMMON(integer), (void *) type,
a6918753 584 NULL, &bt_field_integer_methods);
fc25abce 585 BT_LOGD("Created integer field object: addr=%p, ft-addr=%p",
8deee039 586 integer, type);
fc25abce
PP
587 } else {
588 BT_LOGE_STR("Failed to allocate one integer field.");
589 }
590
8deee039 591 return (void *) integer;
273b65be
JG
592}
593
594static
8deee039 595struct bt_field *bt_field_enumeration_create(struct bt_field_type *type)
273b65be 596{
a6918753
PP
597 struct bt_field_enumeration *enumeration = g_new0(
598 struct bt_field_enumeration, 1);
273b65be 599
fc25abce
PP
600 BT_LOGD("Creating enumeration field object: ft-addr=%p", type);
601
602 if (enumeration) {
a6918753
PP
603 bt_field_common_initialize(
604 BT_TO_COMMON(BT_TO_COMMON(enumeration)),
605 (void *) type, NULL, &bt_field_enumeration_methods);
fc25abce 606 BT_LOGD("Created enumeration field object: addr=%p, ft-addr=%p",
8deee039 607 enumeration, type);
fc25abce
PP
608 } else {
609 BT_LOGE_STR("Failed to allocate one enumeration field.");
610 }
611
8deee039 612 return (void *) enumeration;
273b65be
JG
613}
614
615static
8deee039 616struct bt_field *bt_field_floating_point_create(struct bt_field_type *type)
273b65be 617{
8deee039 618 struct bt_field_common_floating_point *floating_point;
273b65be 619
fc25abce 620 BT_LOGD("Creating floating point number field object: ft-addr=%p", type);
8deee039 621 floating_point = g_new0(struct bt_field_common_floating_point, 1);
fc25abce
PP
622
623 if (floating_point) {
8deee039 624 bt_field_common_initialize(BT_TO_COMMON(floating_point),
a6918753 625 (void *) type, NULL, &bt_field_floating_point_methods);
fc25abce 626 BT_LOGD("Created floating point number field object: addr=%p, ft-addr=%p",
8deee039 627 floating_point, type);
fc25abce
PP
628 } else {
629 BT_LOGE_STR("Failed to allocate one floating point number field.");
630 }
631
8deee039 632 return (void *) floating_point;
273b65be
JG
633}
634
8deee039
PP
635BT_HIDDEN
636int bt_field_common_structure_initialize(struct bt_field_common *field,
637 struct bt_field_type_common *type,
638 bt_object_release_func release_func,
639 struct bt_field_common_methods *methods,
a6918753
PP
640 bt_field_common_create_func field_create_func,
641 GDestroyNotify field_release_func)
8deee039
PP
642{
643 int ret = 0;
644 struct bt_field_type_common_structure *structure_type =
645 BT_FROM_COMMON(type);
646 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
9be89173 647 size_t i;
273b65be 648
8deee039
PP
649 BT_LOGD("Initializing common structure field object: ft-addr=%p", type);
650 bt_field_common_initialize(field, type, release_func, methods);
a6918753 651 structure->fields = g_ptr_array_new_with_free_func(field_release_func);
8deee039 652 g_ptr_array_set_size(structure->fields, structure_type->fields->len);
9be89173 653
a6918753 654 /* Create all fields contained in the structure field. */
9be89173 655 for (i = 0; i < structure_type->fields->len; i++) {
8deee039 656 struct bt_field_common *field;
a6918753
PP
657 struct bt_field_type_common_structure_field *struct_field =
658 BT_FIELD_TYPE_COMMON_STRUCTURE_FIELD_AT_INDEX(
659 structure_type, i);
8deee039 660 field = field_create_func(struct_field->type);
9be89173
JG
661 if (!field) {
662 BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu",
8deee039
PP
663 g_quark_to_string(struct_field->name), i);
664 ret = -1;
9be89173
JG
665 goto end;
666 }
667
668 g_ptr_array_index(structure->fields, i) = field;
669 }
670
8deee039
PP
671 BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p",
672 field, type);
673
273b65be 674end:
9be89173 675 return ret;
273b65be
JG
676}
677
8deee039
PP
678static
679struct bt_field *bt_field_structure_create(struct bt_field_type *type)
680{
681 struct bt_field_common_structure *structure = g_new0(
682 struct bt_field_common_structure, 1);
683 int iret;
684
685 BT_LOGD("Creating structure field object: ft-addr=%p", type);
686
687 if (!structure) {
688 BT_LOGE_STR("Failed to allocate one structure field.");
689 goto end;
690 }
691
692 iret = bt_field_common_structure_initialize(BT_TO_COMMON(structure),
a6918753
PP
693 (void *) type, NULL, &bt_field_structure_methods,
694 (bt_field_common_create_func) bt_field_create_recursive,
695 (GDestroyNotify) bt_field_destroy_recursive);
8deee039
PP
696 if (iret) {
697 BT_PUT(structure);
698 goto end;
699 }
700
701 BT_LOGD("Created structure field object: addr=%p, ft-addr=%p",
702 structure, type);
703
704end:
705 return (void *) structure;
706}
707
a6918753
PP
708BT_HIDDEN
709int bt_field_common_variant_initialize(struct bt_field_common *field,
710 struct bt_field_type_common *type,
711 bt_object_release_func release_func,
712 struct bt_field_common_methods *methods,
713 bt_field_common_create_func field_create_func,
714 GDestroyNotify field_release_func)
715{
716 int ret = 0;
717 struct bt_field_type_common_variant *variant_type =
718 BT_FROM_COMMON(type);
719 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
720 size_t i;
721
722 BT_LOGD("Initializing common variant field object: ft-addr=%p", type);
723 bt_field_common_initialize(field, type, release_func, methods);
724 ret = bt_field_type_common_variant_update_choices(type);
725 if (ret) {
726 BT_LOGE("Cannot update common variant field type choices: "
727 "ret=%d", ret);
728 goto end;
729 }
730
731 variant->fields = g_ptr_array_new_with_free_func(field_release_func);
732 g_ptr_array_set_size(variant->fields, variant_type->choices->len);
733
734 /* Create all fields contained in the variant field. */
735 for (i = 0; i < variant_type->choices->len; i++) {
736 struct bt_field_common *field;
737 struct bt_field_type_common_variant_choice *var_choice =
738 BT_FIELD_TYPE_COMMON_VARIANT_CHOICE_AT_INDEX(
739 variant_type, i);
740
741 field = field_create_func(var_choice->type);
742 if (!field) {
743 BT_LOGE("Failed to create variant field's member: name=\"%s\", index=%zu",
744 g_quark_to_string(var_choice->name), i);
745 ret = -1;
746 goto end;
747 }
748
749 g_ptr_array_index(variant->fields, i) = field;
750 }
751
752 BT_LOGD("Initialized common variant field object: addr=%p, ft-addr=%p",
753 field, type);
754
755end:
756 return ret;
757}
758
273b65be 759static
839d52a5 760struct bt_field *bt_field_variant_create(struct bt_field_type *type)
273b65be 761{
8deee039
PP
762 struct bt_field_common_variant *variant = g_new0(
763 struct bt_field_common_variant, 1);
a6918753 764 int iret;
fc25abce
PP
765
766 BT_LOGD("Creating variant field object: ft-addr=%p", type);
767
a6918753 768 if (!variant) {
fc25abce 769 BT_LOGE_STR("Failed to allocate one variant field.");
a6918753
PP
770 goto end;
771 }
772
773 iret = bt_field_common_variant_initialize(BT_TO_COMMON(variant),
774 (void *) type, NULL, &bt_field_variant_methods,
775 (bt_field_common_create_func) bt_field_create_recursive,
776 (GDestroyNotify) bt_field_destroy_recursive);
777 if (iret) {
778 BT_PUT(variant);
779 goto end;
fc25abce
PP
780 }
781
a6918753
PP
782 BT_LOGD("Created variant field object: addr=%p, ft-addr=%p",
783 variant, type);
784
785end:
8deee039 786 return (void *) variant;
273b65be
JG
787}
788
8deee039
PP
789BT_HIDDEN
790int bt_field_common_array_initialize(struct bt_field_common *field,
791 struct bt_field_type_common *type,
792 bt_object_release_func release_func,
a6918753
PP
793 struct bt_field_common_methods *methods,
794 bt_field_common_create_func field_create_func,
795 GDestroyNotify field_destroy_func)
273b65be 796{
8deee039
PP
797 struct bt_field_type_common_array *array_type = BT_FROM_COMMON(type);
798 struct bt_field_common_array *array = BT_FROM_COMMON(field);
273b65be 799 unsigned int array_length;
8deee039 800 int ret = 0;
a6918753 801 uint64_t i;
273b65be 802
8deee039 803 BT_LOGD("Initializing common array field object: ft-addr=%p", type);
8b45963b 804 BT_ASSERT(type);
8deee039 805 bt_field_common_initialize(field, type, release_func, methods);
273b65be 806 array_length = array_type->length;
fe0fe95c 807 array->elements = g_ptr_array_sized_new(array_length);
273b65be 808 if (!array->elements) {
8deee039
PP
809 ret = -1;
810 goto end;
273b65be
JG
811 }
812
a6918753 813 g_ptr_array_set_free_func(array->elements, field_destroy_func);
273b65be 814 g_ptr_array_set_size(array->elements, array_length);
a6918753
PP
815
816 for (i = 0; i < array_length; i++) {
817 array->elements->pdata[i] = field_create_func(
818 array_type->element_ft);
819 if (!array->elements->pdata[i]) {
820 ret = -1;
821 goto end;
822 }
823 }
824
8deee039
PP
825 BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p",
826 field, type);
273b65be 827
8deee039
PP
828end:
829 return ret;
273b65be
JG
830}
831
832static
8deee039 833struct bt_field *bt_field_array_create(struct bt_field_type *type)
273b65be 834{
8deee039
PP
835 struct bt_field_common_array *array =
836 g_new0(struct bt_field_common_array, 1);
837 int ret;
fc25abce 838
8deee039
PP
839 BT_LOGD("Creating array field object: ft-addr=%p", type);
840 BT_ASSERT(type);
fc25abce 841
8deee039
PP
842 if (!array) {
843 BT_LOGE_STR("Failed to allocate one array field.");
844 goto end;
fc25abce
PP
845 }
846
8deee039 847 ret = bt_field_common_array_initialize(BT_TO_COMMON(array),
a6918753
PP
848 (void *) type, NULL, &bt_field_array_methods,
849 (bt_field_common_create_func) bt_field_create_recursive,
850 (GDestroyNotify) bt_field_destroy_recursive);
8deee039
PP
851 if (ret) {
852 BT_PUT(array);
853 goto end;
854 }
273b65be 855
8deee039
PP
856 BT_LOGD("Created array field object: addr=%p, ft-addr=%p",
857 array, type);
273b65be 858
8deee039
PP
859end:
860 return (void *) array;
273b65be
JG
861}
862
a6918753
PP
863BT_HIDDEN
864int bt_field_common_sequence_initialize(struct bt_field_common *field,
865 struct bt_field_type_common *type,
866 bt_object_release_func release_func,
867 struct bt_field_common_methods *methods,
868 GDestroyNotify field_destroy_func)
869{
870 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
871 int ret = 0;
872
873 BT_LOGD("Initializing common sequence field object: ft-addr=%p", type);
874 BT_ASSERT(type);
875 bt_field_common_initialize(field, type, release_func, methods);
876 sequence->elements = g_ptr_array_new();
877 if (!sequence->elements) {
878 ret = -1;
879 goto end;
880 }
881
882 g_ptr_array_set_free_func(sequence->elements, field_destroy_func);
883 BT_LOGD("Initialized common sequence field object: addr=%p, ft-addr=%p",
884 field, type);
885
886end:
887 return ret;
888}
889
273b65be 890static
8deee039 891struct bt_field *bt_field_sequence_create(struct bt_field_type *type)
273b65be 892{
a6918753
PP
893 struct bt_field_common_sequence *sequence =
894 g_new0(struct bt_field_common_sequence, 1);
895 int ret;
273b65be 896
8deee039 897 BT_LOGD("Creating sequence field object: ft-addr=%p", type);
a6918753 898 BT_ASSERT(type);
273b65be 899
a6918753 900 if (!sequence) {
8deee039 901 BT_LOGE_STR("Failed to allocate one sequence field.");
a6918753 902 goto end;
4fef87ab 903 }
8deee039 904
a6918753
PP
905 ret = bt_field_common_sequence_initialize(BT_TO_COMMON(sequence),
906 (void *) type, NULL, &bt_field_sequence_methods,
907 (GDestroyNotify) bt_field_destroy_recursive);
908 if (ret) {
909 BT_PUT(sequence);
910 goto end;
911 }
912
913 BT_LOGD("Created sequence field object: addr=%p, ft-addr=%p",
914 sequence, type);
915
916end:
8deee039 917 return (void *) sequence;
273b65be
JG
918}
919
920static
8deee039 921struct bt_field *bt_field_string_create(struct bt_field_type *type)
273b65be 922{
8deee039
PP
923 struct bt_field_common_string *string = g_new0(
924 struct bt_field_common_string, 1);
fc25abce 925
8deee039
PP
926 BT_LOGD("Creating string field object: ft-addr=%p", type);
927
928 if (string) {
929 bt_field_common_initialize(BT_TO_COMMON(string),
a6918753 930 (void *) type, NULL, &bt_field_string_methods);
8deee039
PP
931 BT_LOGD("Created string field object: addr=%p, ft-addr=%p",
932 string, type);
933 } else {
934 BT_LOGE_STR("Failed to allocate one string field.");
9b2b7163 935 }
8deee039
PP
936
937 return (void *) string;
273b65be
JG
938}
939
8deee039
PP
940BT_HIDDEN
941int bt_field_common_generic_validate(struct bt_field_common *field)
273b65be 942{
da2f6971 943 return (field && field->payload_set) ? 0 : -1;
273b65be
JG
944}
945
8deee039
PP
946BT_HIDDEN
947int bt_field_common_structure_validate_recursive(struct bt_field_common *field)
273b65be 948{
fc25abce 949 int64_t i;
273b65be 950 int ret = 0;
8deee039 951 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
273b65be 952
8b45963b 953 BT_ASSERT(field);
8b45963b 954
273b65be 955 for (i = 0; i < structure->fields->len; i++) {
8deee039 956 ret = bt_field_common_validate_recursive(
8b45963b 957 (void *) structure->fields->pdata[i]);
fc25abce 958
273b65be 959 if (ret) {
fc25abce 960 int this_ret;
6ce12048 961 const char *name;
6ce12048 962
5fe68922 963 this_ret = bt_field_type_common_structure_borrow_field_by_index(
8b45963b
PP
964 field->type, &name, NULL, i);
965 BT_ASSERT(this_ret == 0);
966 BT_ASSERT_PRE_MSG("Invalid structure field's field: "
8deee039
PP
967 "%![struct-field-]+_f, field-name=\"%s\", "
968 "index=%" PRId64 ", %![field-]+_f",
8b45963b 969 field, name, i, structure->fields->pdata[i]);
273b65be
JG
970 goto end;
971 }
972 }
8b45963b 973
273b65be
JG
974end:
975 return ret;
976}
977
8deee039
PP
978BT_HIDDEN
979int bt_field_common_variant_validate_recursive(struct bt_field_common *field)
273b65be
JG
980{
981 int ret = 0;
8deee039 982 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
273b65be 983
8b45963b 984 BT_ASSERT(field);
a6918753
PP
985
986 if (!variant->current_field) {
987 ret = -1;
988 goto end;
fc25abce 989 }
8b45963b 990
a6918753
PP
991 ret = bt_field_common_validate_recursive(variant->current_field);
992
993end:
273b65be
JG
994 return ret;
995}
996
8deee039
PP
997BT_HIDDEN
998int bt_field_common_array_validate_recursive(struct bt_field_common *field)
273b65be 999{
fc25abce 1000 int64_t i;
273b65be 1001 int ret = 0;
8deee039 1002 struct bt_field_common_array *array = BT_FROM_COMMON(field);
273b65be 1003
8b45963b 1004 BT_ASSERT(field);
8deee039 1005
273b65be 1006 for (i = 0; i < array->elements->len; i++) {
8deee039 1007 ret = bt_field_common_validate_recursive((void *) array->elements->pdata[i]);
273b65be 1008 if (ret) {
8b45963b 1009 BT_ASSERT_PRE_MSG("Invalid array field's element field: "
8deee039
PP
1010 "%![array-field-]+_f, " PRId64 ", "
1011 "%![elem-field-]+_f",
8b45963b 1012 field, i, array->elements->pdata[i]);
273b65be
JG
1013 goto end;
1014 }
1015 }
8b45963b 1016
273b65be
JG
1017end:
1018 return ret;
1019}
1020
8deee039
PP
1021BT_HIDDEN
1022int bt_field_common_sequence_validate_recursive(struct bt_field_common *field)
273b65be
JG
1023{
1024 size_t i;
1025 int ret = 0;
8deee039 1026 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
273b65be 1027
8b45963b 1028 BT_ASSERT(field);
8deee039 1029
273b65be 1030 for (i = 0; i < sequence->elements->len; i++) {
8deee039 1031 ret = bt_field_common_validate_recursive(
8b45963b 1032 (void *) sequence->elements->pdata[i]);
273b65be 1033 if (ret) {
8b45963b 1034 BT_ASSERT_PRE_MSG("Invalid sequence field's element field: "
8deee039
PP
1035 "%![seq-field-]+_f, " PRId64 ", "
1036 "%![elem-field-]+_f",
8b45963b 1037 field, i, sequence->elements->pdata[i]);
273b65be
JG
1038 goto end;
1039 }
1040 }
1041end:
1042 return ret;
1043}
1044
8deee039
PP
1045BT_HIDDEN
1046void bt_field_common_generic_reset(struct bt_field_common *field)
12c8a1a3 1047{
8b45963b 1048 BT_ASSERT(field);
a745c331 1049 field->payload_set = false;
12c8a1a3
JG
1050}
1051
8deee039
PP
1052BT_HIDDEN
1053void bt_field_common_structure_reset_recursive(struct bt_field_common *field)
12c8a1a3 1054{
fc25abce 1055 int64_t i;
8deee039 1056 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
12c8a1a3 1057
8b45963b 1058 BT_ASSERT(field);
8b45963b 1059
12c8a1a3 1060 for (i = 0; i < structure->fields->len; i++) {
8deee039 1061 struct bt_field_common *member = structure->fields->pdata[i];
12c8a1a3
JG
1062
1063 if (!member) {
1064 /*
8b45963b
PP
1065 * Structure members are lazily initialized;
1066 * skip if this member has not been allocated
1067 * yet.
12c8a1a3
JG
1068 */
1069 continue;
1070 }
1071
8deee039 1072 bt_field_common_reset_recursive(member);
12c8a1a3 1073 }
12c8a1a3
JG
1074}
1075
8deee039
PP
1076BT_HIDDEN
1077void bt_field_common_variant_reset_recursive(struct bt_field_common *field)
12c8a1a3 1078{
8deee039 1079 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
12c8a1a3 1080
8b45963b 1081 BT_ASSERT(field);
a6918753 1082 variant->current_field = NULL;
12c8a1a3
JG
1083}
1084
8deee039
PP
1085BT_HIDDEN
1086void bt_field_common_array_reset_recursive(struct bt_field_common *field)
12c8a1a3
JG
1087{
1088 size_t i;
8deee039 1089 struct bt_field_common_array *array = BT_FROM_COMMON(field);
12c8a1a3 1090
8b45963b 1091 BT_ASSERT(field);
8b45963b 1092
12c8a1a3 1093 for (i = 0; i < array->elements->len; i++) {
8deee039 1094 struct bt_field_common *member = array->elements->pdata[i];
12c8a1a3
JG
1095
1096 if (!member) {
1097 /*
8b45963b
PP
1098 * Array elements are lazily initialized; skip
1099 * if this member has not been allocated yet.
12c8a1a3
JG
1100 */
1101 continue;
1102 }
1103
8deee039 1104 bt_field_common_reset_recursive(member);
12c8a1a3 1105 }
12c8a1a3
JG
1106}
1107
8deee039
PP
1108BT_HIDDEN
1109void bt_field_common_sequence_reset_recursive(struct bt_field_common *field)
12c8a1a3 1110{
8deee039 1111 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
a6918753 1112 uint64_t i;
12c8a1a3 1113
8b45963b 1114 BT_ASSERT(field);
8b45963b 1115
a6918753
PP
1116 for (i = 0; i < sequence->elements->len; i++) {
1117 if (sequence->elements->pdata[i]) {
1118 bt_field_common_reset_recursive(
1119 sequence->elements->pdata[i]);
1120 }
12c8a1a3 1121 }
8b45963b 1122
a6918753 1123 sequence->length = 0;
12c8a1a3
JG
1124}
1125
8deee039
PP
1126BT_HIDDEN
1127void bt_field_common_string_reset(struct bt_field_common *field)
12c8a1a3 1128{
8deee039 1129 struct bt_field_common_string *string = BT_FROM_COMMON(field);
12c8a1a3 1130
8b45963b 1131 BT_ASSERT(field);
8deee039
PP
1132 bt_field_common_generic_reset(field);
1133
12c8a1a3
JG
1134 if (string->payload) {
1135 g_string_truncate(string->payload, 0);
1136 }
12c8a1a3
JG
1137}
1138
8deee039 1139BT_HIDDEN
a6918753
PP
1140void bt_field_common_generic_set_is_frozen(struct bt_field_common *field,
1141 bool is_frozen)
918be005 1142{
a6918753 1143 field->frozen = is_frozen;
918be005
PP
1144}
1145
8deee039 1146BT_HIDDEN
a6918753
PP
1147void bt_field_common_structure_set_is_frozen_recursive(
1148 struct bt_field_common *field, bool is_frozen)
918be005 1149{
a6918753 1150 uint64_t i;
8deee039
PP
1151 struct bt_field_common_structure *structure_field =
1152 BT_FROM_COMMON(field);
918be005 1153
fc25abce
PP
1154 BT_LOGD("Freezing structure field object: addr=%p", field);
1155
918be005 1156 for (i = 0; i < structure_field->fields->len; i++) {
a6918753 1157 struct bt_field_common *struct_field =
918be005
PP
1158 g_ptr_array_index(structure_field->fields, i);
1159
fc25abce 1160 BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64,
a6918753
PP
1161 struct_field, i);
1162 bt_field_common_set_is_frozen_recursive(struct_field,
1163 is_frozen);
918be005
PP
1164 }
1165
a6918753 1166 bt_field_common_generic_set_is_frozen(field, is_frozen);
918be005
PP
1167}
1168
8deee039 1169BT_HIDDEN
a6918753
PP
1170void bt_field_common_variant_set_is_frozen_recursive(
1171 struct bt_field_common *field, bool is_frozen)
918be005 1172{
a6918753 1173 uint64_t i;
8deee039 1174 struct bt_field_common_variant *variant_field = BT_FROM_COMMON(field);
918be005 1175
fc25abce 1176 BT_LOGD("Freezing variant field object: addr=%p", field);
a6918753
PP
1177
1178 for (i = 0; i < variant_field->fields->len; i++) {
1179 struct bt_field_common *var_field =
1180 g_ptr_array_index(variant_field->fields, i);
1181
1182 BT_LOGD("Freezing variant field's field: field-addr=%p, index=%" PRId64,
1183 var_field, i);
1184 bt_field_common_set_is_frozen_recursive(var_field, is_frozen);
1185 }
1186
1187 bt_field_common_generic_set_is_frozen(field, is_frozen);
918be005
PP
1188}
1189
8deee039 1190BT_HIDDEN
a6918753
PP
1191void bt_field_common_array_set_is_frozen_recursive(
1192 struct bt_field_common *field, bool is_frozen)
918be005 1193{
fc25abce 1194 int64_t i;
8deee039 1195 struct bt_field_common_array *array_field = BT_FROM_COMMON(field);
918be005 1196
fc25abce
PP
1197 BT_LOGD("Freezing array field object: addr=%p", field);
1198
918be005 1199 for (i = 0; i < array_field->elements->len; i++) {
8deee039 1200 struct bt_field_common *elem_field =
918be005
PP
1201 g_ptr_array_index(array_field->elements, i);
1202
fc25abce
PP
1203 BT_LOGD("Freezing array field object's element field: "
1204 "element-field-addr=%p, index=%" PRId64,
1205 elem_field, i);
a6918753 1206 bt_field_common_set_is_frozen_recursive(elem_field, is_frozen);
918be005
PP
1207 }
1208
a6918753 1209 bt_field_common_generic_set_is_frozen(field, is_frozen);
918be005
PP
1210}
1211
8deee039 1212BT_HIDDEN
a6918753
PP
1213void bt_field_common_sequence_set_is_frozen_recursive(
1214 struct bt_field_common *field, bool is_frozen)
918be005 1215{
fc25abce 1216 int64_t i;
8deee039
PP
1217 struct bt_field_common_sequence *sequence_field =
1218 BT_FROM_COMMON(field);
918be005 1219
fc25abce 1220 BT_LOGD("Freezing sequence field object: addr=%p", field);
918be005 1221
a6918753 1222 for (i = 0; i < sequence_field->length; i++) {
8deee039 1223 struct bt_field_common *elem_field =
918be005
PP
1224 g_ptr_array_index(sequence_field->elements, i);
1225
fc25abce
PP
1226 BT_LOGD("Freezing sequence field object's element field: "
1227 "element-field-addr=%p, index=%" PRId64,
1228 elem_field, i);
a6918753 1229 bt_field_common_set_is_frozen_recursive(elem_field, is_frozen);
918be005
PP
1230 }
1231
a6918753 1232 bt_field_common_generic_set_is_frozen(field, is_frozen);
918be005
PP
1233}
1234
1235BT_HIDDEN
a6918753
PP
1236void _bt_field_common_set_is_frozen_recursive(struct bt_field_common *field,
1237 bool is_frozen)
918be005
PP
1238{
1239 if (!field) {
1240 goto end;
1241 }
1242
fc25abce 1243 if (field->frozen) {
35f77de4
JG
1244 goto end;
1245 }
918be005 1246
a6918753
PP
1247 BT_LOGD("Setting field object's frozen state: addr=%p, is-frozen=%d",
1248 field, is_frozen);
8deee039 1249 BT_ASSERT(field_type_common_has_known_id(field->type));
a6918753
PP
1250 BT_ASSERT(field->methods->set_is_frozen);
1251 field->methods->set_is_frozen(field, is_frozen);
8deee039 1252
918be005
PP
1253end:
1254 return;
1255}
76f869ab 1256
8deee039
PP
1257BT_HIDDEN
1258bt_bool bt_field_common_generic_is_set(struct bt_field_common *field)
76f869ab
JG
1259{
1260 return field && field->payload_set;
1261}
1262
8deee039
PP
1263BT_HIDDEN
1264bt_bool bt_field_common_structure_is_set_recursive(
1265 struct bt_field_common *field)
76f869ab 1266{
53d65c1d 1267 bt_bool is_set = BT_FALSE;
76f869ab 1268 size_t i;
8deee039 1269 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
76f869ab 1270
8b45963b 1271 BT_ASSERT(field);
8deee039 1272
76f869ab 1273 for (i = 0; i < structure->fields->len; i++) {
8deee039 1274 is_set = bt_field_common_is_set_recursive(
ab706977 1275 structure->fields->pdata[i]);
53d65c1d 1276 if (!is_set) {
76f869ab
JG
1277 goto end;
1278 }
1279 }
8deee039 1280
76f869ab 1281end:
53d65c1d 1282 return is_set;
76f869ab
JG
1283}
1284
8deee039
PP
1285BT_HIDDEN
1286bt_bool bt_field_common_variant_is_set_recursive(struct bt_field_common *field)
76f869ab 1287{
8deee039 1288 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
a6918753 1289 bt_bool is_set = BT_FALSE;
76f869ab 1290
8b45963b 1291 BT_ASSERT(field);
a6918753
PP
1292
1293 if (variant->current_field) {
1294 is_set = bt_field_common_is_set_recursive(
1295 variant->current_field);
1296 }
1297
1298 return is_set;
76f869ab
JG
1299}
1300
8deee039
PP
1301BT_HIDDEN
1302bt_bool bt_field_common_array_is_set_recursive(struct bt_field_common *field)
76f869ab
JG
1303{
1304 size_t i;
53d65c1d 1305 bt_bool is_set = BT_FALSE;
8deee039 1306 struct bt_field_common_array *array = BT_FROM_COMMON(field);
76f869ab 1307
8b45963b 1308 BT_ASSERT(field);
8deee039 1309
76f869ab 1310 for (i = 0; i < array->elements->len; i++) {
8deee039 1311 is_set = bt_field_common_is_set_recursive(array->elements->pdata[i]);
53d65c1d 1312 if (!is_set) {
76f869ab
JG
1313 goto end;
1314 }
1315 }
8deee039 1316
76f869ab 1317end:
53d65c1d 1318 return is_set;
76f869ab
JG
1319}
1320
8deee039
PP
1321BT_HIDDEN
1322bt_bool bt_field_common_sequence_is_set_recursive(struct bt_field_common *field)
76f869ab
JG
1323{
1324 size_t i;
53d65c1d 1325 bt_bool is_set = BT_FALSE;
8deee039 1326 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
76f869ab 1327
8b45963b 1328 BT_ASSERT(field);
8deee039 1329
7d7fae6d
JG
1330 if (!sequence->elements) {
1331 goto end;
1332 }
1333
76f869ab 1334 for (i = 0; i < sequence->elements->len; i++) {
8deee039
PP
1335 is_set = bt_field_common_is_set_recursive(
1336 sequence->elements->pdata[i]);
53d65c1d 1337 if (!is_set) {
76f869ab
JG
1338 goto end;
1339 }
1340 }
8deee039 1341
76f869ab 1342end:
53d65c1d 1343 return is_set;
76f869ab 1344}
This page took 0.135037 seconds and 4 git commands to generate.