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