Split CTF IR and CTF writer APIs and implementations
[babeltrace.git] / lib / ctf-ir / fields.c
1 /*
2 * fields.c
3 *
4 * Babeltrace CTF IR - Event Fields
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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
29 #define BT_LOG_TAG "FIELDS"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/ctf-ir/fields-internal.h>
34 #include <babeltrace/ctf-ir/field-types-internal.h>
35 #include <babeltrace/object-internal.h>
36 #include <babeltrace/ref.h>
37 #include <babeltrace/compiler-internal.h>
38 #include <babeltrace/compat/fcntl-internal.h>
39 #include <babeltrace/align-internal.h>
40 #include <babeltrace/assert-internal.h>
41 #include <inttypes.h>
42
43 static
44 struct bt_field_common *bt_field_integer_copy(struct bt_field_common *src);
45
46 static
47 struct bt_field_common *bt_field_enumeration_copy_recursive(
48 struct bt_field_common *src);
49
50 static
51 struct bt_field_common *bt_field_floating_point_copy(
52 struct bt_field_common *src);
53
54 static
55 struct bt_field_common *bt_field_structure_copy_recursive(
56 struct bt_field_common *src);
57
58 static
59 struct bt_field_common *bt_field_variant_copy_recursive(
60 struct bt_field_common *src);
61
62 static
63 struct bt_field_common *bt_field_array_copy_recursive(
64 struct bt_field_common *src);
65
66 static
67 struct bt_field_common *bt_field_sequence_copy_recursive(
68 struct bt_field_common *src);
69
70 static
71 struct bt_field_common *bt_field_string_copy(struct bt_field_common *src);
72
73 static struct bt_field_common_methods bt_field_integer_methods = {
74 .freeze = bt_field_common_generic_freeze,
75 .validate = bt_field_common_generic_validate,
76 .copy = bt_field_integer_copy,
77 .is_set = bt_field_common_generic_is_set,
78 .reset = bt_field_common_generic_reset,
79 };
80
81 static struct bt_field_common_methods bt_field_floating_point_methods = {
82 .freeze = bt_field_common_generic_freeze,
83 .validate = bt_field_common_generic_validate,
84 .copy = bt_field_floating_point_copy,
85 .is_set = bt_field_common_generic_is_set,
86 .reset = bt_field_common_generic_reset,
87 };
88
89 static struct bt_field_common_methods bt_field_enumeration_methods = {
90 .freeze = bt_field_common_enumeration_freeze_recursive,
91 .validate = bt_field_common_enumeration_validate_recursive,
92 .copy = bt_field_enumeration_copy_recursive,
93 .is_set = bt_field_common_enumeration_is_set_recursive,
94 .reset = bt_field_common_enumeration_reset_recursive,
95 };
96
97 static struct bt_field_common_methods bt_field_string_methods = {
98 .freeze = bt_field_common_generic_freeze,
99 .validate = bt_field_common_generic_validate,
100 .copy = bt_field_string_copy,
101 .is_set = bt_field_common_generic_is_set,
102 .reset = bt_field_common_string_reset,
103 };
104
105 static struct bt_field_common_methods bt_field_structure_methods = {
106 .freeze = bt_field_common_structure_freeze_recursive,
107 .validate = bt_field_common_structure_validate_recursive,
108 .copy = bt_field_structure_copy_recursive,
109 .is_set = bt_field_common_structure_is_set_recursive,
110 .reset = bt_field_common_structure_reset_recursive,
111 };
112
113 static struct bt_field_common_methods bt_field_sequence_methods = {
114 .freeze = bt_field_common_sequence_freeze_recursive,
115 .validate = bt_field_common_sequence_validate_recursive,
116 .copy = bt_field_sequence_copy_recursive,
117 .is_set = bt_field_common_sequence_is_set_recursive,
118 .reset = bt_field_common_sequence_reset_recursive,
119 };
120
121 static struct bt_field_common_methods bt_field_array_methods = {
122 .freeze = bt_field_common_array_freeze_recursive,
123 .validate = bt_field_common_array_validate_recursive,
124 .copy = bt_field_array_copy_recursive,
125 .is_set = bt_field_common_array_is_set_recursive,
126 .reset = bt_field_common_array_reset_recursive,
127 };
128
129 static struct bt_field_common_methods bt_field_variant_methods = {
130 .freeze = bt_field_common_variant_freeze_recursive,
131 .validate = bt_field_common_variant_validate_recursive,
132 .copy = bt_field_variant_copy_recursive,
133 .is_set = bt_field_common_variant_is_set_recursive,
134 .reset = bt_field_common_variant_reset_recursive,
135 };
136
137 static
138 struct bt_field *bt_field_integer_create(struct bt_field_type *);
139
140 static
141 struct bt_field *bt_field_enumeration_create(struct bt_field_type *);
142
143 static
144 struct bt_field *bt_field_floating_point_create(struct bt_field_type *);
145
146 static
147 struct bt_field *bt_field_structure_create(struct bt_field_type *);
148
149 static
150 struct bt_field *bt_field_variant_create(struct bt_field_type *);
151
152 static
153 struct bt_field *bt_field_array_create(struct bt_field_type *);
154
155 static
156 struct bt_field *bt_field_sequence_create(struct bt_field_type *);
157
158 static
159 struct bt_field *bt_field_string_create(struct bt_field_type *);
160
161 static
162 struct bt_field *(* const field_create_funcs[])(struct bt_field_type *) = {
163 [BT_FIELD_TYPE_ID_INTEGER] = bt_field_integer_create,
164 [BT_FIELD_TYPE_ID_ENUM] = bt_field_enumeration_create,
165 [BT_FIELD_TYPE_ID_FLOAT] = bt_field_floating_point_create,
166 [BT_FIELD_TYPE_ID_STRUCT] = bt_field_structure_create,
167 [BT_FIELD_TYPE_ID_VARIANT] = bt_field_variant_create,
168 [BT_FIELD_TYPE_ID_ARRAY] = bt_field_array_create,
169 [BT_FIELD_TYPE_ID_SEQUENCE] = bt_field_sequence_create,
170 [BT_FIELD_TYPE_ID_STRING] = bt_field_string_create,
171 };
172
173 struct bt_field *bt_field_create(struct bt_field_type *type)
174 {
175 struct bt_field *field = NULL;
176 enum bt_field_type_id type_id;
177
178 BT_ASSERT_PRE_NON_NULL(type, "Field type");
179 BT_ASSERT(field_type_common_has_known_id((void *) type));
180 BT_ASSERT_PRE(bt_field_type_common_validate((void *) type) == 0,
181 "Field type is invalid: %!+F", type);
182 type_id = bt_field_type_get_type_id(type);
183 field = field_create_funcs[type_id](type);
184 if (!field) {
185 goto end;
186 }
187
188 bt_field_type_freeze(type);
189
190 end:
191 return field;
192 }
193
194 struct bt_field_type *bt_field_get_type(struct bt_field *field)
195 {
196 return (void *) bt_field_common_get_type((void *) field);
197 }
198
199 enum bt_field_type_id bt_field_get_type_id(struct bt_field *field)
200 {
201 struct bt_field_common *field_common = (void *) field;
202
203 BT_ASSERT_PRE_NON_NULL(field, "Field");
204 return field_common->type->id;
205 }
206
207 bt_bool bt_field_is_integer(struct bt_field *field)
208 {
209 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_INTEGER;
210 }
211
212 bt_bool bt_field_is_floating_point(struct bt_field *field)
213 {
214 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_FLOAT;
215 }
216
217 bt_bool bt_field_is_enumeration(struct bt_field *field)
218 {
219 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_ENUM;
220 }
221
222 bt_bool bt_field_is_string(struct bt_field *field)
223 {
224 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_STRING;
225 }
226
227 bt_bool bt_field_is_structure(struct bt_field *field)
228 {
229 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_STRUCT;
230 }
231
232 bt_bool bt_field_is_array(struct bt_field *field)
233 {
234 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_ARRAY;
235 }
236
237 bt_bool bt_field_is_sequence(struct bt_field *field)
238 {
239 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_SEQUENCE;
240 }
241
242 bt_bool bt_field_is_variant(struct bt_field *field)
243 {
244 return bt_field_get_type_id(field) == BT_FIELD_TYPE_ID_VARIANT;
245 }
246
247 BT_HIDDEN
248 int64_t bt_field_sequence_get_int_length(struct bt_field *field)
249 {
250 return bt_field_common_sequence_get_int_length((void *) field);
251 }
252
253 struct bt_field *bt_field_sequence_get_length(struct bt_field *field)
254 {
255 return (void *) bt_field_common_sequence_get_length((void *) field);
256 }
257
258 int bt_field_sequence_set_length(struct bt_field *field,
259 struct bt_field *length_field)
260 {
261 return bt_field_common_sequence_set_length((void *) field,
262 (void *) length_field);
263 }
264
265 struct bt_field *bt_field_structure_get_field_by_index(
266 struct bt_field *field, uint64_t index)
267 {
268 return (void *) bt_field_common_structure_get_field_by_index(
269 (void *) field, index);
270 }
271
272 struct bt_field *bt_field_structure_get_field_by_name(
273 struct bt_field *field, const char *name)
274 {
275 return (void *) bt_field_common_structure_get_field_by_name(
276 (void *) field, name);
277 }
278
279 int bt_field_structure_set_field_by_name(struct bt_field_common *field,
280 const char *name, struct bt_field_common *value)
281 {
282 return bt_field_common_structure_set_field_by_name((void *) field,
283 name, (void *) value);
284 }
285
286 struct bt_field *bt_field_array_get_field(
287 struct bt_field *field, uint64_t index)
288 {
289 return (void *) bt_field_common_array_get_field((void *) field, index,
290 (bt_field_common_create_func) bt_field_create);
291 }
292
293 struct bt_field *bt_field_sequence_get_field(
294 struct bt_field *field, uint64_t index)
295 {
296 return (void *) bt_field_common_sequence_get_field((void *) field,
297 index, (bt_field_common_create_func) bt_field_create);
298 }
299
300 struct bt_field *bt_field_variant_get_field(struct bt_field *field,
301 struct bt_field *tag_field)
302 {
303 return (void *) bt_field_common_variant_get_field((void *) field,
304 (void *) tag_field,
305 (bt_field_common_create_func) bt_field_create);
306 }
307
308 struct bt_field *bt_field_variant_get_current_field(
309 struct bt_field *variant_field)
310 {
311 return (void *) bt_field_common_variant_get_current_field(
312 (void *) variant_field);
313 }
314
315 struct bt_field_common *bt_field_variant_get_tag(
316 struct bt_field_common *variant_field)
317 {
318 return (void *) bt_field_common_variant_get_tag((void *) variant_field);
319 }
320
321 struct bt_field *bt_field_enumeration_get_container(struct bt_field *field)
322 {
323 return (void *) bt_field_common_enumeration_get_container(
324 (void *) field, (bt_field_common_create_func) bt_field_create);
325 }
326
327 struct bt_field_type_enumeration_mapping_iterator *
328 bt_field_enumeration_get_mappings(struct bt_field *field)
329 {
330 return bt_field_common_enumeration_get_mappings((void *) field,
331 (bt_field_common_create_func) bt_field_create);
332 }
333
334 int bt_field_integer_signed_get_value(struct bt_field *field, int64_t *value)
335 {
336 return bt_field_common_integer_signed_get_value((void *) field, value);
337 }
338
339 int bt_field_integer_signed_set_value(struct bt_field *field,
340 int64_t value)
341 {
342 return bt_field_common_integer_signed_set_value((void *) field, value);
343 }
344
345 int bt_field_integer_unsigned_get_value(struct bt_field *field,
346 uint64_t *value)
347 {
348 return bt_field_common_integer_unsigned_get_value((void *) field,
349 value);
350 }
351
352 int bt_field_integer_unsigned_set_value(struct bt_field *field, uint64_t value)
353 {
354 return bt_field_common_integer_unsigned_set_value((void *) field, value);
355 }
356
357 int bt_field_floating_point_get_value(struct bt_field *field,
358 double *value)
359 {
360 return bt_field_common_floating_point_get_value((void *) field, value);
361 }
362
363 int bt_field_floating_point_set_value(struct bt_field *field,
364 double value)
365 {
366 return bt_field_common_floating_point_set_value((void *) field, value);
367 }
368
369 const char *bt_field_string_get_value(struct bt_field *field)
370 {
371 return bt_field_common_string_get_value((void *) field);
372 }
373
374 int bt_field_string_set_value(struct bt_field *field, const char *value)
375 {
376 return bt_field_common_string_set_value((void *) field, value);
377 }
378
379 int bt_field_string_append(struct bt_field *field, const char *value)
380 {
381 return bt_field_common_string_append((void *) field, value);
382 }
383
384 int bt_field_string_append_len(struct bt_field *field,
385 const char *value, unsigned int length)
386 {
387 return bt_field_common_string_append_len((void *) field, value, length);
388 }
389
390 BT_HIDDEN
391 struct bt_field_common *bt_field_common_copy(struct bt_field_common *field)
392 {
393 struct bt_field_common *copy = NULL;
394
395 BT_ASSERT_PRE_NON_NULL(field, "Field");
396 BT_ASSERT(field_type_common_has_known_id(field->type));
397 BT_ASSERT(field->methods->copy);
398 copy = field->methods->copy(field);
399 if (!copy) {
400 BT_LOGW("Cannot create field: ft-addr=%p", field->type);
401 goto end;
402 }
403
404 bt_field_common_set(copy, field->payload_set);
405
406 end:
407 return copy;
408 }
409
410 struct bt_field *bt_field_copy(struct bt_field *field)
411 {
412 return (void *) bt_field_common_copy((void *) field);
413 }
414
415 static void bt_field_common_finalize(struct bt_field_common *field)
416 {
417 BT_ASSERT(field);
418 BT_LOGD_STR("Putting field's type.");
419 bt_put(field->type);
420 }
421
422 BT_HIDDEN
423 void bt_field_common_integer_destroy(struct bt_object *obj)
424 {
425 struct bt_field_common_integer *integer = (void *) obj;
426
427 BT_ASSERT(obj);
428 bt_field_common_finalize(BT_TO_COMMON(integer));
429 BT_LOGD("Destroying integer field object: addr=%p", obj);
430 g_free(obj);
431 }
432
433 BT_HIDDEN
434 void bt_field_common_enumeration_destroy_recursive(struct bt_object *obj)
435 {
436 struct bt_field_common_enumeration *enumeration = (void *) obj;
437
438 BT_ASSERT(enumeration);
439 bt_field_common_finalize(BT_TO_COMMON(enumeration));
440 BT_LOGD("Destroying enumeration field object: addr=%p", obj);
441 BT_LOGD_STR("Putting payload field.");
442 bt_put(enumeration->payload);
443 g_free(enumeration);
444 }
445
446 BT_HIDDEN
447 void bt_field_common_floating_point_destroy(struct bt_object *obj)
448 {
449 struct bt_field_common_floating_point *floating_point = (void *) obj;
450
451 BT_ASSERT(obj);
452 bt_field_common_finalize(BT_TO_COMMON(floating_point));
453 BT_LOGD("Destroying floating point number field object: addr=%p", obj);
454 g_free(obj);
455 }
456
457 BT_HIDDEN
458 void bt_field_common_structure_destroy_recursive(struct bt_object *obj)
459 {
460 struct bt_field_common_structure *structure = (void *) obj;
461
462 BT_ASSERT(obj);
463 bt_field_common_finalize(BT_TO_COMMON(structure));
464 BT_LOGD("Destroying structure field object: addr=%p", obj);
465 g_ptr_array_free(structure->fields, TRUE);
466 g_free(structure);
467 }
468
469 BT_HIDDEN
470 void bt_field_common_variant_destroy_recursive(struct bt_object *obj)
471 {
472 struct bt_field_common_variant *variant = (void *) obj;
473
474 BT_ASSERT(obj);
475 bt_field_common_finalize(BT_TO_COMMON(variant));
476 BT_LOGD("Destroying variant field object: addr=%p", obj);
477 BT_LOGD_STR("Putting tag field.");
478 bt_put(variant->tag);
479 BT_LOGD_STR("Putting payload field.");
480 bt_put(variant->payload);
481 g_free(variant);
482 }
483
484 BT_HIDDEN
485 void bt_field_common_array_destroy_recursive(struct bt_object *obj)
486 {
487 struct bt_field_common_array *array = (void *) obj;
488
489 BT_ASSERT(obj);
490 bt_field_common_finalize(BT_TO_COMMON(array));
491 BT_LOGD("Destroying array field object: addr=%p", obj);
492 g_ptr_array_free(array->elements, TRUE);
493 g_free(array);
494 }
495
496 BT_HIDDEN
497 void bt_field_common_sequence_destroy_recursive(struct bt_object *obj)
498 {
499 struct bt_field_common_sequence *sequence = (void *) obj;
500
501 BT_ASSERT(obj);
502 bt_field_common_finalize(BT_TO_COMMON(sequence));
503 BT_LOGD("Destroying sequence field object: addr=%p", obj);
504
505 if (sequence->elements) {
506 g_ptr_array_free(sequence->elements, TRUE);
507 }
508
509 BT_LOGD_STR("Putting length field.");
510 bt_put(sequence->length);
511 g_free(sequence);
512 }
513
514 BT_HIDDEN
515 void bt_field_common_string_destroy(struct bt_object *obj)
516 {
517 struct bt_field_common_string *string = (void *) obj;
518
519 BT_ASSERT(obj);
520 bt_field_common_finalize(BT_TO_COMMON(string));
521 BT_LOGD("Destroying string field object: addr=%p", obj);
522
523 if (string->payload) {
524 g_string_free(string->payload, TRUE);
525 }
526
527 g_free(string);
528 }
529
530 static
531 struct bt_field *bt_field_integer_create(struct bt_field_type *type)
532 {
533 struct bt_field_common_integer *integer =
534 g_new0(struct bt_field_common_integer, 1);
535
536 BT_LOGD("Creating integer field object: ft-addr=%p", type);
537
538 if (integer) {
539 bt_field_common_initialize(BT_TO_COMMON(integer), (void *) type,
540 bt_field_common_integer_destroy,
541 &bt_field_integer_methods);
542 BT_LOGD("Created integer field object: addr=%p, ft-addr=%p",
543 integer, type);
544 } else {
545 BT_LOGE_STR("Failed to allocate one integer field.");
546 }
547
548 return (void *) integer;
549 }
550
551 static
552 struct bt_field *bt_field_enumeration_create(struct bt_field_type *type)
553 {
554 struct bt_field_common_enumeration *enumeration = g_new0(
555 struct bt_field_common_enumeration, 1);
556
557 BT_LOGD("Creating enumeration field object: ft-addr=%p", type);
558
559 if (enumeration) {
560 bt_field_common_initialize(BT_TO_COMMON(enumeration),
561 (void *) type,
562 bt_field_common_enumeration_destroy_recursive,
563 &bt_field_enumeration_methods);
564 BT_LOGD("Created enumeration field object: addr=%p, ft-addr=%p",
565 enumeration, type);
566 } else {
567 BT_LOGE_STR("Failed to allocate one enumeration field.");
568 }
569
570 return (void *) enumeration;
571 }
572
573 static
574 struct bt_field *bt_field_floating_point_create(struct bt_field_type *type)
575 {
576 struct bt_field_common_floating_point *floating_point;
577
578 BT_LOGD("Creating floating point number field object: ft-addr=%p", type);
579 floating_point = g_new0(struct bt_field_common_floating_point, 1);
580
581 if (floating_point) {
582 bt_field_common_initialize(BT_TO_COMMON(floating_point),
583 (void *) type,
584 bt_field_common_floating_point_destroy,
585 &bt_field_floating_point_methods);
586 BT_LOGD("Created floating point number field object: addr=%p, ft-addr=%p",
587 floating_point, type);
588 } else {
589 BT_LOGE_STR("Failed to allocate one floating point number field.");
590 }
591
592 return (void *) floating_point;
593 }
594
595 BT_HIDDEN
596 int bt_field_common_structure_initialize(struct bt_field_common *field,
597 struct bt_field_type_common *type,
598 bt_object_release_func release_func,
599 struct bt_field_common_methods *methods,
600 bt_field_common_create_func field_create_func)
601 {
602 int ret = 0;
603 struct bt_field_type_common_structure *structure_type =
604 BT_FROM_COMMON(type);
605 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
606 size_t i;
607
608 BT_LOGD("Initializing common structure field object: ft-addr=%p", type);
609 bt_field_common_initialize(field, type, release_func, methods);
610 structure->fields = g_ptr_array_new_with_free_func(
611 (GDestroyNotify) bt_put);
612 g_ptr_array_set_size(structure->fields, structure_type->fields->len);
613
614 /* Create all fields contained by the structure field. */
615 for (i = 0; i < structure_type->fields->len; i++) {
616 struct bt_field_common *field;
617 struct structure_field_common *struct_field =
618 g_ptr_array_index(structure_type->fields, i);
619
620 field = field_create_func(struct_field->type);
621 if (!field) {
622 BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu",
623 g_quark_to_string(struct_field->name), i);
624 ret = -1;
625 goto end;
626 }
627
628 g_ptr_array_index(structure->fields, i) = field;
629 }
630
631 BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p",
632 field, type);
633
634 end:
635 return ret;
636 }
637
638 static
639 struct bt_field *bt_field_structure_create(struct bt_field_type *type)
640 {
641 struct bt_field_common_structure *structure = g_new0(
642 struct bt_field_common_structure, 1);
643 int iret;
644
645 BT_LOGD("Creating structure field object: ft-addr=%p", type);
646
647 if (!structure) {
648 BT_LOGE_STR("Failed to allocate one structure field.");
649 goto end;
650 }
651
652 iret = bt_field_common_structure_initialize(BT_TO_COMMON(structure),
653 (void *) type, bt_field_common_structure_destroy_recursive,
654 &bt_field_structure_methods,
655 (bt_field_common_create_func) bt_field_create);
656 if (iret) {
657 BT_PUT(structure);
658 goto end;
659 }
660
661 BT_LOGD("Created structure field object: addr=%p, ft-addr=%p",
662 structure, type);
663
664 end:
665 return (void *) structure;
666 }
667
668 static
669 struct bt_field *bt_field_variant_create(struct bt_field_type *type)
670 {
671 struct bt_field_common_variant *variant = g_new0(
672 struct bt_field_common_variant, 1);
673
674 BT_LOGD("Creating variant field object: ft-addr=%p", type);
675
676 if (variant) {
677 bt_field_common_initialize(BT_TO_COMMON(variant),
678 (void *) type,
679 bt_field_common_variant_destroy_recursive,
680 &bt_field_variant_methods);
681 BT_LOGD("Created variant field object: addr=%p, ft-addr=%p",
682 variant, type);
683 } else {
684 BT_LOGE_STR("Failed to allocate one variant field.");
685 }
686
687 return (void *) variant;
688 }
689
690 BT_HIDDEN
691 int bt_field_common_array_initialize(struct bt_field_common *field,
692 struct bt_field_type_common *type,
693 bt_object_release_func release_func,
694 struct bt_field_common_methods *methods)
695 {
696 struct bt_field_type_common_array *array_type = BT_FROM_COMMON(type);
697 struct bt_field_common_array *array = BT_FROM_COMMON(field);
698 unsigned int array_length;
699 int ret = 0;
700
701 BT_LOGD("Initializing common array field object: ft-addr=%p", type);
702 BT_ASSERT(type);
703 bt_field_common_initialize(field, type, release_func, methods);
704 array_length = array_type->length;
705 array->elements = g_ptr_array_sized_new(array_length);
706 if (!array->elements) {
707 ret = -1;
708 goto end;
709 }
710
711 g_ptr_array_set_free_func(array->elements, (GDestroyNotify) bt_put);
712 g_ptr_array_set_size(array->elements, array_length);
713 BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p",
714 field, type);
715
716 end:
717 return ret;
718 }
719
720 static
721 struct bt_field *bt_field_array_create(struct bt_field_type *type)
722 {
723 struct bt_field_common_array *array =
724 g_new0(struct bt_field_common_array, 1);
725 int ret;
726
727 BT_LOGD("Creating array field object: ft-addr=%p", type);
728 BT_ASSERT(type);
729
730 if (!array) {
731 BT_LOGE_STR("Failed to allocate one array field.");
732 goto end;
733 }
734
735 ret = bt_field_common_array_initialize(BT_TO_COMMON(array),
736 (void *) type,
737 bt_field_common_array_destroy_recursive,
738 &bt_field_array_methods);
739 if (ret) {
740 BT_PUT(array);
741 goto end;
742 }
743
744 BT_LOGD("Created array field object: addr=%p, ft-addr=%p",
745 array, type);
746
747 end:
748 return (void *) array;
749 }
750
751 static
752 struct bt_field *bt_field_sequence_create(struct bt_field_type *type)
753 {
754 struct bt_field_common_sequence *sequence = g_new0(
755 struct bt_field_common_sequence, 1);
756
757 BT_LOGD("Creating sequence field object: ft-addr=%p", type);
758
759 if (sequence) {
760 bt_field_common_initialize(BT_TO_COMMON(sequence),
761 (void *) type,
762 bt_field_common_sequence_destroy_recursive,
763 &bt_field_sequence_methods);
764 BT_LOGD("Created sequence field object: addr=%p, ft-addr=%p",
765 sequence, type);
766 } else {
767 BT_LOGE_STR("Failed to allocate one sequence field.");
768 }
769
770 return (void *) sequence;
771 }
772
773 static
774 struct bt_field *bt_field_string_create(struct bt_field_type *type)
775 {
776 struct bt_field_common_string *string = g_new0(
777 struct bt_field_common_string, 1);
778
779 BT_LOGD("Creating string field object: ft-addr=%p", type);
780
781 if (string) {
782 bt_field_common_initialize(BT_TO_COMMON(string),
783 (void *) type,
784 bt_field_common_string_destroy,
785 &bt_field_string_methods);
786 BT_LOGD("Created string field object: addr=%p, ft-addr=%p",
787 string, type);
788 } else {
789 BT_LOGE_STR("Failed to allocate one string field.");
790 }
791
792 return (void *) string;
793 }
794
795 BT_HIDDEN
796 int bt_field_common_generic_validate(struct bt_field_common *field)
797 {
798 return (field && field->payload_set) ? 0 : -1;
799 }
800
801 BT_HIDDEN
802 int bt_field_common_enumeration_validate_recursive(
803 struct bt_field_common *field)
804 {
805 int ret;
806 struct bt_field_common_enumeration *enumeration = BT_FROM_COMMON(field);
807
808 BT_ASSERT(field);
809
810 if (!enumeration->payload) {
811 BT_ASSERT_PRE_MSG("Invalid enumeration field: payload is not set: "
812 "%!+_f", field);
813 ret = -1;
814 goto end;
815 }
816
817 ret = bt_field_common_validate_recursive(enumeration->payload);
818
819 end:
820 return ret;
821 }
822
823 BT_HIDDEN
824 int bt_field_common_structure_validate_recursive(struct bt_field_common *field)
825 {
826 int64_t i;
827 int ret = 0;
828 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
829
830 BT_ASSERT(field);
831
832 for (i = 0; i < structure->fields->len; i++) {
833 ret = bt_field_common_validate_recursive(
834 (void *) structure->fields->pdata[i]);
835
836 if (ret) {
837 int this_ret;
838 const char *name;
839
840 this_ret = bt_field_type_common_structure_get_field_by_index(
841 field->type, &name, NULL, i);
842 BT_ASSERT(this_ret == 0);
843 BT_ASSERT_PRE_MSG("Invalid structure field's field: "
844 "%![struct-field-]+_f, field-name=\"%s\", "
845 "index=%" PRId64 ", %![field-]+_f",
846 field, name, i, structure->fields->pdata[i]);
847 goto end;
848 }
849 }
850
851 end:
852 return ret;
853 }
854
855 BT_HIDDEN
856 int bt_field_common_variant_validate_recursive(struct bt_field_common *field)
857 {
858 int ret = 0;
859 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
860
861 BT_ASSERT(field);
862 ret = bt_field_common_validate_recursive(variant->payload);
863 if (ret) {
864 BT_ASSERT_PRE_MSG("Invalid variant field's payload field: "
865 "%![variant-field-]+_f, %![payload-field-]+_f",
866 field, variant->payload);
867 }
868
869 return ret;
870 }
871
872 BT_HIDDEN
873 int bt_field_common_array_validate_recursive(struct bt_field_common *field)
874 {
875 int64_t i;
876 int ret = 0;
877 struct bt_field_common_array *array = BT_FROM_COMMON(field);
878
879 BT_ASSERT(field);
880
881 for (i = 0; i < array->elements->len; i++) {
882 ret = bt_field_common_validate_recursive((void *) array->elements->pdata[i]);
883 if (ret) {
884 BT_ASSERT_PRE_MSG("Invalid array field's element field: "
885 "%![array-field-]+_f, " PRId64 ", "
886 "%![elem-field-]+_f",
887 field, i, array->elements->pdata[i]);
888 goto end;
889 }
890 }
891
892 end:
893 return ret;
894 }
895
896 BT_HIDDEN
897 int bt_field_common_sequence_validate_recursive(struct bt_field_common *field)
898 {
899 size_t i;
900 int ret = 0;
901 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
902
903 BT_ASSERT(field);
904
905 for (i = 0; i < sequence->elements->len; i++) {
906 ret = bt_field_common_validate_recursive(
907 (void *) sequence->elements->pdata[i]);
908 if (ret) {
909 BT_ASSERT_PRE_MSG("Invalid sequence field's element field: "
910 "%![seq-field-]+_f, " PRId64 ", "
911 "%![elem-field-]+_f",
912 field, i, sequence->elements->pdata[i]);
913 goto end;
914 }
915 }
916 end:
917 return ret;
918 }
919
920 BT_HIDDEN
921 void bt_field_common_generic_reset(struct bt_field_common *field)
922 {
923 BT_ASSERT(field);
924 field->payload_set = false;
925 }
926
927 BT_HIDDEN
928 void bt_field_common_enumeration_reset_recursive(struct bt_field_common *field)
929 {
930 struct bt_field_common_enumeration *enumeration = BT_FROM_COMMON(field);
931
932 BT_ASSERT(field);
933
934 if (!enumeration->payload) {
935 return;
936 }
937
938 bt_field_common_reset_recursive(enumeration->payload);
939 }
940
941 BT_HIDDEN
942 void bt_field_common_structure_reset_recursive(struct bt_field_common *field)
943 {
944 int64_t i;
945 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
946
947 BT_ASSERT(field);
948
949 for (i = 0; i < structure->fields->len; i++) {
950 struct bt_field_common *member = structure->fields->pdata[i];
951
952 if (!member) {
953 /*
954 * Structure members are lazily initialized;
955 * skip if this member has not been allocated
956 * yet.
957 */
958 continue;
959 }
960
961 bt_field_common_reset_recursive(member);
962 }
963 }
964
965 BT_HIDDEN
966 void bt_field_common_variant_reset_recursive(struct bt_field_common *field)
967 {
968 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
969
970 BT_ASSERT(field);
971 BT_PUT(variant->tag);
972 BT_PUT(variant->payload);
973 }
974
975 BT_HIDDEN
976 void bt_field_common_array_reset_recursive(struct bt_field_common *field)
977 {
978 size_t i;
979 struct bt_field_common_array *array = BT_FROM_COMMON(field);
980
981 BT_ASSERT(field);
982
983 for (i = 0; i < array->elements->len; i++) {
984 struct bt_field_common *member = array->elements->pdata[i];
985
986 if (!member) {
987 /*
988 * Array elements are lazily initialized; skip
989 * if this member has not been allocated yet.
990 */
991 continue;
992 }
993
994 bt_field_common_reset_recursive(member);
995 }
996 }
997
998 BT_HIDDEN
999 void bt_field_common_sequence_reset_recursive(struct bt_field_common *field)
1000 {
1001 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
1002
1003 BT_ASSERT(field);
1004
1005 if (sequence->elements) {
1006 g_ptr_array_free(sequence->elements, TRUE);
1007 sequence->elements = NULL;
1008 }
1009
1010 BT_PUT(sequence->length);
1011 }
1012
1013 BT_HIDDEN
1014 void bt_field_common_string_reset(struct bt_field_common *field)
1015 {
1016 struct bt_field_common_string *string = BT_FROM_COMMON(field);
1017
1018 BT_ASSERT(field);
1019 bt_field_common_generic_reset(field);
1020
1021 if (string->payload) {
1022 g_string_truncate(string->payload, 0);
1023 }
1024 }
1025
1026 static
1027 struct bt_field_common *bt_field_integer_copy(struct bt_field_common *src)
1028 {
1029 struct bt_field_common_integer *integer_src = (void *) src;
1030 struct bt_field_common_integer *integer_dst;
1031
1032 BT_LOGD("Copying integer field: src-field-addr=%p", src);
1033 integer_dst = (void *) bt_field_create((void *) src->type);
1034 if (!integer_dst) {
1035 goto end;
1036 }
1037
1038 integer_dst->payload = integer_src->payload;
1039 BT_LOGD_STR("Copied integer field.");
1040
1041 end:
1042 return BT_TO_COMMON(integer_dst);
1043 }
1044
1045 static
1046 struct bt_field_common *bt_field_enumeration_copy_recursive(
1047 struct bt_field_common *src)
1048 {
1049 struct bt_field_common_enumeration *enum_src = BT_FROM_COMMON(src);
1050 struct bt_field_common_enumeration *enum_dst;
1051
1052 BT_LOGD("Copying enumeration field: src-field-addr=%p", src);
1053 enum_dst = (void *) bt_field_create((void *) src->type);
1054 if (!enum_dst) {
1055 goto error;
1056 }
1057
1058 if (enum_src->payload) {
1059 BT_LOGD_STR("Copying enumeration field's payload field.");
1060 enum_dst->payload = (void *)
1061 bt_field_copy((void *) enum_src->payload);
1062 if (!enum_dst->payload) {
1063 BT_LOGE_STR("Cannot copy enumeration field's payload field.");
1064 goto error;
1065 }
1066 }
1067
1068 BT_LOGD_STR("Copied enumeration field.");
1069 goto end;
1070
1071 error:
1072 BT_PUT(enum_dst);
1073
1074 end:
1075 return BT_TO_COMMON(enum_dst);
1076 }
1077
1078 static
1079 struct bt_field_common *bt_field_floating_point_copy(
1080 struct bt_field_common *src)
1081 {
1082 struct bt_field_common_floating_point *float_src = BT_FROM_COMMON(src);
1083 struct bt_field_common_floating_point *float_dst;
1084
1085 BT_LOGD("Copying floating point number field: src-field-addr=%p", src);
1086 float_dst = (void *) bt_field_create((void *) src->type);
1087 if (!float_dst) {
1088 goto end;
1089 }
1090
1091 float_dst->payload = float_src->payload;
1092 BT_LOGD_STR("Copied floating point number field.");
1093
1094 end:
1095 return BT_TO_COMMON(float_dst);
1096 }
1097
1098 static
1099 struct bt_field_common *bt_field_structure_copy_recursive(
1100 struct bt_field_common *src)
1101 {
1102 int64_t i;
1103 struct bt_field_common_structure *struct_src = BT_FROM_COMMON(src);
1104 struct bt_field_common_structure *struct_dst;
1105
1106 BT_LOGD("Copying structure field: src-field-addr=%p", src);
1107 struct_dst = (void *) bt_field_create((void *) src->type);
1108 if (!struct_dst) {
1109 goto error;
1110 }
1111
1112 g_ptr_array_set_size(struct_dst->fields, struct_src->fields->len);
1113
1114 for (i = 0; i < struct_src->fields->len; i++) {
1115 struct bt_field_common *field =
1116 g_ptr_array_index(struct_src->fields, i);
1117 struct bt_field_common *field_copy = NULL;
1118
1119 if (field) {
1120 BT_LOGD("Copying structure field's field: src-field-addr=%p"
1121 "index=%" PRId64, field, i);
1122 field_copy = (void *) bt_field_copy((void *) field);
1123 if (!field_copy) {
1124 BT_LOGE("Cannot copy structure field's field: "
1125 "src-field-addr=%p, index=%" PRId64,
1126 field, i);
1127 goto error;
1128 }
1129 }
1130
1131 BT_MOVE(g_ptr_array_index(struct_dst->fields, i), field_copy);
1132 }
1133
1134 BT_LOGD_STR("Copied structure field.");
1135 goto end;
1136
1137 error:
1138 BT_PUT(struct_dst);
1139
1140 end:
1141 return BT_TO_COMMON(struct_dst);
1142 }
1143
1144 static
1145 struct bt_field_common *bt_field_variant_copy_recursive(
1146 struct bt_field_common *src)
1147 {
1148 struct bt_field_common_variant *variant_src = BT_FROM_COMMON(src);
1149 struct bt_field_common_variant *variant_dst;
1150
1151 BT_LOGD("Copying variant field: src-field-addr=%p", src);
1152 variant_dst = (void *) bt_field_create((void *) src->type);
1153 if (!variant_dst) {
1154 goto end;
1155 }
1156
1157 if (variant_src->tag) {
1158 BT_LOGD_STR("Copying variant field's tag field.");
1159 variant_dst->tag = (void *) bt_field_copy(
1160 (void *) variant_src->tag);
1161 if (!variant_dst->tag) {
1162 BT_LOGE_STR("Cannot copy variant field's tag field.");
1163 goto error;
1164 }
1165 }
1166 if (variant_src->payload) {
1167 BT_LOGD_STR("Copying variant field's payload field.");
1168 variant_dst->payload = (void *) bt_field_copy(
1169 (void *) variant_src->payload);
1170 if (!variant_dst->payload) {
1171 BT_LOGE_STR("Cannot copy variant field's payload field.");
1172 goto error;
1173 }
1174 }
1175
1176 BT_LOGD_STR("Copied variant field.");
1177 goto end;
1178
1179 error:
1180 BT_PUT(variant_dst);
1181
1182 end:
1183 return BT_TO_COMMON(variant_dst);
1184 }
1185
1186 static
1187 struct bt_field_common *bt_field_array_copy_recursive(
1188 struct bt_field_common *src)
1189 {
1190 int64_t i;
1191 struct bt_field_common_array *array_src = BT_FROM_COMMON(src);
1192 struct bt_field_common_array *array_dst;
1193
1194 BT_LOGD("Copying array field: src-field-addr=%p", src);
1195 array_dst = (void *) bt_field_create((void *) src->type);
1196 if (!array_dst) {
1197 goto error;
1198 }
1199
1200 g_ptr_array_set_size(array_dst->elements, array_src->elements->len);
1201 for (i = 0; i < array_src->elements->len; i++) {
1202 struct bt_field_common *field =
1203 g_ptr_array_index(array_src->elements, i);
1204 struct bt_field_common *field_copy = NULL;
1205
1206 if (field) {
1207 BT_LOGD("Copying array field's element field: field-addr=%p, "
1208 "index=%" PRId64, field, i);
1209 field_copy = (void *) bt_field_copy((void *) field);
1210 if (!field_copy) {
1211 BT_LOGE("Cannot copy array field's element field: "
1212 "src-field-addr=%p, index=%" PRId64,
1213 field, i);
1214 goto error;
1215 }
1216 }
1217
1218 g_ptr_array_index(array_dst->elements, i) = field_copy;
1219 }
1220
1221 BT_LOGD_STR("Copied array field.");
1222 goto end;
1223
1224 error:
1225 BT_PUT(array_dst);
1226
1227 end:
1228 return BT_TO_COMMON(array_dst);
1229 }
1230
1231 static
1232 struct bt_field_common *bt_field_sequence_copy_recursive(
1233 struct bt_field_common *src)
1234 {
1235 int ret = 0;
1236 int64_t i;
1237 struct bt_field_common_sequence *sequence_src = BT_FROM_COMMON(src);
1238 struct bt_field_common_sequence *sequence_dst;
1239 struct bt_field_common *src_length;
1240 struct bt_field_common *dst_length;
1241
1242 BT_LOGD("Copying sequence field: src-field-addr=%p", src);
1243 sequence_dst = (void *) bt_field_create((void *) src->type);
1244 if (!sequence_dst) {
1245 goto error;
1246 }
1247
1248 src_length = bt_field_common_sequence_get_length(src);
1249 if (!src_length) {
1250 /* no length set yet: keep destination sequence empty */
1251 goto end;
1252 }
1253
1254 /* copy source length */
1255 BT_LOGD_STR("Copying sequence field's length field.");
1256 dst_length = (void *) bt_field_copy((void *) src_length);
1257 BT_PUT(src_length);
1258 if (!dst_length) {
1259 BT_LOGE_STR("Cannot copy sequence field's length field.");
1260 goto error;
1261 }
1262
1263 /* this will initialize the destination sequence's internal array */
1264 ret = bt_field_common_sequence_set_length(
1265 BT_TO_COMMON(sequence_dst), dst_length);
1266 bt_put(dst_length);
1267 if (ret) {
1268 BT_LOGE("Cannot set sequence field copy's length field: "
1269 "dst-length-field-addr=%p", dst_length);
1270 goto error;
1271 }
1272
1273 BT_ASSERT(sequence_dst->elements->len == sequence_src->elements->len);
1274
1275 for (i = 0; i < sequence_src->elements->len; i++) {
1276 struct bt_field_common *field =
1277 g_ptr_array_index(sequence_src->elements, i);
1278 struct bt_field_common *field_copy = NULL;
1279
1280 if (field) {
1281 BT_LOGD("Copying sequence field's element field: field-addr=%p, "
1282 "index=%" PRId64, field, i);
1283 field_copy = (void *) bt_field_copy((void *) field);
1284 if (!field_copy) {
1285 BT_LOGE("Cannot copy sequence field's element field: "
1286 "src-field-addr=%p, index=%" PRId64,
1287 field, i);
1288 goto error;
1289 }
1290 }
1291
1292 g_ptr_array_index(sequence_dst->elements, i) = field_copy;
1293 }
1294
1295 BT_LOGD_STR("Copied sequence field.");
1296 goto end;
1297
1298 error:
1299 BT_PUT(sequence_dst);
1300
1301 end:
1302 return BT_TO_COMMON(sequence_dst);
1303 }
1304
1305 static
1306 struct bt_field_common *bt_field_string_copy(struct bt_field_common *src)
1307 {
1308 struct bt_field_common_string *string_src = BT_FROM_COMMON(src);
1309 struct bt_field_common_string *string_dst;
1310
1311 BT_LOGD("Copying string field: src-field-addr=%p", src);
1312 string_dst = (void *) bt_field_create((void *) src->type);
1313 if (!string_dst) {
1314 goto error;
1315 }
1316
1317 if (string_src->payload) {
1318 string_dst->payload = g_string_new(string_src->payload->str);
1319 if (!string_dst->payload) {
1320 BT_LOGE_STR("Failed to allocate a GString.");
1321 goto error;
1322 }
1323 }
1324
1325 BT_LOGD_STR("Copied string field.");
1326 goto end;
1327
1328 error:
1329 BT_PUT(string_dst);
1330
1331 end:
1332 return BT_TO_COMMON(string_dst);
1333 }
1334
1335 BT_HIDDEN
1336 void bt_field_common_generic_freeze(struct bt_field_common *field)
1337 {
1338 field->frozen = true;
1339 }
1340
1341 BT_HIDDEN
1342 void bt_field_common_enumeration_freeze_recursive(struct bt_field_common *field)
1343 {
1344 struct bt_field_common_enumeration *enum_field = BT_FROM_COMMON(field);
1345
1346 BT_LOGD("Freezing enumeration field object: addr=%p", field);
1347 BT_LOGD("Freezing enumeration field object's contained payload field: payload-field-addr=%p", enum_field->payload);
1348 bt_field_common_freeze_recursive(enum_field->payload);
1349 bt_field_common_generic_freeze(field);
1350 }
1351
1352 BT_HIDDEN
1353 void bt_field_common_structure_freeze_recursive(struct bt_field_common *field)
1354 {
1355 int64_t i;
1356 struct bt_field_common_structure *structure_field =
1357 BT_FROM_COMMON(field);
1358
1359 BT_LOGD("Freezing structure field object: addr=%p", field);
1360
1361 for (i = 0; i < structure_field->fields->len; i++) {
1362 struct bt_field_common *field =
1363 g_ptr_array_index(structure_field->fields, i);
1364
1365 BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64,
1366 field, i);
1367 bt_field_common_freeze_recursive(field);
1368 }
1369
1370 bt_field_common_generic_freeze(field);
1371 }
1372
1373 BT_HIDDEN
1374 void bt_field_common_variant_freeze_recursive(struct bt_field_common *field)
1375 {
1376 struct bt_field_common_variant *variant_field = BT_FROM_COMMON(field);
1377
1378 BT_LOGD("Freezing variant field object: addr=%p", field);
1379 BT_LOGD("Freezing variant field object's tag field: tag-field-addr=%p", variant_field->tag);
1380 bt_field_common_freeze_recursive(variant_field->tag);
1381 BT_LOGD("Freezing variant field object's payload field: payload-field-addr=%p", variant_field->payload);
1382 bt_field_common_freeze_recursive(variant_field->payload);
1383 bt_field_common_generic_freeze(field);
1384 }
1385
1386 BT_HIDDEN
1387 void bt_field_common_array_freeze_recursive(struct bt_field_common *field)
1388 {
1389 int64_t i;
1390 struct bt_field_common_array *array_field = BT_FROM_COMMON(field);
1391
1392 BT_LOGD("Freezing array field object: addr=%p", field);
1393
1394 for (i = 0; i < array_field->elements->len; i++) {
1395 struct bt_field_common *elem_field =
1396 g_ptr_array_index(array_field->elements, i);
1397
1398 BT_LOGD("Freezing array field object's element field: "
1399 "element-field-addr=%p, index=%" PRId64,
1400 elem_field, i);
1401 bt_field_common_freeze_recursive(elem_field);
1402 }
1403
1404 bt_field_common_generic_freeze(field);
1405 }
1406
1407 BT_HIDDEN
1408 void bt_field_common_sequence_freeze_recursive(struct bt_field_common *field)
1409 {
1410 int64_t i;
1411 struct bt_field_common_sequence *sequence_field =
1412 BT_FROM_COMMON(field);
1413
1414 BT_LOGD("Freezing sequence field object: addr=%p", field);
1415 BT_LOGD("Freezing sequence field object's length field: length-field-addr=%p",
1416 sequence_field->length);
1417 bt_field_common_freeze_recursive(sequence_field->length);
1418
1419 for (i = 0; i < sequence_field->elements->len; i++) {
1420 struct bt_field_common *elem_field =
1421 g_ptr_array_index(sequence_field->elements, i);
1422
1423 BT_LOGD("Freezing sequence field object's element field: "
1424 "element-field-addr=%p, index=%" PRId64,
1425 elem_field, i);
1426 bt_field_common_freeze_recursive(elem_field);
1427 }
1428
1429 bt_field_common_generic_freeze(field);
1430 }
1431
1432 BT_HIDDEN
1433 void _bt_field_common_freeze_recursive(struct bt_field_common *field)
1434 {
1435 if (!field) {
1436 goto end;
1437 }
1438
1439 if (field->frozen) {
1440 goto end;
1441 }
1442
1443 BT_LOGD("Freezing field object: addr=%p", field);
1444 BT_ASSERT(field_type_common_has_known_id(field->type));
1445 BT_ASSERT(field->methods->freeze);
1446 field->methods->freeze(field);
1447
1448 end:
1449 return;
1450 }
1451
1452 BT_HIDDEN
1453 bt_bool bt_field_common_generic_is_set(struct bt_field_common *field)
1454 {
1455 return field && field->payload_set;
1456 }
1457
1458 BT_HIDDEN
1459 bt_bool bt_field_common_enumeration_is_set_recursive(
1460 struct bt_field_common *field)
1461 {
1462 bt_bool is_set = BT_FALSE;
1463 struct bt_field_common_enumeration *enumeration = BT_FROM_COMMON(field);
1464
1465 BT_ASSERT(field);
1466
1467 if (!enumeration->payload) {
1468 goto end;
1469 }
1470
1471 is_set = bt_field_common_is_set_recursive(enumeration->payload);
1472
1473 end:
1474 return is_set;
1475 }
1476
1477 BT_HIDDEN
1478 bt_bool bt_field_common_structure_is_set_recursive(
1479 struct bt_field_common *field)
1480 {
1481 bt_bool is_set = BT_FALSE;
1482 size_t i;
1483 struct bt_field_common_structure *structure = BT_FROM_COMMON(field);
1484
1485 BT_ASSERT(field);
1486
1487 for (i = 0; i < structure->fields->len; i++) {
1488 is_set = bt_field_common_is_set_recursive(
1489 structure->fields->pdata[i]);
1490 if (!is_set) {
1491 goto end;
1492 }
1493 }
1494
1495 end:
1496 return is_set;
1497 }
1498
1499 BT_HIDDEN
1500 bt_bool bt_field_common_variant_is_set_recursive(struct bt_field_common *field)
1501 {
1502 struct bt_field_common_variant *variant = BT_FROM_COMMON(field);
1503
1504 BT_ASSERT(field);
1505 return bt_field_common_is_set_recursive(variant->payload);
1506 }
1507
1508 BT_HIDDEN
1509 bt_bool bt_field_common_array_is_set_recursive(struct bt_field_common *field)
1510 {
1511 size_t i;
1512 bt_bool is_set = BT_FALSE;
1513 struct bt_field_common_array *array = BT_FROM_COMMON(field);
1514
1515 BT_ASSERT(field);
1516
1517 for (i = 0; i < array->elements->len; i++) {
1518 is_set = bt_field_common_is_set_recursive(array->elements->pdata[i]);
1519 if (!is_set) {
1520 goto end;
1521 }
1522 }
1523
1524 end:
1525 return is_set;
1526 }
1527
1528 BT_HIDDEN
1529 bt_bool bt_field_common_sequence_is_set_recursive(struct bt_field_common *field)
1530 {
1531 size_t i;
1532 bt_bool is_set = BT_FALSE;
1533 struct bt_field_common_sequence *sequence = BT_FROM_COMMON(field);
1534
1535 BT_ASSERT(field);
1536
1537 if (!sequence->elements) {
1538 goto end;
1539 }
1540
1541 for (i = 0; i < sequence->elements->len; i++) {
1542 is_set = bt_field_common_is_set_recursive(
1543 sequence->elements->pdata[i]);
1544 if (!is_set) {
1545 goto end;
1546 }
1547 }
1548
1549 end:
1550 return is_set;
1551 }
This page took 0.106174 seconds and 4 git commands to generate.