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