Logging: standardize logging tags
[babeltrace.git] / src / ctf-writer / fields.c
CommitLineData
3dca2276
PP
1/*
2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
350ad6c1 25#define BT_LOG_TAG "CTF-WRITER/FIELDS"
67d2ce02 26#include "logging.h"
3dca2276 27
3dca2276
PP
28#include <float.h>
29#include <inttypes.h>
30#include <stdlib.h>
31
578e048b
MJ
32#include <babeltrace2/ctf-writer/object.h>
33
34#include "common/align.h"
35#include "common/assert.h"
36#include "compat/compiler.h"
37#include "compat/endian.h"
38#include "compat/fcntl.h"
39#include "ctfser/ctfser.h"
40
41#include "assert-pre.h"
42#include "fields.h"
43#include "field-types.h"
44#include "object.h"
45
67d2ce02
MJ
46#define BT_CTF_ASSERT_PRE_CTF_FIELD_IS_INT_OR_ENUM(_field, _name) \
47 BT_CTF_ASSERT_PRE((_field)->type->id == BT_CTF_FIELD_TYPE_ID_INTEGER || \
16ca5ff0
PP
48 (_field)->type->id == BT_CTF_FIELD_TYPE_ID_ENUM, \
49 _name " is not an integer or an enumeration field: " \
50 "field-addr=%p", (_field))
51
52BT_HIDDEN
53struct bt_ctf_field_common *bt_ctf_field_common_copy(struct bt_ctf_field_common *field)
54{
55 struct bt_ctf_field_common *copy = NULL;
56
67d2ce02 57 BT_CTF_ASSERT_PRE_NON_NULL(field, "Field");
16ca5ff0
PP
58 BT_ASSERT(field_type_common_has_known_id(field->type));
59 BT_ASSERT(field->methods->copy);
60 copy = field->methods->copy(field);
61 if (!copy) {
62 BT_LOGW("Cannot create field: ft-addr=%p", field->type);
63 goto end;
64 }
65
66 bt_ctf_field_common_set(copy, field->payload_set);
67
68end:
69 return copy;
70}
71
72BT_HIDDEN
73int bt_ctf_field_common_structure_initialize(struct bt_ctf_field_common *field,
74 struct bt_ctf_field_type_common *type,
e1e02a22 75 bool is_shared, bt_ctf_object_release_func release_func,
16ca5ff0
PP
76 struct bt_ctf_field_common_methods *methods,
77 bt_ctf_field_common_create_func field_create_func,
78 GDestroyNotify field_release_func)
79{
80 int ret = 0;
81 struct bt_ctf_field_type_common_structure *structure_type =
82 BT_CTF_FROM_COMMON(type);
83 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
84 size_t i;
85
86 BT_LOGD("Initializing common structure field object: ft-addr=%p", type);
87 bt_ctf_field_common_initialize(field, type, is_shared,
88 release_func, methods);
89 structure->fields = g_ptr_array_new_with_free_func(field_release_func);
90 g_ptr_array_set_size(structure->fields, structure_type->fields->len);
91
92 /* Create all fields contained in the structure field. */
93 for (i = 0; i < structure_type->fields->len; i++) {
94 struct bt_ctf_field_common *field;
95 struct bt_ctf_field_type_common_structure_field *struct_field =
96 BT_CTF_FIELD_TYPE_COMMON_STRUCTURE_FIELD_AT_INDEX(
97 structure_type, i);
98 field = field_create_func(struct_field->type);
99 if (!field) {
100 BT_LOGE("Failed to create structure field's member: name=\"%s\", index=%zu",
101 g_quark_to_string(struct_field->name), i);
102 ret = -1;
103 goto end;
104 }
105
106 g_ptr_array_index(structure->fields, i) = field;
107 }
108
109 BT_LOGD("Initialized common structure field object: addr=%p, ft-addr=%p",
110 field, type);
111
112end:
113 return ret;
114}
115
116BT_HIDDEN
117int bt_ctf_field_common_variant_initialize(struct bt_ctf_field_common *field,
118 struct bt_ctf_field_type_common *type,
e1e02a22 119 bool is_shared, bt_ctf_object_release_func release_func,
16ca5ff0
PP
120 struct bt_ctf_field_common_methods *methods,
121 bt_ctf_field_common_create_func field_create_func,
122 GDestroyNotify field_release_func)
123{
124 int ret = 0;
125 struct bt_ctf_field_type_common_variant *variant_type =
126 BT_CTF_FROM_COMMON(type);
127 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field);
128 size_t i;
129
130 BT_LOGD("Initializing common variant field object: ft-addr=%p", type);
131 bt_ctf_field_common_initialize(field, type, is_shared,
132 release_func, methods);
133 ret = bt_ctf_field_type_common_variant_update_choices(type);
134 if (ret) {
135 BT_LOGE("Cannot update common variant field type choices: "
136 "ret=%d", ret);
137 goto end;
138 }
139
140 variant->fields = g_ptr_array_new_with_free_func(field_release_func);
141 g_ptr_array_set_size(variant->fields, variant_type->choices->len);
142
143 /* Create all fields contained in the variant field. */
144 for (i = 0; i < variant_type->choices->len; i++) {
145 struct bt_ctf_field_common *field;
146 struct bt_ctf_field_type_common_variant_choice *var_choice =
147 BT_CTF_FIELD_TYPE_COMMON_VARIANT_CHOICE_AT_INDEX(
148 variant_type, i);
149
150 field = field_create_func(var_choice->type);
151 if (!field) {
152 BT_LOGE("Failed to create variant field's member: name=\"%s\", index=%zu",
153 g_quark_to_string(var_choice->name), i);
154 ret = -1;
155 goto end;
156 }
157
158 g_ptr_array_index(variant->fields, i) = field;
159 }
160
161 BT_LOGD("Initialized common variant field object: addr=%p, ft-addr=%p",
162 field, type);
163
164end:
165 return ret;
166}
167
168BT_HIDDEN
169int bt_ctf_field_common_string_initialize(struct bt_ctf_field_common *field,
170 struct bt_ctf_field_type_common *type,
e1e02a22 171 bool is_shared, bt_ctf_object_release_func release_func,
16ca5ff0
PP
172 struct bt_ctf_field_common_methods *methods)
173{
174 int ret = 0;
175 struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field);
176
177 BT_LOGD("Initializing common string field object: ft-addr=%p", type);
178 bt_ctf_field_common_initialize(field, type, is_shared,
179 release_func, methods);
180 string->buf = g_array_sized_new(FALSE, FALSE, sizeof(char), 1);
181 if (!string->buf) {
182 ret = -1;
183 goto end;
184 }
185
186 g_array_index(string->buf, char, 0) = '\0';
187 BT_LOGD("Initialized common string field object: addr=%p, ft-addr=%p",
188 field, type);
189
190end:
191 return ret;
192}
193
194BT_HIDDEN
195int bt_ctf_field_common_array_initialize(struct bt_ctf_field_common *field,
196 struct bt_ctf_field_type_common *type,
e1e02a22 197 bool is_shared, bt_ctf_object_release_func release_func,
16ca5ff0
PP
198 struct bt_ctf_field_common_methods *methods,
199 bt_ctf_field_common_create_func field_create_func,
200 GDestroyNotify field_destroy_func)
201{
202 struct bt_ctf_field_type_common_array *array_type = BT_CTF_FROM_COMMON(type);
203 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
204 unsigned int array_length;
205 int ret = 0;
206 uint64_t i;
207
208 BT_LOGD("Initializing common array field object: ft-addr=%p", type);
209 BT_ASSERT(type);
210 bt_ctf_field_common_initialize(field, type, is_shared,
211 release_func, methods);
212 array_length = array_type->length;
213 array->elements = g_ptr_array_sized_new(array_length);
214 if (!array->elements) {
215 ret = -1;
216 goto end;
217 }
218
219 g_ptr_array_set_free_func(array->elements, field_destroy_func);
220 g_ptr_array_set_size(array->elements, array_length);
221
222 for (i = 0; i < array_length; i++) {
223 array->elements->pdata[i] = field_create_func(
224 array_type->element_ft);
225 if (!array->elements->pdata[i]) {
226 ret = -1;
227 goto end;
228 }
229 }
230
231 BT_LOGD("Initialized common array field object: addr=%p, ft-addr=%p",
232 field, type);
233
234end:
235 return ret;
236}
237
238BT_HIDDEN
239int bt_ctf_field_common_sequence_initialize(struct bt_ctf_field_common *field,
240 struct bt_ctf_field_type_common *type,
e1e02a22 241 bool is_shared, bt_ctf_object_release_func release_func,
16ca5ff0
PP
242 struct bt_ctf_field_common_methods *methods,
243 GDestroyNotify field_destroy_func)
244{
245 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
246 int ret = 0;
247
248 BT_LOGD("Initializing common sequence field object: ft-addr=%p", type);
249 BT_ASSERT(type);
250 bt_ctf_field_common_initialize(field, type, is_shared,
251 release_func, methods);
252 sequence->elements = g_ptr_array_new();
253 if (!sequence->elements) {
254 ret = -1;
255 goto end;
256 }
257
258 g_ptr_array_set_free_func(sequence->elements, field_destroy_func);
259 BT_LOGD("Initialized common sequence field object: addr=%p, ft-addr=%p",
260 field, type);
261
262end:
263 return ret;
264}
265
266BT_HIDDEN
267int bt_ctf_field_common_generic_validate(struct bt_ctf_field_common *field)
268{
269 return (field && field->payload_set) ? 0 : -1;
270}
271
272BT_HIDDEN
273int bt_ctf_field_common_structure_validate_recursive(struct bt_ctf_field_common *field)
274{
275 int64_t i;
276 int ret = 0;
277 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
278
279 BT_ASSERT(field);
280
281 for (i = 0; i < structure->fields->len; i++) {
282 ret = bt_ctf_field_common_validate_recursive(
283 (void *) structure->fields->pdata[i]);
284
285 if (ret) {
286 int this_ret;
287 const char *name;
288
289 this_ret = bt_ctf_field_type_common_structure_borrow_field_by_index(
290 field->type, &name, NULL, i);
291 BT_ASSERT(this_ret == 0);
67d2ce02 292 BT_CTF_ASSERT_PRE_MSG("Invalid structure field's field: "
16ca5ff0
PP
293 "struct-field-addr=%p, field-name=\"%s\", "
294 "index=%" PRId64 ", field-addr=%p",
295 field, name, i, structure->fields->pdata[i]);
296 goto end;
297 }
298 }
299
300end:
301 return ret;
302}
303
304BT_HIDDEN
305int bt_ctf_field_common_variant_validate_recursive(struct bt_ctf_field_common *field)
306{
307 int ret = 0;
308 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field);
309
310 BT_ASSERT(field);
311
312 if (!variant->current_field) {
313 ret = -1;
314 goto end;
315 }
316
317 ret = bt_ctf_field_common_validate_recursive(variant->current_field);
318
319end:
320 return ret;
321}
322
323BT_HIDDEN
324int bt_ctf_field_common_array_validate_recursive(struct bt_ctf_field_common *field)
325{
326 int64_t i;
327 int ret = 0;
328 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
329
330 BT_ASSERT(field);
331
332 for (i = 0; i < array->elements->len; i++) {
333 ret = bt_ctf_field_common_validate_recursive((void *) array->elements->pdata[i]);
334 if (ret) {
67d2ce02
MJ
335 BT_CTF_ASSERT_PRE_MSG("Invalid array field's element field: "
336 "array-field-addr=%p, %" PRId64 ", "
16ca5ff0
PP
337 "elem-field-addr=%p",
338 field, i, array->elements->pdata[i]);
339 goto end;
340 }
341 }
342
343end:
344 return ret;
345}
346
347BT_HIDDEN
348int bt_ctf_field_common_sequence_validate_recursive(struct bt_ctf_field_common *field)
349{
67d2ce02 350 int64_t i;
16ca5ff0
PP
351 int ret = 0;
352 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
353
354 BT_ASSERT(field);
355
356 for (i = 0; i < sequence->elements->len; i++) {
357 ret = bt_ctf_field_common_validate_recursive(
358 (void *) sequence->elements->pdata[i]);
359 if (ret) {
67d2ce02
MJ
360 BT_CTF_ASSERT_PRE_MSG("Invalid sequence field's element field: "
361 "seq-field-addr=%p, %" PRId64 ", "
16ca5ff0
PP
362 "elem-field-addr=%p",
363 field, i, sequence->elements->pdata[i]);
364 goto end;
365 }
366 }
367end:
368 return ret;
369}
370
371BT_HIDDEN
372void bt_ctf_field_common_generic_reset(struct bt_ctf_field_common *field)
373{
374 BT_ASSERT(field);
375 field->payload_set = false;
376}
377
378BT_HIDDEN
379void bt_ctf_field_common_structure_reset_recursive(struct bt_ctf_field_common *field)
380{
381 int64_t i;
382 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
383
384 BT_ASSERT(field);
385
386 for (i = 0; i < structure->fields->len; i++) {
387 struct bt_ctf_field_common *member = structure->fields->pdata[i];
388
389 if (!member) {
390 /*
391 * Structure members are lazily initialized;
392 * skip if this member has not been allocated
393 * yet.
394 */
395 continue;
396 }
397
398 bt_ctf_field_common_reset_recursive(member);
399 }
400}
401
402BT_HIDDEN
403void bt_ctf_field_common_variant_reset_recursive(struct bt_ctf_field_common *field)
404{
405 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field);
406
407 BT_ASSERT(field);
408 variant->current_field = NULL;
409}
410
411BT_HIDDEN
412void bt_ctf_field_common_array_reset_recursive(struct bt_ctf_field_common *field)
413{
414 size_t i;
415 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
416
417 BT_ASSERT(field);
418
419 for (i = 0; i < array->elements->len; i++) {
420 struct bt_ctf_field_common *member = array->elements->pdata[i];
421
422 if (!member) {
423 /*
424 * Array elements are lazily initialized; skip
425 * if this member has not been allocated yet.
426 */
427 continue;
428 }
429
430 bt_ctf_field_common_reset_recursive(member);
431 }
432}
433
434BT_HIDDEN
435void bt_ctf_field_common_sequence_reset_recursive(struct bt_ctf_field_common *field)
436{
437 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
438 uint64_t i;
439
440 BT_ASSERT(field);
441
442 for (i = 0; i < sequence->elements->len; i++) {
443 if (sequence->elements->pdata[i]) {
444 bt_ctf_field_common_reset_recursive(
445 sequence->elements->pdata[i]);
446 }
447 }
448
449 sequence->length = 0;
450}
451
452BT_HIDDEN
453void bt_ctf_field_common_generic_set_is_frozen(struct bt_ctf_field_common *field,
454 bool is_frozen)
455{
456 field->frozen = is_frozen;
457}
458
459BT_HIDDEN
460void bt_ctf_field_common_structure_set_is_frozen_recursive(
461 struct bt_ctf_field_common *field, bool is_frozen)
462{
463 uint64_t i;
464 struct bt_ctf_field_common_structure *structure_field =
465 BT_CTF_FROM_COMMON(field);
466
467 BT_LOGD("Freezing structure field object: addr=%p", field);
468
469 for (i = 0; i < structure_field->fields->len; i++) {
470 struct bt_ctf_field_common *struct_field =
471 g_ptr_array_index(structure_field->fields, i);
472
473 BT_LOGD("Freezing structure field's field: field-addr=%p, index=%" PRId64,
474 struct_field, i);
475 bt_ctf_field_common_set_is_frozen_recursive(struct_field,
476 is_frozen);
477 }
478
479 bt_ctf_field_common_generic_set_is_frozen(field, is_frozen);
480}
481
482BT_HIDDEN
483void bt_ctf_field_common_variant_set_is_frozen_recursive(
484 struct bt_ctf_field_common *field, bool is_frozen)
485{
486 uint64_t i;
487 struct bt_ctf_field_common_variant *variant_field = BT_CTF_FROM_COMMON(field);
488
489 BT_LOGD("Freezing variant field object: addr=%p", field);
490
491 for (i = 0; i < variant_field->fields->len; i++) {
492 struct bt_ctf_field_common *var_field =
493 g_ptr_array_index(variant_field->fields, i);
494
495 BT_LOGD("Freezing variant field's field: field-addr=%p, index=%" PRId64,
496 var_field, i);
497 bt_ctf_field_common_set_is_frozen_recursive(var_field, is_frozen);
498 }
499
500 bt_ctf_field_common_generic_set_is_frozen(field, is_frozen);
501}
502
503BT_HIDDEN
504void bt_ctf_field_common_array_set_is_frozen_recursive(
505 struct bt_ctf_field_common *field, bool is_frozen)
506{
507 int64_t i;
508 struct bt_ctf_field_common_array *array_field = BT_CTF_FROM_COMMON(field);
509
510 BT_LOGD("Freezing array field object: addr=%p", field);
511
512 for (i = 0; i < array_field->elements->len; i++) {
513 struct bt_ctf_field_common *elem_field =
514 g_ptr_array_index(array_field->elements, i);
515
516 BT_LOGD("Freezing array field object's element field: "
517 "element-field-addr=%p, index=%" PRId64,
518 elem_field, i);
519 bt_ctf_field_common_set_is_frozen_recursive(elem_field, is_frozen);
520 }
521
522 bt_ctf_field_common_generic_set_is_frozen(field, is_frozen);
523}
524
525BT_HIDDEN
526void bt_ctf_field_common_sequence_set_is_frozen_recursive(
527 struct bt_ctf_field_common *field, bool is_frozen)
528{
529 int64_t i;
530 struct bt_ctf_field_common_sequence *sequence_field =
531 BT_CTF_FROM_COMMON(field);
532
533 BT_LOGD("Freezing sequence field object: addr=%p", field);
534
535 for (i = 0; i < sequence_field->length; i++) {
536 struct bt_ctf_field_common *elem_field =
537 g_ptr_array_index(sequence_field->elements, i);
538
539 BT_LOGD("Freezing sequence field object's element field: "
540 "element-field-addr=%p, index=%" PRId64,
541 elem_field, i);
542 bt_ctf_field_common_set_is_frozen_recursive(elem_field, is_frozen);
543 }
544
545 bt_ctf_field_common_generic_set_is_frozen(field, is_frozen);
546}
547
548BT_HIDDEN
549void _bt_ctf_field_common_set_is_frozen_recursive(struct bt_ctf_field_common *field,
550 bool is_frozen)
551{
552 if (!field) {
553 goto end;
554 }
555
556 BT_LOGD("Setting field object's frozen state: addr=%p, is-frozen=%d",
557 field, is_frozen);
558 BT_ASSERT(field_type_common_has_known_id(field->type));
559 BT_ASSERT(field->methods->set_is_frozen);
560 field->methods->set_is_frozen(field, is_frozen);
561
562end:
563 return;
564}
565
566BT_HIDDEN
567bt_bool bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common *field)
568{
569 return field && field->payload_set;
570}
571
572BT_HIDDEN
573bt_bool bt_ctf_field_common_structure_is_set_recursive(
574 struct bt_ctf_field_common *field)
575{
576 bt_bool is_set = BT_FALSE;
577 size_t i;
578 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
579
580 BT_ASSERT(field);
581
582 for (i = 0; i < structure->fields->len; i++) {
583 is_set = bt_ctf_field_common_is_set_recursive(
584 structure->fields->pdata[i]);
585 if (!is_set) {
586 goto end;
587 }
588 }
589
590end:
591 return is_set;
592}
593
594BT_HIDDEN
595bt_bool bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common *field)
596{
597 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field);
598 bt_bool is_set = BT_FALSE;
599
600 BT_ASSERT(field);
601
602 if (variant->current_field) {
603 is_set = bt_ctf_field_common_is_set_recursive(
604 variant->current_field);
605 }
606
607 return is_set;
608}
609
610BT_HIDDEN
611bt_bool bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common *field)
612{
613 size_t i;
614 bt_bool is_set = BT_FALSE;
615 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
616
617 BT_ASSERT(field);
618
619 for (i = 0; i < array->elements->len; i++) {
620 is_set = bt_ctf_field_common_is_set_recursive(array->elements->pdata[i]);
621 if (!is_set) {
622 goto end;
623 }
624 }
625
626end:
627 return is_set;
628}
629
630BT_HIDDEN
631bt_bool bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common *field)
632{
633 size_t i;
634 bt_bool is_set = BT_FALSE;
635 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
636
637 BT_ASSERT(field);
638
639 if (!sequence->elements) {
640 goto end;
641 }
642
643 for (i = 0; i < sequence->elements->len; i++) {
644 is_set = bt_ctf_field_common_is_set_recursive(
645 sequence->elements->pdata[i]);
646 if (!is_set) {
647 goto end;
648 }
649 }
650
651end:
652 return is_set;
653}
654
655static struct bt_ctf_field_common_methods bt_ctf_field_integer_methods = {
656 .set_is_frozen = bt_ctf_field_common_generic_set_is_frozen,
657 .validate = bt_ctf_field_common_generic_validate,
3dca2276 658 .copy = NULL,
16ca5ff0
PP
659 .is_set = bt_ctf_field_common_generic_is_set,
660 .reset = bt_ctf_field_common_generic_reset,
3dca2276
PP
661};
662
16ca5ff0
PP
663static struct bt_ctf_field_common_methods bt_ctf_field_floating_point_methods = {
664 .set_is_frozen = bt_ctf_field_common_generic_set_is_frozen,
665 .validate = bt_ctf_field_common_generic_validate,
3dca2276 666 .copy = NULL,
16ca5ff0
PP
667 .is_set = bt_ctf_field_common_generic_is_set,
668 .reset = bt_ctf_field_common_generic_reset,
3dca2276
PP
669};
670
312c056a
PP
671static
672void bt_ctf_field_enumeration_set_is_frozen_recursive(
16ca5ff0 673 struct bt_ctf_field_common *field, bool is_frozen);
312c056a
PP
674
675static
16ca5ff0 676int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common *field);
312c056a
PP
677
678static
679bt_bool bt_ctf_field_enumeration_is_set_recursive(
16ca5ff0 680 struct bt_ctf_field_common *field);
312c056a
PP
681
682static
16ca5ff0 683void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common *field);
312c056a 684
16ca5ff0 685static struct bt_ctf_field_common_methods bt_ctf_field_enumeration_methods = {
312c056a
PP
686 .set_is_frozen = bt_ctf_field_enumeration_set_is_frozen_recursive,
687 .validate = bt_ctf_field_enumeration_validate_recursive,
3dca2276 688 .copy = NULL,
312c056a
PP
689 .is_set = bt_ctf_field_enumeration_is_set_recursive,
690 .reset = bt_ctf_field_enumeration_reset_recursive,
3dca2276
PP
691};
692
16ca5ff0
PP
693static struct bt_ctf_field_common_methods bt_ctf_field_string_methods = {
694 .set_is_frozen = bt_ctf_field_common_generic_set_is_frozen,
695 .validate = bt_ctf_field_common_generic_validate,
3dca2276 696 .copy = NULL,
16ca5ff0
PP
697 .is_set = bt_ctf_field_common_generic_is_set,
698 .reset = bt_ctf_field_common_generic_reset,
3dca2276
PP
699};
700
16ca5ff0
PP
701static struct bt_ctf_field_common_methods bt_ctf_field_structure_methods = {
702 .set_is_frozen = bt_ctf_field_common_structure_set_is_frozen_recursive,
703 .validate = bt_ctf_field_common_structure_validate_recursive,
3dca2276 704 .copy = NULL,
16ca5ff0
PP
705 .is_set = bt_ctf_field_common_structure_is_set_recursive,
706 .reset = bt_ctf_field_common_structure_reset_recursive,
3dca2276
PP
707};
708
16ca5ff0
PP
709static struct bt_ctf_field_common_methods bt_ctf_field_sequence_methods = {
710 .set_is_frozen = bt_ctf_field_common_sequence_set_is_frozen_recursive,
711 .validate = bt_ctf_field_common_sequence_validate_recursive,
3dca2276 712 .copy = NULL,
16ca5ff0
PP
713 .is_set = bt_ctf_field_common_sequence_is_set_recursive,
714 .reset = bt_ctf_field_common_sequence_reset_recursive,
3dca2276
PP
715};
716
16ca5ff0
PP
717static struct bt_ctf_field_common_methods bt_ctf_field_array_methods = {
718 .set_is_frozen = bt_ctf_field_common_array_set_is_frozen_recursive,
719 .validate = bt_ctf_field_common_array_validate_recursive,
3dca2276 720 .copy = NULL,
16ca5ff0
PP
721 .is_set = bt_ctf_field_common_array_is_set_recursive,
722 .reset = bt_ctf_field_common_array_reset_recursive,
3dca2276
PP
723};
724
312c056a 725static
16ca5ff0 726void bt_ctf_field_variant_set_is_frozen_recursive(struct bt_ctf_field_common *field,
312c056a
PP
727 bool is_frozen);
728
729static
16ca5ff0 730int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common *field);
312c056a
PP
731
732static
16ca5ff0 733bt_bool bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common *field);
312c056a
PP
734
735static
16ca5ff0 736void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common *field);
312c056a 737
16ca5ff0 738static struct bt_ctf_field_common_methods bt_ctf_field_variant_methods = {
312c056a
PP
739 .set_is_frozen = bt_ctf_field_variant_set_is_frozen_recursive,
740 .validate = bt_ctf_field_variant_validate_recursive,
3dca2276 741 .copy = NULL,
312c056a
PP
742 .is_set = bt_ctf_field_variant_is_set_recursive,
743 .reset = bt_ctf_field_variant_reset_recursive,
3dca2276
PP
744};
745
746static
747struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *);
748
749static
750struct bt_ctf_field *bt_ctf_field_enumeration_create(struct bt_ctf_field_type *);
751
752static
753struct bt_ctf_field *bt_ctf_field_floating_point_create(struct bt_ctf_field_type *);
754
755static
756struct bt_ctf_field *bt_ctf_field_structure_create(struct bt_ctf_field_type *);
757
758static
759struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *);
760
761static
762struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *);
763
764static
765struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *);
766
767static
768struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *);
769
770static
771struct bt_ctf_field *(* const field_create_funcs[])(struct bt_ctf_field_type *) = {
16ca5ff0
PP
772 [BT_CTF_FIELD_TYPE_ID_INTEGER] = bt_ctf_field_integer_create,
773 [BT_CTF_FIELD_TYPE_ID_ENUM] = bt_ctf_field_enumeration_create,
774 [BT_CTF_FIELD_TYPE_ID_FLOAT] = bt_ctf_field_floating_point_create,
775 [BT_CTF_FIELD_TYPE_ID_STRUCT] = bt_ctf_field_structure_create,
776 [BT_CTF_FIELD_TYPE_ID_VARIANT] = bt_ctf_field_variant_create,
777 [BT_CTF_FIELD_TYPE_ID_ARRAY] = bt_ctf_field_array_create,
778 [BT_CTF_FIELD_TYPE_ID_SEQUENCE] = bt_ctf_field_sequence_create,
779 [BT_CTF_FIELD_TYPE_ID_STRING] = bt_ctf_field_string_create,
3dca2276
PP
780};
781
782typedef int (*bt_ctf_field_serialize_recursive_func)(
013f35c6 783 struct bt_ctf_field_common *, struct bt_ctfser *,
3dca2276
PP
784 enum bt_ctf_byte_order);
785
312c056a
PP
786static
787void bt_ctf_field_integer_destroy(struct bt_ctf_field *field)
788{
789 BT_LOGD("Destroying CTF writer integer field object: addr=%p", field);
16ca5ff0 790 bt_ctf_field_common_integer_finalize((void *) field);
312c056a
PP
791 g_free(field);
792}
793
794static
795void bt_ctf_field_floating_point_destroy(struct bt_ctf_field *field)
796{
797 BT_LOGD("Destroying CTF writer floating point field object: addr=%p",
798 field);
16ca5ff0 799 bt_ctf_field_common_floating_point_finalize((void *) field);
312c056a
PP
800 g_free(field);
801}
802
803static
804void bt_ctf_field_enumeration_destroy_recursive(struct bt_ctf_field *field)
805{
16ca5ff0 806 struct bt_ctf_field_enumeration *enumeration = BT_CTF_FROM_COMMON(field);
312c056a
PP
807
808 BT_LOGD("Destroying CTF writer enumeration field object: addr=%p",
809 field);
810 BT_LOGD_STR("Putting container field.");
e1e02a22 811 bt_ctf_object_put_ref(enumeration->container);
16ca5ff0 812 bt_ctf_field_common_finalize((void *) field);
312c056a
PP
813 g_free(field);
814}
815
816static
817void bt_ctf_field_structure_destroy_recursive(struct bt_ctf_field *field)
818{
819 BT_LOGD("Destroying CTF writer structure field object: addr=%p", field);
16ca5ff0 820 bt_ctf_field_common_structure_finalize_recursive((void *) field);
312c056a
PP
821 g_free(field);
822}
823
824static
825void bt_ctf_field_variant_destroy_recursive(struct bt_ctf_field *field)
826{
16ca5ff0 827 struct bt_ctf_field_variant *variant = BT_CTF_FROM_COMMON(field);
312c056a
PP
828
829 BT_LOGD("Destroying CTF writer variant field object: addr=%p", field);
830 BT_LOGD_STR("Putting tag field.");
e1e02a22 831 bt_ctf_object_put_ref(variant->tag);
16ca5ff0 832 bt_ctf_field_common_variant_finalize_recursive((void *) field);
312c056a
PP
833 g_free(field);
834}
835
836static
837void bt_ctf_field_array_destroy_recursive(struct bt_ctf_field *field)
838{
839 BT_LOGD("Destroying CTF writer array field object: addr=%p", field);
16ca5ff0 840 bt_ctf_field_common_array_finalize_recursive((void *) field);
312c056a
PP
841 g_free(field);
842}
843
844static
845void bt_ctf_field_sequence_destroy_recursive(struct bt_ctf_field *field)
846{
847 BT_LOGD("Destroying CTF writer sequence field object: addr=%p", field);
16ca5ff0 848 bt_ctf_field_common_sequence_finalize_recursive((void *) field);
312c056a
PP
849 g_free(field);
850}
851
852static
853void bt_ctf_field_string_destroy(struct bt_ctf_field *field)
854{
855 BT_LOGD("Destroying CTF writer string field object: addr=%p", field);
16ca5ff0 856 bt_ctf_field_common_string_finalize((void *) field);
312c056a
PP
857 g_free(field);
858}
859
3dca2276
PP
860BT_HIDDEN
861int bt_ctf_field_serialize_recursive(struct bt_ctf_field *field,
013f35c6 862 struct bt_ctfser *ctfser,
3dca2276
PP
863 enum bt_ctf_byte_order native_byte_order)
864{
16ca5ff0 865 struct bt_ctf_field_common *field_common = (void *) field;
3dca2276
PP
866 bt_ctf_field_serialize_recursive_func serialize_func;
867
013f35c6 868 BT_ASSERT(ctfser);
67d2ce02 869 BT_CTF_ASSERT_PRE_NON_NULL(field, "Field");
3dca2276
PP
870 BT_ASSERT(field_common->spec.writer.serialize_func);
871 serialize_func = field_common->spec.writer.serialize_func;
013f35c6 872 return serialize_func(field_common, ctfser,
3dca2276
PP
873 native_byte_order);
874}
875
876static
013f35c6
PP
877int bt_ctf_field_integer_serialize(struct bt_ctf_field_common *field,
878 struct bt_ctfser *ctfser,
879 enum bt_ctf_byte_order native_byte_order)
3dca2276
PP
880{
881 int ret;
013f35c6
PP
882 struct bt_ctf_field_type_common_integer *int_type =
883 BT_CTF_FROM_COMMON(field->type);
884 struct bt_ctf_field_common_integer *int_field =
885 BT_CTF_FROM_COMMON(field);
886 enum bt_ctf_byte_order byte_order;
3dca2276 887
67d2ce02 888 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "Integer field");
013f35c6
PP
889 BT_LOGV("Serializing CTF writer integer field: addr=%p, native-bo=%s",
890 field,
891 bt_ctf_byte_order_string(native_byte_order));
892 byte_order = int_type->user_byte_order;
893 if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
894 byte_order = native_byte_order;
3dca2276
PP
895 }
896
d6981059
PP
897 if (int_type->is_signed) {
898 ret = bt_ctfser_write_signed_int(ctfser,
899 int_field->payload.signd, int_type->common.alignment,
900 int_type->size,
901 byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ?
902 LITTLE_ENDIAN : BIG_ENDIAN);
903 } else {
904 ret = bt_ctfser_write_unsigned_int(ctfser,
905 int_field->payload.unsignd, int_type->common.alignment,
906 int_type->size,
907 byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ?
908 LITTLE_ENDIAN : BIG_ENDIAN);
909 }
910
91d81473 911 if (G_UNLIKELY(ret)) {
013f35c6 912 BT_LOGE("Cannot serialize integer field: ret=%d", ret);
3dca2276
PP
913 goto end;
914 }
915
3dca2276
PP
916end:
917 return ret;
918}
919
920static
013f35c6
PP
921int bt_ctf_field_enumeration_serialize_recursive(
922 struct bt_ctf_field_common *field, struct bt_ctfser *ctfser,
3dca2276
PP
923 enum bt_ctf_byte_order native_byte_order)
924{
312c056a 925 struct bt_ctf_field_enumeration *enumeration = (void *) field;
3dca2276 926
013f35c6
PP
927 BT_LOGV("Serializing enumeration field: addr=%p, native-bo=%s",
928 field, bt_ctf_byte_order_string(native_byte_order));
3dca2276 929 BT_LOGV_STR("Serializing enumeration field's payload field.");
312c056a 930 return bt_ctf_field_serialize_recursive(
013f35c6 931 (void *) enumeration->container, ctfser, native_byte_order);
3dca2276
PP
932}
933
934static
16ca5ff0 935int bt_ctf_field_floating_point_serialize(struct bt_ctf_field_common *field,
013f35c6 936 struct bt_ctfser *ctfser,
3dca2276
PP
937 enum bt_ctf_byte_order native_byte_order)
938{
013f35c6
PP
939 int ret = -1;
940 struct bt_ctf_field_type_common_floating_point *flt_type =
941 BT_CTF_FROM_COMMON(field->type);
942 struct bt_ctf_field_common_floating_point *flt_field = BT_CTF_FROM_COMMON(field);
943 enum bt_ctf_byte_order byte_order;
3dca2276 944
67d2ce02 945 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "Floating point number field");
013f35c6
PP
946 BT_LOGV("Serializing floating point number field: "
947 "addr=%p, native-bo=%s", field,
948 bt_ctf_byte_order_string(native_byte_order));
3dca2276 949
013f35c6
PP
950 byte_order = flt_type->user_byte_order;
951 if (byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
952 byte_order = native_byte_order;
953 }
954
955 if (flt_type->mant_dig == FLT_MANT_DIG) {
956 ret = bt_ctfser_write_float32(ctfser, flt_field->payload,
957 flt_type->common.alignment,
958 byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ?
959 LITTLE_ENDIAN : BIG_ENDIAN);
960 } else if (flt_type->mant_dig == DBL_MANT_DIG) {
961 ret = bt_ctfser_write_float64(ctfser, flt_field->payload,
962 flt_type->common.alignment,
963 byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ?
964 LITTLE_ENDIAN : BIG_ENDIAN);
965 } else {
966 abort();
967 }
968
91d81473 969 if (G_UNLIKELY(ret)) {
013f35c6
PP
970 BT_LOGE("Cannot serialize floating point number field: "
971 "ret=%d", ret);
972 goto end;
3dca2276
PP
973 }
974
975end:
976 return ret;
977}
978
979static
16ca5ff0 980int bt_ctf_field_structure_serialize_recursive(struct bt_ctf_field_common *field,
013f35c6 981 struct bt_ctfser *ctfser,
3dca2276
PP
982 enum bt_ctf_byte_order native_byte_order)
983{
984 int64_t i;
013f35c6 985 int ret;
16ca5ff0 986 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
3dca2276 987
013f35c6
PP
988 BT_LOGV("Serializing structure field: addr=%p, native-bo=%s",
989 field, bt_ctf_byte_order_string(native_byte_order));
990 ret = bt_ctfser_align_offset_in_current_packet(ctfser,
991 field->type->alignment);
91d81473 992 if (G_UNLIKELY(ret)) {
013f35c6
PP
993 BT_LOGE("Cannot align offset before serializing structure field: "
994 "ret=%d", ret);
3dca2276
PP
995 goto end;
996 }
997
998 for (i = 0; i < structure->fields->len; i++) {
16ca5ff0 999 struct bt_ctf_field_common *member = g_ptr_array_index(
3dca2276
PP
1000 structure->fields, i);
1001 const char *field_name = NULL;
1002
013f35c6
PP
1003 BT_LOGV("Serializing structure field's field: ser-offset=%" PRIu64 ", "
1004 "field-addr=%p, index=%" PRIu64,
1005 bt_ctfser_get_offset_in_current_packet_bits(ctfser),
1006 member, i);
3dca2276 1007
91d81473 1008 if (G_UNLIKELY(!member)) {
16ca5ff0 1009 ret = bt_ctf_field_type_common_structure_borrow_field_by_index(
3dca2276
PP
1010 field->type, &field_name, NULL, i);
1011 BT_ASSERT(ret == 0);
1012 BT_LOGW("Cannot serialize structure field's field: field is not set: "
1013 "struct-field-addr=%p, "
1014 "field-name=\"%s\", index=%" PRId64,
1015 field, field_name, i);
1016 ret = -1;
1017 goto end;
1018 }
1019
013f35c6 1020 ret = bt_ctf_field_serialize_recursive((void *) member, ctfser,
3dca2276 1021 native_byte_order);
91d81473 1022 if (G_UNLIKELY(ret)) {
16ca5ff0 1023 ret = bt_ctf_field_type_common_structure_borrow_field_by_index(
3dca2276
PP
1024 field->type, &field_name, NULL, i);
1025 BT_ASSERT(ret == 0);
1026 BT_LOGW("Cannot serialize structure field's field: "
1027 "struct-field-addr=%p, field-addr=%p, "
1028 "field-name=\"%s\", index=%" PRId64,
1029 field->type, member, field_name, i);
1030 break;
1031 }
1032 }
1033
1034end:
1035 return ret;
1036}
1037
1038static
16ca5ff0 1039int bt_ctf_field_variant_serialize_recursive(struct bt_ctf_field_common *field,
013f35c6 1040 struct bt_ctfser *ctfser,
3dca2276
PP
1041 enum bt_ctf_byte_order native_byte_order)
1042{
16ca5ff0 1043 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field);
3dca2276 1044
013f35c6
PP
1045 BT_LOGV("Serializing variant field: addr=%p, native-bo=%s",
1046 field, bt_ctf_byte_order_string(native_byte_order));
3dca2276
PP
1047 BT_LOGV_STR("Serializing variant field's payload field.");
1048 return bt_ctf_field_serialize_recursive(
013f35c6 1049 (void *) variant->current_field, ctfser, native_byte_order);
3dca2276
PP
1050}
1051
1052static
16ca5ff0 1053int bt_ctf_field_array_serialize_recursive(struct bt_ctf_field_common *field,
013f35c6 1054 struct bt_ctfser *ctfser,
3dca2276
PP
1055 enum bt_ctf_byte_order native_byte_order)
1056{
1057 int64_t i;
1058 int ret = 0;
16ca5ff0 1059 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
3dca2276 1060
013f35c6
PP
1061 BT_LOGV("Serializing array field: addr=%p, native-bo=%s",
1062 field, bt_ctf_byte_order_string(native_byte_order));
3dca2276
PP
1063
1064 for (i = 0; i < array->elements->len; i++) {
16ca5ff0 1065 struct bt_ctf_field_common *elem_field =
3dca2276
PP
1066 g_ptr_array_index(array->elements, i);
1067
1068 BT_LOGV("Serializing array field's element field: "
013f35c6
PP
1069 "ser-offset=%" PRIu64 ", field-addr=%p, index=%" PRId64,
1070 bt_ctfser_get_offset_in_current_packet_bits(ctfser),
1071 elem_field, i);
3dca2276 1072 ret = bt_ctf_field_serialize_recursive(
013f35c6 1073 (void *) elem_field, ctfser, native_byte_order);
91d81473 1074 if (G_UNLIKELY(ret)) {
3dca2276
PP
1075 BT_LOGW("Cannot serialize array field's element field: "
1076 "array-field-addr=%p, field-addr=%p, "
1077 "index=%" PRId64, field, elem_field, i);
1078 goto end;
1079 }
1080 }
1081
1082end:
1083 return ret;
1084}
1085
1086static
16ca5ff0 1087int bt_ctf_field_sequence_serialize_recursive(struct bt_ctf_field_common *field,
013f35c6 1088 struct bt_ctfser *ctfser,
3dca2276
PP
1089 enum bt_ctf_byte_order native_byte_order)
1090{
1091 int64_t i;
1092 int ret = 0;
16ca5ff0 1093 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
3dca2276 1094
013f35c6
PP
1095 BT_LOGV("Serializing sequence field: addr=%p, native-bo=%s",
1096 field, bt_ctf_byte_order_string(native_byte_order));
3dca2276
PP
1097
1098 for (i = 0; i < sequence->elements->len; i++) {
16ca5ff0 1099 struct bt_ctf_field_common *elem_field =
3dca2276
PP
1100 g_ptr_array_index(sequence->elements, i);
1101
1102 BT_LOGV("Serializing sequence field's element field: "
013f35c6
PP
1103 "ser-offset=%" PRIu64 ", field-addr=%p, index=%" PRId64,
1104 bt_ctfser_get_offset_in_current_packet_bits(ctfser),
1105 elem_field, i);
3dca2276 1106 ret = bt_ctf_field_serialize_recursive(
013f35c6 1107 (void *) elem_field, ctfser, native_byte_order);
91d81473 1108 if (G_UNLIKELY(ret)) {
3dca2276
PP
1109 BT_LOGW("Cannot serialize sequence field's element field: "
1110 "sequence-field-addr=%p, field-addr=%p, "
1111 "index=%" PRId64, field, elem_field, i);
1112 goto end;
1113 }
1114 }
1115
1116end:
1117 return ret;
1118}
1119
1120static
16ca5ff0 1121int bt_ctf_field_string_serialize(struct bt_ctf_field_common *field,
013f35c6 1122 struct bt_ctfser *ctfser,
3dca2276
PP
1123 enum bt_ctf_byte_order native_byte_order)
1124{
013f35c6 1125 int ret;
16ca5ff0 1126 struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field);
3dca2276 1127
67d2ce02 1128 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "String field");
013f35c6
PP
1129 BT_LOGV("Serializing string field: addr=%p, native-bo=%s",
1130 field, bt_ctf_byte_order_string((int) native_byte_order));
1131 ret = bt_ctfser_write_string(ctfser, (const char *) string->buf->data);
91d81473 1132 if (G_UNLIKELY(ret)) {
013f35c6
PP
1133 BT_LOGE("Cannot serialize string field: ret=%d", ret);
1134 goto end;
3dca2276
PP
1135 }
1136
1137end:
3dca2276
PP
1138 return ret;
1139}
1140
1141struct bt_ctf_field *bt_ctf_field_create(struct bt_ctf_field_type *type)
1142{
1143 struct bt_ctf_field *field = NULL;
1144 enum bt_ctf_field_type_id type_id;
1145
67d2ce02 1146 BT_CTF_ASSERT_PRE_NON_NULL(type, "Field type");
3dca2276 1147 BT_ASSERT(field_type_common_has_known_id((void *) type));
67d2ce02 1148 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_validate((void *) type) == 0,
16ca5ff0 1149 "Field type is invalid: ft-addr=%p", type);
3dca2276
PP
1150 type_id = bt_ctf_field_type_get_type_id(type);
1151 field = field_create_funcs[type_id](type);
1152 if (!field) {
1153 goto end;
1154 }
1155
16ca5ff0 1156 bt_ctf_field_type_common_freeze((void *) type);
3dca2276
PP
1157
1158end:
1159 return field;
1160}
1161
1162struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field)
1163{
e1e02a22 1164 return bt_ctf_object_get_ref(bt_ctf_field_common_borrow_type((void *) field));
3dca2276
PP
1165}
1166
1167enum bt_ctf_field_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field)
1168{
16ca5ff0 1169 struct bt_ctf_field_common *field_common = (void *) field;
3dca2276 1170
67d2ce02 1171 BT_CTF_ASSERT_PRE_NON_NULL(field, "Field");
3dca2276
PP
1172 return (int) field_common->type->id;
1173}
1174
3dca2276
PP
1175int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field,
1176 struct bt_ctf_field *length_field)
1177{
312c056a 1178 int ret;
16ca5ff0 1179 struct bt_ctf_field_common *common_length_field = (void *) length_field;
312c056a
PP
1180 uint64_t length;
1181
67d2ce02
MJ
1182 BT_CTF_ASSERT_PRE_NON_NULL(length_field, "Length field");
1183 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET((void *) length_field, "Length field");
1184 BT_CTF_ASSERT_PRE(common_length_field->type->id == BT_CTF_FIELD_TYPE_ID_INTEGER ||
16ca5ff0
PP
1185 common_length_field->type->id == BT_CTF_FIELD_TYPE_ID_ENUM,
1186 "Length field must be an integer or enumeration field: field-addr=%p",
312c056a
PP
1187 length_field);
1188
16ca5ff0 1189 if (common_length_field->type->id == BT_CTF_FIELD_TYPE_ID_ENUM) {
312c056a
PP
1190 struct bt_ctf_field_enumeration *enumeration = (void *)
1191 length_field;
1192
1193 length_field = (void *) enumeration->container;
1194 }
1195
1196 ret = bt_ctf_field_integer_unsigned_get_value(length_field, &length);
1197 BT_ASSERT(ret == 0);
16ca5ff0
PP
1198 return bt_ctf_field_common_sequence_set_length((void *) field,
1199 length, (bt_ctf_field_common_create_func) bt_ctf_field_create);
3dca2276
PP
1200}
1201
1202struct bt_ctf_field *bt_ctf_field_structure_get_field_by_index(
1203 struct bt_ctf_field *field, uint64_t index)
1204{
e1e02a22 1205 return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_index(
094ff7c0 1206 (void *) field, index));
3dca2276
PP
1207}
1208
1209struct bt_ctf_field *bt_ctf_field_structure_get_field_by_name(
1210 struct bt_ctf_field *field, const char *name)
1211{
e1e02a22 1212 return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_name(
094ff7c0 1213 (void *) field, name));
3dca2276
PP
1214}
1215
3dca2276
PP
1216struct bt_ctf_field *bt_ctf_field_array_get_field(
1217 struct bt_ctf_field *field, uint64_t index)
1218{
e1e02a22 1219 return bt_ctf_object_get_ref(
16ca5ff0 1220 bt_ctf_field_common_array_borrow_field((void *) field, index));
3dca2276
PP
1221}
1222
1223struct bt_ctf_field *bt_ctf_field_sequence_get_field(
1224 struct bt_ctf_field *field, uint64_t index)
1225{
e1e02a22 1226 return bt_ctf_object_get_ref(
16ca5ff0 1227 bt_ctf_field_common_sequence_borrow_field((void *) field, index));
3dca2276
PP
1228}
1229
1230struct bt_ctf_field *bt_ctf_field_variant_get_field(struct bt_ctf_field *field,
1231 struct bt_ctf_field *tag_field)
1232{
312c056a
PP
1233 struct bt_ctf_field_variant *variant_field = (void *) field;
1234 struct bt_ctf_field_enumeration *enum_field = (void *) tag_field;
16ca5ff0
PP
1235 struct bt_ctf_field_type_common_variant *variant_ft;
1236 struct bt_ctf_field_type_common_enumeration *tag_ft;
312c056a
PP
1237 struct bt_ctf_field *current_field = NULL;
1238 bt_bool is_signed;
1239 uint64_t tag_uval;
1240 int ret;
1241
67d2ce02
MJ
1242 BT_CTF_ASSERT_PRE_NON_NULL(field, "Variant field");
1243 BT_CTF_ASSERT_PRE_NON_NULL(tag_field, "Tag field");
1244 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET((void *) tag_field, "Tag field");
1245 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(
16ca5ff0
PP
1246 (struct bt_ctf_field_common *) tag_field,
1247 BT_CTF_FIELD_TYPE_ID_ENUM, "Tag field");
67d2ce02 1248 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(
16ca5ff0
PP
1249 (struct bt_ctf_field_common *) field,
1250 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
67d2ce02 1251 BT_CTF_ASSERT_PRE(
16ca5ff0
PP
1252 bt_ctf_field_common_validate_recursive((void *) tag_field) == 0,
1253 "Tag field is invalid: field-addr=%p", tag_field);
1254 variant_ft = BT_CTF_FROM_COMMON(variant_field->common.common.type);
67d2ce02 1255 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
16ca5ff0
PP
1256 BT_CTF_TO_COMMON(variant_ft->tag_ft), enum_field->common.type) == 0,
1257 "Unexpected tag field's type: expected-ft-addr=%p, "
1258 "tag-ft-addr=%p", variant_ft->tag_ft,
312c056a 1259 enum_field->common.type);
16ca5ff0 1260 tag_ft = BT_CTF_FROM_COMMON(enum_field->common.type);
312c056a
PP
1261 is_signed = tag_ft->container_ft->is_signed;
1262
1263 if (is_signed) {
1264 int64_t tag_ival;
1265
1266 ret = bt_ctf_field_integer_signed_get_value(
1267 (void *) enum_field->container, &tag_ival);
1268 tag_uval = (uint64_t) tag_ival;
1269 } else {
1270 ret = bt_ctf_field_integer_unsigned_get_value(
1271 (void *) enum_field->container, &tag_uval);
1272 }
1273
1274 BT_ASSERT(ret == 0);
16ca5ff0 1275 ret = bt_ctf_field_common_variant_set_tag((void *) field, tag_uval,
312c056a
PP
1276 is_signed);
1277 if (ret) {
1278 goto end;
1279 }
1280
e1e02a22
PP
1281 bt_ctf_object_put_ref(variant_field->tag);
1282 variant_field->tag = bt_ctf_object_get_ref(tag_field);
312c056a
PP
1283 current_field = bt_ctf_field_variant_get_current_field(field);
1284 BT_ASSERT(current_field);
1285
1286end:
1287 return current_field;
3dca2276
PP
1288}
1289
1290struct bt_ctf_field *bt_ctf_field_variant_get_current_field(
1291 struct bt_ctf_field *variant_field)
1292{
e1e02a22 1293 return bt_ctf_object_get_ref(bt_ctf_field_common_variant_borrow_current_field(
094ff7c0 1294 (void *) variant_field));
3dca2276
PP
1295}
1296
312c056a
PP
1297BT_HIDDEN
1298struct bt_ctf_field *bt_ctf_field_enumeration_borrow_container(
1299 struct bt_ctf_field *field)
3dca2276 1300{
312c056a
PP
1301 struct bt_ctf_field_enumeration *enumeration = (void *) field;
1302
67d2ce02
MJ
1303 BT_CTF_ASSERT_PRE_NON_NULL(field, "Enumeration field");
1304 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID((struct bt_ctf_field_common *) field,
312c056a
PP
1305 BT_CTF_FIELD_TYPE_ID_ENUM, "Field");
1306 BT_ASSERT(enumeration->container);
1307 return (void *) enumeration->container;
3dca2276
PP
1308}
1309
312c056a
PP
1310struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
1311 struct bt_ctf_field *field)
3dca2276 1312{
e1e02a22 1313 return bt_ctf_object_get_ref(bt_ctf_field_enumeration_borrow_container(field));
3dca2276
PP
1314}
1315
312c056a
PP
1316int bt_ctf_field_integer_signed_get_value(struct bt_ctf_field *field,
1317 int64_t *value)
3dca2276 1318{
16ca5ff0 1319 struct bt_ctf_field_common_integer *integer = (void *) field;
312c056a 1320
67d2ce02
MJ
1321 BT_CTF_ASSERT_PRE_NON_NULL(field, "Integer field");
1322 BT_CTF_ASSERT_PRE_NON_NULL(value, "Value");
1323 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(BT_CTF_TO_COMMON(integer), "Integer field");
1324 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer),
16ca5ff0 1325 BT_CTF_FIELD_TYPE_ID_INTEGER, "Field");
67d2ce02 1326 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_integer_is_signed(
312c056a 1327 integer->common.type),
16ca5ff0 1328 "Field's type is unsigned: field-addr=%p", field);
312c056a
PP
1329 *value = integer->payload.signd;
1330 return 0;
3dca2276
PP
1331}
1332
1333int bt_ctf_field_integer_signed_set_value(struct bt_ctf_field *field,
1334 int64_t value)
1335{
312c056a 1336 int ret = 0;
16ca5ff0
PP
1337 struct bt_ctf_field_common_integer *integer = (void *) field;
1338 struct bt_ctf_field_type_common_integer *integer_type;
312c056a 1339
67d2ce02
MJ
1340 BT_CTF_ASSERT_PRE_NON_NULL(field, "Integer field");
1341 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(BT_CTF_TO_COMMON(integer), "Integer field");
1342 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer),
16ca5ff0
PP
1343 BT_CTF_FIELD_TYPE_ID_INTEGER, "Field");
1344 integer_type = BT_CTF_FROM_COMMON(integer->common.type);
67d2ce02 1345 BT_CTF_ASSERT_PRE(
16ca5ff0
PP
1346 bt_ctf_field_type_common_integer_is_signed(integer->common.type),
1347 "Field's type is unsigned: field-addr=%p", field);
67d2ce02 1348 BT_CTF_ASSERT_PRE(value_is_in_range_signed(integer_type->size, value),
16ca5ff0 1349 "Value is out of bounds: value=%" PRId64 ", field-addr=%p",
312c056a
PP
1350 value, field);
1351 integer->payload.signd = value;
16ca5ff0 1352 bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer), true);
312c056a 1353 return ret;
3dca2276
PP
1354}
1355
1356int bt_ctf_field_integer_unsigned_get_value(struct bt_ctf_field *field,
1357 uint64_t *value)
1358{
16ca5ff0 1359 struct bt_ctf_field_common_integer *integer = (void *) field;
312c056a 1360
67d2ce02
MJ
1361 BT_CTF_ASSERT_PRE_NON_NULL(field, "Integer field");
1362 BT_CTF_ASSERT_PRE_NON_NULL(value, "Value");
1363 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(BT_CTF_TO_COMMON(integer), "Integer field");
1364 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer),
16ca5ff0 1365 BT_CTF_FIELD_TYPE_ID_INTEGER, "Field");
67d2ce02 1366 BT_CTF_ASSERT_PRE(
16ca5ff0
PP
1367 !bt_ctf_field_type_common_integer_is_signed(integer->common.type),
1368 "Field's type is signed: field-addr=%p", field);
312c056a
PP
1369 *value = integer->payload.unsignd;
1370 return 0;
3dca2276
PP
1371}
1372
312c056a
PP
1373int bt_ctf_field_integer_unsigned_set_value(struct bt_ctf_field *field,
1374 uint64_t value)
3dca2276 1375{
16ca5ff0
PP
1376 struct bt_ctf_field_common_integer *integer = (void *) field;
1377 struct bt_ctf_field_type_common_integer *integer_type;
312c056a 1378
67d2ce02
MJ
1379 BT_CTF_ASSERT_PRE_NON_NULL(field, "Integer field");
1380 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HOT(BT_CTF_TO_COMMON(integer), "Integer field");
1381 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(BT_CTF_TO_COMMON(integer),
16ca5ff0
PP
1382 BT_CTF_FIELD_TYPE_ID_INTEGER, "Field");
1383 integer_type = BT_CTF_FROM_COMMON(integer->common.type);
67d2ce02 1384 BT_CTF_ASSERT_PRE(
16ca5ff0
PP
1385 !bt_ctf_field_type_common_integer_is_signed(integer->common.type),
1386 "Field's type is signed: field-addr=%p", field);
67d2ce02 1387 BT_CTF_ASSERT_PRE(value_is_in_range_unsigned(integer_type->size, value),
16ca5ff0 1388 "Value is out of bounds: value=%" PRIu64 ", field-addr=%p",
312c056a
PP
1389 value, field);
1390 integer->payload.unsignd = value;
16ca5ff0 1391 bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer), true);
312c056a 1392 return 0;
3dca2276
PP
1393}
1394
1395int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field,
1396 double *value)
1397{
16ca5ff0 1398 return bt_ctf_field_common_floating_point_get_value((void *) field, value);
3dca2276
PP
1399}
1400
1401int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field,
1402 double value)
1403{
16ca5ff0 1404 return bt_ctf_field_common_floating_point_set_value((void *) field, value);
3dca2276
PP
1405}
1406
1407const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field)
1408{
16ca5ff0 1409 return bt_ctf_field_common_string_get_value((void *) field);
3dca2276
PP
1410}
1411
1412int bt_ctf_field_string_set_value(struct bt_ctf_field *field, const char *value)
1413{
16ca5ff0 1414 return bt_ctf_field_common_string_set_value((void *) field, value);
3dca2276
PP
1415}
1416
1417int bt_ctf_field_string_append(struct bt_ctf_field *field, const char *value)
1418{
16ca5ff0 1419 return bt_ctf_field_common_string_append((void *) field, value);
3dca2276
PP
1420}
1421
1422int bt_ctf_field_string_append_len(struct bt_ctf_field *field,
1423 const char *value, unsigned int length)
1424{
16ca5ff0 1425 return bt_ctf_field_common_string_append_len((void *) field, value, length);
3dca2276
PP
1426}
1427
1428struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
1429{
16ca5ff0 1430 return (void *) bt_ctf_field_common_copy((void *) field);
3dca2276
PP
1431}
1432
1433static
1434struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type)
1435{
16ca5ff0
PP
1436 struct bt_ctf_field_common_integer *integer =
1437 g_new0(struct bt_ctf_field_common_integer, 1);
3dca2276
PP
1438
1439 BT_LOGD("Creating CTF writer integer field object: ft-addr=%p", type);
1440
1441 if (integer) {
16ca5ff0 1442 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(integer), (void *) type,
3fea54f6 1443 true,
e1e02a22 1444 (bt_ctf_object_release_func) bt_ctf_field_integer_destroy,
3dca2276
PP
1445 &bt_ctf_field_integer_methods);
1446 integer->common.spec.writer.serialize_func =
1447 (bt_ctf_field_serialize_recursive_func) bt_ctf_field_integer_serialize;
1448 BT_LOGD("Created CTF writer integer field object: addr=%p, ft-addr=%p",
1449 integer, type);
1450 } else {
1451 BT_LOGE_STR("Failed to allocate one integer field.");
1452 }
1453
1454 return (void *) integer;
1455}
1456
1457static
1458struct bt_ctf_field *bt_ctf_field_enumeration_create(
1459 struct bt_ctf_field_type *type)
1460{
16ca5ff0 1461 struct bt_ctf_field_type_common_enumeration *enum_ft = (void *) type;
312c056a
PP
1462 struct bt_ctf_field_enumeration *enumeration = g_new0(
1463 struct bt_ctf_field_enumeration, 1);
3dca2276
PP
1464
1465 BT_LOGD("Creating CTF writer enumeration field object: ft-addr=%p", type);
1466
312c056a 1467 if (!enumeration) {
3dca2276 1468 BT_LOGE_STR("Failed to allocate one enumeration field.");
312c056a 1469 goto end;
3dca2276
PP
1470 }
1471
16ca5ff0 1472 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(enumeration),
312c056a 1473 (void *) type,
e1e02a22 1474 true, (bt_ctf_object_release_func)
312c056a
PP
1475 bt_ctf_field_enumeration_destroy_recursive,
1476 &bt_ctf_field_enumeration_methods);
1477 enumeration->container = (void *) bt_ctf_field_create(
16ca5ff0 1478 BT_CTF_FROM_COMMON(enum_ft->container_ft));
312c056a 1479 if (!enumeration->container) {
e1e02a22 1480 BT_CTF_OBJECT_PUT_REF_AND_RESET(enumeration);
312c056a
PP
1481 goto end;
1482 }
1483
1484 enumeration->common.spec.writer.serialize_func =
1485 (bt_ctf_field_serialize_recursive_func)
1486 bt_ctf_field_enumeration_serialize_recursive;
1487 BT_LOGD("Created CTF writer enumeration field object: addr=%p, ft-addr=%p",
1488 enumeration, type);
1489
1490end:
3dca2276
PP
1491 return (void *) enumeration;
1492}
1493
1494static
1495struct bt_ctf_field *bt_ctf_field_floating_point_create(
1496 struct bt_ctf_field_type *type)
1497{
16ca5ff0 1498 struct bt_ctf_field_common_floating_point *floating_point;
3dca2276
PP
1499
1500 BT_LOGD("Creating CTF writer floating point number field object: ft-addr=%p", type);
16ca5ff0 1501 floating_point = g_new0(struct bt_ctf_field_common_floating_point, 1);
3dca2276
PP
1502
1503 if (floating_point) {
16ca5ff0 1504 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(floating_point),
3dca2276 1505 (void *) type,
e1e02a22 1506 true, (bt_ctf_object_release_func)
312c056a 1507 bt_ctf_field_floating_point_destroy,
3dca2276
PP
1508 &bt_ctf_field_floating_point_methods);
1509 floating_point->common.spec.writer.serialize_func =
1510 (bt_ctf_field_serialize_recursive_func) bt_ctf_field_floating_point_serialize;
1511 BT_LOGD("Created CTF writer floating point number field object: addr=%p, ft-addr=%p",
1512 floating_point, type);
1513 } else {
1514 BT_LOGE_STR("Failed to allocate one floating point number field.");
1515 }
1516
1517 return (void *) floating_point;
1518}
1519
1520static
1521struct bt_ctf_field *bt_ctf_field_structure_create(
1522 struct bt_ctf_field_type *type)
1523{
16ca5ff0
PP
1524 struct bt_ctf_field_common_structure *structure = g_new0(
1525 struct bt_ctf_field_common_structure, 1);
3dca2276
PP
1526 int iret;
1527
1528 BT_LOGD("Creating CTF writer structure field object: ft-addr=%p", type);
1529
1530 if (!structure) {
1531 BT_LOGE_STR("Failed to allocate one structure field.");
1532 goto end;
1533 }
1534
16ca5ff0 1535 iret = bt_ctf_field_common_structure_initialize(BT_CTF_TO_COMMON(structure),
312c056a 1536 (void *) type,
e1e02a22 1537 true, (bt_ctf_object_release_func)
312c056a 1538 bt_ctf_field_structure_destroy_recursive,
3dca2276 1539 &bt_ctf_field_structure_methods,
16ca5ff0 1540 (bt_ctf_field_common_create_func) bt_ctf_field_create,
e1e02a22 1541 (GDestroyNotify) bt_ctf_object_put_ref);
3dca2276
PP
1542 structure->common.spec.writer.serialize_func =
1543 (bt_ctf_field_serialize_recursive_func) bt_ctf_field_structure_serialize_recursive;
1544 if (iret) {
e1e02a22 1545 BT_CTF_OBJECT_PUT_REF_AND_RESET(structure);
3dca2276
PP
1546 goto end;
1547 }
1548
1549 BT_LOGD("Created CTF writer structure field object: addr=%p, ft-addr=%p",
1550 structure, type);
1551
1552end:
1553 return (void *) structure;
1554}
1555
1556static
1557struct bt_ctf_field *bt_ctf_field_variant_create(struct bt_ctf_field_type *type)
1558{
16ca5ff0 1559 struct bt_ctf_field_type_common_variant *var_ft = (void *) type;
312c056a
PP
1560 struct bt_ctf_field_variant *variant = g_new0(
1561 struct bt_ctf_field_variant, 1);
3dca2276
PP
1562
1563 BT_LOGD("Creating CTF writer variant field object: ft-addr=%p", type);
1564
312c056a 1565 if (!variant) {
3dca2276 1566 BT_LOGE_STR("Failed to allocate one variant field.");
312c056a 1567 goto end;
3dca2276
PP
1568 }
1569
16ca5ff0 1570 bt_ctf_field_common_variant_initialize(BT_CTF_TO_COMMON(BT_CTF_TO_COMMON(variant)),
312c056a 1571 (void *) type,
e1e02a22 1572 true, (bt_ctf_object_release_func)
312c056a
PP
1573 bt_ctf_field_variant_destroy_recursive,
1574 &bt_ctf_field_variant_methods,
16ca5ff0 1575 (bt_ctf_field_common_create_func) bt_ctf_field_create,
e1e02a22 1576 (GDestroyNotify) bt_ctf_object_put_ref);
312c056a 1577 variant->tag = (void *) bt_ctf_field_create(
16ca5ff0 1578 BT_CTF_FROM_COMMON(var_ft->tag_ft));
312c056a
PP
1579 variant->common.common.spec.writer.serialize_func =
1580 (bt_ctf_field_serialize_recursive_func) bt_ctf_field_variant_serialize_recursive;
1581 BT_LOGD("Created CTF writer variant field object: addr=%p, ft-addr=%p",
1582 variant, type);
1583
1584end:
3dca2276
PP
1585 return (void *) variant;
1586}
1587
1588static
1589struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type)
1590{
16ca5ff0
PP
1591 struct bt_ctf_field_common_array *array =
1592 g_new0(struct bt_ctf_field_common_array, 1);
3dca2276
PP
1593 int ret;
1594
1595 BT_LOGD("Creating CTF writer array field object: ft-addr=%p", type);
1596 BT_ASSERT(type);
1597
1598 if (!array) {
1599 BT_LOGE_STR("Failed to allocate one array field.");
1600 goto end;
1601 }
1602
16ca5ff0 1603 ret = bt_ctf_field_common_array_initialize(BT_CTF_TO_COMMON(array),
3dca2276 1604 (void *) type,
e1e02a22 1605 true, (bt_ctf_object_release_func)
312c056a
PP
1606 bt_ctf_field_array_destroy_recursive,
1607 &bt_ctf_field_array_methods,
16ca5ff0 1608 (bt_ctf_field_common_create_func) bt_ctf_field_create,
e1e02a22 1609 (GDestroyNotify) bt_ctf_object_put_ref);
3dca2276
PP
1610 array->common.spec.writer.serialize_func =
1611 (bt_ctf_field_serialize_recursive_func) bt_ctf_field_array_serialize_recursive;
1612 if (ret) {
e1e02a22 1613 BT_CTF_OBJECT_PUT_REF_AND_RESET(array);
3dca2276
PP
1614 goto end;
1615 }
1616
1617 BT_LOGD("Created CTF writer array field object: addr=%p, ft-addr=%p",
1618 array, type);
1619
1620end:
1621 return (void *) array;
1622}
1623
1624static
1625struct bt_ctf_field *bt_ctf_field_sequence_create(struct bt_ctf_field_type *type)
1626{
16ca5ff0
PP
1627 struct bt_ctf_field_common_sequence *sequence = g_new0(
1628 struct bt_ctf_field_common_sequence, 1);
3dca2276
PP
1629
1630 BT_LOGD("Creating CTF writer sequence field object: ft-addr=%p", type);
1631
1632 if (sequence) {
16ca5ff0 1633 bt_ctf_field_common_sequence_initialize(BT_CTF_TO_COMMON(sequence),
3dca2276 1634 (void *) type,
e1e02a22 1635 true, (bt_ctf_object_release_func)
312c056a
PP
1636 bt_ctf_field_sequence_destroy_recursive,
1637 &bt_ctf_field_sequence_methods,
e1e02a22 1638 (GDestroyNotify) bt_ctf_object_put_ref);
3dca2276
PP
1639 sequence->common.spec.writer.serialize_func =
1640 (bt_ctf_field_serialize_recursive_func) bt_ctf_field_sequence_serialize_recursive;
1641 BT_LOGD("Created CTF writer sequence field object: addr=%p, ft-addr=%p",
1642 sequence, type);
1643 } else {
1644 BT_LOGE_STR("Failed to allocate one sequence field.");
1645 }
1646
1647 return (void *) sequence;
1648}
1649
1650static
1651struct bt_ctf_field *bt_ctf_field_string_create(struct bt_ctf_field_type *type)
1652{
16ca5ff0
PP
1653 struct bt_ctf_field_common_string *string = g_new0(
1654 struct bt_ctf_field_common_string, 1);
3dca2276
PP
1655
1656 BT_LOGD("Creating CTF writer string field object: ft-addr=%p", type);
1657
1658 if (string) {
16ca5ff0 1659 bt_ctf_field_common_string_initialize(BT_CTF_TO_COMMON(string),
3dca2276 1660 (void *) type,
e1e02a22 1661 true, (bt_ctf_object_release_func)
312c056a 1662 bt_ctf_field_string_destroy,
3dca2276
PP
1663 &bt_ctf_field_string_methods);
1664 string->common.spec.writer.serialize_func =
1665 (bt_ctf_field_serialize_recursive_func) bt_ctf_field_string_serialize;
1666 BT_LOGD("Created CTF writer string field object: addr=%p, ft-addr=%p",
1667 string, type);
1668 } else {
1669 BT_LOGE_STR("Failed to allocate one string field.");
1670 }
1671
1672 return (void *) string;
1673}
312c056a
PP
1674
1675static
1676void bt_ctf_field_enumeration_set_is_frozen_recursive(
16ca5ff0 1677 struct bt_ctf_field_common *field, bool is_frozen)
312c056a
PP
1678{
1679 struct bt_ctf_field_enumeration *enumeration = (void *) field;
1680
1681 if (enumeration->container) {
16ca5ff0 1682 bt_ctf_field_common_set_is_frozen_recursive(
312c056a
PP
1683 (void *) enumeration->container, is_frozen);
1684 }
1685
16ca5ff0 1686 bt_ctf_field_common_generic_set_is_frozen((void *) field, is_frozen);
312c056a
PP
1687}
1688
1689static
16ca5ff0 1690int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common *field)
312c056a
PP
1691{
1692 int ret = -1;
1693 struct bt_ctf_field_enumeration *enumeration = (void *) field;
1694
1695 if (enumeration->container) {
16ca5ff0 1696 ret = bt_ctf_field_common_validate_recursive(
312c056a
PP
1697 (void *) enumeration->container);
1698 }
1699
1700 return ret;
1701}
1702
1703static
16ca5ff0 1704bt_bool bt_ctf_field_enumeration_is_set_recursive(struct bt_ctf_field_common *field)
312c056a
PP
1705{
1706 bt_bool is_set = BT_FALSE;
1707 struct bt_ctf_field_enumeration *enumeration = (void *) field;
1708
1709 if (enumeration->container) {
16ca5ff0 1710 is_set = bt_ctf_field_common_is_set_recursive(
312c056a
PP
1711 (void *) enumeration->container);
1712 }
1713
1714 return is_set;
1715}
1716
1717static
16ca5ff0 1718void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common *field)
312c056a
PP
1719{
1720 struct bt_ctf_field_enumeration *enumeration = (void *) field;
1721
1722 if (enumeration->container) {
16ca5ff0 1723 bt_ctf_field_common_reset_recursive(
312c056a
PP
1724 (void *) enumeration->container);
1725 }
1726
16ca5ff0 1727 bt_ctf_field_common_generic_reset((void *) field);
312c056a
PP
1728}
1729
1730static
1731void bt_ctf_field_variant_set_is_frozen_recursive(
16ca5ff0 1732 struct bt_ctf_field_common *field, bool is_frozen)
312c056a
PP
1733{
1734 struct bt_ctf_field_variant *variant = (void *) field;
1735
1736 if (variant->tag) {
16ca5ff0 1737 bt_ctf_field_common_set_is_frozen_recursive(
312c056a
PP
1738 (void *) variant->tag, is_frozen);
1739 }
1740
16ca5ff0 1741 bt_ctf_field_common_variant_set_is_frozen_recursive((void *) field,
312c056a
PP
1742 is_frozen);
1743}
1744
1745static
16ca5ff0 1746int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common *field)
312c056a
PP
1747{
1748 int ret;
1749 struct bt_ctf_field_variant *variant = (void *) field;
1750
1751 if (variant->tag) {
16ca5ff0 1752 ret = bt_ctf_field_common_validate_recursive(
312c056a
PP
1753 (void *) variant->tag);
1754 if (ret) {
1755 goto end;
1756 }
1757 }
1758
16ca5ff0 1759 ret = bt_ctf_field_common_variant_validate_recursive((void *) field);
312c056a
PP
1760
1761end:
1762 return ret;
1763}
1764
1765static
16ca5ff0 1766bt_bool bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common *field)
312c056a
PP
1767{
1768 bt_bool is_set;
1769 struct bt_ctf_field_variant *variant = (void *) field;
1770
1771 if (variant->tag) {
16ca5ff0 1772 is_set = bt_ctf_field_common_is_set_recursive(
312c056a
PP
1773 (void *) variant->tag);
1774 if (is_set) {
1775 goto end;
1776 }
1777 }
1778
16ca5ff0 1779 is_set = bt_ctf_field_common_variant_is_set_recursive((void *) field);
312c056a
PP
1780
1781end:
1782 return is_set;
1783}
1784
1785static
16ca5ff0 1786void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common *field)
312c056a
PP
1787{
1788 struct bt_ctf_field_variant *variant = (void *) field;
1789
1790 if (variant->tag) {
16ca5ff0 1791 bt_ctf_field_common_reset_recursive(
312c056a
PP
1792 (void *) variant->tag);
1793 }
1794
16ca5ff0 1795 bt_ctf_field_common_variant_reset_recursive((void *) field);
312c056a
PP
1796}
1797
67d2ce02 1798BT_CTF_ASSERT_PRE_FUNC
312c056a 1799static inline bool field_to_set_has_expected_type(
16ca5ff0
PP
1800 struct bt_ctf_field_common *struct_field,
1801 const char *name, struct bt_ctf_field_common *value)
312c056a
PP
1802{
1803 bool ret = true;
16ca5ff0 1804 struct bt_ctf_field_type_common *expected_field_type = NULL;
312c056a
PP
1805
1806 expected_field_type =
16ca5ff0 1807 bt_ctf_field_type_common_structure_borrow_field_type_by_name(
312c056a
PP
1808 struct_field->type, name);
1809
16ca5ff0 1810 if (bt_ctf_field_type_common_compare(expected_field_type, value->type)) {
67d2ce02 1811 BT_CTF_ASSERT_PRE_MSG("Value field's type is different from the expected field type: "
16ca5ff0 1812 "value-ft-addr=%p, expected-ft-addr=%p", value->type,
312c056a
PP
1813 expected_field_type);
1814 ret = false;
1815 goto end;
1816 }
1817
1818end:
1819 return ret;
1820}
1821
1822BT_HIDDEN
1823int bt_ctf_field_structure_set_field_by_name(struct bt_ctf_field *field,
1824 const char *name, struct bt_ctf_field *value)
1825{
1826 int ret = 0;
1827 GQuark field_quark;
16ca5ff0
PP
1828 struct bt_ctf_field_common *common_field = (void *) field;
1829 struct bt_ctf_field_common_structure *structure =
1830 BT_CTF_FROM_COMMON(common_field);
1831 struct bt_ctf_field_common *common_value = (void *) value;
312c056a
PP
1832 size_t index;
1833 GHashTable *field_name_to_index;
16ca5ff0 1834 struct bt_ctf_field_type_common_structure *structure_ft;
312c056a 1835
67d2ce02
MJ
1836 BT_CTF_ASSERT_PRE_NON_NULL(field, "Parent field");
1837 BT_CTF_ASSERT_PRE_NON_NULL(name, "Field name");
1838 BT_CTF_ASSERT_PRE_NON_NULL(value, "Value field");
1839 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(common_field,
16ca5ff0 1840 BT_CTF_FIELD_TYPE_ID_STRUCT, "Parent field");
67d2ce02 1841 BT_CTF_ASSERT_PRE(field_to_set_has_expected_type(common_field,
312c056a
PP
1842 name, common_value),
1843 "Value field's type is different from the expected field type.");
1844 field_quark = g_quark_from_string(name);
16ca5ff0 1845 structure_ft = BT_CTF_FROM_COMMON(common_field->type);
312c056a
PP
1846 field_name_to_index = structure_ft->field_name_to_index;
1847 if (!g_hash_table_lookup_extended(field_name_to_index,
1848 GUINT_TO_POINTER(field_quark), NULL,
1849 (gpointer *) &index)) {
1850 BT_LOGV("Invalid parameter: no such field in structure field's type: "
1851 "struct-field-addr=%p, struct-ft-addr=%p, "
1852 "field-ft-addr=%p, name=\"%s\"",
1853 field, common_field->type, common_value->type, name);
1854 ret = -1;
1855 goto end;
1856 }
e1e02a22
PP
1857 bt_ctf_object_get_ref(value);
1858 BT_CTF_OBJECT_MOVE_REF(structure->fields->pdata[index], value);
312c056a
PP
1859
1860end:
1861 return ret;
1862}
This page took 0.112397 seconds and 4 git commands to generate.