Fix: Remove overly-strict freeze check on variants and structures
[babeltrace.git] / formats / ctf / ir / event-types.c
CommitLineData
273b65be
JG
1/*
2 * event-types.c
3 *
d2dc44b6 4 * Babeltrace CTF IR - Event Types
273b65be 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#include <babeltrace/ctf-writer/event-types.h>
adc315b8 30#include <babeltrace/ctf-ir/event-types-internal.h>
654c1444 31#include <babeltrace/ctf-ir/utils.h>
6cfb906f 32#include <babeltrace/ctf-ir/clock.h>
273b65be
JG
33#include <babeltrace/ctf-writer/writer-internal.h>
34#include <babeltrace/compiler.h>
35#include <babeltrace/endian.h>
36#include <float.h>
37#include <inttypes.h>
a39fa057 38#include <stdlib.h>
273b65be
JG
39
40struct range_overlap_query {
b92ddaaa
JG
41 union {
42 uint64_t _unsigned;
43 int64_t _signed;
44 } range_start;
45
46 union {
47 uint64_t _unsigned;
48 int64_t _signed;
49 } range_end;
273b65be
JG
50 int overlaps;
51 GQuark mapping_name;
52};
53
2f2d8e05
JG
54static
55void bt_ctf_field_type_destroy(struct bt_ctf_ref *);
273b65be
JG
56static
57void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *);
58static
59void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *);
60static
61void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *);
62static
63void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *);
64static
65void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *);
66static
67void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *);
68static
69void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *);
70static
71void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *);
72
73static
74void (* const type_destroy_funcs[])(struct bt_ctf_ref *) = {
75 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_destroy,
76 [CTF_TYPE_ENUM] =
77 bt_ctf_field_type_enumeration_destroy,
78 [CTF_TYPE_FLOAT] =
79 bt_ctf_field_type_floating_point_destroy,
80 [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_destroy,
81 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_destroy,
82 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_destroy,
83 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_destroy,
84 [CTF_TYPE_STRING] = bt_ctf_field_type_string_destroy,
85};
86
87static
88void generic_field_type_freeze(struct bt_ctf_field_type *);
89static
90void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *);
91static
92void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *);
93static
94void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *);
95static
96void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *);
97static
98void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *);
99
100static
101type_freeze_func const type_freeze_funcs[] = {
102 [CTF_TYPE_INTEGER] = generic_field_type_freeze,
103 [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_freeze,
104 [CTF_TYPE_FLOAT] = generic_field_type_freeze,
105 [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_freeze,
106 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_freeze,
107 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_freeze,
108 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_freeze,
109 [CTF_TYPE_STRING] = generic_field_type_freeze,
110};
111
112static
113int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *,
114 struct metadata_context *);
115static
116int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *,
117 struct metadata_context *);
118static
119int bt_ctf_field_type_floating_point_serialize(
120 struct bt_ctf_field_type *, struct metadata_context *);
121static
122int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *,
123 struct metadata_context *);
124static
125int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *,
126 struct metadata_context *);
127static
128int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *,
129 struct metadata_context *);
130static
131int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *,
132 struct metadata_context *);
133static
134int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *,
135 struct metadata_context *);
136
137static
138type_serialize_func const type_serialize_funcs[] = {
139 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_serialize,
140 [CTF_TYPE_ENUM] =
141 bt_ctf_field_type_enumeration_serialize,
142 [CTF_TYPE_FLOAT] =
143 bt_ctf_field_type_floating_point_serialize,
144 [CTF_TYPE_STRUCT] =
145 bt_ctf_field_type_structure_serialize,
146 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_serialize,
147 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_serialize,
148 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_serialize,
149 [CTF_TYPE_STRING] = bt_ctf_field_type_string_serialize,
150};
151
152static
153void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *,
c35a1669
JG
154 int byte_order, int set_native);
155static
156void bt_ctf_field_type_enumeration_set_byte_order(struct bt_ctf_field_type *,
157 int byte_order, int set_native);
273b65be
JG
158static
159void bt_ctf_field_type_floating_point_set_byte_order(
c35a1669
JG
160 struct bt_ctf_field_type *, int byte_order, int set_native);
161static
162void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *,
163 int byte_order, int set_native);
164static
165void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *,
166 int byte_order, int set_native);
167static
168void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *,
169 int byte_order, int set_native);
170static
171void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *,
172 int byte_order, int set_native);
273b65be 173
c35a1669 174/* The set_native flag only set the byte order if it is set to native */
273b65be
JG
175static
176void (* const set_byte_order_funcs[])(struct bt_ctf_field_type *,
c35a1669
JG
177 int byte_order, int set_native) = {
178 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_set_byte_order,
179 [CTF_TYPE_ENUM] =
180 bt_ctf_field_type_enumeration_set_byte_order,
273b65be
JG
181 [CTF_TYPE_FLOAT] =
182 bt_ctf_field_type_floating_point_set_byte_order,
c35a1669
JG
183 [CTF_TYPE_STRUCT] =
184 bt_ctf_field_type_structure_set_byte_order,
185 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_set_byte_order,
186 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_set_byte_order,
187 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_set_byte_order,
188 [CTF_TYPE_STRING] = NULL,
273b65be
JG
189};
190
24724933
JG
191static
192struct bt_ctf_field_type *bt_ctf_field_type_integer_copy(
193 struct bt_ctf_field_type *);
194static
195struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy(
196 struct bt_ctf_field_type *);
197static
198struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy(
199 struct bt_ctf_field_type *);
200static
201struct bt_ctf_field_type *bt_ctf_field_type_structure_copy(
202 struct bt_ctf_field_type *);
203static
204struct bt_ctf_field_type *bt_ctf_field_type_variant_copy(
205 struct bt_ctf_field_type *);
206static
207struct bt_ctf_field_type *bt_ctf_field_type_array_copy(
208 struct bt_ctf_field_type *);
209static
210struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy(
211 struct bt_ctf_field_type *);
212static
213struct bt_ctf_field_type *bt_ctf_field_type_string_copy(
214 struct bt_ctf_field_type *);
215
216static
217struct bt_ctf_field_type *(* const type_copy_funcs[])(
218 struct bt_ctf_field_type *) = {
219 [CTF_TYPE_INTEGER] = bt_ctf_field_type_integer_copy,
220 [CTF_TYPE_ENUM] = bt_ctf_field_type_enumeration_copy,
221 [CTF_TYPE_FLOAT] = bt_ctf_field_type_floating_point_copy,
222 [CTF_TYPE_STRUCT] = bt_ctf_field_type_structure_copy,
223 [CTF_TYPE_VARIANT] = bt_ctf_field_type_variant_copy,
224 [CTF_TYPE_ARRAY] = bt_ctf_field_type_array_copy,
225 [CTF_TYPE_SEQUENCE] = bt_ctf_field_type_sequence_copy,
226 [CTF_TYPE_STRING] = bt_ctf_field_type_string_copy,
227};
228
273b65be
JG
229static
230void destroy_enumeration_mapping(struct enumeration_mapping *mapping)
231{
232 g_free(mapping);
233}
234
235static
236void destroy_structure_field(struct structure_field *field)
237{
238 if (field->type) {
239 bt_ctf_field_type_put(field->type);
240 }
241
242 g_free(field);
243}
244
245static
246void check_ranges_overlap(gpointer element, gpointer query)
247{
248 struct enumeration_mapping *mapping = element;
249 struct range_overlap_query *overlap_query = query;
250
b92ddaaa
JG
251 if (mapping->range_start._signed <= overlap_query->range_end._signed
252 && overlap_query->range_start._signed <=
253 mapping->range_end._signed) {
254 overlap_query->overlaps = 1;
255 overlap_query->mapping_name = mapping->string;
256 }
257
258 overlap_query->overlaps |=
259 mapping->string == overlap_query->mapping_name;
260}
261
262static
263void check_ranges_overlap_unsigned(gpointer element, gpointer query)
264{
265 struct enumeration_mapping *mapping = element;
266 struct range_overlap_query *overlap_query = query;
267
268 if (mapping->range_start._unsigned <= overlap_query->range_end._unsigned
269 && overlap_query->range_start._unsigned <=
270 mapping->range_end._unsigned) {
273b65be
JG
271 overlap_query->overlaps = 1;
272 overlap_query->mapping_name = mapping->string;
273 }
274
275 overlap_query->overlaps |=
276 mapping->string == overlap_query->mapping_name;
277}
278
b92ddaaa
JG
279static
280gint compare_enumeration_mappings_signed(struct enumeration_mapping **a,
281 struct enumeration_mapping **b)
282{
283 return ((*a)->range_start._signed < (*b)->range_start._signed) ? -1 : 1;
284}
285
286static
287gint compare_enumeration_mappings_unsigned(struct enumeration_mapping **a,
288 struct enumeration_mapping **b)
289{
290 return ((*a)->range_start._unsigned < (*b)->range_start._unsigned) ? -1 : 1;
291}
292
273b65be
JG
293static
294void bt_ctf_field_type_init(struct bt_ctf_field_type *type)
295{
296 enum ctf_type_id type_id = type->declaration->id;
c4e511df 297 int ret;
273b65be
JG
298
299 assert(type && (type_id > CTF_TYPE_UNKNOWN) &&
300 (type_id < NR_CTF_TYPES));
301
302 bt_ctf_ref_init(&type->ref_count);
303 type->freeze = type_freeze_funcs[type_id];
304 type->serialize = type_serialize_funcs[type_id];
c4e511df
MD
305 ret = bt_ctf_field_type_set_byte_order(type, BT_CTF_BYTE_ORDER_NATIVE);
306 assert(!ret);
273b65be
JG
307 type->declaration->alignment = 1;
308}
309
310static
311int add_structure_field(GPtrArray *fields,
312 GHashTable *field_name_to_index,
313 struct bt_ctf_field_type *field_type,
314 const char *field_name)
315{
316 int ret = 0;
317 GQuark name_quark = g_quark_from_string(field_name);
318 struct structure_field *field;
319
320 /* Make sure structure does not contain a field of the same name */
fe0fe95c
JG
321 if (g_hash_table_lookup_extended(field_name_to_index,
322 GUINT_TO_POINTER(name_quark), NULL, NULL)) {
273b65be
JG
323 ret = -1;
324 goto end;
325 }
326
327 field = g_new0(struct structure_field, 1);
328 if (!field) {
329 ret = -1;
330 goto end;
331 }
332
333 bt_ctf_field_type_get(field_type);
334 field->name = name_quark;
335 field->type = field_type;
336 g_hash_table_insert(field_name_to_index,
337 (gpointer) (unsigned long) name_quark,
338 (gpointer) (unsigned long) fields->len);
339 g_ptr_array_add(fields, field);
273b65be
JG
340end:
341 return ret;
342}
343
2f2d8e05
JG
344static
345void bt_ctf_field_type_destroy(struct bt_ctf_ref *ref)
346{
347 struct bt_ctf_field_type *type;
348 enum ctf_type_id type_id;
349
350 if (!ref) {
351 return;
352 }
353
354 type = container_of(ref, struct bt_ctf_field_type, ref_count);
355 type_id = type->declaration->id;
356 if (type_id <= CTF_TYPE_UNKNOWN ||
357 type_id >= NR_CTF_TYPES) {
358 return;
359 }
360
2f2d8e05
JG
361 type_destroy_funcs[type_id](ref);
362}
363
9ce21c30
JG
364BT_HIDDEN
365int bt_ctf_field_type_validate(struct bt_ctf_field_type *type)
366{
367 int ret = 0;
368
369 if (!type) {
370 ret = -1;
371 goto end;
372 }
373
5d161ecc
JG
374 switch (type->declaration->id) {
375 case CTF_TYPE_ENUM:
376 {
9ce21c30
JG
377 struct bt_ctf_field_type_enumeration *enumeration =
378 container_of(type, struct bt_ctf_field_type_enumeration,
379 parent);
380
5d161ecc 381 /* Ensure enum has entries */
9ce21c30 382 ret = enumeration->entries->len ? 0 : -1;
5d161ecc
JG
383 break;
384 }
c6c0ca42
JG
385 case CTF_TYPE_SEQUENCE:
386 {
387 struct bt_ctf_field_type_sequence *sequence =
388 container_of(type, struct bt_ctf_field_type_sequence,
389 parent);
390
391 /* length field name should be set at this point */
392 ret = sequence->length_field_name->len ? 0 : -1;
393 break;
394 }
395 case CTF_TYPE_VARIANT:
396 {
397 struct bt_ctf_field_type_variant *variant =
398 container_of(type, struct bt_ctf_field_type_variant,
399 parent);
400
401 if (variant->tag_name->len == 0 || !variant->tag) {
402 ret = -1;
403 }
404 break;
405 }
5d161ecc
JG
406 default:
407 break;
9ce21c30
JG
408 }
409end:
410 return ret;
411}
412
273b65be
JG
413struct bt_ctf_field_type *bt_ctf_field_type_integer_create(unsigned int size)
414{
415 struct bt_ctf_field_type_integer *integer =
416 g_new0(struct bt_ctf_field_type_integer, 1);
417
1f02e293 418 if (!integer || size == 0 || size > 64) {
273b65be
JG
419 return NULL;
420 }
421
422 integer->parent.declaration = &integer->declaration.p;
423 integer->parent.declaration->id = CTF_TYPE_INTEGER;
424 integer->declaration.len = size;
425 integer->declaration.base = BT_CTF_INTEGER_BASE_DECIMAL;
426 integer->declaration.encoding = CTF_STRING_NONE;
427 bt_ctf_field_type_init(&integer->parent);
428 return &integer->parent;
429}
430
b92ddaaa
JG
431int bt_ctf_field_type_integer_get_size(struct bt_ctf_field_type *type)
432{
433 int ret = 0;
434 struct bt_ctf_field_type_integer *integer;
435
436 if (!type || type->declaration->id != CTF_TYPE_INTEGER) {
437 ret = -1;
438 goto end;
439 }
440
441 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
442 ret = (int) integer->declaration.len;
443end:
444 return ret;
445}
446
447int bt_ctf_field_type_integer_get_signed(struct bt_ctf_field_type *type)
448{
449 int ret = 0;
450 struct bt_ctf_field_type_integer *integer;
451
452 if (!type || type->declaration->id != CTF_TYPE_INTEGER) {
453 ret = -1;
454 goto end;
455 }
456
457 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
458 ret = integer->declaration.signedness;
459end:
460 return ret;
461}
462
273b65be
JG
463int bt_ctf_field_type_integer_set_signed(struct bt_ctf_field_type *type,
464 int is_signed)
465{
466 int ret = 0;
467 struct bt_ctf_field_type_integer *integer;
468
469 if (!type || type->frozen ||
470 type->declaration->id != CTF_TYPE_INTEGER) {
471 ret = -1;
472 goto end;
473 }
474
475 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
273b65be
JG
476 integer->declaration.signedness = !!is_signed;
477end:
478 return ret;
479}
480
b92ddaaa
JG
481enum bt_ctf_integer_base bt_ctf_field_type_integer_get_base(
482 struct bt_ctf_field_type *type)
483{
484 enum bt_ctf_integer_base ret = BT_CTF_INTEGER_BASE_UNKNOWN;
485 struct bt_ctf_field_type_integer *integer;
486
487 if (!type || type->declaration->id != CTF_TYPE_INTEGER) {
488 goto end;
489 }
490
491 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
492 ret = integer->declaration.base;
493end:
494 return ret;
495}
496
273b65be
JG
497int bt_ctf_field_type_integer_set_base(struct bt_ctf_field_type *type,
498 enum bt_ctf_integer_base base)
499{
500 int ret = 0;
501
502 if (!type || type->frozen ||
503 type->declaration->id != CTF_TYPE_INTEGER) {
504 ret = -1;
505 goto end;
506 }
507
508 switch (base) {
509 case BT_CTF_INTEGER_BASE_BINARY:
510 case BT_CTF_INTEGER_BASE_OCTAL:
511 case BT_CTF_INTEGER_BASE_DECIMAL:
512 case BT_CTF_INTEGER_BASE_HEXADECIMAL:
513 {
514 struct bt_ctf_field_type_integer *integer = container_of(type,
515 struct bt_ctf_field_type_integer, parent);
516 integer->declaration.base = base;
517 break;
518 }
519 default:
520 ret = -1;
521 }
522end:
523 return ret;
524}
525
b92ddaaa
JG
526enum ctf_string_encoding bt_ctf_field_type_integer_get_encoding(
527 struct bt_ctf_field_type *type)
528{
529 enum ctf_string_encoding ret = CTF_STRING_UNKNOWN;
530 struct bt_ctf_field_type_integer *integer;
531
532 if (!type || type->declaration->id != CTF_TYPE_INTEGER) {
533 goto end;
534 }
535
536 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
537 ret = integer->declaration.encoding;
538end:
539 return ret;
540}
541
273b65be
JG
542int bt_ctf_field_type_integer_set_encoding(struct bt_ctf_field_type *type,
543 enum ctf_string_encoding encoding)
544{
545 int ret = 0;
546 struct bt_ctf_field_type_integer *integer;
547
548 if (!type || type->frozen ||
549 (type->declaration->id != CTF_TYPE_INTEGER) ||
550 (encoding < CTF_STRING_NONE) ||
551 (encoding >= CTF_STRING_UNKNOWN)) {
552 ret = -1;
553 goto end;
554 }
555
556 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
557 integer->declaration.encoding = encoding;
558end:
559 return ret;
560}
561
6cfb906f
JG
562struct bt_ctf_clock *bt_ctf_field_type_integer_get_mapped_clock(
563 struct bt_ctf_field_type *type)
564{
565 struct bt_ctf_field_type_integer *integer;
566 struct bt_ctf_clock *clock = NULL;
567
568 if (!type) {
569 goto end;
570 }
571
572 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
573 clock = integer->mapped_clock;
574 if (clock) {
575 bt_ctf_clock_get(clock);
576 }
577end:
578 return clock;
579}
580
581int bt_ctf_field_type_integer_set_mapped_clock(
582 struct bt_ctf_field_type *type,
583 struct bt_ctf_clock *clock)
584{
585 struct bt_ctf_field_type_integer *integer;
586 int ret = 0;
587
588 if (!type || type->frozen) {
589 ret = -1;
590 goto end;
591 }
592
593 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
594 if (integer->mapped_clock) {
595 bt_ctf_clock_put(integer->mapped_clock);
596 }
597
598 if (clock) {
599 bt_ctf_clock_get(clock);
600 }
601
602 integer->mapped_clock = clock;
603end:
604 return ret;
605}
606
273b65be
JG
607struct bt_ctf_field_type *bt_ctf_field_type_enumeration_create(
608 struct bt_ctf_field_type *integer_container_type)
609{
610 struct bt_ctf_field_type_enumeration *enumeration = NULL;
611
612 if (!integer_container_type) {
613 goto error;
614 }
615
2a610bb7
JG
616 if (integer_container_type->declaration->id != CTF_TYPE_INTEGER) {
617 goto error;
618 }
619
273b65be
JG
620 enumeration = g_new0(struct bt_ctf_field_type_enumeration, 1);
621 if (!enumeration) {
622 goto error;
623 }
624
625 enumeration->parent.declaration = &enumeration->declaration.p;
626 enumeration->parent.declaration->id = CTF_TYPE_ENUM;
627 bt_ctf_field_type_get(integer_container_type);
628 enumeration->container = integer_container_type;
629 enumeration->entries = g_ptr_array_new_with_free_func(
630 (GDestroyNotify)destroy_enumeration_mapping);
631 bt_ctf_field_type_init(&enumeration->parent);
632 return &enumeration->parent;
633error:
634 g_free(enumeration);
635 return NULL;
636}
637
b92ddaaa
JG
638struct bt_ctf_field_type *bt_ctf_field_type_enumeration_get_container_type(
639 struct bt_ctf_field_type *type)
640{
641 struct bt_ctf_field_type *container_type = NULL;
642 struct bt_ctf_field_type_enumeration *enumeration_type;
643
644 if (!type) {
645 goto end;
646 }
647
648 if (type->declaration->id != CTF_TYPE_ENUM) {
649 goto end;
650 }
651
652 enumeration_type = container_of(type,
653 struct bt_ctf_field_type_enumeration, parent);
654 container_type = enumeration_type->container;
655 bt_ctf_field_type_get(container_type);
656end:
657 return container_type;
658}
659
273b65be
JG
660int bt_ctf_field_type_enumeration_add_mapping(
661 struct bt_ctf_field_type *type, const char *string,
662 int64_t range_start, int64_t range_end)
663{
664 int ret = 0;
665 GQuark mapping_name;
666 struct enumeration_mapping *mapping;
667 struct bt_ctf_field_type_enumeration *enumeration;
668 struct range_overlap_query query;
a39fa057 669 char *escaped_string;
273b65be
JG
670
671 if (!type || (type->declaration->id != CTF_TYPE_ENUM) ||
672 type->frozen ||
673 (range_end < range_start)) {
674 ret = -1;
675 goto end;
676 }
677
a39fa057 678 if (!string || strlen(string) == 0) {
273b65be
JG
679 ret = -1;
680 goto end;
681 }
682
a39fa057
JG
683 escaped_string = g_strescape(string, NULL);
684 if (!escaped_string) {
685 ret = -1;
686 goto end;
687 }
688
689 mapping_name = g_quark_from_string(escaped_string);
b92ddaaa
JG
690 query = (struct range_overlap_query) {
691 .range_start._signed = range_start,
692 .range_end._signed = range_end,
273b65be
JG
693 .mapping_name = mapping_name,
694 .overlaps = 0 };
695 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
696 parent);
697
698 /* Check that the range does not overlap with one already present */
699 g_ptr_array_foreach(enumeration->entries, check_ranges_overlap, &query);
700 if (query.overlaps) {
701 ret = -1;
a39fa057 702 goto error_free;
273b65be
JG
703 }
704
705 mapping = g_new(struct enumeration_mapping, 1);
706 if (!mapping) {
707 ret = -1;
a39fa057 708 goto error_free;
273b65be
JG
709 }
710
b92ddaaa
JG
711 *mapping = (struct enumeration_mapping) {
712 .range_start._signed = range_start,
713 .range_end._signed = range_end, .string = mapping_name};
273b65be 714 g_ptr_array_add(enumeration->entries, mapping);
b92ddaaa
JG
715 g_ptr_array_sort(enumeration->entries,
716 (GCompareFunc)compare_enumeration_mappings_signed);
717error_free:
718 free(escaped_string);
719end:
720 return ret;
721}
722
723int bt_ctf_field_type_enumeration_add_mapping_unsigned(
724 struct bt_ctf_field_type *type, const char *string,
725 uint64_t range_start, uint64_t range_end)
726{
727 int ret = 0;
728 GQuark mapping_name;
729 struct enumeration_mapping *mapping;
730 struct bt_ctf_field_type_enumeration *enumeration;
731 struct range_overlap_query query;
732 char *escaped_string;
733
734 if (!type || (type->declaration->id != CTF_TYPE_ENUM) ||
735 type->frozen ||
736 (range_end < range_start)) {
737 ret = -1;
738 goto end;
739 }
740
741 if (!string || strlen(string) == 0) {
742 ret = -1;
743 goto end;
744 }
745
746 escaped_string = g_strescape(string, NULL);
747 if (!escaped_string) {
748 ret = -1;
749 goto end;
750 }
751
752 mapping_name = g_quark_from_string(escaped_string);
753 query = (struct range_overlap_query) {
754 .range_start._unsigned = range_start,
755 .range_end._unsigned = range_end,
756 .mapping_name = mapping_name,
757 .overlaps = 0 };
758 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
759 parent);
760
761 /* Check that the range does not overlap with one already present */
762 g_ptr_array_foreach(enumeration->entries, check_ranges_overlap_unsigned,
763 &query);
764 if (query.overlaps) {
765 ret = -1;
766 goto error_free;
767 }
768
769 mapping = g_new(struct enumeration_mapping, 1);
770 if (!mapping) {
771 ret = -1;
772 goto error_free;
773 }
774
775 *mapping = (struct enumeration_mapping) {
776 .range_start._unsigned = range_start,
777 .range_end._unsigned = range_end, .string = mapping_name};
778 g_ptr_array_add(enumeration->entries, mapping);
779 g_ptr_array_sort(enumeration->entries,
780 (GCompareFunc)compare_enumeration_mappings_unsigned);
a39fa057
JG
781error_free:
782 free(escaped_string);
273b65be
JG
783end:
784 return ret;
785}
786
e5958c30
JG
787const char *bt_ctf_field_type_enumeration_get_mapping_name_unsigned(
788 struct bt_ctf_field_type_enumeration *enumeration_type,
789 uint64_t value)
790{
791 const char *name = NULL;
792 struct range_overlap_query query =
793 (struct range_overlap_query) {
b92ddaaa
JG
794 .range_start._unsigned = value,
795 .range_end._unsigned = value,
e5958c30
JG
796 .overlaps = 0 };
797
b92ddaaa
JG
798 g_ptr_array_foreach(enumeration_type->entries,
799 check_ranges_overlap_unsigned,
e5958c30
JG
800 &query);
801 if (!query.overlaps) {
802 goto end;
803 }
804
805 name = g_quark_to_string(query.mapping_name);
806end:
807 return name;
808}
809
810const char *bt_ctf_field_type_enumeration_get_mapping_name_signed(
811 struct bt_ctf_field_type_enumeration *enumeration_type,
812 int64_t value)
813{
814 const char *name = NULL;
815 struct range_overlap_query query =
816 (struct range_overlap_query) {
b92ddaaa
JG
817 .range_start._signed = value,
818 .range_end._signed = value,
e5958c30
JG
819 .overlaps = 0 };
820
821 g_ptr_array_foreach(enumeration_type->entries, check_ranges_overlap,
822 &query);
823 if (!query.overlaps) {
824 goto end;
825 }
826
827 name = g_quark_to_string(query.mapping_name);
828end:
829 return name;
830}
831
074ee56d 832int bt_ctf_field_type_enumeration_get_mapping_count(
b92ddaaa
JG
833 struct bt_ctf_field_type *type)
834{
074ee56d 835 int ret = 0;
b92ddaaa
JG
836 struct bt_ctf_field_type_enumeration *enumeration;
837
838 if (!type || (type->declaration->id != CTF_TYPE_ENUM)) {
839 ret = -1;
840 goto end;
841 }
842
843 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
844 parent);
074ee56d 845 ret = (int) enumeration->entries->len;
b92ddaaa
JG
846end:
847 return ret;
848}
849
850static inline
851struct enumeration_mapping *get_enumeration_mapping(
074ee56d 852 struct bt_ctf_field_type *type, int index)
b92ddaaa
JG
853{
854 struct enumeration_mapping *mapping = NULL;
855 struct bt_ctf_field_type_enumeration *enumeration;
856
857 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
858 parent);
859 if (index >= enumeration->entries->len) {
860 goto end;
861 }
862
863 mapping = g_ptr_array_index(enumeration->entries, index);
864end:
865 return mapping;
866}
867
868int bt_ctf_field_type_enumeration_get_mapping(
074ee56d 869 struct bt_ctf_field_type *type, int index,
b92ddaaa
JG
870 const char **string, int64_t *range_start, int64_t *range_end)
871{
872 struct enumeration_mapping *mapping;
873 int ret = 0;
874
074ee56d 875 if (!type || index < 0 || !string || !range_start || !range_end ||
b92ddaaa
JG
876 (type->declaration->id != CTF_TYPE_ENUM)) {
877 ret = -1;
878 goto end;
879 }
880
881 mapping = get_enumeration_mapping(type, index);
882 if (!mapping) {
883 ret = -1;
884 goto end;
885 }
886
887 *string = g_quark_to_string(mapping->string);
888 *range_start = mapping->range_start._signed;
889 *range_end = mapping->range_end._signed;
890end:
891 return ret;
892}
893
894int bt_ctf_field_type_enumeration_get_mapping_unsigned(
074ee56d 895 struct bt_ctf_field_type *type, int index,
b92ddaaa
JG
896 const char **string, uint64_t *range_start, uint64_t *range_end)
897{
898 struct enumeration_mapping *mapping;
899 int ret = 0;
900
074ee56d 901 if (!type || index < 0 || !string || !range_start || !range_end ||
b92ddaaa
JG
902 (type->declaration->id != CTF_TYPE_ENUM)) {
903 ret = -1;
904 goto end;
905 }
906
907 mapping = get_enumeration_mapping(type, index);
908 if (!mapping) {
909 ret = -1;
910 goto end;
911 }
912
913 *string = g_quark_to_string(mapping->string);
914 *range_start = mapping->range_start._unsigned;
915 *range_end = mapping->range_end._unsigned;
916end:
917 return ret;
918}
919
920int bt_ctf_field_type_enumeration_get_mapping_index_by_name(
074ee56d 921 struct bt_ctf_field_type *type, const char *name)
b92ddaaa 922{
b92ddaaa
JG
923 GQuark name_quark;
924 struct bt_ctf_field_type_enumeration *enumeration;
074ee56d 925 int i, ret = 0;
b92ddaaa 926
074ee56d 927 if (!type || !name ||
b92ddaaa
JG
928 (type->declaration->id != CTF_TYPE_ENUM)) {
929 ret = -1;
930 goto end;
931 }
932
933 name_quark = g_quark_try_string(name);
934 if (!name_quark) {
935 ret = -1;
936 goto end;
937 }
938
939 enumeration = container_of(type,
940 struct bt_ctf_field_type_enumeration, parent);
941 for (i = 0; i < enumeration->entries->len; i++) {
942 struct enumeration_mapping *mapping =
943 get_enumeration_mapping(type, i);
944
945 if (mapping->string == name_quark) {
074ee56d 946 ret = i;
b92ddaaa
JG
947 goto end;
948 }
949 }
950
951 ret = -1;
952end:
953 return ret;
954}
955
956int bt_ctf_field_type_enumeration_get_mapping_index_by_value(
074ee56d 957 struct bt_ctf_field_type *type, int64_t value)
b92ddaaa
JG
958{
959 struct bt_ctf_field_type_enumeration *enumeration;
074ee56d 960 int i, ret = 0;
b92ddaaa 961
074ee56d 962 if (!type || (type->declaration->id != CTF_TYPE_ENUM)) {
b92ddaaa
JG
963 ret = -1;
964 goto end;
965 }
966
967 enumeration = container_of(type,
968 struct bt_ctf_field_type_enumeration, parent);
969 for (i = 0; i < enumeration->entries->len; i++) {
970 struct enumeration_mapping *mapping =
971 get_enumeration_mapping(type, i);
972
973 if (value >= mapping->range_start._signed &&
974 value <= mapping->range_end._signed) {
074ee56d 975 ret = i;
b92ddaaa
JG
976 goto end;
977 }
978 }
979
980 ret = -1;
981end:
982 return ret;
983}
984
985int bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(
074ee56d 986 struct bt_ctf_field_type *type, uint64_t value)
b92ddaaa
JG
987{
988 struct bt_ctf_field_type_enumeration *enumeration;
074ee56d 989 int i, ret = 0;
b92ddaaa 990
074ee56d 991 if (!type || (type->declaration->id != CTF_TYPE_ENUM)) {
b92ddaaa
JG
992 ret = -1;
993 goto end;
994 }
995
996 enumeration = container_of(type,
997 struct bt_ctf_field_type_enumeration, parent);
998 for (i = 0; i < enumeration->entries->len; i++) {
999 struct enumeration_mapping *mapping =
1000 get_enumeration_mapping(type, i);
1001
1002 if (value >= mapping->range_start._unsigned &&
1003 value <= mapping->range_end._unsigned) {
074ee56d 1004 ret = i;
b92ddaaa
JG
1005 goto end;
1006 }
1007 }
1008
1009 ret = -1;
1010end:
1011 return ret;
1012}
1013
273b65be
JG
1014struct bt_ctf_field_type *bt_ctf_field_type_floating_point_create(void)
1015{
1016 struct bt_ctf_field_type_floating_point *floating_point =
1017 g_new0(struct bt_ctf_field_type_floating_point, 1);
1018
1019 if (!floating_point) {
1020 goto end;
1021 }
1022
1023 floating_point->declaration.sign = &floating_point->sign;
1024 floating_point->declaration.mantissa = &floating_point->mantissa;
1025 floating_point->declaration.exp = &floating_point->exp;
1026 floating_point->sign.len = 1;
1027 floating_point->parent.declaration = &floating_point->declaration.p;
1028 floating_point->parent.declaration->id = CTF_TYPE_FLOAT;
1029 floating_point->declaration.exp->len =
1030 sizeof(float) * CHAR_BIT - FLT_MANT_DIG;
1031 floating_point->declaration.mantissa->len = FLT_MANT_DIG - 1;
1032 floating_point->sign.p.alignment = 1;
1033 floating_point->mantissa.p.alignment = 1;
1034 floating_point->exp.p.alignment = 1;
1035
1036 bt_ctf_field_type_init(&floating_point->parent);
1037end:
1038 return floating_point ? &floating_point->parent : NULL;
1039}
1040
b92ddaaa
JG
1041int bt_ctf_field_type_floating_point_get_exponent_digits(
1042 struct bt_ctf_field_type *type)
1043{
1044 int ret = 0;
1045 struct bt_ctf_field_type_floating_point *floating_point;
1046
1047 if (!type || (type->declaration->id != CTF_TYPE_FLOAT)) {
1048 ret = -1;
1049 goto end;
1050 }
1051
1052 floating_point = container_of(type,
1053 struct bt_ctf_field_type_floating_point, parent);
1054 ret = (int) floating_point->declaration.exp->len;
1055end:
1056 return ret;
1057}
1058
273b65be
JG
1059int bt_ctf_field_type_floating_point_set_exponent_digits(
1060 struct bt_ctf_field_type *type,
1061 unsigned int exponent_digits)
1062{
1063 int ret = 0;
1064 struct bt_ctf_field_type_floating_point *floating_point;
1065
1066 if (!type || type->frozen ||
1067 (type->declaration->id != CTF_TYPE_FLOAT)) {
1068 ret = -1;
1069 goto end;
1070 }
1071
1072 floating_point = container_of(type,
1073 struct bt_ctf_field_type_floating_point, parent);
1074 if ((exponent_digits != sizeof(float) * CHAR_BIT - FLT_MANT_DIG) &&
1075 (exponent_digits != sizeof(double) * CHAR_BIT - DBL_MANT_DIG) &&
1076 (exponent_digits !=
1077 sizeof(long double) * CHAR_BIT - LDBL_MANT_DIG)) {
1078 ret = -1;
1079 goto end;
1080 }
1081
1082 floating_point->declaration.exp->len = exponent_digits;
1083end:
1084 return ret;
1085}
1086
b92ddaaa
JG
1087int bt_ctf_field_type_floating_point_get_mantissa_digits(
1088 struct bt_ctf_field_type *type)
1089{
1090 int ret = 0;
1091 struct bt_ctf_field_type_floating_point *floating_point;
1092
1093 if (!type || (type->declaration->id != CTF_TYPE_FLOAT)) {
1094 ret = -1;
1095 goto end;
1096 }
1097
1098 floating_point = container_of(type,
1099 struct bt_ctf_field_type_floating_point, parent);
1100 ret = (int) floating_point->mantissa.len + 1;
1101end:
1102 return ret;
1103}
1104
273b65be
JG
1105int bt_ctf_field_type_floating_point_set_mantissa_digits(
1106 struct bt_ctf_field_type *type,
1107 unsigned int mantissa_digits)
1108{
1109 int ret = 0;
1110 struct bt_ctf_field_type_floating_point *floating_point;
1111
1112 if (!type || type->frozen ||
1113 (type->declaration->id != CTF_TYPE_FLOAT)) {
1114 ret = -1;
1115 goto end;
1116 }
1117
1118 floating_point = container_of(type,
1119 struct bt_ctf_field_type_floating_point, parent);
1120
1121 if ((mantissa_digits != FLT_MANT_DIG) &&
1122 (mantissa_digits != DBL_MANT_DIG) &&
1123 (mantissa_digits != LDBL_MANT_DIG)) {
1124 ret = -1;
1125 goto end;
1126 }
1127
1128 floating_point->declaration.mantissa->len = mantissa_digits - 1;
1129end:
1130 return ret;
1131}
1132
1133struct bt_ctf_field_type *bt_ctf_field_type_structure_create(void)
1134{
1135 struct bt_ctf_field_type_structure *structure =
1136 g_new0(struct bt_ctf_field_type_structure, 1);
1137
1138 if (!structure) {
1139 goto error;
1140 }
1141
1142 structure->parent.declaration = &structure->declaration.p;
1143 structure->parent.declaration->id = CTF_TYPE_STRUCT;
273b65be
JG
1144 structure->fields = g_ptr_array_new_with_free_func(
1145 (GDestroyNotify)destroy_structure_field);
1146 structure->field_name_to_index = g_hash_table_new(NULL, NULL);
c35a1669 1147 bt_ctf_field_type_init(&structure->parent);
273b65be
JG
1148 return &structure->parent;
1149error:
1150 return NULL;
1151}
1152
1153int bt_ctf_field_type_structure_add_field(struct bt_ctf_field_type *type,
1154 struct bt_ctf_field_type *field_type,
1155 const char *field_name)
1156{
1157 int ret = 0;
1158 struct bt_ctf_field_type_structure *structure;
1159
1160 if (!type || !field_type || type->frozen ||
654c1444 1161 bt_ctf_validate_identifier(field_name) ||
9ce21c30
JG
1162 (type->declaration->id != CTF_TYPE_STRUCT) ||
1163 bt_ctf_field_type_validate(field_type)) {
e6235f1f 1164 ret = -1;
273b65be
JG
1165 goto end;
1166 }
1167
1168 structure = container_of(type,
1169 struct bt_ctf_field_type_structure, parent);
1170 if (add_structure_field(structure->fields,
1171 structure->field_name_to_index, field_type, field_name)) {
1172 ret = -1;
1173 goto end;
1174 }
b92ddaaa
JG
1175end:
1176 return ret;
1177}
1178
074ee56d 1179int bt_ctf_field_type_structure_get_field_count(
b92ddaaa
JG
1180 struct bt_ctf_field_type *type)
1181{
074ee56d 1182 int ret = 0;
b92ddaaa
JG
1183 struct bt_ctf_field_type_structure *structure;
1184
1185 if (!type || (type->declaration->id != CTF_TYPE_STRUCT)) {
1186 ret = -1;
1187 goto end;
1188 }
1189
1190 structure = container_of(type, struct bt_ctf_field_type_structure,
1191 parent);
074ee56d 1192 ret = (int) structure->fields->len;
b92ddaaa
JG
1193end:
1194 return ret;
1195}
1196
1197int bt_ctf_field_type_structure_get_field(struct bt_ctf_field_type *type,
1198 const char **field_name, struct bt_ctf_field_type **field_type,
074ee56d 1199 int index)
b92ddaaa
JG
1200{
1201 struct bt_ctf_field_type_structure *structure;
1202 struct structure_field *field;
1203 int ret = 0;
1204
f9b799fc 1205 if (!type || index < 0 || (type->declaration->id != CTF_TYPE_STRUCT)) {
b92ddaaa
JG
1206 ret = -1;
1207 goto end;
1208 }
1209
1210 structure = container_of(type, struct bt_ctf_field_type_structure,
1211 parent);
1212 if (index >= structure->fields->len) {
1213 ret = -1;
1214 goto end;
1215 }
1216
1217 field = g_ptr_array_index(structure->fields, index);
f9b799fc
JG
1218 if (field_type) {
1219 *field_type = field->type;
1220 bt_ctf_field_type_get(field->type);
1221 }
1222 if (field_name) {
1223 *field_name = g_quark_to_string(field->name);
1224 }
b92ddaaa
JG
1225end:
1226 return ret;
1227}
1228
1229struct bt_ctf_field_type *bt_ctf_field_type_structure_get_field_type_by_name(
1230 struct bt_ctf_field_type *type,
1231 const char *name)
1232{
1233 size_t index;
1234 GQuark name_quark;
1235 struct structure_field *field;
1236 struct bt_ctf_field_type_structure *structure;
1237 struct bt_ctf_field_type *field_type = NULL;
1238
1239 if (!type || !name) {
1240 goto end;
1241 }
1242
1243 name_quark = g_quark_try_string(name);
1244 if (!name_quark) {
1245 goto end;
1246 }
1247
1248 structure = container_of(type, struct bt_ctf_field_type_structure,
1249 parent);
1250 if (!g_hash_table_lookup_extended(structure->field_name_to_index,
1251 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
1252 goto end;
273b65be 1253 }
b92ddaaa
JG
1254
1255 field = structure->fields->pdata[index];
1256 field_type = field->type;
1257 bt_ctf_field_type_get(field_type);
273b65be 1258end:
b92ddaaa 1259 return field_type;
273b65be
JG
1260}
1261
1262struct bt_ctf_field_type *bt_ctf_field_type_variant_create(
1263 struct bt_ctf_field_type *enum_tag, const char *tag_name)
1264{
1265 struct bt_ctf_field_type_variant *variant = NULL;
1266
6964b7fd 1267 if (tag_name && bt_ctf_validate_identifier(tag_name)) {
273b65be
JG
1268 goto error;
1269 }
1270
1271 variant = g_new0(struct bt_ctf_field_type_variant, 1);
1272 if (!variant) {
1273 goto error;
1274 }
1275
1276 variant->parent.declaration = &variant->declaration.p;
1277 variant->parent.declaration->id = CTF_TYPE_VARIANT;
1278 variant->tag_name = g_string_new(tag_name);
273b65be
JG
1279 variant->field_name_to_index = g_hash_table_new(NULL, NULL);
1280 variant->fields = g_ptr_array_new_with_free_func(
1281 (GDestroyNotify)destroy_structure_field);
6964b7fd
JG
1282 if (enum_tag) {
1283 bt_ctf_field_type_get(enum_tag);
1284 variant->tag = container_of(enum_tag,
1285 struct bt_ctf_field_type_enumeration, parent);
1286 }
1287
c35a1669 1288 bt_ctf_field_type_init(&variant->parent);
46caf2cb
JG
1289 /* A variant's alignment is undefined */
1290 variant->parent.declaration->alignment = 0;
273b65be
JG
1291 return &variant->parent;
1292error:
1293 return NULL;
1294}
1295
b92ddaaa
JG
1296struct bt_ctf_field_type *bt_ctf_field_type_variant_get_tag_type(
1297 struct bt_ctf_field_type *type)
1298{
1299 struct bt_ctf_field_type_variant *variant;
1300 struct bt_ctf_field_type *tag_type = NULL;
1301
1302 if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) {
1303 goto end;
1304 }
1305
1306 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
6964b7fd
JG
1307 if (!variant->tag) {
1308 goto end;
1309 }
1310
b92ddaaa
JG
1311 tag_type = &variant->tag->parent;
1312 bt_ctf_field_type_get(tag_type);
1313end:
1314 return tag_type;
1315}
1316
1317const char *bt_ctf_field_type_variant_get_tag_name(
1318 struct bt_ctf_field_type *type)
1319{
1320 struct bt_ctf_field_type_variant *variant;
1321 const char *tag_name = NULL;
1322
1323 if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) {
1324 goto end;
1325 }
1326
1327 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
6964b7fd
JG
1328 if (variant->tag_name->len == 0) {
1329 goto end;
1330 }
1331
b92ddaaa
JG
1332 tag_name = variant->tag_name->str;
1333end:
1334 return tag_name;
1335}
1336
d9b1ab6d
JG
1337int bt_ctf_field_type_variant_set_tag_name(
1338 struct bt_ctf_field_type *type, const char *name)
1339{
1340 int ret = 0;
1341 struct bt_ctf_field_type_variant *variant;
1342
1343 if (!type || type->frozen ||
1344 (type->declaration->id != CTF_TYPE_VARIANT) ||
1345 bt_ctf_validate_identifier(name)) {
1346 ret = -1;
1347 goto end;
1348 }
1349
1350 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
1351 g_string_assign(variant->tag_name, name);
1352end:
1353 return ret;
1354}
1355
273b65be
JG
1356int bt_ctf_field_type_variant_add_field(struct bt_ctf_field_type *type,
1357 struct bt_ctf_field_type *field_type,
1358 const char *field_name)
1359{
1360 size_t i;
1361 int ret = 0;
273b65be
JG
1362 struct bt_ctf_field_type_variant *variant;
1363 GQuark field_name_quark = g_quark_from_string(field_name);
1364
1365 if (!type || !field_type || type->frozen ||
654c1444 1366 bt_ctf_validate_identifier(field_name) ||
9ce21c30
JG
1367 (type->declaration->id != CTF_TYPE_VARIANT) ||
1368 bt_ctf_field_type_validate(field_type)) {
273b65be
JG
1369 ret = -1;
1370 goto end;
1371 }
1372
1373 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
273b65be 1374
6964b7fd
JG
1375 /* The user has explicitly provided a tag; validate against it. */
1376 if (variant->tag) {
1377 int name_found = 0;
1378
1379 /* Make sure this name is present in the enum tag */
1380 for (i = 0; i < variant->tag->entries->len; i++) {
1381 struct enumeration_mapping *mapping =
1382 g_ptr_array_index(variant->tag->entries, i);
1383
1384 if (mapping->string == field_name_quark) {
1385 name_found = 1;
1386 break;
1387 }
1388 }
1389
1390 if (!name_found) {
1391 /* Validation failed */
1392 ret = -1;
1393 goto end;
273b65be
JG
1394 }
1395 }
1396
6964b7fd
JG
1397 if (add_structure_field(variant->fields, variant->field_name_to_index,
1398 field_type, field_name)) {
273b65be
JG
1399 ret = -1;
1400 goto end;
1401 }
1402end:
1403 return ret;
1404}
1405
b92ddaaa
JG
1406struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_by_name(
1407 struct bt_ctf_field_type *type,
1408 const char *field_name)
1409{
1410 size_t index;
1411 GQuark name_quark;
1412 struct structure_field *field;
1413 struct bt_ctf_field_type_variant *variant;
1414 struct bt_ctf_field_type *field_type = NULL;
1415
1416 if (!type || !field_name) {
1417 goto end;
1418 }
1419
1420 name_quark = g_quark_try_string(field_name);
1421 if (!name_quark) {
1422 goto end;
1423 }
1424
1425 variant = container_of(type, struct bt_ctf_field_type_variant, parent);
1426 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
1427 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
1428 goto end;
1429 }
1430
1431 field = g_ptr_array_index(variant->fields, index);
1432 field_type = field->type;
1433 bt_ctf_field_type_get(field_type);
1434end:
1435 return field_type;
1436}
1437
1438struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_from_tag(
1439 struct bt_ctf_field_type *type,
1440 struct bt_ctf_field *tag)
1441{
1442 const char *enum_value;
1443 struct bt_ctf_field_type *field_type = NULL;
1444
1445 if (!type || !tag || type->declaration->id != CTF_TYPE_VARIANT) {
1446 goto end;
1447 }
1448
1449 enum_value = bt_ctf_field_enumeration_get_mapping_name(tag);
1450 if (!enum_value) {
1451 goto end;
1452 }
1453
1454 /* Already increments field_type's reference count */
1455 field_type = bt_ctf_field_type_variant_get_field_type_by_name(
1456 type, enum_value);
1457end:
1458 return field_type;
1459}
1460
074ee56d 1461int bt_ctf_field_type_variant_get_field_count(struct bt_ctf_field_type *type)
b92ddaaa 1462{
074ee56d 1463 int ret = 0;
b92ddaaa
JG
1464 struct bt_ctf_field_type_variant *variant;
1465
1466 if (!type || (type->declaration->id != CTF_TYPE_VARIANT)) {
1467 ret = -1;
1468 goto end;
1469 }
1470
1471 variant = container_of(type, struct bt_ctf_field_type_variant,
1472 parent);
074ee56d 1473 ret = (int) variant->fields->len;
b92ddaaa
JG
1474end:
1475 return ret;
1476
1477}
1478
1479int bt_ctf_field_type_variant_get_field(struct bt_ctf_field_type *type,
1480 const char **field_name, struct bt_ctf_field_type **field_type,
074ee56d 1481 int index)
b92ddaaa
JG
1482{
1483 struct bt_ctf_field_type_variant *variant;
1484 struct structure_field *field;
1485 int ret = 0;
1486
647f3b93 1487 if (!type || index < 0 || (type->declaration->id != CTF_TYPE_VARIANT)) {
b92ddaaa
JG
1488 ret = -1;
1489 goto end;
1490 }
1491
1492 variant = container_of(type, struct bt_ctf_field_type_variant,
1493 parent);
1494 if (index >= variant->fields->len) {
1495 ret = -1;
1496 goto end;
1497 }
1498
1499 field = g_ptr_array_index(variant->fields, index);
647f3b93
JG
1500 if (field_type) {
1501 *field_type = field->type;
1502 bt_ctf_field_type_get(field->type);
1503 }
1504 if (field_name) {
1505 *field_name = g_quark_to_string(field->name);
1506 }
b92ddaaa
JG
1507end:
1508 return ret;
1509}
1510
273b65be
JG
1511struct bt_ctf_field_type *bt_ctf_field_type_array_create(
1512 struct bt_ctf_field_type *element_type,
1513 unsigned int length)
1514{
1515 struct bt_ctf_field_type_array *array = NULL;
1516
9ce21c30
JG
1517 if (!element_type || length == 0 ||
1518 bt_ctf_field_type_validate(element_type)) {
273b65be
JG
1519 goto error;
1520 }
1521
1522 array = g_new0(struct bt_ctf_field_type_array, 1);
1523 if (!array) {
1524 goto error;
1525 }
1526
1527 array->parent.declaration = &array->declaration.p;
1528 array->parent.declaration->id = CTF_TYPE_ARRAY;
c35a1669 1529
273b65be
JG
1530 bt_ctf_field_type_get(element_type);
1531 array->element_type = element_type;
1532 array->length = length;
c35a1669 1533 bt_ctf_field_type_init(&array->parent);
273b65be
JG
1534 return &array->parent;
1535error:
1536 return NULL;
1537}
1538
b92ddaaa
JG
1539struct bt_ctf_field_type *bt_ctf_field_type_array_get_element_type(
1540 struct bt_ctf_field_type *type)
1541{
1542 struct bt_ctf_field_type *ret = NULL;
1543 struct bt_ctf_field_type_array *array;
1544
1545 if (!type || (type->declaration->id != CTF_TYPE_ARRAY)) {
1546 goto end;
1547 }
1548
1549 array = container_of(type, struct bt_ctf_field_type_array, parent);
1550 ret = array->element_type;
1551 bt_ctf_field_type_get(ret);
1552end:
1553 return ret;
1554}
1555
1556int64_t bt_ctf_field_type_array_get_length(struct bt_ctf_field_type *type)
1557{
1558 int64_t ret;
1559 struct bt_ctf_field_type_array *array;
1560
1561 if (!type || (type->declaration->id != CTF_TYPE_ARRAY)) {
1562 ret = -1;
1563 goto end;
1564 }
1565
1566 array = container_of(type, struct bt_ctf_field_type_array, parent);
1567 ret = (int64_t) array->length;
1568end:
1569 return ret;
1570}
1571
273b65be
JG
1572struct bt_ctf_field_type *bt_ctf_field_type_sequence_create(
1573 struct bt_ctf_field_type *element_type,
1574 const char *length_field_name)
1575{
1576 struct bt_ctf_field_type_sequence *sequence = NULL;
1577
654c1444 1578 if (!element_type || bt_ctf_validate_identifier(length_field_name) ||
9ce21c30 1579 bt_ctf_field_type_validate(element_type)) {
273b65be
JG
1580 goto error;
1581 }
1582
1583 sequence = g_new0(struct bt_ctf_field_type_sequence, 1);
1584 if (!sequence) {
1585 goto error;
1586 }
1587
1588 sequence->parent.declaration = &sequence->declaration.p;
1589 sequence->parent.declaration->id = CTF_TYPE_SEQUENCE;
273b65be
JG
1590 bt_ctf_field_type_get(element_type);
1591 sequence->element_type = element_type;
1592 sequence->length_field_name = g_string_new(length_field_name);
c35a1669 1593 bt_ctf_field_type_init(&sequence->parent);
273b65be
JG
1594 return &sequence->parent;
1595error:
1596 return NULL;
1597}
1598
b92ddaaa
JG
1599struct bt_ctf_field_type *bt_ctf_field_type_sequence_get_element_type(
1600 struct bt_ctf_field_type *type)
1601{
1602 struct bt_ctf_field_type *ret = NULL;
1603 struct bt_ctf_field_type_sequence *sequence;
1604
1605 if (!type || (type->declaration->id != CTF_TYPE_SEQUENCE)) {
1606 goto end;
1607 }
1608
1609 sequence = container_of(type, struct bt_ctf_field_type_sequence,
1610 parent);
1611 ret = sequence->element_type;
1612 bt_ctf_field_type_get(ret);
1613end:
1614 return ret;
1615}
1616
1617const char *bt_ctf_field_type_sequence_get_length_field_name(
1618 struct bt_ctf_field_type *type)
1619{
1620 const char *ret = NULL;
1621 struct bt_ctf_field_type_sequence *sequence;
1622
1623 if (!type || (type->declaration->id != CTF_TYPE_SEQUENCE)) {
1624 goto end;
1625 }
1626
1627 sequence = container_of(type, struct bt_ctf_field_type_sequence,
1628 parent);
1629 ret = sequence->length_field_name->str;
1630end:
1631 return ret;
1632}
1633
273b65be
JG
1634struct bt_ctf_field_type *bt_ctf_field_type_string_create(void)
1635{
1636 struct bt_ctf_field_type_string *string =
1637 g_new0(struct bt_ctf_field_type_string, 1);
1638
1639 if (!string) {
1640 return NULL;
1641 }
1642
1643 string->parent.declaration = &string->declaration.p;
1644 string->parent.declaration->id = CTF_TYPE_STRING;
1645 bt_ctf_field_type_init(&string->parent);
1646 string->declaration.encoding = CTF_STRING_UTF8;
1647 string->parent.declaration->alignment = CHAR_BIT;
1648 return &string->parent;
1649}
1650
b92ddaaa
JG
1651enum ctf_string_encoding bt_ctf_field_type_string_get_encoding(
1652 struct bt_ctf_field_type *type)
1653{
1654 struct bt_ctf_field_type_string *string;
1655 enum ctf_string_encoding ret = CTF_STRING_UNKNOWN;
1656
1657 if (!type || (type->declaration->id != CTF_TYPE_STRING)) {
1658 goto end;
1659 }
1660
1661 string = container_of(type, struct bt_ctf_field_type_string,
1662 parent);
1663 ret = string->declaration.encoding;
1664end:
1665 return ret;
1666}
1667
1668int bt_ctf_field_type_string_set_encoding(struct bt_ctf_field_type *type,
273b65be
JG
1669 enum ctf_string_encoding encoding)
1670{
1671 int ret = 0;
1672 struct bt_ctf_field_type_string *string;
1673
1674 if (!type || type->declaration->id != CTF_TYPE_STRING ||
1675 (encoding != CTF_STRING_UTF8 &&
1676 encoding != CTF_STRING_ASCII)) {
1677 ret = -1;
1678 goto end;
1679 }
1680
1681 string = container_of(type, struct bt_ctf_field_type_string, parent);
1682 string->declaration.encoding = encoding;
1683end:
1684 return ret;
1685}
1686
b92ddaaa
JG
1687int bt_ctf_field_type_get_alignment(struct bt_ctf_field_type *type)
1688{
1689 int ret;
3ffba961 1690 enum ctf_type_id type_id;
b92ddaaa
JG
1691
1692 if (!type) {
1693 ret = -1;
1694 goto end;
1695 }
1696
3ffba961
JG
1697 if (type->frozen) {
1698 ret = (int) type->declaration->alignment;
1699 goto end;
1700 }
1701
1702 type_id = bt_ctf_field_type_get_type_id(type);
1703 switch (type_id) {
1704 case CTF_TYPE_SEQUENCE:
1705 {
1706 struct bt_ctf_field_type *element =
1707 bt_ctf_field_type_sequence_get_element_type(type);
1708
1709 if (!element) {
1710 ret = -1;
1711 goto end;
1712 }
1713
1714 ret = bt_ctf_field_type_get_alignment(element);
1715 bt_ctf_field_type_put(element);
1716 break;
1717 }
1718 case CTF_TYPE_ARRAY:
1719 {
1720 struct bt_ctf_field_type *element =
1721 bt_ctf_field_type_array_get_element_type(type);
1722
1723 if (!element) {
1724 ret = -1;
1725 goto end;
1726 }
1727
1728 ret = bt_ctf_field_type_get_alignment(element);
1729 bt_ctf_field_type_put(element);
1730 break;
1731 }
1732 case CTF_TYPE_STRUCT:
1733 {
1734 int i, element_count;
1735
1736 element_count = bt_ctf_field_type_structure_get_field_count(
1737 type);
1738 if (element_count < 0) {
1739 ret = element_count;
1740 goto end;
1741 }
1742
1743 for (i = 0; i < element_count; i++) {
1744 struct bt_ctf_field_type *field;
1745 int field_alignment;
1746
1747 ret = bt_ctf_field_type_structure_get_field(type, NULL,
1748 &field, i);
1749 if (ret) {
1750 goto end;
1751 }
1752
1753 assert(field);
1754 field_alignment = bt_ctf_field_type_get_alignment(
1755 field);
1756 bt_ctf_field_type_put(field);
1757 if (field_alignment < 0) {
1758 ret = field_alignment;
1759 goto end;
1760 }
1761
1762 type->declaration->alignment = MAX(field_alignment,
1763 type->declaration->alignment);
1764 }
1765 ret = (int) type->declaration->alignment;
1766 break;
1767 }
1768 case CTF_TYPE_UNKNOWN:
1769 ret = -1;
1770 break;
1771 default:
1772 ret = (int) type->declaration->alignment;
1773 break;
1774 }
b92ddaaa
JG
1775end:
1776 return ret;
1777}
1778
273b65be
JG
1779int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type,
1780 unsigned int alignment)
1781{
1782 int ret = 0;
6a43d732 1783 enum ctf_type_id type_id;
273b65be
JG
1784
1785 /* Alignment must be bit-aligned (1) or byte aligned */
1786 if (!type || type->frozen || (alignment != 1 && (alignment & 0x7))) {
1787 ret = -1;
1788 goto end;
1789 }
1790
6a43d732
JG
1791 type_id = bt_ctf_field_type_get_type_id(type);
1792 if (type_id == CTF_TYPE_UNKNOWN) {
1793 ret = -1;
1794 goto end;
1795 }
1796
273b65be
JG
1797 if (type->declaration->id == CTF_TYPE_STRING &&
1798 alignment != CHAR_BIT) {
1799 ret = -1;
1800 goto end;
1801 }
1802
6a43d732
JG
1803 if (type_id == CTF_TYPE_STRUCT || type_id == CTF_TYPE_VARIANT ||
1804 type_id == CTF_TYPE_SEQUENCE || type_id == CTF_TYPE_ARRAY) {
1805 /* Setting an alignment on these types makes no sense */
1806 ret = -1;
1807 goto end;
1808 }
1809
273b65be
JG
1810 type->declaration->alignment = alignment;
1811 ret = 0;
1812end:
1813 return ret;
1814}
1815
b92ddaaa
JG
1816enum bt_ctf_byte_order bt_ctf_field_type_get_byte_order(
1817 struct bt_ctf_field_type *type)
1818{
1819 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
c35a1669 1820 int internal_byte_order = -1;
b92ddaaa
JG
1821
1822 if (!type) {
1823 goto end;
1824 }
1825
1826 switch (type->declaration->id) {
1827 case CTF_TYPE_INTEGER:
1828 {
1829 struct bt_ctf_field_type_integer *integer = container_of(
1830 type, struct bt_ctf_field_type_integer, parent);
c35a1669 1831 internal_byte_order = integer->declaration.byte_order;
b92ddaaa
JG
1832 break;
1833 }
1834 case CTF_TYPE_FLOAT:
1835 {
1836 struct bt_ctf_field_type_floating_point *floating_point =
1837 container_of(type,
1838 struct bt_ctf_field_type_floating_point,
1839 parent);
c35a1669 1840 internal_byte_order = floating_point->declaration.byte_order;
b92ddaaa
JG
1841 break;
1842 }
1843 default:
c35a1669
JG
1844 goto end;
1845 }
1846
1847 switch (internal_byte_order) {
1848 case LITTLE_ENDIAN:
1849 ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN;
b92ddaaa 1850 break;
c35a1669
JG
1851 case BIG_ENDIAN:
1852 ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN;
1853 break;
1854 case 0:
1855 ret = BT_CTF_BYTE_ORDER_NATIVE;
1856 break;
1857 default:
1858 ret = BT_CTF_BYTE_ORDER_UNKNOWN;
b92ddaaa
JG
1859 }
1860end:
1861 return ret;
1862}
1863
273b65be
JG
1864int bt_ctf_field_type_set_byte_order(struct bt_ctf_field_type *type,
1865 enum bt_ctf_byte_order byte_order)
1866{
1867 int ret = 0;
1868 int internal_byte_order;
1869 enum ctf_type_id type_id;
1870
1871 if (!type || type->frozen) {
1872 ret = -1;
1873 goto end;
1874 }
1875
273b65be
JG
1876 switch (byte_order) {
1877 case BT_CTF_BYTE_ORDER_NATIVE:
c35a1669
JG
1878 /* Leave unset. Will be initialized by parent. */
1879 internal_byte_order = 0;
273b65be
JG
1880 break;
1881 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
1882 internal_byte_order = LITTLE_ENDIAN;
1883 break;
1884 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
1885 case BT_CTF_BYTE_ORDER_NETWORK:
1886 internal_byte_order = BIG_ENDIAN;
1887 break;
1888 default:
1889 ret = -1;
1890 goto end;
1891 }
1892
3fa759e9 1893 type_id = type->declaration->id;
273b65be 1894 if (set_byte_order_funcs[type_id]) {
c35a1669 1895 set_byte_order_funcs[type_id](type, internal_byte_order, 0);
273b65be
JG
1896 }
1897end:
1898 return ret;
1899}
1900
b92ddaaa
JG
1901enum ctf_type_id bt_ctf_field_type_get_type_id(
1902 struct bt_ctf_field_type *type)
1903{
1904 if (!type) {
1905 return CTF_TYPE_UNKNOWN;
1906 }
1907
1908 return type->declaration->id;
1909}
2f2d8e05 1910
273b65be
JG
1911void bt_ctf_field_type_get(struct bt_ctf_field_type *type)
1912{
1913 if (!type) {
1914 return;
1915 }
1916
1917 bt_ctf_ref_get(&type->ref_count);
1918}
1919
1920void bt_ctf_field_type_put(struct bt_ctf_field_type *type)
1921{
273b65be
JG
1922 if (!type) {
1923 return;
1924 }
1925
2f2d8e05 1926 bt_ctf_ref_put(&type->ref_count, bt_ctf_field_type_destroy);
273b65be
JG
1927}
1928
1929BT_HIDDEN
1930void bt_ctf_field_type_freeze(struct bt_ctf_field_type *type)
1931{
1932 if (!type) {
1933 return;
1934 }
1935
1936 type->freeze(type);
1937}
1938
1939BT_HIDDEN
b92ddaaa
JG
1940struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_signed(
1941 struct bt_ctf_field_type_variant *variant,
1942 int64_t tag_value)
273b65be
JG
1943{
1944 struct bt_ctf_field_type *type = NULL;
b92ddaaa
JG
1945 GQuark field_name_quark;
1946 gpointer index;
1947 struct structure_field *field_entry;
1948 struct range_overlap_query query = {
1949 .range_start._signed = tag_value,
1950 .range_end._signed = tag_value,
1951 .mapping_name = 0, .overlaps = 0};
273b65be 1952
b92ddaaa
JG
1953 g_ptr_array_foreach(variant->tag->entries, check_ranges_overlap,
1954 &query);
1955 if (!query.overlaps) {
273b65be
JG
1956 goto end;
1957 }
1958
b92ddaaa
JG
1959 field_name_quark = query.mapping_name;
1960 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
1961 GUINT_TO_POINTER(field_name_quark), NULL, &index)) {
273b65be
JG
1962 goto end;
1963 }
1964
e54fab7e 1965 field_entry = g_ptr_array_index(variant->fields, (size_t) index);
b92ddaaa 1966 type = field_entry->type;
273b65be
JG
1967end:
1968 return type;
1969}
1970
1971BT_HIDDEN
b92ddaaa 1972struct bt_ctf_field_type *bt_ctf_field_type_variant_get_field_type_unsigned(
273b65be 1973 struct bt_ctf_field_type_variant *variant,
b92ddaaa 1974 uint64_t tag_value)
273b65be
JG
1975{
1976 struct bt_ctf_field_type *type = NULL;
1977 GQuark field_name_quark;
1978 gpointer index;
1979 struct structure_field *field_entry;
b92ddaaa
JG
1980 struct range_overlap_query query = {
1981 .range_start._unsigned = tag_value,
1982 .range_end._unsigned = tag_value,
1983 .mapping_name = 0, .overlaps = 0};
273b65be 1984
b92ddaaa
JG
1985 g_ptr_array_foreach(variant->tag->entries,
1986 check_ranges_overlap_unsigned,
273b65be
JG
1987 &query);
1988 if (!query.overlaps) {
1989 goto end;
1990 }
1991
1992 field_name_quark = query.mapping_name;
1993 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
1994 GUINT_TO_POINTER(field_name_quark), NULL, &index)) {
1995 goto end;
1996 }
1997
1998 field_entry = g_ptr_array_index(variant->fields, (size_t)index);
1999 type = field_entry->type;
2000end:
2001 return type;
2002}
2003
2004BT_HIDDEN
2005int bt_ctf_field_type_serialize(struct bt_ctf_field_type *type,
2006 struct metadata_context *context)
2007{
2008 int ret;
2009
2010 if (!type || !context) {
2011 ret = -1;
2012 goto end;
2013 }
2014
2015 ret = type->serialize(type, context);
2016end:
2017 return ret;
2018}
2019
c35a1669
JG
2020BT_HIDDEN
2021void bt_ctf_field_type_set_native_byte_order(struct bt_ctf_field_type *type,
2022 int byte_order)
2023{
2024 if (!type) {
2025 return;
2026 }
2027
2028 assert(byte_order == LITTLE_ENDIAN || byte_order == BIG_ENDIAN);
2029 if (set_byte_order_funcs[type->declaration->id]) {
2030 set_byte_order_funcs[type->declaration->id](type,
2031 byte_order, 1);
2032 }
2033}
2034
24724933
JG
2035BT_HIDDEN
2036struct bt_ctf_field_type *bt_ctf_field_type_copy(struct bt_ctf_field_type *type)
2037{
2038 struct bt_ctf_field_type *copy = NULL;
2039
2040 if (!type) {
2041 goto end;
2042 }
2043
2044 copy = type_copy_funcs[type->declaration->id](type);
2045end:
2046 return copy;
2047}
2048
6b64f185
JG
2049BT_HIDDEN
2050struct bt_ctf_field_path *bt_ctf_field_path_create(void)
2051{
2052 struct bt_ctf_field_path *field_path = NULL;
2053
2054 field_path = g_new0(struct bt_ctf_field_path, 1);
2055 if (!field_path) {
2056 goto end;
2057 }
2058
2059 field_path->root = CTF_NODE_UNKNOWN;
2060 field_path->path_indexes = g_array_new(TRUE, FALSE, sizeof(int));
2061 if (!field_path->path_indexes) {
2062 bt_ctf_field_path_destroy(field_path);
2063 field_path = NULL;
2064 }
2065end:
2066 return field_path;
2067}
2068
2069
2070BT_HIDDEN
2071struct bt_ctf_field_path *bt_ctf_field_path_copy(
2072 struct bt_ctf_field_path *path)
2073{
2074 struct bt_ctf_field_path *new_path = bt_ctf_field_path_create();
2075
2076 if (!new_path) {
2077 goto end;
2078 }
2079
2080 new_path->root = path->root;
2081 g_array_insert_vals(new_path->path_indexes, 0,
2082 path->path_indexes->data, path->path_indexes->len);
2083end:
2084 return new_path;
2085}
2086
2087BT_HIDDEN
2088void bt_ctf_field_path_destroy(struct bt_ctf_field_path *path)
2089{
2090 if (!path) {
2091 return;
2092 }
2093
2094 if (path->path_indexes) {
2095 g_array_free(path->path_indexes, TRUE);
2096 }
2097 g_free(path);
2098}
39a5e0db
JG
2099
2100BT_HIDDEN
2101int bt_ctf_field_type_structure_get_field_name_index(
2102 struct bt_ctf_field_type *type, const char *name)
2103{
2104 int ret;
2105 size_t index;
2106 GQuark name_quark;
2107 struct bt_ctf_field_type_structure *structure;
2108
2109 if (!type || !name ||
2110 bt_ctf_field_type_get_type_id(type) != CTF_TYPE_STRUCT) {
2111 ret = -1;
2112 goto end;
2113 }
2114
2115 name_quark = g_quark_try_string(name);
2116 if (!name_quark) {
2117 ret = -1;
2118 goto end;
2119 }
2120
2121 structure = container_of(type, struct bt_ctf_field_type_structure,
2122 parent);
2123 if (!g_hash_table_lookup_extended(structure->field_name_to_index,
2124 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
2125 ret = -1;
2126 goto end;
2127 }
2128 ret = (int) index;
2129end:
2130 return ret;
2131}
736133f1 2132
5cec03e4
JG
2133BT_HIDDEN
2134int bt_ctf_field_type_structure_set_field_index(struct bt_ctf_field_type *type,
2135 struct bt_ctf_field_type *field, int index)
2136{
2137 int ret = 0;
2138 struct bt_ctf_field_type_structure *structure;
2139
6c827042 2140 if (!type || !field ||
5cec03e4
JG
2141 bt_ctf_field_type_get_type_id(type) != CTF_TYPE_STRUCT) {
2142 ret = -1;
2143 goto end;
2144 }
2145
2146 structure = container_of(type, struct bt_ctf_field_type_structure,
2147 parent);
2148 if (index < 0 || index >= structure->fields->len) {
2149 ret = -1;
2150 goto end;
2151 }
2152
2153 bt_ctf_field_type_get(field);
2154 bt_ctf_field_type_put(((struct structure_field *)
2155 g_ptr_array_index(structure->fields, index))->type);
2156 ((struct structure_field *) structure->fields->pdata[index])->type =
2157 field;
2158end:
2159 return ret;
2160}
2161
736133f1
JG
2162BT_HIDDEN
2163int bt_ctf_field_type_variant_get_field_name_index(
2164 struct bt_ctf_field_type *type, const char *name)
2165{
2166 int ret;
2167 size_t index;
2168 GQuark name_quark;
2169 struct bt_ctf_field_type_variant *variant;
2170
2171 if (!type || !name ||
2172 bt_ctf_field_type_get_type_id(type) != CTF_TYPE_VARIANT) {
2173 ret = -1;
2174 goto end;
2175 }
2176
2177 name_quark = g_quark_try_string(name);
2178 if (!name_quark) {
2179 ret = -1;
2180 goto end;
2181 }
2182
2183 variant = container_of(type, struct bt_ctf_field_type_variant,
2184 parent);
2185 if (!g_hash_table_lookup_extended(variant->field_name_to_index,
2186 GUINT_TO_POINTER(name_quark), NULL, (gpointer *)&index)) {
2187 ret = -1;
2188 goto end;
2189 }
2190 ret = (int) index;
2191end:
2192 return ret;
2193}
aa4e271c
JG
2194
2195BT_HIDDEN
2196int bt_ctf_field_type_sequence_set_length_field_path(
2197 struct bt_ctf_field_type *type,
2198 struct bt_ctf_field_path *path)
2199{
2200 int ret = 0;
2201 struct bt_ctf_field_type_sequence *sequence;
2202
2203 if (!type || bt_ctf_field_type_get_type_id(type) != CTF_TYPE_SEQUENCE) {
2204 ret = -1;
2205 goto end;
2206 }
2207
2208 sequence = container_of(type, struct bt_ctf_field_type_sequence,
2209 parent);
2210 if (sequence->length_field_path) {
2211 bt_ctf_field_path_destroy(sequence->length_field_path);
2212 }
2213 sequence->length_field_path = path;
2214end:
2215 return ret;
2216}
4a1e8671
JG
2217
2218BT_HIDDEN
2219int bt_ctf_field_type_variant_set_tag_field_path(struct bt_ctf_field_type *type,
2220 struct bt_ctf_field_path *path)
2221{
2222 int ret = 0;
2223 struct bt_ctf_field_type_variant *variant;
2224
2225 if (!type || bt_ctf_field_type_get_type_id(type) != CTF_TYPE_VARIANT) {
2226 ret = -1;
2227 goto end;
2228 }
2229
2230 variant = container_of(type, struct bt_ctf_field_type_variant,
2231 parent);
2232 if (variant->tag_path) {
2233 bt_ctf_field_path_destroy(variant->tag_path);
2234 }
2235 variant->tag_path = path;
2236end:
2237 return ret;
2238}
3f39933a
JG
2239
2240BT_HIDDEN
2241int bt_ctf_field_type_variant_set_tag(struct bt_ctf_field_type *type,
2242 struct bt_ctf_field_type *tag)
2243{
2244 int ret = 0;
2245 struct bt_ctf_field_type_variant *variant;
2246
2247 if (!type || !tag || type->frozen ||
2248 bt_ctf_field_type_get_type_id(tag) != CTF_TYPE_ENUM) {
2249 ret = -1;
2250 goto end;
2251 }
2252
2253 variant = container_of(type, struct bt_ctf_field_type_variant,
2254 parent);
2255 bt_ctf_field_type_get(tag);
2256 if (variant->tag) {
2257 bt_ctf_field_type_put(&variant->tag->parent);
2258 }
2259 variant->tag = container_of(tag, struct bt_ctf_field_type_enumeration,
2260 parent);
2261end:
2262 return ret;
2263}
2264
5cec03e4
JG
2265BT_HIDDEN
2266int bt_ctf_field_type_variant_set_field_index(struct bt_ctf_field_type *type,
2267 struct bt_ctf_field_type *field, int index)
2268{
2269 int ret = 0;
2270 struct bt_ctf_field_type_variant *variant;
2271
6c827042 2272 if (!type || !field ||
5cec03e4
JG
2273 bt_ctf_field_type_get_type_id(type) != CTF_TYPE_VARIANT) {
2274 ret = -1;
2275 goto end;
2276 }
2277
2278 variant = container_of(type, struct bt_ctf_field_type_variant,
2279 parent);
2280 if (index < 0 || index >= variant->fields->len) {
2281 ret = -1;
2282 goto end;
2283 }
2284
2285 bt_ctf_field_type_get(field);
2286 bt_ctf_field_type_put(((struct structure_field *)
2287 g_ptr_array_index(variant->fields, index))->type);
2288 ((struct structure_field *) variant->fields->pdata[index])->type =
2289 field;
2290end:
2291 return ret;
2292}
2293
273b65be
JG
2294static
2295void bt_ctf_field_type_integer_destroy(struct bt_ctf_ref *ref)
2296{
2297 struct bt_ctf_field_type_integer *integer;
2298
2299 if (!ref) {
2300 return;
2301 }
2302
2303 integer = container_of(
2304 container_of(ref, struct bt_ctf_field_type, ref_count),
2305 struct bt_ctf_field_type_integer, parent);
eee752e5
JG
2306 if (integer->mapped_clock) {
2307 bt_ctf_clock_put(integer->mapped_clock);
2308 }
273b65be
JG
2309 g_free(integer);
2310}
2311
2312static
2313void bt_ctf_field_type_enumeration_destroy(struct bt_ctf_ref *ref)
2314{
2315 struct bt_ctf_field_type_enumeration *enumeration;
2316
2317 if (!ref) {
2318 return;
2319 }
2320
2321 enumeration = container_of(
2322 container_of(ref, struct bt_ctf_field_type, ref_count),
2323 struct bt_ctf_field_type_enumeration, parent);
2324 g_ptr_array_free(enumeration->entries, TRUE);
2325 bt_ctf_field_type_put(enumeration->container);
2326 g_free(enumeration);
2327}
2328
2329static
2330void bt_ctf_field_type_floating_point_destroy(struct bt_ctf_ref *ref)
2331{
2332 struct bt_ctf_field_type_floating_point *floating_point;
2333
2334 if (!ref) {
2335 return;
2336 }
2337
2338 floating_point = container_of(
2339 container_of(ref, struct bt_ctf_field_type, ref_count),
2340 struct bt_ctf_field_type_floating_point, parent);
2341 g_free(floating_point);
2342}
2343
2344static
2345void bt_ctf_field_type_structure_destroy(struct bt_ctf_ref *ref)
2346{
2347 struct bt_ctf_field_type_structure *structure;
2348
2349 if (!ref) {
2350 return;
2351 }
2352
2353 structure = container_of(
2354 container_of(ref, struct bt_ctf_field_type, ref_count),
2355 struct bt_ctf_field_type_structure, parent);
2356 g_ptr_array_free(structure->fields, TRUE);
2357 g_hash_table_destroy(structure->field_name_to_index);
2358 g_free(structure);
2359}
2360
2361static
2362void bt_ctf_field_type_variant_destroy(struct bt_ctf_ref *ref)
2363{
2364 struct bt_ctf_field_type_variant *variant;
2365
2366 if (!ref) {
2367 return;
2368 }
2369
2370 variant = container_of(
2371 container_of(ref, struct bt_ctf_field_type, ref_count),
2372 struct bt_ctf_field_type_variant, parent);
2373 g_ptr_array_free(variant->fields, TRUE);
2374 g_hash_table_destroy(variant->field_name_to_index);
2375 g_string_free(variant->tag_name, TRUE);
2376 bt_ctf_field_type_put(&variant->tag->parent);
4a1e8671 2377 bt_ctf_field_path_destroy(variant->tag_path);
273b65be
JG
2378 g_free(variant);
2379}
2380
2381static
2382void bt_ctf_field_type_array_destroy(struct bt_ctf_ref *ref)
2383{
2384 struct bt_ctf_field_type_array *array;
2385
2386 if (!ref) {
2387 return;
2388 }
2389
2390 array = container_of(
2391 container_of(ref, struct bt_ctf_field_type, ref_count),
2392 struct bt_ctf_field_type_array, parent);
2393 bt_ctf_field_type_put(array->element_type);
2394 g_free(array);
2395}
2396
2397static
2398void bt_ctf_field_type_sequence_destroy(struct bt_ctf_ref *ref)
2399{
2400 struct bt_ctf_field_type_sequence *sequence;
2401
2402 if (!ref) {
2403 return;
2404 }
2405
2406 sequence = container_of(
2407 container_of(ref, struct bt_ctf_field_type, ref_count),
2408 struct bt_ctf_field_type_sequence, parent);
2409 bt_ctf_field_type_put(sequence->element_type);
2410 g_string_free(sequence->length_field_name, TRUE);
aa4e271c 2411 bt_ctf_field_path_destroy(sequence->length_field_path);
273b65be
JG
2412 g_free(sequence);
2413}
2414
2415static
2416void bt_ctf_field_type_string_destroy(struct bt_ctf_ref *ref)
2417{
2418 struct bt_ctf_field_type_string *string;
2419
2420 if (!ref) {
2421 return;
2422 }
2423
2424 string = container_of(
2425 container_of(ref, struct bt_ctf_field_type, ref_count),
2426 struct bt_ctf_field_type_string, parent);
2427 g_free(string);
2428}
2429
2430static
2431void generic_field_type_freeze(struct bt_ctf_field_type *type)
2432{
2433 type->frozen = 1;
2434}
2435
2436static
2437void bt_ctf_field_type_enumeration_freeze(struct bt_ctf_field_type *type)
2438{
2439 struct bt_ctf_field_type_enumeration *enumeration_type = container_of(
2440 type, struct bt_ctf_field_type_enumeration, parent);
2441
2442 generic_field_type_freeze(type);
2443 bt_ctf_field_type_freeze(enumeration_type->container);
2444}
2445
2446static
2447void freeze_structure_field(struct structure_field *field)
2448{
2449 bt_ctf_field_type_freeze(field->type);
2450}
2451
2452static
2453void bt_ctf_field_type_structure_freeze(struct bt_ctf_field_type *type)
2454{
2455 struct bt_ctf_field_type_structure *structure_type = container_of(
2456 type, struct bt_ctf_field_type_structure, parent);
2457
3ffba961
JG
2458 /* Cache the alignment */
2459 type->declaration->alignment = bt_ctf_field_type_get_alignment(type);
273b65be 2460 generic_field_type_freeze(type);
3ffba961
JG
2461 g_ptr_array_foreach(structure_type->fields,
2462 (GFunc) freeze_structure_field, NULL);
273b65be
JG
2463}
2464
2465static
2466void bt_ctf_field_type_variant_freeze(struct bt_ctf_field_type *type)
2467{
2468 struct bt_ctf_field_type_variant *variant_type = container_of(
2469 type, struct bt_ctf_field_type_variant, parent);
2470
3ffba961
JG
2471 /* Cache the alignment */
2472 type->declaration->alignment = bt_ctf_field_type_get_alignment(type);
273b65be 2473 generic_field_type_freeze(type);
3ffba961
JG
2474 g_ptr_array_foreach(variant_type->fields,
2475 (GFunc) freeze_structure_field, NULL);
273b65be
JG
2476}
2477
2478static
2479void bt_ctf_field_type_array_freeze(struct bt_ctf_field_type *type)
2480{
2481 struct bt_ctf_field_type_array *array_type = container_of(
2482 type, struct bt_ctf_field_type_array, parent);
2483
3ffba961
JG
2484 /* Cache the alignment */
2485 type->declaration->alignment = bt_ctf_field_type_get_alignment(type);
273b65be
JG
2486 generic_field_type_freeze(type);
2487 bt_ctf_field_type_freeze(array_type->element_type);
2488}
2489
2490static
2491void bt_ctf_field_type_sequence_freeze(struct bt_ctf_field_type *type)
2492{
2493 struct bt_ctf_field_type_sequence *sequence_type = container_of(
2494 type, struct bt_ctf_field_type_sequence, parent);
2495
3ffba961
JG
2496 /* Cache the alignment */
2497 type->declaration->alignment = bt_ctf_field_type_get_alignment(type);
273b65be
JG
2498 generic_field_type_freeze(type);
2499 bt_ctf_field_type_freeze(sequence_type->element_type);
2500}
2501
2502static
2503const char *get_encoding_string(enum ctf_string_encoding encoding)
2504{
2505 const char *encoding_string;
2506
2507 switch (encoding) {
2508 case CTF_STRING_NONE:
2509 encoding_string = "none";
2510 break;
2511 case CTF_STRING_ASCII:
2512 encoding_string = "ASCII";
2513 break;
2514 case CTF_STRING_UTF8:
2515 encoding_string = "UTF8";
2516 break;
2517 default:
2518 encoding_string = "unknown";
2519 break;
2520 }
2521
2522 return encoding_string;
2523}
2524
2525static
2526const char *get_integer_base_string(enum bt_ctf_integer_base base)
2527{
2528 const char *base_string;
2529
2530 switch (base) {
2531 case BT_CTF_INTEGER_BASE_DECIMAL:
2532 base_string = "decimal";
2533 break;
2534 case BT_CTF_INTEGER_BASE_HEXADECIMAL:
2535 base_string = "hexadecimal";
2536 break;
2537 case BT_CTF_INTEGER_BASE_OCTAL:
2538 base_string = "octal";
2539 break;
2540 case BT_CTF_INTEGER_BASE_BINARY:
2541 base_string = "binary";
2542 break;
2543 default:
2544 base_string = "unknown";
2545 break;
2546 }
2547
2548 return base_string;
2549}
2550
2551static
2552int bt_ctf_field_type_integer_serialize(struct bt_ctf_field_type *type,
2553 struct metadata_context *context)
2554{
2555 struct bt_ctf_field_type_integer *integer = container_of(type,
2556 struct bt_ctf_field_type_integer, parent);
6cfb906f 2557 int ret = 0;
273b65be
JG
2558
2559 g_string_append_printf(context->string,
6cfb906f 2560 "integer { size = %zu; align = %zu; signed = %s; encoding = %s; base = %s; byte_order = %s",
273b65be
JG
2561 integer->declaration.len, type->declaration->alignment,
2562 (integer->declaration.signedness ? "true" : "false"),
2563 get_encoding_string(integer->declaration.encoding),
2564 get_integer_base_string(integer->declaration.base),
2565 get_byte_order_string(integer->declaration.byte_order));
6cfb906f
JG
2566 if (integer->mapped_clock) {
2567 const char *clock_name = bt_ctf_clock_get_name(
2568 integer->mapped_clock);
2569
2570 if (!clock_name) {
2571 ret = -1;
2572 goto end;
2573 }
2574
2575 g_string_append_printf(context->string,
4ebdec03 2576 "; map = clock.%s.value", clock_name);
6cfb906f
JG
2577 }
2578
2579 g_string_append(context->string, "; }");
2580end:
2581 return ret;
273b65be
JG
2582}
2583
2584static
2585int bt_ctf_field_type_enumeration_serialize(struct bt_ctf_field_type *type,
2586 struct metadata_context *context)
2587{
2588 size_t entry;
9ce21c30 2589 int ret;
273b65be
JG
2590 struct bt_ctf_field_type_enumeration *enumeration = container_of(type,
2591 struct bt_ctf_field_type_enumeration, parent);
b92ddaaa
JG
2592 struct bt_ctf_field_type *container_type;
2593 int container_signed;
273b65be 2594
9ce21c30
JG
2595 ret = bt_ctf_field_type_validate(type);
2596 if (ret) {
2597 goto end;
2598 }
2599
b92ddaaa
JG
2600 container_type = bt_ctf_field_type_enumeration_get_container_type(type);
2601 if (!container_type) {
2602 ret = -1;
2603 goto end;
2604 }
2605
2606 container_signed = bt_ctf_field_type_integer_get_signed(container_type);
2607 if (container_signed < 0) {
2608 ret = container_signed;
2609 goto error_put_container_type;
2610 }
2611
273b65be
JG
2612 g_string_append(context->string, "enum : ");
2613 ret = bt_ctf_field_type_serialize(enumeration->container, context);
2614 if (ret) {
b92ddaaa 2615 goto error_put_container_type;
273b65be
JG
2616 }
2617
2618 g_string_append(context->string, " { ");
2619 for (entry = 0; entry < enumeration->entries->len; entry++) {
2620 struct enumeration_mapping *mapping =
2621 enumeration->entries->pdata[entry];
2622
b92ddaaa
JG
2623 if (container_signed) {
2624 if (mapping->range_start._signed ==
2625 mapping->range_end._signed) {
2626 g_string_append_printf(context->string,
2627 "\"%s\" = %" PRId64,
2628 g_quark_to_string(mapping->string),
2629 mapping->range_start._signed);
2630 } else {
2631 g_string_append_printf(context->string,
2632 "\"%s\" = %" PRId64 " ... %" PRId64,
2633 g_quark_to_string(mapping->string),
2634 mapping->range_start._signed,
2635 mapping->range_end._signed);
2636 }
273b65be 2637 } else {
b92ddaaa
JG
2638 if (mapping->range_start._unsigned ==
2639 mapping->range_end._unsigned) {
2640 g_string_append_printf(context->string,
2641 "\"%s\" = %" PRIu64,
2642 g_quark_to_string(mapping->string),
2643 mapping->range_start._unsigned);
2644 } else {
2645 g_string_append_printf(context->string,
2646 "\"%s\" = %" PRIu64 " ... %" PRIu64,
2647 g_quark_to_string(mapping->string),
2648 mapping->range_start._unsigned,
2649 mapping->range_end._unsigned);
2650 }
273b65be
JG
2651 }
2652
2653 g_string_append(context->string,
2654 ((entry != (enumeration->entries->len - 1)) ?
2655 ", " : " }"));
2656 }
2657
2658 if (context->field_name->len) {
2659 g_string_append_printf(context->string, " %s",
2660 context->field_name->str);
2661 g_string_assign(context->field_name, "");
2662 }
b92ddaaa
JG
2663error_put_container_type:
2664 bt_ctf_field_type_put(container_type);
273b65be
JG
2665end:
2666 return ret;
2667}
2668
2669static
2670int bt_ctf_field_type_floating_point_serialize(struct bt_ctf_field_type *type,
2671 struct metadata_context *context)
2672{
2673 struct bt_ctf_field_type_floating_point *floating_point = container_of(
2674 type, struct bt_ctf_field_type_floating_point, parent);
2675
2676 g_string_append_printf(context->string,
2677 "floating_point { exp_dig = %zu; mant_dig = %zu; byte_order = %s; align = %zu; }",
2678 floating_point->declaration.exp->len,
2679 floating_point->declaration.mantissa->len + 1,
2680 get_byte_order_string(floating_point->declaration.byte_order),
2681 type->declaration->alignment);
2682 return 0;
2683}
2684
2685static
2686int bt_ctf_field_type_structure_serialize(struct bt_ctf_field_type *type,
2687 struct metadata_context *context)
2688{
2689 size_t i;
2690 unsigned int indent;
2691 int ret = 0;
2692 struct bt_ctf_field_type_structure *structure = container_of(type,
2693 struct bt_ctf_field_type_structure, parent);
2694 GString *structure_field_name = context->field_name;
2695
2696 context->field_name = g_string_new("");
2697
2698 context->current_indentation_level++;
2699 g_string_append(context->string, "struct {\n");
2700
2701 for (i = 0; i < structure->fields->len; i++) {
2702 struct structure_field *field;
2703
2704 for (indent = 0; indent < context->current_indentation_level;
2705 indent++) {
2706 g_string_append_c(context->string, '\t');
2707 }
2708
2709 field = structure->fields->pdata[i];
2710 g_string_assign(context->field_name,
2711 g_quark_to_string(field->name));
2712 ret = bt_ctf_field_type_serialize(field->type, context);
2713 if (ret) {
2714 goto end;
2715 }
2716
2717 if (context->field_name->len) {
2718 g_string_append_printf(context->string, " %s",
2719 context->field_name->str);
2720 }
2721 g_string_append(context->string, ";\n");
2722 }
2723
2724 context->current_indentation_level--;
2725 for (indent = 0; indent < context->current_indentation_level;
2726 indent++) {
2727 g_string_append_c(context->string, '\t');
2728 }
2729
2730 g_string_append_printf(context->string, "} align(%zu)",
2731 type->declaration->alignment);
2732end:
2733 g_string_free(context->field_name, TRUE);
2734 context->field_name = structure_field_name;
2735 return ret;
2736}
2737
2738static
2739int bt_ctf_field_type_variant_serialize(struct bt_ctf_field_type *type,
2740 struct metadata_context *context)
2741{
2742 size_t i;
2743 unsigned int indent;
2744 int ret = 0;
2745 struct bt_ctf_field_type_variant *variant = container_of(
2746 type, struct bt_ctf_field_type_variant, parent);
2747 GString *variant_field_name = context->field_name;
2748
2749 context->field_name = g_string_new("");
6964b7fd
JG
2750 if (variant->tag_name->len > 0) {
2751 g_string_append_printf(context->string,
2752 "variant <%s> {\n", variant->tag_name->str);
2753 } else {
2754 g_string_append(context->string, "variant {\n");
2755 }
2756
273b65be
JG
2757 context->current_indentation_level++;
2758 for (i = 0; i < variant->fields->len; i++) {
2759 struct structure_field *field = variant->fields->pdata[i];
2760
2761 g_string_assign(context->field_name,
2762 g_quark_to_string(field->name));
2763 for (indent = 0; indent < context->current_indentation_level;
2764 indent++) {
2765 g_string_append_c(context->string, '\t');
2766 }
2767
2768 g_string_assign(context->field_name,
2769 g_quark_to_string(field->name));
2770 ret = bt_ctf_field_type_serialize(field->type, context);
2771 if (ret) {
2772 goto end;
2773 }
2774
2775 if (context->field_name->len) {
2776 g_string_append_printf(context->string, " %s;",
2777 context->field_name->str);
2778 }
2779
2780 g_string_append_c(context->string, '\n');
2781 }
2782
2783 context->current_indentation_level--;
2784 for (indent = 0; indent < context->current_indentation_level;
2785 indent++) {
2786 g_string_append_c(context->string, '\t');
2787 }
2788
2789 g_string_append(context->string, "}");
2790end:
2791 g_string_free(context->field_name, TRUE);
2792 context->field_name = variant_field_name;
2793 return ret;
2794}
2795
2796static
2797int bt_ctf_field_type_array_serialize(struct bt_ctf_field_type *type,
2798 struct metadata_context *context)
2799{
2800 int ret = 0;
2801 struct bt_ctf_field_type_array *array = container_of(type,
2802 struct bt_ctf_field_type_array, parent);
2803
2804 ret = bt_ctf_field_type_serialize(array->element_type, context);
2805 if (ret) {
2806 goto end;
2807 }
2808
2809 if (context->field_name->len) {
2810 g_string_append_printf(context->string, " %s[%u]",
2811 context->field_name->str, array->length);
2812 g_string_assign(context->field_name, "");
2813 } else {
2814 g_string_append_printf(context->string, "[%u]", array->length);
2815 }
2816end:
2817 return ret;
2818}
2819
2820static
2821int bt_ctf_field_type_sequence_serialize(struct bt_ctf_field_type *type,
2822 struct metadata_context *context)
2823{
2824 int ret = 0;
2825 struct bt_ctf_field_type_sequence *sequence = container_of(
2826 type, struct bt_ctf_field_type_sequence, parent);
2827
2828 ret = bt_ctf_field_type_serialize(sequence->element_type, context);
2829 if (ret) {
2830 goto end;
2831 }
2832
2833 if (context->field_name->len) {
2834 g_string_append_printf(context->string, " %s[%s]",
2835 context->field_name->str,
2836 sequence->length_field_name->str);
2837 g_string_assign(context->field_name, "");
2838 } else {
2839 g_string_append_printf(context->string, "[%s]",
2840 sequence->length_field_name->str);
2841 }
2842end:
2843 return ret;
2844}
2845
2846static
2847int bt_ctf_field_type_string_serialize(struct bt_ctf_field_type *type,
2848 struct metadata_context *context)
2849{
2850 struct bt_ctf_field_type_string *string = container_of(
2851 type, struct bt_ctf_field_type_string, parent);
2852
2853 g_string_append_printf(context->string,
2854 "string { encoding = %s; }",
2855 get_encoding_string(string->declaration.encoding));
2856 return 0;
2857}
2858
2859static
2860void bt_ctf_field_type_integer_set_byte_order(struct bt_ctf_field_type *type,
c35a1669 2861 int byte_order, int set_native)
273b65be
JG
2862{
2863 struct bt_ctf_field_type_integer *integer_type = container_of(type,
2864 struct bt_ctf_field_type_integer, parent);
2865
c35a1669
JG
2866 if (set_native) {
2867 integer_type->declaration.byte_order =
2868 integer_type->declaration.byte_order == 0 ?
2869 byte_order : integer_type->declaration.byte_order;
2870 } else {
2871 integer_type->declaration.byte_order = byte_order;
2872 }
2873}
2874
2875static
2876void bt_ctf_field_type_enumeration_set_byte_order(
2877 struct bt_ctf_field_type *type, int byte_order, int set_native)
2878{
2879 struct bt_ctf_field_type_enumeration *enum_type = container_of(type,
2880 struct bt_ctf_field_type_enumeration, parent);
2881
2882 /* Safe to assume that container is an integer */
2883 bt_ctf_field_type_integer_set_byte_order(enum_type->container,
2884 byte_order, set_native);
273b65be
JG
2885}
2886
2887static
2888void bt_ctf_field_type_floating_point_set_byte_order(
c35a1669 2889 struct bt_ctf_field_type *type, int byte_order, int set_native)
273b65be
JG
2890{
2891 struct bt_ctf_field_type_floating_point *floating_point_type =
2892 container_of(type, struct bt_ctf_field_type_floating_point,
2893 parent);
2894
c35a1669
JG
2895 if (set_native) {
2896 floating_point_type->declaration.byte_order =
2897 floating_point_type->declaration.byte_order == 0 ?
2898 byte_order :
2899 floating_point_type->declaration.byte_order;
2900 floating_point_type->sign.byte_order =
2901 floating_point_type->sign.byte_order == 0 ?
2902 byte_order : floating_point_type->sign.byte_order;
2903 floating_point_type->mantissa.byte_order =
2904 floating_point_type->mantissa.byte_order == 0 ?
2905 byte_order : floating_point_type->mantissa.byte_order;
2906 floating_point_type->exp.byte_order =
2907 floating_point_type->exp.byte_order == 0 ?
2908 byte_order : floating_point_type->exp.byte_order;
2909 } else {
2910 floating_point_type->declaration.byte_order = byte_order;
2911 floating_point_type->sign.byte_order = byte_order;
2912 floating_point_type->mantissa.byte_order = byte_order;
2913 floating_point_type->exp.byte_order = byte_order;
2914 }
2915}
2916
2917static
2918void bt_ctf_field_type_structure_set_byte_order(struct bt_ctf_field_type *type,
2919 int byte_order, int set_native)
2920{
2921 int i;
2922 struct bt_ctf_field_type_structure *structure_type =
2923 container_of(type, struct bt_ctf_field_type_structure,
2924 parent);
2925
2926 for (i = 0; i < structure_type->fields->len; i++) {
2927 struct structure_field *field = g_ptr_array_index(
2928 structure_type->fields, i);
2929 struct bt_ctf_field_type *field_type = field->type;
2930
2931 if (set_byte_order_funcs[field_type->declaration->id]) {
2932 set_byte_order_funcs[field_type->declaration->id](
2933 field_type, byte_order, set_native);
2934 }
2935 }
2936}
2937
2938static
2939void bt_ctf_field_type_variant_set_byte_order(struct bt_ctf_field_type *type,
2940 int byte_order, int set_native)
2941{
2942 int i;
2943 struct bt_ctf_field_type_variant *variant_type =
2944 container_of(type, struct bt_ctf_field_type_variant,
2945 parent);
2946
2947 for (i = 0; i < variant_type->fields->len; i++) {
2948 struct structure_field *field = g_ptr_array_index(
2949 variant_type->fields, i);
2950 struct bt_ctf_field_type *field_type = field->type;
2951
2952 if (set_byte_order_funcs[field_type->declaration->id]) {
2953 set_byte_order_funcs[field_type->declaration->id](
2954 field_type, byte_order, set_native);
2955 }
2956 }
2957}
2958
2959static
2960void bt_ctf_field_type_array_set_byte_order(struct bt_ctf_field_type *type,
2961 int byte_order, int set_native)
2962{
2963 struct bt_ctf_field_type_array *array_type =
2964 container_of(type, struct bt_ctf_field_type_array,
2965 parent);
2966
2967 if (set_byte_order_funcs[array_type->element_type->declaration->id]) {
2968 set_byte_order_funcs[array_type->element_type->declaration->id](
2969 array_type->element_type, byte_order, set_native);
2970 }
2971}
2972
2973static
2974void bt_ctf_field_type_sequence_set_byte_order(struct bt_ctf_field_type *type,
2975 int byte_order, int set_native)
2976{
2977 struct bt_ctf_field_type_sequence *sequence_type =
2978 container_of(type, struct bt_ctf_field_type_sequence,
2979 parent);
2980
2981 if (set_byte_order_funcs[
2982 sequence_type->element_type->declaration->id]) {
2983 set_byte_order_funcs[
2984 sequence_type->element_type->declaration->id](
2985 sequence_type->element_type, byte_order, set_native);
2986 }
273b65be 2987}
24724933
JG
2988
2989static
2990struct bt_ctf_field_type *bt_ctf_field_type_integer_copy(
2991 struct bt_ctf_field_type *type)
2992{
2993 struct bt_ctf_field_type *copy;
2994 struct bt_ctf_field_type_integer *integer, *copy_integer;
2995
2996 integer = container_of(type, struct bt_ctf_field_type_integer, parent);
2997 copy = bt_ctf_field_type_integer_create(integer->declaration.len);
2998 if (!copy) {
2999 goto end;
3000 }
3001
3002 copy_integer = container_of(copy, struct bt_ctf_field_type_integer,
3003 parent);
3004 copy_integer->declaration = integer->declaration;
3005 if (integer->mapped_clock) {
3006 bt_ctf_clock_get(integer->mapped_clock);
3007 copy_integer->mapped_clock = integer->mapped_clock;
3008 }
3009end:
3010 return copy;
3011}
3012
3013static
3014struct bt_ctf_field_type *bt_ctf_field_type_enumeration_copy(
3015 struct bt_ctf_field_type *type)
3016{
3017 size_t i;
3018 struct bt_ctf_field_type *copy = NULL, *copy_container;
3019 struct bt_ctf_field_type_enumeration *enumeration, *copy_enumeration;
3020
3021 enumeration = container_of(type, struct bt_ctf_field_type_enumeration,
3022 parent);
3023
3024 /* Copy the source enumeration's container */
3025 copy_container = bt_ctf_field_type_copy(enumeration->container);
3026 if (!copy_container) {
3027 goto end;
3028 }
3029
3030 copy = bt_ctf_field_type_enumeration_create(copy_container);
3031 if (!copy) {
3032 goto end;
3033 }
3034 copy_enumeration = container_of(copy,
3035 struct bt_ctf_field_type_enumeration, parent);
3036
3037 /* Copy all enumaration entries */
3038 for (i = 0; i < enumeration->entries->len; i++) {
3039 struct enumeration_mapping *mapping = g_ptr_array_index(
3040 enumeration->entries, i);
3041 struct enumeration_mapping* copy_mapping = g_new0(
3042 struct enumeration_mapping, 1);
3043
3044 if (!copy_mapping) {
3045 goto error;
3046 }
3047
3048 *copy_mapping = *mapping;
3049 g_ptr_array_add(copy_enumeration->entries, copy_mapping);
3050 }
3051
3052 copy_enumeration->declaration = enumeration->declaration;
3053end:
3054 if (copy_container) {
3055 bt_ctf_field_type_put(copy_container);
3056 }
3057 return copy;
3058error:
3059 if (copy_container) {
3060 bt_ctf_field_type_put(copy_container);
3061 }
3062 bt_ctf_field_type_put(copy);
3063 return NULL;
3064}
3065
3066static
3067struct bt_ctf_field_type *bt_ctf_field_type_floating_point_copy(
3068 struct bt_ctf_field_type *type)
3069{
3070 struct bt_ctf_field_type *copy;
3071 struct bt_ctf_field_type_floating_point *floating_point, *copy_float;
3072
3073 floating_point = container_of(type,
3074 struct bt_ctf_field_type_floating_point, parent);
3075 copy = bt_ctf_field_type_floating_point_create();
3076 if (!copy) {
3077 goto end;
3078 }
3079
3080 copy_float = container_of(copy,
3081 struct bt_ctf_field_type_floating_point, parent);
3082 copy_float->declaration = floating_point->declaration;
3083 copy_float->sign = floating_point->sign;
3084 copy_float->mantissa = floating_point->mantissa;
3085 copy_float->exp = floating_point->exp;
3086end:
3087 return copy;
3088}
3089
3090static
3091struct bt_ctf_field_type *bt_ctf_field_type_structure_copy(
3092 struct bt_ctf_field_type *type)
3093{
3094 int i;
3095 GHashTableIter iter;
3096 gpointer key, value;
3097 struct bt_ctf_field_type *copy;
3098 struct bt_ctf_field_type_structure *structure, *copy_structure;
3099
3100 structure = container_of(type, struct bt_ctf_field_type_structure,
3101 parent);
3102 copy = bt_ctf_field_type_structure_create();
3103 if (!copy) {
3104 goto end;
3105 }
3106
3107 copy_structure = container_of(copy,
3108 struct bt_ctf_field_type_structure, parent);
3109
3110 /* Copy field_name_to_index */
3111 g_hash_table_iter_init(&iter, structure->field_name_to_index);
3112 while (g_hash_table_iter_next (&iter, &key, &value)) {
3113 g_hash_table_insert(copy_structure->field_name_to_index,
3114 key, value);
3115 }
3116
3117 for (i = 0; i < structure->fields->len; i++) {
3118 struct structure_field *entry, *copy_entry;
3119 struct bt_ctf_field_type *copy_field;
3120
3121 copy_entry = g_new0(struct structure_field, 1);
3122 if (!copy_entry) {
3123 goto error;
3124 }
3125
3126 entry = g_ptr_array_index(structure->fields, i);
3127 copy_field = bt_ctf_field_type_copy(entry->type);
3128 if (!copy_field) {
3129 g_free(copy_entry);
3130 goto error;
3131 }
3132
3133 copy_entry->name = entry->name;
3134 copy_entry->type = copy_field;
3135 g_ptr_array_add(copy_structure->fields, copy_entry);
3136 }
3137
3138 copy_structure->declaration = structure->declaration;
3139end:
3140 return copy;
3141error:
3142 bt_ctf_field_type_put(copy);
3143 return NULL;
3144}
3145
3146static
3147struct bt_ctf_field_type *bt_ctf_field_type_variant_copy(
3148 struct bt_ctf_field_type *type)
3149{
3150 int i;
3151 GHashTableIter iter;
3152 gpointer key, value;
3153 struct bt_ctf_field_type *copy = NULL, *copy_tag = NULL;
3154 struct bt_ctf_field_type_variant *variant, *copy_variant;
3155
3156 variant = container_of(type, struct bt_ctf_field_type_variant,
3157 parent);
3158 if (variant->tag) {
3159 copy_tag = bt_ctf_field_type_copy(&variant->tag->parent);
3160 if (!copy_tag) {
3161 goto end;
3162 }
3163 }
3164
3165 copy = bt_ctf_field_type_variant_create(copy_tag,
3166 variant->tag_name->len ? variant->tag_name->str : NULL);
3167 if (!copy) {
3168 goto end;
3169 }
3170
3171 copy_variant = container_of(copy, struct bt_ctf_field_type_variant,
3172 parent);
3173
3174 /* Copy field_name_to_index */
3175 g_hash_table_iter_init(&iter, variant->field_name_to_index);
3176 while (g_hash_table_iter_next (&iter, &key, &value)) {
3177 g_hash_table_insert(copy_variant->field_name_to_index,
3178 key, value);
3179 }
3180
3181 for (i = 0; i < variant->fields->len; i++) {
3182 struct structure_field *entry, *copy_entry;
3183 struct bt_ctf_field_type *copy_field;
3184
3185 copy_entry = g_new0(struct structure_field, 1);
3186 if (!copy_entry) {
3187 goto error;
3188 }
3189
3190 entry = g_ptr_array_index(variant->fields, i);
3191 copy_field = bt_ctf_field_type_copy(entry->type);
3192 if (!copy_field) {
3193 g_free(copy_entry);
3194 goto error;
3195 }
3196
3197 copy_entry->name = entry->name;
3198 copy_entry->type = copy_field;
3199 g_ptr_array_add(copy_variant->fields, copy_entry);
3200 }
3201
3202 copy_variant->declaration = variant->declaration;
4a1e8671
JG
3203 if (variant->tag_path) {
3204 copy_variant->tag_path = bt_ctf_field_path_copy(
3205 variant->tag_path);
3206 if (!copy_variant->tag_path) {
3207 goto error;
3208 }
3209 }
24724933
JG
3210end:
3211 if (copy_tag) {
3212 bt_ctf_field_type_put(copy_tag);
3213 }
3214
3215 return copy;
3216error:
3217 if (copy_tag) {
3218 bt_ctf_field_type_put(copy_tag);
3219 }
3220
3221 bt_ctf_field_type_put(copy);
3222 return NULL;
3223}
3224
3225static
3226struct bt_ctf_field_type *bt_ctf_field_type_array_copy(
3227 struct bt_ctf_field_type *type)
3228{
3229 struct bt_ctf_field_type *copy = NULL, *copy_element;
3230 struct bt_ctf_field_type_array *array, *copy_array;
3231
3232 array = container_of(type, struct bt_ctf_field_type_array,
3233 parent);
3234 copy_element = bt_ctf_field_type_copy(array->element_type);
3235 if (!copy_element) {
3236 goto end;
3237 }
3238
3239 copy = bt_ctf_field_type_array_create(copy_element, array->length);
3240 if (!copy) {
3241 goto end;
3242 }
3243
3244 copy_array = container_of(copy, struct bt_ctf_field_type_array,
3245 parent);
3246 copy_array->declaration = array->declaration;
3247end:
3248 if (copy_element) {
3249 bt_ctf_field_type_put(copy_element);
3250 }
3251
3252 return copy;
3253}
3254
3255static
3256struct bt_ctf_field_type *bt_ctf_field_type_sequence_copy(
3257 struct bt_ctf_field_type *type)
3258{
3259 struct bt_ctf_field_type *copy = NULL, *copy_element;
3260 struct bt_ctf_field_type_sequence *sequence, *copy_sequence;
3261
3262 sequence = container_of(type, struct bt_ctf_field_type_sequence,
3263 parent);
3264 copy_element = bt_ctf_field_type_copy(sequence->element_type);
3265 if (!copy_element) {
3266 goto end;
3267 }
3268
3269 copy = bt_ctf_field_type_sequence_create(copy_element,
3270 sequence->length_field_name->len ?
3271 sequence->length_field_name->str : NULL);
3272 if (!copy) {
3273 goto end;
3274 }
3275
3276 copy_sequence = container_of(copy, struct bt_ctf_field_type_sequence,
3277 parent);
3278 copy_sequence->declaration = sequence->declaration;
aa4e271c
JG
3279 if (sequence->length_field_path) {
3280 copy_sequence->length_field_path = bt_ctf_field_path_copy(
3281 sequence->length_field_path);
3282 if (!copy_sequence->length_field_path) {
3283 goto error;
3284 }
3285 }
24724933
JG
3286end:
3287 if (copy_element) {
3288 bt_ctf_field_type_put(copy_element);
3289 }
3290
3291 return copy;
aa4e271c
JG
3292error:
3293 if (copy) {
3294 bt_ctf_field_type_put(copy);
3295 copy = NULL;
3296 }
3297 goto end;
24724933
JG
3298}
3299
3300static
3301struct bt_ctf_field_type *bt_ctf_field_type_string_copy(
3302 struct bt_ctf_field_type *type)
3303{
3304 struct bt_ctf_field_type *copy;
3305 struct bt_ctf_field_type_string *string, *copy_string;
3306
3307 copy = bt_ctf_field_type_string_create();
3308 if (!copy) {
3309 goto end;
3310 }
3311
3312 string = container_of(type, struct bt_ctf_field_type_string,
3313 parent);
3314 copy_string = container_of(type, struct bt_ctf_field_type_string,
3315 parent);
3316 copy_string->declaration = string->declaration;
3317end:
3318 return copy;
3319}
This page took 0.21205 seconds and 4 git commands to generate.