Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[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>
c4f23e30 30#include <stdbool.h>
3dca2276
PP
31#include <stdlib.h>
32
217cf9d3 33#include <babeltrace2-ctf-writer/object.h>
578e048b
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
67d2ce02
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 || \
16ca5ff0
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
67d2ce02 58 BT_CTF_ASSERT_PRE_NON_NULL(field, "Field");
98b15851
PP
59 BT_ASSERT_DBG(field_type_common_has_known_id(field->type));
60 BT_ASSERT_DBG(field->methods->copy);
16ca5ff0
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,
e1e02a22 76 bool is_shared, bt_ctf_object_release_func release_func,
16ca5ff0
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++) {
50233236 95 struct bt_ctf_field_common *member_field;
16ca5ff0
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);
50233236
FD
99 member_field = field_create_func(struct_field->type);
100 if (!member_field) {
16ca5ff0
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
50233236 107 g_ptr_array_index(structure->fields, i) = member_field;
16ca5ff0
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,
e1e02a22 120 bool is_shared, bt_ctf_object_release_func release_func,
16ca5ff0
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++) {
50233236 146 struct bt_ctf_field_common *member_field;
16ca5ff0
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
50233236
FD
151 member_field = field_create_func(var_choice->type);
152 if (!member_field) {
16ca5ff0
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
50233236 159 g_ptr_array_index(variant->fields, i) = member_field;
16ca5ff0
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,
e1e02a22 172 bool is_shared, bt_ctf_object_release_func release_func,
16ca5ff0
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,
e1e02a22 198 bool is_shared, bt_ctf_object_release_func release_func,
16ca5ff0
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);
98b15851 210 BT_ASSERT_DBG(type);
16ca5ff0
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,
e1e02a22 242 bool is_shared, bt_ctf_object_release_func release_func,
16ca5ff0
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);
98b15851 250 BT_ASSERT_DBG(type);
16ca5ff0
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
98b15851 280 BT_ASSERT_DBG(field);
16ca5ff0
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);
98b15851 292 BT_ASSERT_DBG(this_ret == 0);
67d2ce02 293 BT_CTF_ASSERT_PRE_MSG("Invalid structure field's field: "
16ca5ff0
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
98b15851 311 BT_ASSERT_DBG(field);
16ca5ff0
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
98b15851 331 BT_ASSERT_DBG(field);
16ca5ff0
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) {
67d2ce02
MJ
336 BT_CTF_ASSERT_PRE_MSG("Invalid array field's element field: "
337 "array-field-addr=%p, %" PRId64 ", "
16ca5ff0
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{
67d2ce02 351 int64_t i;
16ca5ff0
PP
352 int ret = 0;
353 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
354
98b15851 355 BT_ASSERT_DBG(field);
16ca5ff0
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) {
67d2ce02
MJ
361 BT_CTF_ASSERT_PRE_MSG("Invalid sequence field's element field: "
362 "seq-field-addr=%p, %" PRId64 ", "
16ca5ff0
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{
98b15851 375 BT_ASSERT_DBG(field);
16ca5ff0
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
98b15851 385 BT_ASSERT_DBG(field);
16ca5ff0
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
98b15851 408 BT_ASSERT_DBG(field);
16ca5ff0
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
98b15851 418 BT_ASSERT_DBG(field);
16ca5ff0
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
98b15851 441 BT_ASSERT_DBG(field);
16ca5ff0
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);
98b15851
PP
559 BT_ASSERT_DBG(field_type_common_has_known_id(field->type));
560 BT_ASSERT_DBG(field->methods->set_is_frozen);
16ca5ff0
PP
561 field->methods->set_is_frozen(field, is_frozen);
562
563end:
564 return;
565}
566
567BT_HIDDEN
00409097 568bt_ctf_bool bt_ctf_field_common_generic_is_set(struct bt_ctf_field_common *field)
16ca5ff0
PP
569{
570 return field && field->payload_set;
571}
572
573BT_HIDDEN
00409097 574bt_ctf_bool bt_ctf_field_common_structure_is_set_recursive(
16ca5ff0
PP
575 struct bt_ctf_field_common *field)
576{
00409097 577 bt_ctf_bool is_set = BT_CTF_FALSE;
16ca5ff0
PP
578 size_t i;
579 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
580
98b15851 581 BT_ASSERT_DBG(field);
16ca5ff0
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
00409097 596bt_ctf_bool bt_ctf_field_common_variant_is_set_recursive(struct bt_ctf_field_common *field)
16ca5ff0
PP
597{
598 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field);
00409097 599 bt_ctf_bool is_set = BT_CTF_FALSE;
16ca5ff0 600
98b15851 601 BT_ASSERT_DBG(field);
16ca5ff0
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
00409097 612bt_ctf_bool bt_ctf_field_common_array_is_set_recursive(struct bt_ctf_field_common *field)
16ca5ff0
PP
613{
614 size_t i;
00409097 615 bt_ctf_bool is_set = BT_CTF_FALSE;
16ca5ff0
PP
616 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
617
98b15851 618 BT_ASSERT_DBG(field);
16ca5ff0
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
00409097 632bt_ctf_bool bt_ctf_field_common_sequence_is_set_recursive(struct bt_ctf_field_common *field)
16ca5ff0
PP
633{
634 size_t i;
00409097 635 bt_ctf_bool is_set = BT_CTF_FALSE;
16ca5ff0
PP
636 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
637
98b15851 638 BT_ASSERT_DBG(field);
16ca5ff0
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,
3dca2276 659 .copy = NULL,
16ca5ff0
PP
660 .is_set = bt_ctf_field_common_generic_is_set,
661 .reset = bt_ctf_field_common_generic_reset,
3dca2276
PP
662};
663
16ca5ff0
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,
3dca2276 667 .copy = NULL,
16ca5ff0
PP
668 .is_set = bt_ctf_field_common_generic_is_set,
669 .reset = bt_ctf_field_common_generic_reset,
3dca2276
PP
670};
671
312c056a
PP
672static
673void bt_ctf_field_enumeration_set_is_frozen_recursive(
16ca5ff0 674 struct bt_ctf_field_common *field, bool is_frozen);
312c056a
PP
675
676static
16ca5ff0 677int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common *field);
312c056a
PP
678
679static
00409097 680bt_ctf_bool bt_ctf_field_enumeration_is_set_recursive(
16ca5ff0 681 struct bt_ctf_field_common *field);
312c056a
PP
682
683static
16ca5ff0 684void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common *field);
312c056a 685
16ca5ff0 686static struct bt_ctf_field_common_methods bt_ctf_field_enumeration_methods = {
312c056a
PP
687 .set_is_frozen = bt_ctf_field_enumeration_set_is_frozen_recursive,
688 .validate = bt_ctf_field_enumeration_validate_recursive,
3dca2276 689 .copy = NULL,
312c056a
PP
690 .is_set = bt_ctf_field_enumeration_is_set_recursive,
691 .reset = bt_ctf_field_enumeration_reset_recursive,
3dca2276
PP
692};
693
16ca5ff0
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,
3dca2276 697 .copy = NULL,
16ca5ff0
PP
698 .is_set = bt_ctf_field_common_generic_is_set,
699 .reset = bt_ctf_field_common_generic_reset,
3dca2276
PP
700};
701
16ca5ff0
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,
3dca2276 705 .copy = NULL,
16ca5ff0
PP
706 .is_set = bt_ctf_field_common_structure_is_set_recursive,
707 .reset = bt_ctf_field_common_structure_reset_recursive,
3dca2276
PP
708};
709
16ca5ff0
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,
3dca2276 713 .copy = NULL,
16ca5ff0
PP
714 .is_set = bt_ctf_field_common_sequence_is_set_recursive,
715 .reset = bt_ctf_field_common_sequence_reset_recursive,
3dca2276
PP
716};
717
16ca5ff0
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,
3dca2276 721 .copy = NULL,
16ca5ff0
PP
722 .is_set = bt_ctf_field_common_array_is_set_recursive,
723 .reset = bt_ctf_field_common_array_reset_recursive,
3dca2276
PP
724};
725
312c056a 726static
16ca5ff0 727void bt_ctf_field_variant_set_is_frozen_recursive(struct bt_ctf_field_common *field,
312c056a
PP
728 bool is_frozen);
729
730static
16ca5ff0 731int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common *field);
312c056a
PP
732
733static
00409097 734bt_ctf_bool bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common *field);
312c056a
PP
735
736static
16ca5ff0 737void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common *field);
312c056a 738
16ca5ff0 739static struct bt_ctf_field_common_methods bt_ctf_field_variant_methods = {
312c056a
PP
740 .set_is_frozen = bt_ctf_field_variant_set_is_frozen_recursive,
741 .validate = bt_ctf_field_variant_validate_recursive,
3dca2276 742 .copy = NULL,
312c056a
PP
743 .is_set = bt_ctf_field_variant_is_set_recursive,
744 .reset = bt_ctf_field_variant_reset_recursive,
3dca2276
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 *) = {
16ca5ff0
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,
3dca2276
PP
781};
782
783typedef int (*bt_ctf_field_serialize_recursive_func)(
013f35c6 784 struct bt_ctf_field_common *, struct bt_ctfser *,
3dca2276
PP
785 enum bt_ctf_byte_order);
786
312c056a
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);
16ca5ff0 791 bt_ctf_field_common_integer_finalize((void *) field);
312c056a
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);
16ca5ff0 800 bt_ctf_field_common_floating_point_finalize((void *) field);
312c056a
PP
801 g_free(field);
802}
803
804static
805void bt_ctf_field_enumeration_destroy_recursive(struct bt_ctf_field *field)
806{
16ca5ff0 807 struct bt_ctf_field_enumeration *enumeration = BT_CTF_FROM_COMMON(field);
312c056a
PP
808
809 BT_LOGD("Destroying CTF writer enumeration field object: addr=%p",
810 field);
811 BT_LOGD_STR("Putting container field.");
e1e02a22 812 bt_ctf_object_put_ref(enumeration->container);
16ca5ff0 813 bt_ctf_field_common_finalize((void *) field);
312c056a
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);
16ca5ff0 821 bt_ctf_field_common_structure_finalize_recursive((void *) field);
312c056a
PP
822 g_free(field);
823}
824
825static
826void bt_ctf_field_variant_destroy_recursive(struct bt_ctf_field *field)
827{
16ca5ff0 828 struct bt_ctf_field_variant *variant = BT_CTF_FROM_COMMON(field);
312c056a
PP
829
830 BT_LOGD("Destroying CTF writer variant field object: addr=%p", field);
831 BT_LOGD_STR("Putting tag field.");
e1e02a22 832 bt_ctf_object_put_ref(variant->tag);
16ca5ff0 833 bt_ctf_field_common_variant_finalize_recursive((void *) field);
312c056a
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);
16ca5ff0 841 bt_ctf_field_common_array_finalize_recursive((void *) field);
312c056a
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);
16ca5ff0 849 bt_ctf_field_common_sequence_finalize_recursive((void *) field);
312c056a
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);
16ca5ff0 857 bt_ctf_field_common_string_finalize((void *) field);
312c056a
PP
858 g_free(field);
859}
860
3dca2276
PP
861BT_HIDDEN
862int bt_ctf_field_serialize_recursive(struct bt_ctf_field *field,
013f35c6 863 struct bt_ctfser *ctfser,
3dca2276
PP
864 enum bt_ctf_byte_order native_byte_order)
865{
16ca5ff0 866 struct bt_ctf_field_common *field_common = (void *) field;
3dca2276
PP
867 bt_ctf_field_serialize_recursive_func serialize_func;
868
98b15851 869 BT_ASSERT_DBG(ctfser);
67d2ce02 870 BT_CTF_ASSERT_PRE_NON_NULL(field, "Field");
98b15851 871 BT_ASSERT_DBG(field_common->spec.writer.serialize_func);
3dca2276 872 serialize_func = field_common->spec.writer.serialize_func;
013f35c6 873 return serialize_func(field_common, ctfser,
3dca2276
PP
874 native_byte_order);
875}
876
877static
013f35c6
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)
3dca2276
PP
881{
882 int ret;
013f35c6
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;
3dca2276 888
67d2ce02 889 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "Integer field");
ef267d12 890 BT_LOGT("Serializing CTF writer integer field: addr=%p, native-bo=%s",
013f35c6
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;
3dca2276
PP
896 }
897
d6981059
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
91d81473 912 if (G_UNLIKELY(ret)) {
013f35c6 913 BT_LOGE("Cannot serialize integer field: ret=%d", ret);
3dca2276
PP
914 goto end;
915 }
916
3dca2276
PP
917end:
918 return ret;
919}
920
921static
013f35c6
PP
922int bt_ctf_field_enumeration_serialize_recursive(
923 struct bt_ctf_field_common *field, struct bt_ctfser *ctfser,
3dca2276
PP
924 enum bt_ctf_byte_order native_byte_order)
925{
312c056a 926 struct bt_ctf_field_enumeration *enumeration = (void *) field;
3dca2276 927
ef267d12 928 BT_LOGT("Serializing enumeration field: addr=%p, native-bo=%s",
013f35c6 929 field, bt_ctf_byte_order_string(native_byte_order));
ef267d12 930 BT_LOGT_STR("Serializing enumeration field's payload field.");
312c056a 931 return bt_ctf_field_serialize_recursive(
013f35c6 932 (void *) enumeration->container, ctfser, native_byte_order);
3dca2276
PP
933}
934
935static
16ca5ff0 936int bt_ctf_field_floating_point_serialize(struct bt_ctf_field_common *field,
013f35c6 937 struct bt_ctfser *ctfser,
3dca2276
PP
938 enum bt_ctf_byte_order native_byte_order)
939{
013f35c6
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;
3dca2276 945
67d2ce02 946 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "Floating point number field");
ef267d12 947 BT_LOGT("Serializing floating point number field: "
013f35c6
PP
948 "addr=%p, native-bo=%s", field,
949 bt_ctf_byte_order_string(native_byte_order));
3dca2276 950
013f35c6
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
91d81473 970 if (G_UNLIKELY(ret)) {
013f35c6
PP
971 BT_LOGE("Cannot serialize floating point number field: "
972 "ret=%d", ret);
973 goto end;
3dca2276
PP
974 }
975
976end:
977 return ret;
978}
979
980static
16ca5ff0 981int bt_ctf_field_structure_serialize_recursive(struct bt_ctf_field_common *field,
013f35c6 982 struct bt_ctfser *ctfser,
3dca2276
PP
983 enum bt_ctf_byte_order native_byte_order)
984{
985 int64_t i;
013f35c6 986 int ret;
16ca5ff0 987 struct bt_ctf_field_common_structure *structure = BT_CTF_FROM_COMMON(field);
3dca2276 988
ef267d12 989 BT_LOGT("Serializing structure field: addr=%p, native-bo=%s",
013f35c6
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);
91d81473 993 if (G_UNLIKELY(ret)) {
013f35c6
PP
994 BT_LOGE("Cannot align offset before serializing structure field: "
995 "ret=%d", ret);
3dca2276
PP
996 goto end;
997 }
998
999 for (i = 0; i < structure->fields->len; i++) {
16ca5ff0 1000 struct bt_ctf_field_common *member = g_ptr_array_index(
3dca2276
PP
1001 structure->fields, i);
1002 const char *field_name = NULL;
1003
ef267d12 1004 BT_LOGT("Serializing structure field's field: ser-offset=%" PRIu64 ", "
013f35c6
PP
1005 "field-addr=%p, index=%" PRIu64,
1006 bt_ctfser_get_offset_in_current_packet_bits(ctfser),
1007 member, i);
3dca2276 1008
91d81473 1009 if (G_UNLIKELY(!member)) {
16ca5ff0 1010 ret = bt_ctf_field_type_common_structure_borrow_field_by_index(
3dca2276 1011 field->type, &field_name, NULL, i);
98b15851 1012 BT_ASSERT_DBG(ret == 0);
3dca2276
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
013f35c6 1021 ret = bt_ctf_field_serialize_recursive((void *) member, ctfser,
3dca2276 1022 native_byte_order);
91d81473 1023 if (G_UNLIKELY(ret)) {
16ca5ff0 1024 ret = bt_ctf_field_type_common_structure_borrow_field_by_index(
3dca2276 1025 field->type, &field_name, NULL, i);
98b15851 1026 BT_ASSERT_DBG(ret == 0);
3dca2276
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
16ca5ff0 1040int bt_ctf_field_variant_serialize_recursive(struct bt_ctf_field_common *field,
013f35c6 1041 struct bt_ctfser *ctfser,
3dca2276
PP
1042 enum bt_ctf_byte_order native_byte_order)
1043{
16ca5ff0 1044 struct bt_ctf_field_common_variant *variant = BT_CTF_FROM_COMMON(field);
3dca2276 1045
ef267d12 1046 BT_LOGT("Serializing variant field: addr=%p, native-bo=%s",
013f35c6 1047 field, bt_ctf_byte_order_string(native_byte_order));
ef267d12 1048 BT_LOGT_STR("Serializing variant field's payload field.");
3dca2276 1049 return bt_ctf_field_serialize_recursive(
013f35c6 1050 (void *) variant->current_field, ctfser, native_byte_order);
3dca2276
PP
1051}
1052
1053static
16ca5ff0 1054int bt_ctf_field_array_serialize_recursive(struct bt_ctf_field_common *field,
013f35c6 1055 struct bt_ctfser *ctfser,
3dca2276
PP
1056 enum bt_ctf_byte_order native_byte_order)
1057{
1058 int64_t i;
1059 int ret = 0;
16ca5ff0 1060 struct bt_ctf_field_common_array *array = BT_CTF_FROM_COMMON(field);
3dca2276 1061
ef267d12 1062 BT_LOGT("Serializing array field: addr=%p, native-bo=%s",
013f35c6 1063 field, bt_ctf_byte_order_string(native_byte_order));
3dca2276
PP
1064
1065 for (i = 0; i < array->elements->len; i++) {
16ca5ff0 1066 struct bt_ctf_field_common *elem_field =
3dca2276
PP
1067 g_ptr_array_index(array->elements, i);
1068
ef267d12 1069 BT_LOGT("Serializing array field's element field: "
013f35c6
PP
1070 "ser-offset=%" PRIu64 ", field-addr=%p, index=%" PRId64,
1071 bt_ctfser_get_offset_in_current_packet_bits(ctfser),
1072 elem_field, i);
3dca2276 1073 ret = bt_ctf_field_serialize_recursive(
013f35c6 1074 (void *) elem_field, ctfser, native_byte_order);
91d81473 1075 if (G_UNLIKELY(ret)) {
3dca2276
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
16ca5ff0 1088int bt_ctf_field_sequence_serialize_recursive(struct bt_ctf_field_common *field,
013f35c6 1089 struct bt_ctfser *ctfser,
3dca2276
PP
1090 enum bt_ctf_byte_order native_byte_order)
1091{
1092 int64_t i;
1093 int ret = 0;
16ca5ff0 1094 struct bt_ctf_field_common_sequence *sequence = BT_CTF_FROM_COMMON(field);
3dca2276 1095
ef267d12 1096 BT_LOGT("Serializing sequence field: addr=%p, native-bo=%s",
013f35c6 1097 field, bt_ctf_byte_order_string(native_byte_order));
3dca2276
PP
1098
1099 for (i = 0; i < sequence->elements->len; i++) {
16ca5ff0 1100 struct bt_ctf_field_common *elem_field =
3dca2276
PP
1101 g_ptr_array_index(sequence->elements, i);
1102
ef267d12 1103 BT_LOGT("Serializing sequence field's element field: "
013f35c6
PP
1104 "ser-offset=%" PRIu64 ", field-addr=%p, index=%" PRId64,
1105 bt_ctfser_get_offset_in_current_packet_bits(ctfser),
1106 elem_field, i);
3dca2276 1107 ret = bt_ctf_field_serialize_recursive(
013f35c6 1108 (void *) elem_field, ctfser, native_byte_order);
91d81473 1109 if (G_UNLIKELY(ret)) {
3dca2276
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
16ca5ff0 1122int bt_ctf_field_string_serialize(struct bt_ctf_field_common *field,
013f35c6 1123 struct bt_ctfser *ctfser,
3dca2276
PP
1124 enum bt_ctf_byte_order native_byte_order)
1125{
013f35c6 1126 int ret;
16ca5ff0 1127 struct bt_ctf_field_common_string *string = BT_CTF_FROM_COMMON(field);
3dca2276 1128
67d2ce02 1129 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_IS_SET(field, "String field");
ef267d12 1130 BT_LOGT("Serializing string field: addr=%p, native-bo=%s",
013f35c6
PP
1131 field, bt_ctf_byte_order_string((int) native_byte_order));
1132 ret = bt_ctfser_write_string(ctfser, (const char *) string->buf->data);
91d81473 1133 if (G_UNLIKELY(ret)) {
013f35c6
PP
1134 BT_LOGE("Cannot serialize string field: ret=%d", ret);
1135 goto end;
3dca2276
PP
1136 }
1137
1138end:
3dca2276
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
67d2ce02 1147 BT_CTF_ASSERT_PRE_NON_NULL(type, "Field type");
98b15851 1148 BT_ASSERT_DBG(field_type_common_has_known_id((void *) type));
67d2ce02 1149 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_validate((void *) type) == 0,
16ca5ff0 1150 "Field type is invalid: ft-addr=%p", type);
3dca2276
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
16ca5ff0 1157 bt_ctf_field_type_common_freeze((void *) type);
3dca2276
PP
1158
1159end:
1160 return field;
1161}
1162
1163struct bt_ctf_field_type *bt_ctf_field_get_type(struct bt_ctf_field *field)
1164{
e1e02a22 1165 return bt_ctf_object_get_ref(bt_ctf_field_common_borrow_type((void *) field));
3dca2276
PP
1166}
1167
1168enum bt_ctf_field_type_id bt_ctf_field_get_type_id(struct bt_ctf_field *field)
1169{
16ca5ff0 1170 struct bt_ctf_field_common *field_common = (void *) field;
3dca2276 1171
67d2ce02 1172 BT_CTF_ASSERT_PRE_NON_NULL(field, "Field");
3dca2276
PP
1173 return (int) field_common->type->id;
1174}
1175
3dca2276
PP
1176int bt_ctf_field_sequence_set_length(struct bt_ctf_field *field,
1177 struct bt_ctf_field *length_field)
1178{
312c056a 1179 int ret;
16ca5ff0 1180 struct bt_ctf_field_common *common_length_field = (void *) length_field;
312c056a
PP
1181 uint64_t length;
1182
67d2ce02
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 ||
16ca5ff0
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",
312c056a
PP
1188 length_field);
1189
16ca5ff0 1190 if (common_length_field->type->id == BT_CTF_FIELD_TYPE_ID_ENUM) {
312c056a
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);
98b15851 1198 BT_ASSERT_DBG(ret == 0);
16ca5ff0
PP
1199 return bt_ctf_field_common_sequence_set_length((void *) field,
1200 length, (bt_ctf_field_common_create_func) bt_ctf_field_create);
3dca2276
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{
e1e02a22 1206 return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_index(
094ff7c0 1207 (void *) field, index));
3dca2276
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{
e1e02a22 1213 return bt_ctf_object_get_ref(bt_ctf_field_common_structure_borrow_field_by_name(
094ff7c0 1214 (void *) field, name));
3dca2276
PP
1215}
1216
3dca2276
PP
1217struct bt_ctf_field *bt_ctf_field_array_get_field(
1218 struct bt_ctf_field *field, uint64_t index)
1219{
e1e02a22 1220 return bt_ctf_object_get_ref(
16ca5ff0 1221 bt_ctf_field_common_array_borrow_field((void *) field, index));
3dca2276
PP
1222}
1223
1224struct bt_ctf_field *bt_ctf_field_sequence_get_field(
1225 struct bt_ctf_field *field, uint64_t index)
1226{
e1e02a22 1227 return bt_ctf_object_get_ref(
16ca5ff0 1228 bt_ctf_field_common_sequence_borrow_field((void *) field, index));
3dca2276
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{
312c056a
PP
1234 struct bt_ctf_field_variant *variant_field = (void *) field;
1235 struct bt_ctf_field_enumeration *enum_field = (void *) tag_field;
16ca5ff0
PP
1236 struct bt_ctf_field_type_common_variant *variant_ft;
1237 struct bt_ctf_field_type_common_enumeration *tag_ft;
312c056a 1238 struct bt_ctf_field *current_field = NULL;
00409097 1239 bt_ctf_bool is_signed;
312c056a
PP
1240 uint64_t tag_uval;
1241 int ret;
1242
67d2ce02
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(
16ca5ff0
PP
1247 (struct bt_ctf_field_common *) tag_field,
1248 BT_CTF_FIELD_TYPE_ID_ENUM, "Tag field");
67d2ce02 1249 BT_CTF_ASSERT_PRE_CTF_FIELD_COMMON_HAS_TYPE_ID(
16ca5ff0
PP
1250 (struct bt_ctf_field_common *) field,
1251 BT_CTF_FIELD_TYPE_ID_VARIANT, "Field");
67d2ce02 1252 BT_CTF_ASSERT_PRE(
16ca5ff0
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);
67d2ce02 1256 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_compare(
16ca5ff0
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,
312c056a 1260 enum_field->common.type);
16ca5ff0 1261 tag_ft = BT_CTF_FROM_COMMON(enum_field->common.type);
312c056a
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
98b15851 1275 BT_ASSERT_DBG(ret == 0);
16ca5ff0 1276 ret = bt_ctf_field_common_variant_set_tag((void *) field, tag_uval,
312c056a
PP
1277 is_signed);
1278 if (ret) {
1279 goto end;
1280 }
1281
e1e02a22
PP
1282 bt_ctf_object_put_ref(variant_field->tag);
1283 variant_field->tag = bt_ctf_object_get_ref(tag_field);
312c056a 1284 current_field = bt_ctf_field_variant_get_current_field(field);
98b15851 1285 BT_ASSERT_DBG(current_field);
312c056a
PP
1286
1287end:
1288 return current_field;
3dca2276
PP
1289}
1290
1291struct bt_ctf_field *bt_ctf_field_variant_get_current_field(
1292 struct bt_ctf_field *variant_field)
1293{
e1e02a22 1294 return bt_ctf_object_get_ref(bt_ctf_field_common_variant_borrow_current_field(
094ff7c0 1295 (void *) variant_field));
3dca2276
PP
1296}
1297
312c056a
PP
1298BT_HIDDEN
1299struct bt_ctf_field *bt_ctf_field_enumeration_borrow_container(
1300 struct bt_ctf_field *field)
3dca2276 1301{
312c056a
PP
1302 struct bt_ctf_field_enumeration *enumeration = (void *) field;
1303
67d2ce02
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,
312c056a 1306 BT_CTF_FIELD_TYPE_ID_ENUM, "Field");
98b15851 1307 BT_ASSERT_DBG(enumeration->container);
312c056a 1308 return (void *) enumeration->container;
3dca2276
PP
1309}
1310
312c056a
PP
1311struct bt_ctf_field *bt_ctf_field_enumeration_get_container(
1312 struct bt_ctf_field *field)
3dca2276 1313{
e1e02a22 1314 return bt_ctf_object_get_ref(bt_ctf_field_enumeration_borrow_container(field));
3dca2276
PP
1315}
1316
312c056a
PP
1317int bt_ctf_field_integer_signed_get_value(struct bt_ctf_field *field,
1318 int64_t *value)
3dca2276 1319{
16ca5ff0 1320 struct bt_ctf_field_common_integer *integer = (void *) field;
312c056a 1321
67d2ce02
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),
16ca5ff0 1326 BT_CTF_FIELD_TYPE_ID_INTEGER, "Field");
67d2ce02 1327 BT_CTF_ASSERT_PRE(bt_ctf_field_type_common_integer_is_signed(
312c056a 1328 integer->common.type),
16ca5ff0 1329 "Field's type is unsigned: field-addr=%p", field);
312c056a
PP
1330 *value = integer->payload.signd;
1331 return 0;
3dca2276
PP
1332}
1333
1334int bt_ctf_field_integer_signed_set_value(struct bt_ctf_field *field,
1335 int64_t value)
1336{
312c056a 1337 int ret = 0;
16ca5ff0
PP
1338 struct bt_ctf_field_common_integer *integer = (void *) field;
1339 struct bt_ctf_field_type_common_integer *integer_type;
312c056a 1340
67d2ce02
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),
16ca5ff0
PP
1344 BT_CTF_FIELD_TYPE_ID_INTEGER, "Field");
1345 integer_type = BT_CTF_FROM_COMMON(integer->common.type);
67d2ce02 1346 BT_CTF_ASSERT_PRE(
16ca5ff0
PP
1347 bt_ctf_field_type_common_integer_is_signed(integer->common.type),
1348 "Field's type is unsigned: field-addr=%p", field);
67d2ce02 1349 BT_CTF_ASSERT_PRE(value_is_in_range_signed(integer_type->size, value),
16ca5ff0 1350 "Value is out of bounds: value=%" PRId64 ", field-addr=%p",
312c056a
PP
1351 value, field);
1352 integer->payload.signd = value;
16ca5ff0 1353 bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer), true);
312c056a 1354 return ret;
3dca2276
PP
1355}
1356
1357int bt_ctf_field_integer_unsigned_get_value(struct bt_ctf_field *field,
1358 uint64_t *value)
1359{
16ca5ff0 1360 struct bt_ctf_field_common_integer *integer = (void *) field;
312c056a 1361
67d2ce02
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),
16ca5ff0 1366 BT_CTF_FIELD_TYPE_ID_INTEGER, "Field");
67d2ce02 1367 BT_CTF_ASSERT_PRE(
16ca5ff0
PP
1368 !bt_ctf_field_type_common_integer_is_signed(integer->common.type),
1369 "Field's type is signed: field-addr=%p", field);
312c056a
PP
1370 *value = integer->payload.unsignd;
1371 return 0;
3dca2276
PP
1372}
1373
312c056a
PP
1374int bt_ctf_field_integer_unsigned_set_value(struct bt_ctf_field *field,
1375 uint64_t value)
3dca2276 1376{
16ca5ff0
PP
1377 struct bt_ctf_field_common_integer *integer = (void *) field;
1378 struct bt_ctf_field_type_common_integer *integer_type;
312c056a 1379
67d2ce02
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),
16ca5ff0
PP
1383 BT_CTF_FIELD_TYPE_ID_INTEGER, "Field");
1384 integer_type = BT_CTF_FROM_COMMON(integer->common.type);
67d2ce02 1385 BT_CTF_ASSERT_PRE(
16ca5ff0
PP
1386 !bt_ctf_field_type_common_integer_is_signed(integer->common.type),
1387 "Field's type is signed: field-addr=%p", field);
67d2ce02 1388 BT_CTF_ASSERT_PRE(value_is_in_range_unsigned(integer_type->size, value),
16ca5ff0 1389 "Value is out of bounds: value=%" PRIu64 ", field-addr=%p",
312c056a
PP
1390 value, field);
1391 integer->payload.unsignd = value;
16ca5ff0 1392 bt_ctf_field_common_set(BT_CTF_TO_COMMON(integer), true);
312c056a 1393 return 0;
3dca2276
PP
1394}
1395
1396int bt_ctf_field_floating_point_get_value(struct bt_ctf_field *field,
1397 double *value)
1398{
16ca5ff0 1399 return bt_ctf_field_common_floating_point_get_value((void *) field, value);
3dca2276
PP
1400}
1401
1402int bt_ctf_field_floating_point_set_value(struct bt_ctf_field *field,
1403 double value)
1404{
16ca5ff0 1405 return bt_ctf_field_common_floating_point_set_value((void *) field, value);
3dca2276
PP
1406}
1407
1408const char *bt_ctf_field_string_get_value(struct bt_ctf_field *field)
1409{
16ca5ff0 1410 return bt_ctf_field_common_string_get_value((void *) field);
3dca2276
PP
1411}
1412
1413int bt_ctf_field_string_set_value(struct bt_ctf_field *field, const char *value)
1414{
16ca5ff0 1415 return bt_ctf_field_common_string_set_value((void *) field, value);
3dca2276
PP
1416}
1417
1418int bt_ctf_field_string_append(struct bt_ctf_field *field, const char *value)
1419{
16ca5ff0 1420 return bt_ctf_field_common_string_append((void *) field, value);
3dca2276
PP
1421}
1422
1423int bt_ctf_field_string_append_len(struct bt_ctf_field *field,
1424 const char *value, unsigned int length)
1425{
16ca5ff0 1426 return bt_ctf_field_common_string_append_len((void *) field, value, length);
3dca2276
PP
1427}
1428
1429struct bt_ctf_field *bt_ctf_field_copy(struct bt_ctf_field *field)
1430{
16ca5ff0 1431 return (void *) bt_ctf_field_common_copy((void *) field);
3dca2276
PP
1432}
1433
1434static
1435struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *type)
1436{
16ca5ff0
PP
1437 struct bt_ctf_field_common_integer *integer =
1438 g_new0(struct bt_ctf_field_common_integer, 1);
3dca2276
PP
1439
1440 BT_LOGD("Creating CTF writer integer field object: ft-addr=%p", type);
1441
1442 if (integer) {
16ca5ff0 1443 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(integer), (void *) type,
3fea54f6 1444 true,
e1e02a22 1445 (bt_ctf_object_release_func) bt_ctf_field_integer_destroy,
3dca2276
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{
16ca5ff0 1462 struct bt_ctf_field_type_common_enumeration *enum_ft = (void *) type;
312c056a
PP
1463 struct bt_ctf_field_enumeration *enumeration = g_new0(
1464 struct bt_ctf_field_enumeration, 1);
3dca2276
PP
1465
1466 BT_LOGD("Creating CTF writer enumeration field object: ft-addr=%p", type);
1467
312c056a 1468 if (!enumeration) {
3dca2276 1469 BT_LOGE_STR("Failed to allocate one enumeration field.");
312c056a 1470 goto end;
3dca2276
PP
1471 }
1472
16ca5ff0 1473 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(enumeration),
312c056a 1474 (void *) type,
e1e02a22 1475 true, (bt_ctf_object_release_func)
312c056a
PP
1476 bt_ctf_field_enumeration_destroy_recursive,
1477 &bt_ctf_field_enumeration_methods);
1478 enumeration->container = (void *) bt_ctf_field_create(
16ca5ff0 1479 BT_CTF_FROM_COMMON(enum_ft->container_ft));
312c056a 1480 if (!enumeration->container) {
e1e02a22 1481 BT_CTF_OBJECT_PUT_REF_AND_RESET(enumeration);
312c056a
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:
3dca2276
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{
16ca5ff0 1499 struct bt_ctf_field_common_floating_point *floating_point;
3dca2276
PP
1500
1501 BT_LOGD("Creating CTF writer floating point number field object: ft-addr=%p", type);
16ca5ff0 1502 floating_point = g_new0(struct bt_ctf_field_common_floating_point, 1);
3dca2276
PP
1503
1504 if (floating_point) {
16ca5ff0 1505 bt_ctf_field_common_initialize(BT_CTF_TO_COMMON(floating_point),
3dca2276 1506 (void *) type,
e1e02a22 1507 true, (bt_ctf_object_release_func)
312c056a 1508 bt_ctf_field_floating_point_destroy,
3dca2276
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{
16ca5ff0
PP
1525 struct bt_ctf_field_common_structure *structure = g_new0(
1526 struct bt_ctf_field_common_structure, 1);
3dca2276
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
16ca5ff0 1536 iret = bt_ctf_field_common_structure_initialize(BT_CTF_TO_COMMON(structure),
312c056a 1537 (void *) type,
e1e02a22 1538 true, (bt_ctf_object_release_func)
312c056a 1539 bt_ctf_field_structure_destroy_recursive,
3dca2276 1540 &bt_ctf_field_structure_methods,
16ca5ff0 1541 (bt_ctf_field_common_create_func) bt_ctf_field_create,
e1e02a22 1542 (GDestroyNotify) bt_ctf_object_put_ref);
3dca2276
PP
1543 structure->common.spec.writer.serialize_func =
1544 (bt_ctf_field_serialize_recursive_func) bt_ctf_field_structure_serialize_recursive;
1545 if (iret) {
e1e02a22 1546 BT_CTF_OBJECT_PUT_REF_AND_RESET(structure);
3dca2276
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{
16ca5ff0 1560 struct bt_ctf_field_type_common_variant *var_ft = (void *) type;
312c056a
PP
1561 struct bt_ctf_field_variant *variant = g_new0(
1562 struct bt_ctf_field_variant, 1);
3dca2276
PP
1563
1564 BT_LOGD("Creating CTF writer variant field object: ft-addr=%p", type);
1565
312c056a 1566 if (!variant) {
3dca2276 1567 BT_LOGE_STR("Failed to allocate one variant field.");
312c056a 1568 goto end;
3dca2276
PP
1569 }
1570
16ca5ff0 1571 bt_ctf_field_common_variant_initialize(BT_CTF_TO_COMMON(BT_CTF_TO_COMMON(variant)),
312c056a 1572 (void *) type,
e1e02a22 1573 true, (bt_ctf_object_release_func)
312c056a
PP
1574 bt_ctf_field_variant_destroy_recursive,
1575 &bt_ctf_field_variant_methods,
16ca5ff0 1576 (bt_ctf_field_common_create_func) bt_ctf_field_create,
e1e02a22 1577 (GDestroyNotify) bt_ctf_object_put_ref);
312c056a 1578 variant->tag = (void *) bt_ctf_field_create(
16ca5ff0 1579 BT_CTF_FROM_COMMON(var_ft->tag_ft));
312c056a
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:
3dca2276
PP
1586 return (void *) variant;
1587}
1588
1589static
1590struct bt_ctf_field *bt_ctf_field_array_create(struct bt_ctf_field_type *type)
1591{
16ca5ff0
PP
1592 struct bt_ctf_field_common_array *array =
1593 g_new0(struct bt_ctf_field_common_array, 1);
3dca2276
PP
1594 int ret;
1595
1596 BT_LOGD("Creating CTF writer array field object: ft-addr=%p", type);
98b15851 1597 BT_ASSERT_DBG(type);
3dca2276
PP
1598
1599 if (!array) {
1600 BT_LOGE_STR("Failed to allocate one array field.");
1601 goto end;
1602 }
1603
16ca5ff0 1604 ret = bt_ctf_field_common_array_initialize(BT_CTF_TO_COMMON(array),
3dca2276 1605 (void *) type,
e1e02a22 1606 true, (bt_ctf_object_release_func)
312c056a
PP
1607 bt_ctf_field_array_destroy_recursive,
1608 &bt_ctf_field_array_methods,
16ca5ff0 1609 (bt_ctf_field_common_create_func) bt_ctf_field_create,
e1e02a22 1610 (GDestroyNotify) bt_ctf_object_put_ref);
3dca2276
PP
1611 array->common.spec.writer.serialize_func =
1612 (bt_ctf_field_serialize_recursive_func) bt_ctf_field_array_serialize_recursive;
1613 if (ret) {
e1e02a22 1614 BT_CTF_OBJECT_PUT_REF_AND_RESET(array);
3dca2276
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{
16ca5ff0
PP
1628 struct bt_ctf_field_common_sequence *sequence = g_new0(
1629 struct bt_ctf_field_common_sequence, 1);
3dca2276
PP
1630
1631 BT_LOGD("Creating CTF writer sequence field object: ft-addr=%p", type);
1632
1633 if (sequence) {
16ca5ff0 1634 bt_ctf_field_common_sequence_initialize(BT_CTF_TO_COMMON(sequence),
3dca2276 1635 (void *) type,
e1e02a22 1636 true, (bt_ctf_object_release_func)
312c056a
PP
1637 bt_ctf_field_sequence_destroy_recursive,
1638 &bt_ctf_field_sequence_methods,
e1e02a22 1639 (GDestroyNotify) bt_ctf_object_put_ref);
3dca2276
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{
16ca5ff0
PP
1654 struct bt_ctf_field_common_string *string = g_new0(
1655 struct bt_ctf_field_common_string, 1);
3dca2276
PP
1656
1657 BT_LOGD("Creating CTF writer string field object: ft-addr=%p", type);
1658
1659 if (string) {
16ca5ff0 1660 bt_ctf_field_common_string_initialize(BT_CTF_TO_COMMON(string),
3dca2276 1661 (void *) type,
e1e02a22 1662 true, (bt_ctf_object_release_func)
312c056a 1663 bt_ctf_field_string_destroy,
3dca2276
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}
312c056a
PP
1675
1676static
1677void bt_ctf_field_enumeration_set_is_frozen_recursive(
16ca5ff0 1678 struct bt_ctf_field_common *field, bool is_frozen)
312c056a
PP
1679{
1680 struct bt_ctf_field_enumeration *enumeration = (void *) field;
1681
1682 if (enumeration->container) {
16ca5ff0 1683 bt_ctf_field_common_set_is_frozen_recursive(
312c056a
PP
1684 (void *) enumeration->container, is_frozen);
1685 }
1686
16ca5ff0 1687 bt_ctf_field_common_generic_set_is_frozen((void *) field, is_frozen);
312c056a
PP
1688}
1689
1690static
16ca5ff0 1691int bt_ctf_field_enumeration_validate_recursive(struct bt_ctf_field_common *field)
312c056a
PP
1692{
1693 int ret = -1;
1694 struct bt_ctf_field_enumeration *enumeration = (void *) field;
1695
1696 if (enumeration->container) {
16ca5ff0 1697 ret = bt_ctf_field_common_validate_recursive(
312c056a
PP
1698 (void *) enumeration->container);
1699 }
1700
1701 return ret;
1702}
1703
1704static
00409097 1705bt_ctf_bool bt_ctf_field_enumeration_is_set_recursive(struct bt_ctf_field_common *field)
312c056a 1706{
00409097 1707 bt_ctf_bool is_set = BT_CTF_FALSE;
312c056a
PP
1708 struct bt_ctf_field_enumeration *enumeration = (void *) field;
1709
1710 if (enumeration->container) {
16ca5ff0 1711 is_set = bt_ctf_field_common_is_set_recursive(
312c056a
PP
1712 (void *) enumeration->container);
1713 }
1714
1715 return is_set;
1716}
1717
1718static
16ca5ff0 1719void bt_ctf_field_enumeration_reset_recursive(struct bt_ctf_field_common *field)
312c056a
PP
1720{
1721 struct bt_ctf_field_enumeration *enumeration = (void *) field;
1722
1723 if (enumeration->container) {
16ca5ff0 1724 bt_ctf_field_common_reset_recursive(
312c056a
PP
1725 (void *) enumeration->container);
1726 }
1727
16ca5ff0 1728 bt_ctf_field_common_generic_reset((void *) field);
312c056a
PP
1729}
1730
1731static
1732void bt_ctf_field_variant_set_is_frozen_recursive(
16ca5ff0 1733 struct bt_ctf_field_common *field, bool is_frozen)
312c056a
PP
1734{
1735 struct bt_ctf_field_variant *variant = (void *) field;
1736
1737 if (variant->tag) {
16ca5ff0 1738 bt_ctf_field_common_set_is_frozen_recursive(
312c056a
PP
1739 (void *) variant->tag, is_frozen);
1740 }
1741
16ca5ff0 1742 bt_ctf_field_common_variant_set_is_frozen_recursive((void *) field,
312c056a
PP
1743 is_frozen);
1744}
1745
1746static
16ca5ff0 1747int bt_ctf_field_variant_validate_recursive(struct bt_ctf_field_common *field)
312c056a
PP
1748{
1749 int ret;
1750 struct bt_ctf_field_variant *variant = (void *) field;
1751
1752 if (variant->tag) {
16ca5ff0 1753 ret = bt_ctf_field_common_validate_recursive(
312c056a
PP
1754 (void *) variant->tag);
1755 if (ret) {
1756 goto end;
1757 }
1758 }
1759
16ca5ff0 1760 ret = bt_ctf_field_common_variant_validate_recursive((void *) field);
312c056a
PP
1761
1762end:
1763 return ret;
1764}
1765
1766static
00409097 1767bt_ctf_bool bt_ctf_field_variant_is_set_recursive(struct bt_ctf_field_common *field)
312c056a 1768{
00409097 1769 bt_ctf_bool is_set;
312c056a
PP
1770 struct bt_ctf_field_variant *variant = (void *) field;
1771
1772 if (variant->tag) {
16ca5ff0 1773 is_set = bt_ctf_field_common_is_set_recursive(
312c056a
PP
1774 (void *) variant->tag);
1775 if (is_set) {
1776 goto end;
1777 }
1778 }
1779
16ca5ff0 1780 is_set = bt_ctf_field_common_variant_is_set_recursive((void *) field);
312c056a
PP
1781
1782end:
1783 return is_set;
1784}
1785
1786static
16ca5ff0 1787void bt_ctf_field_variant_reset_recursive(struct bt_ctf_field_common *field)
312c056a
PP
1788{
1789 struct bt_ctf_field_variant *variant = (void *) field;
1790
1791 if (variant->tag) {
16ca5ff0 1792 bt_ctf_field_common_reset_recursive(
312c056a
PP
1793 (void *) variant->tag);
1794 }
1795
16ca5ff0 1796 bt_ctf_field_common_variant_reset_recursive((void *) field);
312c056a
PP
1797}
1798
67d2ce02 1799BT_CTF_ASSERT_PRE_FUNC
312c056a 1800static inline bool field_to_set_has_expected_type(
16ca5ff0
PP
1801 struct bt_ctf_field_common *struct_field,
1802 const char *name, struct bt_ctf_field_common *value)
312c056a
PP
1803{
1804 bool ret = true;
16ca5ff0 1805 struct bt_ctf_field_type_common *expected_field_type = NULL;
312c056a
PP
1806
1807 expected_field_type =
16ca5ff0 1808 bt_ctf_field_type_common_structure_borrow_field_type_by_name(
312c056a
PP
1809 struct_field->type, name);
1810
16ca5ff0 1811 if (bt_ctf_field_type_common_compare(expected_field_type, value->type)) {
67d2ce02 1812 BT_CTF_ASSERT_PRE_MSG("Value field's type is different from the expected field type: "
16ca5ff0 1813 "value-ft-addr=%p, expected-ft-addr=%p", value->type,
312c056a
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;
16ca5ff0
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;
312c056a
PP
1833 size_t index;
1834 GHashTable *field_name_to_index;
16ca5ff0 1835 struct bt_ctf_field_type_common_structure *structure_ft;
312c056a 1836
67d2ce02
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,
16ca5ff0 1841 BT_CTF_FIELD_TYPE_ID_STRUCT, "Parent field");
67d2ce02 1842 BT_CTF_ASSERT_PRE(field_to_set_has_expected_type(common_field,
312c056a
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);
16ca5ff0 1846 structure_ft = BT_CTF_FROM_COMMON(common_field->type);
312c056a
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)) {
ef267d12 1851 BT_LOGT("Invalid parameter: no such field in structure field's type: "
312c056a
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 }
e1e02a22
PP
1858 bt_ctf_object_get_ref(value);
1859 BT_CTF_OBJECT_MOVE_REF(structure->fields->pdata[index], value);
312c056a
PP
1860
1861end:
1862 return ret;
1863}
This page took 0.13733 seconds and 4 git commands to generate.